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.



25
26
27
28
29
# File 'lib/cura/component/base.rb', line 25

def inherited(subclass)
  super

  Component.all << subclass
end

.typeSymbol

The type of this component class.

Examples:

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

Returns:

  • (Symbol)


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

#applicationApplication

Get the application of this object.

Returns:



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

def application
  return nil if parent.nil?

  parent.application
end

#backgroundColor

Get the background color of this object.

Returns:



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.

Parameters:

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

Options Hash (options):

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

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/cura/component/base.rb', line 113

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:



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

def cursor
  application.cursor
end

#drawComponent

Draw this component.

Returns:



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.

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


165
# File 'lib/cura/component/base.rb', line 165

attribute(:draw, query: true)

#draw?Boolean

Get whether this component should be drawn.

Returns:

  • (Boolean)


# File 'lib/cura/component/base.rb', line 155


#focusComponent

Focus on this component.

Returns:



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.

Returns:

  • (Boolean)


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

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

#foregroundColor

Get the foreground color of this object.

Returns:



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

#inspectString

Instance inspection.

Returns:

  • (String)


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

#pencilPencil

Get the pencil for this application. TODO: Delegate

Returns:



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

def pencil
  application.pencil
end

#updateComponent

Update this component.

Returns:



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

def update
  @draw = true

  self
end

#windowApplication

Get the window of this object.

Returns:



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