Class: Fidgit::Element Abstract
- Includes:
- Event
- Defined in:
- lib/fidgit/elements/element.rb
Overview
An element within the GUI environment.
Direct Known Subclasses
Container, ImageFrame, ScrollBar::Handle, Slider::Handle, TextArea, TextLine
Constant Summary collapse
- DEFAULT_SCHEMA_FILE =
File.(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
-
#align_h ⇒ Object
readonly
Returns the value of attribute align_h.
-
#align_v ⇒ Object
readonly
Returns the value of attribute align_v.
-
#background_color ⇒ Object
Returns the value of attribute background_color.
-
#border_thickness ⇒ Object
readonly
Returns the value of attribute border_thickness.
-
#font ⇒ Object
Returns the value of attribute font.
-
#padding_bottom ⇒ Object
readonly
Returns the value of attribute padding_bottom.
-
#padding_left ⇒ Object
readonly
Returns the value of attribute padding_left.
-
#padding_right ⇒ Object
readonly
Returns the value of attribute padding_right.
-
#padding_top ⇒ Object
readonly
Returns the value of attribute padding_top.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#tip ⇒ Object
readonly
Returns the value of attribute tip.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Class Method Summary collapse
Instance Method Summary collapse
-
#default(*names) ⇒ Object
Get the default value from the schema.
-
#drag?(button) ⇒ Boolean
Can the object be dragged?.
-
#draw ⇒ Object
Redraw the element.
- #draw_frame(*args) ⇒ Object
- #draw_rect(*args) ⇒ Object
- #enabled=(value) ⇒ Object
- #enabled? ⇒ Boolean
-
#height ⇒ Object
Height not including border.
- #height=(value) ⇒ Object
-
#hit?(x, y) ⇒ Boolean
Check if a point (screen coordinates) is over the element.
-
#initialize(options = {}) { ... } ⇒ Element
constructor
A new instance of Element.
- #max_height ⇒ Object
- #max_width ⇒ Object
- #min_height ⇒ Object
- #min_width ⇒ Object
-
#outer_height ⇒ Object
Height including border thickness.
-
#outer_width ⇒ Object
Width including border thickness.
- #recalc ⇒ Object
- #to_s ⇒ Object
-
#update ⇒ Object
Update the element.
-
#width ⇒ Object
Width not including border.
- #width=(value) ⇒ Object
-
#with(&block) ⇒ Object
Evaluate a block, just like it was a constructor block.
- #x ⇒ Object
- #x=(value) ⇒ Object
- #y ⇒ Object
- #y=(value) ⇒ Object
Methods included from Event
#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe
Constructor Details
#initialize(options = {}) { ... } ⇒ Element
Returns a new instance of Element.
144 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 |
# File 'lib/fidgit/elements/element.rb', line 144 def initialize( = {}, &block) = { 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! @enabled = [:enabled] @mouse_over = false # Alignment and min/max dimensions. @align_h = [:align_h] || Array([:align]).last || default(:align_h) raise ArgumentError, "Invalid align_h: #{@align_h}" unless VALID_ALIGN_H.include? @align_h min_width = ([:min_width] || [:width] || 0) max_width = ([:max_width] || [:width] || Float::INFINITY) @width_range = min_width..max_width @align_v = [:align_v] || Array([:align]).first || default(:align_v) raise ArgumentError, "Invalid align_v: #{@align_v}" unless VALID_ALIGN_V.include? @align_v min_height = ([:min_height] || [:height] || 0) max_height = ([:max_height] || [:height] || Float::INFINITY) @height_range = min_height..max_height @background_color = [:background_color].dup @border_color = [:border_color].dup @border_thickness = [:border_thickness] @padding_top = [:padding_top] || [:padding_v] || [:padding] || default(:padding_top) @padding_right = [:padding_right] || [:padding_h] || [:padding] || default(:padding_right) @padding_bottom = [:padding_bottom] || [:padding_v] || [:padding] || default(:padding_bottom) @padding_left = [:padding_left] || [:padding_h] || [:padding] || default(:padding_left) self.parent = [:parent] @z = [:z] @tip = [:tip].dup font_name = if [:font_name].nil? or [:font_name] == :default Gosu::default_font_name else [:font_name].dup end @font = [:font] || Gosu::Font[font_name, [:font_height]] @rect = Chingu::Rect.new([:x], [:y], [:width] || 0, [:height] || 0) end |
Instance Attribute Details
#align_h ⇒ Object (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 |
#align_v ⇒ Object (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 |
#background_color ⇒ Object
Returns the value of attribute background_color.
48 49 50 |
# File 'lib/fidgit/elements/element.rb', line 48 def background_color @background_color end |
#border_thickness ⇒ Object (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 |
#font ⇒ Object
Returns the value of attribute font.
45 46 47 |
# File 'lib/fidgit/elements/element.rb', line 45 def font @font end |
#padding_bottom ⇒ Object (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 |
#padding_left ⇒ Object (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 |
#padding_right ⇒ Object (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 |
#padding_top ⇒ Object (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 |
#parent ⇒ Object
Returns the value of attribute parent.
45 46 47 |
# File 'lib/fidgit/elements/element.rb', line 45 def parent @parent end |
#tip ⇒ Object (readonly)
Returns the value of attribute tip.
45 46 47 |
# File 'lib/fidgit/elements/element.rb', line 45 def tip @tip end |
#z ⇒ Object (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
.new(*args, &block) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/fidgit/elements/element.rb', line 92 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 |
.original_new ⇒ Object
90 |
# File 'lib/fidgit/elements/element.rb', line 90 alias_method :original_new, :new |
.schema ⇒ Object
87 |
# File 'lib/fidgit/elements/element.rb', line 87 def self.schema; @@schema ||= Schema.new(YAML.load(File.read(DEFAULT_SCHEMA_FILE)));; end |
Instance Method Details
#default(*names) ⇒ Object
Get the default value from the schema.
103 104 105 |
# File 'lib/fidgit/elements/element.rb', line 103 def default(*names) self.class.schema.default(self.class, names) end |
#drag?(button) ⇒ Boolean
Can the object be dragged?
73 |
# File 'lib/fidgit/elements/element.rb', line 73 def drag?(); false; end |
#draw ⇒ Object
Redraw the element.
221 222 223 224 225 226 |
# File 'lib/fidgit/elements/element.rb', line 221 def draw draw_background draw_border draw_foreground nil end |
#draw_frame(*args) ⇒ Object
237 238 239 |
# File 'lib/fidgit/elements/element.rb', line 237 def draw_frame(*args) $window.current_game_state.draw_frame(*args) end |
#draw_rect(*args) ⇒ Object
233 234 235 |
# File 'lib/fidgit/elements/element.rb', line 233 def draw_rect(*args) $window.current_game_state.draw_rect(*args) end |
#enabled=(value) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/fidgit/elements/element.rb', line 77 def enabled=(value) if @mouse_over and enabled? and not value $window.current_game_state.unset_mouse_over end @enabled = value end |
#enabled? ⇒ Boolean
75 |
# File 'lib/fidgit/elements/element.rb', line 75 def enabled?; @enabled; end |
#height ⇒ Object
Height not including border.
65 |
# File 'lib/fidgit/elements/element.rb', line 65 def height; rect.height; end |
#height=(value) ⇒ Object
66 |
# File 'lib/fidgit/elements/element.rb', line 66 def height=(value); rect.height = [[value, @height_range.max].min, @height_range.min].max; end |
#hit?(x, y) ⇒ Boolean
Check if a point (screen coordinates) is over the element.
216 217 218 |
# File 'lib/fidgit/elements/element.rb', line 216 def hit?(x, y) @rect.collide_point?(x, y) end |
#max_height ⇒ Object
68 |
# File 'lib/fidgit/elements/element.rb', line 68 def max_height; @height_range.max; end |
#max_width ⇒ Object
60 |
# File 'lib/fidgit/elements/element.rb', line 60 def max_width; @width_range.max; end |
#min_height ⇒ Object
67 |
# File 'lib/fidgit/elements/element.rb', line 67 def min_height; @height_range.min; end |
#min_width ⇒ Object
59 |
# File 'lib/fidgit/elements/element.rb', line 59 def min_width; @width_range.min; end |
#outer_height ⇒ Object
Height including border thickness.
70 |
# File 'lib/fidgit/elements/element.rb', line 70 def outer_height; rect.height + @border_thickness * 2; end |
#outer_width ⇒ Object
Width including border thickness.
62 |
# File 'lib/fidgit/elements/element.rb', line 62 def outer_width; rect.width + @border_thickness * 2; end |
#recalc ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/fidgit/elements/element.rb', line 207 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 ⇒ Object
293 294 295 |
# File 'lib/fidgit/elements/element.rb', line 293 def to_s "#{self.class} (#{x}, #{y}) #{width}x#{height}" end |
#update ⇒ Object
Update the element.
229 230 231 |
# File 'lib/fidgit/elements/element.rb', line 229 def update nil end |
#width ⇒ Object
Width not including border.
57 |
# File 'lib/fidgit/elements/element.rb', line 57 def width; rect.width; end |
#width=(value) ⇒ Object
58 |
# File 'lib/fidgit/elements/element.rb', line 58 def width=(value); rect.width = [[value, @width_range.max].min, @width_range.min].max; end |
#with(&block) ⇒ Object
Evaluate a block, just like it was a constructor block.
274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/fidgit/elements/element.rb', line 274 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 ⇒ Object
50 |
# File 'lib/fidgit/elements/element.rb', line 50 def x; rect.x; end |
#x=(value) ⇒ Object
51 |
# File 'lib/fidgit/elements/element.rb', line 51 def x=(value); rect.x = value; end |
#y ⇒ Object
53 |
# File 'lib/fidgit/elements/element.rb', line 53 def y; rect.y; end |
#y=(value) ⇒ Object
54 |
# File 'lib/fidgit/elements/element.rb', line 54 def y=(value); rect.y = value; end |