Class: Glimmer::LibUI::AttributedString

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/libui/attributed_string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, parent_proxy, args, &block) ⇒ AttributedString

Returns a new instance of AttributedString.



32
33
34
35
36
37
38
39
# File 'lib/glimmer/libui/attributed_string.rb', line 32

def initialize(keyword, parent_proxy, args, &block)
  @keyword = keyword
  @parent_proxy = parent_proxy
  @args = args
  @string = @args.first || ''
  @block = block
  post_add_content if @block.nil?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



30
31
32
# File 'lib/glimmer/libui/attributed_string.rb', line 30

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



30
31
32
# File 'lib/glimmer/libui/attributed_string.rb', line 30

def block
  @block
end

#keywordObject (readonly)

Returns the value of attribute keyword.



30
31
32
# File 'lib/glimmer/libui/attributed_string.rb', line 30

def keyword
  @keyword
end

#parent_proxyObject (readonly)

Returns the value of attribute parent_proxy.



30
31
32
# File 'lib/glimmer/libui/attributed_string.rb', line 30

def parent_proxy
  @parent_proxy
end

Instance Method Details

#area_proxyObject



191
192
193
# File 'lib/glimmer/libui/attributed_string.rb', line 191

def area_proxy
  @parent_proxy.parent_proxy
end

#background(value = nil) ⇒ Object Also known as: background=, set_background



74
75
76
77
78
79
80
81
# File 'lib/glimmer/libui/attributed_string.rb', line 74

def background(value = nil)
  if value.nil?
    @background
  else
    @background = Glimmer::LibUI.interpret_color(value)
    redraw
  end
end

#color(value = nil) ⇒ Object Also known as: color=, set_color



63
64
65
66
67
68
69
70
# File 'lib/glimmer/libui/attributed_string.rb', line 63

def color(value = nil)
  if value.nil?
    @color
  else
    @color = Glimmer::LibUI.interpret_color(value)
    redraw
  end
end

#destroyObject



182
183
184
185
# File 'lib/glimmer/libui/attributed_string.rb', line 182

def destroy
  open_type_features.destroy unless open_type_features.nil?
  @parent_proxy&.children&.delete(self)
end

#draw(area_draw_params) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/glimmer/libui/attributed_string.rb', line 128

def draw(area_draw_params)
  @start = ::LibUI.attributed_string_len(@parent_proxy.attributed_string)
  ::LibUI.attributed_string_append_unattributed(@parent_proxy.attributed_string, @string)
  unless color.nil?
    color_attribute = ::LibUI.new_color_attribute(@color[:r].to_f / 255.0, @color[:g].to_f / 255.0, @color[:b].to_f / 255.0, @color[:a] || 1.0)
    ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, @start, @start + @string.size)
  end
  unless background.nil?
    background_attribute = ::LibUI.new_background_attribute(@background[:r].to_f / 255.0, @background[:g].to_f / 255.0, @background[:b].to_f / 255.0, @background[:a] || 1.0)
    ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, background_attribute, @start, @start + @string.size)
  end
  unless underline.nil?
    underline_attribute = ::LibUI.new_underline_attribute(Glimmer::LibUI.enum_symbol_to_value(:underline, @underline))
    ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, underline_attribute, @start, @start + @string.size)
  end
  unless underline_color.nil?
    if Glimmer::LibUI.enum_symbols(:underline_color).include?(underline_color.to_s.to_sym) && underline_color.to_s.to_sym != :custom
      underline_color_attribute = ::LibUI.new_underline_color_attribute(Glimmer::LibUI.enum_symbol_to_value(:underline_color, @underline_color), 0, 0, 0, 0)
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, underline_color_attribute, @start, @start + @string.size)
    else
      the_color = Glimmer::LibUI.interpret_color(@underline_color)
      underline_color_attribute = ::LibUI.new_underline_color_attribute(0, the_color[:r].to_f / 255.0, the_color[:g].to_f / 255.0, the_color[:b].to_f / 255.0, the_color[:a] || 1.0)
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, underline_color_attribute, @start, @start + @string.size)
    end
  end
  unless font.nil?
    if font[:family]
      family_attribute = ::LibUI.new_family_attribute(font[:family])
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, family_attribute, @start, @start + @string.size)
    end
    if font[:size]
      size_attribute = ::LibUI.new_size_attribute(font[:size])
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, size_attribute, @start, @start + @string.size)
    end
    if font[:weight]
      weight_attribute = ::LibUI.new_weight_attribute(Glimmer::LibUI.enum_symbol_to_value(:text_weight, font[:weight]))
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, weight_attribute, @start, @start + @string.size)
    end
    if font[:italic]
      italic_attribute = ::LibUI.new_italic_attribute(Glimmer::LibUI.enum_symbol_to_value(:text_italic, font[:italic]))
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, italic_attribute, @start, @start + @string.size)
    end
    if font[:stretch]
      stretch_attribute = ::LibUI.new_stretch_attribute(Glimmer::LibUI.enum_symbol_to_value(:text_stretch, font[:stretch]))
      ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, stretch_attribute, @start, @start + @string.size)
    end
  end
  unless open_type_features.nil?
    open_type_features_attribute = ::LibUI.new_features_attribute(open_type_features.libui)
    ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, open_type_features_attribute, @start, @start + @string.size)
  end
  destroy if area_proxy.nil?
end

#font(value = nil) ⇒ Object Also known as: font=, set_font



52
53
54
55
56
57
58
59
# File 'lib/glimmer/libui/attributed_string.rb', line 52

def font(value = nil)
  if value.nil?
    @font
  else
    @font = value
    redraw
  end
end

#open_type_features(value = nil) ⇒ Object Also known as: open_type_features=, set_open_type_features



107
108
109
110
111
112
113
114
# File 'lib/glimmer/libui/attributed_string.rb', line 107

def open_type_features(value = nil)
  if value.nil?
    @open_type_features
  else
    @open_type_features = value
    redraw
  end
end

#post_add_contentObject



118
119
120
121
122
# File 'lib/glimmer/libui/attributed_string.rb', line 118

def post_add_content
  block_result = @block&.call
  @string = block_result if block_result.is_a?(String)
  @parent_proxy&.post_initialize_child(self)
end

#post_initialize_child(child) ⇒ Object



124
125
126
# File 'lib/glimmer/libui/attributed_string.rb', line 124

def post_initialize_child(child)
  self.open_type_features = child if child.is_a?(Glimmer::LibUI::ControlProxy::OpenTypeFeaturesProxy)
end

#redrawObject



187
188
189
# File 'lib/glimmer/libui/attributed_string.rb', line 187

def redraw
  area_proxy&.queue_redraw_all
end

#string(value = nil) ⇒ Object Also known as: string=, set_string



41
42
43
44
45
46
47
48
# File 'lib/glimmer/libui/attributed_string.rb', line 41

def string(value = nil)
  if value.nil?
    @string
  else
    @string = value
    redraw
  end
end

#underline(value = nil) ⇒ Object Also known as: underline=, set_underline



85
86
87
88
89
90
91
92
# File 'lib/glimmer/libui/attributed_string.rb', line 85

def underline(value = nil)
  if value.nil?
    @underline
  else
    @underline = value
    redraw
  end
end

#underline_color(value = nil) ⇒ Object Also known as: underline_color=, set_underline_color



96
97
98
99
100
101
102
103
# File 'lib/glimmer/libui/attributed_string.rb', line 96

def underline_color(value = nil)
  if value.nil?
    @underline_color
  else
    @underline_color = value
    redraw
  end
end