class Fidgit::Element

An element within the GUI environment. @abstract

Constants

DEFAULT_SCHEMA_FILE
VALID_ALIGN_H
VALID_ALIGN_V

Attributes

align_h[R]
align_v[R]
background_color[RW]
border_thickness[R]
font[R]
padding_bottom[R]
padding_left[R]
padding_right[R]
padding_top[R]
parent[R]
tip[RW]
z[R]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/fidgit/elements/element.rb, line 93
def new(*args, &block)
  obj = original_new(*args) # Block should be ignored.
  obj.send :post_init
  obj.send :post_init_block, &block if block_given?
  obj
end
Also aliased as: original_new
new(options = {}, &block) click to toggle source

@yield instance_methods_eval with respect to self.

# File lib/fidgit/elements/element.rb, line 145
def initialize(options = {}, &block)
  options = {
    x: 0,
    y: 0,
    z: 0,
    tip: '',
    font_name: default(:font_name),
    font_height: default(:font_height),
    background_color: default(:background_color),
    border_color: default(:border_color),
    border_thickness: default(:border_thickness),
    enabled: true,
  }.merge! options

  @enabled = options[:enabled]

  @mouse_over = false

  # Alignment and min/max dimensions.
  @align_h = options[:align_h] || Array(options[:align]).last || default(:align_h)
  raise ArgumentError, "Invalid align_h: #{@align_h}" unless VALID_ALIGN_H.include? @align_h

  min_width = (options[:min_width] || options[:width] || 0)
  max_width = (options[:max_width] || options[:width] || Float::INFINITY)
  @width_range = min_width..max_width                                         

  @align_v = options[:align_v] || Array(options[:align]).first ||  default(:align_v)
  raise ArgumentError, "Invalid align_v: #{@align_v}" unless VALID_ALIGN_V.include? @align_v

  min_height = (options[:min_height] || options[:height] || 0)
  max_height = (options[:max_height] || options[:height] || Float::INFINITY)
  @height_range = min_height..max_height

  @background_color = options[:background_color].dup
  @border_color = options[:border_color].dup
  @border_thickness = options[:border_thickness]

  @padding_top = options[:padding_top]       || options[:padding_v] || options[:padding] ||  default(:padding_top)
  @padding_right = options[:padding_right]   || options[:padding_h] || options[:padding] ||  default(:padding_right)
  @padding_bottom = options[:padding_bottom] || options[:padding_v] || options[:padding] ||  default(:padding_bottom)
  @padding_left = options[:padding_left]     || options[:padding_h] || options[:padding] ||  default(:padding_left)
  self.parent = options[:parent]

  @z = options[:z]
  @tip = options[:tip].dup
  font_name = if options[:font_name].nil? or options[:font_name] == :default
                 Gosu::default_font_name
               else
                 options[:font_name].dup
               end

  @font = options[:font] || Gosu::Font[font_name, options[:font_height]]

  @rect = Chingu::Rect.new(options[:x], options[:y], options[:width] || 0, options[:height] || 0)
end
original_new(*args, &block)
Alias for: new
schema() click to toggle source
# File lib/fidgit/elements/element.rb, line 88
def self.schema; @@schema ||= Schema.new(YAML.load(File.read(DEFAULT_SCHEMA_FILE)));; end

Public Instance Methods

default(*names) click to toggle source

Get the default value from the schema.

@param [Symbol, Array<Symbol>] names

# File lib/fidgit/elements/element.rb, line 104
def default(*names)
  self.class.schema.default(self.class, names)
end
drag?(button) click to toggle source

Can the object be dragged?

# File lib/fidgit/elements/element.rb, line 74
def drag?(button); false; end
draw() click to toggle source

Redraw the element.

# File lib/fidgit/elements/element.rb, line 222
def draw
  draw_background
  draw_border
  draw_foreground
  nil
end
draw_frame(*args) click to toggle source
# File lib/fidgit/elements/element.rb, line 238
def draw_frame(*args)
  $window.current_game_state.draw_frame(*args)
end
draw_rect(*args) click to toggle source
# File lib/fidgit/elements/element.rb, line 234
def draw_rect(*args)
  $window.current_game_state.draw_rect(*args)
end
enabled=(value) click to toggle source
# File lib/fidgit/elements/element.rb, line 78
def enabled=(value)
  if @mouse_over and enabled? and not value
    $window.current_game_state.unset_mouse_over
  end

  @enabled = value
end
enabled?() click to toggle source
# File lib/fidgit/elements/element.rb, line 76
def enabled?; @enabled; end
font=(font) click to toggle source
# File lib/fidgit/elements/element.rb, line 201
def font=(font)
  raise TypeError unless font.is_a? Gosu::Font
  @font = font
  recalc
  font
end
height() click to toggle source

Height not including border.

# File lib/fidgit/elements/element.rb, line 66
def height; rect.height; end
height=(value) click to toggle source
# File lib/fidgit/elements/element.rb, line 67
def height=(value); rect.height = [[value, @height_range.max].min, @height_range.min].max; end
hit?(x, y) click to toggle source

Check if a point (screen coordinates) is over the element.

# File lib/fidgit/elements/element.rb, line 217
def hit?(x, y)
  @rect.collide_point?(x, y)
end
max_height() click to toggle source
# File lib/fidgit/elements/element.rb, line 69
def max_height; @height_range.max; end
max_width() click to toggle source
# File lib/fidgit/elements/element.rb, line 61
def max_width; @width_range.max; end
min_height() click to toggle source
# File lib/fidgit/elements/element.rb, line 68
def min_height; @height_range.min; end
min_width() click to toggle source
# File lib/fidgit/elements/element.rb, line 60
def min_width; @width_range.min; end
outer_height() click to toggle source

Height including border thickness.

# File lib/fidgit/elements/element.rb, line 71
def outer_height; rect.height + @border_thickness * 2; end
outer_width() click to toggle source

Width including border thickness.

# File lib/fidgit/elements/element.rb, line 63
def outer_width; rect.width + @border_thickness * 2; end
recalc() click to toggle source
# File lib/fidgit/elements/element.rb, line 208
def recalc
  old_width, old_height = width, height
  layout
  parent.recalc if parent and (width != old_width or height != old_height)

  nil
end
to_s() click to toggle source
# File lib/fidgit/elements/element.rb, line 294
def to_s
  "#{self.class} (#{x}, #{y}) #{width}x#{height}"
end
update() click to toggle source

Update the element.

# File lib/fidgit/elements/element.rb, line 230
def update
  nil
end
width() click to toggle source

Width not including border.

# File lib/fidgit/elements/element.rb, line 58
def width; rect.width; end
width=(value) click to toggle source
# File lib/fidgit/elements/element.rb, line 59
def width=(value); rect.width = [[value, @width_range.max].min, @width_range.min].max; end
with() { |self| ... } click to toggle source

Evaluate a block, just like it was a constructor block.

# File lib/fidgit/elements/element.rb, line 275
def with(&block)
  raise ArgumentError.new("Must pass a block") unless block_given?
  case block.arity
    when 1
      yield self
    when 0
      instance_methods_eval(&block)
    else
      raise "block arity must be 0 or 1"
  end
end
x() click to toggle source
# File lib/fidgit/elements/element.rb, line 51
def x; rect.x; end
x=(value) click to toggle source
# File lib/fidgit/elements/element.rb, line 52
def x=(value); rect.x = value; end
y() click to toggle source
# File lib/fidgit/elements/element.rb, line 54
def y; rect.y; end
y=(value) click to toggle source
# File lib/fidgit/elements/element.rb, line 55
def y=(value); rect.y = value; end

Protected Instance Methods

draw_background() click to toggle source
# File lib/fidgit/elements/element.rb, line 246
def draw_background
  draw_rect(x, y, width, height, z, @background_color) unless @background_color.transparent?
end
draw_border() click to toggle source
# File lib/fidgit/elements/element.rb, line 251
def draw_border
  draw_frame(x, y, width, height, @border_thickness, z, @border_color) if @border_thickness > 0 and not @border_color.transparent?
end
draw_foreground() click to toggle source
# File lib/fidgit/elements/element.rb, line 256
def draw_foreground
  nil
end
layout() click to toggle source

Should be overridden in children to recalculate the width and height of the element and, if a container manage the positions of its children.

# File lib/fidgit/elements/element.rb, line 263
def layout
  nil
end
parent=(parent) click to toggle source
# File lib/fidgit/elements/element.rb, line 243
def parent=(parent); @parent = parent; end
post_init() click to toggle source
# File lib/fidgit/elements/element.rb, line 268
def post_init
  recalc
  @parent.send :add, self if @parent
end
post_init_block(&block) click to toggle source

By default, elements do not accept block arguments.

# File lib/fidgit/elements/element.rb, line 289
def post_init_block(&block)
  raise ArgumentError, "does not accept a block"
end
rect() click to toggle source
# File lib/fidgit/elements/element.rb, line 86
def rect; @rect; end