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

pictcode / app / webroot / js / pictcode / coding_manager.js @ 0b1b8047

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

1
define(['block_selector', 'canvas_manager', 'image_manager', 'block_drawer', 'block_manager', 'input_manager', 'preview_manager'], function(blockSelector, canvasManager, imageManager, blockDrawer, blockManager, inputManager, previewManager){
2

    
3
  var canvas;
4
  var canvasContext;
5
  //拡大率 2-9
6
  var magnification = 6;
7
  var canvasBlockHRatio;
8
  var TAEGET = {
9
    BLOCK : 0,
10
    CANVAS : 1,
11
    NONE : 2
12
  };
13
  var currentTarget = TAEGET.NONE;
14

    
15
  var removeFunctionFromSprite;
16

    
17
  //初期化
18
  function initialize(blocks, removeFuncFromSpr) {
19

    
20
    //各モジュールの初期化
21
    blockSelector.initialize(afterAddBlock);
22
    blockManager.initialize(blocks, drawBlocks,removeFuncFromSpr, didSetTarget);
23
    inputManager.initialize(blockManager.getTargetBlock, drawBlocks, blockManager.getFunctions, didSetTarget);
24

    
25
    //コールバックの設定
26
    removeFunctionFromSprite = removeFuncFromSpr;
27

    
28
    //キャンバスのタップ操作の検出
29
    function touchStart(canvasX, canvasY){
30
      var target = blockDrawer.getPositionInfo(blockManager.blocks, canvasX, canvasY);
31
      switch (target.target){
32

    
33
        case "block":
34
        blockManager.setTarget(canvasX, canvasY, target.block);
35
        currentTarget = TAEGET.BLOCK;
36
        break;
37

    
38
        case "canvas":
39
        blockDrawer.startScroll(canvasX, canvasY);
40
        currentTarget = TAEGET.CANVAS;
41
        break;
42
      }
43
      canvasBlockHRatio = getCvsBlkhRatio();
44
    }
45
    function touchMove(canvasX, canvasY){
46
      switch (currentTarget){
47

    
48
        case TAEGET.BLOCK:
49
        blockManager.changePosition(canvasX, canvasY, canvasBlockHRatio);
50
        break;
51

    
52
        case TAEGET.CANVAS:
53
        blockDrawer.scrollCanvas(canvasX, canvasY, canvasBlockHRatio, drawBlocks);
54
        break;
55
      }
56
    }
57
    function touchEnd(canvasX, canvasY){
58
      currentTarget = TAEGET.NONE;
59
      blockManager.settleBlock(canvasX, canvasY, canvasBlockHRatio);
60
    }
61

    
62
    //キャンバスの初期化
63
    var headerHeight = $('#header')[0].clientHeight;
64
    var footerHeight = $('#footer')[0].clientHeight;
65
    var canvasHeight = document.documentElement.clientHeight - headerHeight - footerHeight;
66
    canvas = canvasManager.initializeCanvas(document.documentElement.clientWidth, canvasHeight, touchStart, touchMove, touchEnd);
67
    $('#coding-tab').append(canvas);
68
    canvasContext = canvas.getContext("2d");
69

    
70
    //プレビューの初期化
71
    var previewCanvas = canvasManager.initializeCanvas(canvasHeight/2*1.25, canvasHeight/2, null, null, null);
72
    previewCanvas.style.position = "absolute";
73
    previewCanvas.style.top = "0px";
74
    previewCanvas.style.right = "0px";
75
    var previewCanvasContext = previewCanvas.getContext("2d");
76
    previewManager.initialize(previewCanvas, previewCanvasContext);
77
    $('#coding-tab').append(previewCanvas);
78

    
79
    //ターゲットの設定
80
    blockManager.setTarget(0, 0, blockManager.getTargetBlock());
81

    
82
    //block画像の設定
83
    _.each(blockManager.blocks, function(block){
84
      block.image = imageManager.images[block.imageKey];
85
    });
86

    
87
    drawBlocks();
88
  }
89

    
90
  function didSwitchToCodingTab(){
91
    blockManager.setTarget(0, 0, blockManager.getTargetBlock());
92
  }
93

    
94
  function didQuitCodingTab(){
95

    
96
  }
97

    
98
  function afterAddBlock(blockId){
99
    blockManager.createNewBlock(blockId);
100
    drawBlocks();
101
  }
102

    
103
  function drawBlocks(){
104
    blockDrawer.drawBlocks(canvas, canvasContext, blockManager.blocks, getCvsBlkhRatio());
105
  }
106

    
107
  function getFunctions(){
108
    return blockManager.getFunctions();
109
  }
110

    
111
  function getCvsBlkhRatio(){
112
    var size =  (document.documentElement.clientWidth + document.documentElement.clientHeight)/2;
113
    var maxNumberOfBlock = 50;
114
    return size/maxNumberOfBlock*magnification;
115
  }
116

    
117
  function didSetTarget(targetBlock){
118
    previewManager.startPreview(targetBlock);
119
  }
120

    
121
  $(document).on("click","#unlink_block", function(event) {
122
    blockManager.unlinkBlock();
123
  });
124

    
125
  $(document).on("click","#copy_block", function(event) {
126
    blockManager.copyBlock();
127
  });
128

    
129
  $(document).on("click","#delete_block", function(event) {
130
    blockManager.deleteBlock();
131
  });
132

    
133
  $(document).on("click","#expland_block", function(event) {
134
    magnification++;
135
    if (magnification > 9) {
136
      magnification = 9;
137
    }else{
138
      drawBlocks();
139
    };
140
  });
141

    
142
  $(document).on("click","#shrink_block", function(event) {
143
    magnification--;
144
    if (magnification < 2) {
145
      magnification = 2;
146
    }else{
147
      drawBlocks();
148
    };
149
  });
150

    
151
  return {
152
    initialize:initialize,
153
    didSwitchToCodingTab:didSwitchToCodingTab,
154
    didQuitCodingTab:didQuitCodingTab,
155
    getFunctions:getFunctions,
156
    findFunctionWithId:blockManager.findFunctionWithId,
157
  };
158
});