Class: CyberarmEngine::Element::Label

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

Direct Known Subclasses

Button

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary

Attributes inherited from CyberarmEngine::Element

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

Instance Method Summary collapse

Methods inherited from CyberarmEngine::Element

#background=, #button_down, #button_up, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #reposition, #root, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #to_s, #toggle, #update, #update_background, #visible?, #width

Methods included from Common

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_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(text, options = {}, block = nil) ⇒ Label

Returns a new instance of Label.



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

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

  @text = Text.new(
    text, font: @options[:font], z: @z, color: @options[:color],
          size: @options[:text_size], shadow: @options[:text_shadow],
          shadow_size: @options[:text_shadow_size],
          shadow_color: @options[:text_shadow_color]
  )

  @raw_text = text
end

Instance Method Details

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



21
22
23
24
25
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 21

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

  # return :handled
end

#handle_text_wrapping(max_width) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 60

def handle_text_wrapping(max_width)
  max_width ||= @parent&.width
  max_width ||= @x - (window.width + noncontent_width)
  wrap_behavior = style.text_wrap
  copy = @raw_text.to_s.dup

  if max_width >= line_width(copy[0]) && line_width(copy) > max_width && wrap_behavior != :none
    breaks = []
    line_start = 0
    line_end   = copy.length

    while line_start != copy.length
      if line_width(copy[line_start...line_end]) > max_width
        line_end = ((line_end - line_start) / 2.0)
      elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
        # To small, grow!
        # TODO: find a more efficient way
        line_end += 1

      else # FOUND IT!
        entering_line_end = line_end.floor
        max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63
        reach = 0

        if wrap_behavior == :word_wrap
          max_reach.times do |i|
            reach = i
            break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
          end

          puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}"
          line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation
        end

        breaks << line_end.floor
        line_start = line_end.floor
        line_end = copy.length

        break if entering_line_end == copy.length || reach == max_reach
      end
    end

    breaks.each_with_index do |pos, index|
      copy.insert(pos + index, "\n") if pos + index >= 0 && pos + index < copy.length
    end
  end

  @text.text = copy
end

#line_width(text) ⇒ Object



110
111
112
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 110

def line_width(text)
  (@x + @text.textobject.markup_width(text) + noncontent_width)
end

#recalculateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 27

def recalculate
  @width  = 0
  @height = 0

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

  handle_text_wrapping(_width)

  @width  = _width  || @text.width.round
  @height = _height || @text.height.round

  @text.y = @style.border_thickness_top + @style.padding_top + @y
  @text.z = @z + 3

  if (text_alignment = @options[:text_align])
    case text_alignment
    when :left
      @text.x = @style.border_thickness_left + @style.padding_left + @x
    when :center
      @text.x = if @text.width <= outer_width
                  @x + outer_width / 2 - @text.width / 2
                else # Act as left aligned
                  @style.border_thickness_left + @style.padding_left + @x
                end
    when :right
      @text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right)
    end
  end

  update_background
end

#renderObject



17
18
19
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 17

def render
  @text.draw
end

#valueObject



114
115
116
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 114

def value
  @raw_text
end

#value=(value) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cyberarm_engine/ui/elements/label.rb', line 118

def value=(value)
  @raw_text = value.to_s.chomp

  old_width = width
  old_height = height
  recalculate

  root.gui_state.request_recalculate if old_width != width || old_height != height

  publish(:changed, self.value)
end