-
Notifications
You must be signed in to change notification settings - Fork 1
/
abs.as
239 lines (200 loc) · 7.72 KB
/
abs.as
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
krpano as3 plugin example / template
for mxmlc (.as)
*/
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
import flash.system.*;
[SWF(width="400", height="300", backgroundColor="#000000")]
public class abs extends Sprite {
public var krpano : Object = null;
public var plugin : Object = null;
public function abs() {
if (stage == null) {
// startup when loaded inside krpano
this.addEventListener(Event.ADDED_TO_STAGE, versioncheck);
} else {
// direct startup - show plugin version info
stage.scaleMode = "noScale";
stage.align = "TL";
var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat("_sans",14,0xFFFFFF,false,false,false,null,null,"center");
txt.autoSize = "center";
txt.htmlText = "krpano\n\nabs plugin";
addChild(txt);
var resizefu:Function = function(event:Event=null):void {
txt.x = (stage.stageWidth - txt.width )/2;
txt.y = (stage.stageHeight - txt.height)/2;
}
stage.addEventListener(Event.RESIZE, resizefu);
resizefu();
}
}
private function versioncheck(evt:Event):void {
// compatibility check of the krpano version by using the old plugin interface:
// - the "version" must be at least "1.0.8.14"
// - and the "build" must be "2011-05-10" or greater
this.removeEventListener(Event.ADDED_TO_STAGE, versioncheck);
var oldkrpanointerface:Object = (getDefinitionByName("krpano_as3_interface") as Class)["getInstance"]();
if (oldkrpanointerface.get("version") < "1.0.8.14" || oldkrpanointerface.get("build") < "2011-05-10") {
oldkrpanointerface.trace(3, "abs plugin - too old krpano viewer version (min. 1.0.8.14)");
}
}
// registerplugin
// - the start for the plugin
// - this function will be called from krpano when the plugin will be loaded
public function registerplugin(krpanointerface:Object, pluginfullpath:String, pluginobject:Object):void {
// get the krpano interface and the plugin object
krpano = krpanointerface;
plugin = pluginobject;
// add a from xml callable functions:
plugin.get_x = get_x;
plugin.get_y = get_y;
plugin.get_width = get_width;
plugin.get_height = get_height;
plugin.get_scale = get_scale;
}
// unloadplugin
// - the end for the plugin
// - this function will be called from krpano when the plugin will be removed
public function unloadplugin():void {
plugin = null;
krpano = null;
}
// Plugin functions
private function get_x(destination:String, name:String, origin:String = 'left'):void {
var layer = krpano.get('layer[' + name + ']');
var x = abs_x(layer);
if (origin != 'left') {
var width = abs_width(layer);
if (origin == 'center') {
x = Math.round(x + width / 2);
} else if (origin == 'right') {
x = Math.round(x + width);
}
}
krpano.set(destination, x);
}
private function get_y(destination:String, name:String, origin:String = 'top'):void {
var layer = krpano.get('layer[' + name + ']');
var y = abs_y(layer);
if (origin != 'top') {
var height = abs_height(layer);
if (origin == 'center') {
y = Math.round(y + height / 2);
} else if (origin == 'bottom') {
y = Math.round(y + height);
}
}
krpano.set(destination, y);
}
private function get_width(destination:String, name:String):void {
var layer = krpano.get('layer[' + name + ']');
var width = abs_width(layer);
krpano.set(destination, width);
}
private function get_height(destination:String, name:String):void {
var layer = krpano.get('layer[' + name + ']');
var height = abs_height(layer);
krpano.set(destination, height);
}
private function get_scale(destination:String, name:String):void {
var layer = krpano.get('layer[' + name + ']');
krpano.set(destination, abs_scale(layer));
}
// Private recursive functions
private function abs_scale(layer:Object) {
if (layer.parent) {
var parent = krpano.get('layer[' + layer.parent + ']');
return layer.scale * abs_scale(parent) / (parent.scalechildren ? 1 : parent.scale);
}
return layer.scale;
}
private function abs_width(layer:Object) {
return layer.pixelwidth / layer.scale * abs_scale(layer);
}
private function abs_height(layer:Object) {
return layer.pixelheight / layer.scale * abs_scale(layer);
}
private function abs_x(layer:Object) {
var left_values:Array = ['lefttop', 'left', 'leftbottom'];
var center_values:Array = ['top', 'center', 'bottom'];
var right_values:Array = ['righttop', 'right', 'rightbottom'];
var parent_scale = 1;
var parent_width = Number(krpano.get('stagewidth'));
if (layer.parent) {
var parent = krpano.get('layer[' + layer.parent + ']');
parent_width = abs_width(parent);
parent_scale = abs_scale(parent) / (parent.scalechildren ? 1 : parent.scale);
}
if (!layer.align) layer.align = 'lefttop';
if (!layer.edge) layer.edge = layer.align;
var x = 0;
if (includes(left_values, layer.align)) {
x = Number(layer.x) * parent_scale;
}
if (includes(center_values, layer.align)) {
x = parent_width / 2 + Number(layer.x) * parent_scale;
}
if (includes(right_values, layer.align)) {
x = parent_width - Number(layer.x) * parent_scale;
}
if (includes(center_values, layer.edge)) {
x = x - abs_width(layer) / 2;
}
if (includes(right_values, layer.edge)) {
x = x - abs_width(layer);
}
x = x + Number(layer.ox) * parent_scale;
x = Math.round(x);
if (parent) {
return x + abs_x(parent);
} else {
return x;
}
}
private function abs_y(layer:Object) {
var top_values:Array = ['lefttop', 'top', 'righttop'];
var center_values:Array = ['left', 'center', 'right'];
var bottom_values:Array = ['leftbottom', 'bottom', 'rightbottom'];
var parent_scale = 1;
var parent_height = Number(krpano.get('stageheight'));
if (layer.parent) {
var parent = krpano.get('layer[' + layer.parent + ']');
parent_height = abs_height(parent);
parent_scale = abs_scale(parent) / (parent.scalechildren ? 1 : parent.scale);
}
if (!layer.align) layer.align = 'lefttop';
if (!layer.edge) layer.edge = layer.align;
var y = 0;
if (includes(top_values, layer.align)) {
y = Number(layer.y) * parent_scale;
}
if (includes(center_values, layer.align)) {
y = parent_height / 2 + Number(layer.y) * parent_scale;
}
if (includes(bottom_values, layer.align)) {
y = parent_height - Number(layer.y) * parent_scale;
}
if (includes(center_values, layer.edge)) {
y = y - abs_height(layer) / 2;
}
if (includes(bottom_values, layer.edge)) {
y = y - abs_height(layer);
}
y = y + Number(layer.oy) * parent_scale;
y = Math.round(y);
if (parent) {
return y + abs_y(parent);
} else {
return y;
}
}
private function includes(array:Array, element:Object) {
return array.indexOf(element) >= 0;
}
}
}