lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.7.1 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.8.0
- old
+ new
@@ -3,104 +3,94 @@
include Theme
include Event
include Common
attr_accessor :x, :y, :z, :enabled
- attr_reader :width, :height, :parent, :options, :event_handler, :background_canvas, :border_canvas
+ attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
- attr_reader :border_thickness, :border_thickness_left, :border_thickness_right, :border_thickness_top, :border_thickness_bottom
- attr_reader :border_color, :border_color_left, :border_color_right, :border_color_top, :border_color_bottom
-
- attr_reader :padding, :padding_left, :padding_right, :padding_top, :padding_bottom
- attr_reader :margin, :margin_left, :margin_right, :margin_top, :margin_bottom
-
def initialize(options = {}, block = nil)
@parent = options[:parent] # parent Container (i.e. flow/stack)
options = theme_defaults(options)
@options = options
@block = block
- @style = Style.new(options)
- @focus = false
- @background_canvas = Background.new
- @border_canvas = BorderCanvas.new(element: self)
+ @focus = false
+ @enabled = true
+ @visible = true
- @x = default(:x)
- @y = default(:y)
- @z = default(:z)
+ @style = Style.new(options)
+ @x = @style.x
+ @y = @style.y
+ @z = @style.z
+
@fixed_x = @x if @x != 0
@fixed_y = @y if @y != 0
- @width = default(:width) || $window.width
- @height = default(:height) || $window.height
+ stylize
- set_border_thickness(default(:border_thickness))
+ default_events
+ end
- set_padding(default(:padding))
+ def stylize
+ @style.width = @style.width || $window.width
+ @style.height = @style.height || $window.height
- set_margin(default(:margin))
+ set_border_thickness(@style.border_thickness)
- set_background(default(:background))
- set_border_color(default(:border_color))
+ set_padding(@style.padding)
- raise "#{self.class} 'x' must be a number" unless @x.is_a?(Numeric)
- raise "#{self.class} 'y' must be a number" unless @y.is_a?(Numeric)
- raise "#{self.class} 'z' must be a number" unless @z.is_a?(Numeric)
- raise "#{self.class} 'width' must be a number" unless @width.is_a?(Numeric) || @width.nil?
- raise "#{self.class} 'height' must be a number" unless @height.is_a?(Numeric) || @height.nil?
- raise "#{self.class} 'options' must be a Hash" unless @options.is_a?(Hash)
+ set_margin(@style.margin)
- # raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric)
+ @style.background_canvas = Background.new
+ @style.border_canvas = BorderCanvas.new(element: self)
- @enabled = true
- @visible = true
-
- default_events
+ set_background(@style.background)
+ set_border_color(@style.border_color)
end
def set_background(background)
- @background = background
- @background_canvas.background = background
+ @style.background = background
+ @style.background_canvas.background = background
end
def set_border_thickness(border_thickness)
- @border_thickness = border_thickness
+ @style.border_thickness = border_thickness
- @border_thickness_left = default(:border_thickness_left) || @border_thickness
- @border_thickness_right = default(:border_thickness_right) || @border_thickness
- @border_thickness_top = default(:border_thickness_top) || @border_thickness
- @border_thickness_bottom = default(:border_thickness_bottom) || @border_thickness
+ @style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
+ @style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
+ @style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
+ @style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
end
def set_border_color(color)
- @border_color = color
+ @style.border_color = color
- @border_color_left = default(:border_color_left) || @border_color
- @border_color_right = default(:border_color_right) || @border_color
- @border_color_top = default(:border_color_top) || @border_color
- @border_color_bottom = default(:border_color_bottom) || @border_color
+ @style.border_color_left = default(:border_color_left) || @style.border_color
+ @style.border_color_right = default(:border_color_right) || @style.border_color
+ @style.border_color_top = default(:border_color_top) || @style.border_color
+ @style.border_color_bottom = default(:border_color_bottom) || @style.border_color
- @border_canvas.color = color
+ @style.border_canvas.color = color
end
def set_padding(padding)
- @padding = padding
+ @style.padding = padding
- @padding_left = default(:padding_left) || @padding
- @padding_right = default(:padding_right) || @padding
- @padding_top = default(:padding_top) || @padding
- @padding_bottom = default(:padding_bottom) || @padding
+ @style.padding_left = default(:padding_left) || @style.padding
+ @style.padding_right = default(:padding_right) || @style.padding
+ @style.padding_top = default(:padding_top) || @style.padding
+ @style.padding_bottom = default(:padding_bottom) || @style.padding
end
def set_margin(margin)
- @margin = margin
+ @style.margin = margin
- @margin_left = default(:margin_left) || @margin
- @margin_right = default(:margin_right) || @margin
- @margin_top = default(:margin_top) || @margin
- @margin_bottom = default(:margin_bottom) || @margin
+ @style.margin_left = default(:margin_left) || @style.margin
+ @style.margin_right = default(:margin_right) || @style.margin
+ @style.margin_top = default(:margin_top) || @style.margin
+ @style.margin_bottom = default(:margin_bottom) || @style.margin
end
def default_events
[:left, :middle, :right].each do |button|
event(:"#{button}_mouse_button")
@@ -143,12 +133,12 @@
end
def draw
return unless @visible
- @background_canvas.draw
- @border_canvas.draw
+ @style.background_canvas.draw
+ @style.border_canvas.draw
render
end
def update
end
@@ -166,48 +156,48 @@
x.between?(@x, @x + width) &&
y.between?(@y, @y + height)
end
def width
- (@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right)
+ if visible?
+ (@style.border_thickness_left + @style.padding_left) + @style.width + (@style.padding_right + @style.border_thickness_right)
+ else
+ 0
+ end
end
def outer_width
- @margin_left + width + @margin_right
+ @style.margin_left + width + @style.margin_right
end
def height
- (@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom)
+ if visible?
+ (@style.border_thickness_top + @style.padding_top) + @style.height + (@style.padding_bottom + @style.border_thickness_bottom)
+ else
+ 0
+ end
end
def outer_height
- @margin_top + height + @margin_bottom
+ @style.margin_top + height + @style.margin_bottom
end
- def style(hash)
- if hash
- @style.set(hash)
- else
- @style.hash
- end
- end
-
def background=(_background)
- @background_canvas.background=(_background)
+ @style.background_canvas.background=(_background)
update_background
end
def update_background
- @background_canvas.x = @x
- @background_canvas.y = @y
- @background_canvas.z = @z
- @background_canvas.width = width
- @background_canvas.height = height
+ @style.background_canvas.x = @x
+ @style.background_canvas.y = @y
+ @style.background_canvas.z = @z
+ @style.background_canvas.width = width
+ @style.background_canvas.height = height
- @background_canvas.update
+ @style.background_canvas.update
- @border_canvas.update
+ @style.border_canvas.update
end
def root
unless @root && @root.parent.nil?
@root = parent
@@ -227,10 +217,9 @@
def recalculate
raise "#{self.class}#recalculate was not overridden!"
end
def reposition
- raise "#{self.class}#reposition was not overridden!"
end
def value
raise "#{self.class}#value was not overridden!"
end
\ No newline at end of file