-
Notifications
You must be signed in to change notification settings - Fork 11
/
window.go
235 lines (200 loc) · 6.26 KB
/
window.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package strife
import (
"fmt"
"log"
"runtime"
"github.com/veandco/go-sdl2/sdl"
)
// RenderInstance is the current render instance, in an ideal
// world this will only be set once. This is exists
// because SDL wants to have the render instance for
// loading images, fonts, etc.
var RenderInstance *Renderer
func init() {
runtime.LockOSThread()
}
// RenderWindow is a window context
// with a rendererer. It contains the handlers
// for events, as well as the render configuration options.
type RenderWindow struct {
*sdl.Window
config *RenderConfig
renderContext *Renderer
handler func(StrifeEvent)
w, h int
closeRequested bool
flags uint32
}
// SetIconImage will set the window icon from the given Image
func (w *RenderWindow) SetIconImage(img *Image) {
w.SetIcon(img.Surface)
}
// CloseRequested will return if the window has
// had a CloseRequest even triggered.
func (w *RenderWindow) CloseRequested() bool {
return w.closeRequested
}
// HandleEvents will set the event handler predicate.
func (w *RenderWindow) HandleEvents(handler func(StrifeEvent)) {
w.handler = handler
}
func (w *RenderWindow) handleKeyboardEvent(evt *sdl.KeyboardEvent) {
keyCode := int(evt.Keysym.Sym)
if evt.Type == sdl.KEYUP {
w.handler(&KeyUpEvent{BaseEvent{}, keyCode})
keyboardInstance.keys[keyCode] = false
} else if evt.Type == sdl.KEYDOWN {
w.handler(&KeyDownEvent{BaseEvent{}, keyCode})
keyboardInstance.keys[keyCode] = true
// append the key press into a key
// buffer which can be processed.
keyboardInstance.buff = append(keyboardInstance.buff, keyCode)
}
}
func (w *RenderWindow) handleMouseButtonEvent(evt *sdl.MouseButtonEvent) {
if evt.Type == sdl.MOUSEBUTTONUP {
mouseInstance.ButtonState = NoMouseButtonsDown
return
}
switch evt.Button {
case sdl.BUTTON_LEFT:
mouseInstance.ButtonState = LeftMouseButton
case sdl.BUTTON_MIDDLE:
mouseInstance.ButtonState = ScrollWheel
case sdl.BUTTON_RIGHT:
mouseInstance.ButtonState = RightMouseButton
}
}
func (w *RenderWindow) handleMouseMotionEvent(evt *sdl.MouseMotionEvent) {
w.handler(&MouseMoveEvent{BaseEvent{}, int(evt.X), int(evt.Y)})
mouseInstance.X = int(evt.X)
mouseInstance.Y = int(evt.Y)
switch evt.State {
case sdl.BUTTON_LEFT:
mouseInstance.ButtonState = LeftMouseButton
case sdl.BUTTON_MIDDLE:
mouseInstance.ButtonState = ScrollWheel
case sdl.BUTTON_RIGHT:
mouseInstance.ButtonState = RightMouseButton
default:
mouseInstance.ButtonState = NoMouseButtonsDown
}
}
func (w *RenderWindow) handleWindowEvent(event *sdl.WindowEvent) {
switch event.Event {
// events that affect visibility
case sdl.WINDOWEVENT_HIDDEN:
w.handler(&WindowVisibilityEvent{BaseEvent{}, Hidden})
case sdl.WINDOWEVENT_SHOWN:
w.handler(&WindowVisibilityEvent{BaseEvent{}, Shown})
case sdl.WINDOWEVENT_EXPOSED:
w.handler(&WindowVisibilityEvent{BaseEvent{}, Exposed})
// size/position stuff
case sdl.WINDOWEVENT_SIZE_CHANGED:
// should this be handled as its own event
// or as a resized event?
fallthrough
case sdl.WINDOWEVENT_RESIZED:
w.handler(&WindowResizeEvent{BaseEvent{}, int(event.Data1), int(event.Data2)})
case sdl.WINDOWEVENT_MOVED:
w.handler(&WindowMoveEvent{BaseEvent{}, int(event.Data1), int(event.Data2)})
// TODO: ENTER/LEAVE ... CLOSE?
// events that are to do with focus!
case sdl.WINDOWEVENT_FOCUS_GAINED:
w.handler(&WindowFocusEvent{BaseEvent{}, FocusLost})
case sdl.WINDOWEVENT_FOCUS_LOST:
w.handler(&WindowFocusEvent{BaseEvent{}, FocusGained})
}
}
func (w *RenderWindow) processEvent(event sdl.Event) {
switch evt := event.(type) {
case *sdl.QuitEvent:
w.handler(&CloseEvent{BaseEvent{}})
case *sdl.KeyboardEvent:
w.handleKeyboardEvent(evt)
case *sdl.MouseButtonEvent:
w.handleMouseButtonEvent(evt)
case *sdl.MouseMotionEvent:
w.handleMouseMotionEvent(evt)
case *sdl.MouseWheelEvent:
w.handler(&MouseWheelEvent{BaseEvent{}, int(evt.X), int(evt.Y)})
case *sdl.WindowEvent:
w.handleWindowEvent(evt)
default:
// log.Println("unhandled event!", reflect.TypeOf(evt), evt, " ... please file an issue on GitHub!")
}
}
// PollEvents will poll for any events. All events
// are unhandled, _except_ for the Quit Event, which will
// destroy the render context, render window, and cause
// this function to return true.
func (w *RenderWindow) PollEvents() {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
w.processEvent(event)
}
}
// GetRenderContext returns the render context
func (w *RenderWindow) GetRenderContext() *Renderer {
return w.renderContext
}
// Close will close the render window
// and destroy the context.
func (w *RenderWindow) Close() {
w.closeRequested = true
w.renderContext.Destroy()
w.Destroy()
}
// SetResizable will add the resizable flag, must be called
// before Create()
func (w *RenderWindow) SetResizable(resizable bool) {
w.flags |= sdl.WINDOW_RESIZABLE
}
// AllowHighDPI will allow a high DPI, must be called
// before Create()
func (w *RenderWindow) AllowHighDPI() {
w.flags |= sdl.WINDOW_ALLOW_HIGHDPI
}
// Create window take all of the settings and create
// a window with a rendering context
func (w *RenderWindow) Create() error {
windowHandle, err := sdl.CreateWindow("", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, int32(w.w), int32(w.h), w.flags)
if err != nil {
return fmt.Errorf("Failed to create window")
}
w.Window = windowHandle
renderer, err := CreateRenderer(w, w.config)
if err != nil {
return err
}
w.renderContext = renderer
RenderInstance = renderer
return nil
}
// GetSize will return the window width and height
func (w *RenderWindow) GetSize() (int, int) {
ww, hh := w.Window.GetSize()
return int(ww), int(hh)
}
// SetupRenderWindow will create a render window of the given
// width and height, with the specified configuration. You can
// specify a default configuration with `strife.DefaultConfig()`.
// Note that this will spawn the window at the centre of the main
// display.
func SetupRenderWindow(w, h int, config *RenderConfig) *RenderWindow {
log.Println("initializing window ", w, "x", h)
EnableDPI()
a, b := GetDisplayDPI(0)
log.Println("dpi, default_dpi = ", a, b)
window := &RenderWindow{
config: config,
w: w,
h: h,
}
window.handler = func(evt StrifeEvent) {
switch evt.(type) {
case *CloseEvent:
window.Close()
}
}
return window
}