Class: AuthorEngine::Button
- Inherits:
-
Object
- Object
- AuthorEngine::Button
- Includes:
- Support
- Defined in:
- lib/author_engine/button.rb
Constant Summary collapse
- PADDING =
2
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#image ⇒ Object
Returns the value of attribute image.
-
#label ⇒ Object
Returns the value of attribute label.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #button_up(id) ⇒ Object
- #call ⇒ Object
- #draw ⇒ Object
- #draw_background(color) ⇒ Object
- #draw_element ⇒ Object
- #draw_tooltip ⇒ Object
-
#initialize(label: nil, tooltip: nil, image: nil, x: 0, y: 0, z: 0, color:, tag: nil, &block) ⇒ Button
constructor
A new instance of Button.
- #mouse_over? ⇒ Boolean
- #position_elements ⇒ Object
- #set_interactive_colors ⇒ Object
Methods included from Support
#code_editor, #sprite_editor, #window
Constructor Details
#initialize(label: nil, tooltip: nil, image: nil, x: 0, y: 0, z: 0, color:, tag: nil, &block) ⇒ Button
Returns a new instance of Button.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/author_engine/button.rb', line 9 def initialize(label: nil, tooltip: nil, image: nil, x: 0, y: 0, z: 0, color:, tag: nil, &block) @label, @image = label, image @x, @y, @z = x, y, z @color = color @tag = tag @block = block @width, @height = 0, 0 @x_padding = PADDING * window.scale_x @y_padding = PADDING * window.scale_y if @label.is_a?(String) @text = AuthorEngine::Text.new(message: @label, x: @x, y: @y, z: @z) end if @image.is_a?(String) @image = AuthorEngine::Image.new(@image, retro: true) end if tooltip.is_a?(String) @tooltip = AuthorEngine::Text.new(message: tooltip, x: @x, y: @y+@height, z: 1000) end set_interactive_colors position_elements return self end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def block @block end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def height @height end |
#image ⇒ Object
Returns the value of attribute image.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def image @image end |
#label ⇒ Object
Returns the value of attribute label.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def label @label end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def tag @tag end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def text @text end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
7 8 9 |
# File 'lib/author_engine/button.rb', line 7 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
8 9 10 |
# File 'lib/author_engine/button.rb', line 8 def z @z end |
Instance Method Details
#button_up(id) ⇒ Object
61 62 63 |
# File 'lib/author_engine/button.rb', line 61 def (id) call if mouse_over? && (id == Gosu::MsLeft) end |
#call ⇒ Object
150 151 152 |
# File 'lib/author_engine/button.rb', line 150 def call @block.call(self) if @block end |
#draw ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/author_engine/button.rb', line 48 def draw if mouse_over? && Gosu.(Gosu::MsLeft) draw_background(@color_active) elsif mouse_over? draw_background(@color_hover) else draw_background(@color) end draw_element draw_tooltip if @tooltip && mouse_over? end |
#draw_background(color) ⇒ Object
99 100 101 |
# File 'lib/author_engine/button.rb', line 99 def draw_background(color) Gosu.draw_rect(@x, @y, @width, @height, color, @z) end |
#draw_element ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/author_engine/button.rb', line 103 def draw_element if @text && @text.is_a?(AuthorEngine::Text) @text.draw elsif @image && @image.is_a?(AuthorEngine::Sprite) @image.draw elsif @image && @image.is_a?(AuthorEngine::Image) @image.draw(@x+@x_padding, @y+@y_padding, @z, (1 * window.square_scale), (1 * window.square_scale)) else raise "Nothing to draw! (label and image were nil or invalid types)" end end |
#draw_tooltip ⇒ Object
118 119 120 121 |
# File 'lib/author_engine/button.rb', line 118 def draw_tooltip Gosu.draw_rect(@tooltip.x-@x_padding, @tooltip.y-(@y_padding*2), @tooltip.width+(@x_padding*2), @tooltip.height+(@y_padding*2), Gosu::Color.rgba(0,0,0, 200), @tooltip.z) @tooltip.draw end |
#mouse_over? ⇒ Boolean
92 93 94 95 96 97 |
# File 'lib/author_engine/button.rb', line 92 def mouse_over? if window.mouse_x.between?(@x, @x+@width) && window.mouse_y.between?(@y, @y+@height) true end end |
#position_elements ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/author_engine/button.rb', line 123 def position_elements if @text && @text.is_a?(AuthorEngine::Text) @text.x, @text.y = @x+@x_padding, @y+@y_padding @width, @height = @text.width+(@x_padding*2), @text.height+(@y_padding*2) elsif @image && @image.is_a?(AuthorEngine::Sprite) @image.x, @image.y = @x+@x_padding, @y+@y_padding @width, @height = @image.width+(@x_padding*2), @image.height+(@y_padding*2) elsif @image && @image.is_a?(AuthorEngine::Image) @width, @height = (@image.width * window.square_scale)+(@x_padding*2), (@image.height * window.square_scale)+(@y_padding) else raise "From Button -> text and image were nil or invalid types" end if @tooltip if (@x + @tooltip.width + @x_padding) > window.width @tooltip.x = @x - (((@x+@tooltip.width) - window.width) + @x_padding) else @tooltip.x = (@x - @tooltip.width / 2) + @width/2 end @tooltip.y = (@y + @height + (@y_padding*2)) end end |
#set_interactive_colors ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/author_engine/button.rb', line 81 def set_interactive_colors if @color.value > 0.7 @color_active = window.darken(@color, 50) @color_hover = window.darken(@color) else @color_active = window.lighten(@color, 50) @color_hover = window.lighten(@color) end end |