Class: Ruby2D::Window
- Inherits:
-
Object
- Object
- Ruby2D::Window
- Defined in:
- lib/ruby2d/window.rb,
ext/ruby2d/ruby2d-opal.rb
Defined Under Namespace
Classes: 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_window_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.
14 15 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 |
# File 'lib/ruby2d/window.rb', line 14 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 @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 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
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby2d/window.rb', line 92 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
234 235 236 |
# File 'lib/ruby2d/window.rb', line 234 def close ext_window_close end |
#controller_callback(which, type, axis, value, button) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/ruby2d/window.rb', line 201 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(ControllerEvent.new(which, type, axis, value, nil)) end # When controller button is pressed when :button_down @events[:controller_button_down].each do |id, e| e.call(ControllerEvent.new(which, type, nil, nil, )) end # When controller button is released when :button_up @events[:controller_button_up].each do |id, e| e.call(ControllerEvent.new(which, type, nil, nil, )) end end end |
#ext_window_show ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'ext/ruby2d/ruby2d-opal.rb', line 258 def ext_window_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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby2d/window.rb', line 54 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 :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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ruby2d/window.rb', line 138 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
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 193 194 195 196 197 198 199 |
# File 'lib/ruby2d/window.rb', line 167 def mouse_callback(type, , direction, x, y, delta_x, delta_y) # Convert to symbols (see MRuby bug in native extension) = .to_sym unless == nil direction = direction.to_sym unless direction == nil # 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
50 51 52 |
# File 'lib/ruby2d/window.rb', line 50 def new_event_key @event_key = @event_key.next end |
#off(event_descriptor) ⇒ Object
134 135 136 |
# File 'lib/ruby2d/window.rb', line 134 def off(event_descriptor) @events[event_descriptor.type].delete(event_descriptor.id) end |
#on(event, &proc) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/ruby2d/window.rb', line 125 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
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby2d/window.rb', line 103 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ruby2d/window.rb', line 75 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
230 231 232 |
# File 'lib/ruby2d/window.rb', line 230 def show ext_window_show end |
#update(&proc) ⇒ Object
120 121 122 123 |
# File 'lib/ruby2d/window.rb', line 120 def update(&proc) @update_proc = proc true end |
#update_callback ⇒ Object
226 227 228 |
# File 'lib/ruby2d/window.rb', line 226 def update_callback @update_proc.call end |