Class: RIButton
- Inherits:
-
Object
- Object
- RIButton
- Defined in:
- lib/RIUI/RIButton.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#font ⇒ Object
Returns the value of attribute font.
-
#font_color ⇒ Object
Returns the value of attribute font_color.
-
#height ⇒ Object
Returns the value of attribute height.
-
#hover_color ⇒ Object
Returns the value of attribute hover_color.
-
#onclick ⇒ Object
Returns the value of attribute onclick.
-
#size ⇒ Object
Returns the value of attribute size.
-
#text ⇒ Object
Returns the value of attribute text.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#contains(x, y) ⇒ Object
Check if the button contains a certain point.
-
#initialize(opts = [:x, :y, :width, :height, :color, :hover_color, :onclick]) ⇒ RIButton
constructor
- x
- X position of the button y
- Y position of the button width
- Width of the button height
- Height of the button color
- Base color of the button hover_color
-
Color the button will change to when the mouse is hovered over it.
- #mouse_down_actions(x, y) ⇒ Object
- #mouse_move_actions(x, y) ⇒ Object
- #mouse_up_actions ⇒ Object
- #onClick(opts = [:onclick]) ⇒ Object
-
#setLabel(opts = [:text, :font, :size, :color]) ⇒ Object
Sets the label of the button.
- #update_actions ⇒ Object
Constructor Details
#initialize(opts = [:x, :y, :width, :height, :color, :hover_color, :onclick]) ⇒ RIButton
- x
-
X position of the button
- y
-
Y position of the button
- width
-
Width of the button
- height
-
Height of the button
- color
-
Base color of the button
- hover_color
-
Color the button will change to when the mouse is hovered over it. Exclude this to have no hover color
- text
-
Text to show on the button’s label
- font
-
Font of the button’s label (note: as of now, you must include the font file within your project for this to work)
- size
-
Size of the button’s label’s text
- font_color
-
Color of the button’s label’s text
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/RIUI/RIButton.rb', line 17 def initialize(opts = [:x, :y, :width, :height, :color, :hover_color, :onclick]) ### Initialize variables and start actions @x = opts[:x] || 0 @y = opts[:y] || 0 @width = opts[:width] || 100 @height = opts[:height] || 50 @color = opts[:color] || 'blue' @hover_color = opts[:hover_color] || @color @onclick = opts[:onclick] @rect = Rectangle.new(x: @x, y: @y, z: 0, width: @width, height: @height, color: @color) #actions end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def color @color end |
#font ⇒ Object
Returns the value of attribute font.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def font @font end |
#font_color ⇒ Object
Returns the value of attribute font_color.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def font_color @font_color end |
#height ⇒ Object
Returns the value of attribute height.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def height @height end |
#hover_color ⇒ Object
Returns the value of attribute hover_color.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def hover_color @hover_color end |
#onclick ⇒ Object
Returns the value of attribute onclick.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def onclick @onclick end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def size @size end |
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def text @text end |
#width ⇒ Object
Returns the value of attribute width.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/RIUI/RIButton.rb', line 4 def y @y end |
Instance Method Details
#contains(x, y) ⇒ Object
Check if the button contains a certain point
29 30 31 32 33 34 35 |
# File 'lib/RIUI/RIButton.rb', line 29 def contains(x, y) ### Check if the button contains a certain point if @rect.contains?(x, y) true else false end end |
#mouse_down_actions(x, y) ⇒ Object
55 56 57 58 59 |
# File 'lib/RIUI/RIButton.rb', line 55 def mouse_down_actions(x, y) if @rect.contains?(x, y) @onclick.call end end |
#mouse_move_actions(x, y) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/RIUI/RIButton.rb', line 48 def mouse_move_actions(x, y) if @rect.contains?(x, y) @rect.color = @hover_color else @rect.color = @color end end |
#mouse_up_actions ⇒ Object
60 |
# File 'lib/RIUI/RIButton.rb', line 60 def mouse_up_actions; end |
#onClick(opts = [:onclick]) ⇒ Object
36 37 38 |
# File 'lib/RIUI/RIButton.rb', line 36 def onClick(opts = [:onclick]) @onclick = opts[:onclick] end |
#setLabel(opts = [:text, :font, :size, :color]) ⇒ Object
Sets the label of the button
39 40 41 42 43 44 45 46 47 |
# File 'lib/RIUI/RIButton.rb', line 39 def setLabel(opts = [:text, :font, :size, :color]) ### Sets the label of the button @text = opts[:text] @font = opts[:font] @size = opts[:size] @font_color = opts[:font_color] @temp_label = Text.new(x: 0, y: 0, text: @text, font: @font, color: @font_color, size: @size) @label = Text.new(x: @rect.x + (@rect.width / 2) - (@temp_label.width/2), y: @rect.y + (@rect.height / 2) - (@temp_label.height/2), text: @text, font: @font, color: @font_color, size: @size) @temp_label.remove end |
#update_actions ⇒ Object
61 |
# File 'lib/RIUI/RIButton.rb', line 61 def update_actions; end |