⚡ Optimization and ready jump to godot
This commit is contained in:
@@ -19,12 +19,7 @@ type BaseObject struct {
|
||||
|
||||
func (p *BaseObject) Create() {}
|
||||
|
||||
func (p *BaseObject) Update() {
|
||||
// Update each child
|
||||
for _, child := range p.Children {
|
||||
child.Update()
|
||||
}
|
||||
}
|
||||
func (p *BaseObject) Update() {}
|
||||
|
||||
func (p *BaseObject) AddChild(child Object) {
|
||||
p.Children = append(p.Children, child)
|
||||
|
@@ -5,8 +5,9 @@ import "github.com/veandco/go-sdl2/sdl"
|
||||
type BaseWidget struct {
|
||||
BaseObject
|
||||
|
||||
Position Vector2D
|
||||
Size Vector2D
|
||||
IsVisible bool
|
||||
Position Vector2D
|
||||
Size Vector2D
|
||||
}
|
||||
|
||||
func (p *BaseWidget) Draw(pen *sdl.Renderer) {
|
||||
|
@@ -6,7 +6,8 @@ import (
|
||||
)
|
||||
|
||||
type RootObject struct {
|
||||
BaseObject
|
||||
Children []Object
|
||||
Overlays []Object
|
||||
|
||||
Analyzer *PerformanceAnalyzer
|
||||
}
|
||||
@@ -21,6 +22,14 @@ func NewRootObject() *RootObject {
|
||||
return in
|
||||
}
|
||||
|
||||
func (p *RootObject) AddChild(child Object) {
|
||||
p.Children = append(p.Children, child)
|
||||
}
|
||||
|
||||
func (p *RootObject) AddOverlay(overlay Object) {
|
||||
p.Overlays = append(p.Overlays, overlay)
|
||||
}
|
||||
|
||||
func (p *RootObject) RunEventLoop(tickDuration time.Duration) {
|
||||
for {
|
||||
startTime := time.Now()
|
||||
@@ -45,7 +54,12 @@ func (p *RootObject) ForEachChildren(cb func(child Object)) {
|
||||
}
|
||||
}
|
||||
|
||||
caller(p)
|
||||
for _, child := range p.Children {
|
||||
caller(child)
|
||||
}
|
||||
for _, child := range p.Overlays {
|
||||
caller(child)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *RootObject) HandleUserEvent(event sdl.Event) {
|
||||
@@ -77,7 +91,9 @@ func (p *RootObject) HandleUserEvent(event sdl.Event) {
|
||||
}
|
||||
|
||||
func (p *RootObject) Update() {
|
||||
p.BaseObject.Update()
|
||||
p.ForEachChildren(func(child Object) {
|
||||
child.Update()
|
||||
})
|
||||
|
||||
// Check collision
|
||||
p.ForEachChildren(func(child Object) {
|
||||
@@ -110,10 +126,6 @@ func (p *RootObject) Draw(pen *sdl.Renderer) {
|
||||
|
||||
// Render each child
|
||||
p.ForEachChildren(func(child Object) {
|
||||
if child == p {
|
||||
// Skip the current to prevent infinite drawing
|
||||
return
|
||||
}
|
||||
if drawableChild, ok := child.(DrawableObject); ok {
|
||||
drawableChild.Draw(pen)
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/highland/codingland/pkg/internal/land"
|
||||
"git.solsynth.dev/highland/codingland/pkg/internal/land/renderer"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/ttf"
|
||||
"log"
|
||||
@@ -20,6 +19,10 @@ type ButtonWidget struct {
|
||||
}
|
||||
|
||||
func (p *ButtonWidget) Draw(pen *sdl.Renderer) {
|
||||
if !p.BaseWidget.IsVisible {
|
||||
return
|
||||
}
|
||||
|
||||
var borderColor, bgColor sdl.Color
|
||||
if p.isClicking {
|
||||
borderColor = sdl.Color{R: 200, G: 0, B: 0, A: 255}
|
||||
@@ -35,18 +38,19 @@ func (p *ButtonWidget) Draw(pen *sdl.Renderer) {
|
||||
var borderWidth int32 = 2
|
||||
|
||||
pen.SetDrawColor(borderColor.R, borderColor.G, borderColor.B, borderColor.A)
|
||||
renderer.FillRoundedRect(pen, &sdl.Rect{
|
||||
pen.FillRect(&sdl.Rect{
|
||||
X: int32(p.Position.X),
|
||||
Y: int32(p.Position.Y),
|
||||
W: int32(p.Size.X),
|
||||
H: int32(p.Size.Y),
|
||||
}, borderColor, 16)
|
||||
renderer.FillRoundedRect(pen, &sdl.Rect{
|
||||
})
|
||||
pen.SetDrawColor(bgColor.R, bgColor.G, bgColor.B, bgColor.A)
|
||||
pen.FillRect(&sdl.Rect{
|
||||
X: int32(p.Position.X) + borderWidth/2,
|
||||
Y: int32(p.Position.Y) + borderWidth/2,
|
||||
W: int32(p.Size.X) - borderWidth,
|
||||
H: int32(p.Size.Y) - borderWidth,
|
||||
}, bgColor, 16)
|
||||
})
|
||||
|
||||
surface, err := p.Font.RenderUTF8Blended(p.Label, sdl.Color{R: 255, G: 255, B: 255, A: 255})
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user