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
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_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 options include: :width # (default 640) sets default canvas to a particular width in pixels :height # (default (480) sets default canvas to a particular height in pixels :update_interval # (default 16.666666) sets the update interval in milliseconds between updates :border # true/false (default false) draws a border around the default canvas :canvas # a canvas to refer to when drawing. Just let the default do its thing :mouse # true/false (default true) turn off mouse event listeners by setting to false.
-
#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 options include:
:width # (default 640) sets default canvas to a particular width in pixels
:height # (default (480) sets default canvas to a particular height in pixels
:update_interval # (default 16.666666) sets the update interval in milliseconds between updates
:border # true/false (default false) draws a border around the default canvas
:canvas # a canvas to refer to when drawing. Just let the default do its thing
:mouse # true/false (default true) turn off mouse event listeners by setting to false
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dare/window.rb', line 15 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.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def canvas @canvas end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def height @height end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def key @key end |
#mouse_x ⇒ Object (readonly)
Returns the value of attribute mouse_x.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def mouse_x @mouse_x end |
#mouse_y ⇒ Object (readonly)
Returns the value of attribute mouse_y.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def mouse_y @mouse_y end |
#ticks ⇒ Object (readonly)
Returns the value of attribute ticks.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def ticks @ticks end |
#update_interval ⇒ Object (readonly)
Returns the value of attribute update_interval.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def update_interval @update_interval end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/dare/window.rb', line 4 def width @width end |
Instance Method Details
#add_keyboard_event_listeners ⇒ Object
adds keyboard event listeners to entire page
68 69 70 71 72 73 74 75 |
# File 'lib/dare/window.rb', line 68 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 end |
#add_mouse_event_listener ⇒ Object
adds mousemove event listener to main canvas
59 60 61 62 63 64 65 |
# File 'lib/dare/window.rb', line 59 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
78 79 80 |
# File 'lib/dare/window.rb', line 78 def () @keys[] end |
#caption(title) ⇒ Object
sets the caption/title of the window to the string passed
119 120 121 |
# File 'lib/dare/window.rb', line 119 def caption(title) `document.getElementById('pageTitle').innerHTML = #{title}` end |
#draw ⇒ Object
gets run every frame of animation override this in your subclass of Dare::Window
48 49 50 |
# File 'lib/dare/window.rb', line 48 def draw end |
#draw_rect(opts = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/dare/window.rb', line 107 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.
124 125 126 |
# File 'lib/dare/window.rb', line 124 def fullscreen? false end |
#get_cursor_position(event) ⇒ Object
sets mouse_x and mouse_y to current mouse positions relative to the main canvas
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dare/window.rb', line 84 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
101 102 103 |
# File 'lib/dare/window.rb', line 101 def get_key_id(event) event[:keyCode] end |
#run! ⇒ Object
starts the game loop for the window.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dare/window.rb', line 34 def run! %x{ function anim_loop() { requestAnimationFrame(anim_loop); #{update}; #{@canvas.context}.clearRect(0, 0, #{width}, #{height}); #{draw}; } requestAnimationFrame(anim_loop); } end |
#text_input ⇒ Object
this is here for Gosu API compatability
129 |
# File 'lib/dare/window.rb', line 129 def text_input; end |
#update ⇒ Object
gets run every update_interval if it can override this in your subclass of Dare::Window
54 55 56 |
# File 'lib/dare/window.rb', line 54 def update end |