diff options
| author | Physcik <mynameisgennadiy@vk.com> | 2026-02-07 23:20:48 +0500 |
|---|---|---|
| committer | Physcik <mynameisgennadiy@vk.com> | 2026-02-07 23:20:48 +0500 |
| commit | 64f718a1cd694be0952fd3700636d81ba05d30f0 (patch) | |
| tree | 3e9a19e34328072e15b2c8e1387484c7a3099265 /engine | |
| parent | 08242fe7c284d8e02c662d65831a08a6482e87a3 (diff) | |
Dynamic menus
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/Builtin/MainMenu.go | 51 | ||||
| -rw-r--r-- | engine/Dynamic/Descriptors/Menu.go | 21 | ||||
| -rw-r--r-- | engine/Dynamic/Descriptors/UIElement.go | 20 | ||||
| -rw-r--r-- | engine/Dynamic/DynamicMenu.go | 71 | ||||
| -rw-r--r-- | engine/MainMenu.json | 21 | ||||
| -rw-r--r-- | engine/UI/FlexElement.go | 2 | ||||
| -rw-r--r-- | engine/UI/GridColumn.go | 4 | ||||
| -rw-r--r-- | engine/main.go | 45 |
8 files changed, 169 insertions, 66 deletions
diff --git a/engine/Builtin/MainMenu.go b/engine/Builtin/MainMenu.go new file mode 100644 index 0000000..aa5d283 --- /dev/null +++ b/engine/Builtin/MainMenu.go @@ -0,0 +1,51 @@ +package builtin + +import ( + _ "embed" + + dynamic "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic" + ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" +) + +const mainMenuLocation string = "../assets/MainMenu.json" + +var menuSidePanelPadding float32 = 0.01 + +// The menu that shows up +var MainMenuVar = ui.Menu { + PaddingX: 0.05, + PaddingY: 0.1, + Contents: &ui.GridRow { + Objects: []ui.UIElement { + &ui.Void{ + Weight: 2, + }, + &ui.GridColumn { + Weight: 1, + Spacing: 0.05, + Style: ui.Style { + Padding: &menuSidePanelPadding, + }, + Objects: []ui.UIElement { + &ui.Label { + Text: "Ruines of Rafdalon", + }, + &ui.Button { + DisplayElement: &ui.Label { + Text: "Load a demo scene", + }, + }, + &ui.Void{ + Weight: 4, + }, + }, + }, + }, + }, +} + +func MainMenu() *dynamic.DynamicMenu { + return &dynamic.DynamicMenu { + FileLocation: mainMenuLocation, + } +} diff --git a/engine/Dynamic/Descriptors/Menu.go b/engine/Dynamic/Descriptors/Menu.go new file mode 100644 index 0000000..fc0c2de --- /dev/null +++ b/engine/Dynamic/Descriptors/Menu.go @@ -0,0 +1,21 @@ +package descriptors + +import ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" + +type MenuDescriptor struct { + PaddingX float32 + PaddingY float32 + Contents UIElementDescriptor +} + +func (base *MenuDescriptor) Parse() (*ui.Menu, error) { + var contents, contentsErr = base.Contents.Parse() + if contentsErr != nil { + return nil, contentsErr + } + return &ui.Menu { + PaddingX: base.PaddingX, + PaddingY: base.PaddingY, + Contents: contents, + }, nil +} diff --git a/engine/Dynamic/Descriptors/UIElement.go b/engine/Dynamic/Descriptors/UIElement.go index 9b88ab3..1b19ba2 100644 --- a/engine/Dynamic/Descriptors/UIElement.go +++ b/engine/Dynamic/Descriptors/UIElement.go @@ -22,11 +22,13 @@ type UIElementDescriptor struct { Spacing float32 } +// We have to initialize the map so that we avoid the circular reference in the recursive parsing func InitUIParser() { typeParsers = map[string]func(*UIElementDescriptor) ui.UIElement { ui.VoidTypeName: parseAsVoid, ui.LabelTypeName: parseAsLabel, ui.GridRowTypeName: parseAsGridRow, + ui.GridColumnTypeName: parseAsGridColumn, } } @@ -82,3 +84,21 @@ func parseAsGridRow(base *UIElementDescriptor) ui.UIElement { } return &outp } + +func parseAsGridColumn(base *UIElementDescriptor) ui.UIElement { + var outp = ui.GridColumn { + Weight: base.Weight, + Spacing: base.Spacing, + Style: base.Style.Parse(), + Objects: make([]ui.UIElement, 0, len(base.Children)), + } + for _, v := range base.Children { + var child, parseErr = v.Parse() + if parseErr != nil { + rl.TraceLog(rl.LogWarning, "Failed to parse a parent element of a grid column: %v", parseErr.Error()) + continue + } + outp.Objects = append(outp.Objects, child) + } + return &outp +} diff --git a/engine/Dynamic/DynamicMenu.go b/engine/Dynamic/DynamicMenu.go new file mode 100644 index 0000000..a1f3cd0 --- /dev/null +++ b/engine/Dynamic/DynamicMenu.go @@ -0,0 +1,71 @@ +package dynamic + +import ( + "encoding/json" + "fmt" + "os" + + coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects" + descriptors "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic/Descriptors" + ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" + rl "github.com/gen2brain/raylib-go/raylib" +) + +// Represents a menu variant that is loaded from a file +type DynamicMenu struct { + FileLocation string + baseMenu *ui.Menu +} + +func (base *DynamicMenu) Create(manager *coreobjects.SceneManager) { + var err = base.LoadMenu() + if err != nil { + rl.TraceLog(rl.LogWarning, "Failed to load the menu from the file %v: %v", base.FileLocation, err.Error()) + base.baseMenu = &ui.Menu { + Contents: &ui.Void{}, + } + } + base.baseMenu.Create(manager) +} + +func (base *DynamicMenu) Destroy() { + base.baseMenu.Destroy() +} + +func (base *DynamicMenu) Update() { + base.baseMenu.Update() +} + +func (base *DynamicMenu) Draw() { + base.baseMenu.Draw() +} + +func (base *DynamicMenu) GetMousePosition() rl.Vector2 { + return rl.GetMousePosition() +} + +func (base *DynamicMenu) String() string { + var outp, jsonErr = json.Marshal(base) + if jsonErr != nil { + return fmt.Sprintf("Failed to parse: %s", jsonErr.Error()) + } + return string(outp) +} + +// Loads the menu from the file. Does not initialize it +func (base *DynamicMenu) LoadMenu() error { + var file, fErr = os.ReadFile(base.FileLocation) + if fErr != nil { + return fErr + } + var descriptor = descriptors.MenuDescriptor{} + if jsonErr := json.Unmarshal(file, &descriptor); jsonErr != nil { + return jsonErr + } + var parsed, parseErr = descriptor.Parse() + if parseErr != nil { + return parseErr + } + base.baseMenu = parsed + return nil +} diff --git a/engine/MainMenu.json b/engine/MainMenu.json deleted file mode 100644 index 8718908..0000000 --- a/engine/MainMenu.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "Type": "gridrow", - "Weight": 2, - "Children": [ - { - "Type": "label", - "Text": "xdd", - "Style": { - "Padding": 0, - "BackgroundColor": "#FFAAFF" - } - }, - { - "Type": "void", - "Text": "xdd", - "Style": { - "Padding": 0 - } - } - ] -} diff --git a/engine/UI/FlexElement.go b/engine/UI/FlexElement.go index 4ec0f35..b8107ee 100644 --- a/engine/UI/FlexElement.go +++ b/engine/UI/FlexElement.go @@ -37,7 +37,7 @@ func getWeightToPixelsRatio(base flexElement, totalSpace float32) float32 { if totalSpacing < 0 { totalSpacing = 0 } - rl.TraceLog(rl.LogInfo, "SpacingPx %v \t Total spacing: %v", spacingPx, totalSpacing) + // rl.TraceLog(rl.LogInfo, "SpacingPx %v \t Total spacing: %v", spacingPx, totalSpacing) return (totalSpace - totalSpacing) / totalWeights } diff --git a/engine/UI/GridColumn.go b/engine/UI/GridColumn.go index b933cf4..46c12ad 100644 --- a/engine/UI/GridColumn.go +++ b/engine/UI/GridColumn.go @@ -2,6 +2,8 @@ package ui import rl "github.com/gen2brain/raylib-go/raylib" +const GridColumnTypeName string = "gridcolumn" + type GridColumn struct { // A portion of the screen the row will occupy. Works similar to CSS's 'flex-grow' Weight float32 @@ -41,7 +43,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) + // 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/main.go b/engine/main.go index b9fb611..3b66d09 100644 --- a/engine/main.go +++ b/engine/main.go @@ -5,6 +5,7 @@ import ( "log" "os" + builtin "github.com/DegustatorPonos/RuinesOfRafdolon/Builtin" components "github.com/DegustatorPonos/RuinesOfRafdolon/Components" coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects" dynamic "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic" @@ -12,7 +13,6 @@ import ( render "github.com/DegustatorPonos/RuinesOfRafdolon/Render" settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings" ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" - rl "github.com/gen2brain/raylib-go/raylib" ) func main() { @@ -33,8 +33,7 @@ func main() { var manager = coreobjects.InitSceneManager() // render.StartLoop(manager, components.Resources.Worlds["MainWorld"]) - var menu = menu_test() - render.StartLoop(manager, menu) + render.StartLoop(manager, builtin.MainMenu()) } func test_desc() (ui.UIElement, error) { @@ -52,43 +51,3 @@ func test_desc() (ui.UIElement, error) { } return parsed, nil } - -func menu_test() *ui.Menu { - var loadedMenu, loadErr = test_desc() - if loadErr != nil { - panic(loadErr.Error()) - } - - var prerender = false - - if !prerender { - return &ui.Menu { - PaddingX: 0.05, - PaddingY: 0.1, - Contents: loadedMenu, - } - } - - return &ui.Menu { - PaddingX: 0.05, - PaddingY: 0.1, - Contents: &ui.GridColumn { - Weight: 2, - Objects: []ui.UIElement { - &ui.Label { - Text: "xdx", - Style: ui.Style { - BacgroundColor: &rl.Pink, - }, - }, - &ui.Label { - Text: "xdx", - Style: ui.Style { - BacgroundColor: &rl.Pink, - }, - }, - }, - }, - } - -} |
