Class: Glimmer::LibUI::Shape

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/libui/shape.rb

Overview

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

Direct Known Subclasses

Arc, Bezier, Figure, Line, Rectangle, Square

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Shape.



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

def initialize(keyword, parent, args, &block)
  @keyword = keyword
  @parent = parent
  @args = args
  @block = block
  set_parameter_defaults
  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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/glimmer/libui/shape.rb', line 111

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[parameter_index] = args.first
      area_proxy&.queue_redraw_all
    else
      @args[parameter_index]
    end
  else
    super
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#keywordObject (readonly)

Returns the value of attribute keyword.



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

def keyword
  @keyword
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

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



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

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

.exists?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/glimmer/libui/shape.rb', line 27

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

.parameter_defaults(*defaults) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/glimmer/libui/shape.rb', line 49

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

.parameters(*params) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/glimmer/libui/shape.rb', line 41

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

.shape_class(keyword) ⇒ Object



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

def shape_class(keyword)
  Glimmer::LibUI.const_get(constant_symbol(keyword))
end

Instance Method Details

#area_proxyObject



98
99
100
# File 'lib/glimmer/libui/shape.rb', line 98

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

#childrenObject



85
86
87
# File 'lib/glimmer/libui/shape.rb', line 85

def children
  @children ||= []
end

#destroyObject



94
95
96
# File 'lib/glimmer/libui/shape.rb', line 94

def destroy
  @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



90
91
92
# File 'lib/glimmer/libui/shape.rb', line 90

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

#path_proxyObject



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

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

#post_add_contentObject

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



76
77
78
# File 'lib/glimmer/libui/shape.rb', line 76

def post_add_content
  @parent&.post_initialize_child(self)
end

#post_initialize_child(child) ⇒ Object

Subclasses may override to perform post initialization work on an added child (normally must call super)



81
82
83
# File 'lib/glimmer/libui/shape.rb', line 81

def post_initialize_child(child)
  children << child
end

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

Returns:

  • (Boolean)


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

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