Skip to content

Commit

Permalink
ui: better example now that embeds in interfaces bug has been fixed in V
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Dec 14, 2023
1 parent 02953a8 commit 9e09f02
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 81 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Windows, macOS, Linux, Raspberry PI, Android, Web (WASM/emscripten) and likely m
music, sounds etc.
* [WIP] Export to different platforms via the `shy export` command.
* [WIP] Intuitive Qt/Widgets/QML (scene graph) inspired `ui` module
supporting *custom* UI items (currently full of BUGS).
supporting *custom* UI items.
* [WIP] ... much more :)

# Currently known downsides
Expand Down
96 changes: 43 additions & 53 deletions examples/ui/ui.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub fn (m &MyUIItem) draw(ui_ &ui.UI) {
m.EventArea.draw(ui_)
}

pub fn (m &MyUIItem) visual_state() ui.VisualState {
return m.EventArea.visual_state()
}

pub fn (m &MyUIItem) event(e ui.Event) ?&ui.Node {
// println('MyUIItem event called ${m.id}')
return m.EventArea.event(e)
Expand All @@ -50,6 +54,10 @@ pub fn (m &MyRect) draw(ui_ &ui.UI) {
m.Rectangle.draw(ui_)
}

pub fn (m &MyRect) visual_state() ui.VisualState {
return m.Rectangle.visual_state()
}

pub fn (m &MyRect) event(e ui.Event) ?&ui.Node {
// println('MyRect event called ${m.id}')
return m.Rectangle.event(e)
Expand All @@ -67,6 +75,7 @@ pub fn (mut a App) init() ! {
height: 100
body: [
&ui.Rectangle{
id: 43
x: 50
y: 50
width: 50
Expand All @@ -78,94 +87,91 @@ pub fn (mut a App) init() ! {
width: 25
height: 25
body: [
/*
&MyRect{
id: 800
width: 20
on_event: [
fn (n &ui.Node, e ui.Event) bool {
node := &&MyRect(n) // TODO this is a weird V quirk
eprintln('MyRect in call: ${ptr_str(n)}')
// eprintln('MyRect in call: ${ptr_str(n)} VS ${ptr_str(node)}')
assert node.id == n.id

// println('MyUIItem on_event reached ${node.id}/${n.id}')
// mut mmui := unsafe { node }
// mmui.width++
// println('${@STRUCT}/MyUIItem on_event was called ${mmui.width}')
mut mmui := unsafe { node }
if mmui.width < 80 {
mmui.width += 0.1
} else if mmui.width >= 50 {
mmui.width = 25
}
// return true
return false
},
]
},
*/
/*
&MyUIItem{
id: 400
width: 10
on_event: [
fn (n &ui.Node, e ui.Event) bool {
node := &&MyUIItem(n) // TODO this is a weird V quirk
eprintln('MyUIItem this in call: ${ptr_str(n)}')
// eprintln('MyUIItem this in call: ${ptr_str(n)} VS ${ptr_str(node)}')
assert node.id == n.id

// println('MyUIItem on_event reached ${node.id}/${n.id}')
// mut mmui := unsafe { node }
// mmui.width++
mut mmui := unsafe { node }
mmui.width += 0.01
// println('${@STRUCT}/MyUIItem on_event was called ${mmui.width}')
// return true
return false
},
]
},*/
},
]
},
]
},
/*
&ui.PointerEventArea{
id: 100
x: 50
y: 50
width: 500
height: 500
/*
on_event: [
fn (n &ui.Node, e ui.Event) bool {
//println('ui.EventArea on_event reached ${n.id}')
// println('ui.EventArea on_event reached ${n.id}')

ea := &&ui.EventArea(n)
assert ea.id == n.id
mut mea := unsafe { ea }
println('EventArea ${mea.id} found .x: ${mea.x}')
// mea.x += 1
// return true
return false
return true
// return false
},
]*/
]
on_pointer_event: [
fn (n &ui.Node, e ui.PointerEvent) bool {
// println('PointerEventArea on_pointer_event reached ${n.id}')

// TODO BUG V mixes up the pointer here see also `shy/ui/item.v` etc.
// dump(typeof(n))
/*

mut pea := &&ui.PointerEventArea(n)
eprintln('PointerEventArea n in call: ${ptr_str(n)}')
eprintln('PointerEventArea this in call: ${ptr_str(n.this)}')
// eprintln('PointerEventArea this in call: ${ptr_str(n.this)}')
eprintln('PointerEventArea &n in call: ${ptr_str(&n)}')
eprintln('PointerEventArea pea in call: ${ptr_str(pea)}')
// TODO `n &ui.Node` -> `n` pointer is not the same as in `ui/item.v`????
assert pea.id == n.id, 'HARD TO REPRODUCE V BUG'

println('PointerEventArea ${pea.id} found .x: ${pea.x}')
pea.x += 1
// pea.x += 1

// mut mpea := unsafe { pea }
// println('PointerEventArea ${mpea.id} found .x: ${mpea.x}')
// mpea.x += 1

// return true
*/

return false
},
]
Expand Down Expand Up @@ -211,7 +217,7 @@ pub fn (mut a App) init() ! {
]
},
]
},*/
},
]
}
a.ui = ui.new(
Expand All @@ -234,52 +240,36 @@ pub fn (mut a App) event(e shy.Event) {

ui_event := ui.shy_to_ui_event(e) or { panic('${@STRUCT}.${@FN}: ${err}') }
if handled_by_node := a.ui.event(ui_event) {
// printing the whole node will make things crash at runtime...
// TODO BUG printing the whole node will make things crash at runtime...
println('Event was handled by ui.Node.id(${handled_by_node.id})')
} else {
}

// Mutability test
a.ui.modify(fn (mut n ui.Node) {
// TODO BUG broken - V doesn't allow for retreiving back the original pointer of a *custom* type implementing ui.Node...
// The horrible thing is that it ~works... sometimes :(
// mut maybe := ui.cast[MyRect](n)
//

/*
if n.id == 420 {
mut my_rect := &&MyRect(n)
// println('oi ${maybe}')
my_rect.x += 0.5
my_rect.y += 0.25
if mut n is MyRect {
println('oi ${n.id}')
if n.id == 420 {
n.x += 0.5
n.y += 0.25
} else {
n.y += 0.1
}
}
*/

// mut maybe := &&MyRect(n)
// println('oi ${maybe}')
// if maybe.id == 420 {
// // println('oi ${maybe}')
// maybe.x += 0.5
// maybe.y += 0.25
//}
/*

if mut n is ui.Rectangle {
// mut n := unsafe { node }
if n.id == 42 {
if n.x < 200 {
n.x += 1
} else {
n.x = 0
}
}
// pid := n.parent() or { 0 }.id
// pid := n.parent().id
if n.id == 43 {
// ppid := n.parent() or { 0 }.id
// ppid := n.parent().id
// println('${pid} vs. ${ppid}')
// pid := n.parent() or { -1 }.id
pid := n.parent().id
println('parent id ${pid}')
}
}*/
}
})

/*
Expand Down
1 change: 0 additions & 1 deletion ui/event.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub enum ButtonState {
pub struct UIEvent {
pub:
timestamp u64
// window &Window // Since ui is based on shy.easy we have only one window
}

pub struct PointerEvent {
Expand Down
Loading

0 comments on commit 9e09f02

Please sign in to comment.