Class: CyberarmEngine::Element::Slider

Inherits:
Container show all
Defined in:
lib/cyberarm_engine/ui/elements/slider.rb

Defined Under Namespace

Classes: Handle

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary collapse

Attributes inherited from Container

#children, #fill_color, #gui_state, #scroll_position, #scroll_target_position, #stroke_color

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 Container

#add, #append, #build, #clear, current_container, current_container=, #debug_draw, #fits_on_line?, #hit_element?, #layout, #max_width, #mouse_wheel_down, #mouse_wheel_up, #move_to_next_line, #position_on_current_line, #position_on_next_line, #remove, #render, #scroll_jump_to_end, #scroll_jump_to_top, #scroll_page_down, #scroll_page_up, #scroll_top, #scroll_top=, #tallest_neighbor, #to_s, #update_child_element_visibity, #update_scroll, #write_tree

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 inherited from CyberarmEngine::Element

#background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #child_of?, #clicked_left_mouse_button, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draggable?, #element_visible?, #enabled=, #enabled?, #enter, #focus, #focused?, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #layout, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #parent_of?, #recalculate_if_size_changed, #released_left_mouse_button, #render, #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_background, #update_background_image, #update_background_nine_slice, #update_styles, #visible?, #width

Methods included from CyberarmEngine::Event

#event, #publish, #subscribe, #unsubscribe

Methods included from Theme

#deep_merge, #default, #theme_defaults

Constructor Details

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

Returns a new instance of Slider.



39
40
41
42
43
44
45
46
47
48
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 39

def initialize(options = {}, block = nil)
  super(options, block)

  @range     = @options[:range] || (0.0..1.0)
  @step_size = @options[:step] || 0.1
  @value     = @options[:value] || (@range.first + @range.last) / 2

  @handle = Handle.new("", parent: self, theme: options[:theme], width: 8, height: 1.0) { close }
  add(@handle)
end

Instance Attribute Details

#rangeObject

Returns the value of attribute range.



37
38
39
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 37

def range
  @range
end

#step_sizeObject

Returns the value of attribute step_size.



37
38
39
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 37

def step_size
  @step_size
end

#valueObject

Returns the value of attribute value.



36
37
38
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 36

def value
  @value
end

Instance Method Details

#drawObject



71
72
73
74
75
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 71

def draw
  super

  @handle.draw
end

#handle_dragged_to(x, _y) ⇒ Object



90
91
92
93
94
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 90

def handle_dragged_to(x, _y)
  @ratio = ((x - @handle.width / 2.0) - @x) / (content_width - @handle.outer_width.to_f)

  self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
end

#holding_left_mouse_button(_sender, x, y) ⇒ Object



84
85
86
87
88
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 84

def holding_left_mouse_button(_sender, x, y)
  handle_dragged_to(x, y)

  :handled
end

#position_handleObject



64
65
66
67
68
69
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 64

def position_handle
  @handle.x = @x + @handle.style.margin_left + @style.padding_left + @style.border_thickness_left +
              ((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)

  @handle.y = @y + @handle.style.margin_top + @style.border_thickness_top + @style.padding_top
end

#recalculateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 50

def recalculate
  _width = dimensional_size(@style.width, :width)
  _height = dimensional_size(@style.height, :height)

  @width  = _width
  @height = _height

  position_handle
  @handle.recalculate
  @handle.update_background

  update_background
end

#updateObject



77
78
79
80
81
82
# File 'lib/cyberarm_engine/ui/elements/slider.rb', line 77

def update
  super

  @tip = format("%.2f", value.to_f)
  @handle.tip = @tip
end