-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_div.go
executable file
·355 lines (280 loc) · 8.09 KB
/
ui_div.go
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"strconv"
"strings"
)
func (ui *Ui) Div_startEx(x, y, w, h int, rx, ry, rw, rh float64, name string) *UiLayoutDiv {
lv := ui.GetCall()
if !lv.call.gridLock {
// cols/rows resizer
lv.call.RenderResizeSpliter(ui)
lv.call.UpdateGrid(ui.win)
lv.call.lastChild = nil
lv.call.gridLock = true
}
grid := InitOsV4(x, y, w, h)
lv.call = lv.call.FindOrCreate(name, grid, ui.GetLastApp())
ui.renderStart(rx, ry, rw, rh)
return lv.call
}
func (ui *Ui) Div_startCoord(x, y, w, h int, coord OsV4, name string) *UiLayoutDiv {
lv := ui.GetCall()
cz := lv.call.canvas.Size
s := coord.Start.Sub(lv.call.canvas.Start)
return ui.Div_startEx(x, y, w, h, float64(s.X)/float64(cz.X), float64(s.Y)/float64(cz.Y), float64(coord.Size.X)/float64(cz.X), float64(coord.Size.Y)/float64(cz.Y), name)
}
func (ui *Ui) Div_startName(x, y, w, h int, name string) *UiLayoutDiv {
return ui.Div_startEx(x, y, w, h, 0, 0, 1, 1, name)
}
func (ui *Ui) Div_start(x, y, w, h int) *UiLayoutDiv {
return ui.Div_startName(x, y, w, h, "")
}
func (ui *Ui) Div_end() {
lv := ui.GetCall()
//if grid is empty
if !lv.call.gridLock {
// cols/rows resizer
lv.call.RenderResizeSpliter(ui)
lv.call.UpdateGrid(ui.win)
lv.call.lastChild = nil
lv.call.gridLock = true
}
ui.renderEnd(false)
}
func (ui *Ui) checkGridLock() bool {
//if root.levels == nil {
// return false
//}
lv := ui.GetCall()
if lv == nil {
return false
}
if lv.call.gridLock /*&& (app.debug == nil || app.debug.conn != nil)*/ {
lv.call.data.app.AddLogErr(errors.New("trying to changed col/row dimension after you already draw div into"))
return false
}
return true
}
func (ui *Ui) Div_col(pos int, val float64) float64 {
if !ui.checkGridLock() {
return -1
}
lv := ui.GetCall()
lv.call.GetInputCol(int(pos)).min = float32(val)
return float64(lv.call.data.cols.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_row(pos int, val float64) float64 {
if !ui.checkGridLock() {
return -1
}
lv := ui.GetCall()
lv.call.GetInputRow(int(pos)).min = float32(val)
return float64(lv.call.data.rows.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_colMax(pos int, val float64) float64 {
if !ui.checkGridLock() {
return -1
}
lv := ui.GetCall()
lv.call.GetInputCol(pos).max = float32(val)
return float64(lv.call.data.cols.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_rowMax(pos int, val float64) float64 {
if !ui.checkGridLock() {
return -1
}
lv := ui.GetCall()
lv.call.GetInputRow(int(pos)).max = float32(val)
return float64(lv.call.data.rows.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_colResize(pos int, name string, val float64, force bool) (bool, float64) {
if !ui.checkGridLock() {
return false, -1
}
lv := ui.GetCall()
//if 'resize' exist in layout than read it from there
if len(name) == 0 {
name = strconv.Itoa(int(pos))
}
res, found := lv.call.data.cols.FindOrAddResize(name)
if !found || force {
res.value = float32(val)
}
lv.call.GetInputCol(int(pos)).resize = res
active := (ui.touch.resize == lv.call.GetHash() && lv.call.touchResizeIsCol && pos == lv.call.touchResizeIndex)
return active, float64(lv.call.data.cols.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_rowResize(pos int, name string, val float64, force bool) (bool, float64) {
if !ui.checkGridLock() {
return false, -1
}
lv := ui.GetCall()
//if 'resize' exist in layout than read it from there
if len(name) == 0 {
name = strconv.Itoa(int(pos))
}
res, found := lv.call.data.rows.FindOrAddResize(name)
if !found || force {
res.value = float32(val)
}
lv.call.GetInputRow(int(pos)).resize = res
active := (ui.touch.resize == lv.call.GetHash() && !lv.call.touchResizeIsCol && pos == lv.call.touchResizeIndex)
return active, float64(lv.call.data.rows.GetOutput(int(pos))) / float64(ui.win.Cell())
}
func (ui *Ui) Div_SpacerRow(x, y, w, h int) {
pl := ui.win.io.GetPalette()
ui.Div_start(x, y, w, h)
ui.Paint_line(0, 0, 1, 1, 0.01,
0, 0.5, 1, 0.5,
pl.GetGrey(0.5), 0.03)
ui.Div_end()
}
func (ui *Ui) Div_SpacerCol(x, y, w, h int) {
pl := ui.win.io.GetPalette()
ui.Div_start(x, y, w, h)
ui.Paint_line(0, 0, 1, 1, 0.01,
0.5, 0, 0.5, 1,
pl.GetGrey(0.5), 0.03)
ui.Div_end()
}
func (ui *Ui) Div_drag(groupName string, id int) {
lv := ui.GetCall()
if lv.call.IsTouchActive(ui) {
drag := &ui.drag
//set
drag.div = lv.call
drag.group = groupName
drag.id = id
//paint
ui.Paint_rect(0, 0, 1, 1, 0, OsCd{0, 0, 0, 180}, 0) //fade
}
}
type SA_Drop_POS int
const (
SA_Drop_INSIDE SA_Drop_POS = 0
SA_Drop_V_LEFT SA_Drop_POS = 1
SA_Drop_V_RIGHT SA_Drop_POS = 2
SA_Drop_H_LEFT SA_Drop_POS = 3
SA_Drop_H_RIGHT SA_Drop_POS = 4
)
func (ui *Ui) Div_drop(groupName string, vertical bool, horizontal bool, inside bool) (int, SA_Drop_POS, bool) {
lv := ui.GetCall()
id := 0
pos := SA_Drop_INSIDE
done := false
touchPos := ui.win.io.touch.pos
q := lv.call.crop
drag := &ui.drag
if q.Inside(touchPos) && strings.EqualFold(drag.group, groupName) && lv.call != drag.div {
r := touchPos.Sub(lv.call.crop.Middle())
if vertical && horizontal {
arx := float32(OsAbs(r.X)) / float32(lv.call.crop.Size.X)
ary := float32(OsAbs(r.Y)) / float32(lv.call.crop.Size.Y)
if arx > ary {
if r.X < 0 {
pos = SA_Drop_H_LEFT
} else {
pos = SA_Drop_H_RIGHT
}
} else {
if r.Y < 0 {
pos = SA_Drop_V_LEFT
} else {
pos = SA_Drop_V_RIGHT
}
}
} else if vertical {
if r.Y < 0 {
pos = SA_Drop_V_LEFT
} else {
pos = SA_Drop_V_RIGHT
}
} else if horizontal {
if r.X < 0 {
pos = SA_Drop_H_LEFT
} else {
pos = SA_Drop_H_RIGHT
}
}
if inside {
if vertical {
q = q.AddSpaceY(lv.call.crop.Size.Y / 3)
}
if horizontal {
q = q.AddSpaceX(lv.call.crop.Size.X / 3)
}
if !vertical && !horizontal {
pos = 0
} else if q.Inside(touchPos) {
pos = 0
}
}
//paint
wx := float64(ui.CellWidth(0.1)) / float64(lv.call.canvas.Size.X)
wy := float64(ui.CellWidth(0.1)) / float64(lv.call.canvas.Size.Y)
switch pos {
case SA_Drop_INSIDE:
ui.Paint_rect(0, 0, 1, 1, 0, OsCd{0, 0, 0, 180}, 0.03)
case SA_Drop_V_LEFT:
ui.Paint_rect(0, 0, 1, wy, 0, OsCd{0, 0, 0, 180}, 0)
case SA_Drop_V_RIGHT:
ui.Paint_rect(0, 1-wy, 1, 1, 0, OsCd{0, 0, 0, 180}, 0)
case SA_Drop_H_LEFT:
ui.Paint_rect(0, 0, wx, 1, 0, OsCd{0, 0, 0, 180}, 0)
case SA_Drop_H_RIGHT:
ui.Paint_rect(1-wx, 0, 1, 1, 0, OsCd{0, 0, 0, 180}, 0)
}
id = drag.id
//if st.stack.data.touch_end {
if ui.win.io.touch.end {
done = true
}
}
return id, pos, done
}
func Div_DropMoveElementIndex(src int, dst int, pos SA_Drop_POS) int {
//check
if src < dst && (pos == SA_Drop_V_LEFT || pos == SA_Drop_H_LEFT) {
dst--
}
if src > dst && (pos == SA_Drop_V_RIGHT || pos == SA_Drop_H_RIGHT) {
dst++
}
return dst
}
func Div_DropMoveElement[T any](array_src *[]T, array_dst *[]T, src int, dst int, pos SA_Drop_POS) {
dst = Div_DropMoveElementIndex(src, dst, pos)
//move(by swap one-by-one)
if array_src == array_dst {
for i := src; i < dst; i++ {
(*array_dst)[i], (*array_dst)[i+1] = (*array_dst)[i+1], (*array_dst)[i]
}
for i := src; i > dst; i-- {
(*array_dst)[i], (*array_dst)[i-1] = (*array_dst)[i-1], (*array_dst)[i]
}
} else {
backup := (*array_src)[src]
//remove
*array_src = append((*array_src)[:src], (*array_src)[src+1:]...)
//insert
if dst < len(*array_dst) {
*array_dst = append((*array_dst)[:dst+1], (*array_dst)[dst:]...)
(*array_dst)[dst] = backup
} else {
*array_dst = append(*array_dst, backup)
}
}
}