Class: Glimmer::LibUI::ControlProxy::PathProxy

Inherits:
Glimmer::LibUI::ControlProxy show all
Includes:
Parent, PerfectShaped, Transformable
Defined in:
lib/glimmer/libui/control_proxy/path_proxy.rb

Overview

Proxy for LibUI path objects

Follows the Proxy Design Pattern

Constant Summary

Constants inherited from Glimmer::LibUI::ControlProxy

BOOLEAN_PROPERTIES, KEYWORD_ALIASES, STRING_PROPERTIES, TransformProxy

Instance Attribute Summary

Attributes inherited from Glimmer::LibUI::ControlProxy

#args, #block, #content_added, #custom_control, #keyword, #libui, #options, #parent_custom_control, #parent_proxy, #slot

Instance Method Summary collapse

Methods included from PerfectShaped

#bounding_box, #contain?, #include?, #move

Methods included from Parent

#children, #post_initialize_child

Methods included from Transformable

#apply_transform, #post_initialize_child, #transform, #undo_transform

Methods inherited from Glimmer::LibUI::ControlProxy

#append_properties, #append_property, #bind_content, #can_handle_listener?, constant_symbol, #content, control_proxies, control_proxy_class, create, #custom_listener_name_aliases, #custom_listener_names, #default_destroy, #deregister_all_custom_listeners, #deregister_custom_listeners, descendant_keyword_constant_map, #destroy_child, #enabled, exists?, #handle_custom_listener, #handle_listener, #has_custom_listener?, image_proxies, keyword, #libui_api_keyword, #listeners, #listeners_for, main_window_proxy, map_descendant_keyword_constants_for, menu_proxies, #method_missing, new_control, #notify_custom_listeners, #post_initialize_child, reset_descendant_keyword_constant_map, #respond_to?, #respond_to_libui?, #send_to_libui, #visible, #window_proxy

Methods included from DataBindable

#data_bind, #data_bind_read, #data_bind_write, #data_binding_model_attribute_observer_registrations

Constructor Details

#initialize(keyword, parent, args, &block) ⇒ PathProxy

Returns a new instance of PathProxy.



39
40
41
42
43
44
45
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 39

def initialize(keyword, parent, args, &block)
  @keyword = keyword
  @parent_proxy = parent
  @args = args
  @block = block
  post_add_content if @block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::LibUI::ControlProxy

Instance Method Details

#destroyObject



150
151
152
153
154
155
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 150

def destroy
  return if ControlProxy.main_window_proxy&.destroying?
  deregister_all_custom_listeners
  @parent_proxy&.children&.delete(self)
  ControlProxy.control_proxies.delete(self)
end

#draw(area_draw_params) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 55

def draw(area_draw_params)
  build_control
  children.dup.each {|child| child.draw(area_draw_params)}
  ::LibUI.draw_path_end(@libui)
  ::LibUI.draw_fill(area_draw_params[:context], @libui, fill_draw_brush.to_ptr) unless fill.empty?
  ::LibUI.draw_stroke(area_draw_params[:context], @libui, stroke_draw_brush, draw_stroke_params) unless stroke.empty?
  ::LibUI.draw_free_path(@libui)
end

#draw_fill_modeObject



64
65
66
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 64

def draw_fill_mode
  @args[0].is_a?(Integer) ? (@args[0] == 0 ? :winding : :alternate ) : ((@args[0].is_a?(String) || @args[0].is_a?(Symbol)) ? @args[0].to_sym : :winding)
end

#draw_fill_mode_valueObject



68
69
70
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 68

def draw_fill_mode_value
  @args[0].is_a?(Integer) ? @args[0] : @args[0].to_s == 'alternate' ? 1 : 0
end

#draw_line_capObject



142
143
144
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 142

def draw_line_cap
  Glimmer::LibUI.enum_symbol_to_value(:draw_line_cap, @stroke && @stroke[:cap])
end

#draw_line_joinObject



146
147
148
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 146

def draw_line_join
  Glimmer::LibUI.enum_symbol_to_value(:draw_line_join, @stroke && @stroke[:join])
end

#draw_stroke_paramsObject



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 130

def draw_stroke_params
  @draw_stroke_params ||= ::LibUI::FFI::DrawStrokeParams.malloc
  @draw_stroke_params.Cap = draw_line_cap # flat
  @draw_stroke_params.Join = draw_line_join # miter
  @draw_stroke_params.Thickness = @stroke[:thickness] || 1
  @draw_stroke_params.MiterLimit = @stroke[:miter_limit] || 10 # DEFAULT_MITER_LIMIT
  @draw_stroke_params.Dashes = @stroke[:dashes].to_a.pack('d*')
  @draw_stroke_params.NumDashes = @stroke[:dashes].to_a.count
  @draw_stroke_params.DashPhase = @stroke[:dash_phase] || 0
  @draw_stroke_params
end

#fill(*args) ⇒ Object Also known as: fill=, set_fill



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 72

def fill(*args)
  args = args.first if args.size == 1 && (args.first.is_a?(Array) || args.first.is_a?(Hash) || args.first.is_a?(String) || args.first.is_a?(Symbol))
  if args.empty?
    @fill ||= {}
  else
    new_color = Glimmer::LibUI.interpret_color(args)
    if new_color != @fill
      # TODO consider replacing unobserve with observer_registration.deregister
      @fill_observer&.unobserve(@fill, attribute_writer_type: [:attribute=, :set_attribute]) if @fill
      @fill = new_color
      request_auto_redraw
    end
  end
  @fill.tap do
    @fill_observer ||= Glimmer::DataBinding::Observer.proc do
      request_auto_redraw
    end
    @fill_observer.observe(@fill, attribute_writer_type: [:attribute=, :set_attribute])
  end
end

#fill_draw_brushObject



95
96
97
98
99
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 95

def fill_draw_brush
  @fill_draw_brush ||= ::LibUI::FFI::DrawBrush.malloc
  init_draw_brush(@fill_draw_brush, @fill)
  @fill_draw_brush
end

#move_by(x_delta, y_delta) ⇒ Object



165
166
167
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 165

def move_by(x_delta, y_delta)
  children.each {|child| child.move_by(x_delta, y_delta)}
end

#perfect_shapeObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 169

def perfect_shape
  require 'perfect-shape'
  the_perfect_shape_dependencies = perfect_shape_dependencies
  if the_perfect_shape_dependencies != @perfect_shape_dependencies
    draw_fill_mode, _ = @perfect_shape_dependencies = the_perfect_shape_dependencies
    shapes = children.map(&:perfect_shape)
    new_shapes = []
    shapes.each do |shape|
      if shape.is_a?(PerfectShape::Path)
        new_shapes += shape.basic_shapes
      else
        new_shapes << shape
      end
    end
    shapes = new_shapes
    winding_rule = draw_fill_mode == :winding ? :wind_non_zero : :wind_even_odd
    @perfect_shape = PerfectShape::Path.new(
      shapes: shapes,
      winding_rule: winding_rule,
      line_to_complex_shapes: true
    )
  end
  @perfect_shape
end

#perfect_shape_dependenciesObject



194
195
196
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 194

def perfect_shape_dependencies
  [draw_fill_mode, children.map(&:perfect_shape_dependencies)]
end

#post_add_contentObject



47
48
49
50
51
52
53
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 47

def post_add_content
  super
  if @parent_proxy.nil? && AreaProxy.current_area_draw_params
    draw(AreaProxy.current_area_draw_params)
    destroy
  end
end

#redrawObject



157
158
159
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 157

def redraw
  @parent_proxy&.redraw
end

#request_auto_redrawObject



161
162
163
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 161

def request_auto_redraw
  @parent_proxy&.request_auto_redraw
end

#stroke(*args) ⇒ Object Also known as: stroke=, set_stroke



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 101

def stroke(*args)
  args = args.first if args.size == 1 && (args.first.is_a?(Array) || args.first.is_a?(Hash) || args.first.is_a?(String) || args.first.is_a?(Symbol))
  if args.empty?
    @stroke ||= {}
  else
    new_color = Glimmer::LibUI.interpret_color(args)
    if new_color != @stroke
      # TODO consider replacing unobserve with observer_registration.deregister
      @stroke_observer&.unobserve(@stroke, attribute_writer_type: [:attribute=, :set_attribute]) if @stroke
      @stroke = Glimmer::LibUI.interpret_color(args)
      request_auto_redraw
    end
  end
  @stroke.tap do
    @stroke_observer ||= Glimmer::DataBinding::Observer.proc do
      request_auto_redraw
    end
    @stroke_observer.observe(@stroke, attribute_writer_type: [:attribute=, :set_attribute])
  end
end

#stroke_draw_brushObject



124
125
126
127
128
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 124

def stroke_draw_brush
  @stroke_draw_brush ||= ::LibUI::FFI::DrawBrush.malloc
  init_draw_brush(@stroke_draw_brush, @stroke)
  @stroke_draw_brush
end