-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneManager.js
143 lines (143 loc) · 4.32 KB
/
SceneManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* シーン管理クラス
* @classs
* @constructor
* @param {String} elementId 対象エレメントID。対象エレメントのマウスイベントを取得して色々する。
* @param {Bool} touchDevice trueならスマートフォン等のタッチパネル操作のブラウザ。falseならPC等のマウス操作のブラウザ。
*/
var SceneManager = function(elementId, touchDevice){
var self = this;
this.scene = null;
this.mouseInfo = new MouseInfo();
this.skipMode = false;
this.nextScene = null;
this.element = document.getElementById(elementId);
/**
* シーンを切り替える
* @param {Object} scene 切り替える対象のシーン
*/
this.changeScene = function(scene){
this.nextScene = scene;
};
this.updateMousePoint = function(event){
var rect = event.target.getBoundingClientRect();
self.mouseInfo.point.x = event.clientX - rect.left;
self.mouseInfo.point.y = event.clientY - rect.top;
};
this.mouseDown = function (event){
self.updateMousePoint(event);
self.mouseInfo.pressed = true;
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.mouseUp = function(event){
self.updateMousePoint(event);
self.mouseInfo.pressed = false;
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.mouseMove = function (event){
self.updateMousePoint(event);
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.mouseOut = function(event){
self.updateMousePoint(event);
self.mouseInfo.pressed = false;
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.touchStart = function (){
var e = event.touches[0];
self.updateMousePoint(e);
self.mouseInfo.pressed = true;
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.touchEnd = function(){
try{
// 何故かiPhoneでエラーになる?
// var e = event.touches[0];
// self.updateMousePoint(e);
self.mouseInfo.pressed = false;
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
if(touchDevice){
self.click();
}
}catch(e){
alert(e);
}
return false;
};
this.click = function(event){
if(self.onCanvasClick)
self.onCanvasClick();
};
this.touchMove = function (){
var e = event.touches[0];
self.updateMousePoint(e);
if(self.scene)
self.scene.updateMouseInfo(self.mouseInfo);
return false;
};
this.timerFunc = function(){
self.update();
self.draw();
++ self.frameCount;
};
this.update = function(){
self.mouseInfo.lastPressed = self.mouseInfo.pressed;
if(self.nextScene){
if(self.scene){
self.scene.finalize();
self.scene = null;
}
self.scene = self.nextScene;
self.nextScene = null;
self.scene.initialize();
}
if(self.scene){
self.scene.update();
}
};
this.draw = function(){
if(!self.skipMode){
// var ctx = self.canvas.getContext('2d');
// ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
// self.scene.draw(self.canvas);
if(self.scene){
self.scene.draw();
}
}
};
this.stopInterval = function(){
clearInterval(self.timerId);
self.timerId = null;
};
this.startInterval = function(skipMode){
self.skipMode = skipMode;
clearInterval(self.timerId);
if(self.skipMode){
self.timerId = setInterval(self.timerFunc, 0);
}else{
self.timerId = setInterval(self.timerFunc, 33);
}
};
if(!touchDevice){
this.element.onmousemove = this.mouseMove;
this.element.onmousedown = this.mouseDown;
this.element.onmouseup = this.mouseUp;
this.element.onmouseout = this.mouseOut;
}else{
this.element.ontouchmove = this.touchMove;
this.element.ontouchstart = this.touchStart;
this.element.ontouchend = this.touchEnd;
}
this.element.onclick = this.click;
};