-
Notifications
You must be signed in to change notification settings - Fork 1
/
abs.js
204 lines (170 loc) · 5.94 KB
/
abs.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
/*
krpanoJS javascript plugin example / template
*/
var krpanoplugin = function() {
var local = this; // save the 'this' pointer from the current plugin object
var krpano = null; // the krpano and plugin interface objects
var plugin = null;
var plugincanvas = null; // optionally - a canvas object for graphic content
var plugincanvascontext = null;
// registerplugin - startup point for the plugin (required)
// - krpanointerface = krpano interface object
// - pluginpath = string with the krpano path of the plugin (e.g. "plugin[pluginname]")
// - pluginobject = the plugin object itself (the same as: pluginobject = krpano.get(pluginpath) )
local.registerplugin = function(krpanointerface, pluginpath, pluginobject) {
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 - end point for the plugin (optionally)
// - will be called from krpano when the plugin will be removed
// - everything that was added by the plugin (objects,intervals,...) should be removed here
local.unloadplugin = function() {
plugin = null;
krpano = null;
}
// Plugin functions
function get_x(destination, name, origin) {
if (typeof(origin) === 'undefined') origin = 'left';
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);
}
function get_y(destination, name, origin) {
if (typeof(origin) === 'undefined') origin = 'top';
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);
}
function get_width(destination, name) {
var layer = krpano.get('layer[' + name + ']');
var width = abs_width(layer);
krpano.set(destination, width);
}
function get_height(destination, name) {
var layer = krpano.get('layer[' + name + ']');
var height = abs_height(layer);
krpano.set(destination, height);
}
function get_scale(destination, name) {
var layer = krpano.get('layer[' + name + ']');
krpano.set(destination, abs_scale(layer));
}
// Private recursive functions
function abs_scale(layer) {
var parent = false;
if (layer.parent) {
parent = krpano.get('layer[' + layer.parent + ']');
return layer.scale * abs_scale(parent) / (parent.scalechildren ? 1 : parent.scale);
}
return layer.scale;
}
function abs_width(layer) {
return layer.pixelwidth / layer.scale * abs_scale(layer);
}
function abs_height(layer) {
return layer.pixelheight / layer.scale * abs_scale(layer);
}
function abs_x(layer) {
var left_values = ['lefttop', 'left', 'leftbottom'];
var center_values = ['top', 'center', 'bottom'];
var right_values = ['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 = layer.x;
if (left_values.contains(layer.align)) {
x = x * parent_scale;
}
if (center_values.contains(layer.align)) {
x = parent_width / 2 + x * parent_scale;
}
if (right_values.contains(layer.align)) {
x = parent_width - x * parent_scale;
}
if (center_values.contains(layer.edge)) {
x = x - abs_width(layer) / 2;
}
if (right_values.contains(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;
}
function abs_y(layer) {
var top_values = ['lefttop', 'top', 'righttop'];
var center_values = ['left', 'center', 'right'];
var bottom_values = ['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 (top_values.contains(layer.align)) {
y = Number(layer.y) * parent_scale;
}
if (center_values.contains(layer.align)) {
y = parent_height / 2 + Number(layer.y) * parent_scale;
}
if (bottom_values.contains(layer.align)) {
y = parent_height - Number(layer.y) * parent_scale;
}
if (center_values.contains(layer.edge)) {
y = y - abs_height(layer) / 2;
}
if (bottom_values.contains(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;
}
}
Array.prototype.contains = function(needle) {
for (i in this) {
if (this[i] == needle) {
return true;
}
}
return false;
}
};