/
frame.js
205 lines (158 loc) · 4.75 KB
/
frame.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
function Frame(rune)
{
Module.call(this,rune);
this.element = null;
this.size = new Rect("200x200");
this.layers = {};
this.active_layer = null;
this.render_layer = null;
this.add_method(new Method("resize",[new Position().name,new Rect().name]));
this.add_method(new Method("select",["text"]));
this.add_mode(new Mode("resize"));
this.install = function()
{
this.select(new Command("frame.select background"));
// Canvas
var starting_canvas = new Rect();
starting_canvas.width = window.innerWidth - 100;
starting_canvas.height = window.innerHeight - 100;
// Clamp
starting_canvas.width = parseInt(starting_canvas.width/40) * 40 - 40;
starting_canvas.height = parseInt(starting_canvas.height/40) * 40 - 40;
this.resize(new Command(starting_canvas.width+"x"+starting_canvas.height));
}
// Methods
this.resize = function(cmd, preview = false)
{
var rect = cmd.rect();
var position = cmd.position() ? cmd.position() : new Position(0,0);
if(preview){ ronin.overlay.draw(position,rect); return; }
for(layer_name in ronin.frame.layers){
ronin.frame.layers[layer_name].resize(rect);
}
ronin.frame.element.width = rect.width * 2;
ronin.frame.element.height = rect.height * 2;
ronin.frame.element.style.width = rect.width+"px";
ronin.frame.element.style.height = rect.height+"px";
ronin.frame.element.style.left = (window.innerWidth - rect.width)/2;
ronin.frame.element.style.top = (window.innerHeight - rect.height)/2;
ronin.on_resize();
this.size = rect;
return 1, "Resized to "+this.size.toString();
}
this.select = function(cmd, preview = false)
{
if(preview){ return; }
var layer_name = cmd.values();
if(!ronin.frame.layers[layer_name]){
this.add_layer(new Layer(layer_name));
}
this.select_layer(this.layers[layer_name]);
ronin.modules["layer"] = this.layers[layer_name];
ronin.layer = this.layers[layer_name];
return 1, "Selected "+this.active_layer.name;
}
this.context = function()
{
return this.active_layer.context();
}
// Misc
this.blink = function()
{
Object.keys(ronin.frame.layers).forEach(function (key) {
ronin.frame.layers[key].blink();
});
setTimeout(function(){ ronin.frame.blink(); }, 30);
}
this.center = function()
{
ronin.frame.element.style.left = (window.innerWidth/2) - (ronin.frame.element.width/4);
ronin.frame.element.style.top = (window.innerHeight/2) - (ronin.frame.element.height/4) - 30;
}
this.select_layer = function(layer)
{
if(!layer || layer.manager){ return; }
this.active_layer = layer;
}
this.add_layer = function(layer)
{
if(this.active_layer){layer.set_depth(this.active_layer.depth+1);}
layer.resize(this.size);
this.layers[layer.name] = layer;
this.element.appendChild(layer.element);
}
// Commands
this.layer_above = function()
{
var keys = Object.keys(ronin.frame.layers);
var loc = keys.indexOf(this.active_layer.name);
if(loc >= keys.length-1){ console.log("Reached end"); return false; }
if(keys[loc+1] != null){ return ronin.frame.layers[keys[loc+1]]; }
}
this.layer_below = function()
{
var keys = Object.keys(ronin.frame.layers);
var loc = keys.indexOf(this.active_layer.name);
if(keys[loc-1] != null){ return ronin.frame.layers[keys[loc-1]]; }
}
// Cursor
this.mouse_mode = function()
{
return "crop";
}
this.mouse_down = function(position)
{
ronin.overlay.draw(position);
}
this.mouse_move = function(position,rect)
{
ronin.overlay.draw(this.mouse_from,rect);
}
this.mouse_up = function(position,rect)
{
ronin.overlay.draw(this.mouse_from,rect)+" "+rect.toString();
var line = "frame.resize "+this.mouse_from.toString()+" "+rect.toString();
ronin.terminal.update(line);
}
this.widget = function()
{
var html = ""
html += this.size.toString()+" ";
html += this.active_layer.name+" ";
var user_layers = 0;
var managed_layers = 0;
count = 0;
for(id in this.layers){
if(this.layers[id].manager){
managed_layers += 1;
}
else{
user_layers += 1;
}
count += 1;
}
html += user_layers+"&"+managed_layers+" ";
html += this.widget_map()+" "
return html
}
this.widget_map = function()
{
html = ""
var keys = Object.keys(ronin.frame.layers);
var loc = keys.indexOf(this.active_layer.name);
i = 0;
while(i < keys.length){
if(i == loc){
html += "<span style='color:white'>|</span>";
}
else if(this.layers[keys[i]].manager){
html += "<span style='color:red'>|</span>";
}
else{
html += "|";
}
i += 1;
}
return html;
}
}