1 | using System;
|
---|
2 | using Bridge;
|
---|
3 | using Bridge.Html5;
|
---|
4 |
|
---|
5 | namespace WebMrbc
|
---|
6 | {
|
---|
7 | public class SwitchCaseNumberBlock : Block
|
---|
8 | {
|
---|
9 | public const string type_name = "switch_case_number";
|
---|
10 |
|
---|
11 | internal Tuple<string, string>[] cases_;
|
---|
12 | internal int defaultCount_;
|
---|
13 |
|
---|
14 | public SwitchCaseNumberBlock()
|
---|
15 | : base(type_name)
|
---|
16 | {
|
---|
17 | }
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | /// Block for swicth/case/default condition.
|
---|
21 | /// </summary>
|
---|
22 | public void init()
|
---|
23 | {
|
---|
24 | setHelpUrl("http://www.example.com/");
|
---|
25 | setColour(210);
|
---|
26 | appendValueInput("SWITCH")
|
---|
27 | .appendField("右の値が");
|
---|
28 | setPreviousStatement(true);
|
---|
29 | setNextStatement(true);
|
---|
30 | setMutator(new Mutator(new[] {
|
---|
31 | SwitchCaseNumberConstBlock.type_name,
|
---|
32 | SwitchCaseNumberRangeBlock.type_name,
|
---|
33 | SwitchCaseNumberDefaultBlock.type_name }));
|
---|
34 | setTooltip(new Func<string>(() => {
|
---|
35 | if ((cases_.Length == 0) && (defaultCount_ == 0)) {
|
---|
36 | return "条件に合うブロックを実行";
|
---|
37 | }
|
---|
38 | else if ((cases_.Length == 0) && (defaultCount_ != 0)) {
|
---|
39 | return "条件に合うブロックを実行、合うものがなければ最後のブロックを実行";
|
---|
40 | }
|
---|
41 | else if ((cases_.Length != 0) && (defaultCount_ == 0)) {
|
---|
42 | return "条件に合うブロックを実行";
|
---|
43 | }
|
---|
44 | else if ((cases_.Length != 0) && (defaultCount_ != 0)) {
|
---|
45 | return "条件に合うブロックを実行、合うものがなければ最後のブロックを実行";
|
---|
46 | }
|
---|
47 | return "";
|
---|
48 | }));
|
---|
49 | cases_ = new Tuple<string, string>[] { new Tuple<string, string>("0", null) };
|
---|
50 | defaultCount_ = 0;
|
---|
51 | updateShape_();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// Create XML to represent the number of else-if and else inputs.
|
---|
56 | /// </summary>
|
---|
57 | /// <returns>XML storage element.</returns>
|
---|
58 | public Element mutationToDom(bool opt_caseIds)
|
---|
59 | {
|
---|
60 | if ((cases_.Length == 0) && (defaultCount_ == 0)) {
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 | var container = Document.CreateElement("mutation");
|
---|
64 | for (var i = 0; i < cases_.Length; i++) {
|
---|
65 | Element caseInfo;
|
---|
66 | var value = getFieldValue("CONST" + i);
|
---|
67 | if (value != null) {
|
---|
68 | caseInfo = Document.CreateElement("case");
|
---|
69 | caseInfo.SetAttribute("value", value);
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | var min = getFieldValue("RANGE_MIN" + i);
|
---|
73 | var max = getFieldValue("RANGE_MAX" + i);
|
---|
74 | if (min != null && max != null) {
|
---|
75 | caseInfo = Document.CreateElement("case");
|
---|
76 | caseInfo.SetAttribute("value", min + ".." + max);
|
---|
77 | }
|
---|
78 | else
|
---|
79 | continue;
|
---|
80 | }
|
---|
81 | container.AppendChild(caseInfo);
|
---|
82 | }
|
---|
83 | container.SetAttribute("default", defaultCount_.ToString());
|
---|
84 | return container;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <summary>
|
---|
88 | /// Parse XML to restore the else-if and else inputs.
|
---|
89 | /// </summary>
|
---|
90 | /// <param name="xmlElement">XML storage element.</param>
|
---|
91 | public void domToMutation(Element xmlElement)
|
---|
92 | {
|
---|
93 | cases_ = new Tuple<string, string>[0];
|
---|
94 | Element childNode;
|
---|
95 | for (var i = 0; (childNode = (dynamic)xmlElement.ChildNodes[i]) != null; i++) {
|
---|
96 | if (childNode.NodeName.ToLower() == "case") {
|
---|
97 | var value = childNode.GetAttribute("value");
|
---|
98 | if (value != null)
|
---|
99 | cases_.Push(new Tuple<string, string>(value, null));
|
---|
100 | else {
|
---|
101 | var min = childNode.GetAttribute("minimum");
|
---|
102 | var max = childNode.GetAttribute("maximum");
|
---|
103 | if ((min != null) && (max != null)) {
|
---|
104 | cases_.Push(new Tuple<string, string>(min, max));
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | var count = xmlElement.GetAttribute("default");
|
---|
110 | defaultCount_ = count == null ? 0 : Bridge.Script.ParseInt(count, 10);
|
---|
111 | updateShape_();
|
---|
112 | }
|
---|
113 |
|
---|
114 | /// <summary>
|
---|
115 | /// Populate the mutator's dialog with this block's components.
|
---|
116 | /// </summary>
|
---|
117 | /// <param name="workspace">Mutator's workspace.</param>
|
---|
118 | /// <returns>Root block in mutator.</returns>
|
---|
119 | public Block decompose(Workspace workspace)
|
---|
120 | {
|
---|
121 | var containerBlock = workspace.newBlock(SwitchCaseNumberContainerBlock.type_name);
|
---|
122 | containerBlock.initSvg();
|
---|
123 | var connection = containerBlock.getInput("STACK").connection;
|
---|
124 | for (var i = 0; i < cases_.Length; i++) {
|
---|
125 | Block caseBlock;
|
---|
126 | var value = getFieldValue("CONST" + i);
|
---|
127 | if (value != null) {
|
---|
128 | caseBlock = workspace.newBlock(SwitchCaseNumberConstBlock.type_name);
|
---|
129 | caseBlock.setFieldValue(value, "CONST");
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | var min = getFieldValue("RANGE_MIN" + i);
|
---|
133 | var max = getFieldValue("RANGE_MAX" + i);
|
---|
134 | if ((min != null) && (max != null)) {
|
---|
135 | caseBlock = workspace.newBlock(SwitchCaseNumberRangeBlock.type_name);
|
---|
136 | caseBlock.setFieldValue(min, "RANGE_MIN");
|
---|
137 | caseBlock.setFieldValue(max, "RANGE_MAX");
|
---|
138 | }
|
---|
139 | else
|
---|
140 | continue;
|
---|
141 | }
|
---|
142 | caseBlock.initSvg();
|
---|
143 | connection.connect(caseBlock.previousConnection);
|
---|
144 | connection = caseBlock.nextConnection;
|
---|
145 | }
|
---|
146 | if (defaultCount_ != 0) {
|
---|
147 | var defaultBlock = workspace.newBlock(SwitchCaseNumberDefaultBlock.type_name);
|
---|
148 | defaultBlock.initSvg();
|
---|
149 | connection.connect(defaultBlock.previousConnection);
|
---|
150 | }
|
---|
151 | return containerBlock;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /// <summary>
|
---|
155 | /// Reconfigure this block based on the mutator dialog's components.
|
---|
156 | /// </summary>
|
---|
157 | /// <param name="containerBlock">Root block in mutator.</param>
|
---|
158 | public void compose(Block containerBlock)
|
---|
159 | {
|
---|
160 | var clauseBlock = containerBlock.getInputTargetBlock("STACK");
|
---|
161 | // Count number of inputs.
|
---|
162 | cases_ = new Tuple<string, string>[0];
|
---|
163 | defaultCount_ = 0;
|
---|
164 | var statementConnections = new Connection[0];
|
---|
165 | Connection defaultStatementConnection = null;
|
---|
166 | while (clauseBlock != null) {
|
---|
167 | switch (clauseBlock.type) {
|
---|
168 | case SwitchCaseNumberConstBlock.type_name: {
|
---|
169 | var value = clauseBlock.getFieldValue("CONST");
|
---|
170 | cases_.Push(new Tuple<string, string>(value, null));
|
---|
171 | statementConnections.Push(((SwitchCaseNumberConstBlock)clauseBlock).statementConnection_);
|
---|
172 | }
|
---|
173 | break;
|
---|
174 | case SwitchCaseNumberRangeBlock.type_name: {
|
---|
175 | var range_min = clauseBlock.getFieldValue("RANGE_MIN");
|
---|
176 | var range_max = clauseBlock.getFieldValue("RANGE_MAX");
|
---|
177 | cases_.Push(new Tuple<string, string>(range_min, range_max));
|
---|
178 | statementConnections.Push(((SwitchCaseNumberRangeBlock)clauseBlock).statementConnection_);
|
---|
179 | }
|
---|
180 | break;
|
---|
181 | case SwitchCaseNumberDefaultBlock.type_name: {
|
---|
182 | defaultCount_++;
|
---|
183 | defaultStatementConnection = ((SwitchCaseNumberDefaultBlock)clauseBlock).statementConnection_;
|
---|
184 | }
|
---|
185 | break;
|
---|
186 | default:
|
---|
187 | throw new Exception("Unknown block type.");
|
---|
188 | }
|
---|
189 | clauseBlock = (clauseBlock.nextConnection != null) ?
|
---|
190 | clauseBlock.nextConnection.targetBlock() : null;
|
---|
191 | }
|
---|
192 | updateShape_();
|
---|
193 | // Reconnect any child blocks.
|
---|
194 | for (var i = 0; i < cases_.Length; i++) {
|
---|
195 | Mutator.reconnect(statementConnections[i], this, "DO" + i);
|
---|
196 | }
|
---|
197 | Mutator.reconnect(defaultStatementConnection, this, "DEFAULT_DO");
|
---|
198 | }
|
---|
199 |
|
---|
200 | /// <summary>
|
---|
201 | /// Store pointers to any connected child blocks.
|
---|
202 | /// </summary>
|
---|
203 | /// <param name="containerBlock">Root block in mutator.</param>
|
---|
204 | public void saveConnections(Block containerBlock)
|
---|
205 | {
|
---|
206 | var clauseBlock = containerBlock.getInputTargetBlock("STACK");
|
---|
207 | var i = 0;
|
---|
208 | while (clauseBlock != null) {
|
---|
209 | switch (clauseBlock.type) {
|
---|
210 | case SwitchCaseNumberConstBlock.type_name: {
|
---|
211 | var inputDo = getInput("DO" + i);
|
---|
212 | ((SwitchCaseNumberConstBlock)clauseBlock).statementConnection_ =
|
---|
213 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
214 | i++;
|
---|
215 | }
|
---|
216 | break;
|
---|
217 | case SwitchCaseNumberRangeBlock.type_name: {
|
---|
218 | var inputDo = getInput("DO" + i);
|
---|
219 | ((SwitchCaseNumberRangeBlock)clauseBlock).statementConnection_ =
|
---|
220 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
221 | i++;
|
---|
222 | }
|
---|
223 | break;
|
---|
224 | case SwitchCaseNumberDefaultBlock.type_name: {
|
---|
225 | var inputDo = getInput("DEFAULT_DO");
|
---|
226 | ((SwitchCaseNumberDefaultBlock)clauseBlock).statementConnection_ =
|
---|
227 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
228 | }
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | throw new Exception("Unknown block type.");
|
---|
232 | }
|
---|
233 |
|
---|
234 | clauseBlock = (clauseBlock.nextConnection != null) ?
|
---|
235 | clauseBlock.nextConnection.targetBlock() : null;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | /// <summary>
|
---|
240 | /// Modify this block to have the correct number of inputs.
|
---|
241 | /// </summary>
|
---|
242 | private void updateShape_()
|
---|
243 | {
|
---|
244 | // Delete everything.
|
---|
245 | if (getInput("DEFAULT") != null) {
|
---|
246 | removeInput("DEFAULT");
|
---|
247 | removeInput("DEFAULT_DO");
|
---|
248 | }
|
---|
249 | var i = 0;
|
---|
250 | while (getInput("CASE" + i) != null) {
|
---|
251 | removeInput("CASE" + i);
|
---|
252 | removeInput("DO" + i);
|
---|
253 | i++;
|
---|
254 | }
|
---|
255 | // Rebuild block.
|
---|
256 | i = 0;
|
---|
257 | foreach (var c in cases_) {
|
---|
258 | if (c.Item2 == null) {
|
---|
259 | appendDummyInput("CASE" + i)
|
---|
260 | .appendField(new FieldNumber(c.Item1, "-Infinity", "Infinity", 0), "CONST" + i)
|
---|
261 | .appendField("の");
|
---|
262 | }
|
---|
263 | else {
|
---|
264 | appendDummyInput("CASE" + i)
|
---|
265 | .appendField(new FieldNumber(c.Item1, "-Infinity", "Infinity", 0), "RANGE_MIN" + i)
|
---|
266 | .appendField("から")
|
---|
267 | .appendField(new FieldNumber(c.Item2, "-Infinity", "Infinity", 0), "RANGE_MAX" + i)
|
---|
268 | .appendField("の");
|
---|
269 | }
|
---|
270 | appendStatementInput("DO" + i)
|
---|
271 | .appendField("とき");
|
---|
272 | i++;
|
---|
273 | }
|
---|
274 | if (defaultCount_ != 0) {
|
---|
275 | appendDummyInput("DEFAULT")
|
---|
276 | .appendField("その他の");
|
---|
277 | appendStatementInput("DEFAULT_DO")
|
---|
278 | .appendField("とき");
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public class SwitchCaseNumberContainerBlock : Block
|
---|
284 | {
|
---|
285 | public const string type_name = "switch_case_number_container";
|
---|
286 |
|
---|
287 | public SwitchCaseNumberContainerBlock()
|
---|
288 | : base(type_name)
|
---|
289 | {
|
---|
290 | }
|
---|
291 |
|
---|
292 | public void init()
|
---|
293 | {
|
---|
294 | appendDummyInput()
|
---|
295 | .appendField("条件");
|
---|
296 | appendStatementInput("STACK");
|
---|
297 | setColour(210);
|
---|
298 | setTooltip("");
|
---|
299 | contextMenu = false;
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | public class SwitchCaseNumberConstBlock : Block
|
---|
304 | {
|
---|
305 | public const string type_name = "switch_case_number_const";
|
---|
306 | public Connection statementConnection_;
|
---|
307 |
|
---|
308 | public SwitchCaseNumberConstBlock()
|
---|
309 | : base(type_name)
|
---|
310 | {
|
---|
311 | }
|
---|
312 |
|
---|
313 | /// <summary>
|
---|
314 | /// Block for swicth/case/default condition.
|
---|
315 | /// </summary>
|
---|
316 | public void init()
|
---|
317 | {
|
---|
318 | setColour(210);
|
---|
319 | appendDummyInput()
|
---|
320 | .appendField("固定値")
|
---|
321 | .appendField("0", "CONST");
|
---|
322 | setPreviousStatement(true);
|
---|
323 | setNextStatement(true);
|
---|
324 | setTooltip("固定値の条件");
|
---|
325 | contextMenu = false;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | public class SwitchCaseNumberRangeBlock : Block
|
---|
330 | {
|
---|
331 | public const string type_name = "switch_case_number_range";
|
---|
332 | public Connection statementConnection_;
|
---|
333 |
|
---|
334 | public SwitchCaseNumberRangeBlock()
|
---|
335 | : base(type_name)
|
---|
336 | {
|
---|
337 | }
|
---|
338 |
|
---|
339 | /// <summary>
|
---|
340 | /// Block for swicth/case/default condition.
|
---|
341 | /// </summary>
|
---|
342 | public void init()
|
---|
343 | {
|
---|
344 | setColour(210);
|
---|
345 | appendDummyInput()
|
---|
346 | .appendField("範囲")
|
---|
347 | .appendField("1", "RANGE_MIN")
|
---|
348 | .appendField("から")
|
---|
349 | .appendField("2", "RANGE_MAX");
|
---|
350 | setPreviousStatement(true);
|
---|
351 | setNextStatement(true);
|
---|
352 | setTooltip("範囲の条件");
|
---|
353 | contextMenu = false;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | public class SwitchCaseNumberDefaultBlock : Block
|
---|
358 | {
|
---|
359 | public const string type_name = "switch_case_number_default";
|
---|
360 | public Connection statementConnection_;
|
---|
361 |
|
---|
362 | public SwitchCaseNumberDefaultBlock()
|
---|
363 | : base(type_name)
|
---|
364 | {
|
---|
365 | }
|
---|
366 |
|
---|
367 | /// <summary>
|
---|
368 | /// Block for swicth/case/default condition.
|
---|
369 | /// </summary>
|
---|
370 | public void init()
|
---|
371 | {
|
---|
372 | setColour(210);
|
---|
373 | appendDummyInput()
|
---|
374 | .appendField("その他");
|
---|
375 | setPreviousStatement(true);
|
---|
376 | setTooltip("条件に当てはまらなかった場合");
|
---|
377 | contextMenu = false;
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | public class SwitchCaseTextBlock : Block
|
---|
382 | {
|
---|
383 | public const string type_name = "switch_case_text";
|
---|
384 |
|
---|
385 | internal Tuple<string, string>[] cases_;
|
---|
386 | internal int defaultCount_;
|
---|
387 |
|
---|
388 | public SwitchCaseTextBlock()
|
---|
389 | : base(type_name)
|
---|
390 | {
|
---|
391 | }
|
---|
392 |
|
---|
393 | /// <summary>
|
---|
394 | /// Block for swicth/case/default condition.
|
---|
395 | /// </summary>
|
---|
396 | public void init()
|
---|
397 | {
|
---|
398 | setHelpUrl("http://www.example.com/");
|
---|
399 | setColour(210);
|
---|
400 | appendValueInput("SWITCH")
|
---|
401 | .appendField("右の値が");
|
---|
402 | setPreviousStatement(true);
|
---|
403 | setNextStatement(true);
|
---|
404 | setMutator(new Mutator(new[] {
|
---|
405 | SwitchCaseTextConstBlock.type_name,
|
---|
406 | SwitchCaseTextRangeBlock.type_name,
|
---|
407 | SwitchCaseTextDefaultBlock.type_name }));
|
---|
408 | setTooltip(new Func<string>(() => {
|
---|
409 | if ((cases_.Length == 0) && (defaultCount_ == 0)) {
|
---|
410 | return "条件に合うブロックを実行";
|
---|
411 | }
|
---|
412 | else if ((cases_.Length == 0) && (defaultCount_ != 0)) {
|
---|
413 | return "条件に合うブロックを実行、合うものがなければ最後のブロックを実行";
|
---|
414 | }
|
---|
415 | else if ((cases_.Length != 0) && (defaultCount_ == 0)) {
|
---|
416 | return "条件に合うブロックを実行";
|
---|
417 | }
|
---|
418 | else if ((cases_.Length != 0) && (defaultCount_ != 0)) {
|
---|
419 | return "条件に合うブロックを実行、合うものがなければ最後のブロックを実行";
|
---|
420 | }
|
---|
421 | return "";
|
---|
422 | }));
|
---|
423 | cases_ = new Tuple<string, string>[] { new Tuple<string, string>("0", null) };
|
---|
424 | defaultCount_ = 0;
|
---|
425 | updateShape_();
|
---|
426 | }
|
---|
427 |
|
---|
428 | /// <summary>
|
---|
429 | /// Create XML to represent the text of else-if and else inputs.
|
---|
430 | /// </summary>
|
---|
431 | /// <returns>XML storage element.</returns>
|
---|
432 | public Element mutationToDom(bool opt_caseIds)
|
---|
433 | {
|
---|
434 | if ((cases_.Length == 0) && (defaultCount_ == 0)) {
|
---|
435 | return null;
|
---|
436 | }
|
---|
437 | var container = Document.CreateElement("mutation");
|
---|
438 | for (var i = 0; i < cases_.Length; i++) {
|
---|
439 | Element caseInfo;
|
---|
440 | var value = getFieldValue("CONST" + i);
|
---|
441 | if (value != null) {
|
---|
442 | caseInfo = Document.CreateElement("case");
|
---|
443 | caseInfo.SetAttribute("value", value);
|
---|
444 | }
|
---|
445 | else {
|
---|
446 | var min = getFieldValue("RANGE_MIN" + i);
|
---|
447 | var max = getFieldValue("RANGE_MAX" + i);
|
---|
448 | if (min != null && max != null) {
|
---|
449 | caseInfo = Document.CreateElement("case");
|
---|
450 | caseInfo.SetAttribute("value", min + ".." + max);
|
---|
451 | }
|
---|
452 | else
|
---|
453 | continue;
|
---|
454 | }
|
---|
455 | container.AppendChild(caseInfo);
|
---|
456 | }
|
---|
457 | container.SetAttribute("default", defaultCount_.ToString());
|
---|
458 | return container;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /// <summary>
|
---|
462 | /// Parse XML to restore the else-if and else inputs.
|
---|
463 | /// </summary>
|
---|
464 | /// <param name="xmlElement">XML storage element.</param>
|
---|
465 | public void domToMutation(Element xmlElement)
|
---|
466 | {
|
---|
467 | cases_ = new Tuple<string, string>[0];
|
---|
468 | Element childNode;
|
---|
469 | for (var i = 0; (childNode = (dynamic)xmlElement.ChildNodes[i]) != null; i++) {
|
---|
470 | if (childNode.NodeName.ToLower() == "case") {
|
---|
471 | var value = childNode.GetAttribute("value");
|
---|
472 | if (value != null)
|
---|
473 | cases_.Push(new Tuple<string, string>(value, null));
|
---|
474 | else {
|
---|
475 | var min = childNode.GetAttribute("minimum");
|
---|
476 | var max = childNode.GetAttribute("maximum");
|
---|
477 | if ((min != null) && (max != null))
|
---|
478 | cases_.Push(new Tuple<string, string>(min, max));
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | var count = xmlElement.GetAttribute("default");
|
---|
483 | defaultCount_ = count == null ? 0 : Bridge.Script.ParseInt(count, 10);
|
---|
484 | updateShape_();
|
---|
485 | }
|
---|
486 |
|
---|
487 | /// <summary>
|
---|
488 | /// Populate the mutator's dialog with this block's components.
|
---|
489 | /// </summary>
|
---|
490 | /// <param name="workspace">Mutator's workspace.</param>
|
---|
491 | /// <returns>Root block in mutator.</returns>
|
---|
492 | public Block decompose(Workspace workspace)
|
---|
493 | {
|
---|
494 | var containerBlock = workspace.newBlock(SwitchCaseTextContainerBlock.type_name);
|
---|
495 | containerBlock.initSvg();
|
---|
496 | var connection = containerBlock.getInput("STACK").connection;
|
---|
497 | for (var i = 0; i < cases_.Length; i++) {
|
---|
498 | Block caseBlock;
|
---|
499 | var value = getFieldValue("CONST" + i);
|
---|
500 | if (value != null) {
|
---|
501 | caseBlock = workspace.newBlock(SwitchCaseTextConstBlock.type_name);
|
---|
502 | caseBlock.setFieldValue(value, "CONST");
|
---|
503 | }
|
---|
504 | else {
|
---|
505 | var min = getFieldValue("RANGE_MIN" + i);
|
---|
506 | var max = getFieldValue("RANGE_MAX" + i);
|
---|
507 | if ((min != null) && (max != null)) {
|
---|
508 | caseBlock = workspace.newBlock(SwitchCaseTextRangeBlock.type_name);
|
---|
509 | caseBlock.setFieldValue(min, "RANGE_MIN");
|
---|
510 | caseBlock.setFieldValue(max, "RANGE_MAX");
|
---|
511 | }
|
---|
512 | else
|
---|
513 | continue;
|
---|
514 | }
|
---|
515 | caseBlock.initSvg();
|
---|
516 | connection.connect(caseBlock.previousConnection);
|
---|
517 | connection = caseBlock.nextConnection;
|
---|
518 | }
|
---|
519 | if (defaultCount_ != 0) {
|
---|
520 | var defaultBlock = workspace.newBlock(SwitchCaseTextDefaultBlock.type_name);
|
---|
521 | defaultBlock.initSvg();
|
---|
522 | connection.connect(defaultBlock.previousConnection);
|
---|
523 | }
|
---|
524 | return containerBlock;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /// <summary>
|
---|
528 | /// Reconfigure this block based on the mutator dialog's components.
|
---|
529 | /// </summary>
|
---|
530 | /// <param name="containerBlock">Root block in mutator.</param>
|
---|
531 | public void compose(Block containerBlock)
|
---|
532 | {
|
---|
533 | var clauseBlock = containerBlock.getInputTargetBlock("STACK");
|
---|
534 | // Count text of inputs.
|
---|
535 | cases_ = new Tuple<string, string>[0];
|
---|
536 | defaultCount_ = 0;
|
---|
537 | var statementConnections = new Connection[0];
|
---|
538 | Connection defaultStatementConnection = null;
|
---|
539 | while (clauseBlock != null) {
|
---|
540 | switch (clauseBlock.type) {
|
---|
541 | case SwitchCaseTextConstBlock.type_name: {
|
---|
542 | var value = clauseBlock.getFieldValue("CONST");
|
---|
543 | cases_.Push(new Tuple<string, string>(value, null));
|
---|
544 | statementConnections.Push(((SwitchCaseTextConstBlock)clauseBlock).statementConnection_);
|
---|
545 | }
|
---|
546 | break;
|
---|
547 | case SwitchCaseTextRangeBlock.type_name: {
|
---|
548 | var range_min = clauseBlock.getFieldValue("RANGE_MIN");
|
---|
549 | var range_max = clauseBlock.getFieldValue("RANGE_MAX");
|
---|
550 | cases_.Push(new Tuple<string, string>(range_min, range_max));
|
---|
551 | statementConnections.Push(((SwitchCaseTextRangeBlock)clauseBlock).statementConnection_);
|
---|
552 | }
|
---|
553 | break;
|
---|
554 | case SwitchCaseTextDefaultBlock.type_name: {
|
---|
555 | defaultCount_++;
|
---|
556 | defaultStatementConnection = ((SwitchCaseTextDefaultBlock)clauseBlock).statementConnection_;
|
---|
557 | }
|
---|
558 | break;
|
---|
559 | default:
|
---|
560 | throw new Exception("Unknown block type.");
|
---|
561 | }
|
---|
562 | clauseBlock = (clauseBlock.nextConnection != null) ?
|
---|
563 | clauseBlock.nextConnection.targetBlock() : null;
|
---|
564 | }
|
---|
565 | updateShape_();
|
---|
566 | // Reconnect any child blocks.
|
---|
567 | for (var i = 0; i <= cases_.Length; i++) {
|
---|
568 | Mutator.reconnect(statementConnections[i], this, "DO" + i);
|
---|
569 | }
|
---|
570 | Mutator.reconnect(defaultStatementConnection, this, "DEFAULT_DO");
|
---|
571 | }
|
---|
572 |
|
---|
573 | /// <summary>
|
---|
574 | /// Store pointers to any connected child blocks.
|
---|
575 | /// </summary>
|
---|
576 | /// <param name="containerBlock">Root block in mutator.</param>
|
---|
577 | public void saveConnections(Block containerBlock)
|
---|
578 | {
|
---|
579 | var clauseBlock = containerBlock.getInputTargetBlock("STACK");
|
---|
580 | var i = 0;
|
---|
581 | while (clauseBlock != null) {
|
---|
582 | switch (clauseBlock.type) {
|
---|
583 | case SwitchCaseTextConstBlock.type_name: {
|
---|
584 | var inputDo = getInput("DO" + i);
|
---|
585 | ((SwitchCaseTextConstBlock)clauseBlock).statementConnection_ =
|
---|
586 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
587 | i++;
|
---|
588 | }
|
---|
589 | break;
|
---|
590 | case SwitchCaseTextRangeBlock.type_name: {
|
---|
591 | var inputDo = getInput("DO" + i);
|
---|
592 | ((SwitchCaseTextRangeBlock)clauseBlock).statementConnection_ =
|
---|
593 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
594 | i++;
|
---|
595 | }
|
---|
596 | break;
|
---|
597 | case SwitchCaseTextDefaultBlock.type_name: {
|
---|
598 | var inputDo = getInput("DEFAULT_DO");
|
---|
599 | ((SwitchCaseTextDefaultBlock)clauseBlock).statementConnection_ =
|
---|
600 | (inputDo != null) ? inputDo.connection.targetConnection : null;
|
---|
601 | }
|
---|
602 | break;
|
---|
603 | default:
|
---|
604 | throw new Exception("Unknown block type.");
|
---|
605 | }
|
---|
606 |
|
---|
607 | clauseBlock = (clauseBlock.nextConnection != null) ?
|
---|
608 | clauseBlock.nextConnection.targetBlock() : null;
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /// <summary>
|
---|
613 | /// Modify this block to have the correct text of inputs.
|
---|
614 | /// </summary>
|
---|
615 | private void updateShape_()
|
---|
616 | {
|
---|
617 | // Delete everything.
|
---|
618 | if (getInput("DEFAULT") != null) {
|
---|
619 | removeInput("DEFAULT");
|
---|
620 | removeInput("DEFAULT_DO");
|
---|
621 | }
|
---|
622 | var i = 0;
|
---|
623 | while (getInput("CASE" + i) != null) {
|
---|
624 | removeInput("CASE" + i);
|
---|
625 | removeInput("DO" + i);
|
---|
626 | i++;
|
---|
627 | }
|
---|
628 | // Rebuild block.
|
---|
629 | i = 0;
|
---|
630 | foreach (var c in cases_) {
|
---|
631 | if (c.Item2 == null) {
|
---|
632 | appendDummyInput("CASE" + i)
|
---|
633 | .appendField(new FieldTextInput(c.Item1), "CONST" + i)
|
---|
634 | .appendField("の");
|
---|
635 | }
|
---|
636 | else {
|
---|
637 | appendDummyInput("CASE" + i)
|
---|
638 | .appendField(new FieldTextInput(c.Item1), "RANGE_MIN" + i)
|
---|
639 | .appendField("から")
|
---|
640 | .appendField(new FieldTextInput(c.Item2), "RANGE_MAX" + i)
|
---|
641 | .appendField("の");
|
---|
642 | }
|
---|
643 | appendStatementInput("DO" + i)
|
---|
644 | .appendField("とき");
|
---|
645 | i++;
|
---|
646 | }
|
---|
647 | if (defaultCount_ != 0) {
|
---|
648 | appendDummyInput("DEFAULT")
|
---|
649 | .appendField("その他の");
|
---|
650 | appendStatementInput("DEFAULT_DO")
|
---|
651 | .appendField("とき");
|
---|
652 | }
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | public class SwitchCaseTextContainerBlock : Block
|
---|
657 | {
|
---|
658 | public const string type_name = "switch_case_text_container";
|
---|
659 |
|
---|
660 | public SwitchCaseTextContainerBlock()
|
---|
661 | : base(type_name)
|
---|
662 | {
|
---|
663 | }
|
---|
664 |
|
---|
665 | public void init()
|
---|
666 | {
|
---|
667 | appendDummyInput()
|
---|
668 | .appendField("条件");
|
---|
669 | appendStatementInput("STACK");
|
---|
670 | setColour(210);
|
---|
671 | setTooltip("");
|
---|
672 | contextMenu = false;
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | public class SwitchCaseTextConstBlock : Block
|
---|
677 | {
|
---|
678 | public const string type_name = "switch_case_text_const";
|
---|
679 | public Connection statementConnection_;
|
---|
680 |
|
---|
681 | public SwitchCaseTextConstBlock()
|
---|
682 | : base(type_name)
|
---|
683 | {
|
---|
684 | }
|
---|
685 |
|
---|
686 | /// <summary>
|
---|
687 | /// Block for swicth/case/default condition.
|
---|
688 | /// </summary>
|
---|
689 | public void init()
|
---|
690 | {
|
---|
691 | setColour(210);
|
---|
692 | appendDummyInput()
|
---|
693 | .appendField("固定値")
|
---|
694 | .appendField("0", "CONST");
|
---|
695 | setPreviousStatement(true);
|
---|
696 | setNextStatement(true);
|
---|
697 | setTooltip("固定値の条件");
|
---|
698 | contextMenu = false;
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | public class SwitchCaseTextRangeBlock : Block
|
---|
703 | {
|
---|
704 | public const string type_name = "switch_case_text_range";
|
---|
705 | public Connection statementConnection_;
|
---|
706 |
|
---|
707 | public SwitchCaseTextRangeBlock()
|
---|
708 | : base(type_name)
|
---|
709 | {
|
---|
710 | }
|
---|
711 |
|
---|
712 | /// <summary>
|
---|
713 | /// Block for swicth/case/default condition.
|
---|
714 | /// </summary>
|
---|
715 | public void init()
|
---|
716 | {
|
---|
717 | setColour(210);
|
---|
718 | appendDummyInput()
|
---|
719 | .appendField("範囲")
|
---|
720 | .appendField("a", "RANGE_MIN")
|
---|
721 | .appendField("から")
|
---|
722 | .appendField("b", "RANGE_MAX");
|
---|
723 | setPreviousStatement(true);
|
---|
724 | setNextStatement(true);
|
---|
725 | setTooltip("範囲の条件");
|
---|
726 | contextMenu = false;
|
---|
727 | }
|
---|
728 | }
|
---|
729 |
|
---|
730 | public class SwitchCaseTextDefaultBlock : Block
|
---|
731 | {
|
---|
732 | public const string type_name = "switch_case_text_default";
|
---|
733 | public Connection statementConnection_;
|
---|
734 |
|
---|
735 | public SwitchCaseTextDefaultBlock()
|
---|
736 | : base(type_name)
|
---|
737 | {
|
---|
738 | }
|
---|
739 |
|
---|
740 | /// <summary>
|
---|
741 | /// Block for swicth/case/default condition.
|
---|
742 | /// </summary>
|
---|
743 | public void init()
|
---|
744 | {
|
---|
745 | setColour(210);
|
---|
746 | appendDummyInput()
|
---|
747 | .appendField("その他");
|
---|
748 | setPreviousStatement(true);
|
---|
749 | setTooltip("条件に当てはまらなかった場合");
|
---|
750 | contextMenu = false;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | partial class Ruby
|
---|
755 | {
|
---|
756 | public node switch_case_number(SwitchCaseNumberBlock block)
|
---|
757 | {
|
---|
758 | // case/when/else condition.
|
---|
759 | var argument0 = valueToCode(block, "SWITCH");
|
---|
760 | if (argument0 == null) argument0 = new int_node(this, -1);
|
---|
761 | case_node.when_t[] code = new case_node.when_t[0];
|
---|
762 | for (int n = 0; n <= block.cases_.Length; n++) {
|
---|
763 | var branch = statementToCode(block, "DO" + n);
|
---|
764 | var argument1 = block.getFieldValue("CONST" + n);
|
---|
765 | if (argument1 != null) {
|
---|
766 | var when = new case_node.when_t() { body = branch };
|
---|
767 | when.value.Push(new int_node(this, argument1 == null ? 0 : Bridge.Script.ParseInt(argument1, 10)));
|
---|
768 | code.Push(when);
|
---|
769 | }
|
---|
770 | else {
|
---|
771 | var min = block.getFieldValue("RANGE_MIN" + n);
|
---|
772 | var max = block.getFieldValue("RANGE_MAX" + n);
|
---|
773 | if ((min != null) && (max != null)) {
|
---|
774 | var when = new case_node.when_t() { body = branch };
|
---|
775 | when.value.Push(new dot2_node(this,
|
---|
776 | new int_node(this, min == null ? 0 : Bridge.Script.ParseInt(min, 10)),
|
---|
777 | new int_node(this, max == null ? 0 : Bridge.Script.ParseInt(max, 10))));
|
---|
778 | code.Push(when);
|
---|
779 | }
|
---|
780 | }
|
---|
781 | }
|
---|
782 | if (block.defaultCount_ != 0) {
|
---|
783 | var branch = statementToCode(block, "DEFAULT_DO");
|
---|
784 | if (branch != null) {
|
---|
785 | var when = new case_node.when_t() { body = branch };
|
---|
786 | code.Push(when);
|
---|
787 | }
|
---|
788 | }
|
---|
789 | return new case_node(this, argument0, code);
|
---|
790 | }
|
---|
791 |
|
---|
792 | public node switch_case_text(SwitchCaseTextBlock block)
|
---|
793 | {
|
---|
794 | // case/when/else condition.
|
---|
795 | var argument0 = valueToCode(block, "SWITCH");
|
---|
796 | if (argument0 == null) argument0 = new str_node(this, "");
|
---|
797 | case_node.when_t[] code = new case_node.when_t[0];
|
---|
798 | for (int n = 0; n <= block.cases_.Length; n++) {
|
---|
799 | var branch = statementToCode(block, "DO" + n);
|
---|
800 | var argument1 = block.getFieldValue("CONST" + n);
|
---|
801 | if (argument1 != null) {
|
---|
802 | var when = new case_node.when_t() { body = branch };
|
---|
803 | when.value.Push(new str_node(this, argument1));
|
---|
804 | code.Push(when);
|
---|
805 | }
|
---|
806 | else {
|
---|
807 | var min = block.getFieldValue("RANGE_MIN" + n);
|
---|
808 | var max = block.getFieldValue("RANGE_MAX" + n);
|
---|
809 | if ((min != null) && (max != null)) {
|
---|
810 | var when = new case_node.when_t() { body = branch };
|
---|
811 | when.value.Push(new dot2_node(this, new str_node(this, min), new str_node(this, max)));
|
---|
812 | code.Push(when);
|
---|
813 | }
|
---|
814 | }
|
---|
815 | }
|
---|
816 | if (block.defaultCount_ != 0) {
|
---|
817 | var branch = statementToCode(block, "DEFAULT_DO");
|
---|
818 | if (branch != null) {
|
---|
819 | var when = new case_node.when_t() { body = branch };
|
---|
820 | code.Push(when);
|
---|
821 | }
|
---|
822 | }
|
---|
823 | return new case_node(this, argument0, code);
|
---|
824 | }
|
---|
825 | }
|
---|
826 | }
|
---|