Compare commits

8 Commits

7 changed files with 78 additions and 21 deletions

View File

@@ -4,7 +4,7 @@ module CyberarmEngine
include Event
include Common
attr_accessor :x, :y, :z, :tip
attr_accessor :x, :y, :z, :tip, :element_visible
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
def initialize(options = {}, block = nil)
@@ -13,9 +13,9 @@ module CyberarmEngine
@options = options
@block = block
@focus = @options[:focus].nil? ? false : @options[:focus]
@enabled = @options[:enabled].nil? ? true : @options[:enabled]
@visible = @options[:visible].nil? ? true : @options[:visible]
@focus = !@options.key?(:focus) ? false : @options[:focus]
@enabled = !@options.key?(:enabled) ? true : @options[:enabled]
@visible = !@options.key?(:visible) ? true : @options[:visible]
@tip = @options[:tip] || ""
@debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
@@ -24,6 +24,7 @@ module CyberarmEngine
@root ||= nil
@gui_state ||= nil
@element_visible = true
@x = @style.x
@y = @style.y
@@ -260,6 +261,10 @@ module CyberarmEngine
@visible
end
def element_visible?
@element_visible
end
def toggle
@visible = !@visible
root.gui_state.request_recalculate
@@ -279,15 +284,14 @@ module CyberarmEngine
def draw
return unless visible?
return unless element_visible?
@style.background_canvas.draw
@style.background_nine_slice_canvas.draw
@style.border_canvas.draw
Gosu.clip_to(@x, @y, width, height) do
render
end
end
def debug_draw
return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT

View File

@@ -86,9 +86,12 @@ module CyberarmEngine
old_width = width
old_height = height
recalculate
root.gui_state.request_recalculate if old_width != width || old_height != height
if old_width != width || old_height != height
root.gui_state.request_recalculate
else
recalculate
end
publish(:changed, self.value)
end

View File

@@ -45,7 +45,7 @@ module CyberarmEngine
root.gui_state.request_recalculate
end
def apend(&block)
def append(&block)
old_container = $__current_container__
$__current_container__ = self
@@ -57,7 +57,12 @@ module CyberarmEngine
end
def render
Gosu.clip_to(@x, @y, width, height) do
Gosu.clip_to(
@x + @style.border_thickness_left + @style.padding_left,
@y + @style.border_thickness_top + @style.padding_top,
content_width + 1,
content_height + 1
) do
@children.each(&:draw)
end
end
@@ -103,6 +108,8 @@ module CyberarmEngine
stylize
# s = Gosu.milliseconds
layout
if is_root?
@@ -129,8 +136,13 @@ module CyberarmEngine
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
Stats.increment(:gui_recalculations_last_frame, 1)
child.element_visible = child.x >= @x - child.width && child.x <= @x + width &&
child.y >= @y - child.height && child.y <= @y + height
end
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
update_background
end
@@ -194,9 +206,8 @@ module CyberarmEngine
if @scroll_position.y < 0
@scroll_position.y += @scroll_speed
@scroll_position.y = 0 if @scroll_position.y > 0
# recalculate
root.gui_state.request_recalculate_for(self)
root.gui_state.request_recalculate_for(self)
return :handled
end
@@ -210,9 +221,8 @@ module CyberarmEngine
if @scroll_position.y.abs < max_scroll_height
@scroll_position.y -= @scroll_speed
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
# recalculate
root.gui_state.request_recalculate_for(self)
root.gui_state.request_recalculate_for(self)
return :handled
end

View File

@@ -1,9 +1,16 @@
module CyberarmEngine
class Element
class Progress < Element
attr_reader :type
def initialize(options = {}, block = nil)
super(options, block)
@animation_speed = options[:animation_speed] || 3_000
@marquee_width = options[:marquee_width] || 0.25
@marquee_offset = 0
@marquee_animation_time = Gosu.milliseconds
@type = options[:type] || :linear
@fraction_background = Background.new(background: @style.fraction_background)
self.value = options[:fraction] || 0.0
end
@@ -24,15 +31,45 @@ module CyberarmEngine
def update_background
super
@fraction_background.x = @style.border_thickness_left + @style.padding_left + @x
@fraction_background.x = (@style.border_thickness_left + @style.padding_left + @x) + @marquee_offset
@fraction_background.y = @style.border_thickness_top + @style.padding_top + @y
@fraction_background.z = @z
@fraction_background.width = @width * @fraction
@fraction_background.width = @width * (@type == :marquee ? @marquee_width : @fraction)
@fraction_background.height = @height
@fraction_background.background = @style.fraction_background
end
def update
super
return unless @type == :marquee
marquee_width = @width * @marquee_width
range = @width + marquee_width
@marquee_offset = (@width * (Gosu.milliseconds - @marquee_animation_time) / @animation_speed) - marquee_width
@marquee_animation_time = Gosu.milliseconds if @marquee_offset > range
update_background
end
def type=(type)
@type = type
case type
when :linear
@marquee_offset = 0
when :marquee
@marquee_offset = 0
@marquee_animation_time = Gosu.milliseconds
else
raise ArgumentError, "Only types :linear and :marquee are supported"
end
update_background
end
def value
@fraction
end

View File

@@ -142,9 +142,12 @@ module CyberarmEngine
old_width = width
old_height = height
recalculate
root.gui_state.request_recalculate if old_width != width || old_height != height
if old_width != width || old_height != height
root.gui_state.request_recalculate
else
recalculate
end
publish(:changed, self.value)
end

View File

@@ -75,10 +75,10 @@ module CyberarmEngine
@root_container.recalculate
@pending_recalculate_request = false
@pending_element_recalculate_requests.clear # GUI has already been recalculated
end
@pending_element_recalculate_requests.each(&:recalculate)
@pending_element_recalculate_requests.clear
if @pending_focus_request
@pending_focus_request = false

View File

@@ -1,4 +1,4 @@
module CyberarmEngine
NAME = "InDev".freeze
VERSION = "0.19.1".freeze
VERSION = "0.20.0".freeze
end