UX improvements for Button:active handling, added Style stub, Default element width/height is now window width/height instead of 0,0; may need tweaking.

This commit is contained in:
2019-03-25 20:07:43 -05:00
parent 8223dd7a01
commit a8b8d5aba1
11 changed files with 82 additions and 35 deletions

View File

@@ -17,6 +17,7 @@ require_relative "cyberarm_engine/objects/multi_line_text"
require_relative "cyberarm_engine/ui/theme" require_relative "cyberarm_engine/ui/theme"
require_relative "cyberarm_engine/ui/event" require_relative "cyberarm_engine/ui/event"
require_relative "cyberarm_engine/ui/style"
require_relative "cyberarm_engine/ui/border_canvas" require_relative "cyberarm_engine/ui/border_canvas"
require_relative "cyberarm_engine/ui/element" require_relative "cyberarm_engine/ui/element"
require_relative "cyberarm_engine/ui/label" require_relative "cyberarm_engine/ui/label"

View File

@@ -15,12 +15,21 @@ module CyberarmEngine
end end
def enter(sender) def enter(sender)
@background_canvas.background = default(:hover, :background) @focus = false unless window.button_down?(Gosu::MsLeft)
@text.color = default(:hover, :color)
if @focus
@background_canvas.background = default(:active, :background)
@text.color = default(:active, :color)
else
@background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
end end
def left_mouse_button(sender, x, y) def left_mouse_button(sender, x, y)
@focus = true
@background_canvas.background = default(:active, :background) @background_canvas.background = default(:active, :background)
window.current_state.focus = self
@text.color = default(:active, :color) @text.color = default(:active, :color)
end end
@@ -28,13 +37,17 @@ module CyberarmEngine
enter(sender) enter(sender)
end end
def clicked_left_mouse_button(sender, x, y)
@block.call(self) if @block
end
def leave(sender) def leave(sender)
@background_canvas.background = default(:background) @background_canvas.background = default(:background)
@text.color = default(:color) @text.color = default(:color)
end end
def clicked_left_mouse_button(sender, x, y) def blur(sender)
@block.call(self) if @block @focus = false
end end
end end
end end

View File

@@ -14,8 +14,6 @@ module CyberarmEngine
@width = @toggle_button.width + @label.width @width = @toggle_button.width + @label.width
@height = @toggle_button.height + @label.height @height = @toggle_button.height + @label.height
recalculate
end end
def text=(text) def text=(text)

View File

@@ -40,23 +40,37 @@ module CyberarmEngine
end end
end end
def clicked_left_mouse_button(sender, x, y) def left_mouse_button(sender, x, y)
@focus = true super
window.current_state.focus=self
window.text_input = @text_input window.text_input = @text_input
@block.call(self) if @block end
def enter(sender)
if @focus
@background_canvas.background = default(:active, :background)
@text.color = default(:active, :color)
else
@background_canvas.background = default(:hover, :background)
@text.color = default(:hover, :color)
end
end
def leave(sender)
end end
def blur(sender) def blur(sender)
@focus = false @focus = false
@background_canvas.background = default(:background)
@text.color = default(:color)
window.text_input = nil window.text_input = nil
end end
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
def caret_position def caret_position
if @type == :password if @type == :password
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos].length) @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
else else
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos]) @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
end end
end end

View File

@@ -19,6 +19,8 @@ module CyberarmEngine
@options = options @options = options
@block = block @block = block
@style = Style.new(options)
@focus = false
@background_canvas = Background.new @background_canvas = Background.new
@border_canvas = BorderCanvas.new(element: self) @border_canvas = BorderCanvas.new(element: self)
@@ -29,8 +31,8 @@ module CyberarmEngine
@fixed_x = @x if @x != 0 @fixed_x = @x if @x != 0
@fixed_y = @y if @y != 0 @fixed_y = @y if @y != 0
@width = default(:width) @width = default(:width) || $window.width
@height = default(:height) @height = default(:height) || $window.height
set_border_thickness(default(:border_thickness)) set_border_thickness(default(:border_thickness))
@@ -44,23 +46,17 @@ module CyberarmEngine
raise "#{self.class} 'x' must be a number" unless @x.is_a?(Numeric) raise "#{self.class} 'x' must be a number" unless @x.is_a?(Numeric)
raise "#{self.class} 'y' must be a number" unless @y.is_a?(Numeric) raise "#{self.class} 'y' must be a number" unless @y.is_a?(Numeric)
raise "#{self.class} 'z' must be a number" unless @z.is_a?(Numeric) raise "#{self.class} 'z' must be a number" unless @z.is_a?(Numeric)
raise "#{self.class} 'width' must be a number" unless @width.is_a?(Numeric) raise "#{self.class} 'width' must be a number" unless @width.is_a?(Numeric) || @width.nil?
raise "#{self.class} 'height' must be a number" unless @height.is_a?(Numeric) raise "#{self.class} 'height' must be a number" unless @height.is_a?(Numeric) || @height.nil?
raise "#{self.class} 'options' must be a Hash" unless @options.is_a?(Hash) raise "#{self.class} 'options' must be a Hash" unless @options.is_a?(Hash)
# raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric) # raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric)
@max_width = @width if @width != 0
@max_height = @height if @height != 0
@enabled = true @enabled = true
default_events default_events
end end
def width=(n); @max_width = n; end
def height=(n); @max_height = n; end
def set_background(background) def set_background(background)
@background = background @background = background
@background_canvas.background = background @background_canvas.background = background
@@ -165,6 +161,14 @@ module CyberarmEngine
@margin_top + height + @margin_bottom @margin_top + height + @margin_bottom
end end
def style(hash)
if hash
@style.set(hash)
else
@style.hash
end
end
def background=(_background) def background=(_background)
@background_canvas.background=(_background) @background_canvas.background=(_background)
update_background update_background

View File

@@ -21,11 +21,17 @@ module CyberarmEngine
setup setup
end end
# throws :blur event to focused element and sets GuiState focused element
# Does NOT throw :focus event at element or set element as focused
def focus=(element) def focus=(element)
@focus.publish(:blur) if @focus and element @focus.publish(:blur) if @focus and element && @focus != element
@focus = element @focus = element
end end
def focused
@focus
end
def update def update
super super

View File

@@ -5,13 +5,13 @@ module CyberarmEngine
@path = path @path = path
@image = Gosu::Image.new(path, retro: @options[:image_retro]) @image = Gosu::Image.new(path, retro: @options[:image_retro])
if @options[:width].nonzero? && @options[:height].nonzero? if @options[:width] && @options[:height]
@scale_x = @options[:width].to_f / @image.width @scale_x = @options[:width].to_f / @image.width
@scale_y = @options[:height].to_f / @image.height @scale_y = @options[:height].to_f / @image.height
elsif @options[:width].nonzero? elsif @options[:width]
@scale_x = @options[:width].to_f / @image.width @scale_x = @options[:width].to_f / @image.width
@scale_y = @scale_x @scale_y = @scale_x
elsif @options[:height].nonzero? elsif @options[:height]
@scale_y = @options[:height].to_f / @image.height @scale_y = @options[:height].to_f / @image.height
@scale_x = @scale_y @scale_x = @scale_y
else else

View File

@@ -12,13 +12,8 @@ module CyberarmEngine
@text.draw @text.draw
end end
def button_up(id) def clicked_left_mouse_button(sender, x, y)
case id @block.call(self) if @block
when Gosu::MsLeft
if mouse_over?
@block.call(self) if @block
end
end
end end
def recalculate def recalculate

View File

@@ -0,0 +1,15 @@
module CyberarmEngine
class Style
def initialize(hash)
@hash = hash
end
def hash
@hash
end
def set(hash)
@hash.merge!(hash)
end
end
end

View File

@@ -32,8 +32,8 @@ module CyberarmEngine
y: 0, y: 0,
z: 30, z: 30,
width: 0, width: nil,
height: 0, height: nil,
color: Gosu::Color::WHITE, color: Gosu::Color::WHITE,
background: Gosu::Color::NONE, background: Gosu::Color::NONE,
margin: 0, margin: 0,

View File

@@ -39,6 +39,7 @@ module CyberarmEngine
super super
@width = @text.textobject.text_width(@options[:checkmark]) @width = @text.textobject.text_width(@options[:checkmark])
update_background
end end
def value def value