diff --git a/lib/cyberarm_engine/ui/container.rb b/lib/cyberarm_engine/ui/container.rb index c2467d5..f065966 100644 --- a/lib/cyberarm_engine/ui/container.rb +++ b/lib/cyberarm_engine/ui/container.rb @@ -15,12 +15,9 @@ module CyberarmEngine @text_color = options[:color] @children = [] - - @theme = {} end def build - @theme.merge(@parent.theme) if @parent @block.call(self) if @block recalculate @@ -40,14 +37,6 @@ module CyberarmEngine @children.each(&:update) end - def theme - @theme - end - - def color(color) - @theme[:color] = color - end - def hit_element?(x, y) @children.reverse_each do |child| case child @@ -66,6 +55,7 @@ module CyberarmEngine def recalculate @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top) return unless visible? + stylize layout @@ -76,9 +66,10 @@ module CyberarmEngine @children.each do |child| child.x += @x child.y += @y - - # Fix child being displaced + + child.stylize child.recalculate + child.reposition # TODO: Implement top,bottom,left,center, and right positioning end update_background diff --git a/lib/cyberarm_engine/ui/dsl.rb b/lib/cyberarm_engine/ui/dsl.rb index dfec3ee..ece9558 100644 --- a/lib/cyberarm_engine/ui/dsl.rb +++ b/lib/cyberarm_engine/ui/dsl.rb @@ -2,7 +2,7 @@ module CyberarmEngine module DSL def flow(options = {}, &block) options[:parent] = @containers.last - options[:theme] = @current_theme + options[:theme] = current_theme _container = Flow.new(options, block) @containers << _container _container.build @@ -14,7 +14,7 @@ module CyberarmEngine def stack(options = {}, &block) options[:parent] = @containers.last - options[:theme] = @current_theme + options[:theme] = current_theme _container = Stack.new(options, block) @containers << _container _container.build @@ -79,16 +79,11 @@ module CyberarmEngine end def background(color = Gosu::Color::NONE) - @containers.last.background = color - end - - # Foreground color, e.g. Text - def color(color) - @containers.last.color(color) + @containers.last.style.background = color end def theme(theme) - @current_theme = theme + @containers.last.options[:theme] = theme end def current_theme diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index 9903dae..dbd8bd6 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -13,43 +13,41 @@ module CyberarmEngine @options = options @block = block - @style = Style.new(options) - @focus = false - @style.background_canvas = Background.new - @style.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 - @style.width = default(:width) || $window.width - @style.height = default(:height) || $window.height - - set_border_thickness(default(:border_thickness)) - - set_padding(default(:padding)) - - set_margin(default(:margin)) - - set_background(default(:background)) - set_border_color(default(:border_color)) - - 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} 'options' must be a Hash" unless @options.is_a?(Hash) - - # raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric) - - @enabled = true - @visible = true + stylize default_events end + def stylize + @style.width = @style.width || $window.width + @style.height = @style.height || $window.height + + set_border_thickness(@style.border_thickness) + + set_padding(@style.padding) + + set_margin(@style.margin) + + @style.background_canvas = Background.new + @style.border_canvas = BorderCanvas.new(element: self) + + set_background(@style.background) + set_border_color(@style.border_color) + end + def set_background(background) @style.background = background @style.background_canvas.background = background @@ -221,7 +219,6 @@ module CyberarmEngine end def reposition - raise "#{self.class}#reposition was not overridden!" end def value diff --git a/lib/cyberarm_engine/ui/style.rb b/lib/cyberarm_engine/ui/style.rb index f6bdb6e..4b63078 100644 --- a/lib/cyberarm_engine/ui/style.rb +++ b/lib/cyberarm_engine/ui/style.rb @@ -1,6 +1,6 @@ module CyberarmEngine class Style - def initialize(hash) + def initialize(hash = {}) @hash = hash end diff --git a/lib/cyberarm_engine/ui/theme.rb b/lib/cyberarm_engine/ui/theme.rb index 2c22c70..b79707f 100644 --- a/lib/cyberarm_engine/ui/theme.rb +++ b/lib/cyberarm_engine/ui/theme.rb @@ -13,7 +13,7 @@ module CyberarmEngine raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element) _theme = THEME _theme = _theme.merge(options[:theme]) if options[:theme] - options.delete(:theme) + _theme.delete(:theme) if options[:theme] hash = {} class_names = self.class.ancestors