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

Inherits:
Glimmer::LibUI::ControlProxy show all
Includes:
Parent, 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, #keyword, #libui, #parent_proxy

Instance Method Summary collapse

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, #can_handle_listener?, constant_symbol, #content, control_proxies, 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, widget_proxy_class, #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.



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

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



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

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



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

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



62
63
64
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 62

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

#draw_line_capObject



136
137
138
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 136

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

#draw_line_joinObject



140
141
142
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 140

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

#draw_stroke_paramsObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 124

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 66

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



89
90
91
92
93
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 89

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

#post_add_contentObject



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

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

#redrawObject



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

def redraw
  @parent_proxy&.redraw
end

#request_auto_redrawObject



155
156
157
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 155

def request_auto_redraw
  @parent_proxy&.request_auto_redraw
end

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/glimmer/libui/control_proxy/path_proxy.rb', line 95

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



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

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