Class: Ruby2D::Window
- Inherits:
-
Object
- Object
- Ruby2D::Window
- Defined in:
- lib/ruby2d/window.rb
Defined Under Namespace
Classes: ControllerAxisEvent, ControllerButtonEvent, ControllerEvent, EventDescriptor, KeyEvent, MouseEvent
Class Method Summary collapse
- .add(object) ⇒ Object
- .background ⇒ Object
- .borderless ⇒ Object
- .clear ⇒ Object
- .close ⇒ Object
- .current ⇒ Object
- .diagnostics ⇒ Object
- .display_height ⇒ Object
- .display_width ⇒ Object
- .fps ⇒ Object
- .fps_cap ⇒ Object
- .frames ⇒ Object
- .fullscreen ⇒ Object
- .get(sym, opts = nil) ⇒ Object
- .height ⇒ Object
- .highdpi ⇒ Object
- .mouse_x ⇒ Object
- .mouse_y ⇒ Object
- .off(event_descriptor) ⇒ Object
- .on(event, &proc) ⇒ Object
- .opened? ⇒ Boolean
- .remove(object) ⇒ Object
- .render(&proc) ⇒ Object
- .render_ready_check ⇒ Object
- .resizable ⇒ Object
- .screenshot(opts = nil) ⇒ Object
- .set(opts) ⇒ Object
- .show ⇒ Object
- .title ⇒ Object
- .update(&proc) ⇒ Object
- .viewport_height ⇒ Object
- .viewport_width ⇒ Object
- .width ⇒ Object
Instance Method Summary collapse
-
#add(object) ⇒ Object
Add an object to the window.
-
#add_controller_mappings ⇒ Object
Add controller mappings from file.
-
#clear ⇒ Object
Clear all objects from the window.
-
#close ⇒ Object
Close the window.
-
#controller_axis(axis) ⇒ Object
Controller axis event method for class pattern.
-
#controller_button_down(btn) ⇒ Object
Controller button down event method for class pattern.
-
#controller_button_up(btn) ⇒ Object
Controller button up event method for class pattern.
-
#controller_callback(which, type, axis, value, button) ⇒ Object
Controller callback method, called by the native and web extentions.
-
#get(sym, opts = nil) ⇒ Object
Retrieve an attribute of the window.
-
#initialize(title: 'Ruby 2D', width: 640, height: 480, fps_cap: 60, vsync: true) ⇒ Window
constructor
Create a Window.
-
#key_callback(type, key) ⇒ Object
Key callback method, called by the native and web extentions.
-
#key_down(key) ⇒ Object
Key down event method for class pattern.
-
#key_held(key) ⇒ Object
Key held event method for class pattern.
-
#key_up(key) ⇒ Object
Key up event method for class pattern.
-
#mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object
Mouse callback method, called by the native and web extentions.
-
#mouse_down(btn) ⇒ Object
Mouse down event method for class pattern.
-
#mouse_move ⇒ Object
Mouse move event method for class pattern.
-
#mouse_scroll ⇒ Object
Mouse scroll event method for class pattern.
-
#mouse_up(btn) ⇒ Object
Mouse up event method for class pattern.
-
#new_event_key ⇒ Object
Generate a new event key (ID).
-
#off(event_descriptor) ⇒ Object
Remove an event handler.
-
#on(event, &proc) ⇒ Object
Set an event handler.
-
#remove(object) ⇒ Object
Remove an object from the window.
-
#render(&proc) ⇒ Object
Set the render callback.
-
#render_callback ⇒ Object
Render callback method, called by the native and web extentions.
-
#screenshot(path) ⇒ Object
Take screenshot.
-
#set(opts) ⇒ Object
Set a window attribute.
-
#show ⇒ Object
Show the window.
-
#update(&proc) ⇒ Object
Set the update callback.
-
#update_callback ⇒ Object
Update callback method, called by the native and web extentions.
Constructor Details
#initialize(title: 'Ruby 2D', width: 640, height: 480, fps_cap: 60, vsync: true) ⇒ Window
Create a Window
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 |
# File 'lib/ruby2d/window.rb', line 27 def initialize(title: 'Ruby 2D', width: 640, height: 480, fps_cap: 60, vsync: true) # Title of the window @title = title # Window size @width = width @height = height # Frames per second upper limit, and the actual FPS @fps_cap = fps_cap @fps = @fps_cap # Vertical synchronization, set to prevent screen tearing (recommended) @vsync = vsync # Total number of frames that have been rendered @frames = 0 # Renderable objects currently in the window, like a linear scene graph @objects = [] _init_window_defaults _init_event_stores _init_event_registrations _init_procs_dsl_console end |
Class Method Details
.add(object) ⇒ Object
155 156 157 |
# File 'lib/ruby2d/window.rb', line 155 def add(object) DSL.window.add(object) end |
.background ⇒ Object
67 68 69 |
# File 'lib/ruby2d/window.rb', line 67 def background get(:background) end |
.borderless ⇒ Object
99 100 101 |
# File 'lib/ruby2d/window.rb', line 99 def borderless get(:borderless) end |
.current ⇒ Object
59 60 61 |
# File 'lib/ruby2d/window.rb', line 59 def current get(:window) end |
.diagnostics ⇒ Object
131 132 133 |
# File 'lib/ruby2d/window.rb', line 131 def diagnostics get(:diagnostics) end |
.display_height ⇒ Object
91 92 93 |
# File 'lib/ruby2d/window.rb', line 91 def display_height get(:display_height) end |
.display_width ⇒ Object
87 88 89 |
# File 'lib/ruby2d/window.rb', line 87 def display_width get(:display_width) end |
.fps ⇒ Object
115 116 117 |
# File 'lib/ruby2d/window.rb', line 115 def fps get(:fps) end |
.fps_cap ⇒ Object
119 120 121 |
# File 'lib/ruby2d/window.rb', line 119 def fps_cap get(:fps_cap) end |
.frames ⇒ Object
111 112 113 |
# File 'lib/ruby2d/window.rb', line 111 def frames get(:frames) end |
.fullscreen ⇒ Object
103 104 105 |
# File 'lib/ruby2d/window.rb', line 103 def fullscreen get(:fullscreen) end |
.get(sym, opts = nil) ⇒ Object
139 140 141 |
# File 'lib/ruby2d/window.rb', line 139 def get(sym, opts = nil) DSL.window.get(sym, opts) end |
.highdpi ⇒ Object
107 108 109 |
# File 'lib/ruby2d/window.rb', line 107 def highdpi get(:highdpi) end |
.mouse_x ⇒ Object
123 124 125 |
# File 'lib/ruby2d/window.rb', line 123 def mouse_x get(:mouse_x) end |
.mouse_y ⇒ Object
127 128 129 |
# File 'lib/ruby2d/window.rb', line 127 def mouse_y get(:mouse_y) end |
.off(event_descriptor) ⇒ Object
151 152 153 |
# File 'lib/ruby2d/window.rb', line 151 def off(event_descriptor) DSL.window.off(event_descriptor) end |
.on(event, &proc) ⇒ Object
147 148 149 |
# File 'lib/ruby2d/window.rb', line 147 def on(event, &proc) DSL.window.on(event, &proc) end |
.opened? ⇒ Boolean
190 191 192 |
# File 'lib/ruby2d/window.rb', line 190 def opened? @open_window end |
.remove(object) ⇒ Object
159 160 161 |
# File 'lib/ruby2d/window.rb', line 159 def remove(object) DSL.window.remove(object) end |
.render(&proc) ⇒ Object
171 172 173 |
# File 'lib/ruby2d/window.rb', line 171 def render(&proc) DSL.window.render(&proc) end |
.render_ready_check ⇒ Object
183 184 185 186 187 188 |
# File 'lib/ruby2d/window.rb', line 183 def render_ready_check return if opened? raise Error, 'Attempting to draw before the window is ready. Please put calls to draw() inside of a render block.' end |
.resizable ⇒ Object
95 96 97 |
# File 'lib/ruby2d/window.rb', line 95 def resizable get(:resizable) end |
.screenshot(opts = nil) ⇒ Object
135 136 137 |
# File 'lib/ruby2d/window.rb', line 135 def screenshot(opts = nil) get(:screenshot, opts) end |
.set(opts) ⇒ Object
143 144 145 |
# File 'lib/ruby2d/window.rb', line 143 def set(opts) DSL.window.set(opts) end |
.title ⇒ Object
63 64 65 |
# File 'lib/ruby2d/window.rb', line 63 def title get(:title) end |
.update(&proc) ⇒ Object
167 168 169 |
# File 'lib/ruby2d/window.rb', line 167 def update(&proc) DSL.window.update(&proc) end |
.viewport_height ⇒ Object
83 84 85 |
# File 'lib/ruby2d/window.rb', line 83 def get(:viewport_height) end |
.viewport_width ⇒ Object
79 80 81 |
# File 'lib/ruby2d/window.rb', line 79 def get(:viewport_width) end |
Instance Method Details
#add(object) ⇒ Object
Add an object to the window
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/ruby2d/window.rb', line 271 def add(object) case object when nil raise Error, "Cannot add '#{object.class}' to window!" when Array object.each { |x| add_object(x) } else add_object(object) end end |
#add_controller_mappings ⇒ Object
Add controller mappings from file
410 411 412 |
# File 'lib/ruby2d/window.rb', line 410 def add_controller_mappings ext_add_controller_mappings(@controller_mappings) if File.exist? @controller_mappings end |
#clear ⇒ Object
Clear all objects from the window
294 295 296 |
# File 'lib/ruby2d/window.rb', line 294 def clear @objects.clear end |
#close ⇒ Object
Close the window
492 493 494 |
# File 'lib/ruby2d/window.rb', line 492 def close ext_close end |
#controller_axis(axis) ⇒ Object
Controller axis event method for class pattern
415 416 417 |
# File 'lib/ruby2d/window.rb', line 415 def controller_axis(axis) @controller_axes_moved.include? axis end |
#controller_button_down(btn) ⇒ Object
Controller button down event method for class pattern
420 421 422 |
# File 'lib/ruby2d/window.rb', line 420 def (btn) @controller_buttons_down.include? btn end |
#controller_button_up(btn) ⇒ Object
Controller button up event method for class pattern
425 426 427 |
# File 'lib/ruby2d/window.rb', line 425 def (btn) @controller_buttons_up.include? btn end |
#controller_callback(which, type, axis, value, button) ⇒ Object
Controller callback method, called by the native and web extentions
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/ruby2d/window.rb', line 430 def controller_callback(which, type, axis, value, ) # All controller events @events[:controller].each do |_id, e| e.call(ControllerEvent.new(which, type, axis, value, )) end case type # When controller axis motion, like analog sticks when :axis _handle_controller_axis which, axis, value # When controller button is pressed when :button_down which, # When controller button is released when :button_up which, end end |
#get(sym, opts = nil) ⇒ Object
Retrieve an attribute of the window
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 236 237 238 239 |
# File 'lib/ruby2d/window.rb', line 211 def get(sym, opts = nil) case sym when :window then self when :title then @title when :background then @background when :width then @width when :height then @height when :viewport_width then @viewport_width when :viewport_height then @viewport_height when :display_width, :display_height ext_get_display_dimensions if sym == :display_width @display_width else @display_height end when :resizable then @resizable when :borderless then @borderless when :fullscreen then @fullscreen when :highdpi then @highdpi when :frames then @frames when :fps then @fps when :fps_cap then @fps_cap when :mouse_x then @mouse_x when :mouse_y then @mouse_y when :diagnostics then @diagnostics when :screenshot then screenshot(opts) end end |
#key_callback(type, key) ⇒ Object
Key callback method, called by the native and web extentions
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/ruby2d/window.rb', line 345 def key_callback(type, key) key = key.downcase # All key events @events[:key].each do |_id, e| e.call(KeyEvent.new(type, key)) end case type # When key is pressed, fired once when :down _handle_key_down type, key # When key is being held down, fired every frame when :held _handle_key_held type, key # When key released, fired once when :up _handle_key_up type, key end end |
#key_down(key) ⇒ Object
Key down event method for class pattern
330 331 332 |
# File 'lib/ruby2d/window.rb', line 330 def key_down(key) @keys_down.include? key end |
#key_held(key) ⇒ Object
Key held event method for class pattern
335 336 337 |
# File 'lib/ruby2d/window.rb', line 335 def key_held(key) @keys_held.include? key end |
#key_up(key) ⇒ Object
Key up event method for class pattern
340 341 342 |
# File 'lib/ruby2d/window.rb', line 340 def key_up(key) @keys_up.include? key end |
#mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object
Mouse callback method, called by the native and web extentions
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/ruby2d/window.rb', line 387 def mouse_callback(type, , direction, x, y, delta_x, delta_y) # All mouse events @events[:mouse].each do |_id, e| e.call(MouseEvent.new(type, , direction, x, y, delta_x, delta_y)) end case type # When mouse button pressed when :down _handle_mouse_down type, , x, y # When mouse button released when :up _handle_mouse_up type, , x, y # When mouse motion / movement when :scroll _handle_mouse_scroll type, direction, delta_x, delta_y # When mouse scrolling, wheel or trackpad when :move _handle_mouse_move type, x, y, delta_x, delta_y end end |
#mouse_down(btn) ⇒ Object
Mouse down event method for class pattern
367 368 369 |
# File 'lib/ruby2d/window.rb', line 367 def mouse_down(btn) @mouse_buttons_down.include? btn end |
#mouse_move ⇒ Object
Mouse move event method for class pattern
382 383 384 |
# File 'lib/ruby2d/window.rb', line 382 def mouse_move @mouse_move_event end |
#mouse_scroll ⇒ Object
Mouse scroll event method for class pattern
377 378 379 |
# File 'lib/ruby2d/window.rb', line 377 def mouse_scroll @mouse_scroll_event end |
#mouse_up(btn) ⇒ Object
Mouse up event method for class pattern
372 373 374 |
# File 'lib/ruby2d/window.rb', line 372 def mouse_up(btn) @mouse_buttons_up.include? btn end |
#new_event_key ⇒ Object
Generate a new event key (ID)
311 312 313 |
# File 'lib/ruby2d/window.rb', line 311 def new_event_key @event_key = @event_key.next end |
#off(event_descriptor) ⇒ Object
Remove an event handler
325 326 327 |
# File 'lib/ruby2d/window.rb', line 325 def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end |
#on(event, &proc) ⇒ Object
Set an event handler
316 317 318 319 320 321 322 |
# File 'lib/ruby2d/window.rb', line 316 def on(event, &proc) raise Error, "`#{event}` is not a valid event type" unless @events.key? event event_id = new_event_key @events[event][event_id] = proc EventDescriptor.new(event, event_id) end |
#remove(object) ⇒ Object
Remove an object from the window
283 284 285 286 287 288 289 290 291 |
# File 'lib/ruby2d/window.rb', line 283 def remove(object) raise Error, "Cannot remove '#{object.class}' from window!" if object.nil? ix = @objects.index(object) return false if ix.nil? @objects.delete_at(ix) true end |
#render(&proc) ⇒ Object
Set the render callback
305 306 307 308 |
# File 'lib/ruby2d/window.rb', line 305 def render(&proc) @render_proc = proc true end |
#render_callback ⇒ Object
Render callback method, called by the native and web extentions
463 464 465 466 467 |
# File 'lib/ruby2d/window.rb', line 463 def render_callback render unless @using_dsl @render_proc.call end |
#screenshot(path) ⇒ Object
Take screenshot
478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/ruby2d/window.rb', line 478 def screenshot(path) if path ext_screenshot(path) else time = if RUBY_ENGINE == 'ruby' Time.now.utc.strftime '%Y-%m-%d--%H-%M-%S' else Time.now.utc.to_i end ext_screenshot("./screenshot-#{time}.png") end end |
#set(opts) ⇒ Object
Set a window attribute
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/ruby2d/window.rb', line 258 def set(opts) # Store new window attributes, or ignore if nil _set_any_window_properties opts _set_any_window_dimensions opts @fps_cap = opts[:fps_cap] if opts[:fps_cap] return if opts[:diagnostics].nil? @diagnostics = opts[:diagnostics] ext_diagnostics(@diagnostics) end |
#show ⇒ Object
Show the window
470 471 472 473 474 475 |
# File 'lib/ruby2d/window.rb', line 470 def show raise Error, 'Window#show called multiple times, Ruby2D only supports a single open window' if Window.opened? Window.send(:opened!) ext_show end |
#update(&proc) ⇒ Object
Set the update callback
299 300 301 302 |
# File 'lib/ruby2d/window.rb', line 299 def update(&proc) @update_proc = proc true end |
#update_callback ⇒ Object
Update callback method, called by the native and web extentions
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/ruby2d/window.rb', line 450 def update_callback update unless @using_dsl @update_proc.call # Accept and eval commands if in console mode _handle_console_input if @console && $stdin.ready? # Clear inputs if using class pattern _clear_event_stores unless @using_dsl end |