class AuthorEngine::Button

Constants

PADDING

Attributes

block[R]
height[R]
image[R]
label[R]
tag[R]
text[R]
width[R]
x[R]
y[R]
z[RW]

Public Class Methods

new(label: nil, tooltip: nil, image: nil, x: 0, y: 0, z: 0, color:, tag: nil, &block) click to toggle source
# 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

Public Instance Methods

button_up(id) click to toggle source
# File lib/author_engine/button.rb, line 61
def button_up(id)
  call if mouse_over? && (id == Gosu::MsLeft)
end
call() click to toggle source
# File lib/author_engine/button.rb, line 150
def call
  @block.call(self) if @block
end
draw() click to toggle source
# File lib/author_engine/button.rb, line 48
def draw
  if mouse_over? && Gosu.button_down?(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) click to toggle source
# File lib/author_engine/button.rb, line 99
def draw_background(color)
  Gosu.draw_rect(@x, @y, @width, @height, color, @z)
end
draw_element() click to toggle source
# 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() click to toggle source
# 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
image=(gosu_image) click to toggle source
# File lib/author_engine/button.rb, line 38
def image=(gosu_image)
  @image = gosu_image
  position_elements
end
label=(text) click to toggle source
# File lib/author_engine/button.rb, line 43
def label=(text)
  @label.text = text
  position_elements
end
mouse_over?() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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
x=(n) click to toggle source
# File lib/author_engine/button.rb, line 65
def x=(n)
  @x = n
  position_elements
end
y=(n) click to toggle source
# File lib/author_engine/button.rb, line 70
def y=(n)
  @y = n
  position_elements
end