b0c4241c837e0889de823abaffe6cf70f66e3843
[nit.git] / lib / gamnit / gamnit_linux.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Support services for Gamnit on GNU/Linux
16 module gamnit_linux
17
18 import sdl2::events
19
20 intrude import gamnit
21 intrude import display_linux
22
23 redef class App
24 private var sdl_event_buffer = new SDLEventBuffer.malloc
25
26 redef fun feed_events
27 do
28 var display = display
29 if display == null then return
30
31 loop
32 var avail = sdl_event_buffer.poll_event
33 if not avail then break
34
35 # Convert to an SDL event with data
36 var sdl_event = sdl_event_buffer.to_event
37 if sdl_event isa SDLWindowEvent and sdl_event.is_resized then
38 display.width = sdl_event.data1
39 display.height = sdl_event.data2
40 on_resize display
41 end
42
43 # Convert to `mnit::inputs` conforming objects
44 var gamnit_event = sdl_event.to_gamnit_event(sdl_event_buffer)
45 accept_event gamnit_event
46 end
47 end
48 end
49
50 # ---
51 # Redef services from `sdl2::events`
52
53 redef class SDLEvent
54 private fun to_gamnit_event(buffer: SDLEventBuffer): GamnitInputEvent
55 do return new GamnitOtherEvent(buffer, self)
56 end
57
58 redef class SDLQuitEvent
59 redef fun to_gamnit_event(buffer) do return new GamnitQuitEvent(buffer, self)
60 end
61
62 redef class SDLMouseEvent
63 redef fun to_gamnit_event(buffer) do return new GamnitPointerEvent(buffer, self)
64 end
65
66 redef class SDLMouseWheelEvent
67 redef fun to_gamnit_event(buffer) do return new GamnitMouseWheelEvent(buffer, self)
68 end
69
70 redef class SDLKeyboardEvent
71 redef fun to_gamnit_event(buffer) do return new GamnitKeyEvent(buffer, self)
72 end
73
74 # ---
75 # Implement `mnit::inputs` interfaces
76
77 # SDL2 event wrapper implementing the `mnit::input` API
78 #
79 # There is two views to the underlying native object: `buffer` and `native`.
80 class GamnitInputEvent
81 super InputEvent
82
83 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
84 var buffer: SDLEventBuffer
85
86 # Native SDL 2 event
87 var native: NATIVE
88
89 # Type of the `native` underlying SDL 2 event
90 type NATIVE: SDLEvent
91 end
92
93 # Event on user requested quit
94 class GamnitQuitEvent
95 super GamnitInputEvent
96 super QuitEvent
97
98 redef type NATIVE: SDLQuitEvent
99 end
100
101 # Event on keyboard input
102 class GamnitKeyEvent
103 super GamnitInputEvent
104 super KeyEvent
105
106 redef type NATIVE: SDLKeyboardEvent
107
108 redef fun is_down do return buffer.is_keydown
109 redef fun is_up do return buffer.is_keyup
110 redef fun is_arrow_up do return native.keysym.is_up
111 redef fun is_arrow_left do return native.keysym.is_left
112 redef fun is_arrow_down do return native.keysym.is_down
113 redef fun is_arrow_right do return native.keysym.is_right
114 redef fun to_c do return native.to_s.chars.first
115 redef fun name do return native.to_s.to_lower
116 end
117
118 # Event on pointer, mouse and finger input
119 class GamnitPointerEvent
120 super GamnitInputEvent
121 super PointerEvent
122
123 redef type NATIVE: SDLMouseEvent
124
125 redef fun x do return native.x.to_f
126 redef fun y do return native.y.to_f
127 redef fun is_move do return buffer.is_mouse_motion
128
129 redef fun pressed
130 do
131 var native = native
132 if native isa SDLMouseButtonEvent then
133 return native.pressed and native.button == 1
134 else if native isa SDLMouseMotionEvent then
135 return native.state & 1 == 1
136 else abort
137 end
138 end
139
140 # Event on mouse wheel input
141 class GamnitMouseWheelEvent
142 super GamnitInputEvent
143
144 redef type NATIVE: SDLMouseWheelEvent
145
146 # Horizontal scroll amount
147 fun x: Float do return native.x.to_f
148
149 # Vertical scroll amount
150 fun y: Float do return native.y.to_f
151 end
152
153 # SDL2 event not handled by gamnit
154 class GamnitOtherEvent
155 super GamnitInputEvent
156 end