Improved flow behavior, renamed stroke to color, added outer_width and outer_height to elements, added setters for width, and height.

This commit is contained in:
2019-03-05 10:46:48 -06:00
parent 7eae43d566
commit 76c81ee7a9
6 changed files with 53 additions and 34 deletions

View File

@@ -44,12 +44,8 @@ module CyberarmEngine
@theme @theme
end end
def stroke(color) def color(color)
@theme[:stroke] = color @theme[:color] = color
end
def fill(color)
@theme[:fill] = color
end end
def hit_element?(x, y) def hit_element?(x, y)
@@ -73,7 +69,7 @@ module CyberarmEngine
layout layout
@width = @max_width ? @max_width : (@children.map {|c| c.x + c.width + c.margin_right }.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_top }.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 # Move child to parent after positioning
@children.each do |child| @children.each do |child|
@@ -95,27 +91,50 @@ module CyberarmEngine
@max_width ? @max_width : window.width - (@parent ? @parent.margin_right + @margin_right : @margin_right) @max_width ? @max_width : window.width - (@parent ? @parent.margin_right + @margin_right : @margin_right)
end end
def fits_on_line?(element) def fits_on_line?(element) # Flow
@current_position.x + element.margin_left + element.width + element.margin_right <= max_width @current_position.x + element.outer_width <= max_width &&
@current_position.x + element.outer_width <= window.width
end end
def position_on_current_line(element) def position_on_current_line(element) # Flow
element.x = element.margin_left + @current_position.x element.x = element.margin_left + @current_position.x
element.y = element.margin_top + @current_position.y element.y = element.margin_top + @current_position.y
element.recalculate element.recalculate
@current_position.x += element.width + element.margin_right @current_position.x += element.outer_width
@current_position.x = @margin_left + @x if @current_position.x >= max_width @current_position.x = @margin_left if @current_position.x >= max_width
end 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.x = element.margin_left + @current_position.x
element.y = element.margin_top + @current_position.y element.y = element.margin_top + @current_position.y
element.recalculate element.recalculate
@current_position.y += element.height + element.margin_bottom @current_position.y += element.outer_height
end end
# def mouse_wheel_up(sender, x, y) # def mouse_wheel_up(sender, x, y)

View File

@@ -75,13 +75,8 @@ module CyberarmEngine
end end
# Foreground color, e.g. Text # Foreground color, e.g. Text
def stroke(color) def color(color)
@containers.last.stroke(color) @containers.last.color(color)
end
# Element background color
def fill(color)
@containers.last.fill(color)
end end
end end
end end

View File

@@ -20,9 +20,11 @@ module CyberarmEngine
end end
def render def render
Gosu.clip_to(@text.x, @text.y, @width, @text.height) do
draw_text draw_text
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
end end
end
def update def update
if @type == :password if @type == :password

View File

@@ -4,8 +4,8 @@ module CyberarmEngine
include Event include Event
include Common include Common
attr_accessor :x, :y, :z, :width, :height, :enabled attr_accessor :x, :y, :z, :enabled
attr_reader :parent, :options, :event_handler, :background_canvas, :border_canvas 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_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 :border_color, :border_color_left, :border_color_right, :border_color_top, :border_color_bottom
@@ -58,6 +58,9 @@ module CyberarmEngine
default_events default_events
end end
def width=(n); @max_width = n; end
def height=(n); @max_height = n; end
def set_background(background) def set_background(background)
@background = background @background = background
@background_canvas.background = background @background_canvas.background = background
@@ -150,10 +153,18 @@ module CyberarmEngine
(@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right) (@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right)
end end
def outer_width
@margin_left + width + @margin_right
end
def height def height
(@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom) (@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom)
end end
def outer_height
@margin_top + height + @margin_bottom
end
def background=(_background) def background=(_background)
@background_canvas.background=(_background) @background_canvas.background=(_background)
update_background update_background

View File

@@ -7,15 +7,7 @@ module CyberarmEngine
if fits_on_line?(child) if fits_on_line?(child)
position_on_current_line(child) position_on_current_line(child)
else else
@current_position.x = @margin_left + @x position_on_next_line(child)
@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
end end
end end
end end

View File

@@ -3,7 +3,7 @@ module CyberarmEngine
def initialize(text, options = {}, block = nil) def initialize(text, options = {}, block = nil)
super(options, block) 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 return self
end end