Class: Glimmer::LibUI::Shape

Inherits:
Object
  • Object
show all
Includes:
DataBindable, Parent
Defined in:
lib/glimmer/libui/shape.rb,
lib/glimmer/libui/shape/arc.rb,
lib/glimmer/libui/shape/line.rb,
lib/glimmer/libui/shape/bezier.rb,
lib/glimmer/libui/shape/circle.rb,
lib/glimmer/libui/shape/figure.rb,
lib/glimmer/libui/shape/square.rb,
lib/glimmer/libui/shape/polygon.rb,
lib/glimmer/libui/shape/polyline.rb,
lib/glimmer/libui/shape/rectangle.rb,
lib/glimmer/libui/shape/polybezier.rb

Overview

Represents LibUI lightweight shape objects nested under path (e.g. line, rectangle, arc, bezier)

Direct Known Subclasses

Arc, Bezier, Circle, Figure, Line, Polybezier, Polygon, Polyline, Rectangle, Square

Defined Under Namespace

Classes: Arc, Bezier, Circle, Figure, Line, Polybezier, Polygon, Polyline, Rectangle, Square

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataBindable

#data_bind, #data_bind_read, #data_bind_write, #data_binding_model_attribute_observer_registrations

Methods included from Parent

#children

Constructor Details

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

Returns a new instance of Shape.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/glimmer/libui/shape.rb', line 194

def method_missing(method_name, *args, &block)
  method_name_parameter = method_name.to_s.sub(/=$/, '').sub(/^set_/, '').to_sym
  if self.class.parameters.include?(method_name_parameter)
    method_name = method_name.to_s
    parameter_index = self.class.parameters.index(method_name_parameter)
    if method_name.start_with?('set_') || method_name.end_with?('=') || !args.empty?
      args = [args] if args.size > 1
      if args.first != @args[parameter_index]
        @args[parameter_index] = args.first
        request_auto_redraw
      end
    else
      @args[parameter_index]
    end
  else # TODO consider if there is a need to redirect anything to path proxy or delete this TODO
    super
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



70
71
72
# File 'lib/glimmer/libui/shape.rb', line 70

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



70
71
72
# File 'lib/glimmer/libui/shape.rb', line 70

def block
  @block
end

#content_addedObject (readonly) Also known as: content_added?

Returns the value of attribute content_added.



70
71
72
# File 'lib/glimmer/libui/shape.rb', line 70

def content_added
  @content_added
end

#keywordObject (readonly)

Returns the value of attribute keyword.



70
71
72
# File 'lib/glimmer/libui/shape.rb', line 70

def keyword
  @keyword
end

#parentObject (readonly)

Returns the value of attribute parent.



70
71
72
# File 'lib/glimmer/libui/shape.rb', line 70

def parent
  @parent
end

Class Method Details

.constant_symbol(keyword) ⇒ Object



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

def constant_symbol(keyword)
  "#{keyword.camelcase(:upper)}".to_sym
end

.create(keyword, parent, args, &block) ⇒ Object



38
39
40
# File 'lib/glimmer/libui/shape.rb', line 38

def create(keyword, parent, args, &block)
  shape_class(keyword).new(keyword, parent, args, &block)
end

.exists?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/glimmer/libui/shape.rb', line 32

def exists?(keyword)
  Shape.constants.include?(constant_symbol(keyword)) and
    shape_class(keyword).respond_to?(:ancestors) and
    shape_class(keyword).ancestors.include?(Shape)
end

.parameter_defaults(*defaults) ⇒ Object



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

def parameter_defaults(*defaults)
  if defaults.empty?
    @parameter_defaults
  else
    @parameter_defaults = defaults
  end
end

.parameters(*params) ⇒ Object



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

def parameters(*params)
  if params.empty?
    @parameters
  else
    @parameters = params
  end
end

.shape_class(keyword) ⇒ Object



42
43
44
# File 'lib/glimmer/libui/shape.rb', line 42

def shape_class(keyword)
  Shape.const_get(constant_symbol(keyword))
end

Instance Method Details

#area_proxyObject



123
124
125
# File 'lib/glimmer/libui/shape.rb', line 123

def area_proxy
  find_parent_in_ancestors { |parent| parent.nil? || parent.is_a?(ControlProxy::AreaProxy) }
end

#bounding_boxObject

Returns bounding box Array consisting of

minx, miny, width, height


170
171
172
173
174
175
176
177
178
179
# File 'lib/glimmer/libui/shape.rb', line 170

def bounding_box
  perfect_shape_bounding_box = perfect_shape&.bounding_box
  return if perfect_shape_bounding_box.nil?
  [
    perfect_shape_bounding_box.x,
    perfect_shape_bounding_box.y,
    perfect_shape_bounding_box.width,
    perfect_shape_bounding_box.height,
  ]
end

#contain?(*point, outline: false, distance_tolerance: 0) ⇒ Boolean

Returns if shape contains point on the inside when outline is false (default) or if point is on the outline when outline is true distance_tolerance is used when outline is true to enable a fuzz factor in determining if a point lies on the outline (e.g. makes it easier to select a shape by mouse)

Returns:

  • (Boolean)


154
155
156
# File 'lib/glimmer/libui/shape.rb', line 154

def contain?(*point, outline: false, distance_tolerance: 0)
  perfect_shape&.contain?(*point, outline: outline, distance_tolerance: distance_tolerance)
end

#content(&block) ⇒ Object



100
101
102
103
# File 'lib/glimmer/libui/shape.rb', line 100

def content(&block)
  Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Libui::ShapeExpression.new, @keyword, {post_add_content: @content_added}, &block)
  request_auto_redraw
end

#destroyObject



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

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

#draw(area_draw_params) ⇒ Object

Subclasses must override to perform draw work and call super afterwards to ensure calling destroy when semi-declarative in an on_draw method



106
107
108
# File 'lib/glimmer/libui/shape.rb', line 106

def draw(area_draw_params)
  destroy if area_proxy.nil?
end

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



131
132
133
# File 'lib/glimmer/libui/shape.rb', line 131

def fill(*args)
  path_proxy.fill(*args)
end

#include?(*point) ⇒ Boolean

Returns if shape includes point on the inside when filled or if shape includes point on the outline when stroked

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/glimmer/libui/shape.rb', line 160

def include?(*point)
  if fill.empty?
    contain?(*point, outline: true, distance_tolerance: ((stroke[:thickness] || 1) - 1))
  else
    contain?(*point)
  end
end

#path_proxyObject



127
128
129
# File 'lib/glimmer/libui/shape.rb', line 127

def path_proxy
  find_parent_in_ancestors { |parent| parent.nil? || parent.is_a?(ControlProxy::PathProxy) }
end

#perfect_shapeObject

Returns PerfectShape object matching this shape to enable executing computational geometry algorithms

Subclasses must implement



185
186
187
# File 'lib/glimmer/libui/shape.rb', line 185

def perfect_shape
  # No Op
end

#post_add_contentObject

Subclasses may override to perform post add_content work (normally must call super)



84
85
86
87
88
89
90
# File 'lib/glimmer/libui/shape.rb', line 84

def post_add_content
  unless @content_added
    @parent&.post_initialize_child(self)
    @parent.post_add_content if implicit_path? && dynamic?
    @content_added = true
  end
end

#post_initialize_child(child, add_child: true) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/glimmer/libui/shape.rb', line 92

def post_initialize_child(child, add_child: true)
  if child.is_a?(ControlProxy::MatrixProxy)
    path_proxy.post_initialize_child(child, add_child: add_child)
  else
    super(child, add_child: add_child)
  end
end

#redrawObject



110
111
112
# File 'lib/glimmer/libui/shape.rb', line 110

def redraw
  area_proxy&.redraw
end

#request_auto_redrawObject



114
115
116
# File 'lib/glimmer/libui/shape.rb', line 114

def request_auto_redraw
  area_proxy&.request_auto_redraw
end

#respond_to?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
# File 'lib/glimmer/libui/shape.rb', line 189

def respond_to?(method_name, *args, &block)
  self.class.parameters.include?(method_name.to_s.sub(/=$/, '').sub(/^set_/, '').to_sym) or
    super(method_name, true)
end

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



137
138
139
# File 'lib/glimmer/libui/shape.rb', line 137

def stroke(*args)
  path_proxy.stroke(*args)
end

#transform(matrix = nil) ⇒ Object Also known as: transform=, set_transform



143
144
145
# File 'lib/glimmer/libui/shape.rb', line 143

def transform(matrix = nil)
  path_proxy.transform(matrix)
end