Skip to content

Commit

Permalink
FIX: use screen offset when drawing widget
Browse files Browse the repository at this point in the history
  • Loading branch information
qtxie committed Aug 26, 2023
1 parent 26ddb8b commit 800ef46
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions modules/view/backends/terminal/definitions.reds
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ ROUNDED_RECT_F!: alias struct! [
#define TEXT_WRAP_FLAG 20h

render-func!: alias function! [
x [integer!]
y [integer!]
widget [widget!]
]

Expand Down
7 changes: 4 additions & 3 deletions modules/view/backends/terminal/screen.reds
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,16 @@ screen: context [
render-widget: func [
widget [widget!]
/local
sym [integer!]
p [red-block!]
obj [red-object!]
end [red-object!]
x y [integer!]
][
if widget/flags and WIDGET_FLAG_HIDDEN <> 0 [exit]

sym: WIDGET_TYPE(widget)
widget/render widget
x: 0 y: 0
_widget/to-screen-pt widget :x :y
widget/render x y widget

p: CHILD_WIDGET(widget)
if TYPE_OF(p) = TYPE_BLOCK [
Expand Down
28 changes: 25 additions & 3 deletions modules/view/backends/terminal/widget.reds
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ _widget: context [
]

default-render-func: func [
x [integer!]
y [integer!]
widget [widget!]
][
0
Expand Down Expand Up @@ -73,6 +75,25 @@ _widget: context [
y/value: as-integer g/box/top
]

to-screen-pt: func [
widget [widget!]
p-x [int-ptr!]
p-y [int-ptr!]
/local
x [float32!]
y [float32!]
][
x: F32_0
y: F32_0
while [all [widget <> null widget/type <> window]][ ;-- window is always full screen
x: x + widget/box/left
y: y + widget/box/top
widget: widget/parent
]
p-x/value: as-integer x
p-y/value: as-integer y
]

find-child: func [
widget [widget!]
x [float32!]
Expand Down Expand Up @@ -153,6 +174,8 @@ _widget: context [
]

render-text: func [
x [integer!]
y [integer!]
widget [widget!]
flags [integer!]
/local
Expand All @@ -165,7 +188,7 @@ _widget: context [
p [pixel!]
end [pixel!]
n cnt [integer!]
x y w h [integer!]
w h [integer!]
fg bg [integer!]
][
values: get-face-values widget
Expand All @@ -183,8 +206,7 @@ _widget: context [
fg: true-color << 24 or get-font-color font
]

x: 0 y: 0 w: 0 h: 0
_widget/get-offset widget :x :y
w: 0 h: 0
_widget/get-size widget :w :h

p: screen/buffer + (screen/width * y + x)
Expand Down
4 changes: 3 additions & 1 deletion modules/view/backends/terminal/widgets/button.reds
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ on-button-event: func [
]

draw-button: func [
x [integer!]
y [integer!]
widget [widget!]
/local
flags [integer!]
Expand All @@ -49,5 +51,5 @@ draw-button: func [
if WIDGET_FOCUSED?(widget) [
flags: flags or PIXEL_INVERTED
]
_widget/render-text widget flags
_widget/render-text x y widget flags
]
7 changes: 3 additions & 4 deletions modules/view/backends/terminal/widgets/field.reds
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,14 @@ on-field-edit: func [
]

draw-field: func [
x [integer!]
y [integer!]
widget [widget!]
/local
x y [integer!]
field [field-data!]
][
_widget/render-text widget 0
_widget/render-text x y widget 0
if WIDGET_FOCUSED?(widget) [
x: 0 y: 0
_widget/get-offset widget :x :y
field: as field-data! widget/data
screen/cursor-x: x + field/cursor
screen/cursor-y: y
Expand Down
4 changes: 3 additions & 1 deletion modules/view/backends/terminal/widgets/text.reds
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ init-text: func [
]

draw-text: func [
x [integer!]
y [integer!]
widget [widget!]
][
_widget/render-text widget 0
_widget/render-text x y widget 0
]

0 comments on commit 800ef46

Please sign in to comment.