Răsfoiți Sursa

11 Dialogue Node Types...
Please welcome the ConditionalBranch.
Branch was renamed to the fucking Switch that it is.

Reddo 5 ani în urmă
părinte
comite
b0714751cc
1 a modificat fișierele cu 149 adăugiri și 3 ștergeri
  1. 149 3
      tools/dialogger/dialogger.js

+ 149 - 3
tools/dialogger/dialogger.js

@@ -14,6 +14,7 @@ var con =
 		['dialogue.Text', 'dialogue.Function'],
 		['dialogue.Text', 'dialogue.Tree'],
 		['dialogue.Text', 'dialogue.GoToLabel'],
+		['dialogue.Text', 'dialogue.ConditionalBranch'],
 		['dialogue.Node', 'dialogue.Text'],
 		['dialogue.Node', 'dialogue.Node'],
 		['dialogue.Node', 'dialogue.Choice'],
@@ -22,6 +23,7 @@ var con =
 		['dialogue.Node', 'dialogue.Function'],
 		['dialogue.Node', 'dialogue.Tree'],
 		['dialogue.Node', 'dialogue.GoToLabel'],
+		['dialogue.Node', 'dialogue.ConditionalBranch'],
 		['dialogue.Tree', 'dialogue.Node'],
 		['dialogue.Tree', 'dialogue.Text'],
 		['dialogue.Tree', 'dialogue.Choice'],
@@ -30,6 +32,7 @@ var con =
 		['dialogue.Tree', 'dialogue.Function'],
 		['dialogue.Tree', 'dialogue.Tree'],
 		['dialogue.Tree', 'dialogue.GoToLabel'],
+		['dialogue.Tree', 'dialogue.ConditionalBranch'],
 		['dialogue.StartNode', 'dialogue.Text'],
 		['dialogue.StartNode', 'dialogue.Node'],
 		['dialogue.StartNode', 'dialogue.Choice'],
@@ -38,6 +41,7 @@ var con =
 		['dialogue.StartNode', 'dialogue.Function'],
 		['dialogue.StartNode', 'dialogue.Tree'],
 		['dialogue.StartNode', 'dialogue.GoToLabel'],
+		['dialogue.StartNode', 'dialogue.ConditionalBranch'],
 		['dialogue.Choice', 'dialogue.Text'],
 		['dialogue.Choice', 'dialogue.Node'],
 		['dialogue.Choice', 'dialogue.Set'],
@@ -45,6 +49,7 @@ var con =
 		['dialogue.Choice', 'dialogue.Function'],
 		['dialogue.Choice', 'dialogue.Tree'],
 		['dialogue.Choice', 'dialogue.GoToLabel'],
+		['dialogue.Choice', 'dialogue.ConditionalBranch'],
 		['dialogue.Set', 'dialogue.Text'],
 		['dialogue.Set', 'dialogue.Node'],
 		['dialogue.Set', 'dialogue.Set'],
@@ -52,6 +57,7 @@ var con =
 		['dialogue.Set', 'dialogue.Function'],
 		['dialogue.Set', 'dialogue.Tree'],
 		['dialogue.Set', 'dialogue.GoToLabel'],
+		['dialogue.Set', 'dialogue.ConditionalBranch'],
 		['dialogue.Function', 'dialogue.Text'],
 		['dialogue.Function', 'dialogue.Node'],
 		['dialogue.Function', 'dialogue.Set'],
@@ -59,6 +65,7 @@ var con =
 		['dialogue.Function', 'dialogue.Function'],
 		['dialogue.Function', 'dialogue.Tree'],
 		['dialogue.Function', 'dialogue.GoToLabel'],
+		['dialogue.Function', 'dialogue.ConditionalBranch'],
 		['dialogue.Branch', 'dialogue.Text'],
 		['dialogue.Branch', 'dialogue.Node'],
 		['dialogue.Branch', 'dialogue.Set'],
@@ -66,6 +73,15 @@ var con =
 		['dialogue.Branch', 'dialogue.Function'],
 		['dialogue.Branch', 'dialogue.Tree'],
 		['dialogue.Branch', 'dialogue.GoToLabel'],
+		['dialogue.Branch', 'dialogue.ConditionalBranch'],
+		['dialogue.ConditionalBranch', 'dialogue.Text'],
+		['dialogue.ConditionalBranch', 'dialogue.Node'],
+		['dialogue.ConditionalBranch', 'dialogue.Set'],
+		['dialogue.ConditionalBranch', 'dialogue.ConditionalBranch'],
+		['dialogue.ConditionalBranch', 'dialogue.Branch'],
+		['dialogue.ConditionalBranch', 'dialogue.Function'],
+		['dialogue.ConditionalBranch', 'dialogue.Tree'],
+		['dialogue.ConditionalBranch', 'dialogue.GoToLabel'],
 	],
 
 	default_link: new joint.dia.Link(
@@ -381,6 +397,112 @@ joint.shapes.dialogue.BranchView = joint.shapes.dialogue.BaseView.extend(
 	},
 });
 
+joint.shapes.dialogue.ConditionalBranch = joint.shapes.devs.Model.extend(
+	{
+		defaults: joint.util.deepSupplement
+		(
+			{
+				type: 'dialogue.ConditionalBranch',
+				size: { width: 200, height: 55, },
+				inPorts: ['input'],
+				outPorts: ['output0'],
+				values: [],
+			},
+			joint.shapes.dialogue.Base.prototype.defaults
+		),
+	});
+joint.shapes.dialogue.ConditionalBranchView = joint.shapes.dialogue.BaseView.extend(
+	{
+		template:
+			[
+				'<div class="node">',
+				'<span class="label"></span>',
+				'<button class="delete">x</button>',
+				'<button class="add">+</button>',
+				'<button class="remove">-</button>',
+				'<input type="text" value="Default" readonly/>',
+				'</div>',
+			].join(''),
+
+		initialize: function()
+		{
+			joint.shapes.dialogue.BaseView.prototype.initialize.apply(this, arguments);
+			this.$box.find('.add').on('click', _.bind(this.addPort, this));
+			this.$box.find('.remove').on('click', _.bind(this.removePort, this));
+		},
+
+		removePort: function()
+		{
+			if (this.model.get('outPorts').length > 1)
+			{
+				var outPorts = this.model.get('outPorts').slice(0);
+				outPorts.pop();
+				this.model.set('outPorts', outPorts);
+				var values = this.model.get('values').slice(0);
+				values.pop();
+				this.model.set('values', values);
+				this.updateSize();
+			}
+		},
+
+		addPort: function()
+		{
+			var outPorts = this.model.get('outPorts').slice(0);
+			outPorts.push('output' + outPorts.length.toString());
+			this.model.set('outPorts', outPorts);
+			var values = this.model.get('values').slice(0);
+			values.push(null);
+			this.model.set('values', values);
+			this.updateSize();
+		},
+
+		updateBox: function()
+		{
+			joint.shapes.dialogue.BaseView.prototype.updateBox.apply(this, arguments);
+			var values = this.model.get('values');
+			var valueFields = this.$box.find('input.value');
+
+			// Add value fields if necessary
+			for (var i = valueFields.length; i < values.length; i++)
+			{
+				// Prevent paper from handling pointerdown.
+				var $field = $('<input type="text" class="value" />');
+				$field.attr('placeholder', 'Condition ' + (i + 1).toString());
+				$field.attr('index', i);
+				this.$box.append($field);
+				$field.on('mousedown click', function(evt) { evt.stopPropagation(); });
+
+				// This is an example of reacting on the input change and storing the input data in the cell model.
+				$field.on('change', _.bind(function(evt)
+				{
+					var values = this.model.get('values').slice(0);
+					values[$(evt.target).attr('index')] = $(evt.target).val();
+					this.model.set('values', values);
+				}, this));
+			}
+
+			// Remove value fields if necessary
+			for (var i = values.length; i < valueFields.length; i++)
+				$(valueFields[i]).remove();
+
+			// Update value fields
+			valueFields = this.$box.find('input.value');
+			for (var i = 0; i < valueFields.length; i++)
+			{
+				var field = $(valueFields[i]);
+				if (!field.is(':focus'))
+					field.val(values[i]);
+			}
+		},
+
+		updateSize: function()
+		{
+			var textField = this.$box.find('input');
+			var height = textField.outerHeight(true);
+			this.model.set('size', { width: 200, height: 95 + Math.max(0, (this.model.get('outPorts').length - 1) * height) });
+		},
+	});
+
 
 joint.shapes.dialogue.StartNode = joint.shapes.devs.Model.extend(
 {
@@ -692,6 +814,15 @@ func.optimized_data = function()
 					node.branches[branch] = null;
 				}
 			}
+			else if (node.type === 'ConditionalBranch')
+			{
+				node.branches = {};
+				for (var j = 0; j < cell.values.length; j++)
+				{
+					var branch = cell.values[j];
+					node.branches[branch] = null;
+				}
+			}
 			else if (node.type === 'Set')
 			{
 				node.variable = cell.name;
@@ -958,12 +1089,26 @@ func.ts_data = function(nodes, name) {
 			}
 			finalString += "tree.addNode(branch);\n";
 			addDeclaration("let branch : DialogueBranch;\n");
+		} else if (node.type == "ConditionalBranch") {
+			finalString += "branch = new DialogueBranch(" + JSON.stringify(node.id) + ");\n";
+			//finalString += "branch.setVariable(() => { return " + node.variable + ";});\n";
+			for (let key in node.branches) {
+				let branchCondition = key;
+				let branchTarget = node.branches[key];
+				if (branchCondition == "_default") {
+					finalString += "branch.setNext(" + JSON.stringify(branchTarget) + ");\n";
+				} else {
+					finalString += "branch.addBranch(" + JSON.stringify(branchTarget) + ", () => { return " + branchCondition + ";});\n";
+				}
+			}
+			finalString += "tree.addNode(branch);\n";
+			addDeclaration("let branch : DialogueBranch;\n");
 		} else if (node.type == "Set" || node.type == "Function") {
 			finalString += "set = new DialogueSet(" + JSON.stringify(node.id) + ");\n";
 			if (node.type == "Function") {
-				finalString += "set.setFunction(() => {" + node.name + "});\n";
+				finalString += "set.setFunction(() => {\n" + node.name + "\n});\n";
 			} else {
-				finalString += "set.setFunction(() => { " + node.variable + " = " + node.value + ";});\n";
+				finalString += "set.setFunction(() => {\n " + node.variable + " = " + node.value + ";\n});\n";
 			}
 			if (node.next != null && node.next != undefined) {
 				finalString += "set.setNext(" + JSON.stringify(node.next) + ");\n";
@@ -1148,7 +1293,8 @@ func.ts_data = function(nodes, name) {
 	state.menu = new nw.Menu();
 	state.menu.append(new nw.MenuItem({ label: 'Text', click: func.add_node(joint.shapes.dialogue.Text) }));
 	state.menu.append(new nw.MenuItem({ label: 'Choice', click: func.add_node(joint.shapes.dialogue.Choice) }));
-	state.menu.append(new nw.MenuItem({ label: 'Branch', click: func.add_node(joint.shapes.dialogue.Branch) }));
+	state.menu.append(new nw.MenuItem({ label: 'Switch', click: func.add_node(joint.shapes.dialogue.Branch) }));
+	state.menu.append(new nw.MenuItem({ label: 'Branch', click: func.add_node(joint.shapes.dialogue.ConditionalBranch) }));
 	state.menu.append(new nw.MenuItem({ label: 'Set', click: func.add_node(joint.shapes.dialogue.Set) }));
 	state.menu.append(new nw.MenuItem({ label: 'Function', click: func.add_node(joint.shapes.dialogue.Function) }));
 	state.menu.append(new nw.MenuItem({ label: 'GoToLabel', click: func.add_node(joint.shapes.dialogue.GoToLabel) }));