Major refactor of Container and elements to support a more Shoes-like construction of menus (in complete)

This commit is contained in:
2019-01-31 17:33:05 -06:00
parent 76ac1d553a
commit 5188532ad0
12 changed files with 328 additions and 275 deletions

View File

@@ -5,13 +5,20 @@ require_relative "cyberarm_engine/version"
require_relative "cyberarm_engine/common" require_relative "cyberarm_engine/common"
require_relative "cyberarm_engine/game_object" require_relative "cyberarm_engine/game_object"
require_relative "cyberarm_engine/game_state"
require_relative "cyberarm_engine/engine" require_relative "cyberarm_engine/engine"
require_relative "cyberarm_engine/objects/text" require_relative "cyberarm_engine/objects/text"
require_relative "cyberarm_engine/objects/multi_line_text" require_relative "cyberarm_engine/objects/multi_line_text"
require_relative "cyberarm_engine/ui/element"
require_relative "cyberarm_engine/ui/label"
require_relative "cyberarm_engine/ui/button" require_relative "cyberarm_engine/ui/button"
require_relative "cyberarm_engine/ui/check_box" require_relative "cyberarm_engine/ui/check_box"
require_relative "cyberarm_engine/ui/input" require_relative "cyberarm_engine/ui/edit_line"
require_relative "cyberarm_engine/ui/container" require_relative "cyberarm_engine/ui/container"
require_relative "cyberarm_engine/ui/flow"
require_relative "cyberarm_engine/ui/stack"
require_relative "cyberarm_engine/ui/dsl"
require_relative "cyberarm_engine/game_state"

View File

@@ -1,17 +1,19 @@
module CyberarmEngine module CyberarmEngine
class GameState class GameState
include Common include Common
include DSL
SCALE_X_BASE = 1920.0 attr_accessor :options, :global_pause, :active_container, :active_grid
SCALE_Y_BASE = 1080.0
attr_accessor :options, :global_pause
attr_reader :game_objects attr_reader :game_objects
def initialize(options={}) def initialize(options={})
@options = options unless @options @options = options
@game_objects = [] @game_objects = []
@global_pause = false @global_pause = false
@active_container = nil
@active_grid = nil
setup setup
end end
@@ -19,18 +21,11 @@ module CyberarmEngine
end end
def draw def draw
@game_objects.each do |o| @game_objects.each(&:draw)
o.draw if o.visible
end
end end
def update def update
@game_objects.each do |o| @game_objects.each(&:update)
unless o.paused || @global_pause
o.update
o.update_debug_text if $debug
end
end
end end
def destroy def destroy
@@ -40,7 +35,7 @@ module CyberarmEngine
def button_up(id) def button_up(id)
@game_objects.each do |o| @game_objects.each do |o|
o.button_up(id) unless o.paused o.button_up(id)
end end
end end

View File

@@ -74,20 +74,21 @@ module CyberarmEngine
if @shadow && !ARGV.join.include?("--no-shadow") if @shadow && !ARGV.join.include?("--no-shadow")
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) if @shadow_alpha <= @color.alpha _color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) if @shadow_alpha <= @color.alpha
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @color.alpha) unless @shadow_alpha <= @color.alpha _color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @color.alpha) unless @shadow_alpha <= @color.alpha
@textobject.draw(@text, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color) @textobject.draw_markup(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
@textobject.draw_markup(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color)
@textobject.draw_markup(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
end end
@textobject.draw(@text, @x, @y, @z, @factor_x, @factor_y, @color) @textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
end end
def update; end def update; end

View File

@@ -1,122 +1,48 @@
module CyberarmEngine module CyberarmEngine
BUTTON_TEXT_COLOR = Gosu::Color::WHITE class Button < Element
BUTTON_TEXT_ACTIVE_COLOR = Gosu::Color::BLACK def initialize(text, options = {}, block = nil)
BUTTON_COLOR = Gosu::Color.rgb(12,12,12) super(options, block)
BUTTON_HOVER_COLOR = Gosu::Color.rgb(100, 100, 100)
BUTTON_ACTIVE_COLOR = Gosu::Color.rgb(50, 50, 50)
BUTTON_TEXT_SIZE = 20
BUTTON_PADDING = 10
class Button @text = Text.new(text, font: @options[:font], color: @options[:interactive_stroke], size: @options[:text_size], shadow: @options[:text_shadow])
attr_accessor :text, :x, :y, :offset_x, :offset_y, :tooltip, :block
def initialize(text, x, y, auto_manage = true, tooltip = "", &block)
@text = Text.new(text, x: x, y: y, size: BUTTON_TEXT_SIZE, color: BUTTON_TEXT_COLOR, shadow: true)
@tooltip=Text.new(tooltip, x: x, y: y-(height/4*3), z: 10_000, size: BUTTON_TEXT_SIZE, color: BUTTON_TEXT_COLOR, shadow: false)
@x = x
@y = y
_x_ = @x+(@text.textobject.text_width(@text.text)/2)-(@tooltip.textobject.text_width(@tooltip.text)/2)
@tooltip.x = _x_+BUTTON_PADDING
auto_adjust_tooltip_position
@offset_x, @offset_y = 0, 0
if block
@block = Proc.new{yield(self)}
else
@block = Proc.new {}
end
Window.instance.elements.push(self) if auto_manage
return self return self
end end
def update_position_toolip
_x_ = @x+(@text.textobject.text_width(@text.text)/2)-(@tooltip.textobject.text_width(@tooltip.text)/2)
@tooltip.x = _x_+BUTTON_PADDING
auto_adjust_tooltip_position
end
def auto_adjust_tooltip_position
if @tooltip.x <= 1
@tooltip.x = 2
elsif @tooltip.x+@tooltip.textobject.text_width(@tooltip.text) > $window.width-(BUTTON_PADDING+1)
@tooltip.x = $window.width-@tooltip.textobject.text_width(@tooltip.text)
end
end
def draw def draw
@text.draw @text.draw
$window.draw_rect(@x, @y, width, height, BUTTON_COLOR) $window.draw_rect(@x, @y, width, height, @options[:background], @z+1)
if mouse_clicked_on_check if mouse_over? && $window.button_down?(Gosu::MsLeft)
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_ACTIVE_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_active_background], @z+2)
elsif mouse_over? elsif mouse_over?
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_HOVER_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_hover_background], @z+2)
show_tooltip # show_tooltip
else else
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_background], @z+2)
end end
end
def update
@text.x = @x+BUTTON_PADDING
@text.y = @y+BUTTON_PADDING
end end
def button_up(id) def button_up(id)
case id case id
when Gosu::MsLeft when Gosu::MsLeft
click_check
end
end
def click_check
if mouse_over? if mouse_over?
puts "Clicked: #{@text.text}" @block.call(self) if @block
@block.call if @block.is_a?(Proc)
end
end
def mouse_clicked_on_check
if mouse_over? && Gosu.button_down?(Gosu::MsLeft)
true
end
end
def mouse_over?
if $window.mouse_x.between?(@x+@offset_x, @x+@offset_x+width)
if $window.mouse_y.between?(@y+@offset_y, @y+@offset_y+height)
true
end end
end end
end end
def show_tooltip def recalculate
if @tooltip.text != "" @width = @text.width
x = @tooltip.x-BUTTON_PADDING @height= @text.height
$window.draw_rect(x, @y-height, width(@tooltip), height(@tooltip), BUTTON_ACTIVE_COLOR, 9_999) @text.x = @x + @padding
$window.draw_rect(x-1, @y-height-1, width(@tooltip)+2, height(@tooltip)+2, Gosu::Color::WHITE, 9_998) @text.y = @y + @padding
@tooltip.draw @text.z = @z + 3
end
end end
def width(text_object = @text) def value
text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2 @text.text
end
def height(text_object = @text)
text_object.textobject.height+BUTTON_PADDING*2
end
def set_offset(x, y)
@offset_x, @offset_y = x, y
end
def update_text(string)
@text.text = string
end end
end end
end end

View File

@@ -1,47 +1,28 @@
module CyberarmEngine module CyberarmEngine
class CheckBox class CheckBox < Element
SIZE = 22 def initialize(options, block = nil)
super(options, block)
@checked = options[:checked] || false
attr_accessor :x, :y, :checked @text = Text.new("X", font: @options[:font], color: @options[:interactive_stroke], size: @options[:text_size], shadow: @options[:text_shadow])
attr_reader :text
def initialize(x, y, checked = false, size = CheckBox::SIZE)
@x, @y = x, y
@checked = checked
@size = size
@text = Text.new("", x: x, y: y, size: size, color: BUTTON_TEXT_COLOR, shadow: true)
return self return self
end end
def x=(int)
@x = int
@text.x = int
end
def y=(int)
@y = int
@text.y = int
end
def draw def draw
$window.draw_rect(@x, @y, width, height, Gosu::Color::BLACK) $window.draw_rect(@x, @y, width, height, @options[:background], @z+1)
if mouse_over? if mouse_over?
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_HOVER_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_hover_background], @z+2)
else else
if @checked if @checked
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_ACTIVE_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_active_background], @z+2)
else else
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_COLOR) $window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_background], @z + 2)
end
end
if @checked
@text.draw
end end
end end
def update @text.draw if @checked
@text.x = @x+BUTTON_PADDING
@text.y = @y+BUTTON_PADDING
end end
def button_up(id) def button_up(id)
@@ -51,23 +32,22 @@ module CyberarmEngine
else else
@checked = true @checked = true
end end
@block.call(self) if @block
end end
end end
def mouse_over? def recalculate
if $window.mouse_x.between?(@x, @x+width) @width = @text.width
if $window.mouse_y.between?(@y, @y+height) @height= @text.height
true
end @text.x = @x + @padding
end @text.y = @y + @padding
@text.z = @z + 3
end end
def width(text_object = @text) def value
text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2 @checked
end
def height(text_object = @text)
text_object.textobject.height+BUTTON_PADDING*2
end end
end end
end end

View File

@@ -2,30 +2,55 @@ module CyberarmEngine
class Container class Container
include Common include Common
attr_accessor :text_color attr_accessor :stroke_color, :fill_color, :background_color
attr_reader :elements, :x, :y, :width, :height, :options attr_reader :elements, :x, :y, :z, :width, :height, :options
attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height
def initialize(x = 0, y = 0, width = $window.width, height = $window.height, options = {}) def initialize(options = {}, block = nil)
raise unless x.is_a?(Numeric) options[:parent].active_container = self
raise unless y.is_a?(Numeric)
raise unless width.is_a?(Numeric) options = {
raise unless height.is_a?(Numeric) x: 0, y: 0, z: 0,
raise unless options.is_a?(Hash) width: $window.width, height: $window.height
@x, @y, @width, @height, @internal_width, @internal_height = x, y, width, height-y, width, height-y }.merge!(options)
x = options.dig(:x)
y = options.dig(:y)
z = options.dig(:z)
width = options.dig(:width)
height = options.dig(:height)
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} 'width' must be a number" unless width.is_a?(Numeric)
raise "#{self.class} 'height' must be a number" unless height.is_a?(Numeric)
raise "#{self.class} 'options' must be a Hash" unless options.is_a?(Hash)
@x, @y, @z, @width, @height, @internal_width, @internal_height = x, y, z, width-x, height-y, width-x, height-y
@scroll_x, @scroll_y = 0, 0 @scroll_x, @scroll_y = 0, 0
@scroll_speed = 10 @scroll_speed = 10
@options = {} @options = options
@allow_recreation_on_resize = true
@text_color = options[:text_color] || Gosu::Color::WHITE @text_color = options[:text_color] || Gosu::Color::WHITE
@background_color = Gosu::Color::NONE
@elements = [] @elements = []
if defined?(self.setup); setup; end block.call(self) if block
options[:parent].active_container = nil
recalculate
return self
end end
def draw def draw
Gosu.clip_to(x, y, width, height) do Gosu.clip_to(x, y, width, height) do
background
Gosu.translate(scroll_x, scroll_y) do Gosu.translate(scroll_x, scroll_y) do
@elements.each(&:draw) @elements.each(&:draw)
end end
@@ -48,7 +73,6 @@ module CyberarmEngine
@scroll_y-=@scroll_speed @scroll_y-=@scroll_speed
if $window.height-@internal_height-y > 0 if $window.height-@internal_height-y > 0
@scroll_y = 0 @scroll_y = 0
p "H: #{@height-@internal_height}", "Y: #{@scroll_y}"
else else
@scroll_y = @height-@internal_height if @scroll_y <= @height-@internal_height @scroll_y = @height-@internal_height if @scroll_y <= @height-@internal_height
end end
@@ -61,103 +85,35 @@ module CyberarmEngine
@elements.each {|e| if defined?(e.button_up); e.button_up(id); end} @elements.each {|e| if defined?(e.button_up); e.button_up(id); end}
end end
def build(&block) def background
yield(self) Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z)
end end
def text(text, x, y, size = 18, color = self.text_color, alignment = nil, font = nil) def recalculate
relative_x(x) raise "mode was not defined!" unless @mode
relative_y(y) @packing_x = @x
_text = Text.new(text, x: relative_x, y: relative_y, size: size, color: color, alignment: alignment, font: font) @packing_y = @y
@elements.push(_text)
if _text.y-(_text.height*2) > @internal_height
@internal_height+=_text.height
end
return _text @elements.each do |element|
end flow(element) if @mode == :flow
stack(element) if @mode == :stack
def button(text, x, y, tooltip = "", &block)
relative_x(x)
relative_y(y)
_button = Button.new(text, relative_x, relative_y, false, tooltip) { if block.is_a?(Proc); block.call; end }
@elements.push(_button)
if _button.y-(_button.height*2) > @internal_height
@internal_height+=_button.height
end
return _button
end
def input(text, x, y, width = Input::WIDTH, size = 18, color = Gosu::Color::BLACK, tooltip = "")
relative_x(x)
relative_y(y)
_input = Input.new(text, relative_x, relative_y, width, size, color)
@elements.push(_input)
if _input.y-(_input.height*2) > @internal_height
@internal_height+=_input.height
end
return _input
end
def check_box(x, y, checked = false, size = CheckBox::SIZE)
relative_x(x)
relative_y(y)
_check_box = CheckBox.new(relative_x, relative_y, checked, size)
@elements.push(_check_box)
if _check_box.y-(_check_box.height*2) > @internal_height
@internal_height+=_check_box.height
end
return _check_box
end
def resize
if @allow_recreation_on_resize
$window.active_container = self.class.new
end end
end end
# Fills container background with color def flow(element)
def fill(color = Gosu::Color::BLACK, z = -1) element.x = @x + @packing_x
Gosu.draw_rect(@x, @y, @width, @height, color, z) element.y = @y
element.recalculate
@packing_x += element.width + 1
end end
def set_layout_y(start, spacing) def stack(element)
@layout_y_start = start element.x = @x
@layout_y_spacing = spacing element.y = @y + @packing_y
@layout_y_count = 0 element.recalculate
end
def layout_y(stay = false) @packing_y += element.height + 1
i = @layout_y_start+(@layout_y_spacing*@layout_y_count)
@layout_y_count+=1 unless stay
return i
end
# Return X position relative to container
def relative_x(int)
self.x+int
end
# Return Y position relative to container
def relative_y(int)
self.y+int
end
def calc_percentage(positive, total)
begin
i = ((positive.to_f/total.to_f)*100.0).round(2)
if !i.nan?
return "#{i}%"
else
"N/A"
end
rescue ZeroDivisionError => e
puts e
return "N/A" # 0 / 0, safe to assume no actionable data
end
end end
end end
end end

View File

@@ -0,0 +1,61 @@
module CyberarmEngine
module DSL
def flow(options = {}, &block)
puts "Flow"
options[:parent] = self
_flow = Flow.new(options, block)
@active_container = _flow
@game_objects << _flow
return _flow
end
def stack(options = {}, &block)
puts "Stack"
options[:parent] = self
_stack = Stack.new(options, block)
@active_container = _stack
@game_objects << _stack
return _stack
end
def label(text, options = {})
options[:parent] = @active_container
_text = Label.new(text, options)
@active_container.elements << _text
return _text
end
def button(text, options = {}, &block)
options[:parent] = @active_container
_button = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
@active_container.elements << _button
return _button
end
def edit_line(text, options = {}, &block)
options[:parent] = @active_container
_edit_line = EditLine.new(text, options, block)
@active_container.elements << _edit_line
return _edit_line
end
def check_box(options = {}, &block)
options[:parent] = @active_container
_check_box = CheckBox.new(options, block)
@active_container.elements << _check_box
return _check_box
end
def background(color = Gosu::Color::NONE)
@active_container.background_color = color
end
end
end

View File

@@ -1,13 +1,13 @@
module CyberarmEngine module CyberarmEngine
class Input class EditLine < Element
WIDTH = 200 WIDTH = 200
FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(150,150,144) FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(150,150,144)
NO_FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(130,130,130) NO_FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(130,130,130)
attr_accessor :text, :x, :y, :width, :size, :color, :type, :focus attr_accessor :text, :x, :y, :width, :size, :color, :type, :focus
attr_reader :text_object, :text_input, :height, :fixed_x attr_reader :text_object, :text_input, :height
def initialize(text, x, y, width = WIDTH, size = Text::SIZE, color = Gosu::Color::BLACK, tooltip = "", type = nil) def initialize(text, options = {})
@text = text @text = text
@x, @y= x, y @x, @y= x, y
@width= width @width= width
@@ -24,8 +24,6 @@ module CyberarmEngine
@text_input.text = @text @text_input.text = @text
@background_color = NO_FOCUS_BACKGROUND_COLOR @background_color = NO_FOCUS_BACKGROUND_COLOR
@fixed_x = @x
@x_offset= 0
@carot_ticks = 0 @carot_ticks = 0
@carot_width = 2.5 @carot_width = 2.5
@@ -97,14 +95,6 @@ module CyberarmEngine
end end
end end
def mouse_over?
if $window.mouse_x.between?(@x, @x+width)
if $window.mouse_y.between?(@y, @y+height)
true
end
end
end
def width(text_object = @text_object) def width(text_object = @text_object)
# text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2 # text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2
@width @width

View File

@@ -0,0 +1,91 @@
module CyberarmEngine
class Element
DEFAULTS = {
x: 0,
y: 0,
z: 30,
width: 0,
height: 0
}
THEME = {
stroke: Gosu::Color::WHITE,
fill: Gosu::Color::NONE,
background: Gosu::Color.rgb(12,12,12),
padding: 20,
margin: 0,
interactive_stroke: Gosu::Color::WHITE,
interactive_active_stroke: Gosu::Color::BLACK,
interactive_background: Gosu::Color::GRAY,
interactive_hover_background: Gosu::Color.rgb(100, 100, 100),
interactive_active_background: Gosu::Color.rgb(50, 50, 50),
text_size: 22,
text_shadow: true,
font: "Consolas"
}
attr_accessor :x, :y, :z
attr_accessor :offset_x, :offset_y
def initialize(options = {}, block = nil)
options = (THEME).merge(DEFAULTS).merge(options)
@options = options
@block = block
@offset_x = 0
@offset_y = 0
@x = options[:x]
@y = options[:y]
@z = options[:z]
@width = options[:width]
@height = options[:width]
@padding = options[:padding]
@margin = options[:margin]
@parent = options[:parent]
end
def draw
end
def update
end
def button_down(id)
end
def button_up(id)
end
def mouse_over?
if $window.mouse_x.between?(@x + @offset_x, @x + @offset_x + width)
if $window.mouse_y.between?(@y + @offset_y, @y + @offset_y + height)
true
end
end
end
def width
@width + (@padding * 2)
end
def height
@height + (@padding * 2)
end
def recalculate
end
def value
raise "#{self.klass}#value was not overridden!"
end
end
end

View File

@@ -0,0 +1,10 @@
module CyberarmEngine
class Flow < Container
include Common
def initialize(options = {}, block = nil)
@mode = :flow
super
end
end
end

View File

@@ -0,0 +1,26 @@
module CyberarmEngine
class Label < Element
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])
return self
end
def draw
$window.draw_rect(@x, @y, width, height, @options[:fill], @z+1)
@text.draw
end
def recalculate
@width = @text.width
@height= @text.height
@text.x = @x + @padding
@text.y = @y + @padding
@text.z = @z + 3
end
end
end

View File

@@ -0,0 +1,10 @@
module CyberarmEngine
class Stack < Container
include Common
def initialize(options = {}, block = nil)
@mode = :stack
super
end
end
end