-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAction.TransformSegments.js
209 lines (179 loc) · 7.6 KB
/
Action.TransformSegments.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* 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 file is responsible for handling all the animation that occurs when undo, redoing
, and resizing segments on the canvas.
It does some of it job by storing the old translation, scale and any other objects that
are needed and also applying the new values to the canvas segment(s).
*/
TransformSegments.animation_length = 0.25;
function TransformSegments(in_segments)
{
this.segments = new Array();
this.backup_scale = new Array();
this.backup_translation = new Array();
this.backup_world = new Array(); // Tuple representing world_mins, world_maxs
this.new_scale = new Array();
this.new_translation = new Array();
this.new_world = new Array();
for(var k = 0; k < in_segments.length; k++)
{
var segment = in_segments[k];
this.segments.push(segment);
this.backup_scale.push(segment.scale.clone());
this.backup_translation.push(segment.translation.clone());
this.backup_world.push(new Tuple(segment.world_mins.clone(), segment.world_maxs.clone()));
}
this.frames = 0.0;
this.start_time = 0;
this.undoing = true;
this.should_keep = false;
}
// need to call this to get the new values for each transform
TransformSegments.prototype.add_new_transforms = function(in_segments)
{
if(in_segments.length != this.segments.length)
console.log("ERROR in TransformSegments.prototype.add_new_transforms");
this.should_keep = true;
for(var k = 0; k < in_segments.length; k++)
{
var segment = in_segments[k];
this.new_scale.push(segment.scale.clone());
this.new_translation.push(segment.translation.clone());
this.new_world.push(new Tuple(segment.world_mins.clone(), segment.world_maxs.clone()));
}
}
TransformSegments.current;
TransformSegments.prototype.rescale = function(elapsed, utc_ms)
{
var current_time = (new Date()).getTime();
var delta = (current_time- utc_ms) / 1000.0; // time since last frame in seconds
if(elapsed == 0.0)
TransformSegments.current = this;
var fraction = elapsed / TransformSegments.animation_length;
if(fraction > 1.0)
fraction = 1.0;
if(this.undoing)
{
for(var j = 0; j < this.segments.length; j++)
{
var segment = this.segments[j];
segment.align_size = false;
segment.scale.Set(Vector2.Add(this.new_scale[j],Vector2.Multiply(fraction, Vector2.Subtract(this.backup_scale[j], this.new_scale[j]))));
segment.translation.Set(Vector2.Add(this.new_translation[j],Vector2.Multiply(fraction, Vector2.Subtract(this.backup_translation[j], this.new_translation[j]))));
segment.world_mins.Set(Vector2.Add(this.new_world[j].item1,Vector2.Multiply(fraction, Vector2.Subtract(this.backup_world[j].item1, this.new_world[j].item1))));
segment.world_maxs.Set(Vector2.Add(this.new_world[j].item2,Vector2.Multiply(fraction, Vector2.Subtract(this.backup_world[j].item2, this.new_world[j].item2))));
segment.update_extents();
}
}
else
{
for(var j = 0; j < this.segments.length; j++)
{
var segment = this.segments[j];
segment.scale.Set(Vector2.Add(this.backup_scale[j],Vector2.Multiply(fraction, Vector2.Subtract(this.new_scale[j], this.backup_scale[j]))));
segment.translation.Set(Vector2.Add(this.backup_translation[j],Vector2.Multiply(fraction, Vector2.Subtract(this.new_translation[j], this.backup_translation[j]))));
segment.world_mins.Set(Vector2.Add(this.backup_world[j].item1,Vector2.Multiply(fraction, Vector2.Subtract(this.new_world[j].item1, this.backup_world[j].item1))));
segment.world_maxs.Set(Vector2.Add(this.backup_world[j].item2,Vector2.Multiply(fraction, Vector2.Subtract(this.new_world[j].item2, this.backup_world[j].item2))));
segment.update_extents();
}
}
// set dirty flag
for(var j = 0; j < this.segments.length; j++)
{
this.segments[j].dirty_flag = true;
}
Editor.update_selected_bb();
RenderManager.render();
// Added because each TeX_Input element in the 'this.segments' array has to
// compensate for horizontal flip
for(var j = 0; j < this.segments.length; j++)
{
if(this.segments[j].constructor == TeX_Input){
this.segments[j].dirty_flag = true;
this.segments[j].change_offset = true;
this.segments[j].render();
}
}
this.frames++;
if(fraction == 1.0)
{
// bail out
TransformSegments.current = null;
var total_time = ((current_time - this.start_time) / 1000.0);
console.log("total time: " + total_time);
console.log("mean framerate: " + (this.frames / total_time));
return;
}
var total_delta = ((new Date()).getTime()- utc_ms) / 1000.0;
var sb = new StringBuilder();
sb.append("TransformSegments.current.rescale(").append(String(elapsed + total_delta)).append(',').append((new Date()).getTime()).append(");");
setTimeout(sb.toString());
}
TransformSegments.prototype.Undo = function()
{
this.framerate = 0.0;
this.frames = 0.0;
this.start_time = (new Date()).getTime();
this.undoing = true;
this.rescale(0.0, this.start_time);
}
TransformSegments.prototype.shouldKeep = function()
{
return this.should_keep;
// TODO: The block below isn't necessary. Comment left by ako9833
for(var k = 0; k < this.segments.length; k++)
{
var segment = this.segments[k];
if(segment.scale.equals(this.backup_scale[k]) == false)
return true;
if(segment.translation.equals(this.backup_translation[k]) == false)
return true;
}
return false;
}
TransformSegments.prototype.Apply = function()
{
this.framerate = 0.0;
this.frames = 0.0;
this.start_time = (new Date()).getTime();
this.undoing = false;
this.rescale(0.0, this.start_time);
}
TransformSegments.prototype.toXML = function()
{
var sb = new StringBuilder();
sb.append("<Action type=\"transform_segments\">").appendLine();
for(var k = 0; k < this.segments.length; k++)
{
var segment = this.segments[k];
sb.append("\t").append("<Transform instanceID=\"").append(String(segment.instance_id)).append("\" ");
sb.append("scale=\"").append(this.new_scale[k].toString()).append("\" translation=\"").append(this.new_translation[k].toString()).append("\"/>");
sb.append("world_mins=\"").append(this.new_world[k].item1.toString()).append("\" world_maxs=\"").append(this.new_world[k].item2.toString()).append("\"/>");
sb.appendLine();
}
sb.append("</Action>");
return sb.toString();
}
TransformSegments.prototype.toString = function()
{
return "TransformSegments";
}