-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditor.EditorMode.js
More file actions
162 lines (143 loc) · 5.23 KB
/
Copy pathEditor.EditorMode.js
File metadata and controls
162 lines (143 loc) · 5.23 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* This file is part of min.
*
* min is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* min is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with min. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2014 Richard Pospesel, Kevin Hart, Lei Hu, Siyu Zhu, David Stalnaker,
* Christopher Sasarak, Robert LiVolsi, Awelemdy Orakwue, and Richard Zanibbi
* (Document and Pattern Recognition Lab, RIT)
*/
/*
This object is the parent of all modes for the editor.
It defines the interface that an editor mode should have and also
contains code to be used by all events descended from it.
This constructor will check if we're on a touch device or a computer
and then select the appropriate events from that.
*/
function EditorMode(){
if(Modernizr.touch){
this.onDown = EditorMode.onTouchStart;
this.onMove = EditorMode.onTouchMove;
/*
On mobile devices both a mouse event and a touch event could fire this
creates problems in some cases such as when we need to check the number
of touches. EventStrings are what we should use to bind in each case.
Note that hammer events aren't included, they are fine with their own
strings for now.
*/
this.event_strings = {
"onDown": "touchstart",
"onMove": "touchmove",
"onUp": "touchend"
};
}
else{
this.onDown = EditorMode.onMouseDown;
this.onMove = EditorMode.onMouseMove;
this.event_strings = {
"onDown": "mousedown",
"onMove": "mousemove",
"onUp": "mouseup"
};
}
}
/*
This function is used for when we first switch into a mode in order
to set things up. e.g bind the event listeners, change colors, etc.
*/
EditorMode.prototype.init_mode = function(){
alert("init_mode not implemented!");
}
/*
This function is used for when we are finished with a mode
and need to do teardown, such as unbinding event handlers.
*/
EditorMode.prototype.close_mode = function(){
alert("close_mode not implemented!");
}
EditorMode.prototype.allEvents = function(e){
this.tmpLast = Editor.lastEvent;
Editor.lastEvent = e.originalEvent;
}
// Code to run for all modes with a mouse
EditorMode.onMouseDown = function(e){
this.allEvents(e);
if(e.button == 0){
Editor.mouse_position_prev = Editor.mouse_position;
Editor.mouse_position = new Vector2(e.pageX - Editor.div_position[0], e.pageY - Editor.div_position[1]);
}
}
// Code to run for all modes with a touch pad on touch
EditorMode.onTouchStart = function(e){
this.allEvents(e);
var first = e.originalEvent.changedTouches[0];
Editor.mouse_position_prev = Editor.mouse_position;
Editor.mouse_position = new Vector2(first.pageX - Editor.div_position[0], first.pageY - Editor.div_position[1]);
}
// Initial actions for a move event with a mouse.
EditorMode.onMouseMove = function(e){
this.allEvents(e);
Editor.mouse_position_prev = Editor.mouse_position;
Editor.mouse_position = new Vector2(e.pageX - Editor.div_position[0], e.pageY - Editor.div_position[1]);
Editor.mouse_move_distance = Vector2.Distance(Editor.mouse_position, Editor.mouse_position_prev);
}
// Initial actions for move with a finger
EditorMode.onTouchMove = function(e){
this.allEvents(e);
var first = e.originalEvent.changedTouches[0];
Editor.mouse_position_prev = Editor.mouse_position;
Editor.mouse_position = new Vector2(first.pageX - Editor.div_position[0], first.pageY - Editor.div_position[1]);
Editor.mouse_move_distance = Vector2.Distance(Editor.mouse_position, Editor.mouse_position_prev);
}
EditorMode.prototype.onUp = function(e){
this.allEvents(e);
}
EditorMode.prototype.onDoubleClick = function(e){
this.allEvents(e);
}
/*
This function creates another function for switching into
a new mode. The returned function can then be bound to a
button.
*/
EditorMode.mkModeSwitchFn = function(new_mode){
return function(e){
if(Editor.current_mode != null)
Editor.current_mode.close_mode();
Editor.current_mode = new_mode;
Editor.current_mode.init_mode();
};
}
// What do do for every keypress event in a mode
EditorMode.onKeyPress = function(e){
this.allEvents(e);
// TODO: This should eventually be uncommented
//if(e.keyCode == KeyCode.enter) {
// Editor.search();
// return;
//}
}
/*
This function take an event handler and returns a new one which
will ignore multiple touches. This function assumes we will be receiving a
jquery event.
*/
EditorMode.mkIgnoreMultipleTouches = function(wrap_fn){
return function(e){
// originalEvent is because JQuery only copies some event properties
if(e.originalEvent.touches.length > 1)
return;
wrap_fn(e);
};
}