mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 20970e5aa9 | |||
| 76eb1a85d5 | |||
| e9d75d17bf | |||
| e8bb2cac17 | |||
| 29fbac7140 | |||
| d050f63c2b | |||
| af24fc8690 | |||
| f63b893c70 | |||
| 5adc27feef | |||
| 62636158f7 | |||
| 2179e11ba1 | |||
| 1ac5e0695e | |||
| 732dc2c957 | |||
| 886680ab31 | |||
| 0268a8a5fb | |||
| 1c22c36d6b |
@@ -29,11 +29,11 @@ Gem::Specification.new do |spec|
|
|||||||
|
|
||||||
spec.add_dependency "clipboard", "~> 1.3.5"
|
spec.add_dependency "clipboard", "~> 1.3.5"
|
||||||
spec.add_dependency "excon", "~> 0.78.0"
|
spec.add_dependency "excon", "~> 0.78.0"
|
||||||
spec.add_dependency "gosu", "~> 1.0.0"
|
spec.add_dependency "gosu", "~> 1.1"
|
||||||
spec.add_dependency "gosu_more_drawables", "~> 0.3"
|
spec.add_dependency "gosu_more_drawables", "~> 0.3"
|
||||||
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 1.16"
|
spec.add_development_dependency "bundler", "~> 2.2"
|
||||||
spec.add_development_dependency "minitest", "~> 5.0"
|
spec.add_development_dependency "minitest", "~> 5.0"
|
||||||
spec.add_development_dependency "rake", "~> 13.0"
|
spec.add_development_dependency "rake", "~> 13.0"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ require_relative "cyberarm_engine/ui/event"
|
|||||||
require_relative "cyberarm_engine/ui/style"
|
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/elements/label"
|
require_relative "cyberarm_engine/ui/elements/text_block"
|
||||||
require_relative "cyberarm_engine/ui/elements/button"
|
require_relative "cyberarm_engine/ui/elements/button"
|
||||||
require_relative "cyberarm_engine/ui/elements/toggle_button"
|
require_relative "cyberarm_engine/ui/elements/toggle_button"
|
||||||
require_relative "cyberarm_engine/ui/elements/list_box"
|
require_relative "cyberarm_engine/ui/elements/list_box"
|
||||||
|
|||||||
@@ -8,11 +8,29 @@ module CyberarmEngine
|
|||||||
container(CyberarmEngine::Element::Stack, options, &block)
|
container(CyberarmEngine::Element::Stack, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Remove in version 0.16.0+
|
||||||
def label(text, options = {}, &block)
|
def label(text, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
|
|
||||||
add_element(Element::Label.new(text, options, block))
|
add_element(Element::TextBlock.new(text, options, block))
|
||||||
|
end
|
||||||
|
|
||||||
|
[
|
||||||
|
"Banner",
|
||||||
|
"Title",
|
||||||
|
"Subtitle",
|
||||||
|
"Tagline",
|
||||||
|
"Caption",
|
||||||
|
"Para",
|
||||||
|
"Inscription"
|
||||||
|
].each do |const|
|
||||||
|
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
||||||
|
options[:parent] = element_parent
|
||||||
|
options[:theme] = current_theme
|
||||||
|
|
||||||
|
add_element(Element.const_get(const).new(text, options, block))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def button(text, options = {}, &block)
|
def button(text, options = {}, &block)
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ module CyberarmEngine
|
|||||||
@options = options
|
@options = options
|
||||||
@block = block
|
@block = block
|
||||||
|
|
||||||
@focus = false
|
@focus = @options[:focus].nil? ? false : @options[:focus]
|
||||||
@enabled = true
|
@enabled = @options[:enabled].nil? ? true : @options[:enabled]
|
||||||
@visible = true
|
@visible = @options[:visible].nil? ? true : @options[:visible]
|
||||||
@tip = @options[:tip] || ""
|
@tip = @options[:tip] || ""
|
||||||
|
|
||||||
@style = Style.new(options)
|
@style = Style.new(options)
|
||||||
|
|
||||||
@@ -39,6 +39,8 @@ module CyberarmEngine
|
|||||||
stylize
|
stylize
|
||||||
|
|
||||||
default_events
|
default_events
|
||||||
|
|
||||||
|
root.gui_state.request_focus(self) if @options[:autofocus]
|
||||||
end
|
end
|
||||||
|
|
||||||
def stylize
|
def stylize
|
||||||
@@ -116,6 +118,7 @@ module CyberarmEngine
|
|||||||
event(:hover)
|
event(:hover)
|
||||||
event(:leave)
|
event(:leave)
|
||||||
|
|
||||||
|
event(:focus)
|
||||||
event(:blur)
|
event(:blur)
|
||||||
|
|
||||||
event(:changed)
|
event(:changed)
|
||||||
@@ -226,6 +229,22 @@ module CyberarmEngine
|
|||||||
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def scroll_width
|
||||||
|
@children.sum { |c| c.width } + noncontent_width
|
||||||
|
end
|
||||||
|
|
||||||
|
def scroll_height
|
||||||
|
@children.sum { |c| c.height } + noncontent_height
|
||||||
|
end
|
||||||
|
|
||||||
|
def max_scroll_width
|
||||||
|
scroll_width - width
|
||||||
|
end
|
||||||
|
|
||||||
|
def max_scroll_height
|
||||||
|
scroll_height - height
|
||||||
|
end
|
||||||
|
|
||||||
def dimensional_size(size, dimension)
|
def dimensional_size(size, dimension)
|
||||||
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
||||||
|
|
||||||
@@ -277,6 +296,12 @@ module CyberarmEngine
|
|||||||
@gui_state != nil
|
@gui_state != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def focus(_)
|
||||||
|
warn "#{self.class}#focus was not overridden!"
|
||||||
|
|
||||||
|
:handled
|
||||||
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
raise "#{self.class}#recalculate was not overridden!"
|
raise "#{self.class}#recalculate was not overridden!"
|
||||||
end
|
end
|
||||||
@@ -295,5 +320,9 @@ module CyberarmEngine
|
|||||||
def to_s
|
def to_s
|
||||||
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
|
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Element
|
class Element
|
||||||
class Button < Label
|
class Button < TextBlock
|
||||||
def initialize(text_or_image, options = {}, block = nil)
|
def initialize(text_or_image, options = {}, block = nil)
|
||||||
@image = nil
|
@image = nil
|
||||||
@scale_x = 1
|
@scale_x = 1
|
||||||
@@ -10,7 +10,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
super(text_or_image, options, block)
|
super(text_or_image, options, block)
|
||||||
|
|
||||||
@style.background_canvas.background = default(:background)
|
@style.background_canvas.background = @style.background
|
||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
@@ -37,12 +37,15 @@ module CyberarmEngine
|
|||||||
def enter(_sender)
|
def enter(_sender)
|
||||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||||
|
|
||||||
if @focus
|
if !@enabled
|
||||||
@style.background_canvas.background = default(:active, :background)
|
@style.background_canvas.background = @style.disabled[:background]
|
||||||
@text.color = default(:active, :color)
|
@text.color = @style.disabled[:color]
|
||||||
|
elsif @focus
|
||||||
|
@style.background_canvas.background = @style.active[:background]
|
||||||
|
@text.color = @style.active[:color]
|
||||||
else
|
else
|
||||||
@style.background_canvas.background = default(:hover, :background)
|
@style.background_canvas.background = @style.hover[:background]
|
||||||
@text.color = default(:hover, :color)
|
@text.color = @style.hover[:color]
|
||||||
end
|
end
|
||||||
|
|
||||||
:handled
|
:handled
|
||||||
@@ -50,9 +53,16 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def left_mouse_button(_sender, _x, _y)
|
def left_mouse_button(_sender, _x, _y)
|
||||||
@focus = true
|
@focus = true
|
||||||
@style.background_canvas.background = default(:active, :background)
|
|
||||||
|
unless @enabled
|
||||||
|
@style.background_canvas.background = @style.disabled[:background]
|
||||||
|
@text.color = @style.disabled[:color]
|
||||||
|
else
|
||||||
|
@style.background_canvas.background = @style.active[:background]
|
||||||
|
@text.color = @style.active[:color]
|
||||||
|
end
|
||||||
|
|
||||||
window.current_state.focus = self
|
window.current_state.focus = self
|
||||||
@text.color = default(:active, :color)
|
|
||||||
|
|
||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
@@ -64,14 +74,19 @@ module CyberarmEngine
|
|||||||
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 @enabled && @block
|
||||||
|
|
||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def leave(_sender)
|
def leave(_sender)
|
||||||
@style.background_canvas.background = default(:background)
|
unless @enabled
|
||||||
@text.color = default(:color)
|
@style.background_canvas.background = @style.disabled[:background]
|
||||||
|
@text.color = @style.disabled[:color]
|
||||||
|
else
|
||||||
|
@style.background_canvas.background = @style.background
|
||||||
|
@text.color = @style.color
|
||||||
|
end
|
||||||
|
|
||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
@@ -83,6 +98,14 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
|
unless @enabled
|
||||||
|
@style.background_canvas.background = @style.disabled[:background]
|
||||||
|
@text.color = @style.disabled[:color]
|
||||||
|
else
|
||||||
|
@style.background_canvas.background = @style.background
|
||||||
|
@text.color = @style.color
|
||||||
|
end
|
||||||
|
|
||||||
if @image
|
if @image
|
||||||
@width = 0
|
@width = 0
|
||||||
@height = 0
|
@height = 0
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ module CyberarmEngine
|
|||||||
options[:toggled] = options[:checked]
|
options[:toggled] = options[:checked]
|
||||||
|
|
||||||
@toggle_button = ToggleButton.new(options)
|
@toggle_button = ToggleButton.new(options)
|
||||||
@label = Label.new(text, options)
|
@label = TextBlock.new(text, options)
|
||||||
|
|
||||||
@label.subscribe(:holding_left_mouse_button) do |sender, x, y|
|
@label.subscribe(:holding_left_mouse_button) do |sender, x, y|
|
||||||
@toggle_button.left_mouse_button(sender, x, y)
|
@toggle_button.left_mouse_button(sender, x, y)
|
||||||
|
|||||||
@@ -4,19 +4,20 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :stroke_color, :fill_color
|
attr_accessor :stroke_color, :fill_color
|
||||||
attr_reader :children, :gui_state, :scroll_x, :scroll_y
|
attr_reader :children, :gui_state, :scroll_position
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
def initialize(options = {}, block = nil)
|
||||||
@gui_state = options.delete(:gui_state)
|
@gui_state = options.delete(:gui_state)
|
||||||
super
|
super
|
||||||
|
|
||||||
@scroll_x = 0
|
@scroll_position = Vector.new(0, 0)
|
||||||
@scroll_y = 0
|
@scroll_speed = 40
|
||||||
@scroll_speed = 10
|
|
||||||
|
|
||||||
@text_color = options[:color]
|
@text_color = options[:color]
|
||||||
|
|
||||||
@children = []
|
@children = []
|
||||||
|
|
||||||
|
event(:window_size_changed)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build
|
def build
|
||||||
@@ -44,6 +45,17 @@ module CyberarmEngine
|
|||||||
root.gui_state.request_recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def apend(&block)
|
||||||
|
old_container = $__current_container__
|
||||||
|
|
||||||
|
$__current_container__ = self
|
||||||
|
block.call(self) if block
|
||||||
|
|
||||||
|
$__current_container__ = old_container
|
||||||
|
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
Gosu.clip_to(@x, @y, width, height) do
|
Gosu.clip_to(@x, @y, width, height) do
|
||||||
@children.each(&:draw)
|
@children.each(&:draw)
|
||||||
@@ -98,6 +110,8 @@ 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)
|
||||||
|
@current_position += @scroll_position
|
||||||
|
|
||||||
return unless visible?
|
return unless visible?
|
||||||
|
|
||||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||||
@@ -189,15 +203,31 @@ module CyberarmEngine
|
|||||||
@current_position.y += element.outer_height
|
@current_position.y += element.outer_height
|
||||||
end
|
end
|
||||||
|
|
||||||
# def mouse_wheel_up(sender, x, y)
|
def mouse_wheel_up(sender, x, y)
|
||||||
# @children.each {|c| c.y -= @scroll_speed}
|
return unless @style.scroll
|
||||||
# @children.each {|c| c.recalculate}
|
return if height < max_scroll_height
|
||||||
# end
|
|
||||||
|
|
||||||
# def mouse_wheel_down(sender, x, y)
|
if @scroll_position.y < 0
|
||||||
# @children.each {|c| c.y += @scroll_speed}
|
@scroll_position.y += @scroll_speed
|
||||||
# @children.each {|c| c.recalculate}
|
@scroll_position.y = 0 if @scroll_position.y > 0
|
||||||
# end
|
recalculate
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def mouse_wheel_down(sender, x, y)
|
||||||
|
return unless @style.scroll
|
||||||
|
return if height < max_scroll_height
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
@children.map { |c| c.class }.join(", ")
|
@children.map { |c| c.class }.join(", ")
|
||||||
|
|||||||
@@ -67,6 +67,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
if @last_text_value != value
|
if @last_text_value != value
|
||||||
@last_text_value = value
|
@last_text_value = value
|
||||||
|
@show_caret = true
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
|
||||||
publish(:changed, value)
|
publish(:changed, value)
|
||||||
end
|
end
|
||||||
@@ -210,6 +212,16 @@ module CyberarmEngine
|
|||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def focus(sender)
|
||||||
|
@focus = true
|
||||||
|
@style.background_canvas.background = default(:active, :background)
|
||||||
|
@text.color = default(:active, :color)
|
||||||
|
window.text_input = @text_input
|
||||||
|
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
|
||||||
|
|
||||||
|
:handled
|
||||||
|
end
|
||||||
|
|
||||||
def blur(_sender)
|
def blur(_sender)
|
||||||
@focus = false
|
@focus = false
|
||||||
@style.background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
@@ -251,6 +263,10 @@ module CyberarmEngine
|
|||||||
def value
|
def value
|
||||||
@text_input.text
|
@text_input.text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def value=(string)
|
||||||
|
@text_input.text = string
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Element
|
class Element
|
||||||
class Image < Element
|
class Image < Element
|
||||||
def initialize(path, options = {}, block = nil)
|
def initialize(path_or_image, options = {}, block = nil)
|
||||||
super(options, block)
|
super(options, block)
|
||||||
@path = path
|
@path = path_or_image if path_or_image.is_a?(String)
|
||||||
|
|
||||||
|
@image = Gosu::Image.new(path_or_image, retro: @options[:retro], tileable: @options[:tileable]) if @path
|
||||||
|
@image = path_or_image unless @path
|
||||||
|
|
||||||
@image = Gosu::Image.new(path, retro: @options[:image_retro])
|
|
||||||
@scale_x = 1
|
@scale_x = 1
|
||||||
@scale_y = 1
|
@scale_y = 1
|
||||||
end
|
end
|
||||||
@@ -45,9 +47,24 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@width = _width || @image.width.round * @scale_x
|
@width = _width || @image.width.round * @scale_x
|
||||||
@height = _height || @image.height.round * @scale_y
|
@height = _height || @image.height.round * @scale_y
|
||||||
|
|
||||||
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
|
@image
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(path_or_image, retro: false, tileable: false)
|
||||||
|
@path = path_or_image if path_or_image.is_a?(String)
|
||||||
|
|
||||||
|
@image = Gosu::Image.new(path_or_image, retro: retro, tileable: tileable) if @path
|
||||||
|
@image = path_or_image unless @path
|
||||||
|
|
||||||
|
recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def path
|
||||||
@path
|
@path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Element
|
class Element
|
||||||
class Label < Element
|
class TextBlock < Element
|
||||||
def initialize(text, options = {}, block = nil)
|
def initialize(text, options = {}, block = nil)
|
||||||
super(options, block)
|
super(options, block)
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clicked_left_mouse_button(_sender, _x, _y)
|
def clicked_left_mouse_button(_sender, _x, _y)
|
||||||
@block&.call(self)
|
@block&.call(self) if @enabled
|
||||||
|
|
||||||
# return :handled
|
# return :handled
|
||||||
end
|
end
|
||||||
@@ -127,5 +127,30 @@ module CyberarmEngine
|
|||||||
publish(:changed, self.value)
|
publish(:changed, self.value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Banner < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Title < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Subtitle < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Tagline < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Caption < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Para < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Inscription < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: Remove in version 0.16.0+
|
||||||
|
class Label < TextBlock
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,13 +22,16 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def @menu.recalculate
|
def @menu.recalculate
|
||||||
super
|
super
|
||||||
|
|
||||||
recalculate_menu
|
recalculate_menu
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.choose = @choose
|
||||||
end
|
end
|
||||||
|
|
||||||
def choose=(item)
|
def choose=(item)
|
||||||
valid = @items.detect { |i| i == item }
|
valid = @items.detect { |i| i == item }
|
||||||
return unless valid # TODO: Throw an error?
|
raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid
|
||||||
|
|
||||||
@choose = item
|
@choose = item
|
||||||
|
|
||||||
@@ -45,13 +48,24 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def show_menu
|
def show_menu
|
||||||
@menu.clear
|
@menu.clear
|
||||||
@items.each do |item|
|
|
||||||
[@block]
|
|
||||||
block = proc { self.choose = item; @block.call(item) if @block }
|
|
||||||
b = Button.new(item,
|
|
||||||
{ parent: @menu, width: 1.0, theme: @options[:theme], margin: 0, border_color: 0x00ffffff }, block)
|
|
||||||
|
|
||||||
@menu.add(b)
|
@items.each do |item|
|
||||||
|
btn = Button.new(
|
||||||
|
item,
|
||||||
|
{
|
||||||
|
parent: @menu,
|
||||||
|
width: 1.0,
|
||||||
|
theme: @options[:theme],
|
||||||
|
margin: 0,
|
||||||
|
border_color: 0x00ffffff
|
||||||
|
},
|
||||||
|
proc do
|
||||||
|
self.choose = item
|
||||||
|
@block&.call(item)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
@menu.add(btn)
|
||||||
end
|
end
|
||||||
recalculate
|
recalculate
|
||||||
|
|
||||||
|
|||||||
156
lib/cyberarm_engine/ui/elements/text_block.rb
Normal file
156
lib/cyberarm_engine/ui/elements/text_block.rb
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class TextBlock < Element
|
||||||
|
def initialize(text, options = {}, block = nil)
|
||||||
|
super(options, block)
|
||||||
|
|
||||||
|
@text = Text.new(
|
||||||
|
text, font: @options[:font], z: @z, color: @options[:color],
|
||||||
|
size: @options[:text_size], shadow: @options[:text_shadow],
|
||||||
|
shadow_size: @options[:text_shadow_size],
|
||||||
|
shadow_color: @options[:text_shadow_color]
|
||||||
|
)
|
||||||
|
|
||||||
|
@raw_text = text
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@text.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
def clicked_left_mouse_button(_sender, _x, _y)
|
||||||
|
@block&.call(self) if @enabled
|
||||||
|
|
||||||
|
# return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
@width = 0
|
||||||
|
@height = 0
|
||||||
|
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height = dimensional_size(@style.height, :height)
|
||||||
|
|
||||||
|
handle_text_wrapping(_width)
|
||||||
|
|
||||||
|
@width = _width || @text.width.round
|
||||||
|
@height = _height || @text.height.round
|
||||||
|
|
||||||
|
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||||
|
@text.z = @z + 3
|
||||||
|
|
||||||
|
if (text_alignment = @options[:text_align])
|
||||||
|
case text_alignment
|
||||||
|
when :left
|
||||||
|
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||||
|
when :center
|
||||||
|
@text.x = if @text.width <= outer_width
|
||||||
|
@x + outer_width / 2 - @text.width / 2
|
||||||
|
else # Act as left aligned
|
||||||
|
@style.border_thickness_left + @style.padding_left + @x
|
||||||
|
end
|
||||||
|
when :right
|
||||||
|
@text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_text_wrapping(max_width)
|
||||||
|
max_width ||= @parent&.width
|
||||||
|
max_width ||= @x - (window.width + noncontent_width)
|
||||||
|
wrap_behavior = style.text_wrap
|
||||||
|
copy = @raw_text.to_s.dup
|
||||||
|
|
||||||
|
if line_width(copy[0]) <= max_width && line_width(copy) > max_width && wrap_behavior != :none
|
||||||
|
breaks = []
|
||||||
|
line_start = 0
|
||||||
|
line_end = copy.length
|
||||||
|
|
||||||
|
while line_start != copy.length
|
||||||
|
if line_width(copy[line_start...line_end]) > max_width
|
||||||
|
line_end = ((line_end - line_start) / 2.0)
|
||||||
|
line_end = 1.0 if line_end <= 1
|
||||||
|
elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
|
||||||
|
# To small, grow!
|
||||||
|
# TODO: find a more efficient way
|
||||||
|
line_end += 1
|
||||||
|
|
||||||
|
else # FOUND IT!
|
||||||
|
entering_line_end = line_end.floor
|
||||||
|
max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63
|
||||||
|
reach = 0
|
||||||
|
|
||||||
|
if wrap_behavior == :word_wrap
|
||||||
|
max_reach.times do |i|
|
||||||
|
reach = i
|
||||||
|
break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
|
||||||
|
end
|
||||||
|
|
||||||
|
# puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}"
|
||||||
|
line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation
|
||||||
|
end
|
||||||
|
|
||||||
|
breaks << line_end.floor
|
||||||
|
line_start = line_end.floor
|
||||||
|
line_end = copy.length
|
||||||
|
|
||||||
|
break if entering_line_end == copy.length || reach == max_reach
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
breaks.each_with_index do |pos, index|
|
||||||
|
copy.insert(pos + index, "\n") if pos + index >= 0 && pos + index < copy.length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@text.text = copy
|
||||||
|
end
|
||||||
|
|
||||||
|
def line_width(text)
|
||||||
|
(@text.textobject.markup_width(text) + noncontent_width)
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@raw_text
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(value)
|
||||||
|
@raw_text = value.to_s.chomp
|
||||||
|
|
||||||
|
old_width = width
|
||||||
|
old_height = height
|
||||||
|
recalculate
|
||||||
|
|
||||||
|
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||||
|
|
||||||
|
publish(:changed, self.value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Banner < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Title < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Subtitle < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Tagline < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Caption < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Para < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class Inscription < TextBlock
|
||||||
|
end
|
||||||
|
|
||||||
|
class ToolTip < TextBlock
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -26,7 +26,7 @@ module CyberarmEngine
|
|||||||
@dragging_element = nil
|
@dragging_element = nil
|
||||||
@pending_recalculate_request = false
|
@pending_recalculate_request = false
|
||||||
|
|
||||||
@tip = CyberarmEngine::Text.new("", size: 22, z: Float::INFINITY)
|
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY)
|
||||||
@menu = nil
|
@menu = nil
|
||||||
@min_drag_distance = 0
|
@min_drag_distance = 0
|
||||||
@mouse_pos = Vector.new
|
@mouse_pos = Vector.new
|
||||||
@@ -51,9 +51,8 @@ module CyberarmEngine
|
|||||||
@menu.draw
|
@menu.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
if @tip.text.length.positive?
|
if @tip.value.length.positive?
|
||||||
Gosu.flush
|
Gosu.flush
|
||||||
Gosu.draw_rect(@tip.x - 2, @tip.y - 2, @tip.width + 4, @tip.height + 4, 0xff020202, Float::INFINITY)
|
|
||||||
@tip.draw
|
@tip.draw
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -67,6 +66,13 @@ module CyberarmEngine
|
|||||||
@pending_recalculate_request = false
|
@pending_recalculate_request = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @pending_focus_request
|
||||||
|
@pending_focus_request = false
|
||||||
|
|
||||||
|
self.focus = @pending_focus_element
|
||||||
|
@pending_focus_element.publish(:focus)
|
||||||
|
end
|
||||||
|
|
||||||
@menu&.update
|
@menu&.update
|
||||||
super
|
super
|
||||||
|
|
||||||
@@ -87,11 +93,17 @@ module CyberarmEngine
|
|||||||
|
|
||||||
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
||||||
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
||||||
@tip.text = @mouse_over.tip if @mouse_over
|
@tip.value = @mouse_over.tip if @mouse_over
|
||||||
@tip.x = window.mouse_x - @tip.width / 2
|
@tip.x = window.mouse_x - @tip.width / 2
|
||||||
@tip.y = window.mouse_y - @tip.height - 4
|
@tip.x = 0 if @tip.x < 0
|
||||||
|
@tip.x = window.width - @tip.width if @tip.x + @tip.width > window.width
|
||||||
|
@tip.y = window.mouse_y - @tip.height
|
||||||
|
@tip.y = 0 if @tip.y < 0
|
||||||
|
@tip.y = window.height - @tip.height if @tip.y + @tip.height > window.height
|
||||||
|
@tip.update
|
||||||
|
@tip.recalculate
|
||||||
else
|
else
|
||||||
@tip.text = ""
|
@tip.value = ""
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@mouse_moved_at = Gosu.milliseconds
|
@mouse_moved_at = Gosu.milliseconds
|
||||||
@@ -100,14 +112,17 @@ module CyberarmEngine
|
|||||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||||
@mouse_pos = @last_mouse_pos.clone
|
@mouse_pos = @last_mouse_pos.clone
|
||||||
|
|
||||||
request_recalculate if @active_width != window.width || @active_height != window.height
|
if @active_width != window.width || @active_height != window.height
|
||||||
|
request_recalculate
|
||||||
|
@root_container.publish(:window_size_changed)
|
||||||
|
end
|
||||||
|
|
||||||
@active_width = window.width
|
@active_width = window.width
|
||||||
@active_height = window.height
|
@active_height = window.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def tool_tip_delay
|
def tool_tip_delay
|
||||||
500 # ms
|
250 # ms
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
@@ -207,6 +222,11 @@ module CyberarmEngine
|
|||||||
@pending_recalculate_request = true
|
@pending_recalculate_request = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def request_focus(element)
|
||||||
|
@pending_focus_request = true
|
||||||
|
@pending_focus_element = element
|
||||||
|
end
|
||||||
|
|
||||||
def show_menu(list_box)
|
def show_menu(list_box)
|
||||||
@menu = list_box
|
@menu = list_box
|
||||||
end
|
end
|
||||||
@@ -214,5 +234,14 @@ module CyberarmEngine
|
|||||||
def hide_menu
|
def hide_menu
|
||||||
@menu = nil
|
@menu = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
# "#{self.class} children=#{@children.map { |c| c.to_s }}"
|
||||||
|
@root_container.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def inspect
|
||||||
|
to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -82,6 +82,11 @@ module CyberarmEngine
|
|||||||
active: {
|
active: {
|
||||||
color: Gosu::Color::BLACK,
|
color: Gosu::Color::BLACK,
|
||||||
background: ["ffB23E41".to_i(16)]
|
background: ["ffB23E41".to_i(16)]
|
||||||
|
},
|
||||||
|
|
||||||
|
disabled: {
|
||||||
|
color: Gosu::Color::GRAY,
|
||||||
|
background: 0xff303030
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -98,12 +103,13 @@ module CyberarmEngine
|
|||||||
|
|
||||||
Image: { # < Element
|
Image: { # < Element
|
||||||
color: Gosu::Color::WHITE,
|
color: Gosu::Color::WHITE,
|
||||||
|
tileable: false,
|
||||||
retro: false
|
retro: false
|
||||||
},
|
},
|
||||||
|
|
||||||
Label: { # < Element
|
TextBlock: { # < Element
|
||||||
text_size: 28,
|
text_size: 28,
|
||||||
text_wrap: :none, # :word_wrap, :break_word, :none
|
text_wrap: :word_wrap, # :word_wrap, :break_word, :none
|
||||||
text_shadow: false,
|
text_shadow: false,
|
||||||
text_align: :left,
|
text_align: :left,
|
||||||
font: "Arial",
|
font: "Arial",
|
||||||
@@ -111,6 +117,45 @@ module CyberarmEngine
|
|||||||
padding: 2
|
padding: 2
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Banner: { # < TextBlock
|
||||||
|
text_size: 48
|
||||||
|
},
|
||||||
|
|
||||||
|
Title: { # < TextBlock
|
||||||
|
text_size: 34
|
||||||
|
},
|
||||||
|
|
||||||
|
Subtitle: { # < TextBlock
|
||||||
|
text_size: 26
|
||||||
|
},
|
||||||
|
|
||||||
|
Tagline: { # < TextBlock
|
||||||
|
text_size: 24
|
||||||
|
},
|
||||||
|
|
||||||
|
Caption: { # < TextBlock
|
||||||
|
text_size: 22
|
||||||
|
},
|
||||||
|
|
||||||
|
Para: { # < TextBlock
|
||||||
|
text_size: 18
|
||||||
|
},
|
||||||
|
|
||||||
|
Inscription: { # < TextBlock
|
||||||
|
text_size: 16
|
||||||
|
},
|
||||||
|
|
||||||
|
ToolTip: { # < TextBlock
|
||||||
|
color: Gosu::Color::WHITE,
|
||||||
|
padding_top: 4,
|
||||||
|
padding_bottom: 4,
|
||||||
|
padding_left: 8,
|
||||||
|
padding_right: 8,
|
||||||
|
border_thickness: 1,
|
||||||
|
border_color: 0xffaaaaaa,
|
||||||
|
background: 0xff404040
|
||||||
|
},
|
||||||
|
|
||||||
ToggleButton: { # < Button
|
ToggleButton: { # < Button
|
||||||
checkmark: "√"
|
checkmark: "√"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev".freeze
|
NAME = "InDev".freeze
|
||||||
VERSION = "0.15.0".freeze
|
VERSION = "0.17.1".freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Window < Gosu::Window
|
class Window < Gosu::Window
|
||||||
|
include Common
|
||||||
|
|
||||||
IMAGES = {}
|
IMAGES = {}
|
||||||
SAMPLES = {}
|
SAMPLES = {}
|
||||||
SONGS = {}
|
SONGS = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user