Skip to content

Commit 863e660

Browse files
committed
Easier event handling, fix #2
1 parent 64098af commit 863e660

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

examples/short_example.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,24 @@ music = sfMusic_createFromFile(joinpath(@__DIR__, "Chrono_Trigger.ogg"))
2525

2626
sfMusic_play(music)
2727

28-
event_ref = Ref(sfEvent(sfEvtClosed))
28+
event_ref = Ref{sfEvent}()
2929

3030
while Bool(sfRenderWindow_isOpen(window))
3131
# process events
3232
while Bool(sfRenderWindow_pollEvent(window, event_ref))
3333
# close window : exit
34-
event_ref.x.type == sfEvtClosed && sfRenderWindow_close(window)
34+
event_ref[].type == sfEvtClosed && sfRenderWindow_close(window)
35+
event_ref[].type == sfEvtResized && println("Trigger sfEvtResized.")
36+
event_ref[].type == sfEvtLostFocus && println("Trigger sfEvtLostFocus.")
37+
event_ref[].type == sfEvtGainedFocus && println("Trigger sfEvtGainedFocus.")
38+
event_ref[].type == sfEvtTextEntered && println("Trigger sfEvtTextEntered: $(event_ref[].text.unicode)")
39+
event_ref[].type == sfEvtKeyPressed && println("Trigger sfEvtKeyPressed: $(event_ref[].key.code)")
40+
event_ref[].type == sfEvtKeyReleased && println("Trigger sfEvtKeyReleased: $(event_ref[].key.code)")
41+
event_ref[].type == sfEvtMouseWheelMoved && println("Trigger sfEvtMouseWheelMoved: $(event_ref[].mouseWheel.sfMouseVerticalWheel)")
42+
event_ref[].type == sfEvtMouseWheelScrolled && println("Trigger sfEvtMouseWheelScrolled: $(event_ref[].mouseWheel.sfMouseVerticalWheel)")
43+
event_ref[].type == sfEvtMouseButtonPressed && println("Trigger sfEvtMouseButtonPressed: $(event_ref[].mouseButton.button)")
44+
event_ref[].type == sfEvtMouseButtonReleased && println("Trigger sfEvtMouseButtonReleased: $(event_ref[].mouseButton.x), $(event_ref[].mouseButton.y)")
45+
event_ref[].type == sfEvtMouseMoved && println("Trigger sfEvtMouseMoved: $(event_ref[].mouseMove.x), $(event_ref[].mouseMove.y)")
3546
end
3647
# clear the screen
3748
sfRenderWindow_clear(window, sfColor_fromRGBA(0,0,0,1))

src/Window/window_api.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window.h
1+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window.h
22
# Automatically generated using Clang.jl
33

4-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Clipboard.h
4+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Clipboard.h
55
# Automatically generated using Clang.jl
66

77

@@ -20,7 +20,7 @@ end
2020
function sfClipboard_setUnicodeString(text)
2121
ccall((:sfClipboard_setUnicodeString, libcsfml_window), Cvoid, (Ptr{sfUint32},), text)
2222
end
23-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Context.h
23+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Context.h
2424
# Automatically generated using Clang.jl
2525

2626

@@ -43,7 +43,7 @@ end
4343
function sfContext_getActiveContextId()
4444
ccall((:sfContext_getActiveContextId, libcsfml_window), sfUint64, ())
4545
end
46-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Cursor.h
46+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Cursor.h
4747
# Automatically generated using Clang.jl
4848

4949

@@ -58,13 +58,13 @@ end
5858
function sfCursor_destroy(cursor)
5959
ccall((:sfCursor_destroy, libcsfml_window), Cvoid, (Ptr{sfCursor},), cursor)
6060
end
61-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Event.h
61+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Event.h
6262
# Automatically generated using Clang.jl
6363

64-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Export.h
64+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Export.h
6565
# Automatically generated using Clang.jl
6666

67-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Joystick.h
67+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Joystick.h
6868
# Automatically generated using Clang.jl
6969

7070

@@ -95,10 +95,10 @@ end
9595
function sfJoystick_update()
9696
ccall((:sfJoystick_update, libcsfml_window), Cvoid, ())
9797
end
98-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/JoystickIdentification.h
98+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/JoystickIdentification.h
9999
# Automatically generated using Clang.jl
100100

101-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Keyboard.h
101+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Keyboard.h
102102
# Automatically generated using Clang.jl
103103

104104

@@ -109,7 +109,7 @@ end
109109
function sfKeyboard_setVirtualKeyboardVisible(visible)
110110
ccall((:sfKeyboard_setVirtualKeyboardVisible, libcsfml_window), Cvoid, (sfBool,), visible)
111111
end
112-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Mouse.h
112+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Mouse.h
113113
# Automatically generated using Clang.jl
114114

115115

@@ -124,7 +124,7 @@ end
124124
function sfMouse_setPosition(position, relativeTo)
125125
ccall((:sfMouse_setPosition, libcsfml_window), Cvoid, (sfVector2i, Ptr{sfWindow}), position, relativeTo)
126126
end
127-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Sensor.h
127+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Sensor.h
128128
# Automatically generated using Clang.jl
129129

130130

@@ -139,7 +139,7 @@ end
139139
function sfSensor_getValue(sensor)
140140
ccall((:sfSensor_getValue, libcsfml_window), sfVector3f, (sfSensorType,), sensor)
141141
end
142-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Touch.h
142+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Touch.h
143143
# Automatically generated using Clang.jl
144144

145145

@@ -150,10 +150,10 @@ end
150150
function sfTouch_getPosition(finger, relativeTo)
151151
ccall((:sfTouch_getPosition, libcsfml_window), sfVector2i, (UInt32, Ptr{sfWindow}), finger, relativeTo)
152152
end
153-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Types.h
153+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Types.h
154154
# Automatically generated using Clang.jl
155155

156-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/VideoMode.h
156+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/VideoMode.h
157157
# Automatically generated using Clang.jl
158158

159159

@@ -168,7 +168,7 @@ end
168168
function sfVideoMode_isValid(mode)
169169
ccall((:sfVideoMode_isValid, libcsfml_window), sfBool, (sfVideoMode,), mode)
170170
end
171-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/Window.h
171+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/Window.h
172172
# Automatically generated using Clang.jl
173173

174174

@@ -287,6 +287,6 @@ end
287287
function sfWindow_getSystemHandle(window)
288288
ccall((:sfWindow_getSystemHandle, libcsfml_window), sfWindowHandle, (Ptr{sfWindow},), window)
289289
end
290-
# Julia wrapper for header: /Users/gnimuc/LibCSFML/gen/../deps/usr/include/SFML/Window/WindowHandle.h
290+
# Julia wrapper for header: /Users/gnimuc/.julia/dev/LibCSFML/gen/../deps/usr/include/SFML/Window/WindowHandle.h
291291
# Automatically generated using Clang.jl
292292

src/Window/window_common.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,46 @@ end
264264

265265
struct sfEvent
266266
type::sfEventType
267+
data::NTuple{20,UInt8}
268+
end
269+
270+
@inline function unpack_union(::Type{T}, obj::S) where {T,S}
271+
ref = Ref{S}(obj)
272+
GC.@preserve ref begin
273+
ptr = Base.unsafe_convert(Ptr{Cvoid}, ref)
274+
target = unsafe_load(Ptr{T}(ptr))
275+
end
276+
return target
277+
end
278+
279+
function Base.getproperty(obj::sfEvent, sym::Symbol)
280+
if sym === :type
281+
return getfield(obj, :type)
282+
elseif sym === :size
283+
return unpack_union(sfSizeEvent, obj)
284+
elseif sym === :key
285+
return unpack_union(sfKeyEvent, obj)
286+
elseif sym === :text
287+
return unpack_union(sfTextEvent, obj)
288+
elseif sym === :mouseMove
289+
return unpack_union(sfMouseMoveEvent, obj)
290+
elseif sym === :mouseButton
291+
return unpack_union(sfMouseButtonEvent, obj)
292+
elseif sym === :mouseWheel
293+
return unpack_union(sfMouseWheelEvent, obj)
294+
elseif sym === :mouseWheelScroll
295+
return unpack_union(sfMouseWheelScrollEvent, obj)
296+
elseif sym === :joystickMove
297+
return unpack_union(sfJoystickMoveEvent, obj)
298+
elseif sym === :joystickButton
299+
return unpack_union(sfJoystickButtonEvent, obj)
300+
elseif sym === :joystickConnect
301+
return unpack_union(sfJoystickConnectEvent, obj)
302+
elseif sym === :touch
303+
return unpack_union(sfTouchEvent, obj)
304+
elseif sym === :sensor
305+
return unpack_union(sfSensorEvent, obj)
306+
end
267307
end
268308

269309
# const CSFML_WINDOW_API = CSFML_API_IMPORT

0 commit comments

Comments
 (0)