Class: Cura::Component::Base

Overview

The base class for all components.

All components use a box model similar to CSS. Margins, borders, paddings, then content.

Direct Known Subclasses

Group, Label

Instance Attribute Summary

Attributes included from Attributes::HasAncestry

#parent

Attributes included from Attributes::HasOffsets

#offsets

Attributes included from Attributes::HasEvents

#event_handler

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize

Class Method Details

.inherited(subclass) ⇒ Object

On subclass hook.



22
23
24
# File 'lib/cura/component/base.rb', line 22

def inherited(subclass)
  Component.all << subclass
end

.typeSymbol

The type of this component class.

Examples:

Cura::Component::XMLTools::AttributeLabel.type # => :xml_tools_attribute_label

Returns:

  • (Symbol)


31
32
33
34
35
# File 'lib/cura/component/base.rb', line 31

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

#applicationApplication

Get the application of this object.

Returns:



69
70
71
72
73
# File 'lib/cura/component/base.rb', line 69

def application
  return nil if parent.nil?

  parent.application
end

#backgroundColor

Get the background color of this object.

Returns:



111
112
113
# File 'lib/cura/component/base.rb', line 111

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.

Parameters:

  • options (#to_h) (defaults to: {})

Options Hash (options):

  • :x (#to_i)
  • :y (#to_i)

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/cura/component/base.rb', line 95

def contains_coordinates?(options={})
  options = options.to_h

  (absolute_x..absolute_x + width).include?(options[:x].to_i) && (absolute_y..absolute_y + width).include?(options[:y].to_i)
end

#cursorCursor

Get the cursor for this application. TODO: Delegate something like: def_delegate(:cursor) { application }

Returns:



54
55
56
# File 'lib/cura/component/base.rb', line 54

def cursor
  application.cursor
end

#drawComponent

Draw this component.

Returns:



125
126
127
128
129
130
131
132
# File 'lib/cura/component/base.rb', line 125

def draw
  if @visible
    draw_background
    draw_border
  end

  self
end

#focusComponent

Focus on this component.

Returns:



78
79
80
# File 'lib/cura/component/base.rb', line 78

def focus
  application.dispatcher.target = self
end

#focused?Boolean

Check whether this component is focused.

Returns:

  • (Boolean)


85
86
87
# File 'lib/cura/component/base.rb', line 85

def focused?
  application.dispatcher.target == self
end

#foregroundColor

Get the foreground color of this object.

Returns:



104
105
106
# File 'lib/cura/component/base.rb', line 104

def foreground
  get_or_inherit_color(:foreground, Color.black)
end

#inspectString

Instance inspection.

Returns:

  • (String)


137
138
139
# File 'lib/cura/component/base.rb', line 137

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

#pencilPencil

Get the pencil for this application. TODO: Delegate

Returns:



62
63
64
# File 'lib/cura/component/base.rb', line 62

def pencil
  application.pencil
end

#updateComponent

Update this component.

Returns:



118
119
120
# File 'lib/cura/component/base.rb', line 118

def update
  self
end