Added partial support for fixed element width/height and added support for dynamic width/height (currently only works on Containers)

This commit is contained in:
2019-06-25 14:56:41 -05:00
parent 2fe8e6042b
commit a972cfac98
5 changed files with 53 additions and 28 deletions

View File

@@ -31,8 +31,10 @@ module CyberarmEngine
end end
def render def render
Gosu.clip_to(@x, @y, width, height) do
@children.each(&:draw) @children.each(&:draw)
end end
end
def update def update
@children.each(&:update) @children.each(&:update)
@@ -61,13 +63,16 @@ module CyberarmEngine
layout layout
if is_root? if is_root?
@style.width = window.width @width = @style.width = window.width
@style.height = window.height @height = @style.height = window.height
else else
@style.width = @max_width ? @max_width : (@children.map {|c| c.x + c.outer_width }.max || 0).round _width = dimensional_size(@style.width, :width)
@style.height = @max_height ? @max_height : (@children.map {|c| c.y + c.outer_height}.max || 0).round _height= dimensional_size(@style.height,:height)
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
end end
# Move child to parent after positioning # Move child to parent after positioning
@children.each do |child| @children.each do |child|
child.x += @x child.x += @x

View File

@@ -79,7 +79,7 @@ module CyberarmEngine
def recalculate def recalculate
super super
@style.width = default(:width) @width = dimensional_size(@style.width, :width) || default(:width)
update_background update_background
end end

View File

@@ -23,11 +23,14 @@ module CyberarmEngine
@y = @style.y @y = @style.y
@z = @style.z @z = @style.z
@width = 0
@height = 0
@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 @style.width = default(:width) || nil
@style.height = default(:height) || $window.height @style.height = default(:height) || nil
stylize stylize
@@ -159,7 +162,7 @@ module CyberarmEngine
def width def width
if visible? if visible?
(@style.border_thickness_left + @style.padding_left) + @style.width + (@style.padding_right + @style.border_thickness_right) (@style.border_thickness_left + @style.padding_left) + @width + (@style.padding_right + @style.border_thickness_right)
else else
0 0
end end
@@ -171,7 +174,7 @@ module CyberarmEngine
def height def height
if visible? if visible?
(@style.border_thickness_top + @style.padding_top) + @style.height + (@style.padding_bottom + @style.border_thickness_bottom) (@style.border_thickness_top + @style.padding_top) + @height + (@style.padding_bottom + @style.border_thickness_bottom)
else else
0 0
end end
@@ -181,6 +184,19 @@ module CyberarmEngine
@style.margin_top + height + @style.margin_bottom @style.margin_top + height + @style.margin_bottom
end end
private def dimensional_size(size, dimension)
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
if size && size.is_a?(Numeric)
if size.between?(0.0, 1.0)
@parent.send(:"#{dimension}") * size
else
size
end
else
nil
end
end
def background=(_background) def background=(_background)
@style.background_canvas.background=(_background) @style.background_canvas.background=(_background)
update_background update_background

View File

@@ -5,23 +5,9 @@ module CyberarmEngine
@path = path @path = path
@image = Gosu::Image.new(path, retro: @options[:image_retro]) @image = Gosu::Image.new(path, retro: @options[:image_retro])
if @options[:width] && @options[:height]
@scale_x = @options[:width].to_f / @image.width
@scale_y = @options[:height].to_f / @image.height
elsif @options[:width]
@scale_x = @options[:width].to_f / @image.width
@scale_y = @scale_x
elsif @options[:height]
@scale_y = @options[:height].to_f / @image.height
@scale_x = @scale_y
else
@scale_x, @scale_y = 1, 1 @scale_x, @scale_y = 1, 1
end end
raise "Scale X" unless @scale_x.is_a?(Numeric)
raise "Scale Y" unless @scale_y.is_a?(Numeric)
end
def render def render
@image.draw( @image.draw(
@style.border_thickness_left + @style.padding_left + @x, @style.border_thickness_left + @style.padding_left + @x,
@@ -35,8 +21,24 @@ module CyberarmEngine
end end
def recalculate def recalculate
@style.width = @image.width * @scale_x _width = dimensional_size(@style.width, :width)
@style.height = @image.height * @scale_y _height= dimensional_size(@style.height,:height)
if _width && _height
@scale_x = _width.to_f / @image.width
@scale_y = _height.to_f / @image.height
elsif _width
@scale_x = _width.to_f / @image.width
@scale_y = @scale_x
elsif _height
@scale_y = _height.to_f / @image.height
@scale_x = @scale_y
else
@scale_x, @scale_y = 1, 1
end
@width = _width ? _width : @image.width.round * @scale_x
@height= _height ? _height : @image.height.round * @scale_y
end end
def value def value

View File

@@ -17,8 +17,10 @@ module CyberarmEngine
end end
def recalculate def recalculate
@style.width = @text.width.round _width = dimensional_size(@style.width, :width)
@style.height= @text.height.round _height= dimensional_size(@style.height,:height)
@width = _width ? _width : @text.width.round
@height= _height ? _height : @text.height.round
@text.x = @style.border_thickness_left + @style.padding_left + @x @text.x = @style.border_thickness_left + @style.padding_left + @x
@text.y = @style.border_thickness_top + @style.padding_top + @y @text.y = @style.border_thickness_top + @style.padding_top + @y