-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoreWSI.go
185 lines (150 loc) · 6.08 KB
/
coreWSI.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
package frenyard
// ExitFlag when set to true, exits the application.
var ExitFlag bool = false
// Backend is the set of "entrypoint" functions to the core API.
type Backend interface {
// Begins the frame loop. Stops when ExitFlag is set to true.
Run(ticker func(frameTime float64)) error
CreateWindow(name string, size Vec2i, vsync bool, receiver WindowReceiver) (Window, error)
CreateTexture(size Vec2i, pixels []uint32) Texture
}
// GlobalBackend is the global instance of Backend.
var GlobalBackend Backend
// TargetFrameTime controls the framerate of the application.
var TargetFrameTime float64 = 0.05 // 20FPS
// WindowReceiver receives window events.
type WindowReceiver interface {
FyRStart(w Window)
FyRTick(time float64)
FyRNormalEvent(n NormalEvent)
FyRMouseEvent(m MouseEvent)
// Note: The window is destroyed after this completes.
FyRClose()
}
// Window type. This type MAY be user-implemented with the understanding that no Core/CoreExt functions accept Window or Renderer (hence there are no potential issues), and that this is still intended as a Core/CoreExt type so doesn't get the name-prefixing.
type Window interface {
Renderer
Name() string
SetName(name string)
Present()
Destroy()
// Gets the DPI of the window. This can change. Oh well.
GetLocalDPI() float64
// Sets the size of the window (if possible)
SetSize(size Vec2i)
// Gets the text input, or nil for no input (useful to check when unfocusing)
TextInput() TextInput
// Sets the text input, or nil for no input
SetTextInput(input TextInput)
}
// TextInput represents a text input.
type TextInput interface {
// Called when the text input is made active.
FyTOpen()
// Area gets the area of the TextInput.
FyTArea() Area2i
// Sets the candidate buffer.
FyTEditing(text string, start int, length int)
// Writes into the editing text buffer and clears the candidate buffer.
FyTInput(text string)
// Called when the text input is changed to any other input.
FyTClose()
}
/*
* There are two kinds of event: Normal events and Mouse Events.
* Mouse Events get lots of special treatment as they bypass focus targeting, need offset logic, etc.
* Normal Events have handling based on some booleans and are mostly defined in the framework.
*/
// NormalEventRoute represents a target for events.
type NormalEventRoute uint8
const (
// NormalEventRouteStop Do not forward.
NormalEventRouteStop NormalEventRoute = iota
// NormalEventRouteFocus Routes to the focused element.
NormalEventRouteFocus
// NormalEventRouteBroadcast Broadcast to every element.
NormalEventRouteBroadcast
// NormalEventRouteStructuralBroadcast Broadcast to every element, even if an event-blocking firewall is active.
NormalEventRouteStructuralBroadcast
)
// NormalEvent is the base of standard, non-special event types
type NormalEvent interface {
// Indicates how to route the event.
FyVRoute() NormalEventRoute
// Offset the event by some amount. Apply the same way as MouseEvent.
FyVOffset(amount Vec2i) NormalEvent
}
// SDL keycode modifiers - https://github.com/libsdl-org/SDL/blob/213fbd0/include/SDL3/SDL_keycode.h#L332-L354
// (also see https://wiki.libsdl.org/SDL2/SDLKeycodeLookup)
const (
ModifierCtrl uint16 = 0x40 | 0x80
ModifierShift uint16 = 0x1 | 0x2
ModifierAlt uint16 = 0x100 | 0x200
)
// KeyEvent represents a key changing state. It uses the same constants as SDL2 because I never planned for key input to be in here, but then someone said "what about a search box". Nevermind that this opens a massive can of worms...
type KeyEvent struct {
Pressed bool
Scancode int32
Keycode int32
Modifiers uint16
}
// FyVRoute implements NormalEvent.FyVRoute
func (ke KeyEvent) FyVRoute() NormalEventRoute {
return NormalEventRouteFocus
}
// FyVOffset implements NormalEvent.FyVOffset
func (ke KeyEvent) FyVOffset(amount Vec2i) NormalEvent {
return ke
}
// MouseEventID describes a type of MouseEvent.
type MouseEventID uint8
// MouseEventMove indicates that the event is because the mouse was moved.
const MouseEventMove MouseEventID = 0
// MouseEventDown indicates that the event is because a mouse button was pressed
const MouseEventDown MouseEventID = 1
// MouseEventUp indicates that the event is because a mouse button was released
const MouseEventUp MouseEventID = 2
// MouseButton describes a mouse button.
type MouseButton int8
// MouseButtonNone indicates that no button was involved. Only appears for MouseEventMove
const MouseButtonNone MouseButton = -1
// Numbers from here forward must be allocatable in _fy_Panel_ButtonsDown
// Including the scroll buttons!
// MouseButtonLeft is the left mouse button.
const MouseButtonLeft MouseButton = 0
// MouseButtonMiddle is the middle mouse button (Do be warned: Laptop users do not get this in any 'easy to understand' form.)
const MouseButtonMiddle MouseButton = 1
// MouseButtonRight is the right mouse button
const MouseButtonRight MouseButton = 2
// MouseButtonX1 is a fancy auxiliary mouse button that not all people have
const MouseButtonX1 MouseButton = 3
// MouseButtonX2 is a fancy auxiliary mouse button that not all people have
const MouseButtonX2 MouseButton = 4
// These are a form of button because it simplifies the implementation massively at no real cost.
// MouseButtonScrollUp is a virtual scroll
const MouseButtonScrollUp MouseButton = 5
// MouseButtonScrollDown is a virtual scroll
const MouseButtonScrollDown MouseButton = 6
// MouseButtonScrollLeft is a virtual scroll
const MouseButtonScrollLeft MouseButton = 7
// MouseButtonScrollRight is a virtual scroll
const MouseButtonScrollRight MouseButton = 8
// MouseButtonLength is not a real button. You may need to use (int8)(0) in for loops.
const MouseButtonLength MouseButton = 9
// MouseEvent is a mouse event.
type MouseEvent struct {
// Where the mouse is *relative to the receiving element.*
Pos Vec2i
// Indicates the sub-type of the event.
ID MouseEventID
// Meaningless for MOUSEEVENT_MOVE. See MOUSEBUTTON_*
Button MouseButton
}
// Offset offsets the mouse event by a given amount.
func (ev MouseEvent) Offset(offset Vec2i) MouseEvent {
return MouseEvent{
Pos: ev.Pos.Add(offset),
ID: ev.ID,
Button: ev.Button,
}
}