Improved theme handling

This commit is contained in:
2019-06-21 13:53:57 -05:00
parent 8c5c5e1b7b
commit d1dec5791b
5 changed files with 36 additions and 53 deletions

View File

@@ -15,12 +15,9 @@ module CyberarmEngine
@text_color = options[:color] @text_color = options[:color]
@children = [] @children = []
@theme = {}
end end
def build def build
@theme.merge(@parent.theme) if @parent
@block.call(self) if @block @block.call(self) if @block
recalculate recalculate
@@ -40,14 +37,6 @@ module CyberarmEngine
@children.each(&:update) @children.each(&:update)
end end
def theme
@theme
end
def color(color)
@theme[:color] = color
end
def hit_element?(x, y) def hit_element?(x, y)
@children.reverse_each do |child| @children.reverse_each do |child|
case child case child
@@ -66,6 +55,7 @@ module CyberarmEngine
def recalculate def recalculate
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top) @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
return unless visible? return unless visible?
stylize
layout layout
@@ -77,8 +67,9 @@ module CyberarmEngine
child.x += @x child.x += @x
child.y += @y child.y += @y
# Fix child being displaced child.stylize
child.recalculate child.recalculate
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
end end
update_background update_background

View File

@@ -2,7 +2,7 @@ module CyberarmEngine
module DSL module DSL
def flow(options = {}, &block) def flow(options = {}, &block)
options[:parent] = @containers.last options[:parent] = @containers.last
options[:theme] = @current_theme options[:theme] = current_theme
_container = Flow.new(options, block) _container = Flow.new(options, block)
@containers << _container @containers << _container
_container.build _container.build
@@ -14,7 +14,7 @@ module CyberarmEngine
def stack(options = {}, &block) def stack(options = {}, &block)
options[:parent] = @containers.last options[:parent] = @containers.last
options[:theme] = @current_theme options[:theme] = current_theme
_container = Stack.new(options, block) _container = Stack.new(options, block)
@containers << _container @containers << _container
_container.build _container.build
@@ -79,16 +79,11 @@ module CyberarmEngine
end end
def background(color = Gosu::Color::NONE) def background(color = Gosu::Color::NONE)
@containers.last.background = color @containers.last.style.background = color
end
# Foreground color, e.g. Text
def color(color)
@containers.last.color(color)
end end
def theme(theme) def theme(theme)
@current_theme = theme @containers.last.options[:theme] = theme
end end
def current_theme def current_theme

View File

@@ -13,43 +13,41 @@ module CyberarmEngine
@options = options @options = options
@block = block @block = block
@style = Style.new(options)
@focus = false @focus = false
@style.background_canvas = Background.new @enabled = true
@style.border_canvas = BorderCanvas.new(element: self) @visible = true
@x = default(:x) @style = Style.new(options)
@y = default(:y)
@z = default(:z) @x = @style.x
@y = @style.y
@z = @style.z
@fixed_x = @x if @x != 0 @fixed_x = @x if @x != 0
@fixed_y = @y if @y != 0 @fixed_y = @y if @y != 0
@style.width = default(:width) || $window.width stylize
@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
default_events default_events
end 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) def set_background(background)
@style.background = background @style.background = background
@style.background_canvas.background = background @style.background_canvas.background = background
@@ -221,7 +219,6 @@ module CyberarmEngine
end end
def reposition def reposition
raise "#{self.class}#reposition was not overridden!"
end end
def value def value

View File

@@ -1,6 +1,6 @@
module CyberarmEngine module CyberarmEngine
class Style class Style
def initialize(hash) def initialize(hash = {})
@hash = hash @hash = hash
end end

View File

@@ -13,7 +13,7 @@ module CyberarmEngine
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element) raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
_theme = THEME _theme = THEME
_theme = _theme.merge(options[:theme]) if options[:theme] _theme = _theme.merge(options[:theme]) if options[:theme]
options.delete(:theme) _theme.delete(:theme) if options[:theme]
hash = {} hash = {}
class_names = self.class.ancestors class_names = self.class.ancestors