Improve and optimization

This commit is contained in:
LittleSheep 2024-07-18 22:59:03 +08:00
parent 1610584ce4
commit 1b3a9833eb
9 changed files with 110 additions and 53 deletions

View File

@ -0,0 +1 @@
package config

View File

@ -1,7 +1,15 @@
package land package land
type UserEventType = int
const (
UserEventMouseDown = UserEventType(iota)
UserEventMouseUp
UserEventMouseMove
)
type InteractableObject interface { type InteractableObject interface {
GetPosition() Vector2D GetPosition() Vector2D
GetSize() Vector2D GetSize() Vector2D
OnClick(mousePos Vector2D) OnEvent(event UserEventType, args ...any)
} }

View File

@ -2,20 +2,20 @@ package land
import "github.com/veandco/go-sdl2/sdl" import "github.com/veandco/go-sdl2/sdl"
type BaseUIObject struct { type BaseWidget struct {
BaseObject BaseObject
Position Vector2D Position Vector2D
Size Vector2D Size Vector2D
} }
func (p *BaseUIObject) Draw(pen *sdl.Renderer) { func (p *BaseWidget) Draw(pen *sdl.Renderer) {
} }
func (p *BaseUIObject) GetPosition() Vector2D { func (p *BaseWidget) GetPosition() Vector2D {
return p.Position return p.Position
} }
func (p *BaseUIObject) GetSize() Vector2D { func (p *BaseWidget) GetSize() Vector2D {
return p.Size return p.Size
} }

View File

@ -0,0 +1,10 @@
package renderer
import (
"github.com/veandco/go-sdl2/gfx"
"github.com/veandco/go-sdl2/sdl"
)
func FillRoundedRect(pen *sdl.Renderer, rect *sdl.Rect, color sdl.Color, radius int32) {
gfx.RoundedRectangleColor(pen, rect.X, rect.Y, rect.X+rect.W, rect.Y+rect.H, radius, color)
}

View File

@ -51,19 +51,29 @@ func (p *RootObject) ForEachChildren(cb func(child Object)) {
func (p *RootObject) HandleUserEvent(event sdl.Event) { func (p *RootObject) HandleUserEvent(event sdl.Event) {
switch event := event.(type) { switch event := event.(type) {
case *sdl.MouseButtonEvent: case *sdl.MouseButtonEvent:
x, y := float64(event.X), float64(event.Y)
vec := Vector2D{x, y}
p.ForEachChildren(func(child Object) {
if interChild, ok := child.(InteractableObject); ok {
if IsOverlapWithPoint(child.(PositionedObject), vec) {
if event.Type == sdl.MOUSEBUTTONDOWN { if event.Type == sdl.MOUSEBUTTONDOWN {
interChild.OnEvent(UserEventMouseDown, vec)
} else if event.Type == sdl.MOUSEBUTTONUP {
interChild.OnEvent(UserEventMouseUp, vec)
}
}
}
})
case *sdl.MouseMotionEvent:
x, y := float64(event.X), float64(event.Y) x, y := float64(event.X), float64(event.Y)
vec := Vector2D{x, y} vec := Vector2D{x, y}
p.ForEachChildren(func(child Object) { p.ForEachChildren(func(child Object) {
if interChild, ok := child.(InteractableObject); ok { if interChild, ok := child.(InteractableObject); ok {
isOverlap := IsOverlapWithPoint(child.(PositionedObject), vec) isOverlap := IsOverlapWithPoint(child.(PositionedObject), vec)
if isOverlap { interChild.OnEvent(UserEventMouseMove, vec, isOverlap)
interChild.OnClick(vec)
}
} }
}) })
} }
}
} }
func (p *RootObject) Update() { func (p *RootObject) Update() {

View File

@ -2,36 +2,51 @@ package ui
import ( import (
"git.solsynth.dev/highland/codingland/pkg/internal/land" "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/sdl"
"github.com/veandco/go-sdl2/ttf" "github.com/veandco/go-sdl2/ttf"
"log" "log"
) )
type ButtonObject struct { type ButtonWidget struct {
land.BaseUIObject land.BaseWidget
Label string Label string
Font *ttf.Font Font *ttf.Font
OnClickEvent func(mousePos land.Vector2D) OnClick func(mousePos land.Vector2D)
isHovering bool
isClicking bool
} }
func (p *ButtonObject) Draw(pen *sdl.Renderer) { func (p *ButtonWidget) Draw(pen *sdl.Renderer) {
var borderColor, bgColor sdl.Color
if p.isClicking {
borderColor = sdl.Color{R: 200, G: 0, B: 0, A: 255}
bgColor = sdl.Color{R: 255, G: 0, B: 0, A: 255}
} else if p.isHovering {
borderColor = sdl.Color{R: 0, G: 200, B: 0, A: 255}
bgColor = sdl.Color{R: 0, G: 255, B: 0, A: 255}
} else {
borderColor = sdl.Color{R: 255, G: 255, B: 255, A: 255}
bgColor = sdl.Color{R: 0, G: 0, B: 0, A: 255}
}
var borderWidth int32 = 2 var borderWidth int32 = 2
pen.SetDrawColor(255, 255, 255, 255) pen.SetDrawColor(borderColor.R, borderColor.G, borderColor.B, borderColor.A)
pen.FillRect(&sdl.Rect{ renderer.FillRoundedRect(pen, &sdl.Rect{
X: int32(p.Position.X), X: int32(p.Position.X),
Y: int32(p.Position.Y), Y: int32(p.Position.Y),
W: int32(p.Size.X), W: int32(p.Size.X),
H: int32(p.Size.Y), H: int32(p.Size.Y),
}) }, borderColor, 16)
pen.SetDrawColor(0, 0, 0, 255) renderer.FillRoundedRect(pen, &sdl.Rect{
pen.FillRect(&sdl.Rect{
X: int32(p.Position.X) + borderWidth/2, X: int32(p.Position.X) + borderWidth/2,
Y: int32(p.Position.Y) + borderWidth/2, Y: int32(p.Position.Y) + borderWidth/2,
W: int32(p.Size.X) - borderWidth, W: int32(p.Size.X) - borderWidth,
H: int32(p.Size.Y) - 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}) surface, err := p.Font.RenderUTF8Blended(p.Label, sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil { if err != nil {
@ -47,15 +62,22 @@ func (p *ButtonObject) Draw(pen *sdl.Renderer) {
} }
defer texture.Destroy() defer texture.Destroy()
textRect := sdl.Rect{ textWidth := surface.W
X: int32(p.BaseUIObject.Position.X), textHeight := surface.H
Y: int32(p.BaseUIObject.Position.Y), textX := int32(p.BaseWidget.Position.X) + (int32(p.BaseWidget.Size.X)-textWidth)/2
W: surface.W, textY := int32(p.BaseWidget.Position.Y) + (int32(p.BaseWidget.Size.Y)-textHeight)/2
H: surface.H, textRect := sdl.Rect{X: textX, Y: textY, W: textWidth, H: textHeight}
}
pen.Copy(texture, nil, &textRect) pen.Copy(texture, nil, &textRect)
} }
func (p *ButtonObject) OnClick(mousePos land.Vector2D) { func (p *ButtonWidget) OnEvent(event land.UserEventType, args ...any) {
p.OnClickEvent(mousePos) switch event {
case land.UserEventMouseDown:
p.isClicking = true
p.OnClick(args[0].(land.Vector2D))
case land.UserEventMouseUp:
p.isClicking = false
case land.UserEventMouseMove:
p.isHovering = args[1].(bool)
}
} }

View File

@ -1,6 +1,8 @@
package land package land
import "fmt" import (
"fmt"
)
type Vector2D struct { type Vector2D struct {
X, Y float64 X, Y float64

View File

@ -40,6 +40,8 @@ func (p *Tile) GetSize() land.Vector2D {
func (p *Tile) OnCollide(other land.CollidableObject) { func (p *Tile) OnCollide(other land.CollidableObject) {
} }
func (p *Tile) OnClick(mousePos land.Vector2D) { func (p *Tile) OnEvent(event land.UserEventType, args ...any) {
log.Printf("user clicked tile: %s", mousePos.String()) if event == land.UserEventMouseDown {
log.Printf("user clicked tile: %s", args[0].(land.Vector2D).String())
}
} }

View File

@ -45,26 +45,15 @@ func main() {
} }
defer renderer.Destroy() defer renderer.Destroy()
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1") font, err := ttf.OpenFont("assets/fonts/NotoSans.ttf", 14)
font, err := ttf.OpenFont("assets/fonts/NotoSans.ttf", 12)
if err != nil { if err != nil {
log.Fatalf("Failed to open font: %s", err) log.Fatalf("Failed to open font: %s", err)
} else {
font.SetStyle(ttf.STYLE_BOLD)
} }
defer font.Close() defer font.Close()
root := land.NewRootObject() root := land.NewRootObject()
root.AddChild(&ui.ButtonObject{
BaseUIObject: land.BaseUIObject{
Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25},
Size: land.Vector2D{X: 120, Y: 50},
},
Label: "Hello, World!",
Font: font,
OnClickEvent: func(_ land.Vector2D) {
sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_INFORMATION, "You clicked me!", "你惊扰了古希腊掌控按钮的神", window)
},
})
root.AddChild(&entities.Player{ root.AddChild(&entities.Player{
Position: land.Vector2D{X: windowWidth/2 - 25, Y: windowHeight/2 - 25}, Position: land.Vector2D{X: windowWidth/2 - 25, Y: windowHeight/2 - 25},
Size: land.Vector2D{X: 50, Y: 50}, Size: land.Vector2D{X: 50, Y: 50},
@ -73,6 +62,17 @@ func main() {
Position: land.Vector2D{X: 50, Y: 50}, Position: land.Vector2D{X: 50, Y: 50},
Size: land.Vector2D{X: 50, Y: 50}, Size: land.Vector2D{X: 50, Y: 50},
}) })
root.AddChild(&ui.ButtonWidget{
BaseWidget: land.BaseWidget{
Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25},
Size: land.Vector2D{X: 120, Y: 50},
},
Label: "Hello, World!",
Font: font,
OnClick: func(_ land.Vector2D) {
log.Println("你惊扰了古希腊掌控按钮的神")
},
})
// 10ms delay use be 100tps average // 10ms delay use be 100tps average
go root.RunEventLoop(10 * time.Millisecond) go root.RunEventLoop(10 * time.Millisecond)
@ -83,13 +83,15 @@ func main() {
switch event.(type) { switch event.(type) {
case *sdl.QuitEvent: case *sdl.QuitEvent:
running = false running = false
case *sdl.MouseButtonEvent: default:
root.HandleUserEvent(event) root.HandleUserEvent(event)
} }
} }
// Draw the entire object tree
root.Draw(renderer) root.Draw(renderer)
// Show on the screen
renderer.Present() renderer.Present()
} }
} }