-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.go
More file actions
97 lines (80 loc) · 1.89 KB
/
graphics.go
File metadata and controls
97 lines (80 loc) · 1.89 KB
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
package main
import (
"github.com/veandco/go-sdl2/sdl"
"image"
"image/color"
"time"
)
type Clickable interface{
GetRect() *sdl.Rect
GetOnClick() OnClick
}
type OnClick func()
type Button struct{
Rect *sdl.Rect
Text string
OnClick OnClick
ForeColor sdl.Color
}
func (b *Button) GetRect() *sdl.Rect{
return b.Rect
}
func (b *Button) GetOnClick() OnClick{
return b.OnClick
}
func (b *Button) Draw(g *Graphics){
re,gr,bl,a,_ := g.renderer.GetDrawColor()
g.renderer.SetDrawColor(240,240,240,0)
g.renderer.DrawRect(b.Rect)
renderText(g,b.Text,b.ForeColor,&sdl.Point{b.Rect.X,b.Rect.Y})
g.renderer.SetDrawColor(re,gr,bl,a)
}
type LogLabel struct{
Rect *sdl.Rect
Text string
VisibleDuration time.Duration
visibleStartTime time.Time
visible bool
ForeColor sdl.Color
BackColor sdl.Color
}
func (b *LogLabel)Show(){
b.visible = true
b.visibleStartTime = time.Now()
}
func (b *LogLabel)Tick () {
if time.Now().Sub(b.visibleStartTime)>b.VisibleDuration{
b.visible=false
}
}
func (b *LogLabel) Draw(g *Graphics){
if !b.visible{
return
}
re,gr,bl,a,_ := g.renderer.GetDrawColor()
renderText(g,b.Text,b.ForeColor,&sdl.Point{b.Rect.X,b.Rect.Y})
g.renderer.SetDrawColor(re,gr,bl,a)
}
func ForEachButton(buttons []*Button, f func(b *Button)){
for _,x:=range buttons{
f(x)
}
}
func CheckClickEvent(t *sdl.MouseButtonEvent,clickable Clickable){
clickPoint := &sdl.Point{t.X,t.Y}
if clickPoint.InRect(clickable.GetRect()) && t.State==1{
clickable.GetOnClick()()
}
}
func renderText(g *Graphics,text string,color sdl.Color,point *sdl.Point){
surface,_ := g.font.RenderUTF8Blended(text,color)
defer surface.Free()
txtSurface,_ := g.renderer.CreateTextureFromSurface(surface)
defer txtSurface.Destroy()
g.renderer.Copy(txtSurface,nil,
&sdl.Rect{point.X,point.Y,surface.W,surface.H})
}
func getCenterColor(img *image.RGBA)color.Color{
c:= img.At(img.Rect.Dx()/2,img.Rect.Dy()/2)
return c
}