Class: Glimmer::LibUI::AttributedString

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataBindable

#data_bind

Constructor Details

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

Returns a new instance of AttributedString.



36
37
38
39
40
41
42
43
# File 'lib/glimmer/libui/attributed_string.rb', line 36

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.



33
34
35
# File 'lib/glimmer/libui/attributed_string.rb', line 33

def args
  @args
end

#blockObject

Returns the value of attribute block.



34
35
36
# File 'lib/glimmer/libui/attributed_string.rb', line 34

def block
  @block
end

#keywordObject (readonly)

Returns the value of attribute keyword.



33
34
35
# File 'lib/glimmer/libui/attributed_string.rb', line 33

def keyword
  @keyword
end

#parent_proxyObject (readonly)

Returns the value of attribute parent_proxy.



33
34
35
# File 'lib/glimmer/libui/attributed_string.rb', line 33

def parent_proxy
  @parent_proxy
end

Instance Method Details

#area_proxyObject



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

def area_proxy
  @parent_proxy.parent_proxy
end

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



78
79
80
81
82
83
84
85
# File 'lib/glimmer/libui/attributed_string.rb', line 78

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



67
68
69
70
71
72
73
74
# File 'lib/glimmer/libui/attributed_string.rb', line 67

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

#content(&block) ⇒ Object



207
208
209
# File 'lib/glimmer/libui/attributed_string.rb', line 207

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

#destroyObject



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

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

#draw(area_draw_params) ⇒ Object



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
186
187
188
# File 'lib/glimmer/libui/attributed_string.rb', line 136

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



56
57
58
59
60
61
62
63
# File 'lib/glimmer/libui/attributed_string.rb', line 56

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



111
112
113
114
115
116
117
118
# File 'lib/glimmer/libui/attributed_string.rb', line 111

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



122
123
124
125
126
127
128
129
130
# File 'lib/glimmer/libui/attributed_string.rb', line 122

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



132
133
134
# File 'lib/glimmer/libui/attributed_string.rb', line 132

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

#redrawObject



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

def redraw
  area_proxy&.redraw
end

#request_auto_redrawObject



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

def request_auto_redraw
  area_proxy&.request_auto_redraw
end

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



45
46
47
48
49
50
51
52
# File 'lib/glimmer/libui/attributed_string.rb', line 45

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

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



89
90
91
92
93
94
95
96
# File 'lib/glimmer/libui/attributed_string.rb', line 89

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



100
101
102
103
104
105
106
107
# File 'lib/glimmer/libui/attributed_string.rb', line 100

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