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.



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

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

Returns the value of attribute block.



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

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



200
201
202
# File 'lib/glimmer/libui/attributed_string.rb', line 200

def area_proxy
  @parent_proxy.parent_proxy
end

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



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

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



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

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

#content(&block) ⇒ Object



204
205
206
# File 'lib/glimmer/libui/attributed_string.rb', line 204

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Libui::StringExpression.new, @keyword, &block)
end

#destroyObject



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

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

#draw(area_draw_params) ⇒ Object



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
181
182
183
184
185
# File 'lib/glimmer/libui/attributed_string.rb', line 133

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



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

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



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

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

#post_add_content(block = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/glimmer/libui/attributed_string.rb', line 119

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

#post_initialize_child(child) ⇒ Object



129
130
131
# File 'lib/glimmer/libui/attributed_string.rb', line 129

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

#redrawObject



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

def redraw
  area_proxy&.redraw
end

#request_auto_redrawObject



196
197
198
# File 'lib/glimmer/libui/attributed_string.rb', line 196

def request_auto_redraw
  area_proxy&.request_auto_redraw
end

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



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

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

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



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

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



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

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