統計
| ブランチ: | リビジョン:

pictcode / app / webroot / js / pictcode / preview_manager.js @ 9d2f0219

履歴 | 表示 | アノテート | ダウンロード (30.87 KB)

1
define(['block_data', 'image_manager', 'sprite_drawer', 'block_manager', 'block_drawer', 'utility', 'tag_selector'], function(blockData, imageManager, spriteDrawer, blockManager,blockDrawer , utility, tagSelector){
2

    
3
  var canvas;
4
  var canvasContext;
5
  var dt = 20;//ミリ秒
6
  var timer;
7
  var dummy = {};
8
  var initialDummy = {size:125, position:{x:0, y:0}, scale:1.0, angle:0, opacity:1.0, mirrored:false};
9

    
10
  var aspectRatio;
11

    
12
  var ACTION_TYPE = {
13
    ROTATE_RIGHT : 0,
14
    ROTATE_LEFT : 1,
15
    ZOOM : 2,
16
    OPACITY : 3,
17
  };
18

    
19
  var BRANCH_TYPE = {
20
    EQUAL : 0,
21
    LARGER: 1,
22
    SMALLER: 2,
23
    EQLARGER: 3,
24
    EQSMALLER: 4,
25
  };
26

    
27
  var CALC_TYPE = {
28
    ADD : 0,
29
    SUBSTRACT: 1,
30
    MULTIPLICATE: 2,
31
    DIVIDE: 3,
32
    REST: 4,
33
  };
34

    
35
  var successiveFuncBlock = null;
36
  var successiveIntervalTime = 1000;
37
  var successiveIntervalTimer;
38

    
39
  function initialize(cvs, cvsCtx){
40
    initialDummy.image = imageManager.images.preview_001;
41
    canvas = cvs;
42
    canvasContext = cvsCtx;
43
    canvasContext.fillStyle = '#4682b4';
44
    aspectRatio = spriteDrawer.editorInnerSize.height/spriteDrawer.editorInnerSize.width;
45
    initializeDummy();
46
    initializePreview();
47
  }
48

    
49
  function startPreview(targetBlock){
50
    successiveFuncBlock = null;
51
    doPreview(targetBlock);
52
  }
53

    
54
  function doPreview(targetBlock){
55
    stopPreview();
56
    initializeDummy();
57

    
58
    switch(targetBlock.name){
59
      case "start_function":
60
      startFunction(targetBlock);
61
      break;
62
      case "return_function":
63
      returnFunction(targetBlock);
64
      break;
65
      case "fork":
66
      fork(targetBlock);
67
      break;
68
      case "move":
69
      move(targetBlock);
70
      break;
71
      case "rotate_right":
72
      action(targetBlock, ACTION_TYPE.ROTATE_RIGHT);
73
      break;
74
      case "rotate_left":
75
      action(targetBlock, ACTION_TYPE.ROTATE_LEFT);
76
      break;
77
      case "zoom":
78
      action(targetBlock, ACTION_TYPE.ZOOM);
79
      break;
80
      case "opacity":
81
      action(targetBlock, ACTION_TYPE.OPACITY);
82
      break;
83
      case "self_duplicate":
84
      selfDuplicate(targetBlock);
85
      break;
86
      case "self_kill":
87
      selfKill(targetBlock);
88
      break;
89
      case "mirror":
90
      mirror(targetBlock);
91
      break;
92
      case "gravity":
93
      gravity(targetBlock);
94
      break;
95
      case "branch_equal":
96
      branch(targetBlock, BRANCH_TYPE.EQUAL);
97
      break;
98
      case "branch_larger":
99
      branch(targetBlock, BRANCH_TYPE.LARGER);
100
      break;
101
      case "branch_smaller":
102
      branch(targetBlock, BRANCH_TYPE.SMALLER);
103
      break;
104
      case "branch_eqlarger":
105
      branch(targetBlock, BRANCH_TYPE.EQLARGER);
106
      break;
107
      case "branch_eqsmaller":
108
      branch(targetBlock, BRANCH_TYPE.EQSMALLER);
109
      break;
110
      case "var_scalar":
111
      scalar(targetBlock);
112
      break;
113
      case "var_add":
114
      variable(targetBlock, CALC_TYPE.ADD);
115
      break;
116
      case "var_substract":
117
      variable(targetBlock, CALC_TYPE.SUBSTRACT);
118
      break;
119
      case "var_multiplicate":
120
      variable(targetBlock, CALC_TYPE.MULTIPLICATE);
121
      break;
122
      case "var_divide":
123
      variable(targetBlock, CALC_TYPE.DIVIDE);
124
      break;
125
      case "var_rest":
126
      variable(targetBlock, CALC_TYPE.REST);
127
      break;
128
      case "var_substitute":
129
      substitute(targetBlock);
130
      break;
131
      case "set_timer":
132
      setTimer(targetBlock);
133
      break;
134
      case "wait":
135
      wait(targetBlock);
136
      break;
137
      case "start_app":
138
      startApp(targetBlock);
139
      break;
140
      case "finish_app":
141
      finishApp(targetBlock);
142
      break;
143
      case "tap_enable":
144
      tapEnable(targetBlock);
145
      break;
146
      case "swipe_enable":
147
      swipeEnable(targetBlock);
148
      break;
149
      case "func_on_tag":
150
      funcOnTag(targetBlock);
151
      break;
152
      case "duplicate":
153
      duplicate(targetBlock);
154
      break;
155
      case "display":
156
      display(targetBlock);
157
      break;
158
      case "play_sound":
159
      playSound(targetBlock);
160
      break;
161
      default:
162
      initializePreview();
163
      break;
164
    }
165
  }
166

    
167
  function stopPreview(){
168
    if (timer) {
169
      clearInterval(timer);
170
    };
171
    if (successiveIntervalTimer) {
172
      clearTimeout(successiveIntervalTimer);
173
    };
174
  }
175

    
176
  function initializePreview(){
177
    var cvDummySize = canvas.height*0.25;
178
    var cvGbRatio = cvDummySize / dummy.size;
179
    drawDummy(cvDummySize, cvGbRatio);
180
  }
181

    
182
  function initializeDummy(){
183
    dummy.image = initialDummy.image;
184
    dummy.size = initialDummy.size;
185
    dummy.position = {x:initialDummy.position.x, y:initialDummy.position.y};
186
    dummy.scale = initialDummy.scale;
187
    dummy.angle = initialDummy.angle;
188
    dummy.opacity = initialDummy.opacity;
189
    dummy.mirrored = initialDummy.mirrored;
190
    dummy.speed = 0;
191
  }
192

    
193
  function drawDummy(cvDummySize, cvGbRatio, isTwined){
194
    var cvDummyLeft = canvas.width/2 - cvDummySize/2 + dummy.position.x*cvGbRatio;
195
    var cvDummyTop = canvas.height/2 - cvDummySize/2 - dummy.position.y*cvGbRatio;
196

    
197
    canvasContext.clearRect(0, 0, canvas.width, canvas.height);
198
    canvasContext.fillRect(0, 0, canvas.width, canvas.height);
199
    var cvBgSize = {width:spriteDrawer.editorInnerSize.width*cvGbRatio*1.1, height:spriteDrawer.editorInnerSize.height*cvGbRatio*1.1};
200
    canvasContext.drawImage(imageManager.images.preview_002, canvas.width/2-cvBgSize.width/2, canvas.height/2-cvBgSize.height/2, cvBgSize.width, cvBgSize.height);
201
    canvasContext.save();
202
    canvasContext.lineWidth = 10;
203
    canvasContext.strokeStyle = "#8b4513";
204
    canvasContext.strokeRect(0, 0, canvas.width, canvas.height);
205
    canvasContext.translate(canvas.width/2, canvas.height/2);
206
    canvasContext.rotate(dummy.angle);
207
    if (dummy.mirrored) {
208
      canvasContext.transform(-1, 0, 0, 1, 0, 0);
209
    };
210
    canvasContext.translate(-canvas.width/2, -canvas.height/2);
211
    canvasContext.globalAlpha = dummy.opacity;
212
    canvasContext.drawImage(dummy.image, cvDummyLeft, cvDummyTop, cvDummySize, cvDummySize);
213
    if (isTwined) {
214
      var twinedLeft = canvas.width/2 - cvDummySize/2 - dummy.position.x*cvGbRatio;
215
      canvasContext.drawImage(dummy.image, twinedLeft, cvDummyTop, cvDummySize, cvDummySize);
216
    };
217
    canvasContext.restore();
218
  }
219

    
220
  function drawEmpty(){
221
    canvasContext.clearRect(0, 0, canvas.width, canvas.height);
222
    canvasContext.fillRect(0, 0, canvas.width, canvas.height);
223
    canvasContext.drawImage(imageManager.images.preview_002, 0, 0, canvas.width, canvas.height);
224
    canvasContext.save();
225
    canvasContext.lineWidth = 10;
226
    canvasContext.strokeStyle = "#8b4513";
227
    canvasContext.strokeRect(0, 0, canvas.width, canvas.height);
228
    canvasContext.restore();
229
  }
230

    
231
  function drawText(text, x, y, size, color){
232
    canvasContext.save();
233
    canvasContext.textAlign = "center";
234
    canvasContext.textBaseline = "middle";
235
    // var metrics = canvasContext.measureText(text);
236
    // var fotSize = Math.floor(size/metrics.width*14);
237
    // var maxFontSize = canvas.width*0.15;
238
    // if (fotSize > maxFontSize) {
239
    //   fotSize = maxFontSize;
240
    // };
241
    var fontSize = size;
242
    canvasContext.font = "bold "+ fontSize + "px " + "sans-serif";
243
    canvasContext.fillStyle = color;
244
    canvasContext.fillText(text, x, y-size*0.21, canvas.width*0.9);
245
    canvasContext.restore();
246
  }
247

    
248
  function goNextBlock(block){
249
    if (timer) {
250
      clearInterval(timer);
251
    };
252

    
253
    if (!successiveFuncBlock) {return};
254
    if (!block.bottomLinks) {
255
      doPreview(successiveFuncBlock);
256
      return;
257
    };
258

    
259
    var nextLink = null;
260
    for(var i=block.bottomLinks.length-1; i>=0; i--){
261
      nextLink = block.bottomLinks[i].id;
262
    }
263
    if (!nextLink) {
264
      doPreview(successiveFuncBlock);
265
      return;
266
    };
267
    var nextBlock = blockManager.findBlock(nextLink);
268
    if (nextBlock) {
269
      doPreview(nextBlock);
270
      return;
271
    }else{
272
      doPreview(successiveFuncBlock);
273
      return;
274
    };
275
  }
276

    
277
  function startFunction(block){
278
    drawEmpty();
279

    
280
    var v1 = blockManager.getValue(block.value1);
281
    v1 = utility.numberToLargeAlphabet(v1);
282
    var funcSize =  {width:canvas.width*0.4, height:canvas.width*0.4*0.327};
283
    canvasContext.drawImage(imageManager.images.blk_function_001, canvas.width/2-funcSize.width/2, canvas.height*0.5-funcSize.height/2, funcSize.width, funcSize.height);
284
    drawText(v1, canvas.width*0.626, canvas.height*0.5, canvas.width*0.05, "black");
285

    
286
    successiveFuncBlock = block;
287
    successiveIntervalTimer = setTimeout(function () {
288
      if (!block.bottomLinks[0].id) {
289
        return;
290
      };
291
      var nextBlock = blockManager.findBlock(block.bottomLinks[0].id);
292
      if (nextBlock) {
293
        doPreview(nextBlock);
294
      };
295
    }, successiveIntervalTime);
296
  }
297

    
298
  function returnFunction(block){
299
    drawEmpty();
300

    
301
    var v1 = blockManager.getValue(block.value1);
302
    v1 = utility.numberToLargeAlphabet(v1);
303
    var funcSize =  {width:canvas.width*0.4, height:canvas.width*0.4*0.327};
304
    canvasContext.drawImage(imageManager.images.blk_function_001, canvas.width/2-funcSize.width/2, canvas.height*0.6-funcSize.height/2, funcSize.width, funcSize.height);
305
    drawText(v1, canvas.width*0.626, canvas.height*0.6, canvas.width*0.05, "black");
306

    
307
    var arrowSize = canvas.width*0.14;
308
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width/2-arrowSize/2, canvas.height*0.35-arrowSize/2, arrowSize, arrowSize);
309

    
310
    if (successiveFuncBlock) {
311
      successiveIntervalTimer = setTimeout(function () {
312
        if (successiveFuncBlock) {
313
          doPreview(successiveFuncBlock);
314
        };
315
      }, successiveIntervalTime);
316
    };
317
  }
318

    
319
  function fork(block){
320
    drawEmpty();
321

    
322
    var forkSize =  {width:canvas.width*0.9, height:canvas.width*0.9*0.113};
323
    canvasContext.drawImage(imageManager.images.blk_function_003, canvas.width/2-forkSize.width/2, canvas.height/2-forkSize.height/2, forkSize.width, forkSize.height);
324

    
325
    var arrowSize = canvas.width*0.1;
326
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width*0.185-arrowSize/2, canvas.height*0.35-arrowSize/2, arrowSize, arrowSize);
327
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width*0.185-arrowSize/2, canvas.height*0.63-arrowSize/2, arrowSize, arrowSize);
328
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width*0.778-arrowSize/2, canvas.height*0.63-arrowSize/2, arrowSize, arrowSize)
329

    
330
    if (successiveFuncBlock) {
331
      successiveIntervalTimer = setTimeout(function () {
332
        if (successiveFuncBlock) {
333
          goNextBlock(block);
334
        };
335
      }, successiveIntervalTime);
336
    };
337
  }
338

    
339
  function move(block){
340
    var duration = blockManager.getValue(block.value1) * 1000;
341
    var passedTime = 0;
342
    var isInterval = false;
343
    var intervalTime = 0;
344

    
345
    dummy.position = {x:-block.value2.value.x/2, y:-block.value2.value.y/2};
346
    var gbDummySize = dummy.size;
347
    var gbDistance;
348
    var cvsDistance;
349
    if (Math.abs(block.value2.value.x) > Math.abs(block.value2.value.y)) {
350
      gbDistance = Math.abs(block.value2.value.x);
351
      cvsDistance = canvas.width;
352
    }else{
353
      gbDistance = Math.abs(block.value2.value.y);
354
      cvsDistance = canvas.height;
355
    };
356
    var cvDummySize = cvsDistance * gbDummySize / (gbDummySize+gbDistance);
357
    var cvDistance = cvsDistance * gbDistance / (gbDummySize+gbDistance);
358
    var cvGbRatio = cvDummySize / gbDummySize;
359

    
360
    var gbd = {dx:block.value2.value.x, dy:block.value2.value.y};
361
    if (duration > 0) {
362
      gbd = {dx:gbd.dx*dt/duration, dy:gbd.dy*dt/duration};
363
    }
364

    
365
    timer = setInterval(function(){
366
      if (isInterval) {
367
        intervalTime += dt;
368
        if (intervalTime == 500) {
369
          dummy.position.x = -block.value2.value.x/2;
370
          dummy.position.y = -block.value2.value.y/2;
371
          drawDummy(cvDummySize, cvGbRatio, false);
372
        }else if (intervalTime >= 1200) {
373
          isInterval = false;
374
          passedTime = 0;
375
          if (block.value1.isRandom) {
376
            duration = utility.getRandomDecimal(block.value1.value.min, block.value1.value.max)*1000;
377
            gbd = {dx:block.value2.value.x, dy:block.value2.value.y};
378
            if (duration > 0) {
379
              gbd = {dx:gbd.dx*dt/duration, dy:gbd.dy*dt/duration};
380
            }
381
          };
382
        };
383
        return;
384
      };
385

    
386
      dummy.position.x += gbd.dx;
387
      dummy.position.y += gbd.dy;
388
      passedTime += dt;
389
      drawDummy(cvDummySize, cvGbRatio, false);
390
      if(passedTime >= duration){
391
        if (successiveFuncBlock) {
392
          goNextBlock(block);
393
        }else{
394
          isInterval = true;
395
          intervalTime = 0;
396
        };
397
      };
398
    } , dt);
399
}
400
  //
401
  function action(block, actionType){
402
    var duration = blockManager.getValue(block.value1) * 1000;
403
    var degree = blockManager.getValue(block.value2);
404

    
405
    var passedTime = 0;
406
    var isInterval = false;
407
    var intervalTime = 0;
408

    
409
    var ds = getIncrement(duration, degree, actionType);
410
    var cvDummySize;
411
    switch(actionType){
412
      case ACTION_TYPE.ROTATE_RIGHT:
413
      cvDummySize = canvas.height*0.45;
414
      break;
415
      case ACTION_TYPE.ROTATE_LEFT:
416
      cvDummySize = canvas.height*0.45;
417
      break;
418
      case ACTION_TYPE.ZOOM:
419
      cvDummySize = canvas.height*0.25;
420
      break;
421
      case ACTION_TYPE.OPACITY:
422
      cvDummySize = canvas.height*0.45;
423
      break;
424
    }
425
    if (!ds || !cvDummySize) {return;};
426

    
427
    var cvGbRatio = cvDummySize / dummy.size;
428

    
429
    timer = setInterval(function(){
430
      if (isInterval) {
431
        intervalTime += dt;
432
        if (intervalTime == 500) {
433
          initializeDummy();
434
          drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
435
        }else if (intervalTime >= 1200) {
436
          isInterval = false;
437
          passedTime = 0;
438
          var isRandom = false;
439
          if (block.value1.isRandom) {
440
            isRandom = true;
441
            duration = blockManager.getValue(block.value1) * 1000;
442
          }
443
          if (block.value2.isRandom) {
444
            isRandom = true;
445
            degree = blockManager.getValue(block.value2);
446
          }
447
          if (isRandom) {
448
            ds = getIncrement(duration, degree, actionType);
449
          };
450
        };
451
        return;
452
      };
453

    
454
      switch(actionType){
455
        case ACTION_TYPE.ROTATE_RIGHT:
456
        case ACTION_TYPE.ROTATE_LEFT:
457
        dummy.angle += ds;
458
        break;
459
        case ACTION_TYPE.ZOOM:
460
        dummy.scale += ds;
461
        break;
462
        case ACTION_TYPE.OPACITY:
463
        dummy.opacity += ds;
464
        break;
465
      }
466

    
467
      passedTime += dt;
468
      drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
469

    
470
      if(passedTime >= duration){
471
        if (successiveFuncBlock) {
472
          goNextBlock(block);
473
        }else{
474
          isInterval = true;
475
          intervalTime = 0;
476
        };
477
      }
478
    } ,dt);
479
} 
480
  //
481
  function getIncrement(duration, degree, actionType){
482
    var ds;
483
    switch(actionType){
484
      case ACTION_TYPE.ROTATE_RIGHT:
485
      if (duration > 0) {
486
        ds = (degree*Math.PI/180 - dummy.angle) * dt / duration;
487
      }else{
488
        ds = degree*Math.PI/180 - dummy.angle;
489
      };
490
      break;
491
      case ACTION_TYPE.ROTATE_LEFT:
492
      if (duration > 0) {
493
        ds = -(degree*Math.PI/180 - dummy.angle) * dt / duration;
494
      }else{
495
        ds = -(degree*Math.PI/180 - dummy.angle);
496
      };
497
      break;
498
      case ACTION_TYPE.ZOOM:
499
      if (duration > 0) {
500
        ds = (degree - dummy.scale) * dt / duration;
501
      }else{
502
        ds = (degree - dummy.scale);
503
      };
504
      break;
505
      case ACTION_TYPE.OPACITY:
506
      if (duration > 0) {
507
        ds = (degree - dummy.opacity) * dt / duration;
508
      }else{
509
        ds = degree - dummy.opacity;
510
      };
511
      break;
512
    }
513
    return ds;
514
  }
515

    
516
  function selfDuplicate(block){
517

    
518
    var passedTime = 0;
519
    var isInterval = false;
520
    var intervalTime = 0;
521
    var duration = 500;
522

    
523
    var cvDummySize=  canvas.height*0.45;
524
    var ds = cvDummySize*0.28 * dt / duration;
525
    var cvGbRatio = cvDummySize / dummy.size;
526

    
527
    timer = setInterval(function(){
528
      if (isInterval) {
529
        intervalTime += dt;
530
        if (intervalTime == 1000) {
531
          drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
532
        }else if (intervalTime >= 1700) {
533
          isInterval = false;
534
          passedTime = 0;
535
        };
536
        return;
537
      };
538

    
539
      dummy.position.x += ds;
540
      passedTime += dt;
541
      drawDummy(cvDummySize*dummy.scale, cvGbRatio, true);
542

    
543
      if(passedTime >= duration){
544
        if (successiveFuncBlock) {
545
          goNextBlock(block);
546
        }else{
547
          initializeDummy();
548
          isInterval = true;
549
          intervalTime = 0;
550
        };
551
      }
552
    } , dt);
553
  } 
554
  //
555
  function selfKill(block){
556

    
557
    var passedTime = 0;
558
    var isInterval = false;
559
    var intervalTime = 0;
560
    var duration = 1700;
561

    
562
    var cvDummySize=  canvas.height*0.45;
563
    var cvGbRatio = cvDummySize / dummy.size;
564

    
565
    drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
566
    timer = setInterval(function(){
567
      if (isInterval) {
568
        intervalTime += dt;
569
        if (intervalTime >= duration) {
570
          if (successiveFuncBlock) {
571
            goNextBlock(block);
572
          }else{
573
            isInterval = false;
574
            passedTime = 0;
575
            initializeDummy();
576
            drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
577
          };
578
        };
579
        return;
580
      };
581

    
582
      passedTime += dt;
583

    
584
      if(passedTime >= duration){
585
        isInterval = true;
586
        intervalTime = 0;
587
        dummy.opacity = 0;
588
        drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
589
        return;
590
      }
591
    } , dt);
592
  } 
593

    
594
  function mirror(block){
595

    
596
    var passedTime = 0;
597
    var isInterval = false;
598
    var intervalTime = 0;
599
    var duration = 1200;
600

    
601
    var cvDummySize=  canvas.height*0.45;
602
    var cvGbRatio = cvDummySize / dummy.size;
603

    
604
    drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
605
    timer = setInterval(function(){
606
      if (isInterval) {
607
        intervalTime += dt;
608
        if (intervalTime >= duration) {
609
          if (successiveFuncBlock) {
610
            goNextBlock(block);
611
          }else{
612
            isInterval = false;
613
            passedTime = 0;
614
            initializeDummy();
615
            drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
616
          };
617
        };
618
        return;
619
      };
620

    
621
      passedTime += dt;
622

    
623
      if(passedTime >= duration){
624
        isInterval = true;
625
        intervalTime = 0;
626
        dummy.mirrored = true;
627
        drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
628
        return;
629
      }
630
    } , dt);
631
  } 
632
  //
633
  function gravity(block){
634

    
635
    var passedTime = 0;
636
    var isInterval = false;
637
    var intervalTime = 0;
638
    var duration = 1400;
639
    var speed = 0;
640
    var dsp = 0.4;
641

    
642
    var cvDummySize=  canvas.height*0.2;
643
    var cvGbRatio = cvDummySize / dummy.size;
644
    dummy.position.y = dummy.size*1.5;
645
    if (!block.value1.value) {
646
      drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
647
      return;
648
    };
649

    
650
    drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
651
    timer = setInterval(function(){
652
      if (isInterval) {
653
        intervalTime += dt;
654
        if (intervalTime >= duration) {
655
          isInterval = false;
656
          passedTime = 0.0;
657
          dummy.speed = 0;
658
          dummy.position.y = dummy.size*1.5;
659
          drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
660
        };
661
        return;
662
      };
663

    
664
      passedTime += dt;
665
      dummy.speed += dsp;
666
      dummy.position.y -= dummy.speed;
667
      drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
668

    
669
      if(passedTime >= duration){
670
        if (successiveFuncBlock) {
671
          goNextBlock(block);
672
        }else{
673
          isInterval = true;
674
          intervalTime = 0;
675
          return;
676
        };
677
      }
678
    } , dt);
679
  }
680

    
681
  function branch(block, branchType){
682
    drawEmpty();
683

    
684
    var v1 = blockManager.getValue(block.value1)*1;
685
    var v2 = blockManager.getValue(block.value2)*1;
686
    drawText(v1, canvas.width*0.25, canvas.height/2, canvas.width*0.12, "black");
687
    drawText(v2, canvas.width*0.75, canvas.height/2, canvas.width*0.12, "black");
688

    
689
    var flag = false;
690
    var sign;
691
    switch(branchType){
692
      case BRANCH_TYPE.EQUAL:
693
      sign = "=";
694
      if (v1 == v2) {
695
        flag = true;
696
      };
697
      break;
698
      case BRANCH_TYPE.LARGER:
699
      sign = ">";
700
      if (v1 > v2) {
701
        flag = true;
702
      };
703
      break;
704
      case BRANCH_TYPE.SMALLER:
705
      sign = "<";
706
      if (v1 < v2) {
707
        flag = true;
708
      };
709
      break;
710
      case BRANCH_TYPE.EQLARGER:
711
      sign = "";
712
      if (v1 >= v2) {
713
        flag = true;
714
      };
715
      break;
716
      case BRANCH_TYPE.EQSMALLER:
717
      sign = "";
718
      if (v1 <= v2) {
719
        flag = true;
720
      };
721
      break;
722
    }
723
    drawText(sign, canvas.width/2, canvas.height/2, canvas.width*0.14, "black");
724

    
725
    var tfImage;
726
    if (flag) {
727
      tfImage = imageManager.images.icon_blk_016
728
    }else{
729
      tfImage = imageManager.images.icon_blk_017
730
    };
731
    var tfImageSize = canvas.width*0.2;
732
    canvasContext.drawImage(tfImage, canvas.width/2-tfImageSize/2, canvas.height*0.7, tfImageSize, tfImageSize);
733

    
734
    if (successiveFuncBlock) {
735
      successiveIntervalTimer = setTimeout(function () {
736
        if (successiveFuncBlock) {
737
          goNextBlock(block);
738
        };
739
      }, successiveIntervalTime);
740
    };
741
  }
742

    
743
  function scalar(block){
744
    drawEmpty();
745

    
746
    var v2 = blockManager.getValue(block.value2)*1;
747
    drawText(v2, canvas.width/2, canvas.height/2, canvas.width*0.15, "black");
748
  }
749

    
750
  function variable(block, branchType){
751
    drawEmpty();
752

    
753
    var v1 = blockManager.getValue(block.value1)*1;
754
    var v2 = blockManager.getValue(block.value2)*1;
755

    
756
    var sign;
757
    var result = 0;
758
    switch(branchType){
759
      case BRANCH_TYPE.EQUAL:
760
      sign = "+";
761
      result = v1 + v2;
762
      break;
763
      case BRANCH_TYPE.LARGER:
764
      sign = "-";
765
      result = v1 - v2;
766
      break;
767
      case BRANCH_TYPE.SMALLER:
768
      sign = "×";
769
      result = v1 * v2;
770
      break;
771
      case BRANCH_TYPE.EQLARGER:
772
      sign = "÷";
773
      result = v1 / v2;
774
      break;
775
      case BRANCH_TYPE.EQSMALLER:
776
      sign = "%";
777
      result = v1 % v2;
778
      break;
779
    }
780
    result = result.toFixed(1);
781
    drawText(v1 + " " + sign + " " + v2 + "" + result, canvas.width*0.5, canvas.height*0.5, canvas.width*0.1, "black");
782

    
783
    if (successiveFuncBlock) {
784
      successiveIntervalTimer = setTimeout(function () {
785
        if (successiveFuncBlock) {
786
          goNextBlock(block);
787
        };
788
      }, successiveIntervalTime);
789
    };    
790
  }
791

    
792
  function substitute(block){
793
    drawEmpty();
794

    
795
    var v1 = blockManager.getValue(block.value1)*1;
796
    var v2 = blockManager.getValue(block.value2)*1;
797

    
798
    drawText(v1 + "" + v2, canvas.width*0.5, canvas.height*0.5, canvas.width*0.1, "black");
799

    
800
    if (successiveFuncBlock) {
801
      successiveIntervalTimer = setTimeout(function () {
802
        if (successiveFuncBlock) {
803
          goNextBlock(block);
804
        };
805
      }, successiveIntervalTime);
806
    };    
807
  }
808

    
809
  function setTimer(block){
810

    
811
    var v1 = blockManager.getValue(block.value1);
812
    var v2 = blockManager.getValue(block.value2);
813
    v2 = utility.numberToLargeAlphabet(v2);
814

    
815
    function initializeSetTimer(){
816
      drawEmpty();
817

    
818
      canvasContext.save();
819
      canvasContext.globalAlpha = 0.5;
820
      var imageSize = canvas.width*0.5;
821
      canvasContext.drawImage(imageManager.images.icon_blk_024, canvas.width/2-imageSize/2, canvas.height*0.35-imageSize/2, imageSize, imageSize);
822
      canvasContext.restore();
823
      var funcSize =  {width:canvas.width*0.4, height:canvas.width*0.4*0.327};
824
      canvasContext.drawImage(imageManager.images.blk_function_001, canvas.width/2-funcSize.width/2, canvas.height*0.6-funcSize.height/2, funcSize.width, funcSize.height);
825
      drawText(v2, canvas.width*0.626, canvas.height*0.6, canvas.width*0.05, "black");
826
    }
827
    initializeSetTimer();
828

    
829
    var passedTime = 0;
830
    var isInterval = false;
831
    var intervalTime = 0;
832
    var duration = v1/2*1000;
833

    
834
    var arrowSize = canvas.width*0.16;
835
    // canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width/2-arrowSize/2, canvas.height*0.8-arrowSize/2, arrowSize, arrowSize);
836

    
837
    timer = setInterval(function(){
838
      if (isInterval) {
839
        intervalTime += dt;
840
        if (intervalTime >= duration) {
841
          if (successiveFuncBlock) {
842
            goNextBlock(block);
843
          }else{
844
            isInterval = false;
845
            passedTime = 0;
846
            initializeSetTimer();
847
          };
848
        };
849
        return;
850
      };
851

    
852
      passedTime += dt;
853

    
854
      if(passedTime >= duration){
855
        isInterval = true;
856
        intervalTime = 0;
857
        canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width/2-arrowSize/2, canvas.height*0.8-arrowSize/2, arrowSize, arrowSize);
858
        return;
859
      }
860
    } , dt);
861
  }
862

    
863
  function wait(block){
864
    drawEmpty();
865

    
866
    canvasContext.save();
867
    canvasContext.globalAlpha = 0.5;
868
    var imageSize = canvas.width*0.5;
869
    canvasContext.drawImage(imageManager.images.icon_blk_024, canvas.width/2-imageSize/2, canvas.height/2-imageSize/2, imageSize, imageSize);
870
    canvasContext.restore();
871

    
872
    var v1 = blockManager.getValue(block.value1);
873
    drawText(v1, canvas.width*0.5, canvas.height*0.52, canvas.width*0.1, "black");
874

    
875
    if (successiveFuncBlock) {
876
      successiveIntervalTimer = setTimeout(function () {
877
        if (successiveFuncBlock) {
878
          goNextBlock(block);
879
        };
880
      }, successiveIntervalTime);
881
    };  
882
  }
883

    
884
  function startApp(block){
885
    drawEmpty();
886

    
887
    var imageSize = canvas.width*0.4;
888
    canvasContext.drawImage(imageManager.images.ui_002, canvas.width/2-imageSize/2, canvas.height*0.45-imageSize/2, imageSize, imageSize);
889
    var arrowSize = canvas.width*0.16;
890
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width/2-arrowSize/2, canvas.height*0.8-arrowSize/2, arrowSize, arrowSize);
891

    
892
    if (successiveFuncBlock) {
893
      successiveIntervalTimer = setTimeout(function () {
894
        if (successiveFuncBlock) {
895
          goNextBlock(block);
896
        };
897
      }, successiveIntervalTime);
898
    };  
899
  }
900

    
901
  function finishApp(block){
902
    drawEmpty();
903

    
904
    var imageSize = canvas.width*0.4;
905
    canvasContext.drawImage(imageManager.images.btn_play_002, canvas.width/2-imageSize/2, canvas.height/2-imageSize*0.406/2, imageSize, imageSize*0.406);
906

    
907
    if (successiveFuncBlock) {
908
      successiveIntervalTimer = setTimeout(function () {
909
        if (successiveFuncBlock) {
910
          goNextBlock(block);
911
        };
912
      }, successiveIntervalTime);
913
    };  
914
  }
915

    
916
  function tapEnable(block){
917
    drawEmpty();
918

    
919
    var imageSize = canvas.width*0.4;
920
    canvasContext.drawImage(imageManager.images.icon_blk_026, canvas.width/2-imageSize/2, canvas.height*0.45-imageSize/2, imageSize, imageSize);
921

    
922
    var tfImage;
923
    if (blockManager.getValue(block.value1)) {
924
      tfImage = imageManager.images.icon_blk_016
925
    }else{
926
      tfImage = imageManager.images.icon_blk_017
927
    };
928
    var tfImageSize = canvas.width*0.2;
929
    canvasContext.drawImage(tfImage, canvas.width/2-tfImageSize/2, canvas.height*0.7, tfImageSize, tfImageSize);
930

    
931
    if (successiveFuncBlock) {
932
      successiveIntervalTimer = setTimeout(function () {
933
        if (successiveFuncBlock) {
934
          goNextBlock(block);
935
        };
936
      }, successiveIntervalTime);
937
    };  
938
  }
939

    
940
  function swipeEnable(block){
941
    drawEmpty();
942

    
943
    var imageSize = canvas.width*0.4;
944
    canvasContext.drawImage(imageManager.images.icon_blk_026, canvas.width/2-imageSize/2, canvas.height*0.45-imageSize/2, imageSize, imageSize);
945

    
946
    var tfImage;
947
    if (blockManager.getValue(block.value1)) {
948
      tfImage = imageManager.images.icon_blk_016
949
    }else{
950
      tfImage = imageManager.images.icon_blk_017
951
    };
952
    var tfImageSize = canvas.width*0.2;
953
    canvasContext.drawImage(tfImage, canvas.width/2-tfImageSize/2, canvas.height*0.7, tfImageSize, tfImageSize);
954

    
955
    if (successiveFuncBlock) {
956
      successiveIntervalTimer = setTimeout(function () {
957
        if (successiveFuncBlock) {
958
          goNextBlock(block);
959
        };
960
      }, successiveIntervalTime);
961
    };      
962
  }
963

    
964
  function funcOnTag(block){
965
    drawEmpty();
966

    
967
    var cvDummySize=  canvas.height*0.4;
968
    var cvGbRatio = cvDummySize / dummy.size;
969
    dummy.position.y = 80;
970
    drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
971

    
972
    var v1 = blockManager.getValue(block.value1);
973
    blockDrawer.drawCircleFilledText(v1, canvas.width*0.5, canvas.height*0.25, canvas.width*0.1, canvas.width*0.5, canvas.height*0.25, canvas.width*0.075, canvasContext, tagSelector.tagColors[v1]);
974
    var v2 = blockManager.getValue(block.value2);
975
    v2 = utility.numberToLargeAlphabet(v2);
976
    var funcSize =  {width:canvas.width*0.4, height:canvas.width*0.4*0.327};
977
    canvasContext.drawImage(imageManager.images.blk_function_001, canvas.width/2-funcSize.width/2, canvas.height*0.85-funcSize.height/2, funcSize.width, funcSize.height);
978
    drawText(v2, canvas.width*0.626, canvas.height*0.85, canvas.width*0.05, "black");
979

    
980
    var arrowSize = canvas.width*0.14;
981
    canvasContext.drawImage(imageManager.images.input_vector_002, canvas.width/2-arrowSize/2, canvas.height*0.55-arrowSize/2, arrowSize, arrowSize);
982

    
983
    if (successiveFuncBlock) {
984
      successiveIntervalTimer = setTimeout(function () {
985
        if (successiveFuncBlock) {
986
          goNextBlock(block);
987
        };
988
      }, successiveIntervalTime);
989
    };  
990
  }
991

    
992
  function duplicate(block){
993

    
994
    dummy.image = imageManager.images.icon_blk_023;
995

    
996
    var passedTime = 0;
997
    var isInterval = false;
998
    var intervalTime = 0;
999
    var duration = 250;
1000

    
1001
    var cvDummySize=  canvas.height*0.45;
1002
    var ds = cvDummySize*0.28 * dt / duration;
1003
    var cvGbRatio = cvDummySize / dummy.size;
1004

    
1005
    timer = setInterval(function(){
1006
      if (isInterval) {
1007
        intervalTime += dt;
1008
        if (intervalTime == 1000) {
1009
          if (successiveFuncBlock) {
1010
            goNextBlock(block);
1011
          }else{
1012
            drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
1013
          };          
1014
        }else if (intervalTime >= 1700) {        
1015
          isInterval = false;
1016
          passedTime = 0;
1017
        };
1018
        return;
1019
      };
1020

    
1021
      dummy.position.x += ds;
1022
      passedTime += dt;
1023

    
1024
      if(passedTime >= duration){
1025
        initializeDummy();
1026
        dummy.image = imageManager.images.icon_blk_023;
1027
        isInterval = true;
1028
        intervalTime = 0;
1029
        return;
1030
      }
1031

    
1032
      drawDummy(cvDummySize*dummy.scale, cvGbRatio, true);
1033
    } , dt);
1034
  }
1035

    
1036
  function display(block){
1037
    var cvDummySize=  canvas.height*0.4;
1038
    var cvGbRatio = cvDummySize / dummy.size;
1039
    drawDummy(cvDummySize*dummy.scale, cvGbRatio, false);
1040

    
1041
    var v1 = blockManager.getValue(block.value1);
1042
    blockDrawer.drawCircleFilledText(v1, canvas.width*0.5, canvas.height*0.7, canvas.width*0.1, canvas.width*0.5, canvas.height*0.7, canvas.width*0.075, canvasContext, tagSelector.tagColors[v1]);
1043

    
1044
    var v2 = blockManager.getValue(block.value2);
1045
    v2 = Math.floor(v2);
1046
    drawText(v2, canvas.width*0.5, canvas.height*0.55, canvas.width*0.15, "gray");
1047

    
1048
    if (successiveFuncBlock) {
1049
      successiveIntervalTimer = setTimeout(function () {
1050
        if (successiveFuncBlock) {
1051
          goNextBlock(block);
1052
        };
1053
      }, successiveIntervalTime);
1054
    };      
1055
  }
1056

    
1057
  function playSound(block){
1058
    drawEmpty();
1059

    
1060
    if (successiveFuncBlock) {
1061
      successiveIntervalTimer = setTimeout(function () {
1062
        if (successiveFuncBlock) {
1063
          goNextBlock(block);
1064
        };
1065
      }, successiveIntervalTime);
1066
    };    
1067
  }
1068

    
1069
  return {
1070
    initialize:initialize,
1071
    startPreview:startPreview,
1072
    stopPreview:stopPreview,
1073
  };
1074
});