Class: Dare::Window
- Inherits:
-
Object
- Object
- Dare::Window
- Defined in:
- lib/dare/window.rb
Instance Attribute Summary collapse
-
#canvas ⇒ Object
readonly
Returns the value of attribute canvas.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#mouse_x ⇒ Object
readonly
Returns the value of attribute mouse_x.
-
#mouse_y ⇒ Object
readonly
Returns the value of attribute mouse_y.
-
#ticks ⇒ Object
readonly
Returns the value of attribute ticks.
-
#update_interval ⇒ Object
readonly
Returns the value of attribute update_interval.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#add_keyboard_event_listeners ⇒ Object
adds keyboard event listeners to entire page.
-
#add_mouse_event_listener ⇒ Object
adds mousemove event listener to main canvas.
-
#button_down?(button) ⇒ Boolean
checks to see if button passed is currently being pressed.
-
#caption=(title) ⇒ Object
(also: #title=)
sets the caption/title of the window to the string passed.
-
#draw ⇒ Object
gets run every frame of animation override this in your subclass of Dare::Window.
-
#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z) ⇒ Object
works the same as Gosu::Window.draw_quad.
- #draw_rect(opts = {}) ⇒ Object
-
#fullscreen? ⇒ Boolean
checks if game is fullscreen.
-
#get_cursor_position(event) ⇒ Object
sets mouse_x and mouse_y to current mouse positions relative to the main canvas.
-
#get_key_id(event) ⇒ Object
retrieves key code of current pressed key for keydown or keyup event.
-
#initialize(opts = {}) ⇒ Window
constructor
Creates a new window object to hold all your game goodness.
-
#run! ⇒ Object
starts the game loop for the window.
-
#text_input ⇒ Object
this is here for Gosu API compatability.
-
#update ⇒ Object
gets run every update_interval if it can override this in your subclass of Dare::Window.
Constructor Details
#initialize(opts = {}) ⇒ Window
Creates a new window object to hold all your game goodness
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dare/window.rb', line 21 def initialize(opts = {}) opts[:width] ||= 640 opts[:height] ||= 480 opts[:update_interval] ||= 16.666666 opts[:border] ||= false opts[:canvas] ||= Canvas.new(width: opts[:width], height: opts[:height], border: opts[:border]) opts[:mouse] ||= true @width = opts[:width] @height = opts[:height] @update_interval = opts[:update_interval] @clock = opts[:clock] @canvas = opts[:canvas] Dare.default_canvas ||= @canvas @keys = [] add_mouse_event_listener if opts[:mouse] add_keyboard_event_listeners end |
Instance Attribute Details
#canvas ⇒ Object (readonly)
Returns the value of attribute canvas.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def canvas @canvas end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def height @height end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def key @key end |
#mouse_x ⇒ Object (readonly)
Returns the value of attribute mouse_x.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def mouse_x @mouse_x end |
#mouse_y ⇒ Object (readonly)
Returns the value of attribute mouse_y.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def mouse_y @mouse_y end |
#ticks ⇒ Object (readonly)
Returns the value of attribute ticks.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def ticks @ticks end |
#update_interval ⇒ Object (readonly)
Returns the value of attribute update_interval.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def update_interval @update_interval end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
10 11 12 |
# File 'lib/dare/window.rb', line 10 def width @width end |
Instance Method Details
#add_keyboard_event_listeners ⇒ Object
adds keyboard event listeners to entire page
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dare/window.rb', line 77 def add_keyboard_event_listeners Element.find("html").on :keydown do |event| @keys[get_key_id(event)] = true end Element.find("html").on :keyup do |event| @keys[get_key_id(event)] = false end ::Window.on :blur do |event| @keys.fill false end end |
#add_mouse_event_listener ⇒ Object
adds mousemove event listener to main canvas
68 69 70 71 72 73 74 |
# File 'lib/dare/window.rb', line 68 def add_mouse_event_listener Element.find("##{@canvas.id}").on :mousemove do |event| coords = get_cursor_position(event) @mouse_x = coords.x[:x] @mouse_y = coords.x[:y] end end |
#button_down?(button) ⇒ Boolean
checks to see if button passed is currently being pressed
90 91 92 |
# File 'lib/dare/window.rb', line 90 def () @keys[] end |
#caption=(title) ⇒ Object Also known as: title=
sets the caption/title of the window to the string passed
162 163 164 |
# File 'lib/dare/window.rb', line 162 def (title) `document.getElementById('pageTitle').innerHTML = #{title}` end |
#draw ⇒ Object
gets run every frame of animation override this in your subclass of Dare::Window
57 58 59 |
# File 'lib/dare/window.rb', line 57 def draw end |
#draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z) ⇒ Object
works the same as Gosu::Window.draw_quad
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/dare/window.rb', line 131 def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z) %x{ var QUALITY = 256; var canvas_colors = document.createElement( 'canvas' ); canvas_colors.width = 2; canvas_colors.height = 2; var context_colors = canvas_colors.getContext( '2d' ); context_colors.fillStyle = 'rgba(0,0,0,1)'; context_colors.fillRect( 0, 0, 2, 2 ); var image_colors = context_colors.getImageData( 0, 0, 2, 2 ); var data = image_colors.data; var canvas_render = #{@canvas}; var context_render = #{@canvas.context}; context_render.translate( - QUALITY / 2, - QUALITY / 2 ); context_render.scale( QUALITY, QUALITY ); data[ 0 ] = 255; // Top-left, red component data[ 5 ] = 255; // Top-right, green component data[ 10 ] = 255; // Bottom-left, blue component context_colors.putImageData( image_colors, 0, 0 ); context_render.drawImage( canvas_colors, 0, 0 ); } end |
#draw_rect(opts = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dare/window.rb', line 119 def draw_rect(opts = {}) x = opts[:top_left][0] y = opts[:top_left][1] width = opts[:width] height = opts[:height] color = opts[:color] `#{@canvas.context}.fillStyle = #{color}` `#{@canvas.context}.fillRect(#{x}, #{y}, #{width}, #{height})` end |
#fullscreen? ⇒ Boolean
checks if game is fullscreen. currently not implemented.
169 170 171 |
# File 'lib/dare/window.rb', line 169 def fullscreen? false end |
#get_cursor_position(event) ⇒ Object
sets mouse_x and mouse_y to current mouse positions relative to the main canvas
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dare/window.rb', line 96 def get_cursor_position(event) if (event.page_x && event.page_y) x = event.page_x y = event.page_y else doc = Opal.Document[0] x = event[:clientX] + doc.scrollLeft + doc.documentElement.scrollLeft y = event[:clientY] + doc.body.scrollTop + doc.documentElement.scrollTop end x -= `#{@canvas.canvas}.offsetLeft` y -= `#{@canvas.canvas}.offsetTop` Coordinates.new(x: x, y: y) end |
#get_key_id(event) ⇒ Object
retrieves key code of current pressed key for keydown or keyup event
113 114 115 |
# File 'lib/dare/window.rb', line 113 def get_key_id(event) event[:keyCode] end |
#run! ⇒ Object
starts the game loop for the window.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dare/window.rb', line 40 def run! %x{ function update_loop() { #{update}; } function anim_loop() { requestAnimationFrame(anim_loop); #{@canvas.canvas}.width = #{@canvas.canvas}.width; #{draw}; } setInterval(update_loop, #{@update_interval}); requestAnimationFrame(anim_loop); } end |
#text_input ⇒ Object
this is here for Gosu API compatability
174 |
# File 'lib/dare/window.rb', line 174 def text_input; end |
#update ⇒ Object
gets run every update_interval if it can override this in your subclass of Dare::Window
63 64 65 |
# File 'lib/dare/window.rb', line 63 def update end |