diff options
Diffstat (limited to 'engine/Dynamic/Descriptors')
| -rw-r--r-- | engine/Dynamic/Descriptors/Style.go | 78 | ||||
| -rw-r--r-- | engine/Dynamic/Descriptors/UIElement.go | 84 |
2 files changed, 162 insertions, 0 deletions
diff --git a/engine/Dynamic/Descriptors/Style.go b/engine/Dynamic/Descriptors/Style.go new file mode 100644 index 0000000..b7b9510 --- /dev/null +++ b/engine/Dynamic/Descriptors/Style.go @@ -0,0 +1,78 @@ +package descriptors + +import ( + "fmt" + "strings" + + "github.com/gookit/color" + + ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" + rl "github.com/gen2brain/raylib-go/raylib" +) + +// Using this method instead of the base style structs allows us to use +// hex values as colours since golang's implimetation of JSON deserializer +// doesn't really like color.RGBA +type StyleDescriptor struct { + BackgroundColor string + FontColor string + Padding *float32 + Roundness *float32 +} + +func (base *StyleDescriptor) Parse() ui.Style { + var outp = ui.Style { + BacgroundColor: base.getBackgroundColor(), + FontColor: base.getFontColor(), + Roundness: base.Roundness, + Padding: base.Padding, + } + return outp +} + +func (base *StyleDescriptor) getBackgroundColor() *rl.Color { + if len(base.BackgroundColor) == 0 { + return nil + } + var outp, err = parseColor(base.BackgroundColor) + if err != nil { + rl.TraceLog(rl.LogWarning, "Failed to parse the background color %s in style: %s", base.BackgroundColor, err.Error()) + return nil + } + return outp +} + +func (base *StyleDescriptor) getFontColor() *rl.Color { + if len(base.FontColor) == 0 { + return nil + } + var outp, err = parseColor(base.FontColor) + if err != nil { + rl.TraceLog(rl.LogWarning, "Failed to parse the font color %s in style: %s", base.FontColor, err.Error()) + return nil + } + return outp +} + +func parseColor(val string) (*rl.Color, error) { + val = strings.TrimPrefix(val, "#") + if !isColorValid(val) { + return nil, fmt.Errorf("Invalid color notation") + } + var parsed = color.HEX(val) + var outp = rl.NewColor(parsed[0], parsed[1], parsed[2], parsed[3]) + // If there is no alpha cahnnel + if len(val) % 2 == 0 { + outp.A = 0xFF + } + return &outp, nil +} + +func isColorValid(val string) bool { + switch len(val) { + // #RGB, #RGBA, #RRGGBB, #RRGGBBAA + case 3, 4, 6, 8: + return true + } + return false +} diff --git a/engine/Dynamic/Descriptors/UIElement.go b/engine/Dynamic/Descriptors/UIElement.go new file mode 100644 index 0000000..e0540ee --- /dev/null +++ b/engine/Dynamic/Descriptors/UIElement.go @@ -0,0 +1,84 @@ +package descriptors + +import ( + "fmt" + "strings" + + ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI" + rl "github.com/gen2brain/raylib-go/raylib" +) + +type UIElementDescriptor struct { + Type string + Weight float32 + Style StyleDescriptor + + // For labels + Text string + Alignment string + + // For grid elements + Children []UIElementDescriptor + Spacing float32 +} + +func InitUIParser() { + typeParsers = map[string]func(*UIElementDescriptor) ui.UIElement { + ui.VoidTypeName: parseAsVoid, + ui.LabelTypeName: parseAsLabel, + ui.GridRowTypeName: parseAsGridRow, + } +} + +// The item type to its parser relationship +var typeParsers map[string]func(*UIElementDescriptor) ui.UIElement + +func (base *UIElementDescriptor) Parse() (ui.UIElement, error) { + var elemType = strings.ToLower(base.Type) + var descFunc, exists = typeParsers[elemType] + if exists { + return descFunc(base), nil + } + return nil, fmt.Errorf("Unknown UI element type") +} + +func parseAsVoid(base *UIElementDescriptor) ui.UIElement { + return &ui.Void { + Weight: base.Weight, + } +} + +func parseAsLabel(base *UIElementDescriptor) ui.UIElement { + var alignment ui.TextAlignment + switch strings.ToLower(base.Alignment) { + case "center": + alignment = ui.Center + case "start": + alignment = ui.Start + } + + return &ui.Label { + WidthWeight: base.Weight, + Text: base.Text, + Alignment: alignment, + Style: base.Style.Parse(), + } +} + +func parseAsGridRow(base *UIElementDescriptor) ui.UIElement { + var outp = ui.GridRow { + 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 row: %v", parseErr.Error()) + continue + } + outp.Objects = append(outp.Objects, child) + } + return &outp +} |
