-
Notifications
You must be signed in to change notification settings - Fork 1
/
worldmapwidget.go
179 lines (159 loc) · 5.59 KB
/
worldmapwidget.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dctycoon
import (
"math"
"time"
"github.com/nzin/dctycoon/global"
"github.com/nzin/dctycoon/supplier"
"github.com/nzin/sws"
"github.com/veandco/go-sdl2/sdl"
log "github.com/sirupsen/logrus"
)
// WorldmapWidget is a worldmap image widget
// where you can select a Location (see supplier.LocationType and supplier.AvailableLocation)
type WorldmapWidget struct {
sws.CoreWidget
selected string
hotspot string
background *sdl.Surface
xshift int32
yshift int32
scale float64
hotspotcallback func(selected, hotspot string)
spotzoomeffect int32
evtspotzoomeffect *sws.TimerEvent
hotspotzoomeffect int32
evthotspotzoomeffect *sws.TimerEvent
}
func (self *WorldmapWidget) MouseMove(x, y, xrel, yrel int32) {
var currenthotspot string
for locationid, l := range supplier.AvailableLocation {
xlocation := float64(l.Xmap)*self.scale + float64(self.xshift)
ylocation := float64(l.Ymap)*self.scale + float64(self.yshift)
if math.Abs((float64(x)-xlocation)*(float64(x)-xlocation)+(float64(y)-ylocation)*(float64(y)-ylocation)) < 200.0 {
currenthotspot = locationid
}
}
if currenthotspot != self.hotspot {
self.hotspot = currenthotspot
if self.hotspotcallback != nil {
self.hotspotcallback(self.selected, self.hotspot)
}
self.hotspotzoomeffect = 0
if self.evthotspotzoomeffect != nil {
self.evthotspotzoomeffect.StopRepeat()
self.evthotspotzoomeffect = nil
}
self.evthotspotzoomeffect = sws.TimerAddEvent(time.Now(), 30*time.Millisecond, func(evt *sws.TimerEvent) {
self.hotspotzoomeffect++
if self.hotspotzoomeffect > 10 {
evt.StopRepeat()
}
self.PostUpdate()
})
}
self.PostUpdate()
}
func (self *WorldmapWidget) MousePressDown(x, y int32, button uint8) {
self.selected = self.hotspot
self.PostUpdate()
}
func (self *WorldmapWidget) Repaint() {
// background image
self.FillRect(0, 0, self.Width(), self.Height(), 0xff000000)
self.background.Blit(&sdl.Rect{X: 0, Y: 0, W: self.background.W, H: self.background.H}, self.Surface(), &sdl.Rect{X: 0, Y: 0, W: self.background.W, H: self.background.H})
// different spots
for locationid, l := range supplier.AvailableLocation {
x := int32(float64(l.Xmap)*self.scale) + self.xshift
y := int32(float64(l.Ymap)*self.scale) + self.yshift
alpha := self.spotzoomeffect*10 - (l.Xmap / 8)
if alpha < 0 {
alpha = 0
}
if alpha > 255 {
alpha = 255
}
self.SetDrawColor(0x46, 0xc8, 0xe8, uint8(alpha))
if locationid == self.hotspot {
self.SetDrawColor(0xff, 0xa0, 0xa0, 255)
}
if locationid == self.selected {
self.SetDrawColor(0xff, 0x20, 0x20, 255)
}
// for dy := y - 3; dy < y+3; dy++ {
// self.DrawLine(x-3, dy, x+3, dy)
// }
stretch := (255 - alpha) / 20
for dy := int32(0); dy < 4; dy++ {
self.DrawLine(x-dy-stretch, y-dy-stretch, x+dy+stretch, y-dy-stretch)
self.DrawLine(x-dy-stretch, y+dy+stretch, x+dy+stretch, y+dy+stretch)
self.DrawLine(x-dy-stretch, y-dy-stretch, x-dy-stretch, y+dy+stretch)
self.DrawLine(x+dy+stretch, y-dy-stretch, x+dy+stretch, y+dy+stretch)
}
if self.hotspotzoomeffect > 0 && locationid == self.hotspot && self.hotspotzoomeffect < 10 {
self.SetDrawColor(0xff, 0xa0, 0xa0, 255-uint8(20*self.hotspotzoomeffect))
for dy := int32(0); dy < 4; dy++ {
self.DrawLine(x-dy-self.hotspotzoomeffect, y-dy-self.hotspotzoomeffect, x+dy+self.hotspotzoomeffect, y-dy-self.hotspotzoomeffect)
self.DrawLine(x-dy-self.hotspotzoomeffect, y+dy+self.hotspotzoomeffect, x+dy+self.hotspotzoomeffect, y+dy+self.hotspotzoomeffect)
self.DrawLine(x-dy-self.hotspotzoomeffect, y-dy-self.hotspotzoomeffect, x-dy-self.hotspotzoomeffect, y+dy+self.hotspotzoomeffect)
self.DrawLine(x+dy+self.hotspotzoomeffect, y-dy-self.hotspotzoomeffect, x+dy+self.hotspotzoomeffect, y+dy+self.hotspotzoomeffect)
}
}
}
// children
for _, child := range self.GetChildren() {
// adjust the clipping to the current child
child.Repaint()
rectSrc := sdl.Rect{0, 0, child.Width(), child.Height()}
rectDst := sdl.Rect{child.X(), child.Y(), child.Width(), child.Height()}
child.Surface().Blit(&rectSrc, self.Surface(), &rectDst)
}
}
func (self *WorldmapWidget) SetLocationCallback(callback func(selected, hotspot string)) {
self.hotspotcallback = callback
}
func (self *WorldmapWidget) Reset() {
log.Debug("WorldmapWidget::Reset()")
self.selected = ""
self.hotspot = ""
self.spotzoomeffect = 0
if self.evtspotzoomeffect != nil {
self.evtspotzoomeffect.StopRepeat()
self.evtspotzoomeffect = nil
}
self.evtspotzoomeffect = sws.TimerAddEvent(time.Now(), 15*time.Millisecond, func(evt *sws.TimerEvent) {
self.spotzoomeffect++
if self.spotzoomeffect > 100 {
evt.StopRepeat()
}
self.PostUpdate()
})
}
func NewWorldmapWidget(w, h int32) *WorldmapWidget {
corewidget := sws.NewCoreWidget(w, h)
widget := &WorldmapWidget{
CoreWidget: *corewidget,
background: nil,
}
surface, err := sdl.CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000)
if err != nil {
panic(err)
}
widget.background = surface
if image, err := global.LoadImageAsset("assets/ui/worldmap.jpg"); err == nil {
dstw := w
dsth := h
if image.W*h > image.H*w {
dsth = image.H * w / image.W
widget.scale = float64(w) / float64(image.W)
} else {
dstw = image.W * h / image.H
widget.scale = float64(h) / float64(image.H)
}
widget.xshift = (w - dstw) / 2
widget.yshift = (h - dsth) / 2
image.BlitScaled(&sdl.Rect{X: 0, Y: 0, W: image.W, H: image.H}, widget.background, &sdl.Rect{X: widget.xshift, Y: widget.yshift, W: dstw, H: dsth})
} else {
panic(err)
}
return widget
}