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 Common
def push_state(klass, options={})
$window.push_state(klass, options)
window.push_state(klass, options)
end
def current_state
$window.current_state
window.current_state
end
def previous_state
$window.previous_state
window.previous_state
end
def pop_state
$window.pop_state
window.pop_state
end
def show_cursor
$window.show_cursor
window.show_cursor
end
def show_cursor=boolean
$window.show_cursor = boolean
window.show_cursor = boolean
end
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
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
def lighten(color, amount = 25)
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
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
def darken(color, amount = 25)
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
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
@@ -88,4 +88,4 @@ module CyberarmEngine
$window
end
end
end
end

View File

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

View File

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

View File

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

View File

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

View File

@@ -46,6 +46,8 @@ module CyberarmEngine
@caret_last_interval = Gosu.milliseconds
@show_caret = true
return :handled
end
def enter(sender)
@@ -56,12 +58,16 @@ module CyberarmEngine
@style.background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
return :handled
end
def leave(sender)
unless @focus
super
end
return :handled
end
def blur(sender)
@@ -69,6 +75,8 @@ module CyberarmEngine
@style.background_canvas.background = default(:background)
@text.color = default(:color)
window.text_input = nil
return :handled
end
# 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)
@block.call(self) if @block
return :handled
end
def recalculate

View File

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

View File

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

View File

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