From 1e77f52e46e40c0c82d1a482d19de28a8f6e9df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 30 Jun 2019 16:11:20 +0700 Subject: [PATCH 1/7] Object oriented widgets --- menu/scene_tabs.go | 26 ++++++++ menu/w2.go | 156 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 menu/w2.go diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index bb7e18ab..95b879be 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -304,4 +304,30 @@ func (tabs sceneTabs) drawHintBar() { Hint(&Props{}, "key-left-right", "NAVIGATE"), Hint(&Props{}, "key-x", "OPEN"), )() + + (&vBox{ + Color: video.Color{0, 0, 0, 0.2}, + BorderRadius: 0.1, + Padding: 20, + Children: []Widget{ + &label{ + Text: "Bonjour", + Scale: 0.8 * menu.ratio, + Height: 50, + Color: video.Color{0.2, 0.2, 0, 1}, + }, + &label{ + Text: "This is a longer piece of text", + Scale: 0.6 * menu.ratio, + Height: 50, + Color: video.Color{0, 0, 0, 1}, + }, + &hBox{ + Children: []Widget{ + mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), + mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), + }, + }, + }, + }).Draw(0, 0) } diff --git a/menu/w2.go b/menu/w2.go new file mode 100644 index 00000000..c0c356b1 --- /dev/null +++ b/menu/w2.go @@ -0,0 +1,156 @@ +package menu + +import ( + "github.com/libretro/ludo/video" +) + +type Widget interface { + Draw(x, y float32) + Layout() (w float32, h float32) + Size() (w float32, h float32) +} + +// HBox + +type hBox struct { + Width, Height float32 + Padding float32 + BorderRadius float32 + Color video.Color + Hidden bool + Children []Widget +} + +func (hb *hBox) Draw(x, y float32) { + hb.Layout() + vid.DrawRect(x, y, hb.Width+hb.Padding*2, hb.Height+hb.Padding*2, hb.BorderRadius, hb.Color) + var advance float32 + for _, child := range hb.Children { + w, _ := child.Size() + child.Draw(x+advance+hb.Padding, y+hb.Padding) + advance += w + } +} + +func (hb *hBox) Layout() (float32, float32) { + hb.Width = 0 + for _, child := range hb.Children { + w, h := child.Layout() + hb.Width += w + if h > hb.Height { + hb.Height = h + } + } + return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 +} + +func (hb *hBox) Size() (float32, float32) { + return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 +} + +// VBox + +type vBox struct { + Width, Height float32 + Padding float32 + BorderRadius float32 + Color video.Color + Hidden bool + Children []Widget +} + +func (vb *vBox) Draw(x, y float32) { + vb.Layout() + vid.DrawRect(x, y, vb.Width+vb.Padding*2, vb.Height+vb.Padding*2, vb.BorderRadius, vb.Color) + var advance float32 + for _, child := range vb.Children { + _, h := child.Size() + child.Draw(x+vb.Padding, y+advance+vb.Padding) + advance += h + } +} + +func (vb *vBox) Layout() (float32, float32) { + vb.Width = 0 + for _, child := range vb.Children { + w, h := child.Layout() + vb.Height += h + if w > vb.Width { + vb.Width = w + } + } + return vb.Width + vb.Padding*2, vb.Height + vb.Padding*2 +} + +func (vb *vBox) Size() (float32, float32) { + return vb.Width + vb.Padding*2, vb.Height + vb.Padding*2 +} + +// Label + +type label struct { + Width, Height float32 + Scale float32 + Color video.Color + Hidden bool + Text string +} + +func (lb *label) Draw(x, y float32) { + lb.Layout() + vid.Font.SetColor(lb.Color.R, lb.Color.G, lb.Color.B, lb.Color.A) + vid.Font.Printf(x, y+lb.Height*0.67, lb.Scale, lb.Text) +} + +func (lb *label) Layout() (float32, float32) { + lb.Width = vid.Font.Width(lb.Scale, lb.Text) + return lb.Width, lb.Height +} + +func (lb *label) Size() (float32, float32) { + return lb.Width, lb.Height +} + +// Image + +type image struct { + Width, Height float32 + Scale float32 + Color video.Color + Hidden bool + Image uint32 +} + +func (img *image) Draw(x, y float32) { + vid.DrawImage(img.Image, x, y, img.Width, img.Height, img.Scale, img.Color) +} + +func (img *image) Layout() (float32, float32) { + return img.Width, img.Height +} + +func (img *image) Size() (float32, float32) { + return img.Width, img.Height +} + +func mkButton(icon, txt string, c video.Color) Widget { + return &hBox{ + Color: c, + BorderRadius: 0.2, + Children: []Widget{ + &image{ + Width: 70, + Height: 70, + Color: video.Color{1, 1, 1, 1}, + Scale: 1, + Image: menu.icons[icon], + }, + &label{ + Height: 70, + Color: video.Color{1, 1, 1, 1}, + Scale: 0.6 * menu.ratio, + Text: txt, + }, + }, + } +} From f1a0f856e2fb3439082c3139d63a6be6b0ce58cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 30 Jun 2019 22:02:14 +0700 Subject: [PATCH 2/7] Contructors --- menu/scene_tabs.go | 37 ++++++++--------- menu/w2.go | 98 ++++++++++++++++++++++++++++------------------ 2 files changed, 76 insertions(+), 59 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index 95b879be..5d053d84 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -305,29 +305,26 @@ func (tabs sceneTabs) drawHintBar() { Hint(&Props{}, "key-x", "OPEN"), )() - (&vBox{ + mkVBox(wProps{ Color: video.Color{0, 0, 0, 0.2}, BorderRadius: 0.1, Padding: 20, - Children: []Widget{ - &label{ - Text: "Bonjour", - Scale: 0.8 * menu.ratio, - Height: 50, - Color: video.Color{0.2, 0.2, 0, 1}, - }, - &label{ - Text: "This is a longer piece of text", - Scale: 0.6 * menu.ratio, - Height: 50, - Color: video.Color{0, 0, 0, 1}, - }, - &hBox{ - Children: []Widget{ - mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), - mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), - }, + }, + mkLabel(wProps{ + Scale: 0.8 * menu.ratio, + Height: 50, + Color: video.Color{0.2, 0.2, 0, 1}, + }, "Bonjour"), + mkLabel(wProps{ + Scale: 0.6 * menu.ratio, + Height: 50, + Color: video.Color{0.2, 0.2, 0, 1}, + }, "This is a longer piece of text"), + &hBox{ + Children: []Widget{ + mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), + mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), }, }, - }).Draw(0, 0) + ).Draw(0, 0) } diff --git a/menu/w2.go b/menu/w2.go index c0c356b1..edb20710 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -10,15 +10,27 @@ type Widget interface { Size() (w float32, h float32) } -// HBox - -type hBox struct { - Width, Height float32 +type wProps struct { Padding float32 BorderRadius float32 + Width, Height float32 + Scale float32 Color video.Color Hidden bool - Children []Widget +} + +// HBox + +func mkHBox(props wProps, children ...Widget) Widget { + return &hBox{ + Children: children, + wProps: props, + } +} + +type hBox struct { + Children []Widget + wProps } func (hb *hBox) Draw(x, y float32) { @@ -50,13 +62,16 @@ func (hb *hBox) Size() (float32, float32) { // VBox +func mkVBox(props wProps, children ...Widget) Widget { + return &vBox{ + Children: children, + wProps: props, + } +} + type vBox struct { - Width, Height float32 - Padding float32 - BorderRadius float32 - Color video.Color - Hidden bool - Children []Widget + Children []Widget + wProps } func (vb *vBox) Draw(x, y float32) { @@ -89,11 +104,15 @@ func (vb *vBox) Size() (float32, float32) { // Label type label struct { - Width, Height float32 - Scale float32 - Color video.Color - Hidden bool - Text string + Text string + wProps +} + +func mkLabel(props wProps, text string) Widget { + return &label{ + Text: text, + wProps: props, + } } func (lb *label) Draw(x, y float32) { @@ -114,15 +133,19 @@ func (lb *label) Size() (float32, float32) { // Image type image struct { - Width, Height float32 - Scale float32 - Color video.Color - Hidden bool - Image uint32 + Texture uint32 + wProps +} + +func mkImage(props wProps, texture uint32) Widget { + return &image{ + Texture: texture, + wProps: props, + } } func (img *image) Draw(x, y float32) { - vid.DrawImage(img.Image, x, y, img.Width, img.Height, img.Scale, img.Color) + vid.DrawImage(img.Texture, x, y, img.Width, img.Height, img.Scale, img.Color) } func (img *image) Layout() (float32, float32) { @@ -134,23 +157,20 @@ func (img *image) Size() (float32, float32) { } func mkButton(icon, txt string, c video.Color) Widget { - return &hBox{ + return mkHBox(wProps{ Color: c, BorderRadius: 0.2, - Children: []Widget{ - &image{ - Width: 70, - Height: 70, - Color: video.Color{1, 1, 1, 1}, - Scale: 1, - Image: menu.icons[icon], - }, - &label{ - Height: 70, - Color: video.Color{1, 1, 1, 1}, - Scale: 0.6 * menu.ratio, - Text: txt, - }, - }, - } + }, + mkImage(wProps{ + Width: 70, + Height: 70, + Color: video.Color{1, 1, 1, 1}, + Scale: 1, + }, menu.icons[icon]), + mkLabel(wProps{ + Height: 70, + Color: video.Color{1, 1, 1, 1}, + Scale: 0.6 * menu.ratio, + }, txt), + ) } From b83029d7d46acfed511f51c8a7db18aebdc7036a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 30 Jun 2019 22:33:32 +0700 Subject: [PATCH 3/7] Simplify code --- menu/scene_tabs.go | 10 ++--- menu/w2.go | 97 +++++++++++++++++++--------------------------- 2 files changed, 44 insertions(+), 63 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index 5d053d84..6a7bb3f2 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -320,11 +320,9 @@ func (tabs sceneTabs) drawHintBar() { Height: 50, Color: video.Color{0.2, 0.2, 0, 1}, }, "This is a longer piece of text"), - &hBox{ - Children: []Widget{ - mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), - mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), - }, - }, + mkVBox(wProps{}, + mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), + mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), + ), ).Draw(0, 0) } diff --git a/menu/w2.go b/menu/w2.go index edb20710..9fefa904 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -19,88 +19,71 @@ type wProps struct { Hidden bool } -// HBox +// Box + +type box struct { + Children []Widget + Direction Direction + wProps +} func mkHBox(props wProps, children ...Widget) Widget { - return &hBox{ - Children: children, - wProps: props, + return &box{ + Children: children, + Direction: Horizontal, + wProps: props, } } -type hBox struct { - Children []Widget - wProps +func mkVBox(props wProps, children ...Widget) Widget { + return &box{ + Children: children, + Direction: Vertical, + wProps: props, + } } -func (hb *hBox) Draw(x, y float32) { +func (hb *box) Draw(x, y float32) { hb.Layout() vid.DrawRect(x, y, hb.Width+hb.Padding*2, hb.Height+hb.Padding*2, hb.BorderRadius, hb.Color) var advance float32 for _, child := range hb.Children { - w, _ := child.Size() - child.Draw(x+advance+hb.Padding, y+hb.Padding) - advance += w + w, h := child.Size() + switch hb.Direction { + case Horizontal: + child.Draw(x+advance+hb.Padding, y+hb.Padding) + advance += w + case Vertical: + child.Draw(x+hb.Padding, y+advance+hb.Padding) + advance += h + } } } -func (hb *hBox) Layout() (float32, float32) { +func (hb *box) Layout() (float32, float32) { hb.Width = 0 for _, child := range hb.Children { w, h := child.Layout() - hb.Width += w - if h > hb.Height { - hb.Height = h + switch hb.Direction { + case Horizontal: + hb.Width += w + if h > hb.Height { + hb.Height = h + } + case Vertical: + hb.Height += h + if w > hb.Width { + hb.Width = w + } } } return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 } -func (hb *hBox) Size() (float32, float32) { +func (hb *box) Size() (float32, float32) { return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 } -// VBox - -func mkVBox(props wProps, children ...Widget) Widget { - return &vBox{ - Children: children, - wProps: props, - } -} - -type vBox struct { - Children []Widget - wProps -} - -func (vb *vBox) Draw(x, y float32) { - vb.Layout() - vid.DrawRect(x, y, vb.Width+vb.Padding*2, vb.Height+vb.Padding*2, vb.BorderRadius, vb.Color) - var advance float32 - for _, child := range vb.Children { - _, h := child.Size() - child.Draw(x+vb.Padding, y+advance+vb.Padding) - advance += h - } -} - -func (vb *vBox) Layout() (float32, float32) { - vb.Width = 0 - for _, child := range vb.Children { - w, h := child.Layout() - vb.Height += h - if w > vb.Width { - vb.Width = w - } - } - return vb.Width + vb.Padding*2, vb.Height + vb.Padding*2 -} - -func (vb *vBox) Size() (float32, float32) { - return vb.Width + vb.Padding*2, vb.Height + vb.Padding*2 -} - // Label type label struct { From 505839aea8a8eb6cff425792a30b0a387a67ad0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Tue, 2 Jul 2019 11:15:21 +0700 Subject: [PATCH 4/7] Fixes --- menu/scene_tabs.go | 2 +- menu/w2.go | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index 6a7bb3f2..d8544136 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -320,7 +320,7 @@ func (tabs sceneTabs) drawHintBar() { Height: 50, Color: video.Color{0.2, 0.2, 0, 1}, }, "This is a longer piece of text"), - mkVBox(wProps{}, + mkHBox(wProps{}, mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), ), diff --git a/menu/w2.go b/menu/w2.go index 9fefa904..104dfa61 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -43,45 +43,45 @@ func mkVBox(props wProps, children ...Widget) Widget { } } -func (hb *box) Draw(x, y float32) { - hb.Layout() - vid.DrawRect(x, y, hb.Width+hb.Padding*2, hb.Height+hb.Padding*2, hb.BorderRadius, hb.Color) +func (b *box) Draw(x, y float32) { + b.Layout() + vid.DrawRect(x, y, b.Width+b.Padding*2, b.Height+b.Padding*2, b.BorderRadius, b.Color) var advance float32 - for _, child := range hb.Children { + for _, child := range b.Children { w, h := child.Size() - switch hb.Direction { + switch b.Direction { case Horizontal: - child.Draw(x+advance+hb.Padding, y+hb.Padding) + child.Draw(x+advance+b.Padding, y+b.Padding) advance += w case Vertical: - child.Draw(x+hb.Padding, y+advance+hb.Padding) + child.Draw(x+b.Padding, y+advance+b.Padding) advance += h } } } -func (hb *box) Layout() (float32, float32) { - hb.Width = 0 - for _, child := range hb.Children { +func (b *box) Layout() (float32, float32) { + b.Width = 0 + for _, child := range b.Children { w, h := child.Layout() - switch hb.Direction { + switch b.Direction { case Horizontal: - hb.Width += w - if h > hb.Height { - hb.Height = h + b.Width += w + if h > b.Height { + b.Height = h } case Vertical: - hb.Height += h - if w > hb.Width { - hb.Width = w + b.Height += h + if w > b.Width { + b.Width = w } } } - return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 + return b.Width + b.Padding*2, b.Height + b.Padding*2 } -func (hb *box) Size() (float32, float32) { - return hb.Width + hb.Padding*2, hb.Height + hb.Padding*2 +func (b *box) Size() (float32, float32) { + return b.Width + b.Padding*2, b.Height + b.Padding*2 } // Label From 6f401f6c8759152172e2e369d830dcae93e0e52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 14 Jul 2019 10:58:30 +0700 Subject: [PATCH 5/7] Support 4 direction margins and paddings --- menu/scene_tabs.go | 31 ++++++++++++++------ menu/w2.go | 73 ++++++++++++++++++++++++++++------------------ 2 files changed, 67 insertions(+), 37 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index d8544136..9d8bcfd0 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -308,21 +308,34 @@ func (tabs sceneTabs) drawHintBar() { mkVBox(wProps{ Color: video.Color{0, 0, 0, 0.2}, BorderRadius: 0.1, - Padding: 20, + Padding: dirs{40, 40, 40, 40}, + Margin: dirs{80, 80, 80, 80}, }, mkLabel(wProps{ - Scale: 0.8 * menu.ratio, - Height: 50, - Color: video.Color{0.2, 0.2, 0, 1}, + Scale: 0.8 * menu.ratio, + Height: 50, + Padding: dirs{20, 20, 0, 0}, + Color: video.Color{0.2, 0.2, 0, 1}, }, "Bonjour"), mkLabel(wProps{ - Scale: 0.6 * menu.ratio, - Height: 50, - Color: video.Color{0.2, 0.2, 0, 1}, + Scale: 0.6 * menu.ratio, + Height: 50, + Padding: dirs{20, 20, 0, 0}, + Color: video.Color{0.2, 0.2, 0, 1}, }, "This is a longer piece of text"), mkHBox(wProps{}, - mkButton("key-z", "Fuufuu", video.Color{0, 0.5, 0, 1}), - mkButton("key-x", "Lehleh", video.Color{0.5, 0, 0.5, 1}), + mkButton(wProps{ + Color: video.Color{0, 0.5, 0, 1}, + BorderRadius: 1, + Margin: dirs{20, 0, 0, 20}, + Padding: dirs{0, 0, 20, 40}, + }, "key-z", "Fuufuu"), + mkButton(wProps{ + Color: video.Color{0.5, 0.5, 0, 1}, + BorderRadius: 1, + Margin: dirs{20, 0, 0, 20}, + Padding: dirs{0, 0, 20, 40}, + }, "key-x", "Lehleh"), ), ).Draw(0, 0) } diff --git a/menu/w2.go b/menu/w2.go index 104dfa61..43fcb456 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -7,11 +7,20 @@ import ( type Widget interface { Draw(x, y float32) Layout() (w float32, h float32) - Size() (w float32, h float32) } +const ( + top int = iota + bottom + left + right +) + +type dirs [4]float32 + type wProps struct { - Padding float32 + Margin dirs + Padding dirs BorderRadius float32 Width, Height float32 Scale float32 @@ -45,16 +54,23 @@ func mkVBox(props wProps, children ...Widget) Widget { func (b *box) Draw(x, y float32) { b.Layout() - vid.DrawRect(x, y, b.Width+b.Padding*2, b.Height+b.Padding*2, b.BorderRadius, b.Color) + vid.DrawRect( + x+b.Margin[left], + y+b.Margin[top], + b.Width+b.Padding[left]+b.Padding[right], + b.Height+b.Padding[top]+b.Padding[bottom], + b.BorderRadius, + b.Color, + ) var advance float32 for _, child := range b.Children { - w, h := child.Size() + w, h := child.Layout() switch b.Direction { case Horizontal: - child.Draw(x+advance+b.Padding, y+b.Padding) + child.Draw(x+advance+b.Padding[left]+b.Margin[left], y+b.Padding[top]+b.Margin[top]) advance += w case Vertical: - child.Draw(x+b.Padding, y+advance+b.Padding) + child.Draw(x+b.Padding[left]+b.Margin[left], y+advance+b.Padding[top]+b.Margin[top]) advance += h } } @@ -77,11 +93,8 @@ func (b *box) Layout() (float32, float32) { } } } - return b.Width + b.Padding*2, b.Height + b.Padding*2 -} - -func (b *box) Size() (float32, float32) { - return b.Width + b.Padding*2, b.Height + b.Padding*2 + return b.Width + b.Padding[left] + b.Padding[right] + b.Margin[left] + b.Margin[right], + b.Height + b.Padding[top] + b.Padding[bottom] + b.Margin[top] + b.Margin[bottom] } // Label @@ -101,16 +114,18 @@ func mkLabel(props wProps, text string) Widget { func (lb *label) Draw(x, y float32) { lb.Layout() vid.Font.SetColor(lb.Color.R, lb.Color.G, lb.Color.B, lb.Color.A) - vid.Font.Printf(x, y+lb.Height*0.67, lb.Scale, lb.Text) + vid.Font.Printf( + x+lb.Padding[left]+lb.Margin[left], + y+lb.Padding[top]+lb.Margin[top]+lb.Height*0.67, + lb.Scale, + lb.Text, + ) } func (lb *label) Layout() (float32, float32) { lb.Width = vid.Font.Width(lb.Scale, lb.Text) - return lb.Width, lb.Height -} - -func (lb *label) Size() (float32, float32) { - return lb.Width, lb.Height + return lb.Width + lb.Padding[left] + lb.Padding[right] + lb.Margin[left] + lb.Margin[right], + lb.Height + lb.Padding[top] + lb.Padding[bottom] + lb.Margin[top] + lb.Margin[bottom] } // Image @@ -128,22 +143,24 @@ func mkImage(props wProps, texture uint32) Widget { } func (img *image) Draw(x, y float32) { - vid.DrawImage(img.Texture, x, y, img.Width, img.Height, img.Scale, img.Color) + vid.DrawImage( + img.Texture, + x+img.Padding[left]+img.Margin[left], + y+img.Padding[left]+img.Margin[left], + img.Width, + img.Height, + img.Scale, + img.Color, + ) } func (img *image) Layout() (float32, float32) { - return img.Width, img.Height -} - -func (img *image) Size() (float32, float32) { - return img.Width, img.Height + return img.Width + img.Padding[left] + img.Padding[right] + img.Margin[left] + img.Margin[right], + img.Height + img.Padding[top] + img.Padding[bottom] + img.Margin[top] + img.Margin[bottom] } -func mkButton(icon, txt string, c video.Color) Widget { - return mkHBox(wProps{ - Color: c, - BorderRadius: 0.2, - }, +func mkButton(props wProps, icon, txt string) Widget { + return mkHBox(props, mkImage(wProps{ Width: 70, Height: 70, From 2450631a7d7afc0eee2bf17b9558f09a4d1081df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 14 Jul 2019 11:16:48 +0700 Subject: [PATCH 6/7] Add the Dirs helper --- menu/scene_tabs.go | 22 +++++++++++----------- menu/w2.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index 9d8bcfd0..50da83bc 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -306,7 +306,7 @@ func (tabs sceneTabs) drawHintBar() { )() mkVBox(wProps{ - Color: video.Color{0, 0, 0, 0.2}, + Color: video.Color{0, 0, 0, 0.9}, BorderRadius: 0.1, Padding: dirs{40, 40, 40, 40}, Margin: dirs{80, 80, 80, 80}, @@ -314,27 +314,27 @@ func (tabs sceneTabs) drawHintBar() { mkLabel(wProps{ Scale: 0.8 * menu.ratio, Height: 50, - Padding: dirs{20, 20, 0, 0}, - Color: video.Color{0.2, 0.2, 0, 1}, - }, "Bonjour"), + Padding: Dirs(20, 0), + Color: video.Color{1, 1, 1, 1}, + }, "Hello world"), mkLabel(wProps{ Scale: 0.6 * menu.ratio, Height: 50, - Padding: dirs{20, 20, 0, 0}, - Color: video.Color{0.2, 0.2, 0, 1}, - }, "This is a longer piece of text"), + Padding: Dirs(20, 0), + Color: video.Color{1, 1, 1, 0.75}, + }, "This is a longer piece of text here"), mkHBox(wProps{}, mkButton(wProps{ Color: video.Color{0, 0.5, 0, 1}, BorderRadius: 1, - Margin: dirs{20, 0, 0, 20}, - Padding: dirs{0, 0, 20, 40}, + Margin: Dirs(20, 0, 0, 20), + Padding: Dirs(0, 0, 20, 40), }, "key-z", "Fuufuu"), mkButton(wProps{ Color: video.Color{0.5, 0.5, 0, 1}, BorderRadius: 1, - Margin: dirs{20, 0, 0, 20}, - Padding: dirs{0, 0, 20, 40}, + Margin: Dirs(20, 0, 0, 20), + Padding: Dirs(0, 0, 20, 40), }, "key-x", "Lehleh"), ), ).Draw(0, 0) diff --git a/menu/w2.go b/menu/w2.go index 43fcb456..c4b9081a 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -28,6 +28,18 @@ type wProps struct { Hidden bool } +// Dirs returns a padding or margin array, allowing specifying the 4 directions +// as {top, bottom, left, right}, but also taking shortcuts like +// {vertical, horizontal} and {allfour} +func Dirs(ds ...float32) dirs { + if len(ds) >= 4 { + return dirs{ds[0], ds[1], ds[2], ds[3]} + } else if len(ds) >= 2 { + return dirs{ds[0], ds[0], ds[1], ds[1]} + } + return dirs{ds[0], ds[0], ds[0], ds[0]} +} + // Box type box struct { From cea45aa62a1791a1238216e694d365eecec0d535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Andr=C3=A9=20Santoni?= Date: Sun, 14 Jul 2019 11:29:11 +0700 Subject: [PATCH 7/7] Fix image padding --- menu/scene_tabs.go | 21 ++++++++++++++------- menu/w2.go | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/menu/scene_tabs.go b/menu/scene_tabs.go index 50da83bc..ad5a61e5 100644 --- a/menu/scene_tabs.go +++ b/menu/scene_tabs.go @@ -308,8 +308,8 @@ func (tabs sceneTabs) drawHintBar() { mkVBox(wProps{ Color: video.Color{0, 0, 0, 0.9}, BorderRadius: 0.1, - Padding: dirs{40, 40, 40, 40}, - Margin: dirs{80, 80, 80, 80}, + Padding: Dirs(40), + Margin: Dirs(40), }, mkLabel(wProps{ Scale: 0.8 * menu.ratio, @@ -322,20 +322,27 @@ func (tabs sceneTabs) drawHintBar() { Height: 50, Padding: Dirs(20, 0), Color: video.Color{1, 1, 1, 0.75}, - }, "This is a longer piece of text here"), + }, "Here is a nice screenshot:"), + mkImage(wProps{ + Width: 640, + Height: 480, + Color: video.Color{1, 1, 1, 1}, + Scale: 1, + Padding: Dirs(20, 0), + }, menu.icons["img-dl"]), mkHBox(wProps{}, mkButton(wProps{ - Color: video.Color{0, 0.5, 0, 1}, + Color: video.Color{1, 1, 1, 0.25}, BorderRadius: 1, Margin: Dirs(20, 0, 0, 20), Padding: Dirs(0, 0, 20, 40), - }, "key-z", "Fuufuu"), + }, "key-z", "Cancel"), mkButton(wProps{ - Color: video.Color{0.5, 0.5, 0, 1}, + Color: video.Color{0, 0.5, 0, 1}, BorderRadius: 1, Margin: Dirs(20, 0, 0, 20), Padding: Dirs(0, 0, 20, 40), - }, "key-x", "Lehleh"), + }, "key-x", "Ok do this"), ), ).Draw(0, 0) } diff --git a/menu/w2.go b/menu/w2.go index c4b9081a..896f2aba 100644 --- a/menu/w2.go +++ b/menu/w2.go @@ -158,7 +158,7 @@ func (img *image) Draw(x, y float32) { vid.DrawImage( img.Texture, x+img.Padding[left]+img.Margin[left], - y+img.Padding[left]+img.Margin[left], + y+img.Padding[top]+img.Margin[top], img.Width, img.Height, img.Scale,