ruby2d 0.9.3 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/assets/README.md +7 -9
- data/assets/Rakefile +85 -0
- data/assets/include/SDL2/SDL.h +4 -1
- data/assets/include/SDL2/SDL_assert.h +3 -1
- data/assets/include/SDL2/SDL_atomic.h +20 -2
- data/assets/include/SDL2/SDL_audio.h +47 -14
- data/assets/include/SDL2/SDL_bits.h +10 -1
- data/assets/include/SDL2/SDL_blendmode.h +10 -7
- data/assets/include/SDL2/SDL_clipboard.h +1 -1
- data/assets/include/SDL2/SDL_config.h +24 -390
- data/assets/include/SDL2/SDL_config_android.h +182 -0
- data/assets/include/SDL2/SDL_config_iphoneos.h +207 -0
- data/assets/include/SDL2/SDL_config_macosx.h +266 -0
- data/assets/include/SDL2/SDL_config_minimal.h +85 -0
- data/assets/include/SDL2/SDL_config_os2.h +188 -0
- data/assets/include/SDL2/SDL_config_pandora.h +135 -0
- data/assets/include/SDL2/SDL_config_psp.h +165 -0
- data/assets/include/SDL2/SDL_config_windows.h +288 -0
- data/assets/include/SDL2/SDL_config_winrt.h +243 -0
- data/assets/include/SDL2/SDL_config_wiz.h +149 -0
- data/assets/include/SDL2/SDL_copying.h +20 -0
- data/assets/include/SDL2/SDL_cpuinfo.h +119 -8
- data/assets/include/SDL2/SDL_egl.h +4 -1
- data/assets/include/SDL2/SDL_endian.h +6 -3
- data/assets/include/SDL2/SDL_error.h +38 -2
- data/assets/include/SDL2/SDL_events.h +67 -28
- data/assets/include/SDL2/SDL_filesystem.h +1 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
- data/assets/include/SDL2/SDL_gesture.h +1 -1
- data/assets/include/SDL2/SDL_haptic.h +10 -1
- data/assets/include/SDL2/SDL_hints.h +460 -17
- data/assets/include/SDL2/SDL_image.h +2 -2
- data/assets/include/SDL2/SDL_joystick.h +115 -24
- data/assets/include/SDL2/SDL_keyboard.h +1 -1
- data/assets/include/SDL2/SDL_keycode.h +11 -9
- data/assets/include/SDL2/SDL_loadso.h +1 -1
- data/assets/include/SDL2/SDL_locale.h +101 -0
- data/assets/include/SDL2/SDL_log.h +3 -3
- data/assets/include/SDL2/SDL_main.h +28 -16
- data/assets/include/SDL2/SDL_messagebox.h +6 -4
- data/assets/include/SDL2/SDL_metal.h +117 -0
- data/assets/include/SDL2/SDL_misc.h +75 -0
- data/assets/include/SDL2/SDL_mouse.h +1 -1
- data/assets/include/SDL2/SDL_mutex.h +1 -1
- data/assets/include/SDL2/SDL_name.h +1 -1
- data/assets/include/SDL2/SDL_opengl.h +1 -1
- data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
- data/assets/include/SDL2/SDL_opengles.h +1 -1
- data/assets/include/SDL2/SDL_opengles2.h +1 -1
- data/assets/include/SDL2/SDL_pixels.h +27 -18
- data/assets/include/SDL2/SDL_platform.h +1 -1
- data/assets/include/SDL2/SDL_power.h +1 -1
- data/assets/include/SDL2/SDL_quit.h +1 -1
- data/assets/include/SDL2/SDL_rect.h +29 -3
- data/assets/include/SDL2/SDL_render.h +230 -3
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +51 -22
- data/assets/include/SDL2/SDL_scancode.h +2 -2
- data/assets/include/SDL2/SDL_sensor.h +28 -12
- data/assets/include/SDL2/SDL_shape.h +1 -1
- data/assets/include/SDL2/SDL_stdinc.h +44 -4
- data/assets/include/SDL2/SDL_surface.h +12 -2
- data/assets/include/SDL2/SDL_system.h +50 -4
- data/assets/include/SDL2/SDL_syswm.h +39 -9
- data/assets/include/SDL2/SDL_test.h +1 -1
- data/assets/include/SDL2/SDL_test_assert.h +1 -1
- data/assets/include/SDL2/SDL_test_common.h +32 -2
- data/assets/include/SDL2/SDL_test_compare.h +1 -1
- data/assets/include/SDL2/SDL_test_crc32.h +1 -1
- data/assets/include/SDL2/SDL_test_font.h +1 -1
- data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
- data/assets/include/SDL2/SDL_test_harness.h +1 -1
- data/assets/include/SDL2/SDL_test_images.h +1 -1
- data/assets/include/SDL2/SDL_test_log.h +1 -1
- data/assets/include/SDL2/SDL_test_md5.h +1 -1
- data/assets/include/SDL2/SDL_test_memory.h +3 -3
- data/assets/include/SDL2/SDL_test_random.h +1 -1
- data/assets/include/SDL2/SDL_thread.h +34 -11
- data/assets/include/SDL2/SDL_timer.h +1 -1
- data/assets/include/SDL2/SDL_touch.h +17 -1
- data/assets/include/SDL2/SDL_types.h +1 -1
- data/assets/include/SDL2/SDL_version.h +2 -2
- data/assets/include/SDL2/SDL_video.h +11 -5
- data/assets/include/SDL2/SDL_vulkan.h +9 -11
- data/assets/include/SDL2/begin_code.h +8 -9
- data/assets/include/SDL2/close_code.h +4 -1
- data/assets/macos/lib/libFLAC.a +0 -0
- data/assets/macos/lib/libSDL2.a +0 -0
- data/assets/macos/lib/libSDL2_image.a +0 -0
- data/assets/macos/lib/libSDL2_mixer.a +0 -0
- data/assets/macos/lib/libSDL2_ttf.a +0 -0
- data/assets/macos/lib/libfreetype.a +0 -0
- data/assets/macos/lib/libjpeg.a +0 -0
- data/assets/macos/lib/libmpg123.a +0 -0
- data/assets/macos/lib/libogg.a +0 -0
- data/assets/macos/lib/libpng16.a +0 -0
- data/assets/macos/lib/libtiff.a +0 -0
- data/assets/macos/lib/libvorbis.a +0 -0
- data/assets/macos/lib/libvorbisfile.a +0 -0
- data/assets/macos/lib/libwebp.a +0 -0
- data/assets/mingw/bin/SDL2.dll +0 -0
- data/assets/mingw/bin/SDL2_image.dll +0 -0
- data/assets/mingw/bin/libpng16-16.dll +0 -0
- data/assets/mingw/bin/libtiff-5.dll +0 -0
- data/assets/mingw/bin/libwebp-7.dll +0 -0
- data/assets/mingw/lib/libSDL2.a +0 -0
- data/assets/mingw/lib/libSDL2.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_image.a +0 -0
- data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_test.a +0 -0
- data/assets/mingw/lib/libSDL2main.a +0 -0
- data/bin/ruby2d +1 -0
- data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
- data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
- data/ext/ruby2d/extconf.rb +36 -58
- data/ext/ruby2d/font.c +35 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +72 -100
- data/ext/ruby2d/gl2.c +86 -0
- data/ext/ruby2d/gl3.c +305 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
- data/ext/ruby2d/image.c +58 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
- data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
- data/ext/ruby2d/ruby2d.c +449 -439
- data/ext/ruby2d/ruby2d.h +652 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
- data/ext/ruby2d/sound.c +118 -0
- data/ext/ruby2d/text.c +22 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +73 -62
- data/lib/ruby2d/circle.rb +21 -3
- data/lib/ruby2d/cli/build.rb +4 -7
- data/lib/ruby2d/dsl.rb +16 -9
- data/lib/ruby2d/entity.rb +17 -0
- data/lib/ruby2d/font.rb +23 -3
- data/lib/ruby2d/image.rb +35 -7
- data/lib/ruby2d/line.rb +23 -1
- data/lib/ruby2d/music.rb +5 -0
- data/lib/ruby2d/pixel.rb +17 -0
- data/lib/ruby2d/quad.rb +21 -1
- data/lib/ruby2d/rectangle.rb +12 -3
- data/lib/ruby2d/renderable.rb +4 -20
- data/lib/ruby2d/sound.rb +30 -0
- data/lib/ruby2d/sprite.rb +57 -75
- data/lib/ruby2d/square.rb +10 -1
- data/lib/ruby2d/text.rb +55 -12
- data/lib/ruby2d/texture.rb +28 -0
- data/lib/ruby2d/tileset.rb +87 -0
- data/lib/ruby2d/triangle.rb +19 -1
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/vertices.rb +84 -0
- data/lib/ruby2d/window.rb +259 -22
- data/lib/ruby2d.rb +6 -1
- metadata +41 -26
- data/assets/include/simple2d.h +0 -735
- data/assets/linux/simple2d/Makefile +0 -257
- data/assets/linux/simple2d/bin/simple2d.sh +0 -1312
- data/assets/linux/simple2d/include/simple2d.h +0 -735
- data/assets/linux/simple2d/src/gl2.c +0 -146
- data/assets/linux/simple2d/src/gl3.c +0 -275
- data/assets/linux/simple2d/src/image.c +0 -138
- data/assets/linux/simple2d/src/sound.c +0 -56
- data/assets/linux/simple2d/src/sprite.c +0 -147
- data/assets/linux/simple2d/src/text.c +0 -129
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
data/lib/ruby2d/triangle.rb
CHANGED
@@ -17,7 +17,7 @@ module Ruby2D
|
|
17
17
|
@y3 = opts[:y3] || 100
|
18
18
|
@z = opts[:z] || 0
|
19
19
|
self.color = opts[:color] || 'white'
|
20
|
-
self.opacity = opts[:opacity] if opts[:opacity]
|
20
|
+
self.color.opacity = opts[:opacity] if opts[:opacity]
|
21
21
|
add
|
22
22
|
end
|
23
23
|
|
@@ -38,8 +38,26 @@ module Ruby2D
|
|
38
38
|
questioned_area <= self_area
|
39
39
|
end
|
40
40
|
|
41
|
+
def self.draw(opts = {})
|
42
|
+
Window.render_ready_check
|
43
|
+
|
44
|
+
ext_draw([
|
45
|
+
opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
|
46
|
+
opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
|
47
|
+
opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3]
|
48
|
+
])
|
49
|
+
end
|
50
|
+
|
41
51
|
private
|
42
52
|
|
53
|
+
def render
|
54
|
+
self.class.ext_draw([
|
55
|
+
@x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
|
56
|
+
@x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
|
57
|
+
@x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a
|
58
|
+
])
|
59
|
+
end
|
60
|
+
|
43
61
|
def triangle_area(x1, y1, x2, y2, x3, y3)
|
44
62
|
(x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
|
45
63
|
end
|
data/lib/ruby2d/version.rb
CHANGED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Ruby2D::Vertices
|
2
|
+
|
3
|
+
# This class generates a vertices array which are passed to the openGL rendering code.
|
4
|
+
# The vertices array is split up into 4 groups (1 - top left, 2 - top right, 3 - bottom right, 4 - bottom left)
|
5
|
+
# This class is responsible for transforming textures, it can scale / crop / rotate and flip textures
|
6
|
+
|
7
|
+
module Ruby2D
|
8
|
+
class Vertices
|
9
|
+
def initialize(x, y, width, height, rotate, crop: nil, flip: nil)
|
10
|
+
@flip = flip
|
11
|
+
@x = x
|
12
|
+
@y = y
|
13
|
+
@width = width.to_f
|
14
|
+
@height = height.to_f
|
15
|
+
|
16
|
+
if @flip == :horizontal || @flip == :both
|
17
|
+
@x = @x + @width
|
18
|
+
@width = -@width
|
19
|
+
end
|
20
|
+
|
21
|
+
if @flip == :vertical || @flip == :both
|
22
|
+
@y = y + @height
|
23
|
+
@height = -@height
|
24
|
+
end
|
25
|
+
|
26
|
+
@rotate = rotate
|
27
|
+
@rx = @x + (@width / 2.0)
|
28
|
+
@ry = @y + (@height / 2.0)
|
29
|
+
@crop = crop
|
30
|
+
end
|
31
|
+
|
32
|
+
def coordinates
|
33
|
+
x1, y1 = rotate(@x, @y); # Top left
|
34
|
+
x2, y2 = rotate(@x + @width, @y); # Top right
|
35
|
+
x3, y3 = rotate(@x + @width, @y + @height); # Bottom right
|
36
|
+
x4, y4 = rotate(@x, @y + @height); # Bottom left
|
37
|
+
|
38
|
+
[ x1, y1, x2, y2, x3, y3, x4, y4 ]
|
39
|
+
end
|
40
|
+
|
41
|
+
def texture_coordinates
|
42
|
+
if @crop.nil?
|
43
|
+
tx1 = 0.0; ty1 = 0.0 # Top left
|
44
|
+
tx2 = 1.0; ty2 = 0.0 # Top right
|
45
|
+
tx3 = 1.0; ty3 = 1.0 # Bottom right
|
46
|
+
tx4 = 0.0; ty4 = 1.0 # Bottom left
|
47
|
+
else
|
48
|
+
tx1 = @crop[:x] / @crop[:image_width].to_f; ty1 = @crop[:y] / @crop[:image_height].to_f # Top left
|
49
|
+
tx2 = tx1 + (@crop[:width] / @crop[:image_width].to_f); ty2 = ty1 # Top right
|
50
|
+
tx3 = tx2; ty3 = ty1 + (@crop[:height] / @crop[:image_height].to_f) # Botttom right
|
51
|
+
tx4 = tx1; ty4 = ty3 # Bottom left
|
52
|
+
end
|
53
|
+
|
54
|
+
[ tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4 ]
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def rotate(x, y)
|
60
|
+
return [x, y] if @rotate == 0
|
61
|
+
|
62
|
+
# Convert from degrees to radians
|
63
|
+
angle = @rotate * Math::PI / 180.0
|
64
|
+
|
65
|
+
# Get the sine and cosine of the angle
|
66
|
+
sa = Math.sin(angle)
|
67
|
+
ca = Math.cos(angle)
|
68
|
+
|
69
|
+
# Translate point to origin
|
70
|
+
x -= @rx
|
71
|
+
y -= @ry
|
72
|
+
|
73
|
+
# Rotate point
|
74
|
+
xnew = x * ca - y * sa;
|
75
|
+
ynew = x * sa + y * ca;
|
76
|
+
|
77
|
+
# Translate point back
|
78
|
+
x = xnew + @rx;
|
79
|
+
y = ynew + @ry;
|
80
|
+
|
81
|
+
[x, y]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/ruby2d/window.rb
CHANGED
@@ -4,22 +4,20 @@
|
|
4
4
|
|
5
5
|
module Ruby2D
|
6
6
|
class Window
|
7
|
+
@@open_window = false
|
7
8
|
|
8
9
|
# Event structures
|
9
|
-
EventDescriptor
|
10
|
-
MouseEvent
|
11
|
-
KeyEvent
|
10
|
+
EventDescriptor = Struct.new(:type, :id)
|
11
|
+
MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y)
|
12
|
+
KeyEvent = Struct.new(:type, :key)
|
12
13
|
ControllerEvent = Struct.new(:which, :type, :axis, :value, :button)
|
13
14
|
ControllerAxisEvent = Struct.new(:which, :axis, :value)
|
14
15
|
ControllerButtonEvent = Struct.new(:which, :button)
|
15
16
|
|
16
17
|
def initialize(args = {})
|
17
18
|
|
18
|
-
# This window instance, stored so it can be called by the class methods
|
19
|
-
@@window = self
|
20
|
-
|
21
19
|
# Title of the window
|
22
|
-
@title = args[:title]
|
20
|
+
@title = args[:title] || "Ruby 2D"
|
23
21
|
|
24
22
|
# Window background color
|
25
23
|
@background = Color.new([0.0, 0.0, 0.0, 1.0])
|
@@ -51,14 +49,39 @@ module Ruby2D
|
|
51
49
|
# Vertical synchronization, set to prevent screen tearing (recommended)
|
52
50
|
@vsync = args[:vsync] || true
|
53
51
|
|
52
|
+
# Renderable objects currently in the window, like a linear scene graph
|
53
|
+
@objects = []
|
54
|
+
|
55
|
+
# Entities currently in the window
|
56
|
+
@entities = []
|
57
|
+
|
54
58
|
# Mouse X and Y position in the window
|
55
59
|
@mouse_x, @mouse_y = 0, 0
|
56
60
|
|
57
61
|
# Controller axis and button mappings file
|
58
62
|
@controller_mappings = File.expand_path('~') + "/.ruby2d/controllers.txt"
|
59
63
|
|
60
|
-
#
|
61
|
-
@
|
64
|
+
# Event stores for class pattern
|
65
|
+
@keys_down = []
|
66
|
+
@keys_held = []
|
67
|
+
@keys_up = []
|
68
|
+
@mouse_buttons_down = []
|
69
|
+
@mouse_buttons_up = []
|
70
|
+
@mouse_scroll_event = false
|
71
|
+
@mouse_scroll_direction = nil
|
72
|
+
@mouse_scroll_delta_x = 0
|
73
|
+
@mouse_scroll_delta_y = 0
|
74
|
+
@mouse_move_event = false
|
75
|
+
@mouse_move_delta_x = 0
|
76
|
+
@mouse_move_delta_y = 0
|
77
|
+
@controller_id = nil
|
78
|
+
@controller_axes_moved = []
|
79
|
+
@controller_axis_left_x = 0
|
80
|
+
@controller_axis_left_y = 0
|
81
|
+
@controller_axis_right_x = 0
|
82
|
+
@controller_axis_right_y = 0
|
83
|
+
@controller_buttons_down = []
|
84
|
+
@controller_buttons_up = []
|
62
85
|
|
63
86
|
# Unique ID for the input event being registered
|
64
87
|
@event_key = 0
|
@@ -83,6 +106,12 @@ module Ruby2D
|
|
83
106
|
# The window update block
|
84
107
|
@update_proc = Proc.new {}
|
85
108
|
|
109
|
+
# The window render block
|
110
|
+
@render_proc = Proc.new {}
|
111
|
+
|
112
|
+
# Detect if window is being used through the DSL or as a class instance
|
113
|
+
@using_dsl = !(method(:update).parameters.empty? || method(:render).parameters.empty?)
|
114
|
+
|
86
115
|
# Whether diagnostic messages should be printed
|
87
116
|
@diagnostics = false
|
88
117
|
|
@@ -118,43 +147,53 @@ module Ruby2D
|
|
118
147
|
def screenshot(opts = nil); get(:screenshot, opts) end
|
119
148
|
|
120
149
|
def get(sym, opts = nil)
|
121
|
-
|
150
|
+
DSL.window.get(sym, opts)
|
122
151
|
end
|
123
152
|
|
124
153
|
def set(opts)
|
125
|
-
|
154
|
+
DSL.window.set(opts)
|
126
155
|
end
|
127
156
|
|
128
157
|
def on(event, &proc)
|
129
|
-
|
158
|
+
DSL.window.on(event, &proc)
|
130
159
|
end
|
131
160
|
|
132
161
|
def off(event_descriptor)
|
133
|
-
|
162
|
+
DSL.window.off(event_descriptor)
|
134
163
|
end
|
135
164
|
|
136
165
|
def add(o)
|
137
|
-
|
166
|
+
DSL.window.add(o)
|
138
167
|
end
|
139
168
|
|
140
169
|
def remove(o)
|
141
|
-
|
170
|
+
DSL.window.remove(o)
|
142
171
|
end
|
143
172
|
|
144
173
|
def clear
|
145
|
-
|
174
|
+
DSL.window.clear
|
146
175
|
end
|
147
176
|
|
148
177
|
def update(&proc)
|
149
|
-
|
178
|
+
DSL.window.update(&proc)
|
179
|
+
end
|
180
|
+
|
181
|
+
def render(&proc)
|
182
|
+
DSL.window.render(&proc)
|
150
183
|
end
|
151
184
|
|
152
185
|
def show
|
153
|
-
|
186
|
+
DSL.window.show
|
154
187
|
end
|
155
188
|
|
156
189
|
def close
|
157
|
-
|
190
|
+
DSL.window.close
|
191
|
+
end
|
192
|
+
|
193
|
+
def render_ready_check
|
194
|
+
unless @@open_window
|
195
|
+
raise Error, "Attempting to draw before the window is ready. Please put calls to draw() inside of a render block."
|
196
|
+
end
|
158
197
|
end
|
159
198
|
end
|
160
199
|
|
@@ -219,6 +258,8 @@ module Ruby2D
|
|
219
258
|
case o
|
220
259
|
when nil
|
221
260
|
raise Error, "Cannot add '#{o.class}' to window!"
|
261
|
+
when Entity
|
262
|
+
@entities.push(o)
|
222
263
|
when Array
|
223
264
|
o.each { |x| add_object(x) }
|
224
265
|
else
|
@@ -232,8 +273,9 @@ module Ruby2D
|
|
232
273
|
raise Error, "Cannot remove '#{o.class}' from window!"
|
233
274
|
end
|
234
275
|
|
235
|
-
|
236
|
-
|
276
|
+
collection = o.class.ancestors.include?(Ruby2D::Entity) ? @entities : @objects
|
277
|
+
if i = collection.index(o)
|
278
|
+
collection.delete_at(i)
|
237
279
|
true
|
238
280
|
else
|
239
281
|
false
|
@@ -243,14 +285,21 @@ module Ruby2D
|
|
243
285
|
# Clear all objects from the window
|
244
286
|
def clear
|
245
287
|
@objects.clear
|
288
|
+
@entities.clear
|
246
289
|
end
|
247
290
|
|
248
|
-
# Set
|
291
|
+
# Set the update callback
|
249
292
|
def update(&proc)
|
250
293
|
@update_proc = proc
|
251
294
|
true
|
252
295
|
end
|
253
296
|
|
297
|
+
# Set the render callback
|
298
|
+
def render(&proc)
|
299
|
+
@render_proc = proc
|
300
|
+
true
|
301
|
+
end
|
302
|
+
|
254
303
|
# Generate a new event key (ID)
|
255
304
|
def new_event_key
|
256
305
|
@event_key = @event_key.next
|
@@ -271,6 +320,21 @@ module Ruby2D
|
|
271
320
|
@events[event_descriptor.type].delete(event_descriptor.id)
|
272
321
|
end
|
273
322
|
|
323
|
+
# Key down event method for class pattern
|
324
|
+
def key_down(key)
|
325
|
+
@keys_down.include? key
|
326
|
+
end
|
327
|
+
|
328
|
+
# Key held event method for class pattern
|
329
|
+
def key_held(key)
|
330
|
+
@keys_held.include? key
|
331
|
+
end
|
332
|
+
|
333
|
+
# Key up event method for class pattern
|
334
|
+
def key_up(key)
|
335
|
+
@keys_up.include? key
|
336
|
+
end
|
337
|
+
|
274
338
|
# Key callback method, called by the native and web extentions
|
275
339
|
def key_callback(type, key)
|
276
340
|
key = key.downcase
|
@@ -283,22 +347,66 @@ module Ruby2D
|
|
283
347
|
case type
|
284
348
|
# When key is pressed, fired once
|
285
349
|
when :down
|
350
|
+
# For class pattern
|
351
|
+
unless @using_dsl
|
352
|
+
unless @keys_down.include? key
|
353
|
+
@keys_down << key
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
# Call event handler
|
286
358
|
@events[:key_down].each do |id, e|
|
287
359
|
e.call(KeyEvent.new(type, key))
|
288
360
|
end
|
289
361
|
# When key is being held down, fired every frame
|
290
362
|
when :held
|
363
|
+
# For class pattern
|
364
|
+
unless @using_dsl
|
365
|
+
unless @keys_held.include? key
|
366
|
+
@keys_held << key
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
# Call event handler
|
291
371
|
@events[:key_held].each do |id, e|
|
292
372
|
e.call(KeyEvent.new(type, key))
|
293
373
|
end
|
294
374
|
# When key released, fired once
|
295
375
|
when :up
|
376
|
+
# For class pattern
|
377
|
+
unless @using_dsl
|
378
|
+
unless @keys_up.include? key
|
379
|
+
@keys_up << key
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
# Call event handler
|
296
384
|
@events[:key_up].each do |id, e|
|
297
385
|
e.call(KeyEvent.new(type, key))
|
298
386
|
end
|
299
387
|
end
|
300
388
|
end
|
301
389
|
|
390
|
+
# Mouse down event method for class pattern
|
391
|
+
def mouse_down(btn)
|
392
|
+
@mouse_buttons_down.include? btn
|
393
|
+
end
|
394
|
+
|
395
|
+
# Mouse up event method for class pattern
|
396
|
+
def mouse_up(btn)
|
397
|
+
@mouse_buttons_up.include? btn
|
398
|
+
end
|
399
|
+
|
400
|
+
# Mouse scroll event method for class pattern
|
401
|
+
def mouse_scroll
|
402
|
+
@mouse_scroll_event
|
403
|
+
end
|
404
|
+
|
405
|
+
# Mouse move event method for class pattern
|
406
|
+
def mouse_move
|
407
|
+
@mouse_move_event
|
408
|
+
end
|
409
|
+
|
302
410
|
# Mouse callback method, called by the native and web extentions
|
303
411
|
def mouse_callback(type, button, direction, x, y, delta_x, delta_y)
|
304
412
|
# All mouse events
|
@@ -309,21 +417,54 @@ module Ruby2D
|
|
309
417
|
case type
|
310
418
|
# When mouse button pressed
|
311
419
|
when :down
|
420
|
+
# For class pattern
|
421
|
+
unless @using_dsl
|
422
|
+
unless @mouse_buttons_down.include? button
|
423
|
+
@mouse_buttons_down << button
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
# Call event handler
|
312
428
|
@events[:mouse_down].each do |id, e|
|
313
429
|
e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
|
314
430
|
end
|
315
431
|
# When mouse button released
|
316
432
|
when :up
|
433
|
+
# For class pattern
|
434
|
+
unless @using_dsl
|
435
|
+
unless @mouse_buttons_up.include? button
|
436
|
+
@mouse_buttons_up << button
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
# Call event handler
|
317
441
|
@events[:mouse_up].each do |id, e|
|
318
442
|
e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
|
319
443
|
end
|
320
444
|
# When mouse motion / movement
|
321
445
|
when :scroll
|
446
|
+
# For class pattern
|
447
|
+
unless @using_dsl
|
448
|
+
@mouse_scroll_event = true
|
449
|
+
@mouse_scroll_direction = direction
|
450
|
+
@mouse_scroll_delta_x = delta_x
|
451
|
+
@mouse_scroll_delta_y = delta_y
|
452
|
+
end
|
453
|
+
|
454
|
+
# Call event handler
|
322
455
|
@events[:mouse_scroll].each do |id, e|
|
323
456
|
e.call(MouseEvent.new(type, nil, direction, nil, nil, delta_x, delta_y))
|
324
457
|
end
|
325
458
|
# When mouse scrolling, wheel or trackpad
|
326
459
|
when :move
|
460
|
+
# For class pattern
|
461
|
+
unless @using_dsl
|
462
|
+
@mouse_move_event = true
|
463
|
+
@mouse_move_delta_x = delta_x
|
464
|
+
@mouse_move_delta_y = delta_y
|
465
|
+
end
|
466
|
+
|
467
|
+
# Call event handler
|
327
468
|
@events[:mouse_move].each do |id, e|
|
328
469
|
e.call(MouseEvent.new(type, nil, nil, x, y, delta_x, delta_y))
|
329
470
|
end
|
@@ -337,6 +478,21 @@ module Ruby2D
|
|
337
478
|
end
|
338
479
|
end
|
339
480
|
|
481
|
+
# Controller axis event method for class pattern
|
482
|
+
def controller_axis(axis)
|
483
|
+
@controller_axes_moved.include? axis
|
484
|
+
end
|
485
|
+
|
486
|
+
# Controller button down event method for class pattern
|
487
|
+
def controller_button_down(btn)
|
488
|
+
@controller_buttons_down.include? btn
|
489
|
+
end
|
490
|
+
|
491
|
+
# Controller button up event method for class pattern
|
492
|
+
def controller_button_up(btn)
|
493
|
+
@controller_buttons_up.include? btn
|
494
|
+
end
|
495
|
+
|
340
496
|
# Controller callback method, called by the native and web extentions
|
341
497
|
def controller_callback(which, type, axis, value, button)
|
342
498
|
# All controller events
|
@@ -347,16 +503,56 @@ module Ruby2D
|
|
347
503
|
case type
|
348
504
|
# When controller axis motion, like analog sticks
|
349
505
|
when :axis
|
506
|
+
|
507
|
+
# For class pattern
|
508
|
+
unless @using_dsl
|
509
|
+
@controller_id = which
|
510
|
+
|
511
|
+
unless @controller_axes_moved.include? axis
|
512
|
+
@controller_axes_moved << axis
|
513
|
+
end
|
514
|
+
|
515
|
+
case axis
|
516
|
+
when :left_x
|
517
|
+
@controller_axis_left_x = value
|
518
|
+
when :left_y
|
519
|
+
@controller_axis_left_y = value
|
520
|
+
when :right_x
|
521
|
+
@controller_axis_right_x = value
|
522
|
+
when :right_y
|
523
|
+
@controller_axis_right_y = value
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
527
|
+
# Call event handler
|
350
528
|
@events[:controller_axis].each do |id, e|
|
351
529
|
e.call(ControllerAxisEvent.new(which, axis, value))
|
352
530
|
end
|
353
531
|
# When controller button is pressed
|
354
532
|
when :button_down
|
533
|
+
# For class pattern
|
534
|
+
unless @using_dsl
|
535
|
+
@controller_id = which
|
536
|
+
unless @controller_buttons_down.include? button
|
537
|
+
@controller_buttons_down << button
|
538
|
+
end
|
539
|
+
end
|
540
|
+
|
541
|
+
# Call event handler
|
355
542
|
@events[:controller_button_down].each do |id, e|
|
356
543
|
e.call(ControllerButtonEvent.new(which, button))
|
357
544
|
end
|
358
545
|
# When controller button is released
|
359
546
|
when :button_up
|
547
|
+
# For class pattern
|
548
|
+
unless @using_dsl
|
549
|
+
@controller_id = which
|
550
|
+
unless @controller_buttons_up.include? button
|
551
|
+
@controller_buttons_up << button
|
552
|
+
end
|
553
|
+
end
|
554
|
+
|
555
|
+
# Call event handler
|
360
556
|
@events[:controller_button_up].each do |id, e|
|
361
557
|
e.call(ControllerButtonEvent.new(which, button))
|
362
558
|
end
|
@@ -365,8 +561,17 @@ module Ruby2D
|
|
365
561
|
|
366
562
|
# Update callback method, called by the native and web extentions
|
367
563
|
def update_callback
|
564
|
+
unless @using_dsl
|
565
|
+
update
|
566
|
+
end
|
567
|
+
|
368
568
|
@update_proc.call
|
369
569
|
|
570
|
+
# Run update method on all entities
|
571
|
+
@entities.each do |e|
|
572
|
+
e.update
|
573
|
+
end
|
574
|
+
|
370
575
|
# Accept and eval commands if in console mode
|
371
576
|
if @console
|
372
577
|
if STDIN.ready?
|
@@ -385,10 +590,42 @@ module Ruby2D
|
|
385
590
|
end
|
386
591
|
end
|
387
592
|
|
593
|
+
# Clear inputs if using class pattern
|
594
|
+
unless @using_dsl
|
595
|
+
@keys_down.clear
|
596
|
+
@keys_held.clear
|
597
|
+
@keys_up.clear
|
598
|
+
@mouse_buttons_down.clear
|
599
|
+
@mouse_buttons_up.clear
|
600
|
+
@mouse_scroll_event = false
|
601
|
+
@mouse_move_event = false
|
602
|
+
@controller_axes_moved.clear
|
603
|
+
@controller_buttons_down.clear
|
604
|
+
@controller_buttons_up.clear
|
605
|
+
end
|
606
|
+
end
|
607
|
+
|
608
|
+
# Render callback method, called by the native and web extentions
|
609
|
+
def render_callback
|
610
|
+
unless @using_dsl
|
611
|
+
render
|
612
|
+
end
|
613
|
+
|
614
|
+
@render_proc.call
|
615
|
+
|
616
|
+
# Run render method on all entities
|
617
|
+
@entities.each do |e|
|
618
|
+
e.render
|
619
|
+
end
|
388
620
|
end
|
389
621
|
|
390
622
|
# Show the window
|
391
623
|
def show
|
624
|
+
if @@open_window
|
625
|
+
raise Error, "Window#show called multiple times, Ruby2D only supports a single open window"
|
626
|
+
end
|
627
|
+
|
628
|
+
@@open_window = true
|
392
629
|
ext_show
|
393
630
|
end
|
394
631
|
|