summaryrefslogtreecommitdiff
path: root/engine/UI/GridRow.go
blob: 3b6d90d1423608d9f1dd2f95135f4e41bbfe590a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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'
	Weight float32
	// The objects that lay in this row
	Objects []UIElement
	// Spaces between the columns
	Spacing float32
	// Styling preferences
	Style Style `json:"-"`
	BackgroundElement UIElement

	location rl.Rectangle
	cache *layoutCache
}

func (base *GridRow) GetElements() []UIElement {
	return base.Objects
}

func (base *GridRow) GetSpacing() float32 {
	return base.Spacing
}

func (base *GridRow) Init(parent *Menu) {
	base.Style.FillMissing(defaultGridStyle)
	for _, v := range base.Objects {
		v.Init(parent)
	}
	base.cache = &layoutCache {
		ScreenResolution: rl.Vector2{ X:0, Y:0 },
	}

	if base.BackgroundElement != nil {
		base.BackgroundElement.Init(parent)
	}
}

func (base *GridRow) Destroy() {
}

func (base *GridRow) GetStyle() *Style {
	return &base.Style
}

func (base *GridRow) GetBackgroundElement() UIElement {
	return base.BackgroundElement
}

// Gets the scale width of the element. Works similar to CSS's 'flex-grow'
func (base *GridRow) GetOccupationWeight() float32 {
	return base.Weight
}

// Draw the element with the given size
func (base *GridRow) Draw(span *rl.Rectangle) {
	if !base.cache.IsValid() {
		base.RecalculateCache(span)
	}
	drawStackedElementBackground(base, span)
	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,
			Width: offset.Y,
			Height: span.Height - *base.Style.Padding * 2,
		}
		v.Draw(&elemSpan)
	}
}

func (base *GridRow) RecalculateCache(span *rl.Rectangle) {
	base.cache.SpanResolution = rl.Vector2{ 
		X: span.Width,
		Y: span.Height,
	}
	base.cache.ScreenResolution = rl.Vector2 {
		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 grid row: %s", jsonErr.Error())
	}
	return string(outp)
}