mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Sliders work better, fixed CheckBox not passing along options hash, Text width/height now accept a string, changed EditLine text selection color, temporary back to back gui recalculations to fix positioning errors until Container#layout can safely be called after determining element width and height.
This commit is contained in:
@@ -8,6 +8,7 @@ require "json"
|
|||||||
require "gosu_more_drawables"
|
require "gosu_more_drawables"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/version"
|
require_relative "cyberarm_engine/version"
|
||||||
|
require_relative "cyberarm_engine/stats"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/common"
|
require_relative "cyberarm_engine/common"
|
||||||
|
|
||||||
|
|||||||
21
lib/cyberarm_engine/stats.rb
Normal file
21
lib/cyberarm_engine/stats.rb
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Stats
|
||||||
|
@@hash = {
|
||||||
|
gui_recalculations_last_frame: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
def self.get(key)
|
||||||
|
@@hash.dig(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.increment(key, n)
|
||||||
|
@@hash[key] += n
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.clear
|
||||||
|
@@hash.each do |key, value|
|
||||||
|
@@hash[key] = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -9,7 +9,7 @@ module CyberarmEngine
|
|||||||
@text = text.to_s || ""
|
@text = text.to_s || ""
|
||||||
@options = options
|
@options = options
|
||||||
@size = options[:size] || 18
|
@size = options[:size] || 18
|
||||||
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
@font = options[:font] || Gosu.default_font_name
|
||||||
@x = options[:x] || 0
|
@x = options[:x] || 0
|
||||||
@y = options[:y] || 0
|
@y = options[:y] || 0
|
||||||
@z = options[:z] || 1025
|
@z = options[:z] || 1025
|
||||||
@@ -98,12 +98,12 @@ module CyberarmEngine
|
|||||||
@shadow_color = n
|
@shadow_color = n
|
||||||
end
|
end
|
||||||
|
|
||||||
def width
|
def width(text = @text)
|
||||||
textobject.text_width(@text)
|
textobject.text_width(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
def height
|
def height(text = @text)
|
||||||
@text.lines.count > 0 ? (@text.lines.count) * textobject.height : @textobject.height
|
text.lines.count > 0 ? (text.lines.count) * textobject.height : @textobject.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ module CyberarmEngine
|
|||||||
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||||
if size && size.is_a?(Numeric)
|
if size && size.is_a?(Numeric)
|
||||||
if size.between?(0.0, 1.0)
|
if size.between?(0.0, 1.0)
|
||||||
((@parent.send(:"content_#{dimension}") - self.send(:"noncontent_#{dimension}") - 1) * size).round
|
((@parent.send(:"content_#{dimension}") - self.send(:"noncontent_#{dimension}")) * size).round
|
||||||
else
|
else
|
||||||
size
|
size
|
||||||
end
|
end
|
||||||
@@ -246,6 +246,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
|
return self if is_root?
|
||||||
|
|
||||||
unless @root && @root.parent.nil?
|
unless @root && @root.parent.nil?
|
||||||
@root = parent
|
@root = parent
|
||||||
|
|
||||||
@@ -279,5 +281,9 @@ module CyberarmEngine
|
|||||||
def value=(value)
|
def value=(value)
|
||||||
raise "#{self.class}#value= was not overridden!"
|
raise "#{self.class}#value= was not overridden!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{ value.is_a?(String) ? "\"#{value}\"" : value }"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -2,7 +2,7 @@ module CyberarmEngine
|
|||||||
class Element
|
class Element
|
||||||
class CheckBox < Flow
|
class CheckBox < Flow
|
||||||
def initialize(text, options, block = nil)
|
def initialize(text, options, block = nil)
|
||||||
super({}, block = nil)
|
super(options, block)
|
||||||
options[:toggled] = options[:checked]
|
options[:toggled] = options[:checked]
|
||||||
|
|
||||||
@toggle_button = ToggleButton.new(options)
|
@toggle_button = ToggleButton.new(options)
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ module CyberarmEngine
|
|||||||
def build
|
def build
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
def add(element)
|
def add(element)
|
||||||
@children << element
|
@children << element
|
||||||
|
|
||||||
recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear(&block)
|
def clear(&block)
|
||||||
@@ -41,7 +41,6 @@ module CyberarmEngine
|
|||||||
|
|
||||||
$__current_container__ = old_container
|
$__current_container__ = old_container
|
||||||
|
|
||||||
recalculate
|
|
||||||
root.gui_state.request_recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -75,6 +74,9 @@ module CyberarmEngine
|
|||||||
def recalculate
|
def recalculate
|
||||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||||
return unless visible?
|
return unless visible?
|
||||||
|
|
||||||
|
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||||
|
|
||||||
stylize
|
stylize
|
||||||
|
|
||||||
layout
|
layout
|
||||||
@@ -92,7 +94,6 @@ module CyberarmEngine
|
|||||||
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Move child to parent after positioning
|
# Move child to parent after positioning
|
||||||
@children.each do |child|
|
@children.each do |child|
|
||||||
child.x += (@x + @style.border_thickness_left) - style.margin_left
|
child.x += (@x + @style.border_thickness_left) - style.margin_left
|
||||||
@@ -101,6 +102,8 @@ module CyberarmEngine
|
|||||||
child.stylize
|
child.stylize
|
||||||
child.recalculate
|
child.recalculate
|
||||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||||
|
|
||||||
|
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
@@ -110,13 +113,13 @@ module CyberarmEngine
|
|||||||
raise "Not overridden"
|
raise "Not overridden"
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Fix @max_width no longer exists
|
|
||||||
def max_width
|
def max_width
|
||||||
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_width ? outer_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Fix container automatic width (0.0..1.0) not considered
|
|
||||||
def fits_on_line?(element) # Flow
|
def fits_on_line?(element) # Flow
|
||||||
|
p [@options[:id], @width] if @options[:id]
|
||||||
@current_position.x + element.outer_width <= max_width &&
|
@current_position.x + element.outer_width <= max_width &&
|
||||||
@current_position.x + element.outer_width <= window.width
|
@current_position.x + element.outer_width <= window.width
|
||||||
end
|
end
|
||||||
@@ -175,6 +178,25 @@ module CyberarmEngine
|
|||||||
def value
|
def value
|
||||||
@children.map {|c| c.class}.join(", ")
|
@children.map {|c| c.class}.join(", ")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_tree(indent = "", index = 0)
|
||||||
|
puts self
|
||||||
|
|
||||||
|
indent = indent + " "
|
||||||
|
@children.each_with_index do |child, i|
|
||||||
|
print "#{indent}#{i}: "
|
||||||
|
|
||||||
|
if child.is_a?(Container)
|
||||||
|
child.write_tree(indent)
|
||||||
|
else
|
||||||
|
puts child
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -100,6 +100,22 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def caret_position
|
||||||
|
text_input_position_for(:caret_pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
def selection_start_position
|
||||||
|
text_input_position_for(:selection_start)
|
||||||
|
end
|
||||||
|
|
||||||
|
def text_input_position_for(method)
|
||||||
|
if @type == :password
|
||||||
|
@text.x + @text.width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
|
||||||
|
else
|
||||||
|
@text.x + @text.width(@text_input.text[0..@text_input.send(method)])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
def left_mouse_button(sender, x, y)
|
||||||
super
|
super
|
||||||
window.text_input = @text_input
|
window.text_input = @text_input
|
||||||
@@ -141,22 +157,6 @@ module CyberarmEngine
|
|||||||
return :handled
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def caret_position
|
|
||||||
text_input_position_for(:caret_pos)
|
|
||||||
end
|
|
||||||
|
|
||||||
def selection_start_position
|
|
||||||
text_input_position_for(:selection_start)
|
|
||||||
end
|
|
||||||
|
|
||||||
def text_input_position_for(method)
|
|
||||||
if @type == :password
|
|
||||||
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
|
|
||||||
else
|
|
||||||
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.send(method)])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
super
|
super
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Element
|
class Element
|
||||||
class Flow < Container
|
class Flow < Container
|
||||||
include Common
|
|
||||||
|
|
||||||
def layout
|
def layout
|
||||||
@children.each do |child|
|
@children.each do |child|
|
||||||
if fits_on_line?(child)
|
if fits_on_line?(child)
|
||||||
|
|||||||
@@ -52,16 +52,20 @@ module CyberarmEngine
|
|||||||
@width = _width
|
@width = _width
|
||||||
@height = _height
|
@height = _height
|
||||||
|
|
||||||
@handle.x = @x + @style.border_thickness_left + @style.padding_left
|
position_handle
|
||||||
@handle.y = @y + @style.border_thickness_top + @style.padding_top
|
|
||||||
@handle.recalculate
|
@handle.recalculate
|
||||||
@handle.update_background
|
@handle.update_background
|
||||||
|
|
||||||
# pp @handle.height
|
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def position_handle
|
||||||
|
@handle.x = @x + @style.padding_left + @style.border_thickness_left +
|
||||||
|
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
|
||||||
|
|
||||||
|
@handle.y = @y + @style.border_thickness_top + @style.padding_top
|
||||||
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
super
|
super
|
||||||
|
|
||||||
@@ -82,13 +86,9 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_dragged_to(x, y)
|
def handle_dragged_to(x, y)
|
||||||
puts
|
@ratio = ((x - @handle.width / 2) - @x) / (content_width - @handle.outer_width)
|
||||||
pp x, y, @handle.width, content_width
|
|
||||||
@ratio = ((x - @handle.width) - @x) / content_width
|
|
||||||
|
|
||||||
# p [@ratio, @value]
|
|
||||||
self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
|
self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
@@ -97,9 +97,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def value=(n)
|
def value=(n)
|
||||||
@value = n
|
@value = n
|
||||||
@handle.x = @x + @style.padding_left + @style.border_thickness_left +
|
position_handle
|
||||||
(content_width * (@value - @range.min) / (@range.max - @range.min).to_f)
|
|
||||||
|
|
||||||
@handle.recalculate
|
@handle.recalculate
|
||||||
|
|
||||||
publish(:changed, @value)
|
publish(:changed, @value)
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Element
|
class Element
|
||||||
class Stack < Container
|
class Stack < Container
|
||||||
include Common
|
|
||||||
|
|
||||||
def layout
|
def layout
|
||||||
@children.each do |child|
|
@children.each do |child|
|
||||||
move_to_next_line(child)
|
move_to_next_line(child)
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
if @pending_recalculate_request
|
if @pending_recalculate_request
|
||||||
|
@root_container.recalculate
|
||||||
@root_container.recalculate
|
@root_container.recalculate
|
||||||
@pending_recalculate_request = false
|
@pending_recalculate_request = false
|
||||||
end
|
end
|
||||||
@@ -104,6 +105,8 @@ module CyberarmEngine
|
|||||||
redirect_mouse_button(:middle)
|
redirect_mouse_button(:middle)
|
||||||
when Gosu::MsRight
|
when Gosu::MsRight
|
||||||
redirect_mouse_button(:right)
|
redirect_mouse_button(:right)
|
||||||
|
when Gosu::KbF5
|
||||||
|
request_recalculate
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ module CyberarmEngine
|
|||||||
caret_width: 2,
|
caret_width: 2,
|
||||||
caret_color: Gosu::Color::WHITE,
|
caret_color: Gosu::Color::WHITE,
|
||||||
caret_interval: 500,
|
caret_interval: 500,
|
||||||
selection_color: Gosu::Color::GREEN,
|
selection_color: Gosu::Color.rgba(255, 128, 50, 200),
|
||||||
},
|
},
|
||||||
|
|
||||||
Image: { # < Element
|
Image: { # < Element
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
Stats.clear
|
||||||
|
|
||||||
current_state.update if current_state
|
current_state.update if current_state
|
||||||
@last_frame_time = Gosu.milliseconds-@current_frame_time
|
@last_frame_time = Gosu.milliseconds-@current_frame_time
|
||||||
@current_frame_time = Gosu.milliseconds
|
@current_frame_time = Gosu.milliseconds
|
||||||
|
|||||||
Reference in New Issue
Block a user