Class: Hokusai::Util::SegmentRenderer

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/util/clamping_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment, iterator) ⇒ SegmentRenderer

Returns a new instance of SegmentRenderer.



7
8
9
10
11
12
13
14
15
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 7

def initialize(segment, iterator)
  @segment = segment
  @iterator = iterator
  @started = false
  @start_select = 0
  @stop_select = nil
  @select_x = nil
  @select_width = nil
end

Instance Attribute Details

#iteratorObject (readonly)

Returns the value of attribute iterator.



3
4
5
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 3

def iterator
  @iterator
end

#segmentObject (readonly)

Returns the value of attribute segment.



3
4
5
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 3

def segment
  @segment
end

#select_widthObject

Returns the value of attribute select_width.



4
5
6
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 4

def select_width
  @select_width
end

#select_xObject

Returns the value of attribute select_x.



4
5
6
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 4

def select_x
  @select_x
end

#start_selectObject

Returns the value of attribute start_select.



4
5
6
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 4

def start_select
  @start_select
end

#startedObject

Returns the value of attribute started.



4
5
6
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 4

def started
  @started
end

#stop_selectObject

Returns the value of attribute stop_select.



4
5
6
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 4

def stop_select
  @stop_select
end

Instance Method Details

#can_render_inside(font_size, boundary) ⇒ Object



121
122
123
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 121

def can_render_inside(font_size, boundary)
  iterator.y >= boundary[0] && iterator.y + font_size <= boundary[1]
end

#draw(font_size, boundary, selection: nil) ⇒ Object



17
18
19
20
21
22
23
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 17

def draw(font_size, boundary, selection: nil)
  # selection_extract(font_size, boundary, selection: selection)
  draw_text(font_size, boundary)
  # selection_update(selection)
  # self.start_select = 0.0
  # self.stop_select = nil
end

#draw_text(font_size, boundary) ⇒ Object



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
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 83

def draw_text(font_size, boundary)
  if can_render_inside(font_size, boundary)
    x = @iterator.x

    if @iterator.clamping.markdown
      segment.groups.each do |group|
        text = iterator.clamping[group.offset, group.size]

        if text == "\n"
          next
          # x += group.width
        end
        
        @iterator.on_draw_cb&.call(text, x, y, group)

        x += group.width
      end
    else
      @iterator.on_draw_cb&.call(text, x, y, segment)
    end
  end

  @iterator.y += font_size
  @iterator.height += font_size
end

#selection_extract(font_size, boundary, selection: nil) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 25

def selection_extract(font_size, boundary, selection: nil)
  local_x = iterator.x
  y = iterator.y
  hit_box = Hokusai::Rect.new(0, 0, 0, 0)

  segment.chars.each do |char|
    if can_render_inside(font_size, boundary) && segment.char_is_selected(char)
      self.select_x ||= local_x
      self.select_width = select_width.nil? ? char.width : (select_width + char.width)
  
      if selector = selection
        if segment.select_begin == char.offset && selector.up? && !iterator.cursor_set && !selector.started
          iterator.cursor_position = [local_x, y, char.width, font_size.to_f]
          iterator.cursor_set = true
          selector.started = true
        elsif segment.select_end == char.offset && selector.down? && started
          iterator.cursor_position = [local_x + char.width, y, char.width, font_size.to_f]
        end
      end
    elsif !segment.select_end.nil? && segment.select_end < char.offset
      selection&.started = false
    end

    # hit_box.x = local_x
    # hit_box.y = y
    # hit_box.width = char.width
    # hit_box.height = font_size.to_f
    hit_box = Hokusai::Rect.new(local_x, y, char.width, font_size.to_f)
    iterator.on_char_cb&.call(char, hit_box, char.offset)

    if selector = selection
      if selector.active? && selector.selected(local_x, y, char.width.to_f, font_size.to_f)
        self.stop_select = char.offset

        unless started
          self.start_select = char.offset
          self.started = true
        end
      end
    end

    local_x += char.width
  end

  return unless select_x && select_width
  iterator.on_draw_selection_cb&.call(select_x, y, select_width, font_size.to_f)
end

#selection_update(selection) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 73

def selection_update(selection)
  return unless selection&.active?
  segment.make_selection(start_select, stop_select)

  return if stop_select.nil?

  iterator.start_select ||= start_select + iterator.cursor_offset
  iterator.stop_select = stop_select + iterator.cursor_offset
end

#textObject



117
118
119
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 117

def text
  iterator.clamping[segment.offset, segment.size]
end

#xObject



109
110
111
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 109

def x
  iterator.x
end

#yObject



113
114
115
# File 'ui/src/hokusai/util/clamping_iterator.rb', line 113

def y
  iterator.y
end