mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Major refactor of Container and elements to support a more Shoes-like construction of menus (in complete)
This commit is contained in:
@@ -5,13 +5,20 @@ require_relative "cyberarm_engine/version"
|
||||
require_relative "cyberarm_engine/common"
|
||||
|
||||
require_relative "cyberarm_engine/game_object"
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
require_relative "cyberarm_engine/engine"
|
||||
|
||||
require_relative "cyberarm_engine/objects/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/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/flow"
|
||||
require_relative "cyberarm_engine/ui/stack"
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
module CyberarmEngine
|
||||
class GameState
|
||||
include Common
|
||||
include DSL
|
||||
|
||||
SCALE_X_BASE = 1920.0
|
||||
SCALE_Y_BASE = 1080.0
|
||||
attr_accessor :options, :global_pause
|
||||
attr_accessor :options, :global_pause, :active_container, :active_grid
|
||||
attr_reader :game_objects
|
||||
|
||||
def initialize(options={})
|
||||
@options = options unless @options
|
||||
@options = options
|
||||
@game_objects = []
|
||||
@global_pause = false
|
||||
|
||||
@active_container = nil
|
||||
@active_grid = nil
|
||||
|
||||
setup
|
||||
end
|
||||
|
||||
@@ -19,18 +21,11 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def draw
|
||||
@game_objects.each do |o|
|
||||
o.draw if o.visible
|
||||
end
|
||||
@game_objects.each(&:draw)
|
||||
end
|
||||
|
||||
def update
|
||||
@game_objects.each do |o|
|
||||
unless o.paused || @global_pause
|
||||
o.update
|
||||
o.update_debug_text if $debug
|
||||
end
|
||||
end
|
||||
@game_objects.each(&:update)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@@ -40,7 +35,7 @@ module CyberarmEngine
|
||||
|
||||
def button_up(id)
|
||||
@game_objects.each do |o|
|
||||
o.button_up(id) unless o.paused
|
||||
o.button_up(id)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -74,20 +74,21 @@ module CyberarmEngine
|
||||
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, @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(@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)
|
||||
|
||||
@textobject.draw(@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, @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(@text, @x+@shadow_size, @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_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
|
||||
|
||||
@textobject.draw(@text, @x, @y, @z, @factor_x, @factor_y, @color)
|
||||
@textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
|
||||
end
|
||||
|
||||
def update; end
|
||||
|
||||
@@ -1,122 +1,48 @@
|
||||
module CyberarmEngine
|
||||
BUTTON_TEXT_COLOR = Gosu::Color::WHITE
|
||||
BUTTON_TEXT_ACTIVE_COLOR = Gosu::Color::BLACK
|
||||
BUTTON_COLOR = Gosu::Color.rgb(12,12,12)
|
||||
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 < Element
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
class Button
|
||||
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
|
||||
@text = Text.new(text, font: @options[:font], color: @options[:interactive_stroke], size: @options[:text_size], shadow: @options[:text_shadow])
|
||||
|
||||
return self
|
||||
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
|
||||
@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
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_ACTIVE_COLOR)
|
||||
if mouse_over? && $window.button_down?(Gosu::MsLeft)
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_active_background], @z+2)
|
||||
elsif mouse_over?
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_HOVER_COLOR)
|
||||
show_tooltip
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_hover_background], @z+2)
|
||||
# show_tooltip
|
||||
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
|
||||
|
||||
def update
|
||||
@text.x = @x+BUTTON_PADDING
|
||||
@text.y = @y+BUTTON_PADDING
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
case id
|
||||
when Gosu::MsLeft
|
||||
click_check
|
||||
end
|
||||
end
|
||||
|
||||
def click_check
|
||||
if mouse_over?
|
||||
puts "Clicked: #{@text.text}"
|
||||
@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
|
||||
@block.call(self) if @block
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def show_tooltip
|
||||
if @tooltip.text != ""
|
||||
x = @tooltip.x-BUTTON_PADDING
|
||||
def recalculate
|
||||
@width = @text.width
|
||||
@height= @text.height
|
||||
|
||||
$window.draw_rect(x, @y-height, width(@tooltip), height(@tooltip), BUTTON_ACTIVE_COLOR, 9_999)
|
||||
$window.draw_rect(x-1, @y-height-1, width(@tooltip)+2, height(@tooltip)+2, Gosu::Color::WHITE, 9_998)
|
||||
@tooltip.draw
|
||||
end
|
||||
@text.x = @x + @padding
|
||||
@text.y = @y + @padding
|
||||
@text.z = @z + 3
|
||||
end
|
||||
|
||||
def width(text_object = @text)
|
||||
text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2
|
||||
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
|
||||
def value
|
||||
@text.text
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,47 +1,28 @@
|
||||
module CyberarmEngine
|
||||
class CheckBox
|
||||
SIZE = 22
|
||||
class CheckBox < Element
|
||||
def initialize(options, block = nil)
|
||||
super(options, block)
|
||||
@checked = options[:checked] || false
|
||||
|
||||
attr_accessor :x, :y, :checked
|
||||
attr_reader :text
|
||||
@text = Text.new("X", font: @options[:font], color: @options[:interactive_stroke], size: @options[:text_size], shadow: @options[:text_shadow])
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
def x=(int)
|
||||
@x = int
|
||||
@text.x = int
|
||||
end
|
||||
|
||||
def y=(int)
|
||||
@y = int
|
||||
@text.y = int
|
||||
end
|
||||
|
||||
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?
|
||||
$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
|
||||
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
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, BUTTON_COLOR)
|
||||
end
|
||||
end
|
||||
if @checked
|
||||
@text.draw
|
||||
$window.draw_rect(@x+1, @y+1, width-2, height-2, @options[:interactive_background], @z + 2)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@text.x = @x+BUTTON_PADDING
|
||||
@text.y = @y+BUTTON_PADDING
|
||||
@text.draw if @checked
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
@@ -51,23 +32,22 @@ module CyberarmEngine
|
||||
else
|
||||
@checked = true
|
||||
end
|
||||
|
||||
@block.call(self) if @block
|
||||
end
|
||||
end
|
||||
|
||||
def mouse_over?
|
||||
if $window.mouse_x.between?(@x, @x+width)
|
||||
if $window.mouse_y.between?(@y, @y+height)
|
||||
true
|
||||
end
|
||||
end
|
||||
def recalculate
|
||||
@width = @text.width
|
||||
@height= @text.height
|
||||
|
||||
@text.x = @x + @padding
|
||||
@text.y = @y + @padding
|
||||
@text.z = @z + 3
|
||||
end
|
||||
|
||||
def width(text_object = @text)
|
||||
text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2
|
||||
end
|
||||
|
||||
def height(text_object = @text)
|
||||
text_object.textobject.height+BUTTON_PADDING*2
|
||||
def value
|
||||
@checked
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,30 +2,55 @@ module CyberarmEngine
|
||||
class Container
|
||||
include Common
|
||||
|
||||
attr_accessor :text_color
|
||||
attr_reader :elements, :x, :y, :width, :height, :options
|
||||
attr_accessor :stroke_color, :fill_color, :background_color
|
||||
attr_reader :elements, :x, :y, :z, :width, :height, :options
|
||||
attr_reader :scroll_x, :scroll_y, :internal_width, :internal_height
|
||||
|
||||
def initialize(x = 0, y = 0, width = $window.width, height = $window.height, options = {})
|
||||
raise unless x.is_a?(Numeric)
|
||||
raise unless y.is_a?(Numeric)
|
||||
raise unless width.is_a?(Numeric)
|
||||
raise unless height.is_a?(Numeric)
|
||||
raise unless options.is_a?(Hash)
|
||||
@x, @y, @width, @height, @internal_width, @internal_height = x, y, width, height-y, width, height-y
|
||||
def initialize(options = {}, block = nil)
|
||||
options[:parent].active_container = self
|
||||
|
||||
options = {
|
||||
x: 0, y: 0, z: 0,
|
||||
width: $window.width, height: $window.height
|
||||
}.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_speed = 10
|
||||
|
||||
@options = {}
|
||||
@allow_recreation_on_resize = true
|
||||
@options = options
|
||||
|
||||
@text_color = options[:text_color] || Gosu::Color::WHITE
|
||||
@background_color = Gosu::Color::NONE
|
||||
@elements = []
|
||||
|
||||
if defined?(self.setup); setup; end
|
||||
block.call(self) if block
|
||||
|
||||
options[:parent].active_container = nil
|
||||
|
||||
recalculate
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def draw
|
||||
Gosu.clip_to(x, y, width, height) do
|
||||
background
|
||||
|
||||
Gosu.translate(scroll_x, scroll_y) do
|
||||
@elements.each(&:draw)
|
||||
end
|
||||
@@ -48,7 +73,6 @@ module CyberarmEngine
|
||||
@scroll_y-=@scroll_speed
|
||||
if $window.height-@internal_height-y > 0
|
||||
@scroll_y = 0
|
||||
p "H: #{@height-@internal_height}", "Y: #{@scroll_y}"
|
||||
else
|
||||
@scroll_y = @height-@internal_height if @scroll_y <= @height-@internal_height
|
||||
end
|
||||
@@ -61,103 +85,35 @@ module CyberarmEngine
|
||||
@elements.each {|e| if defined?(e.button_up); e.button_up(id); end}
|
||||
end
|
||||
|
||||
def build(&block)
|
||||
yield(self)
|
||||
def background
|
||||
Gosu.draw_rect(@x, @y, @width, @height, @background_color, @z)
|
||||
end
|
||||
|
||||
def text(text, x, y, size = 18, color = self.text_color, alignment = nil, font = nil)
|
||||
relative_x(x)
|
||||
relative_y(y)
|
||||
_text = Text.new(text, x: relative_x, y: relative_y, size: size, color: color, alignment: alignment, font: font)
|
||||
@elements.push(_text)
|
||||
if _text.y-(_text.height*2) > @internal_height
|
||||
@internal_height+=_text.height
|
||||
end
|
||||
def recalculate
|
||||
raise "mode was not defined!" unless @mode
|
||||
@packing_x = @x
|
||||
@packing_y = @y
|
||||
|
||||
return _text
|
||||
end
|
||||
|
||||
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
|
||||
@elements.each do |element|
|
||||
flow(element) if @mode == :flow
|
||||
stack(element) if @mode == :stack
|
||||
end
|
||||
end
|
||||
|
||||
# Fills container background with color
|
||||
def fill(color = Gosu::Color::BLACK, z = -1)
|
||||
Gosu.draw_rect(@x, @y, @width, @height, color, z)
|
||||
def flow(element)
|
||||
element.x = @x + @packing_x
|
||||
element.y = @y
|
||||
element.recalculate
|
||||
|
||||
@packing_x += element.width + 1
|
||||
end
|
||||
|
||||
def set_layout_y(start, spacing)
|
||||
@layout_y_start = start
|
||||
@layout_y_spacing = spacing
|
||||
@layout_y_count = 0
|
||||
end
|
||||
def stack(element)
|
||||
element.x = @x
|
||||
element.y = @y + @packing_y
|
||||
element.recalculate
|
||||
|
||||
def layout_y(stay = false)
|
||||
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
|
||||
@packing_y += element.height + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
61
lib/cyberarm_engine/ui/dsl.rb
Normal file
61
lib/cyberarm_engine/ui/dsl.rb
Normal 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
|
||||
@@ -1,13 +1,13 @@
|
||||
module CyberarmEngine
|
||||
class Input
|
||||
class EditLine < Element
|
||||
WIDTH = 200
|
||||
FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(150,150,144)
|
||||
NO_FOCUS_BACKGROUND_COLOR = Gosu::Color.rgb(130,130,130)
|
||||
|
||||
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
|
||||
@x, @y= x, y
|
||||
@width= width
|
||||
@@ -24,8 +24,6 @@ module CyberarmEngine
|
||||
@text_input.text = @text
|
||||
|
||||
@background_color = NO_FOCUS_BACKGROUND_COLOR
|
||||
@fixed_x = @x
|
||||
@x_offset= 0
|
||||
|
||||
@carot_ticks = 0
|
||||
@carot_width = 2.5
|
||||
@@ -97,14 +95,6 @@ module CyberarmEngine
|
||||
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)
|
||||
# text_object.textobject.text_width(text_object.text)+BUTTON_PADDING*2
|
||||
@width
|
||||
91
lib/cyberarm_engine/ui/element.rb
Normal file
91
lib/cyberarm_engine/ui/element.rb
Normal 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
|
||||
10
lib/cyberarm_engine/ui/flow.rb
Normal file
10
lib/cyberarm_engine/ui/flow.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module CyberarmEngine
|
||||
class Flow < Container
|
||||
include Common
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@mode = :flow
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
26
lib/cyberarm_engine/ui/label.rb
Normal file
26
lib/cyberarm_engine/ui/label.rb
Normal 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
|
||||
10
lib/cyberarm_engine/ui/stack.rb
Normal file
10
lib/cyberarm_engine/ui/stack.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module CyberarmEngine
|
||||
class Stack < Container
|
||||
include Common
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@mode = :stack
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user