summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/MainMenu.json5
-rw-r--r--engine/CoreObjects/Image.go4
-rw-r--r--engine/Dynamic/Descriptors/UIElement.go19
-rw-r--r--engine/UI/Image.go45
-rw-r--r--engine/main.go2
5 files changed, 72 insertions, 3 deletions
diff --git a/assets/MainMenu.json b/assets/MainMenu.json
index 09e333b..d44ab69 100644
--- a/assets/MainMenu.json
+++ b/assets/MainMenu.json
@@ -11,8 +11,13 @@
{
"Type": "gridcolumn",
"Weight": 1,
+ "Spacing": 0.01,
"Children": [
{
+ "Type": "image",
+ "TextureName": "testPackage/water.png"
+ },
+ {
"Type": "label",
"Text": "Ruines of Rafdalon"
},
diff --git a/engine/CoreObjects/Image.go b/engine/CoreObjects/Image.go
index 96c2da8..299334c 100644
--- a/engine/CoreObjects/Image.go
+++ b/engine/CoreObjects/Image.go
@@ -2,6 +2,6 @@ package coreobjects
import rl "github.com/gen2brain/raylib-go/raylib"
-type Image interface {
- DrawAt(rl.Vector2)
+type Drawable interface {
+ DrawAt(rl.Rectangle)
}
diff --git a/engine/Dynamic/Descriptors/UIElement.go b/engine/Dynamic/Descriptors/UIElement.go
index 1b19ba2..da523e9 100644
--- a/engine/Dynamic/Descriptors/UIElement.go
+++ b/engine/Dynamic/Descriptors/UIElement.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strings"
+ components "github.com/DegustatorPonos/RuinesOfRafdolon/Components"
ui "github.com/DegustatorPonos/RuinesOfRafdolon/UI"
rl "github.com/gen2brain/raylib-go/raylib"
)
@@ -20,6 +21,9 @@ type UIElementDescriptor struct {
// For grid elements
Children []UIElementDescriptor
Spacing float32
+
+ // For images
+ TextureName string
}
// We have to initialize the map so that we avoid the circular reference in the recursive parsing
@@ -27,6 +31,7 @@ func InitUIParser() {
typeParsers = map[string]func(*UIElementDescriptor) ui.UIElement {
ui.VoidTypeName: parseAsVoid,
ui.LabelTypeName: parseAsLabel,
+ ui.ImageTypeName: parseAsImage,
ui.GridRowTypeName: parseAsGridRow,
ui.GridColumnTypeName: parseAsGridColumn,
}
@@ -102,3 +107,17 @@ func parseAsGridColumn(base *UIElementDescriptor) ui.UIElement {
}
return &outp
}
+
+func parseAsImage(base *UIElementDescriptor) ui.UIElement {
+ var texture, err = components.Resources.Textures.GetTextureByName(base.TextureName)
+ if err != nil {
+ rl.TraceLog(rl.LogWarning, "Failed to parse a texture %s requested by an inage UI element: %v", base.TextureName, err.Error())
+ return nil
+ }
+ var outp = ui.Image {
+ Weight: base.Weight,
+ Texture: texture,
+ Style: base.Style.Parse(),
+ }
+ return &outp
+}
diff --git a/engine/UI/Image.go b/engine/UI/Image.go
new file mode 100644
index 0000000..f7ac7e2
--- /dev/null
+++ b/engine/UI/Image.go
@@ -0,0 +1,45 @@
+package ui
+
+import (
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+const ImageTypeName string = "image"
+
+type Image struct {
+ Texture *rl.Texture2D
+ Weight float32
+
+ Style Style `json:"-"`
+ parentMenu *Menu `json:"-"`
+ textureRect *rl.Rectangle
+}
+
+func (base *Image) Init(parent *Menu) {
+ base.parentMenu = parent
+ base.textureRect = &rl.Rectangle{
+ X: 0, Y: 0,
+ Width: float32(base.Texture.Width),
+ Height: float32(base.Texture.Height),
+ }
+ if base.Weight == 0 {
+ base.Weight = 1
+ }
+}
+
+func (base *Image) Destroy() {
+}
+
+// Gets the scale width of the element. Works similar to CSS's 'flex-grow'
+func (base *Image) GetOccupationWeight() float32 {
+ return base.Weight
+}
+
+// Draw the element with the given size
+func (base *Image) Draw(trg *rl.Rectangle) {
+ rl.DrawTexturePro(*base.Texture,
+ *base.textureRect, *trg,
+ rl.Vector2Zero(),
+ 0, rl.White)
+}
+
diff --git a/engine/main.go b/engine/main.go
index effc894..3b66d09 100644
--- a/engine/main.go
+++ b/engine/main.go
@@ -32,7 +32,7 @@ func main() {
log.Printf("Resource manager: %s", &components.Resources)
var manager = coreobjects.InitSceneManager()
- render.StartLoop(manager, components.Resources.Worlds["MainWorld"])
+ // render.StartLoop(manager, components.Resources.Worlds["MainWorld"])
render.StartLoop(manager, builtin.MainMenu())
}