Skip to content

Commit aedea48

Browse files
committed
Change minos2 coordinates to floating point for better integration with OpenGL
1 parent f167fff commit aedea48

File tree

2 files changed

+100
-86
lines changed

2 files changed

+100
-86
lines changed

minos2/widgets-test.fs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,37 @@ frame new value f6
3030
text new value f7
3131
text new value f8
3232

33+
: dw* ( f -- f' ) dpy-w @ fm* ;
34+
: dh* ( f -- f' ) dpy-h @ fm* ;
3335

3436
: !f1 ( -- ) f1 >o
35-
0 0 dpy-w @ 4 / 0 dpy-h @ 2/ resize
36-
32 border ! $FFFFFFFF frame-color !
37+
0e 0e .25e dw* 0e .5e dh* resize
38+
32e border sf! $FFFFFFFF frame-color !
3739
button2 o> ;
3840

3941
: !f2 ( -- ) f2 >o
40-
dpy-w @ 2/ 0 dpy-w @ 2/ 0 dpy-h @ 19 20 */ resize
41-
32 border ! $FF7FFFFF frame-color !
42+
.5e dw* 0e .5e dw* 0e .95e dh* resize
43+
32e border sf! $FF7FFFFF frame-color !
4244
button3 o> ;
4345

4446
: !f3 ( -- ) f3 >o
45-
0 dpy-h @ 2/ dpy-w @ 2/ 0 dpy-h @ 2/ 2/ resize
46-
16 border ! $FFFF7FFF frame-color !
47+
0e .5e dh* .5e dw* 0e .25e dh* resize
48+
16e border sf! $FFFF7FFF frame-color !
4749
button1 o> ;
4850

4951
: !f4 ( -- ) f4 >o
50-
0 dpy-h @ 3 4 */ dpy-w @ 4 / 0 dpy-h @ 5 / resize
51-
32 border ! $FF7F7FFF frame-color !
52+
0e .75e dh* .25e dw* 0e .2e dh* resize
53+
32e border sf! $FF7F7FFF frame-color !
5254
button1 o> ;
5355

5456
: !f5 ( -- ) f5 >o
55-
dpy-w @ 4 / dpy-h @ 3 4 */ dpy-w @ 4 / 0 dpy-h @ 5 / resize
56-
8 border ! $7FFF7FFF frame-color !
57+
.25e dw* .75e dh* .25e dw* 0e .2e dh* resize
58+
8e border sf! $7FFF7FFF frame-color !
5759
button1 o> ;
5860

5961
: !f6 ( -- ) f6 >o
60-
dpy-w @ 4 / 0 dpy-w @ 4 / 0 dpy-h @ 2/ resize
61-
16 border ! $7FFFFFFF frame-color !
62+
.25e dw* 0e .25e dw* 0e .5e dh* resize
63+
16e border sf! $7FFFFFFF frame-color !
6264
button2 o> ;
6365

6466
also freetype-gl
@@ -93,11 +95,11 @@ texture_font_new_from_file Value font2
9395
previous
9496

9597
: !f7 ( -- ) f7 >o
96-
8 x ! dpy-h @ 4 / y ! "Dös isch a Tägscht!" text-string $!
98+
8e x sf! .25e dh* y sf! "Dös isch a Tägscht!" text-string $!
9799
$884400FF text-color ! font1 text-font ! o> ;
98100

99101
: !f8 ( -- ) f8 >o
100-
8 x ! dpy-h @ 5 8 */ y ! "这是一个文本:在德语说" text-string $!
102+
8e x sf! .625e dh* y sf! "这是一个文本:在德语说" text-string $!
101103
$004488FF text-color ! font2 text-font ! o> ;
102104

103105
: !widgets ( -- ) !f1 !f2 !f3 !f4 !f5 !f6 !f7 !f8 ;

minos2/widgets.fs

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,36 @@ end-class actor
5151
object class
5252
field: next-w
5353
field: parent-w
54-
field: x
55-
field: y
56-
field: w
57-
field: h \ above baseline
58-
field: d \ below baseline
54+
sffield: x
55+
sffield: y
56+
sffield: w
57+
sffield: h \ above baseline
58+
sffield: d \ below baseline
5959
method draw-init ( -- ) \ init draw
6060
method draw-bg ( -- ) \ button background draw
6161
method draw-icon ( -- ) \ icons draw
6262
method draw-thumbnail ( -- ) \ thumbnails draw
6363
method draw-image ( -- ) \ image draw
6464
method draw-text ( -- ) \ text draw
65-
method hglue ( -- typ sub add )
66-
method dglue ( -- typ sub add )
67-
method vglue ( -- typ sub add )
68-
method hglue@ ( -- typ sub add ) \ cached variant
69-
method dglue@ ( -- typ sub add ) \ cached variant
70-
method vglue@ ( -- typ sub add ) \ cached variant
71-
method xywh ( -- x0 y0 w h )
72-
method xywhd ( -- x y w h d )
73-
method resize ( x y w h d -- )
65+
method hglue ( -- rtyp rsub radd )
66+
method dglue ( -- rtyp rsub radd )
67+
method vglue ( -- rtyp rsub radd )
68+
method hglue@ ( -- rtyp rsub radd ) \ cached variant
69+
method dglue@ ( -- rtyp rsub radd ) \ cached variant
70+
method vglue@ ( -- rtyp rsub radd ) \ cached variant
71+
method xywh ( -- rx0 ry0 rw rh )
72+
method xywhd ( -- rx ry rw rh rd )
73+
method resize ( rx ry rw rh rd -- )
7474
method !size \ set your own size
7575
end-class widget
7676

77-
:noname x @ y @ h @ - w @ h @ d @ + ; widget to xywh
78-
:noname x @ y @ w @ h @ d @ ; widget to xywhd
77+
:noname x sf@ y sf@ h sf@ f- w sf@ h sf@ d sf@ f+ ; widget to xywh
78+
:noname x sf@ y sf@ w sf@ h sf@ d sf@ ; widget to xywhd
7979
' noop widget to !size
80-
:noname w @ 0 0 ; widget to hglue
81-
:noname h @ 0 0 ; widget to vglue
82-
:noname d @ 0 0 ; widget to dglue
83-
:noname d ! h ! w ! y ! x ! ; widget to resize
80+
:noname w sf@ 0e fdup ; widget to hglue
81+
:noname h sf@ 0e fdup ; widget to vglue
82+
:noname d sf@ 0e fdup ; widget to dglue
83+
:noname d sf! h sf! w sf! y sf! x sf! ; widget to resize
8484
' hglue widget to hglue@
8585
' vglue widget to vglue@
8686
' dglue widget to dglue@
@@ -90,22 +90,22 @@ style-tex 1024 dup rgba-newtex
9090

9191
\ glues
9292

93-
begin-structure glue-s
94-
cell +field glue-t \ typical size
95-
cell +field glue-s \ shrink by
96-
cell +field glue-a \ add by
93+
begin-structure glues
94+
sffield: glue-t \ typical size
95+
sffield: glue-s \ shrink by
96+
sffield: glue-a \ add by
9797
end-structure
9898

9999
widget class
100-
3 cells +field hglue-c
101-
3 cells +field dglue-c
102-
3 cells +field vglue-c
100+
glues +field hglue-c
101+
glues +field dglue-c
102+
glues +field vglue-c
103103
end-class glue
104104

105-
: @+ ( addr -- u addr' ) dup >r @ r> cell+ ;
106-
: !- ( addr -- u addr' ) dup >r ! r> cell- ;
107-
: glue@ ( addr -- t s a ) @+ @+ @ ;
108-
: glue! ( t s a addr -- ) 2 cells + !- !- ! ;
105+
: sf@+ ( addr -- u addr' ) dup sf@ sfloat+ ;
106+
: sf!- ( addr -- u addr' ) dup sf! [ 1 sfloats ]L - ;
107+
: glue@ ( addr -- t s a ) sf@+ sf@+ sf@ ;
108+
: glue! ( t s a addr -- ) [ 2 sfloats ]L + sf!- sf!- sf! ;
109109
:noname hglue-c glue@ ; dup glue to hglue@ glue to hglue
110110
:noname dglue-c glue@ ; dup glue to dglue@ glue to dglue
111111
:noname vglue-c glue@ ; dup glue to vglue@ glue to vglue
@@ -137,29 +137,29 @@ end-class tile
137137
x2 y1 >xy frame-color @ rgba>c n> 1e 0e frame# @ #>st v+
138138
x1 y1 >xy frame-color @ rgba>c n> 0e 0e frame# @ #>st v+
139139
v> dup i, dup 1+ i, dup 2 + i, dup i, dup 2 + i, 3 + i, ;
140+
: >xyxy ( rx ry rw rh -- rx0 ry0 rx1 ry1 )
141+
{ f: w f: h } fover w f+ fover h f+ ;
140142
: tile-draw ( -- )
141-
xywh { x y w h }
142-
x s>f y s>f x w + s>f y h + s>f
143-
draw-rectangle GL_TRIANGLES draw-elements ;
143+
xywh >xyxy draw-rectangle GL_TRIANGLES draw-elements ;
144144

145145
' tile-draw tile is draw-bg
146146

147147
\ frame widget
148148

149149
tile class
150-
field: border
150+
sffield: border
151151
end-class frame
152152

153153
Create button-st 0e sf, 0.25e sf, 0.75e sf, 1e sf,
154154
DOES> swap sfloats + sf@ ;
155155
: button-border ( n -- gray ) dup 2/ xor ;
156-
: >border ( x b i w -- r ) >r
157-
button-border >r
158-
r@ 1 and 0= IF drop 0 THEN
159-
r> 2 and IF negate r@ + THEN + s>f rdrop ;
156+
: >border ( rx rb i rw -- r ) { f: w }
157+
button-border dup
158+
1 and 0= IF fdrop 0e THEN
159+
2 and IF fnegate w f+ THEN f+ ;
160160

161161
: frame-draw ( -- )
162-
frame# @ frame-color @ border @ xywh { f c b x y w h }
162+
frame# @ frame-color @ border sf@ xywh { f c f: b f: x f: y f: w f: h }
163163
i>off >v
164164
4 0 DO
165165
4 0 DO
@@ -188,12 +188,14 @@ Variable glyphs$
188188
: text-init ( -- )
189189
text-font @ to font text-string $@ glyphs$ $+! ;
190190
: text-text ( -- )
191-
x @ text-border @ + s>f penxy sf! y @ s>f penxy sfloat+ sf!
191+
x sf@ text-border sf@ f+ penxy sf! y sf@ penxy sfloat+ sf!
192192
text-font @ to font text-color @ color !
193193
text-string $@ render-string ;
194194
: text-!size ( -- )
195195
text-string $@ layout-string
196-
f>s text-border @ + d ! f>s text-border @ + h ! f>s text-border @ 2* + w ! ;
196+
text-border sf@ f+ d sf!
197+
text-border sf@ f+ h sf!
198+
text-border sf@ f2* f+ w sf! ;
197199
' text-init text to draw-init
198200
' text-text text to draw-text
199201
' text-!size text to !size
@@ -274,25 +276,33 @@ box class
274276
end-class vbox \ vertical alignment
275277
box class end-class zbox \ overlay alignment
276278

277-
: 0glue ( -- t s a ) 0 0 0 ;
278-
: 1glue ( -- t s a ) 0 0 [ -1 8 rshift ]L ; \ can have 128 1glues in a row
279+
1e20 fconstant 1fil
280+
1e40 fconstant 1fill
281+
1e60 fconstant 1filll
282+
283+
: fils ( f -- f' ) 1fil f* ;
284+
: fills ( f -- f' ) 1fill f* ;
285+
: fillls ( f -- f' ) 1filll f* ;
286+
287+
: 0glue ( -- t s a ) 0e 0e 0e ;
288+
: 1glue ( -- t s a ) 0e 0e 1fil ;
279289

280290
glue new Constant glue*1
281291
glue new Constant glue*2
282292
glue*1 >o 1glue hglue-c glue! 1glue dglue-c glue! 1glue vglue-c glue! o>
283-
glue*2 >o 1glue 2* hglue-c glue! 1glue 2* dglue-c glue! 1glue 2* vglue-c glue! o>
293+
glue*2 >o 1glue f2* hglue-c glue! 1glue f2* dglue-c glue! 1glue f2* vglue-c glue! o>
284294

285-
: g3>2 ( t s a -- min a ) over + >r - r> ;
295+
: g3>2 ( t s a -- min a ) fover f+ { f: a } f- a ;
286296

287-
: glue+ { t1 s1 a1 t2 s2 a2 -- t3 s3 a3 }
288-
t1 t2 + s1 s2 + a1 a2 + ;
289-
: glue* { t1 s1 a1 t2 s2 a2 -- t3 s3 a3 }
290-
t1 t2 max
291-
t1 s1 - t2 s2 - max over - 0 max
292-
t1 a1 + t2 a2 + min 2 pick - 0 max ;
297+
: glue+ { f: t1 f: s1 f: a1 f: t2 f: s2 f: a2 -- t3 s3 a3 }
298+
t1 t2 f+ s1 s2 f+ a1 a2 f+ ;
299+
: glue* { f: t1 f: s1 f: a1 f: t2 f: s2 f: a2 -- t3 s3 a3 }
300+
t1 t2 fmax
301+
t1 s1 f- t2 s2 f- fmax fover f- 0e fmax
302+
t1 a1 f+ t2 a2 f+ fmin 2 fpick f- 0e fmax ;
293303
: baseglue ( -- b 0 max )
294-
baseline @ 0 [ -1 1 rshift ]L ;
295-
: glue-drop ( t s a -- ) 2drop drop ;
304+
baseline sf@ 0e 1fil ;
305+
: glue-drop ( t s a -- ) fdrop fdrop fdrop ;
296306

297307
: hglue+ 0glue [: hglue@ glue+ ;] do-childs ;
298308
: dglue+ 0glue [: glue-drop dglue@ ;] do-childs ; \ last dglue
@@ -317,33 +327,34 @@ glue*2 >o 1glue 2* hglue-c glue! 1glue 2* dglue-c glue! 1glue 2* vglue-c glue! o
317327

318328
\ add glues up for hboxes
319329

320-
: hglue-step { gp ga rd rg rx -- gp ga rd' rg' rx' }
321-
gp ga rx x !
322-
hglue@ g3>2 { xmin xa }
323-
rg xa + gp ga */ rd - dup rd + rg xa +
324-
rot xmin + dup x @ - w ! ;
330+
: hglue-step { f: gp f: ga f: rd f: rg f: rx -- gp ga rd' rg' rx' }
331+
gp ga rx x sf!
332+
hglue@ g3>2 { f: xmin f: xa }
333+
rg xa f+ gp f* ga f/ rd f- fdup rd f+ rg xa f+
334+
frot xmin f+ fdup x sf@ f- w sf! ;
325335

326-
: hbox-resize1 { y h d -- y h d } x @ y w @ h d resize y h d ;
327-
: hbox-resize { x y w h d -- }
328-
hglue g3>2 { wmin a }
329-
w wmin - a 0 0 x ['] hglue-step do-childs 2drop 2drop drop
330-
y h d ['] hbox-resize1 do-childs drop 2drop ;
336+
: hbox-resize1 { f: y f: h f: d -- y h d } x sf@ y w sf@ h d resize y h d ;
337+
: hbox-resize { f: x f: y f: w f: h f: d -- }
338+
hglue g3>2 { f: wmin f: a }
339+
w wmin f- a 0e 0e x ['] hglue-step do-childs fdrop fdrop fdrop fdrop fdrop
340+
y h d ['] hbox-resize1 do-childs fdrop fdrop fdrop ;
331341

332342
' hbox-resize hbox is resize
333343

334344
\ add glues up for vboxes
335345

336-
: vglue-step { gp ga rd rg ry td sd ad -- gp ga rd' rg' ry' td' sd' ad' }
346+
: vglue-step { f: gp f: ga f: rd f: rg f: ry f: td f: sd f: ad -- gp ga rd' rg' ry' td' sd' ad' }
337347
gp ga baseglue
338348
vglue@ td sd ad glue+ glue* g3>2 { ymin ya }
339-
rg ya + gp ga */ rd - dup rd + rg ya +
340-
rot ymin baseline @ max + dup ry ! dglue@ ;
349+
rg ya f+ gp f* ga f/ rd f- fdup rd f+ rg ya f+
350+
frot ymin baseline sf@ fmax fdup d sf@ f- h sf! f+ fdup y sf! dglue@ ;
341351

342-
: vbox-resize1 { x w -- x w } x y @ w h @ d @ resize x w ;
343-
: vbox-resize { x y w h d -- }
352+
: vbox-resize1 { f: x f: w -- x w } x y sf@ w h sf@ d sf@ resize x w ;
353+
: vbox-resize { f: x f: y f: w f: h f: d -- }
344354
vglue g3>2 { hmin a }
345-
h hmin - a 0 0 y 0 0 0 ['] vglue-step do-childs 2drop 2drop 2drop 2drop
346-
x w ['] vbox-resize1 do-childs 2drop ;
355+
h hmin f- a 0e 0e y 0e 0e 0e ['] vglue-step do-childs
356+
fdrop fdrop fdrop fdrop fdrop fdrop fdrop
357+
x w ['] vbox-resize1 do-childs fdrop fdrop ;
347358

348359
$10 stack: box-depth
349360
: {{ ( -- ) depth box-depth >stack ;
@@ -352,4 +363,5 @@ $10 stack: box-depth
352363
: }}v ( n1 .. nm -- hbox ) }} vbox new >o +childs o o> ;
353364
: }}z ( n1 .. nm -- hbox ) }} zbox new >o +childs o o> ;
354365

355-
previous previous previous set-current
366+
previous previous previous
367+
set-current

0 commit comments

Comments
 (0)