diff options
| -rw-r--r-- | engine/UI/Button.go | 49 | ||||
| -rw-r--r-- | engine/UI/Label.go | 11 | ||||
| -rw-r--r-- | engine/UI/Style.go | 7 | ||||
| -rw-r--r-- | engine/UI/UIElement.go | 1 | ||||
| -rw-r--r-- | engine/main.go | 11 |
5 files changed, 70 insertions, 9 deletions
diff --git a/engine/UI/Button.go b/engine/UI/Button.go new file mode 100644 index 0000000..8d336a9 --- /dev/null +++ b/engine/UI/Button.go @@ -0,0 +1,49 @@ +package ui + +import ( + rl "github.com/gen2brain/raylib-go/raylib" +) + +type DetectionType uint8 + +const ( + OnClick DetectionType = iota + OnHold +) + +type Button struct { + WidthWeight float32 + DisplayElement UIElement + EventType DetectionType + ButtonType rl.MouseButton + + clicked func(rl.MouseButton) bool +} + +func (base *Button) Init(parent *Menu) { + base.DisplayElement.Init(parent) + + switch base.EventType { + case OnClick: + base.clicked = rl.IsMouseButtonReleased + case OnHold: + base.clicked = rl.IsMouseButtonDown + } +} + +func (base *Button) Destroy() { +} + +func (base *Button) GetOccupationWeight() float32 { + return base.WidthWeight +} + +func (base *Button) Draw(position *rl.Rectangle) { + base.DisplayElement.Draw(position) + if base.clicked(base.ButtonType) { + var mousePos = rl.GetMousePosition() + if rl.CheckCollisionPointRec(mousePos, *position) { + rl.TraceLog(rl.LogInfo, "xdx") + } + } +} diff --git a/engine/UI/Label.go b/engine/UI/Label.go index a1fdf00..f412eff 100644 --- a/engine/UI/Label.go +++ b/engine/UI/Label.go @@ -3,6 +3,7 @@ package ui import rl "github.com/gen2brain/raylib-go/raylib" var defaultLabelPadding float32 = 5 +var defaultLabelRoundness float32 = 0.2 // If the text formation formula is applied without it the text is // overlowing on the right due to rounding errors @@ -12,6 +13,7 @@ var defaultLabelStyle = &Style{ BacgroundColor: &rl.LightGray, FontColor: &rl.DarkGray, Padding: &defaultLabelPadding, + Roundness: &defaultLabelRoundness , } type Label struct { @@ -28,12 +30,12 @@ func (base *Label) Init(*Menu) { func (base *Label) Destroy() { } -func (base *Label) Update() { -} - func (base *Label) Draw(position *rl.Rectangle) { // rl.TraceLog(rl.LogInfo, "Drawn at %v/%v/%v/%v", position.X, position.Y, position.Width, position.Height) - rl.DrawRectangleRec(*position, *base.Style.BacgroundColor) + rl.DrawRectangleRounded(*position, + *base.Style.Roundness, + 0, // Assume the segments param is always 0 - it doesn't really matter without a border + *base.Style.BacgroundColor) rl.DrawText(base.Text, int32(position.X+*base.Style.Padding), int32(position.Y+*base.Style.Padding), @@ -45,6 +47,7 @@ func (base *Label) GetOccupationWeight() float32 { return base.WidthWeight } +// Returns the size of the text that will not overflow in the side func (base *Label) getTextHeight(position *rl.Rectangle) int32 { var width = position.Width - 2 * *base.Style.Padding var outp = position.Height - 2 * *base.Style.Padding diff --git a/engine/UI/Style.go b/engine/UI/Style.go index 1901e4b..091a6a3 100644 --- a/engine/UI/Style.go +++ b/engine/UI/Style.go @@ -4,10 +4,14 @@ import ( rl "github.com/gen2brain/raylib-go/raylib" ) +// Arguments that UI elements use to render type Style struct { BacgroundColor *rl.Color FontColor *rl.Color Padding *float32 + + // Used in the drawing rectangles. Works like border-radius + Roundness *float32 } // Fills up not specifiend elements with default ones @@ -21,4 +25,7 @@ func (base *Style) FillMissing(defaultStyle *Style) { if base.FontColor == nil { base.FontColor = defaultLabelStyle.FontColor } + if base.Roundness == nil { + base.Roundness = defaultLabelStyle.Roundness + } } diff --git a/engine/UI/UIElement.go b/engine/UI/UIElement.go index b6f29ca..774efee 100644 --- a/engine/UI/UIElement.go +++ b/engine/UI/UIElement.go @@ -5,7 +5,6 @@ import rl "github.com/gen2brain/raylib-go/raylib" type UIElement interface { Init(*Menu) Destroy() - Update() // Gets the scale width of the element. Works similar to CSS's 'flex-grow' GetOccupationWeight() float32 // Draw the element with the given size diff --git a/engine/main.go b/engine/main.go index 7df8cae..0d2e657 100644 --- a/engine/main.go +++ b/engine/main.go @@ -52,10 +52,13 @@ func menu_test() *ui.Menu { { HeightWeight: 2, Objects: []ui.UIElement { - &ui.Label{ - Text: "Center div", - Style: ui.Style{ - BacgroundColor: &rl.Pink, + &ui.Button { + EventType: ui.OnClick, + DisplayElement: &ui.Label { + Text: "Center div", + Style: ui.Style { + BacgroundColor: &rl.White, + }, }, }, }, |
