summaryrefslogtreecommitdiff
path: root/engine/UI
diff options
context:
space:
mode:
authorPhyscik <mynameisgennadiy@vk.com>2026-02-06 01:55:27 +0500
committerPhyscik <mynameisgennadiy@vk.com>2026-02-06 01:55:27 +0500
commit85666467056ccae2011128e0d38ea2dbd1efee8a (patch)
treefa91af8a8e9be05e6ef87a4ecddc6b3f0303dea4 /engine/UI
parente071f6670cf2d379237bf9cc66bd99696e48df8d (diff)
Menu description pt. 1
Diffstat (limited to 'engine/UI')
-rw-r--r--engine/UI/Button.go5
-rw-r--r--engine/UI/FlexElement.go20
-rw-r--r--engine/UI/GridColumn.go13
-rw-r--r--engine/UI/GridRow.go24
-rw-r--r--engine/UI/Label.go24
-rw-r--r--engine/UI/Menu.go12
-rw-r--r--engine/UI/Style.go8
-rw-r--r--engine/UI/Void.go36
8 files changed, 119 insertions, 23 deletions
diff --git a/engine/UI/Button.go b/engine/UI/Button.go
index a8b9168..f865c5f 100644
--- a/engine/UI/Button.go
+++ b/engine/UI/Button.go
@@ -16,6 +16,7 @@ type Button struct {
DisplayElement UIElement
EventType DetectionType
ButtonType rl.MouseButton
+ OnClick func()
clicked func(rl.MouseButton) bool
}
@@ -43,10 +44,10 @@ func (base *Button) GetOccupationWeight() float32 {
func (base *Button) Draw(position *rl.Rectangle) {
base.DisplayElement.Draw(position)
- if base.clicked(base.ButtonType) {
+ if base.OnClick != nil && base.clicked(base.ButtonType) {
var mousePos = rl.GetMousePosition()
if rl.CheckCollisionPointRec(mousePos, *position) {
- rl.TraceLog(rl.LogInfo, "xdx")
+ base.OnClick()
}
}
}
diff --git a/engine/UI/FlexElement.go b/engine/UI/FlexElement.go
index b8d1b38..4ec0f35 100644
--- a/engine/UI/FlexElement.go
+++ b/engine/UI/FlexElement.go
@@ -1,6 +1,8 @@
package ui
import (
+ "encoding/json"
+ "fmt"
"image/color"
rl "github.com/gen2brain/raylib-go/raylib"
@@ -32,13 +34,14 @@ func getWeightToPixelsRatio(base flexElement, totalSpace float32) float32 {
}
var spacingPx = totalSpace * base.GetSpacing()
var totalSpacing = float32(len(elements) - 1) * spacingPx
- if totalSpace < 0 {
- totalSpace = 0
+ if totalSpacing < 0 {
+ totalSpacing = 0
}
+ rl.TraceLog(rl.LogInfo, "SpacingPx %v \t Total spacing: %v", spacingPx, totalSpacing)
return (totalSpace - totalSpacing) / totalWeights
}
-
+// A list of values that are used in the span calculations
type layoutCache struct {
// The screen resolution the cache was calculated for
ScreenResolution rl.Vector2
@@ -62,6 +65,7 @@ func (base layoutCache) IsValid() bool {
base.ScreenResolution.Y == float32(rl.GetScreenHeight())
}
+// Fills in the offsets and width/height of a layout
func (base *layoutCache) CalculateOffsets(X float32, Y float32, paddingX float32, paddingY float32) {
base.OffsetX = (X * paddingX)
base.OffsetY = (Y * paddingY)
@@ -69,6 +73,8 @@ func (base *layoutCache) CalculateOffsets(X float32, Y float32, paddingX float32
base.Height = Y - base.OffsetY * 2
}
+// Fills in the RowLocations field of the cache.
+// Requires rows, WeightToPixels multiplyer, spacing (in px) and base offset (in px) that will be used as a start of a span.
func (base *layoutCache) CalculateRowsLocations (rows []UIElement, WeightToPixels float32, spacing float32, baseOffset float32) {
base.RowLocations = make([]rl.Vector2, len(rows))
var ptr float32 = baseOffset
@@ -82,3 +88,11 @@ func (base *layoutCache) CalculateRowsLocations (rows []UIElement, WeightToPixel
ptr += elemSpace + spacing
}
}
+
+func (base layoutCache) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/UI/GridColumn.go b/engine/UI/GridColumn.go
index 359d0d4..b933cf4 100644
--- a/engine/UI/GridColumn.go
+++ b/engine/UI/GridColumn.go
@@ -4,20 +4,20 @@ import rl "github.com/gen2brain/raylib-go/raylib"
type GridColumn struct {
// A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow'
- WidthWeight float32
- // The objects that lay in this row
- Spacing float32
+ Weight float32
// Spaces between the columns
Objects []UIElement
+ // The objects that lay in this row
+ Spacing float32
// Styling preferences
- Style *Style
+ Style Style `json:"-"`
location rl.Rectangle
cache layoutCache
}
func (base *GridColumn) Init(parent *Menu) {
- base.Style = InitStyle(base.Style, defaultGridStyle)
+ base.Style.FillMissing(defaultGridStyle)
for _, v := range base.Objects {
v.Init(parent)
}
@@ -31,7 +31,7 @@ func (base *GridColumn) Destroy() {
// Gets the scale width of the element. Works similar to CSS's 'flex-grow'
func (base *GridColumn) GetOccupationWeight() float32 {
- return base.WidthWeight
+ return base.Weight
}
// Draw the element with the given size
@@ -41,6 +41,7 @@ func (base *GridColumn) Draw(span *rl.Rectangle) {
}
for i, v := range base.Objects {
var offset = base.cache.RowLocations[i]
+ rl.TraceLog(rl.LogInfo, "Cache: %v", base.cache)
var elemSpan = rl.Rectangle {
X: span.X + *base.Style.Padding,
Y: offset.X,
diff --git a/engine/UI/GridRow.go b/engine/UI/GridRow.go
index 3139d97..4940948 100644
--- a/engine/UI/GridRow.go
+++ b/engine/UI/GridRow.go
@@ -1,18 +1,23 @@
package ui
import (
+ "encoding/json"
+ "fmt"
+
rl "github.com/gen2brain/raylib-go/raylib"
)
+const GridRowTypeName string = "gridrow"
+
type GridRow struct {
// A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow'
- HeightWeight float32
+ Weight float32
// The objects that lay in this row
Objects []UIElement
// Spaces between the columns
Spacing float32
// Styling preferences
- Style *Style
+ Style Style `json:"-"`
location rl.Rectangle
cache layoutCache
@@ -27,7 +32,7 @@ func (base *GridRow) GetSpacing() float32 {
}
func (base *GridRow) Init(parent *Menu) {
- base.Style = InitStyle(base.Style, defaultGridStyle)
+ base.Style.FillMissing(defaultGridStyle)
for _, v := range base.Objects {
v.Init(parent)
}
@@ -41,7 +46,7 @@ func (base *GridRow) Destroy() {
// Gets the scale width of the element. Works similar to CSS's 'flex-grow'
func (base *GridRow) GetOccupationWeight() float32 {
- return base.HeightWeight
+ return base.Weight
}
// Draw the element with the given size
@@ -51,6 +56,7 @@ func (base *GridRow) Draw(span *rl.Rectangle) {
}
for i, v := range base.Objects {
var offset = base.cache.RowLocations[i]
+ // rl.TraceLog(rl.LogInfo, "Cache: %v", base.cache)
var elemSpan = rl.Rectangle {
X: offset.X,
Y: span.Y + *base.Style.Padding,
@@ -70,8 +76,18 @@ func (base *GridRow) RecalculateCache(span *rl.Rectangle) {
X: float32(rl.GetScreenWidth()),
Y: float32(rl.GetScreenHeight()),
}
+ rl.TraceLog(rl.LogInfo, "Span: %vx%v \tPadding: %v", span.Width, span.Height, *base.Style.Padding)
base.cache.CalculateOffsets(span.Width, span.Height, *base.Style.Padding, *base.Style.Padding)
var weightToScale = getWeightToPixelsRatio(base, base.cache.Width)
+ rl.TraceLog(rl.LogInfo, "Ratio: %v", weightToScale)
var spacingPx = base.Spacing * span.Width
base.cache.CalculateRowsLocations(base.Objects, weightToScale, spacingPx, span.X)
}
+
+func (base GridRow) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/UI/Label.go b/engine/UI/Label.go
index 3a3528f..53c6e96 100644
--- a/engine/UI/Label.go
+++ b/engine/UI/Label.go
@@ -1,8 +1,15 @@
package ui
-import rl "github.com/gen2brain/raylib-go/raylib"
+import (
+ "encoding/json"
+ "fmt"
-var defaultLabelPadding float32 = 5
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+const LabelTypeName string = "label"
+
+var defaultLabelPadding float32 = 0.1
var defaultLabelRoundness float32 = 0.2
// If the text formation formula is applied without it the text is
@@ -13,7 +20,7 @@ var defaultLabelStyle = &Style{
BacgroundColor: &rl.LightGray,
FontColor: &rl.DarkGray,
Padding: &defaultLabelPadding,
- Roundness: &defaultLabelRoundness ,
+ Roundness: &defaultLabelRoundness,
}
type TextAlignment uint8
@@ -23,12 +30,13 @@ const (
Start
)
+// A UI element containing a text field
type Label struct {
WidthWeight float32
Text string
Alignment TextAlignment
- Style Style
+ Style Style `json:"-"`
}
func (base *Label) Init(*Menu) {
@@ -73,3 +81,11 @@ func (base *Label) getTextHeight(position *rl.Rectangle) int32 {
}
return int32(outp)
}
+
+func (base Label) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/UI/Menu.go b/engine/UI/Menu.go
index fa3888b..32e0dcc 100644
--- a/engine/UI/Menu.go
+++ b/engine/UI/Menu.go
@@ -1,6 +1,9 @@
package ui
import (
+ "encoding/json"
+ "fmt"
+
coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
rl "github.com/gen2brain/raylib-go/raylib"
@@ -23,6 +26,7 @@ func (base *Menu) Create(manager *coreobjects.SceneManager) {
base.manager = manager
base.cache = &layoutCache{}
base.Contents.Init(base)
+ // rl.TraceLog(rl.LogInfo, "Parsed: %s", base)
}
func (base *Menu) Destroy() {
@@ -92,3 +96,11 @@ func (base *Menu) RecalculateCache() {
base.PaddingX,
base.PaddingY)
}
+
+func (base Menu) String() string {
+ var outp, jsonErr = json.MarshalIndent(base, "", " ")
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/UI/Style.go b/engine/UI/Style.go
index 1e05bcf..f308fba 100644
--- a/engine/UI/Style.go
+++ b/engine/UI/Style.go
@@ -17,16 +17,16 @@ type Style struct {
// Fills up not specifiend elements with default ones
func (base *Style) FillMissing(defaultStyle *Style) {
if base.BacgroundColor == nil {
- base.BacgroundColor = defaultLabelStyle.BacgroundColor
+ base.BacgroundColor = defaultStyle.BacgroundColor
}
if base.Padding == nil {
- base.Padding = defaultLabelStyle.Padding
+ base.Padding = defaultStyle.Padding
}
if base.FontColor == nil {
- base.FontColor = defaultLabelStyle.FontColor
+ base.FontColor = defaultStyle.FontColor
}
if base.Roundness == nil {
- base.Roundness = defaultLabelStyle.Roundness
+ base.Roundness = defaultStyle.Roundness
}
}
diff --git a/engine/UI/Void.go b/engine/UI/Void.go
new file mode 100644
index 0000000..f167f30
--- /dev/null
+++ b/engine/UI/Void.go
@@ -0,0 +1,36 @@
+package ui
+
+import (
+ "encoding/json"
+ "fmt"
+
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+type Void struct {
+ Weight float32
+}
+
+// Used in the element parsing from JSON
+const VoidTypeName string = "void"
+
+func (base *Void) Init(*Menu) {
+}
+
+func (base *Void) Destroy() {
+}
+
+func (base *Void) GetOccupationWeight() float32 {
+ return base.Weight
+}
+
+func (base *Void) Draw(*rl.Rectangle) {
+}
+
+func (base Void) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}