class Cura::Component::Base

The base class for all components.

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

Public Class Methods

inherited(subclass) click to toggle source

On subclass hook.

Calls superclass method
# File lib/cura/component/base.rb, line 25
def inherited(subclass)
  super

  Component.all << subclass
end
type() click to toggle source

The type of this component class.

@example

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

@return [Symbol]

# 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

Public Instance Methods

application() click to toggle source

Get the application of this object.

@return [Application]

# File lib/cura/component/base.rb, line 76
def application
  return nil if parent.nil?

  parent.application
end
background() click to toggle source

Get the background color of this object.

@return [Color]

# File lib/cura/component/base.rb, line 129
def background
  get_or_inherit_color(:background, Color.white)
end
contains_coordinates?(options={}) click to toggle source

Determine if the given absolute coordinates are within the bounds of this component.

@param [#to_h] options @option options [#to_i] :x @option options [#to_i] :y @return [Boolean]

# 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
cursor() click to toggle source

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

@return [Cursor]

# File lib/cura/component/base.rb, line 61
def cursor
  application.cursor
end
draw() click to toggle source

Draw this component.

@return [Component]

# File lib/cura/component/base.rb, line 145
def draw
  return self unless @visible
  return self unless draw?

  draw_background
  draw_border

  self
end
focus() click to toggle source

Focus on this component.

@return [Component]

# File lib/cura/component/base.rb, line 96
def focus
  application.dispatcher.target = self
end
focused?() click to toggle source

Check whether this component is focused.

@return [Boolean]

# File lib/cura/component/base.rb, line 103
def focused?
  application.dispatcher.target == self
end
foreground() click to toggle source

Get the foreground color of this object.

@return [Color]

# File lib/cura/component/base.rb, line 122
def foreground
  get_or_inherit_color(:foreground, Color.black)
end
get_or_inherit_color(name, default) click to toggle source
# 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() click to toggle source

Instance inspection.

@return [String]

# 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() click to toggle source

Get the pencil for this application. TODO: Delegate

@return [Pencil]

# File lib/cura/component/base.rb, line 69
def pencil
  application.pencil
end
update() click to toggle source

Update this component.

@return [Component]

# File lib/cura/component/base.rb, line 136
def update
  @draw = true

  self
end
window() click to toggle source

Get the window of this object.

@return [Application]

# 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