Class: Cura::Component::Base
- Inherits:
-
Object
- Object
- Cura::Component::Base
- Includes:
- Attributes::HasAncestry, Attributes::HasAttributes, Attributes::HasColors, Attributes::HasDimensions, Attributes::HasEvents, Attributes::HasFocusability, Attributes::HasInitialize, Attributes::HasOffsets, Attributes::HasRelativeCoordinates, Attributes::HasVisibility, Helpers::Component::Drawing
- Defined in:
- lib/cura/component/base.rb
Overview
The base class for all components.
All components use a box model similar to CSS. Margins, borders, paddings, then content.
Instance Attribute Summary
Attributes included from Attributes::HasAncestry
Attributes included from Attributes::HasOffsets
Attributes included from Attributes::HasEvents
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
On subclass hook.
-
.type ⇒ Symbol
The type of this component class.
Instance Method Summary collapse
-
#application ⇒ Application
Get the application of this object.
-
#background ⇒ Color
Get the background color of this object.
-
#contains_coordinates?(options = {}) ⇒ Boolean
Determine if the given absolute coordinates are within the bounds of this component.
-
#cursor ⇒ Cursor
Get the cursor for this application.
-
#draw ⇒ Component
Draw this component.
-
#draw= ⇒ Boolean
Set whether this component should be drawn.
-
#draw? ⇒ Boolean
Get whether this component should be drawn.
-
#focus ⇒ Component
Focus on this component.
-
#focused? ⇒ Boolean
Check whether this component is focused.
-
#foreground ⇒ Color
Get the foreground color of this object.
- #get_or_inherit_color(name, default) ⇒ Object
-
#inspect ⇒ String
Instance inspection.
-
#pencil ⇒ Pencil
Get the pencil for this application.
-
#update ⇒ Component
Update this component.
-
#window ⇒ Application
Get the window of this object.
Methods included from Attributes::HasVisibility
#initialize, #visible=, #visible?
Methods included from Attributes::HasAttributes
included, #initialize, #update_attributes
Methods included from Attributes::HasRelativeCoordinates
#absolute_x, #absolute_y, #initialize
Methods included from Attributes::HasCoordinates
#initialize, #x, #x=, #y, #y=
Methods included from Attributes::HasAncestry
#ancestors, #initialize, #parent?
Methods included from Attributes::HasOffsets
#border, #border=, #initialize, #margin, #margin=, #padding, #padding=
Methods included from Attributes::HasColors
#background=, #foreground=, #initialize
Methods included from Attributes::HasFocusability
#focusable=, #focusable?, #initialize
Methods included from Attributes::HasEvents
included, #initialize, #on_event
Methods included from Attributes::HasDimensions
#height, #height=, #initialize, #resize, #width, #width=
Methods included from Attributes::HasInitialize
Class Method Details
.inherited(subclass) ⇒ Object
On subclass hook.
25 26 27 28 29 |
# File 'lib/cura/component/base.rb', line 25 def inherited(subclass) super Component.all << subclass end |
.type ⇒ Symbol
The type of this component class.
36 37 38 39 40 |
# File 'lib/cura/component/base.rb', line 36 def type # TODO: Helper method for this sort of thing @type ||= to_s.gsub(/^Cura::Component::/, "") .gsub(/([A-Z][A-Za-z]*)([A-Z][A-Za-z0-9_]*)/, "\\1_\\2") .gsub(/::/, "_").downcase.to_sym end |
Instance Method Details
#application ⇒ Application
Get the application of this object.
76 77 78 79 80 |
# File 'lib/cura/component/base.rb', line 76 def application return nil if parent.nil? parent.application end |
#background ⇒ Color
Get the background color of this object.
129 130 131 |
# File 'lib/cura/component/base.rb', line 129 def background get_or_inherit_color(:background, Color.white) end |
#contains_coordinates?(options = {}) ⇒ Boolean
Determine if the given absolute coordinates are within the bounds of this component.
113 114 115 116 117 |
# File 'lib/cura/component/base.rb', line 113 def contains_coordinates?(={}) = .to_h (absolute_x..absolute_x + width).include?([:x].to_i) && (absolute_y..absolute_y + width).include?([:y].to_i) end |
#cursor ⇒ Cursor
Get the cursor for this application. TODO: Delegate something like: def_delegate(:cursor) { application }
61 62 63 |
# File 'lib/cura/component/base.rb', line 61 def cursor application.cursor end |
#draw ⇒ Component
Draw this component.
145 146 147 148 149 150 151 152 153 |
# File 'lib/cura/component/base.rb', line 145 def draw return self unless @visible return self unless draw? draw_background draw_border self end |
#draw= ⇒ Boolean
Set whether this component should be drawn.
165 |
# File 'lib/cura/component/base.rb', line 165 attribute(:draw, query: true) |
#draw? ⇒ Boolean
Get whether this component should be drawn.
|
# File 'lib/cura/component/base.rb', line 155
|
#focus ⇒ Component
Focus on this component.
96 97 98 |
# File 'lib/cura/component/base.rb', line 96 def focus application.dispatcher.target = self end |
#focused? ⇒ Boolean
Check whether this component is focused.
103 104 105 |
# File 'lib/cura/component/base.rb', line 103 def focused? application.dispatcher.target == self end |
#foreground ⇒ Color
Get the foreground color of this object.
122 123 124 |
# File 'lib/cura/component/base.rb', line 122 def foreground get_or_inherit_color(:foreground, Color.black) end |
#get_or_inherit_color(name, default) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/cura/component/base.rb', line 174 def get_or_inherit_color(name, default) value = instance_variable_get("@#{name}") return value unless value == :inherit return default unless respond_to?(:parent) && parent.respond_to?(name) parent.send(name) end |
#inspect ⇒ String
Instance inspection.
170 171 172 |
# File 'lib/cura/component/base.rb', line 170 def inspect "#<#{self.class}:0x#{__id__.to_s(16)} x=#{x} y=#{y} absolute_x=#{absolute_x} absolute_y=#{absolute_y} w=#{width} h=#{height} parent=#{@parent.class}:0x#{@parent.__id__.to_s(16)}>" end |
#pencil ⇒ Pencil
Get the pencil for this application. TODO: Delegate
69 70 71 |
# File 'lib/cura/component/base.rb', line 69 def pencil application.pencil end |
#update ⇒ Component
Update this component.
136 137 138 139 140 |
# File 'lib/cura/component/base.rb', line 136 def update @draw = true self end |
#window ⇒ Application
Get the window of this object.
85 86 87 88 89 90 91 |
# File 'lib/cura/component/base.rb', line 85 def window return nil if parent.nil? return nil if parent.is_a?(Cura::Application) return parent if parent.is_a?(Cura::Window) parent.window end |