Class: CyberarmEngine::Element::Image

Inherits:
CyberarmEngine::Element show all
Defined in:
lib/cyberarm_engine/ui/elements/image.rb

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary

Attributes inherited from CyberarmEngine::Element

#background_canvas, #border_canvas, #element_visible, #event_handler, #options, #parent, #style, #tip, #x, #y, #z

Instance Method Summary collapse

Methods inherited from CyberarmEngine::Element

#background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #child_of?, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draggable?, #draw, #element_visible?, #enabled=, #enabled?, #enter, #focus, #focused?, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #parent_of?, #recalculate, #recalculate_if_size_changed, #released_left_mouse_button, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_background_image, #set_background_nine_slice, #set_border_color, #set_border_thickness, #set_color, #set_font, #set_margin, #set_padding, #set_static_position, #show, #space_available_height, #space_available_width, #stylize, #to_s, #toggle, #update, #update_background, #update_background_image, #update_background_nine_slice, #update_styles, #visible?, #width

Methods included from Common

#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window

Methods included from CyberarmEngine::Event

#event, #publish, #subscribe, #unsubscribe

Methods included from Theme

#deep_merge, #default, #theme_defaults

Constructor Details

#initialize(path_or_image, options = {}, block = nil) ⇒ Image

Returns a new instance of Image.



4
5
6
7
8
9
10
11
12
13
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 4

def initialize(path_or_image, options = {}, block = nil)
  super(options, block)
  @path = path_or_image if path_or_image.is_a?(String)

  @image = Gosu::Image.new(path_or_image, retro: @options[:retro], tileable: @options[:tileable]) if @path
  @image = path_or_image unless @path

  @scale_x = 1
  @scale_y = 1
end

Instance Method Details

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



24
25
26
27
28
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 24

def clicked_left_mouse_button(_sender, _x, _y)
  @block.call(self) if @block

  :handled
end

#layoutObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 30

def layout
  _width = dimensional_size(@style.width, :width)
  _height = dimensional_size(@style.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 * @scale_x).floor
  @height = _height || (@image.height * @scale_y).floor

  update_background
end

#pathObject



67
68
69
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 67

def path
  @path
end

#renderObject



15
16
17
18
19
20
21
22
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 15

def render
  @image.draw(
    @style.border_thickness_left + @style.padding_left + @x,
    @style.border_thickness_top + @style.padding_top + @y,
    @z + 2,
    @scale_x, @scale_y, @style.color
  )
end

#valueObject



54
55
56
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 54

def value
  @image
end

#value=(path_or_image, retro: false, tileable: false) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/cyberarm_engine/ui/elements/image.rb', line 58

def value=(path_or_image, retro: false, tileable: false)
  @path = path_or_image if path_or_image.is_a?(String)

  @image = Gosu::Image.new(path_or_image, retro: retro, tileable: tileable) if @path
  @image = path_or_image unless @path

  recalculate
end