Class: Ruby2D::Window
- Inherits:
-
Object
- Object
- Ruby2D::Window
- Defined in:
- lib/ruby2d/window.rb,
ext/ruby2d/ruby2d-opal.rb
Defined Under Namespace
Classes: ControllerAxisEvent, ControllerButtonEvent, ControllerEvent, EventDescriptor, KeyEvent, MouseEvent
Instance Attribute Summary collapse
-
#fps ⇒ Object
Returns the value of attribute fps.
-
#frames ⇒ Object
Returns the value of attribute frames.
-
#mouse_x ⇒ Object
Returns the value of attribute mouse_x.
-
#mouse_y ⇒ Object
Returns the value of attribute mouse_y.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
Instance Method Summary collapse
- #add(o) ⇒ Object
- #clear ⇒ Object
- #close ⇒ Object
- #controller_callback(which, type, axis, value, button) ⇒ Object
- #ext_show ⇒ Object
- #get(sym) ⇒ Object
-
#initialize(args = {}) ⇒ Window
constructor
A new instance of Window.
- #key_callback(type, key) ⇒ Object
- #mouse_callback(type, button, direction, x, y, delta_x, delta_y) ⇒ Object
- #new_event_key ⇒ Object
- #off(event_descriptor) ⇒ Object
- #on(event, &proc) ⇒ Object
- #remove(o) ⇒ Object
- #set(opts) ⇒ Object
- #show ⇒ Object
- #update(&proc) ⇒ Object
- #update_callback ⇒ Object
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 |
# File 'lib/ruby2d/window.rb', line 16 def initialize(args = {}) @title = args[:title] || "Ruby 2D" @background = Color.new([0.0, 0.0, 0.0, 1.0]) @width = args[:width] || 640 @height = args[:height] || 480 @viewport_width, @viewport_height = nil, nil @display_width, @display_height = nil, nil @resizable = false @borderless = false @fullscreen = false @highdpi = false @frames = 0 @fps_cap = args[:fps] || 60 @fps = @fps_cap @vsync = args[:vsync] || true @mouse_x, @mouse_y = 0, 0 @objects = [] @event_key = 0 @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: {} } @update_proc = Proc.new {} @diagnostics = false @controller_mappings_path = File.join(Dir.home, ".ruby2d", "controllers.txt") end |
Instance Attribute Details
#fps ⇒ Object
Returns the value of attribute fps.
7 8 9 |
# File 'lib/ruby2d/window.rb', line 7 def fps @fps end |
#frames ⇒ Object
Returns the value of attribute frames.
7 8 9 |
# File 'lib/ruby2d/window.rb', line 7 def frames @frames end |
#mouse_x ⇒ Object
Returns the value of attribute mouse_x.
7 8 9 |
# File 'lib/ruby2d/window.rb', line 7 def mouse_x @mouse_x end |
#mouse_y ⇒ Object
Returns the value of attribute mouse_y.
7 8 9 |
# File 'lib/ruby2d/window.rb', line 7 def mouse_y @mouse_y end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
6 7 8 |
# File 'lib/ruby2d/window.rb', line 6 def objects @objects end |
Instance Method Details
#add(o) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ruby2d/window.rb', line 103 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 |
#close ⇒ Object
241 242 243 |
# File 'lib/ruby2d/window.rb', line 241 def close ext_close end |
#controller_callback(which, type, axis, value, button) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/ruby2d/window.rb', line 208 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 |
#ext_show ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'ext/ruby2d/ruby2d-opal.rb', line 263 def ext_show $R2D_WINDOW = self ` var width = #{$R2D_WINDOW.get(:width)}; var height = #{$R2D_WINDOW.get(:height)}; var vp_w = #{$R2D_WINDOW.get(:viewport_width)}; var viewport_width = vp_w != Opal.nil ? vp_w : width; var vp_h = #{$R2D_WINDOW.get(:viewport_height)}; var viewport_height = vp_h != Opal.nil ? vp_h : height; win = S2D.CreateWindow( #{$R2D_WINDOW.get(:title)}, width, height, update, render, "ruby2d-app", {} ); win.viewport.width = viewport_width; win.viewport.height = viewport_height; win.on_key = on_key; win.on_mouse = on_mouse; S2D.Show(win); ` end |
#get(sym) ⇒ Object
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 |
# File 'lib/ruby2d/window.rb', line 58 def get(sym) 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 :mouse_x; @mouse_x when :mouse_y; @mouse_y when :diagnostics; @diagnostics end end |
#key_callback(type, key) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ruby2d/window.rb', line 149 def key_callback(type, key) # puts "===", "type: #{type}", "key: #{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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/ruby2d/window.rb', line 178 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
54 55 56 |
# File 'lib/ruby2d/window.rb', line 54 def new_event_key @event_key = @event_key.next end |
#off(event_descriptor) ⇒ Object
145 146 147 |
# File 'lib/ruby2d/window.rb', line 145 def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end |
#on(event, &proc) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/ruby2d/window.rb', line 136 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
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ruby2d/window.rb', line 114 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 |
#set(opts) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby2d/window.rb', line 86 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 @width = opts[:width] || @width @height = opts[:height] || @height @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 @diagnostics = opts[:diagnostics] || @diagnostics end |
#show ⇒ Object
237 238 239 |
# File 'lib/ruby2d/window.rb', line 237 def show ext_show end |
#update(&proc) ⇒ Object
131 132 133 134 |
# File 'lib/ruby2d/window.rb', line 131 def update(&proc) @update_proc = proc true end |
#update_callback ⇒ Object
233 234 235 |
# File 'lib/ruby2d/window.rb', line 233 def update_callback @update_proc.call end |