From b67353c1ae8652206452687956ec30b8341e55e6 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 19 Jul 2024 10:55:05 +0800 Subject: [PATCH] :zap: Optimization and ready jump to godot --- pkg/internal/entities/player.go | 2 -- pkg/internal/land/object.go | 7 +------ pkg/internal/land/object_ui.go | 5 +++-- pkg/internal/land/root.go | 26 +++++++++++++++++++------- pkg/internal/land/ui/button.go | 14 +++++++++----- pkg/main.go | 7 ++++--- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/pkg/internal/entities/player.go b/pkg/internal/entities/player.go index 1dff2e1..d384d8b 100644 --- a/pkg/internal/entities/player.go +++ b/pkg/internal/entities/player.go @@ -36,8 +36,6 @@ func (p *Player) Update() { if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 { p.Move(1, 0) } - - p.BaseObject.Update() } func (p *Player) Move(deltaX, deltaY float64) { diff --git a/pkg/internal/land/object.go b/pkg/internal/land/object.go index 0b543d8..65e478a 100644 --- a/pkg/internal/land/object.go +++ b/pkg/internal/land/object.go @@ -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) diff --git a/pkg/internal/land/object_ui.go b/pkg/internal/land/object_ui.go index eea7506..31298c2 100644 --- a/pkg/internal/land/object_ui.go +++ b/pkg/internal/land/object_ui.go @@ -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) { diff --git a/pkg/internal/land/root.go b/pkg/internal/land/root.go index 510849b..334f0fe 100644 --- a/pkg/internal/land/root.go +++ b/pkg/internal/land/root.go @@ -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) } diff --git a/pkg/internal/land/ui/button.go b/pkg/internal/land/ui/button.go index 72bc250..baa2a2e 100644 --- a/pkg/internal/land/ui/button.go +++ b/pkg/internal/land/ui/button.go @@ -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 { diff --git a/pkg/main.go b/pkg/main.go index a682656..161316b 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -62,10 +62,11 @@ func main() { Position: land.Vector2D{X: 50, Y: 50}, Size: land.Vector2D{X: 50, Y: 50}, }) - root.AddChild(&ui.ButtonWidget{ + root.AddOverlay(&ui.ButtonWidget{ BaseWidget: land.BaseWidget{ - Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25}, - Size: land.Vector2D{X: 120, Y: 50}, + IsVisible: true, + Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25}, + Size: land.Vector2D{X: 120, Y: 50}, }, Label: "Hello, World!", Font: font,