Class: CyberarmEngine::Element::TextBlock

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

Direct Known Subclasses

Banner, Button, Caption, Inscription, Link, Para, Subtitle, Tagline, Title, ToolTip

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=, #blur, #button_down, #button_up, #clicked_left_mouse_button, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #enter, #focus, #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, #released_left_mouse_button, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #to_s, #toggle, #update, #update_background, #update_styles, #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, #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(text, options = {}, block = nil) ⇒ TextBlock

Returns a new instance of TextBlock.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cyberarm_engine/ui/elements/text_block.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

#handle_text_wrapping(max_width) ⇒ Object



54
55
56
57
58
59
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
# File 'lib/cyberarm_engine/ui/elements/text_block.rb', line 54

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 line_width(copy[0]) <= max_width && 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)
        line_end = 1.0 if line_end <= 1
      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



105
106
107
# File 'lib/cyberarm_engine/ui/elements/text_block.rb', line 105

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

#recalculateObject



21
22
23
24
25
26
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
# File 'lib/cyberarm_engine/ui/elements/text_block.rb', line 21

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/text_block.rb', line 17

def render
  @text.draw
end

#valueObject



109
110
111
# File 'lib/cyberarm_engine/ui/elements/text_block.rb', line 109

def value
  @raw_text
end

#value=(value) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cyberarm_engine/ui/elements/text_block.rb', line 113

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