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(o) ⇒ 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
- .remove(o) ⇒ 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(o) ⇒ 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_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(args = {}) ⇒ Window
constructor
A new instance of Window.
-
#key_callback(type, key) ⇒ Object
Key callback method, called by the native and web extentions.
-
#mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object
Mouse callback method, called by the native and web extentions.
-
#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(o) ⇒ Object
Remove an object from the window.
-
#screenshot(path) ⇒ Object
Take screenshot.
-
#set(opts) ⇒ Object
Set a window attribute.
-
#show ⇒ Object
Show the window.
-
#update(&proc) ⇒ Object
Set an update callback.
-
#update_callback ⇒ Object
Update callback method, called by the native and web extentions.
Constructor Details
#initialize(args = {}) ⇒ Window
Returns a new instance of Window.
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 |
# File 'lib/ruby2d/window.rb', line 16 def initialize(args = {}) # This window instance, stored so it can be called by the class methods @@window = self # Title of the window @title = args[:title] || "Ruby 2D" # Window background color @background = Color.new([0.0, 0.0, 0.0, 1.0]) # Window icon @icon = nil # Window size and characteristics @width = args[:width] || 640 @height = args[:height] || 480 @resizable = false @borderless = false @fullscreen = false @highdpi = false # Size of the window's viewport (the drawable area) @viewport_width, @viewport_height = nil, nil # Size of the computer's display @display_width, @display_height = nil, nil # Total number of frames that have been rendered @frames = 0 # Frames per second upper limit, and the actual FPS @fps_cap = args[:fps_cap] || 60 @fps = @fps_cap # Vertical synchronization, set to prevent screen tearing (recommended) @vsync = args[:vsync] || true # Mouse X and Y position in the window @mouse_x, @mouse_y = 0, 0 # Controller axis and button mappings file @controller_mappings = File.('~') + "/.ruby2d/controllers.txt" # Renderable objects currently in the window, like a linear scene graph @objects = [] # Unique ID for the input event being registered @event_key = 0 # Registered input events @events = { key: {}, key_down: {}, key_held: {}, key_up: {}, mouse: {}, mouse_up: {}, mouse_down: {}, mouse_scroll: {}, mouse_move: {}, controller: {}, controller_axis: {}, controller_button_down: {}, controller_button_up: {} } # The window update block @update_proc = Proc.new {} # Whether diagnostic messages should be printed @diagnostics = false # Console mode, enabled at command line if RUBY_ENGINE == 'ruby' @console = defined?($ruby2d_console_mode) ? true : false else @console = false end end |
Class Method Details
.add(o) ⇒ Object
136 137 138 |
# File 'lib/ruby2d/window.rb', line 136 def add(o) @@window.add(o) end |
.background ⇒ Object
101 |
# File 'lib/ruby2d/window.rb', line 101 def background; get(:background) end |
.borderless ⇒ Object
109 |
# File 'lib/ruby2d/window.rb', line 109 def borderless; get(:borderless) end |
.current ⇒ Object
99 |
# File 'lib/ruby2d/window.rb', line 99 def current; get(:window) end |
.diagnostics ⇒ Object
117 |
# File 'lib/ruby2d/window.rb', line 117 def diagnostics; get(:diagnostics) end |
.display_height ⇒ Object
107 |
# File 'lib/ruby2d/window.rb', line 107 def display_height; get(:display_height) end |
.display_width ⇒ Object
106 |
# File 'lib/ruby2d/window.rb', line 106 def display_width; get(:display_width) end |
.fps ⇒ Object
113 |
# File 'lib/ruby2d/window.rb', line 113 def fps; get(:fps) end |
.fps_cap ⇒ Object
114 |
# File 'lib/ruby2d/window.rb', line 114 def fps_cap; get(:fps_cap) end |
.frames ⇒ Object
112 |
# File 'lib/ruby2d/window.rb', line 112 def frames; get(:frames) end |
.fullscreen ⇒ Object
110 |
# File 'lib/ruby2d/window.rb', line 110 def fullscreen; get(:fullscreen) end |
.get(sym, opts = nil) ⇒ Object
120 121 122 |
# File 'lib/ruby2d/window.rb', line 120 def get(sym, opts = nil) @@window.get(sym, opts) end |
.height ⇒ Object
103 |
# File 'lib/ruby2d/window.rb', line 103 def height; get(:height) end |
.highdpi ⇒ Object
111 |
# File 'lib/ruby2d/window.rb', line 111 def highdpi; get(:highdpi) end |
.mouse_x ⇒ Object
115 |
# File 'lib/ruby2d/window.rb', line 115 def mouse_x; get(:mouse_x) end |
.mouse_y ⇒ Object
116 |
# File 'lib/ruby2d/window.rb', line 116 def mouse_y; get(:mouse_y) end |
.off(event_descriptor) ⇒ Object
132 133 134 |
# File 'lib/ruby2d/window.rb', line 132 def off(event_descriptor) @@window.off(event_descriptor) end |
.on(event, &proc) ⇒ Object
128 129 130 |
# File 'lib/ruby2d/window.rb', line 128 def on(event, &proc) @@window.on(event, &proc) end |
.remove(o) ⇒ Object
140 141 142 |
# File 'lib/ruby2d/window.rb', line 140 def remove(o) @@window.remove(o) end |
.resizable ⇒ Object
108 |
# File 'lib/ruby2d/window.rb', line 108 def resizable; get(:resizable) end |
.screenshot(opts = nil) ⇒ Object
118 |
# File 'lib/ruby2d/window.rb', line 118 def screenshot(opts = nil); get(:screenshot, opts) end |
.set(opts) ⇒ Object
124 125 126 |
# File 'lib/ruby2d/window.rb', line 124 def set(opts) @@window.set(opts) end |
.title ⇒ Object
100 |
# File 'lib/ruby2d/window.rb', line 100 def title; get(:title) end |
.update(&proc) ⇒ Object
148 149 150 |
# File 'lib/ruby2d/window.rb', line 148 def update(&proc) @@window.update(&proc) end |
.viewport_height ⇒ Object
105 |
# File 'lib/ruby2d/window.rb', line 105 def ; get(:viewport_height) end |
.viewport_width ⇒ Object
104 |
# File 'lib/ruby2d/window.rb', line 104 def ; get(:viewport_width) end |
.width ⇒ Object
102 |
# File 'lib/ruby2d/window.rb', line 102 def width; get(:width) end |
Instance Method Details
#add(o) ⇒ Object
Add an object to the window
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ruby2d/window.rb', line 218 def add(o) case o when nil raise Error, "Cannot add '#{o.class}' to window!" when Array o.each { |x| add_object(x) } else add_object(o) end end |
#add_controller_mappings ⇒ Object
Add controller mappings from file
334 335 336 337 338 |
# File 'lib/ruby2d/window.rb', line 334 def add_controller_mappings if File.exist? @controller_mappings ext_add_controller_mappings(@controller_mappings) end end |
#clear ⇒ Object
Clear all objects from the window
244 245 246 |
# File 'lib/ruby2d/window.rb', line 244 def clear @objects.clear end |
#close ⇒ Object
Close the window
410 411 412 |
# File 'lib/ruby2d/window.rb', line 410 def close ext_close end |
#controller_callback(which, type, axis, value, button) ⇒ Object
Controller callback method, called by the native and web extentions
341 342 343 344 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 341 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 @events[:controller_axis].each do |id, e| e.call(ControllerAxisEvent.new(which, axis, value)) end # When controller button is pressed when :button_down @events[:controller_button_down].each do |id, e| e.call(ControllerButtonEvent.new(which, )) end # When controller button is released when :button_up @events[:controller_button_up].each do |id, e| e.call(ControllerButtonEvent.new(which, )) end end end |
#get(sym, opts = nil) ⇒ Object
Retrieve an attribute of the window
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ruby2d/window.rb', line 164 def get(sym, opts = nil) case sym when :window; self when :title; @title when :background; @background when :width; @width when :height; @height when :viewport_width; @viewport_width when :viewport_height; @viewport_height when :display_width, :display_height ext_get_display_dimensions if sym == :display_width @display_width else @display_height end when :resizable; @resizable when :borderless; @borderless when :fullscreen; @fullscreen when :highdpi; @highdpi when :frames; @frames when :fps; @fps when :fps_cap; @fps_cap when :mouse_x; @mouse_x when :mouse_y; @mouse_y when :diagnostics; @diagnostics when :screenshot; screenshot(opts) end end |
#key_callback(type, key) ⇒ Object
Key callback method, called by the native and web extentions
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/ruby2d/window.rb', line 275 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 @events[:key_down].each do |id, e| e.call(KeyEvent.new(type, key)) end # When key is being held down, fired every frame when :held @events[:key_held].each do |id, e| e.call(KeyEvent.new(type, key)) end # When key released, fired once when :up @events[:key_up].each do |id, e| e.call(KeyEvent.new(type, key)) end end end |
#mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object
Mouse callback method, called by the native and web extentions
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/ruby2d/window.rb', line 303 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 @events[:mouse_down].each do |id, e| e.call(MouseEvent.new(type, , nil, x, y, nil, nil)) end # When mouse button released when :up @events[:mouse_up].each do |id, e| e.call(MouseEvent.new(type, , nil, x, y, nil, nil)) end # When mouse motion / movement when :scroll @events[:mouse_scroll].each do |id, e| e.call(MouseEvent.new(type, nil, direction, nil, nil, delta_x, delta_y)) end # When mouse scrolling, wheel or trackpad when :move @events[:mouse_move].each do |id, e| e.call(MouseEvent.new(type, nil, nil, x, y, delta_x, delta_y)) end end end |
#new_event_key ⇒ Object
Generate a new event key (ID)
255 256 257 |
# File 'lib/ruby2d/window.rb', line 255 def new_event_key @event_key = @event_key.next end |
#off(event_descriptor) ⇒ Object
Remove an event handler
270 271 272 |
# File 'lib/ruby2d/window.rb', line 270 def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end |
#on(event, &proc) ⇒ Object
Set an event handler
260 261 262 263 264 265 266 267 |
# File 'lib/ruby2d/window.rb', line 260 def on(event, &proc) unless @events.has_key? event raise Error, "`#{event}` is not a valid event type" end event_id = new_event_key @events[event][event_id] = proc EventDescriptor.new(event, event_id) end |
#remove(o) ⇒ Object
Remove an object from the window
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/ruby2d/window.rb', line 230 def remove(o) if o == nil raise Error, "Cannot remove '#{o.class}' from window!" end if i = @objects.index(o) @objects.delete_at(i) true else false end end |
#screenshot(path) ⇒ Object
Take screenshot
396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/ruby2d/window.rb', line 396 def screenshot(path) if path ext_screenshot(path) else if RUBY_ENGINE == 'ruby' time = Time.now.utc.strftime '%Y-%m-%d--%H-%M-%S' else time = Time.now.utc.to_i end ext_screenshot("./screenshot-#{time}.png") end end |
#set(opts) ⇒ Object
Set a window attribute
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ruby2d/window.rb', line 195 def set(opts) # Store new window attributes, or ignore if nil @title = opts[:title] || @title if Color.is_valid? opts[:background] @background = Color.new(opts[:background]) end @icon = opts[:icon] || @icon @width = opts[:width] || @width @height = opts[:height] || @height @fps_cap = opts[:fps_cap] || @fps_cap @viewport_width = opts[:viewport_width] || @viewport_width @viewport_height = opts[:viewport_height] || @viewport_height @resizable = opts[:resizable] || @resizable @borderless = opts[:borderless] || @borderless @fullscreen = opts[:fullscreen] || @fullscreen @highdpi = opts[:highdpi] || @highdpi unless opts[:diagnostics].nil? @diagnostics = opts[:diagnostics] ext_diagnostics(@diagnostics) end end |
#show ⇒ Object
Show the window
391 392 393 |
# File 'lib/ruby2d/window.rb', line 391 def show ext_show end |
#update(&proc) ⇒ Object
Set an update callback
249 250 251 252 |
# File 'lib/ruby2d/window.rb', line 249 def update(&proc) @update_proc = proc true end |
#update_callback ⇒ Object
Update callback method, called by the native and web extentions
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/ruby2d/window.rb', line 367 def update_callback @update_proc.call # Accept and eval commands if in console mode if @console if STDIN.ready? cmd = STDIN.gets begin res = eval(cmd, TOPLEVEL_BINDING) STDOUT.puts "=> #{res.inspect}" STDOUT.flush rescue SyntaxError => se STDOUT.puts se STDOUT.flush rescue Exception => e STDOUT.puts e STDOUT.flush end end end end |