/
Form.js
172 lines (143 loc) · 3.11 KB
/
Form.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
"use strict";
function Form(parent)
{
//Parent
this.parent = (parent !== undefined) ? parent : document.body;
//ID
var id = "form" + Form.id;
Form.id++;
//Create element
this.element = document.createElement("div");
this.element.id = id;
this.element.style.position = "absolute";
//Element atributes
this.size = new THREE.Vector2(0,0);
this.position = new THREE.Vector2(0,0);
this.visible = true;
this.enabled = true;
//Elements attached
this.spacing = new THREE.Vector2(10, 10);
this.rows = [];
this.rows.push([]);
//Add element to document
this.parent.appendChild(this.element);
}
//Form conter
Form.id = 0;
//Add a element to form (in actual row)
Form.prototype.add = function(elem)
{
if(this.rows.length > 0)
{
this.rows[this.rows.length - 1].push(elem);
if(elem.parent !== this.element)
{
elem.parent = this.element;
this.element.appendChild(elem.element);
}
}
}
//Create text element and add to form
Form.prototype.addText = function(text)
{
var element = new Text(this.element);
element.fit_content = true;
element.size.set(0, 20);
element.setAlignment(Text.LEFT);
element.setText(text);
this.add(element);
return element;
}
//Create div element and add to form
Form.prototype.addDivision = function(x, y)
{
var division = new Division(this.element);
division.size.set(x, y);
this.add(division);
return division;
}
//Add new row to form
Form.prototype.nextRow = function()
{
this.rows.push([]);
}
//Add last row from form
Form.prototype.removeLastRow = function()
{
if(this.rows.length > 0)
{
var row = this.rows.pop();
for(var i = 0; i < row.length; i++)
{
row[i].destroy();
}
}
}
//Remove element
Form.prototype.destroy = function()
{
try
{
this.parent.removeChild(this.element);
}
catch(e){}
}
//Update
Form.prototype.update = function(){}
//Update interface
Form.prototype.updateInterface = function()
{
//Set visiblity
if(this.visible)
{
this.element.style.visibility = "visible";
}
else
{
this.element.style.visibility = "hidden";
}
//Position and size trackers
var position = new THREE.Vector2(0, 0);
var size = this.size.set(0, 0);
//Updated attached elements
for(var i = 0; i < this.rows.length; i++)
{
var max_size_y = 0;
for(var j = 0; j < this.rows[i].length; j++)
{
var element = this.rows[i][j];
if(element.visible)
{
//Update element
element.position.set(position.x, position.y);
element.visible = this.visible;
element.updateInterface();
//Restore visibility
element.visible = true;
//Update position tracker
if(element.size.y > max_size_y)
{
max_size_y = element.size.y;
}
position.x += element.size.x + this.spacing.x;
}
}
//Update form size x
if(size.x < position.x)
{
size.x = position.x;
}
//Update position tracker
if(position.x !== 0)
{
position.x = 0;
position.y += max_size_y + this.spacing.y;
}
}
size.y = position.y;
//Update element
this.element.style.top = this.position.y + "px";
this.element.style.left = this.position.x + "px";
this.element.style.width = this.size.x + "px";
this.element.style.height = this.size.y + "px";
}