Class: Fidgit::Element Abstract

Inherits:
Object show all
Includes:
Event
Defined in:
lib/fidgit/elements/element.rb

Overview

This class is abstract.

An element within the GUI environment.

Direct Known Subclasses

Container, ImageFrame, ScrollBar::Handle, Slider::Handle, TextArea, TextLine

Constant Summary

DEFAULT_SCHEMA_FILE =
File.expand_path(File.join(__FILE__, '..', '..', '..', '..', 'config', 'default_schema.yml'))
VALID_ALIGN_H =
[:left, :center, :right, :fill]
VALID_ALIGN_V =
[:top, :center, :bottom, :fill]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

- (Element) initialize(options = {}) { ... }

Returns a new instance of Element

Yields:

  • instance_methods_eval with respect to self.

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# 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

Instance Attribute Details

- (Object) align_h (readonly)

Returns the value of attribute align_h



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def align_h
  @align_h
end

- (Object) align_v (readonly)

Returns the value of attribute align_v



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def align_v
  @align_v
end

- (Object) background_color

Returns the value of attribute background_color



48
49
50
# File 'lib/fidgit/elements/element.rb', line 48

def background_color
  @background_color
end

- (Object) border_thickness (readonly)

Returns the value of attribute border_thickness



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def border_thickness
  @border_thickness
end

- (Object) font

Returns the value of attribute font



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def font
  @font
end

- (Object) padding_bottom (readonly)

Returns the value of attribute padding_bottom



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def padding_bottom
  @padding_bottom
end

- (Object) padding_left (readonly)

Returns the value of attribute padding_left



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def padding_left
  @padding_left
end

- (Object) padding_right (readonly)

Returns the value of attribute padding_right



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def padding_right
  @padding_right
end

- (Object) padding_top (readonly)

Returns the value of attribute padding_top



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def padding_top
  @padding_top
end

- (Object) parent

Returns the value of attribute parent



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def parent
  @parent
end

- (Object) tip

Returns the value of attribute tip



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def tip
  @tip
end

- (Object) z (readonly)

Returns the value of attribute z



45
46
47
# File 'lib/fidgit/elements/element.rb', line 45

def z
  @z
end

Class Method Details

+ (Object) new(*args, &block)



93
94
95
96
97
98
# 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

+ (Object) original_new



91
# File 'lib/fidgit/elements/element.rb', line 91

alias_method :original_new, :new

+ (Object) schema



88
# File 'lib/fidgit/elements/element.rb', line 88

def self.schema; @@schema ||= Schema.new(YAML.load(File.read(DEFAULT_SCHEMA_FILE)));; end

Instance Method Details

- (Object) default(*names)

Get the default value from the schema.

Parameters:

  • names (Symbol, Array<Symbol>)


104
105
106
# File 'lib/fidgit/elements/element.rb', line 104

def default(*names)
  self.class.schema.default(self.class, names)
end

- (Boolean) drag?(button)

Can the object be dragged?

Returns:

  • (Boolean)


74
# File 'lib/fidgit/elements/element.rb', line 74

def drag?(button); false; end

- (Object) draw

Redraw the element.



222
223
224
225
226
227
# File 'lib/fidgit/elements/element.rb', line 222

def draw
  draw_background
  draw_border
  draw_foreground
  nil
end

- (Object) draw_frame(*args)



238
239
240
# File 'lib/fidgit/elements/element.rb', line 238

def draw_frame(*args)
  $window.current_game_state.draw_frame(*args)
end

- (Object) draw_rect(*args)



234
235
236
# File 'lib/fidgit/elements/element.rb', line 234

def draw_rect(*args)
  $window.current_game_state.draw_rect(*args)
end

- (Object) enabled=(value)



78
79
80
81
82
83
84
# 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

- (Boolean) enabled?

Returns:

  • (Boolean)


76
# File 'lib/fidgit/elements/element.rb', line 76

def enabled?; @enabled; end

- (Object) height

Height not including border.



66
# File 'lib/fidgit/elements/element.rb', line 66

def height; rect.height; end

- (Object) height=(value)



67
# File 'lib/fidgit/elements/element.rb', line 67

def height=(value); rect.height = [[value, @height_range.max].min, @height_range.min].max; end

- (Boolean) hit?(x, y)

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

Returns:

  • (Boolean)


217
218
219
# File 'lib/fidgit/elements/element.rb', line 217

def hit?(x, y)
  @rect.collide_point?(x, y)
end

- (Object) max_height



69
# File 'lib/fidgit/elements/element.rb', line 69

def max_height; @height_range.max; end

- (Object) max_width



61
# File 'lib/fidgit/elements/element.rb', line 61

def max_width; @width_range.max; end

- (Object) min_height



68
# File 'lib/fidgit/elements/element.rb', line 68

def min_height; @height_range.min; end

- (Object) min_width



60
# File 'lib/fidgit/elements/element.rb', line 60

def min_width; @width_range.min; end

- (Object) outer_height

Height including border thickness.



71
# File 'lib/fidgit/elements/element.rb', line 71

def outer_height; rect.height + @border_thickness * 2; end

- (Object) outer_width

Width including border thickness.



63
# File 'lib/fidgit/elements/element.rb', line 63

def outer_width; rect.width + @border_thickness * 2; end

- (Object) recalc



208
209
210
211
212
213
214
# 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

- (Object) to_s



294
295
296
# File 'lib/fidgit/elements/element.rb', line 294

def to_s
  "#{self.class} (#{x}, #{y}) #{width}x#{height}"
end

- (Object) update

Update the element.



230
231
232
# File 'lib/fidgit/elements/element.rb', line 230

def update
  nil
end

- (Object) width

Width not including border.



58
# File 'lib/fidgit/elements/element.rb', line 58

def width; rect.width; end

- (Object) width=(value)



59
# File 'lib/fidgit/elements/element.rb', line 59

def width=(value); rect.width = [[value, @width_range.max].min, @width_range.min].max; end

- (Object) with(&block)

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

Raises:

  • (ArgumentError)


275
276
277
278
279
280
281
282
283
284
285
# 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

- (Object) x



51
# File 'lib/fidgit/elements/element.rb', line 51

def x; rect.x; end

- (Object) x=(value)



52
# File 'lib/fidgit/elements/element.rb', line 52

def x=(value); rect.x = value; end

- (Object) y



54
# File 'lib/fidgit/elements/element.rb', line 54

def y; rect.y; end

- (Object) y=(value)



55
# File 'lib/fidgit/elements/element.rb', line 55

def y=(value); rect.y = value; end