Class: AuthorEngine::TouchButton
- Inherits:
-
Object
- Object
- AuthorEngine::TouchButton
- Defined in:
- lib/author_engine/game/opal/touch_button.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #active ⇒ Object
- #draw ⇒ Object
- #inactive ⇒ Object
-
#initialize(label:, color:, x:, y: nil, width:, height:, side:, for_key: nil, &block) ⇒ TouchButton
constructor
A new instance of TouchButton.
- #trigger?(touches) ⇒ Boolean
Constructor Details
#initialize(label:, color:, x:, y: nil, width:, height:, side:, for_key: nil, &block) ⇒ TouchButton
Returns a new instance of TouchButton.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 4 def initialize(label:, color:, x:, y: nil, width:, height:, side:, for_key: nil, &block) @label, @color, @x, @y, @width, @height = label, color, x, y, width, height @side, @for_key = side, for_key @block = block @buttons = AuthorEngine::Part::OpalInput::BUTTONS @key_states = AuthorEngine::Part::OpalInput::KEY_STATES @game = AuthorEngine::GameRunner.instance.game @game_width = 128 * @game. @game_x = `window.innerWidth/2 - #{@game_width/2}` if @side == :left @x = @game_x-@x elsif @side == :right @x = @game_x+@game_width+@x else raise "side must be :left or :right" end @y = `window.innerHeight/2 - #{height/2}` unless @y.is_a?(Numeric) end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 3 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 3 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 3 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 3 def y @y end |
Instance Method Details
#active ⇒ Object
58 59 60 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 58 def active @key_states[@buttons[@for_key]] = true end |
#draw ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 27 def draw `#{@game.}.fillStyle = #{@color}` `#{@game.}.fillRect(#{@x}, #{@y}, #{@width}, #{@height})` font = "#{@height}px Connection, Consolas" `#{@game.}.font = #{font}` `#{@game.}.fillStyle = "white"` `#{@game.}.textBaseline = "top"` `#{@game.}.fillText(#{@label}, #{@x}, #{@y}, #{@width})` end |
#inactive ⇒ Object
62 63 64 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 62 def inactive @key_states[@buttons[@for_key]] = false end |
#trigger?(touches) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/author_engine/game/opal/touch_button.rb', line 38 def trigger?(touches) triggered = false touches.detect do |id, touch| if touch.x.between?(@x, @x+@width) && touch.y.between?(@y, @y+@height) triggered = true end end if @for_key active if triggered inactive unless triggered else @block.call if @block && triggered end return triggered end |