Elements now return :handled when they're supposed to, GameState initialization and setup are now seperated

This commit is contained in:
2019-09-12 08:14:47 -05:00
parent 6fafe98008
commit 788d987da1
10 changed files with 45 additions and 19 deletions

View File

@@ -1,50 +1,50 @@
module CyberarmEngine module CyberarmEngine
module Common module Common
def push_state(klass, options={}) def push_state(klass, options={})
$window.push_state(klass, options) window.push_state(klass, options)
end end
def current_state def current_state
$window.current_state window.current_state
end end
def previous_state def previous_state
$window.previous_state window.previous_state
end end
def pop_state def pop_state
$window.pop_state window.pop_state
end end
def show_cursor def show_cursor
$window.show_cursor window.show_cursor
end end
def show_cursor=boolean def show_cursor=boolean
$window.show_cursor = boolean window.show_cursor = boolean
end end
def draw_rect(x, y, width, height, color, z = 0) def draw_rect(x, y, width, height, color, z = 0)
$window.draw_rect(x,y,width,height,color,z) Gosu.draw_rect(x, y, width, height, color, z)
end end
def fill(color, z = 0) def fill(color, z = 0)
draw_rect(0, 0, $window.width, $window.height, color, z) draw_rect(0, 0, window.width, window.height, color, z)
end end
def lighten(color, amount = 25) def lighten(color, amount = 25)
if defined?(color.alpha) if defined?(color.alpha)
return Gosu::Color.rgba(color.red+amount, color.green+amount, color.blue+amount, color.alpha) return Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
else else
return Gosu::Color.rgb(color.red+amount, color.green+amount, color.blue+amount) return Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
end end
end end
def darken(color, amount = 25) def darken(color, amount = 25)
if defined?(color.alpha) if defined?(color.alpha)
return Gosu::Color.rgba(color.red-amount, color.green-amount, color.blue-amount, color.alpha) return Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
else else
return Gosu::Color.rgb(color.red-amount, color.green-amount, color.blue-amount) return Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
end end
end end
@@ -88,4 +88,4 @@ module CyberarmEngine
$window $window
end end
end end
end end

View File

@@ -58,11 +58,15 @@ module CyberarmEngine
end end
def push_state(klass, options={}) def push_state(klass, options={})
options = {setup: true}.merge(options)
if klass.instance_of?(klass.class) && defined?(klass.options) if klass.instance_of?(klass.class) && defined?(klass.options)
@states << klass @states << klass
klass.setup if options[:setup]
else else
@states << klass.new(options) if child_of?(klass, GameState) @states << klass.new(options) if child_of?(klass, GameState)
@states << klass.new if child_of?(klass, Element::Container) @states << klass.new if child_of?(klass, Element::Container)
current_state.setup if current_state.class == klass && options[:setup]
end end
end end
@@ -94,4 +98,4 @@ module CyberarmEngine
end end
end end
end end
end end

View File

@@ -155,7 +155,7 @@ module CyberarmEngine
def button_up(id) def button_up(id)
end end
def button_down?(id) def button_down(id)
end end
def find_closest(game_object_class) def find_closest(game_object_class)

View File

@@ -12,8 +12,6 @@ module CyberarmEngine
$window.text_input = nil unless options[:preserve_text_input] $window.text_input = nil unless options[:preserve_text_input]
@down_keys = {} @down_keys = {}
setup
end end
def setup def setup

View File

@@ -25,6 +25,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background) @style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color) @text.color = default(:hover, :color)
end end
return :handled
end end
def left_mouse_button(sender, x, y) def left_mouse_button(sender, x, y)
@@ -32,23 +34,33 @@ module CyberarmEngine
@style.background_canvas.background = default(:active, :background) @style.background_canvas.background = default(:active, :background)
window.current_state.focus = self window.current_state.focus = self
@text.color = default(:active, :color) @text.color = default(:active, :color)
return :handled
end end
def released_left_mouse_button(sender,x, y) def released_left_mouse_button(sender,x, y)
enter(sender) enter(sender)
return :handled
end end
def clicked_left_mouse_button(sender, x, y) def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block @block.call(self) if @block
return :handled
end end
def leave(sender) def leave(sender)
@style.background_canvas.background = default(:background) @style.background_canvas.background = default(:background)
@text.color = default(:color) @text.color = default(:color)
return :handled
end end
def blur(sender) def blur(sender)
@focus = false @focus = false
return :handled
end end
end end
end end

View File

@@ -46,6 +46,8 @@ module CyberarmEngine
@caret_last_interval = Gosu.milliseconds @caret_last_interval = Gosu.milliseconds
@show_caret = true @show_caret = true
return :handled
end end
def enter(sender) def enter(sender)
@@ -56,12 +58,16 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background) @style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color) @text.color = default(:hover, :color)
end end
return :handled
end end
def leave(sender) def leave(sender)
unless @focus unless @focus
super super
end end
return :handled
end end
def blur(sender) def blur(sender)
@@ -69,6 +75,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:background) @style.background_canvas.background = default(:background)
@text.color = default(:color) @text.color = default(:color)
window.text_input = nil window.text_input = nil
return :handled
end end
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text # TODO: Fix caret rendering in wrong position unless caret_pos is at end of text

View File

@@ -19,6 +19,8 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y) def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block @block.call(self) if @block
return :handled
end end
def recalculate def recalculate

View File

@@ -13,6 +13,8 @@ module CyberarmEngine
def clicked_left_mouse_button(sender, x, y) def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block @block.call(self) if @block
return :handled
end end
def recalculate def recalculate

View File

@@ -24,6 +24,8 @@ module CyberarmEngine
toggle toggle
@block.call(self) if @block @block.call(self) if @block
return :handled
end end
def toggle def toggle

View File

@@ -22,8 +22,6 @@ module CyberarmEngine
@mouse_down_on = {} @mouse_down_on = {}
@mouse_down_position = {} @mouse_down_position = {}
@pending_recalculate_request = false @pending_recalculate_request = false
setup
end end
# throws :blur event to focused element and sets GuiState focused element # throws :blur event to focused element and sets GuiState focused element