mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bed78e7dc8 | |||
| bdce85613b | |||
| b3bfa0d654 | |||
| a972cfac98 | |||
| 2fe8e6042b | |||
| f68a8383af | |||
| 72df060059 | |||
| e95b4c05e2 | |||
| 4642056576 | |||
| 3d2402b7f7 |
@@ -19,16 +19,17 @@ require_relative "cyberarm_engine/ui/event"
|
||||
require_relative "cyberarm_engine/ui/style"
|
||||
require_relative "cyberarm_engine/ui/border_canvas"
|
||||
require_relative "cyberarm_engine/ui/element"
|
||||
require_relative "cyberarm_engine/ui/label"
|
||||
require_relative "cyberarm_engine/ui/button"
|
||||
require_relative "cyberarm_engine/ui/toggle_button"
|
||||
require_relative "cyberarm_engine/ui/edit_line"
|
||||
require_relative "cyberarm_engine/ui/image"
|
||||
require_relative "cyberarm_engine/ui/container"
|
||||
require_relative "cyberarm_engine/ui/elements/label"
|
||||
require_relative "cyberarm_engine/ui/elements/button"
|
||||
require_relative "cyberarm_engine/ui/elements/toggle_button"
|
||||
require_relative "cyberarm_engine/ui/elements/edit_line"
|
||||
require_relative "cyberarm_engine/ui/elements/image"
|
||||
require_relative "cyberarm_engine/ui/elements/container"
|
||||
require_relative "cyberarm_engine/ui/elements/flow"
|
||||
require_relative "cyberarm_engine/ui/elements/stack"
|
||||
require_relative "cyberarm_engine/ui/elements/check_box"
|
||||
require_relative "cyberarm_engine/ui/elements/progress"
|
||||
|
||||
require_relative "cyberarm_engine/ui/flow"
|
||||
require_relative "cyberarm_engine/ui/stack"
|
||||
require_relative "cyberarm_engine/ui/check_box"
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
|
||||
@@ -158,9 +158,20 @@ module CyberarmEngine
|
||||
@top_right = background[:top_right]
|
||||
@bottom_left = background[:bottom_left]
|
||||
@bottom_right = background[:bottom_right]
|
||||
elsif background.is_a?(Range)
|
||||
set([background.begin, background.begin, background.end, background.end])
|
||||
else
|
||||
raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Add <=> method to support Range based gradients
|
||||
module Gosu
|
||||
class Color
|
||||
def <=>(other)
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,7 +62,7 @@ module CyberarmEngine
|
||||
@states << klass
|
||||
else
|
||||
@states << klass.new(options) if child_of?(klass, GameState)
|
||||
@states << klass.new if child_of?(klass, Container)
|
||||
@states << klass.new if child_of?(klass, Element::Container)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Button < Label
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(text, options, block)
|
||||
|
||||
@style.background_canvas.background = default(:background)
|
||||
end
|
||||
|
||||
def render
|
||||
draw_text
|
||||
end
|
||||
|
||||
def draw_text
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||
|
||||
if @focus
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
@focus = true
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
window.current_state.focus = self
|
||||
@text.color = default(:active, :color)
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender,x, y)
|
||||
enter(sender)
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
end
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,60 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class CheckBox < Flow
|
||||
def initialize(text, options, block = nil)
|
||||
super({}, block = nil)
|
||||
options[:toggled] = options[:checked]
|
||||
|
||||
@toggle_button = ToggleButton.new(options)
|
||||
@label = Label.new(text, options)
|
||||
|
||||
define_label_singletons
|
||||
|
||||
add(@toggle_button)
|
||||
add(@label)
|
||||
|
||||
@style.width = @toggle_button.width + @label.width
|
||||
@style.height = @toggle_button.height + @label.height
|
||||
end
|
||||
|
||||
def text=(text)
|
||||
@label.text = text
|
||||
recalculate
|
||||
end
|
||||
|
||||
def value
|
||||
@toggle_button.value
|
||||
end
|
||||
|
||||
def value=(bool)
|
||||
@toggle_button.vlaue = bool
|
||||
end
|
||||
|
||||
def define_label_singletons
|
||||
@label.define_singleton_method(:_toggle_button) do |button|
|
||||
@_toggle_button = button
|
||||
end
|
||||
|
||||
@label._toggle_button(@toggle_button)
|
||||
|
||||
@label.define_singleton_method(:holding_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:released_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.released_left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:clicked_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.clicked_left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:enter) do |sender|
|
||||
@_toggle_button.enter(sender)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:leave) do |sender|
|
||||
@_toggle_button.leave(sender)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,146 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Container < Element
|
||||
include Common
|
||||
|
||||
attr_accessor :stroke_color, :fill_color
|
||||
attr_reader :children
|
||||
attr_reader :scroll_x, :scroll_y
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
super
|
||||
|
||||
@scroll_x, @scroll_y = 0, 0
|
||||
@scroll_speed = 10
|
||||
|
||||
@text_color = options[:color]
|
||||
|
||||
@children = []
|
||||
end
|
||||
|
||||
def build
|
||||
@block.call(self) if @block
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def add(element)
|
||||
@children << element
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def render
|
||||
@children.each(&:draw)
|
||||
end
|
||||
|
||||
def update
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
def hit_element?(x, y)
|
||||
@children.reverse_each do |child|
|
||||
case child
|
||||
when Container
|
||||
if element = child.hit_element?(x, y)
|
||||
return element
|
||||
end
|
||||
else
|
||||
return child if child.hit?(x, y)
|
||||
end
|
||||
end
|
||||
|
||||
self if hit?(x, y)
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||
return unless visible?
|
||||
stylize
|
||||
|
||||
layout
|
||||
|
||||
@style.width = @max_width ? @max_width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
|
||||
@style.height = @max_height ? @max_height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
||||
|
||||
# Move child to parent after positioning
|
||||
@children.each do |child|
|
||||
child.x += @x
|
||||
child.y += @y
|
||||
|
||||
child.stylize
|
||||
child.recalculate
|
||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||
end
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def layout
|
||||
raise "Not overridden"
|
||||
end
|
||||
|
||||
def max_width
|
||||
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
end
|
||||
|
||||
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) # Flow
|
||||
element.x = element.style.margin_left + @current_position.x
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
element.recalculate
|
||||
|
||||
@current_position.x += element.outer_width
|
||||
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
||||
end
|
||||
|
||||
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 = @style.margin_left
|
||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
||||
|
||||
child.x = child.style.margin_left + @current_position.x
|
||||
child.y = child.style.margin_top + @current_position.y
|
||||
|
||||
child.recalculate
|
||||
|
||||
@current_position.x += child.outer_width
|
||||
end
|
||||
|
||||
def move_to_next_line(element) # Stack
|
||||
element.x = element.style.margin_left + @current_position.x
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
element.recalculate
|
||||
|
||||
@current_position.y += element.outer_height
|
||||
end
|
||||
|
||||
# def mouse_wheel_up(sender, x, y)
|
||||
# @children.each {|c| c.y -= @scroll_speed}
|
||||
# @children.each {|c| c.recalculate}
|
||||
# end
|
||||
|
||||
# def mouse_wheel_down(sender, x, y)
|
||||
# @children.each {|c| c.y += @scroll_speed}
|
||||
# @children.each {|c| c.recalculate}
|
||||
# end
|
||||
|
||||
def value
|
||||
@children.map {|c| c.class}.join(", ")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,10 +3,10 @@ module CyberarmEngine
|
||||
def flow(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_container = Flow.new(options, block)
|
||||
_container = Element::Flow.new(options, block)
|
||||
@containers << _container
|
||||
_container.build
|
||||
options[:parent].add(_container)
|
||||
_container.parent.add(_container)
|
||||
@containers.pop
|
||||
|
||||
return _container
|
||||
@@ -15,10 +15,10 @@ module CyberarmEngine
|
||||
def stack(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_container = Stack.new(options, block)
|
||||
_container = Element::Stack.new(options, block)
|
||||
@containers << _container
|
||||
_container.build
|
||||
options[:parent].add(_container)
|
||||
_container.parent.add(_container)
|
||||
@containers.pop
|
||||
|
||||
return _container
|
||||
@@ -27,7 +27,7 @@ module CyberarmEngine
|
||||
def label(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = Label.new(text, options, block)
|
||||
_element = Element::Label.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
@@ -36,7 +36,7 @@ module CyberarmEngine
|
||||
def button(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
||||
_element = Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
@@ -45,7 +45,7 @@ module CyberarmEngine
|
||||
def edit_line(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = EditLine.new(text, options, block)
|
||||
_element = Element::EditLine.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
@@ -54,7 +54,7 @@ module CyberarmEngine
|
||||
def toggle_button(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = ToggleButton.new(options, block)
|
||||
_element = Element::ToggleButton.new(options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
@@ -63,7 +63,7 @@ module CyberarmEngine
|
||||
def check_box(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = CheckBox.new(text, options, block)
|
||||
_element = Element::CheckBox.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
@@ -72,7 +72,16 @@ module CyberarmEngine
|
||||
def image(path, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = Image.new(path, options, block)
|
||||
_element = Element::Image.new(path, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
end
|
||||
|
||||
def progress(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_element = Element::Progress.new(options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class EditLine < Button
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(text, options, block)
|
||||
|
||||
@type = default(:type)
|
||||
|
||||
|
||||
@caret_width = default(:caret_width)
|
||||
@caret_height= @text.height
|
||||
@caret_color = default(:caret_color)
|
||||
@caret_interval = default(:caret_interval)
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
|
||||
@text_input = Gosu::TextInput.new
|
||||
@text_input.text = text
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def render
|
||||
Gosu.clip_to(@text.x, @text.y, @style.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
|
||||
if @type == :password
|
||||
@text.text = default(:password_character) * @text_input.text.length
|
||||
else
|
||||
@text.text = @text_input.text
|
||||
end
|
||||
|
||||
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
|
||||
@show_caret = !@show_caret
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
super
|
||||
window.text_input = @text_input
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
if @focus
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
end
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
unless @focus
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
window.text_input = nil
|
||||
end
|
||||
|
||||
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
||||
def caret_position
|
||||
if @type == :password
|
||||
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
|
||||
else
|
||||
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
@style.width = default(:width)
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@text_input.text
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,7 @@ module CyberarmEngine
|
||||
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@parent = options[:parent] # parent Container (i.e. flow/stack)
|
||||
@parent = options.delete(:parent) # parent Container (i.e. flow/stack)
|
||||
options = theme_defaults(options)
|
||||
@options = options
|
||||
@block = block
|
||||
@@ -23,18 +23,21 @@ module CyberarmEngine
|
||||
@y = @style.y
|
||||
@z = @style.z
|
||||
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
@fixed_x = @x if @x != 0
|
||||
@fixed_y = @y if @y != 0
|
||||
|
||||
@style.width = default(:width) || nil
|
||||
@style.height = default(:height) || nil
|
||||
|
||||
stylize
|
||||
|
||||
default_events
|
||||
end
|
||||
|
||||
def stylize
|
||||
@style.width = @style.width || $window.width
|
||||
@style.height = @style.height || $window.height
|
||||
|
||||
set_border_thickness(@style.border_thickness)
|
||||
|
||||
set_padding(@style.padding)
|
||||
@@ -119,17 +122,17 @@ module CyberarmEngine
|
||||
|
||||
def toggle
|
||||
@visible = !@visible
|
||||
root.recalculate
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def show
|
||||
@visible = true
|
||||
root.recalculate
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def hide
|
||||
@visible = false
|
||||
root.recalculate
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -159,7 +162,7 @@ module CyberarmEngine
|
||||
|
||||
def width
|
||||
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
|
||||
0
|
||||
end
|
||||
@@ -171,7 +174,7 @@ module CyberarmEngine
|
||||
|
||||
def height
|
||||
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
|
||||
0
|
||||
end
|
||||
@@ -181,6 +184,19 @@ module CyberarmEngine
|
||||
@style.margin_top + height + @style.margin_bottom
|
||||
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)
|
||||
@style.background_canvas.background=(_background)
|
||||
update_background
|
||||
@@ -214,6 +230,10 @@ module CyberarmEngine
|
||||
@root
|
||||
end
|
||||
|
||||
def is_root?
|
||||
@gui_state != nil
|
||||
end
|
||||
|
||||
def recalculate
|
||||
raise "#{self.class}#recalculate was not overridden!"
|
||||
end
|
||||
|
||||
55
lib/cyberarm_engine/ui/elements/button.rb
Normal file
55
lib/cyberarm_engine/ui/elements/button.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Button < Label
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(text, options, block)
|
||||
|
||||
@style.background_canvas.background = default(:background)
|
||||
end
|
||||
|
||||
def render
|
||||
draw_text
|
||||
end
|
||||
|
||||
def draw_text
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||
|
||||
if @focus
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
@focus = true
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
window.current_state.focus = self
|
||||
@text.color = default(:active, :color)
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender,x, y)
|
||||
enter(sender)
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
end
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
59
lib/cyberarm_engine/ui/elements/check_box.rb
Normal file
59
lib/cyberarm_engine/ui/elements/check_box.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class CheckBox < Flow
|
||||
def initialize(text, options, block = nil)
|
||||
super({}, block = nil)
|
||||
options[:toggled] = options[:checked]
|
||||
|
||||
@toggle_button = ToggleButton.new(options)
|
||||
@label = Label.new(text, options)
|
||||
|
||||
define_label_singletons
|
||||
|
||||
add(@toggle_button)
|
||||
add(@label)
|
||||
end
|
||||
|
||||
def text=(text)
|
||||
@label.text = text
|
||||
recalculate
|
||||
end
|
||||
|
||||
def value
|
||||
@toggle_button.value
|
||||
end
|
||||
|
||||
def value=(bool)
|
||||
@toggle_button.vlaue = bool
|
||||
end
|
||||
|
||||
def define_label_singletons
|
||||
@label.define_singleton_method(:_toggle_button) do |button|
|
||||
@_toggle_button = button
|
||||
end
|
||||
|
||||
@label._toggle_button(@toggle_button)
|
||||
|
||||
@label.define_singleton_method(:holding_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:released_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.released_left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:clicked_left_mouse_button) do |sender, x, y|
|
||||
@_toggle_button.clicked_left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:enter) do |sender|
|
||||
@_toggle_button.enter(sender)
|
||||
end
|
||||
|
||||
@label.define_singleton_method(:leave) do |sender|
|
||||
@_toggle_button.leave(sender)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
159
lib/cyberarm_engine/ui/elements/container.rb
Normal file
159
lib/cyberarm_engine/ui/elements/container.rb
Normal file
@@ -0,0 +1,159 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Container < Element
|
||||
include Common
|
||||
|
||||
attr_accessor :stroke_color, :fill_color
|
||||
attr_reader :children, :gui_state
|
||||
attr_reader :scroll_x, :scroll_y
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@gui_state = options.delete(:gui_state)
|
||||
super
|
||||
|
||||
@scroll_x, @scroll_y = 0, 0
|
||||
@scroll_speed = 10
|
||||
|
||||
@text_color = options[:color]
|
||||
|
||||
@children = []
|
||||
end
|
||||
|
||||
def build
|
||||
@block.call(self) if @block
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def add(element)
|
||||
@children << element
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def render
|
||||
Gosu.clip_to(@x, @y, width, height) do
|
||||
@children.each(&:draw)
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
def hit_element?(x, y)
|
||||
@children.reverse_each do |child|
|
||||
case child
|
||||
when Container
|
||||
if element = child.hit_element?(x, y)
|
||||
return element
|
||||
end
|
||||
else
|
||||
return child if child.hit?(x, y)
|
||||
end
|
||||
end
|
||||
|
||||
self if hit?(x, y)
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||
return unless visible?
|
||||
stylize
|
||||
|
||||
layout
|
||||
|
||||
if is_root?
|
||||
@width = @style.width = window.width
|
||||
@height = @style.height = window.height
|
||||
else
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_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
|
||||
|
||||
|
||||
# Move child to parent after positioning
|
||||
@children.each do |child|
|
||||
child.x += @x
|
||||
child.y += @y
|
||||
|
||||
child.stylize
|
||||
child.recalculate
|
||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||
end
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def layout
|
||||
raise "Not overridden"
|
||||
end
|
||||
|
||||
def max_width
|
||||
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
end
|
||||
|
||||
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) # Flow
|
||||
element.x = element.style.margin_left + @current_position.x
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
element.recalculate
|
||||
|
||||
@current_position.x += element.outer_width
|
||||
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
||||
end
|
||||
|
||||
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 = @style.margin_left
|
||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
||||
|
||||
child.x = child.style.margin_left + @current_position.x
|
||||
child.y = child.style.margin_top + @current_position.y
|
||||
|
||||
child.recalculate
|
||||
|
||||
@current_position.x += child.outer_width
|
||||
end
|
||||
|
||||
def move_to_next_line(element) # Stack
|
||||
element.x = element.style.margin_left + @current_position.x
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
element.recalculate
|
||||
|
||||
@current_position.y += element.outer_height
|
||||
end
|
||||
|
||||
# def mouse_wheel_up(sender, x, y)
|
||||
# @children.each {|c| c.y -= @scroll_speed}
|
||||
# @children.each {|c| c.recalculate}
|
||||
# end
|
||||
|
||||
# def mouse_wheel_down(sender, x, y)
|
||||
# @children.each {|c| c.y += @scroll_speed}
|
||||
# @children.each {|c| c.recalculate}
|
||||
# end
|
||||
|
||||
def value
|
||||
@children.map {|c| c.class}.join(", ")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
92
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
92
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
@@ -0,0 +1,92 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class EditLine < Button
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(text, options, block)
|
||||
|
||||
@type = default(:type)
|
||||
|
||||
@caret_width = default(:caret_width)
|
||||
@caret_height= @text.height
|
||||
@caret_color = default(:caret_color)
|
||||
@caret_interval = default(:caret_interval)
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
|
||||
@text_input = Gosu::TextInput.new
|
||||
@text_input.text = text
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def render
|
||||
Gosu.clip_to(@text.x, @text.y, @style.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
|
||||
if @type == :password
|
||||
@text.text = default(:password_character) * @text_input.text.length
|
||||
else
|
||||
@text.text = @text_input.text
|
||||
end
|
||||
|
||||
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
|
||||
@show_caret = !@show_caret
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
super
|
||||
window.text_input = @text_input
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
if @focus
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
end
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
unless @focus
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
window.text_input = nil
|
||||
end
|
||||
|
||||
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
||||
def caret_position
|
||||
if @type == :password
|
||||
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
|
||||
else
|
||||
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
@width = dimensional_size(@style.width, :width) || default(:width)
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@text_input.text
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
lib/cyberarm_engine/ui/elements/flow.rb
Normal file
17
lib/cyberarm_engine/ui/elements/flow.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Flow < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
if fits_on_line?(child)
|
||||
position_on_current_line(child)
|
||||
else
|
||||
position_on_next_line(child)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
50
lib/cyberarm_engine/ui/elements/image.rb
Normal file
50
lib/cyberarm_engine/ui/elements/image.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Image < Element
|
||||
def initialize(path, options = {}, block = nil)
|
||||
super(options, block)
|
||||
@path = path
|
||||
|
||||
@image = Gosu::Image.new(path, retro: @options[:image_retro])
|
||||
@scale_x, @scale_y = 1, 1
|
||||
end
|
||||
|
||||
def render
|
||||
@image.draw(
|
||||
@style.border_thickness_left + @style.padding_left + @x,
|
||||
@style.border_thickness_top + @style.padding_top + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y) # TODO: Add color support?
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_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
|
||||
|
||||
def value
|
||||
@path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
45
lib/cyberarm_engine/ui/elements/label.rb
Normal file
45
lib/cyberarm_engine/ui/elements/label.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Label < Element
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
@text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow])
|
||||
end
|
||||
|
||||
def render
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_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.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@text.text
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
@text.text = value
|
||||
|
||||
old_width, old_height = width, height
|
||||
recalculate
|
||||
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
50
lib/cyberarm_engine/ui/elements/progress.rb
Normal file
50
lib/cyberarm_engine/ui/elements/progress.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Progress < Element
|
||||
def initialize(options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
@fraction_background = Background.new(background: @style.fraction_background)
|
||||
self.value = options[:fraction] ? options[:fraction] : 0.0
|
||||
end
|
||||
|
||||
def render
|
||||
@fraction_background.draw
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height= dimensional_size(@style.height,:height)
|
||||
@width = _width
|
||||
@height= _height
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def update_background
|
||||
super
|
||||
|
||||
@fraction_background.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
@fraction_background.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@fraction_background.z = @z
|
||||
@fraction_background.width = @width * @fraction
|
||||
@fraction_background.height = @height
|
||||
|
||||
@fraction_background.background = @style.fraction_background
|
||||
end
|
||||
|
||||
def value
|
||||
@fraction
|
||||
end
|
||||
|
||||
def value=(decimal)
|
||||
raise "value must be number" unless decimal.is_a?(Numeric)
|
||||
|
||||
@fraction = decimal.clamp(0.0, 1.0)
|
||||
update_background
|
||||
|
||||
return @fraction
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
13
lib/cyberarm_engine/ui/elements/stack.rb
Normal file
13
lib/cyberarm_engine/ui/elements/stack.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Stack < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
move_to_next_line(child)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
54
lib/cyberarm_engine/ui/elements/toggle_button.rb
Normal file
54
lib/cyberarm_engine/ui/elements/toggle_button.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class ToggleButton < Button
|
||||
attr_reader :toggled
|
||||
|
||||
def initialize(options, block = nil)
|
||||
super(options[:checkmark], options, block)
|
||||
@toggled = options[:toggled] || false
|
||||
if @toggled
|
||||
@text.text = @options[:checkmark]
|
||||
else
|
||||
@text.text = ""
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def toggled=(boolean)
|
||||
@toggled = !boolean
|
||||
toggle
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
toggle
|
||||
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def toggle
|
||||
if @toggled
|
||||
@toggled = false
|
||||
@text.text = ""
|
||||
else
|
||||
@toggled = true
|
||||
@text.text = @options[:checkmark]
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height= dimensional_size(@style.height,:height)
|
||||
@width = _width ? _width : @text.textobject.text_width(@options[:checkmark])
|
||||
@height = _height ? _height : @text.height
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@toggled
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,15 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Flow < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
if fits_on_line?(child)
|
||||
position_on_current_line(child)
|
||||
else
|
||||
position_on_next_line(child)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,15 +10,18 @@ module CyberarmEngine
|
||||
|
||||
@down_keys = {}
|
||||
|
||||
@root_container = Stack.new
|
||||
@root_container = Element::Stack.new(gui_state: self)
|
||||
@game_objects << @root_container
|
||||
@containers = [@root_container]
|
||||
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
|
||||
@focus = nil
|
||||
@mouse_over = nil
|
||||
@mouse_down_on = {}
|
||||
@mouse_down_position = {}
|
||||
|
||||
@pending_recalculate_request = false
|
||||
|
||||
setup
|
||||
end
|
||||
@@ -35,6 +38,11 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def update
|
||||
if @pending_recalculate_request
|
||||
@root_container.recalculate
|
||||
@pending_recalculate_request = false
|
||||
end
|
||||
|
||||
super
|
||||
|
||||
new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
|
||||
@@ -49,6 +57,11 @@ module CyberarmEngine
|
||||
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
|
||||
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
|
||||
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
|
||||
|
||||
request_recalculate if @active_width != window.width || @active_height != window.height
|
||||
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
@@ -115,5 +128,10 @@ module CyberarmEngine
|
||||
def redirect_mouse_wheel(button)
|
||||
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
|
||||
end
|
||||
|
||||
# Schedule a full GUI recalculation on next update
|
||||
def request_recalculate
|
||||
@pending_recalculate_request = true
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,46 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Image < Element
|
||||
def initialize(path, options = {}, block = nil)
|
||||
super(options, block)
|
||||
@path = path
|
||||
|
||||
@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
|
||||
end
|
||||
|
||||
raise "Scale X" unless @scale_x.is_a?(Numeric)
|
||||
raise "Scale Y" unless @scale_y.is_a?(Numeric)
|
||||
end
|
||||
|
||||
def render
|
||||
@image.draw(
|
||||
@style.border_thickness_left + @style.padding_left + @x,
|
||||
@style.border_thickness_top + @style.padding_top + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y) # TODO: Add color support?
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@style.width = @image.width * @scale_x
|
||||
@style.height = @image.height * @scale_y
|
||||
end
|
||||
|
||||
def value
|
||||
@path
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,43 +0,0 @@
|
||||
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[:color], size: @options[:text_size], shadow: @options[:text_shadow])
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def render
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@style.width = @text.width.round
|
||||
@style.height= @text.height.round
|
||||
|
||||
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@text.text
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
@text.text = value
|
||||
|
||||
old_width, old_height = width, height
|
||||
recalculate
|
||||
|
||||
root.recalculate if old_width != width || old_height != height
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Stack < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
move_to_next_line(child)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,24 @@
|
||||
module Gosu
|
||||
class Color
|
||||
def _dump(level)
|
||||
[
|
||||
"%02X" % self.alpha,
|
||||
"%02X" % self.red,
|
||||
"%02X" % self.green,
|
||||
"%02X" % self.blue
|
||||
].join
|
||||
end
|
||||
|
||||
def self._load(hex)
|
||||
argb(hex.to_i(16))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module CyberarmEngine
|
||||
class Style
|
||||
def initialize(hash = {})
|
||||
@hash = hash
|
||||
@hash = Marshal.load(Marshal.dump(hash))
|
||||
end
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
|
||||
@@ -26,7 +26,23 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
hash.merge(options)
|
||||
deep_merge(hash, options)
|
||||
end
|
||||
|
||||
# Derived from Rails Hash#deep_merge!
|
||||
# Enables passing partial themes through Element options without issue
|
||||
def deep_merge(original, intergrate, &block)
|
||||
hash = original.merge(intergrate) do |key, this_val, other_val|
|
||||
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
||||
deep_merge(this_val, other_val, &block)
|
||||
elsif block_given?
|
||||
block.call(key, this_val, other_val)
|
||||
else
|
||||
other_val
|
||||
end
|
||||
end
|
||||
|
||||
return hash
|
||||
end
|
||||
|
||||
THEME = {
|
||||
@@ -74,7 +90,7 @@ module CyberarmEngine
|
||||
caret_interval: 500,
|
||||
},
|
||||
|
||||
Image: {
|
||||
Image: { # < Element
|
||||
retro: false
|
||||
},
|
||||
|
||||
@@ -88,6 +104,15 @@ module CyberarmEngine
|
||||
|
||||
ToggleButton: { # < Button
|
||||
checkmark: "√"
|
||||
},
|
||||
|
||||
Progress: { # < Element
|
||||
width: 250,
|
||||
height: 36,
|
||||
background: 0xff111111,
|
||||
fraction_background: [0xffc75e61, 0xffe26623],
|
||||
border_thickness: 4,
|
||||
border_color: [0xffd59674, 0xffff8746]
|
||||
}
|
||||
}.freeze
|
||||
end
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class ToggleButton < Button
|
||||
attr_reader :toggled
|
||||
|
||||
def initialize(options, block = nil)
|
||||
super(options[:checkmark], options, block)
|
||||
@toggled = options[:toggled] || false
|
||||
if @toggled
|
||||
@text.text = @options[:checkmark]
|
||||
else
|
||||
@text.text = ""
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
def toggled=(boolean)
|
||||
@toggled = !boolean
|
||||
toggle
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
toggle
|
||||
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def toggle
|
||||
if @toggled
|
||||
@toggled = false
|
||||
@text.text = ""
|
||||
else
|
||||
@toggled = true
|
||||
@text.text = @options[:checkmark]
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
@style.width = @text.textobject.text_width(@options[:checkmark])
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@toggled
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev"
|
||||
VERSION = "0.8.0"
|
||||
VERSION = "0.9.0"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user