mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2026-09-20 05:43:51 +00:00
100 lines
2.3 KiB
Ruby
100 lines
2.3 KiB
Ruby
module CyberarmEngine
|
|
class Element
|
|
class Button < TextBlock
|
|
def initialize(text_or_image, options = {}, block = nil)
|
|
@image = nil
|
|
@scale_x = 1
|
|
@scale_y = 1
|
|
|
|
@image = text_or_image if text_or_image.is_a?(Gosu::Image)
|
|
|
|
super(text_or_image, options, block)
|
|
|
|
@background_canvas.background = styled(:background)
|
|
end
|
|
|
|
def render
|
|
if @image
|
|
draw_image
|
|
else
|
|
draw_text
|
|
end
|
|
end
|
|
|
|
def draw_image
|
|
@image.draw(
|
|
styled(:border_thickness_left) + styled(:margin_left) + styled(:padding_left) + @x,
|
|
styled(:border_thickness_top) + styled(:margin_top) + styled(:padding_top) + @y,
|
|
@z + 2,
|
|
@scale_x, @scale_y, @text.color
|
|
)
|
|
end
|
|
|
|
def draw_text
|
|
@text.draw
|
|
end
|
|
|
|
def layout
|
|
@background_canvas.background = styled(:background)
|
|
@text.color = styled(:color)
|
|
|
|
if @image
|
|
@width = 0
|
|
@height = 0
|
|
|
|
_width = dimensional_size(styled(:image_width), :width)
|
|
_height = dimensional_size(styled(:image_height), :height)
|
|
|
|
if _width && _height
|
|
@scale_x = _width.to_f / @image.width
|
|
@scale_y = _height.to_f / @image.height
|
|
elsif _width
|
|
@scale_x = _width.to_f / @image.width
|
|
@scale_y = @scale_x
|
|
elsif _height
|
|
@scale_y = _height.to_f / @image.height
|
|
@scale_x = @scale_y
|
|
else
|
|
@scale_x = 1
|
|
@scale_y = 1
|
|
end
|
|
|
|
@width = _width || @image.width.floor * @scale_x
|
|
@height = _height || @image.height.floor * @scale_y
|
|
|
|
update_background
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def value
|
|
@image || super
|
|
end
|
|
|
|
def value=(value)
|
|
old_value = self.value
|
|
|
|
if value.is_a?(Gosu::Image)
|
|
@image = value
|
|
else
|
|
super
|
|
end
|
|
|
|
old_width = width
|
|
old_height = height
|
|
|
|
if old_width != width || old_height != height
|
|
root.gui_state.request_recalculate
|
|
else
|
|
recalculate
|
|
end
|
|
|
|
publish(:changed, self.value) if value != old_value
|
|
|
|
self.value
|
|
end
|
|
end
|
|
end
|
|
end
|