From 76c81ee7a9a9fd304b69cb16c399c891edb00b86 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 5 Mar 2019 10:46:48 -0600 Subject: [PATCH] Improved flow behavior, renamed stroke to color, added outer_width and outer_height to elements, added setters for width, and height. --- lib/cyberarm_engine/ui/container.rb | 49 ++++++++++++++++++++--------- lib/cyberarm_engine/ui/dsl.rb | 9 ++---- lib/cyberarm_engine/ui/edit_line.rb | 2 ++ lib/cyberarm_engine/ui/element.rb | 15 +++++++-- lib/cyberarm_engine/ui/flow.rb | 10 +----- lib/cyberarm_engine/ui/label.rb | 2 +- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/lib/cyberarm_engine/ui/container.rb b/lib/cyberarm_engine/ui/container.rb index 711c733..9b30495 100644 --- a/lib/cyberarm_engine/ui/container.rb +++ b/lib/cyberarm_engine/ui/container.rb @@ -44,12 +44,8 @@ module CyberarmEngine @theme end - def stroke(color) - @theme[:stroke] = color - end - - def fill(color) - @theme[:fill] = color + def color(color) + @theme[:color] = color end def hit_element?(x, y) @@ -72,8 +68,8 @@ module CyberarmEngine layout - @width = @max_width ? @max_width : (@children.map {|c| c.x + c.width + c.margin_right}.max || 0).round - @height = @max_height ? @max_height : (@children.map {|c| c.y + c.height+ c.margin_top }.max || 0).round + @width = @max_width ? @max_width : (@children.map {|c| c.x + c.width + c.margin_right }.max || 0).round + @height = @max_height ? @max_height : (@children.map {|c| c.y + c.height + c.margin_bottom}.max || 0).round # Move child to parent after positioning @children.each do |child| @@ -95,27 +91,50 @@ module CyberarmEngine @max_width ? @max_width : window.width - (@parent ? @parent.margin_right + @margin_right : @margin_right) end - def fits_on_line?(element) - @current_position.x + element.margin_left + element.width + element.margin_right <= max_width + def fits_on_line?(element) # Flow + @current_position.x + element.outer_width <= max_width && + @current_position.x + element.outer_width <= window.width end - def position_on_current_line(element) + def position_on_current_line(element) # Flow element.x = element.margin_left + @current_position.x element.y = element.margin_top + @current_position.y element.recalculate - @current_position.x += element.width + element.margin_right - @current_position.x = @margin_left + @x if @current_position.x >= max_width + @current_position.x += element.outer_width + @current_position.x = @margin_left if @current_position.x >= max_width end - def move_to_next_line(element) + def tallest_neighbor(querier, y_position) # Flow + response = querier + @children.each do |child| + response = child if child.outer_height > response.outer_height + break if child == querier + end + + return response + end + + def position_on_next_line(child) # Flow + @current_position.x = @margin_left + @current_position.y += tallest_neighbor(child, @current_position.y).outer_height + + child.x = child.margin_left + @current_position.x + child.y = child.margin_top + @current_position.y + + child.recalculate + + @current_position.x += child.outer_width + end + + def move_to_next_line(element) # Stack element.x = element.margin_left + @current_position.x element.y = element.margin_top + @current_position.y element.recalculate - @current_position.y += element.height + element.margin_bottom + @current_position.y += element.outer_height end # def mouse_wheel_up(sender, x, y) diff --git a/lib/cyberarm_engine/ui/dsl.rb b/lib/cyberarm_engine/ui/dsl.rb index b7b9cf7..d811968 100644 --- a/lib/cyberarm_engine/ui/dsl.rb +++ b/lib/cyberarm_engine/ui/dsl.rb @@ -75,13 +75,8 @@ module CyberarmEngine end # Foreground color, e.g. Text - def stroke(color) - @containers.last.stroke(color) - end - - # Element background color - def fill(color) - @containers.last.fill(color) + def color(color) + @containers.last.color(color) end end end \ No newline at end of file diff --git a/lib/cyberarm_engine/ui/edit_line.rb b/lib/cyberarm_engine/ui/edit_line.rb index d1a04d1..b28c8ba 100644 --- a/lib/cyberarm_engine/ui/edit_line.rb +++ b/lib/cyberarm_engine/ui/edit_line.rb @@ -20,8 +20,10 @@ module CyberarmEngine end def render + Gosu.clip_to(@text.x, @text.y, @width, @text.height) do draw_text Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret + end end def update diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index a3610cd..e3fca11 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -4,8 +4,8 @@ module CyberarmEngine include Event include Common - attr_accessor :x, :y, :z, :width, :height, :enabled - attr_reader :parent, :options, :event_handler, :background_canvas, :border_canvas + attr_accessor :x, :y, :z, :enabled + attr_reader :width, :height, :parent, :options, :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 @@ -58,6 +58,9 @@ module CyberarmEngine default_events end + def width=(n); @max_width = n; end + def height=(n); @max_height = n; end + def set_background(background) @background = background @background_canvas.background = background @@ -150,10 +153,18 @@ module CyberarmEngine (@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right) end + def outer_width + @margin_left + width + @margin_right + end + def height (@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom) end + def outer_height + @margin_top + height + @margin_bottom + end + def background=(_background) @background_canvas.background=(_background) update_background diff --git a/lib/cyberarm_engine/ui/flow.rb b/lib/cyberarm_engine/ui/flow.rb index 7ad647c..732c77c 100644 --- a/lib/cyberarm_engine/ui/flow.rb +++ b/lib/cyberarm_engine/ui/flow.rb @@ -7,15 +7,7 @@ module CyberarmEngine if fits_on_line?(child) position_on_current_line(child) else - @current_position.x = @margin_left + @x - @current_position.y += child.height + child.margin_bottom - - child.x = element.margin_left + @current_position.x - child.y = element.margin_top + @current_position.y - - child.recalculate - - @current_position.x += child.width + child.margin_right + position_on_next_line(child) end end end diff --git a/lib/cyberarm_engine/ui/label.rb b/lib/cyberarm_engine/ui/label.rb index 468beb8..36dd0e5 100644 --- a/lib/cyberarm_engine/ui/label.rb +++ b/lib/cyberarm_engine/ui/label.rb @@ -3,7 +3,7 @@ module CyberarmEngine def initialize(text, options = {}, block = nil) super(options, block) - @text = Text.new(text, font: @options[:font], z: @z, color: @options[:stroke], size: @options[:text_size], shadow: @options[:text_shadow]) + @text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow]) return self end