Compare commits

29 Commits

Author SHA1 Message Date
55382a7c14 Bump version 2022-10-23 18:40:46 -05:00
883de3db9f Added support for TextBlock's to have text_v_align to compliment text_h_align, EditLine and EditBox will now preserve their focus appear, fixed crash in ToggleButton due to using :Label instead of :TextBlock, misc. tweaks. 2022-10-23 18:38:51 -05:00
41c0b27937 Fixed layout bug for Flow when the element x + width is greater than the parent's width it would not correctly wrap 2022-10-20 09:36:20 -05:00
2fd5d398cf Fix crash when EditLine receives a paste from clipboard 2022-10-07 20:32:18 -05:00
2e66509f87 Removed use of the clipboard gem since Gosu now supports this natively! Added :static option for Text to render text using Gosu::Image.from_text/markup which results in nicer looking text, improvements to EditLine to display the caret in the correct place (doesn't position properly if :text_static is true) 2022-10-04 10:16:32 -05:00
521b3937dd Fixed Slider element's Handle not styling or positioning properly 2022-07-26 12:20:13 -05:00
ab9f9e8e7a Refactored dimentional_size to split out space_available_width/height, initial implementation of verticial and horizontal container alignment 2022-06-12 16:03:23 -05:00
705138f7ad Fixed elements in a Flow container not positioned correctly after the first wrap 2022-06-12 11:36:22 -05:00
94a65f447c Fixed long standing issue with ListBox menu not taking it's parents width only the parent has a specified width and fixed clicking on the menu's host element to hide the menu causes it to hide and reappear instantly. 2022-06-12 11:25:29 -05:00
97296b080c Bump version 2022-06-05 09:20:17 -05:00
ca73a2d8e8 Added all the callbacks to Window and GameState, removed all but one usage of global variables (). 2022-06-05 09:19:30 -05:00
0a62e5180a Add down arrow for ListBox 2022-05-03 20:01:34 -05:00
31e909eb30 Probably fix layout issues caused by floating point errors where an element is wrapped when it has space (e.g. 2 elements with a width 0.5 not fitting properly) 2022-05-02 19:08:47 -05:00
300e7c4e59 Implemented tiled mode for BackgroundImage and other improvements 2022-05-02 19:06:40 -05:00
be98fe47ad Fixed ListBox element menu getting cut off if it's near the bottom of the screen by stacking it ontop of the host element 2022-05-02 19:05:57 -05:00
a75afaf47a Added support for UI to have background_images, fixed TextBlock text overdrawing 2022-04-25 20:12:10 -05:00
d81df5f4e2 Moved min_width/height, max_width/height and fill control from Container into Element#dimensional_size so any element can use them 2022-04-25 16:48:17 -05:00
f2ea0d9942 Maybe final container fill mode fix? 2022-04-04 13:26:40 -05:00
08d068f156 Fixed weirdness for Container dynamic fill mode 2022-04-04 10:08:39 -05:00
a4d02038c3 Fixes for Container's calculating fill size and for Flow's max_width 2022-04-03 13:06:10 -05:00
37bdd6ef23 Added support for min/max width/height and for fill which dynamically sets elements width/height to the max available width for Flow parents and height for Stack parents 2022-04-03 11:07:31 -05:00
24bd769a32 Bump version 2022-03-05 14:43:50 -06:00
2be5733bc1 Fixed Container not considering padding when clipping render area 2022-01-29 10:08:33 -06:00
c8734ae98b Container child elements that are not visible are no longer drawn 2021-12-24 17:00:02 -06:00
c35d587419 Fixed passing nil for :enabled state as boolean causing it to be set as true instead of false 2021-12-22 14:58:17 -06:00
153871e7f3 Improved performance a touch: fixed never clearing list of elements to recalculate unless a full gui recalcuate occurred, don't recalculate button/text if a full gui recalculate is requested, fixed typo: apend -> append 2021-12-16 22:09:56 -06:00
cf91d4a083 Merge branch 'master' of https://github.com/cyberarm/cyberarm_engine 2021-12-03 15:33:38 -06:00
35ad687d4c Reducce cpu usage by not using clip_to for each and every element 2021-12-03 15:33:32 -06:00
54802e1254 Added support 'marquee' style progress bars i.e. non-linear progress bar with scrolling bar 2021-12-02 08:15:39 -06:00
24 changed files with 670 additions and 226 deletions

View File

@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = %w[lib assets] spec.require_paths = %w[lib assets]
spec.add_dependency "clipboard", "~> 1.3"
spec.add_dependency "excon", "~> 0.88" spec.add_dependency "excon", "~> 0.88"
spec.add_dependency "gosu", "~> 1.1" spec.add_dependency "gosu", "~> 1.1"
spec.add_dependency "gosu_more_drawables", "~> 0.3" spec.add_dependency "gosu_more_drawables", "~> 0.3"

View File

@@ -8,7 +8,6 @@ end
require "json" require "json"
require "excon" require "excon"
require "gosu_more_drawables" require "gosu_more_drawables"
require "clipboard"
require_relative "cyberarm_engine/version" require_relative "cyberarm_engine/version"
require_relative "cyberarm_engine/stats" require_relative "cyberarm_engine/stats"
@@ -24,6 +23,7 @@ require_relative "cyberarm_engine/transform"
require_relative "cyberarm_engine/ray" require_relative "cyberarm_engine/ray"
require_relative "cyberarm_engine/background" require_relative "cyberarm_engine/background"
require_relative "cyberarm_engine/background_nine_slice" require_relative "cyberarm_engine/background_nine_slice"
require_relative "cyberarm_engine/background_image"
require_relative "cyberarm_engine/animator" require_relative "cyberarm_engine/animator"
require_relative "cyberarm_engine/text" require_relative "cyberarm_engine/text"

View File

@@ -0,0 +1,93 @@
module CyberarmEngine
class BackgroundImage
include CyberarmEngine::Common
attr_accessor :x, :y, :z, :mode
attr_reader :image, :width, :height, :color
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :fill, color: Gosu::Color::WHITE)
@image_path = image_path
@image = get_image(image_path) if image_path
@x = x
@y = y
@z = z
@width = width
@height = height
@mode = mode
@color = color
@cached_tiling = nil
end
def image=(image_path)
@cached_tiling = nil if image_path != @image_path
@image_path = image_path
@image = image_path ? get_image(image_path) : image_path
end
def width=(n)
@cached_tiling = nil if @width != n
@width = n
end
def height=(n)
@cached_tiling = nil if @height != n
@height = n
end
def color=(c)
@cached_tiling = nil if @color != c
@color = c
end
def width_scale
(@width.to_f / @image.width).abs
end
def height_scale
(@height.to_f / @image.height).abs
end
def draw
return unless @image
Gosu.clip_to(@x, @y, @width, @height) do
send(:"draw_#{mode}")
end
end
def draw_stretch
@image.draw(@x, @y, @z, width_scale, height_scale, @color)
end
def draw_tiled
@cached_tiling ||= Gosu.record(@width, @height) do
height_scale.ceil.times do |y|
width_scale.ceil.times do |x|
@image.draw(x * @image.width, y * @image.height, @z, 1, 1, @color)
end
end
end
@cached_tiling.draw(@x, @y, @z)
end
def draw_fill
if @width * width_scale > height * height_scale
draw_fill_width
else
draw_fill_height
end
end
def draw_fill_width
@image.draw(@x, @y, @z, width_scale, width_scale, @color)
end
def draw_fill_height
@image.draw(@x, @y, @z, height_scale, height_scale, @color)
end
end
end

View File

@@ -8,8 +8,11 @@ module CyberarmEngine
window.current_state window.current_state
end end
def previous_state def previous_state(state = nil)
window.previous_state raise "Only available for CyberarmEngine::GameState and subclasses" unless is_a?(CyberarmEngine::GameState) || state.is_a?(CyberarmEngine::GameState)
i = window.states.index(state || self)
window.states[i - 1] unless (i - 1).negative?
end end
def pop_state def pop_state
@@ -95,7 +98,7 @@ module CyberarmEngine
end end
def window def window
$window CyberarmEngine::Window.instance
end end
def control_down? def control_down?

View File

@@ -7,7 +7,7 @@ module CyberarmEngine
attr_reader :alpha attr_reader :alpha
def initialize(options = {}) def initialize(options = {})
$window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil? window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil?
@options = options @options = options
@image = options[:image] ? image(options[:image]) : nil @image = options[:image] ? image(options[:image]) : nil
@@ -55,9 +55,9 @@ module CyberarmEngine
if $debug if $debug
show_debug_heading show_debug_heading
$window.draw_circle(@position.x, @position.y, radius, 9999, @debug_color) Gosu.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
if @debug_text.text != "" if @debug_text.text != ""
$window.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20, Gosu.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20,
Gosu::Color.rgba(0, 0, 0, 200), 9999) Gosu::Color.rgba(0, 0, 0, 200), 9999)
@debug_text.draw @debug_text.draw
end end
@@ -102,13 +102,13 @@ module CyberarmEngine
end end
def _x_visible def _x_visible
x.between?(($window.width / 2) - @world_center_point.x, ($window.width / 2) + @world_center_point.x) || x.between?((window.width / 2) - @world_center_point.x, (window.width / 2) + @world_center_point.x) ||
x.between?((@world_center_point.x - $window.width / 2), ($window.width / 2) + @world_center_point.x) x.between?((@world_center_point.x - window.width / 2), (window.width / 2) + @world_center_point.x)
end end
def _y_visible def _y_visible
y.between?(($window.height / 2) - @world_center_point.y, ($window.height / 2) + @world_center_point.y) || y.between?((window.height / 2) - @world_center_point.y, (window.height / 2) + @world_center_point.y) ||
y.between?(@world_center_point.y - ($window.height / 2), ($window.height / 2) + @world_center_point.y) y.between?(@world_center_point.y - (window.height / 2), (window.height / 2) + @world_center_point.y)
end end
def heading(ahead_by = 100, _object = nil, angle_only = false) def heading(ahead_by = 100, _object = nil, angle_only = false)
@@ -153,10 +153,6 @@ module CyberarmEngine
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int) @color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
end end
def draw_rect(x, y, width, height, color, z = 0)
$window.draw_rect(x, y, width, height, color, z)
end
def button_up(id) def button_up(id)
end end
@@ -190,12 +186,12 @@ module CyberarmEngine
# Duplication... so DRY. # Duplication... so DRY.
def each_circle_collision(object, _resolve_with = :width, &block) def each_circle_collision(object, _resolve_with = :width, &block)
if object.class != Class && object.instance_of?(object.class) if object.class != Class && object.instance_of?(object.class)
$window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o| window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o|
distance = Gosu.distance(x, y, object.x, object.y) distance = Gosu.distance(x, y, object.x, object.y)
block.call(o, object) if distance <= radius + object.radius && block block.call(o, object) if distance <= radius + object.radius && block
end end
else else
list = $window.current_state.game_objects.select { |i| i.instance_of?(object) } list = window.current_state.game_objects.select { |i| i.instance_of?(object) }
list.each do |o| list.each do |o|
next if self == o next if self == o
@@ -206,9 +202,9 @@ module CyberarmEngine
end end
def destroy def destroy
if $window.current_state if window.current_state
$window.current_state.game_objects.each do |o| window.current_state.game_objects.each do |o|
$window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self
end end
end end
end end
@@ -220,13 +216,13 @@ module CyberarmEngine
def self.each_circle_collision(object, _resolve_with = :width, &block) def self.each_circle_collision(object, _resolve_with = :width, &block)
if object.class != Class && object.instance_of?(object.class) if object.class != Class && object.instance_of?(object.class)
$window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o| window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o|
distance = Gosu.distance(o.x, o.y, object.x, object.y) distance = Gosu.distance(o.x, o.y, object.x, object.y)
block.call(o, object) if distance <= o.radius + object.radius && block block.call(o, object) if distance <= o.radius + object.radius && block
end end
else else
lista = $window.current_state.game_objects.select { |i| i.instance_of?(self) } lista = window.current_state.game_objects.select { |i| i.instance_of?(self) }
listb = $window.current_state.game_objects.select { |i| i.instance_of?(object) } listb = window.current_state.game_objects.select { |i| i.instance_of?(object) }
lista.product(listb).each do |o, o2| lista.product(listb).each do |o, o2|
next if o == o2 next if o == o2
@@ -238,9 +234,9 @@ module CyberarmEngine
def self.destroy_all def self.destroy_all
INSTANCES.clear INSTANCES.clear
if $window.current_state if window.current_state
$window.current_state.game_objects.each do |o| window.current_state.game_objects.each do |o|
$window.current_state.game_objects.delete(o) if o.is_a?(self.class) window.current_state.game_objects.delete(o) if o.is_a?(self.class)
end end
end end
end end

View File

@@ -9,7 +9,7 @@ module CyberarmEngine
@options = options @options = options
@game_objects = [] @game_objects = []
@global_pause = false @global_pause = false
$window.text_input = nil unless options[:preserve_text_input] window.text_input = nil unless options[:preserve_text_input]
@down_keys = {} @down_keys = {}
end end
@@ -30,48 +30,23 @@ module CyberarmEngine
@game_objects.each(&:update) @game_objects.each(&:update)
end end
def draw_bounding_box(box) def needs_redraw?
x = box.x true
y = box.y
max_x = box.max_x
max_y = box.max_y
color = Gosu::Color.rgba(255, 127, 64, 240)
# pipe = 4
# Gosu.draw_rect(x-width, y-height, x+(width*2), y+(height*2), color, Float::INFINITY)
# puts "BB render: #{x}:#{y} w:#{x.abs+width} h:#{y.abs+height}"
# Gosu.draw_rect(x, y, x.abs+width, y.abs+height, color, Float::INFINITY)
# TOP LEFT to BOTTOM LEFT
$window.draw_line(
x, y, color,
x, max_y, color,
Float::INFINITY
)
# BOTTOM LEFT to BOTTOM RIGHT
$window.draw_line(
x, max_y, color,
max_x, max_y, color,
Float::INFINITY
)
# BOTTOM RIGHT to TOP RIGHT
$window.draw_line(
max_x, max_y, color,
max_x, y, color,
Float::INFINITY
)
# TOP RIGHT to TOP LEFT
$window.draw_line(
max_x, y, color,
x, y, color,
Float::INFINITY
)
end end
def destroy def drop(filename)
@options.clear end
@game_objects.clear
def gamepad_connected(index)
end
def gamepad_disconnected(index)
end
def gain_focus
end
def lose_focus
end end
def button_down(id) def button_down(id)
@@ -90,6 +65,54 @@ module CyberarmEngine
end end
end end
def close
window.close!
end
def draw_bounding_box(box)
x = box.x
y = box.y
max_x = box.max_x
max_y = box.max_y
color = Gosu::Color.rgba(255, 127, 64, 240)
# pipe = 4
# Gosu.draw_rect(x-width, y-height, x+(width*2), y+(height*2), color, Float::INFINITY)
# puts "BB render: #{x}:#{y} w:#{x.abs+width} h:#{y.abs+height}"
# Gosu.draw_rect(x, y, x.abs+width, y.abs+height, color, Float::INFINITY)
# TOP LEFT to BOTTOM LEFT
Gosu.draw_line(
x, y, color,
x, max_y, color,
Float::INFINITY
)
# BOTTOM LEFT to BOTTOM RIGHT
Gosu.draw_line(
x, max_y, color,
max_x, max_y, color,
Float::INFINITY
)
# BOTTOM RIGHT to TOP RIGHT
Gosu.draw_line(
max_x, max_y, color,
max_x, y, color,
Float::INFINITY
)
# TOP RIGHT to TOP LEFT
Gosu.draw_line(
max_x, y, color,
x, y, color,
Float::INFINITY
)
end
def destroy
@options.clear
@game_objects.clear
end
def add_game_object(object) def add_game_object(object)
@game_objects << object @game_objects << object
end end

View File

@@ -4,7 +4,7 @@ module CyberarmEngine
def initialize def initialize
@bounding_box_renderer = BoundingBoxRenderer.new @bounding_box_renderer = BoundingBoxRenderer.new
@opengl_renderer = OpenGLRenderer.new(width: $window.width, height: $window.height) @opengl_renderer = OpenGLRenderer.new(width: CyberarmEngine::Window.instance.width, height: CyberarmEngine::Window.instance.height)
end end
def draw(camera, lights, entities) def draw(camera, lights, entities)

View File

@@ -29,12 +29,14 @@ module CyberarmEngine
@border = true if options[:border].nil? @border = true if options[:border].nil?
@border_size = options[:border_size] || 1 @border_size = options[:border_size] || 1
@border_alpha = options[:border_alpha] || 30 @border_alpha = options[:border_alpha] || 30
@border_color = options[:border_color] @border_color = options[:border_color] || Gosu::Color::BLACK
@shadow = options[:shadow] @shadow = options[:shadow]
@shadow_size = options[:shadow_size] || 2 @shadow_size = options[:shadow_size] || 2
@shadow_alpha = options[:shadow_alpha] || 30 @shadow_alpha = options[:shadow_alpha] || 30
@shadow_color = options[:shadow_color] @shadow_color = options[:shadow_color] || Gosu::Color::BLACK
@static = options[:static] || (options[:static].nil? || options[:static] == false ? false : true)
@textobject = check_cache(@size, @font) @textobject = check_cache(@size, @font)
@@ -43,9 +45,9 @@ module CyberarmEngine
when :left when :left
@x = 0 + BUTTON_PADDING @x = 0 + BUTTON_PADDING
when :center when :center
@x = ($window.width / 2) - (@textobject.text_width(@text) / 2) @x = (CyberarmEngine::Window.instance.width / 2) - (@textobject.text_width(@text) / 2)
when :right when :right
@x = $window.width - BUTTON_PADDING - @textobject.text_width(@text) @x = CyberarmEngine::Window.instance.width - BUTTON_PADDING - @textobject.text_width(@text)
end end
end end
end end
@@ -84,46 +86,49 @@ module CyberarmEngine
end end
def text=(string) def text=(string)
@rendered_border = nil invalidate_cache! if @text != string
@text = string @text = string
end end
def factor_x=(n) def factor_x=(n)
@rendered_border = nil invalidate_cache! if @factor_x != n
@factor_x = n @factor_x = n
end end
def factor_y=(n) def factor_y=(n)
@rendered_border = nil invalidate_cache! if @factor_y != n
@factor_y = n @factor_y = n
end end
def color=(color) def color=(color)
@rendered_border = nil old_color = @color
if color if color
@color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color) @color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color)
else else
raise "color cannot be nil" raise "color cannot be nil"
end end
invalidate_cache! if old_color != color
end end
def border=(boolean) def border=(boolean)
@rendered_border = nil invalidate_cache! if @border != boolean
@border = boolean @border = boolean
end end
def border_size=(n) def border_size=(n)
@rendered_border = nil invalidate_cache! if @border_size != n
@border_size = n @border_size = n
end end
def border_alpha=(n) def border_alpha=(n)
@rendered_border = nil invalidate_cache! if @border_alpha != n
@border_alpha = n @border_alpha = n
end end
def border_color=(n) def border_color=(n)
@rendered_border = nil invalidate_cache! if @border_color != n
@border_color = n @border_color = n
end end
@@ -132,11 +137,27 @@ module CyberarmEngine
end end
def text_width(text = @text) def text_width(text = @text)
textobject.text_width(text) + @border_size + @shadow_size spacing = 0
spacing += @border_size if @border
spacing += @shadow_size if @shadow
if text == @text && @static && @gosu_cached_text_image
@gosu_cached_text_image&.width + spacing
else
textobject.text_width(text) + spacing
end
end end
def markup_width(text = @text) def markup_width(text = @text)
textobject.markup_width(text) + @border_size + @shadow_size spacing = 0
spacing += @border_size if @border
spacing += @shadow_size if @shadow
if text == @text && @static && @gosu_cached_text_image
@gosu_cached_text_image&.width + spacing
else
textobject.markup_width(text) + spacing
end
end end
def height(text = @text) def height(text = @text)
@@ -148,6 +169,38 @@ module CyberarmEngine
end end
def draw(method = :draw_markup) def draw(method = :draw_markup)
if @static
if @border && !@cached_text_border_image
_x = @border_size
_y = @border_size
_width = method == :draw_markup ? text_width : markup_width
img = Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
@cached_text_border_image = Gosu.render((_width + (@border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
img.draw(-_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(-_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(0, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(0, _y, @z, @factor_x, @factor_y, @border_color, @mode)
img.draw(-_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
end
end
@cached_text_shadow_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) if @shadow
@gosu_cached_text_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
@cached_text_border_image.draw(@x, @y, @z, @factor_x, @factor_y, @border_color, @mode) if @border
@cached_text_shadow_image.draw(@x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, @shadow_color, @mode) if @shadow
@gosu_cached_text_image.draw(@x, @y, @z, @factor_x, @factor_y, @color, @mode)
else
if @border && !ARGV.join.include?("--no-border") if @border && !ARGV.join.include?("--no-border")
border_alpha = @color.alpha <= 30 ? @color.alpha : @border_alpha border_alpha = @color.alpha <= 30 ? @color.alpha : @border_alpha
border_color = @border_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, border_color = @border_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
@@ -158,7 +211,7 @@ module CyberarmEngine
_y = @border_size _y = @border_size
_width = method == :draw_markup ? text_width : markup_width _width = method == :draw_markup ? text_width : markup_width
@rendered_border ||= Gosu.render((_width + (border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do @cached_text_border_image ||= Gosu.render((_width + (border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
@textobject.send(method, @text, _x - @border_size, _y, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x - @border_size, _y, @z, @factor_x, @factor_y, white, @mode)
@textobject.send(method, @text, _x - @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x - @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
@@ -172,7 +225,7 @@ module CyberarmEngine
@textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
end end
@rendered_border.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color) @cached_text_border_image.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color)
end end
if @shadow if @shadow
@@ -182,6 +235,7 @@ module CyberarmEngine
@textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode) @textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode)
end end
end
def alpha=(n) def alpha=(n)
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n) @color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n)
@@ -193,5 +247,11 @@ module CyberarmEngine
def update def update
end end
def invalidate_cache!
@cached_text_border_image = nil
@cached_text_shadow_image = nil
@gosu_cached_text_image = nil
end
end end
end end

View File

@@ -116,7 +116,7 @@ module CyberarmEngine
end end
private def element_parent private def element_parent
$__current_container__ CyberarmEngine::Element::Container.current_container
end end
private def container(klass, options = {}, &block) private def container(klass, options = {}, &block)
@@ -126,12 +126,12 @@ module CyberarmEngine
_container = klass.new(options, block) _container = klass.new(options, block)
old_parent = element_parent old_parent = element_parent
$__current_container__ = _container CyberarmEngine::Element::Container.current_container = _container
_container.build _container.build
_container.parent.add(_container) _container.parent.add(_container)
$__current_container__ = old_parent CyberarmEngine::Element::Container.current_container = old_parent
_container _container
end end

View File

@@ -4,7 +4,7 @@ module CyberarmEngine
include Event include Event
include Common 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 attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@@ -13,9 +13,9 @@ module CyberarmEngine
@options = options @options = options
@block = block @block = block
@focus = @options[:focus].nil? ? false : @options[:focus] @focus = !@options.key?(:focus) ? false : @options[:focus]
@enabled = @options[:enabled].nil? ? true : @options[:enabled] @enabled = !@options.key?(:enabled) ? true : @options[:enabled]
@visible = @options[:visible].nil? ? true : @options[:visible] @visible = !@options.key?(:visible) ? true : @options[:visible]
@tip = @options[:tip] || "" @tip = @options[:tip] || ""
@debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color] @debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
@@ -24,6 +24,7 @@ module CyberarmEngine
@root ||= nil @root ||= nil
@gui_state ||= nil @gui_state ||= nil
@element_visible = true
@x = @style.x @x = @style.x
@y = @style.y @y = @style.y
@@ -37,6 +38,7 @@ module CyberarmEngine
@style.background_canvas = Background.new @style.background_canvas = Background.new
@style.background_nine_slice_canvas = BackgroundNineSlice.new @style.background_nine_slice_canvas = BackgroundNineSlice.new
@style.background_image_canvas = BackgroundImage.new
@style.border_canvas = BorderCanvas.new(element: self) @style.border_canvas = BorderCanvas.new(element: self)
@style_event = :default @style_event = :default
@@ -59,6 +61,7 @@ module CyberarmEngine
set_background set_background
set_background_nine_slice set_background_nine_slice
set_background_image
set_border_thickness set_border_thickness
set_border_color set_border_color
@@ -103,6 +106,14 @@ module CyberarmEngine
@style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge @style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
end end
def set_background_image
@style.background_image = safe_style_fetch(:background_image)
@style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch
@style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE
@style.background_image_canvas.mode = @style.background_image_mode
@style.background_image_canvas.color = @style.background_image_color
end
def set_border_thickness def set_border_thickness
@style.border_thickness = safe_style_fetch(:border_thickness) @style.border_thickness = safe_style_fetch(:border_thickness)
@@ -260,6 +271,10 @@ module CyberarmEngine
@visible @visible
end end
def element_visible?
@element_visible
end
def toggle def toggle
@visible = !@visible @visible = !@visible
root.gui_state.request_recalculate root.gui_state.request_recalculate
@@ -279,15 +294,15 @@ module CyberarmEngine
def draw def draw
return unless visible? return unless visible?
return unless element_visible?
@style.background_canvas.draw @style.background_canvas.draw
@style.background_nine_slice_canvas.draw @style.background_nine_slice_canvas.draw
@style.background_image_canvas.draw
@style.border_canvas.draw @style.border_canvas.draw
Gosu.clip_to(@x, @y, width, height) do
render render
end end
end
def debug_draw def debug_draw
return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
@@ -425,11 +440,42 @@ module CyberarmEngine
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)
if size.is_a?(Numeric) && size.between?(0.0, 1.0) new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0)
(@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round (@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
else else
size size
end end
if @parent && @style.fill # Handle fill behavior
if dimension == :width && @parent.is_a?(Flow)
return space_available_width - noncontent_width
elsif dimension == :height && @parent.is_a?(Stack)
return space_available_height - noncontent_height
end
else # Handle min_width/height and max_width/height
return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}")
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}")
end
new_size
end
def space_available_width
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
available_space = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings)
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end
def space_available_height
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
available_space = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings)
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end end
def background=(_background) def background=(_background)
@@ -446,6 +492,7 @@ module CyberarmEngine
@style.background_canvas.update @style.background_canvas.update
update_background_nine_slice update_background_nine_slice
update_background_image
@style.border_canvas.update @style.border_canvas.update
end end
@@ -473,6 +520,24 @@ module CyberarmEngine
@style.background_nine_slice_canvas.image = @style.background_nine_slice @style.background_nine_slice_canvas.image = @style.background_nine_slice
end end
def background_image=(image_path)
@style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
update_background_image
end
def update_background_image
@style.background_image_canvas.x = @x
@style.background_image_canvas.y = @y
@style.background_image_canvas.z = @z
@style.background_image_canvas.width = width
@style.background_image_canvas.height = height
@style.background_image_canvas.mode = @style.background_image_mode
@style.background_image_canvas.color = @style.background_image_color
@style.background_image_canvas.image = @style.background_image
end
def root def root
return self if is_root? return self if is_root?

View File

@@ -64,8 +64,8 @@ module CyberarmEngine
@scale_y = 1 @scale_y = 1
end end
@width = _width || @image.width.round * @scale_x @width = _width || @image.width.floor * @scale_x
@height = _height || @image.height.round * @scale_y @height = _height || @image.height.floor * @scale_y
update_background update_background
else else
@@ -86,9 +86,12 @@ module CyberarmEngine
old_width = width old_width = width
old_height = height 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) publish(:changed, self.value)
end end

View File

@@ -6,6 +6,16 @@ module CyberarmEngine
attr_accessor :stroke_color, :fill_color attr_accessor :stroke_color, :fill_color
attr_reader :children, :gui_state, :scroll_position attr_reader :children, :gui_state, :scroll_position
def self.current_container
@@current_container
end
def self.current_container=(container)
raise ArgumentError, "Expected container to an an instance of CyberarmEngine::Element::Container, got #{container.class}" unless container.is_a?(CyberarmEngine::Element::Container)
@@current_container = container
end
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@gui_state = options.delete(:gui_state) @gui_state = options.delete(:gui_state)
super super
@@ -35,29 +45,34 @@ module CyberarmEngine
def clear(&block) def clear(&block)
@children.clear @children.clear
old_container = $__current_container__ old_container = CyberarmEngine::Element::Container.current_container
$__current_container__ = self CyberarmEngine::Element::Container.current_container = self
block.call(self) if block block.call(self) if block
$__current_container__ = old_container CyberarmEngine::Element::Container.current_container = old_container
root.gui_state.request_recalculate root.gui_state.request_recalculate
end end
def apend(&block) def append(&block)
old_container = $__current_container__ old_container = CyberarmEngine::Element::Container.current_container
$__current_container__ = self CyberarmEngine::Element::Container.current_container = self
block.call(self) if block block.call(self) if block
$__current_container__ = old_container CyberarmEngine::Element::Container.current_container = old_container
root.gui_state.request_recalculate root.gui_state.request_recalculate
end end
def render 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) @children.each(&:draw)
end end
end end
@@ -103,6 +118,8 @@ module CyberarmEngine
stylize stylize
# s = Gosu.milliseconds
layout layout
if is_root? if is_root?
@@ -115,11 +132,35 @@ module CyberarmEngine
_width = dimensional_size(@style.width, :width) _width = dimensional_size(@style.width, :width)
_height = dimensional_size(@style.height, :height) _height = dimensional_size(@style.height, :height)
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).round @width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).round @height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
end end
# Move child to parent after positioning # FIXME: Correctly handle alignment when element has siblings
# FIXME: Enable alignment for any element, not just containers
if @style.v_align
space = space_available_height
case @style.v_align
when :center
@y = parent.height / 2 - height / 2
when :bottom
@y = parent.height - height
end
end
if @style.h_align
space = space_available_width
case @style.h_align
when :center
@x = parent.width / 2 - width / 2
when :right
@x = parent.width - width
end
end
# Move children to parent after positioning
@children.each do |child| @children.each do |child|
child.x += (@x + @style.border_thickness_left) - style.margin_left child.x += (@x + @style.border_thickness_left) - style.margin_left
child.y += (@y + @style.border_thickness_top) - style.margin_top child.y += (@y + @style.border_thickness_top) - style.margin_top
@@ -129,8 +170,13 @@ module CyberarmEngine
child.reposition # TODO: Implement top,bottom,left,center, and right positioning child.reposition # TODO: Implement top,bottom,left,center, and right positioning
Stats.increment(:gui_recalculations_last_frame, 1) 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 end
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
update_background update_background
end end
@@ -139,16 +185,17 @@ module CyberarmEngine
end end
def max_width def max_width
_width = dimensional_size(@style.width, :width) # _width = dimensional_size(@style.width, :width)
if _width # if _width
# outer_width
# else
# window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
# end
outer_width outer_width
else
window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
end
end end
def fits_on_line?(element) # Flow def fits_on_line?(element) # Flow
p [@options[:id], @width] if @options[:id]
@current_position.x + element.outer_width <= max_width && @current_position.x + element.outer_width <= max_width &&
@current_position.x + element.outer_width <= window.width @current_position.x + element.outer_width <= window.width
end end
@@ -158,7 +205,6 @@ module CyberarmEngine
element.y = element.style.margin_top + @current_position.y element.y = element.style.margin_top + @current_position.y
@current_position.x += element.outer_width @current_position.x += element.outer_width
@current_position.x = @style.margin_left if @current_position.x >= max_width
end end
def tallest_neighbor(querier, _y_position) # Flow def tallest_neighbor(querier, _y_position) # Flow
@@ -171,14 +217,14 @@ module CyberarmEngine
response response
end end
def position_on_next_line(child) # Flow def position_on_next_line(element) # Flow
@current_position.x = @style.margin_left @current_position.x = @style.margin_left + @style.padding_left
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height @current_position.y += tallest_neighbor(element, @current_position.y).outer_height
child.x = child.style.margin_left + @current_position.x element.x = element.style.margin_left + @current_position.x
child.y = child.style.margin_top + @current_position.y element.y = element.style.margin_top + @current_position.y
@current_position.x += child.outer_width @current_position.x += element.outer_width
end end
def move_to_next_line(element) # Stack def move_to_next_line(element) # Stack
@@ -194,9 +240,8 @@ module CyberarmEngine
if @scroll_position.y < 0 if @scroll_position.y < 0
@scroll_position.y += @scroll_speed @scroll_position.y += @scroll_speed
@scroll_position.y = 0 if @scroll_position.y > 0 @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 return :handled
end end
@@ -210,9 +255,8 @@ module CyberarmEngine
if @scroll_position.y.abs < max_scroll_height if @scroll_position.y.abs < max_scroll_height
@scroll_position.y -= @scroll_speed @scroll_position.y -= @scroll_speed
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height @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 return :handled
end end

View File

@@ -98,7 +98,7 @@ module CyberarmEngine
end end
def row_at(y) def row_at(y)
((y - @text.y) / @text.textobject.height).round ((y - @text.y) / @text.textobject.height).floor
end end
def column_at(x, y) def column_at(x, y)

View File

@@ -1,6 +1,20 @@
module CyberarmEngine module CyberarmEngine
class Element class Element
class EditLine < Button class EditLine < Button
class TextInput < Gosu::TextInput
def filter=(filter)
@filter = filter
end
def filter(text_in)
if @filter
@filter.call(text_in)
else
text_in
end
end
end
def initialize(text, options = {}, block = nil) def initialize(text, options = {}, block = nil)
@filter = options.delete(:filter) @filter = options.delete(:filter)
super(text, options, block) super(text, options, block)
@@ -14,17 +28,11 @@ module CyberarmEngine
@caret_last_interval = Gosu.milliseconds @caret_last_interval = Gosu.milliseconds
@show_caret = true @show_caret = true
@text_input = Gosu::TextInput.new @text_input = TextInput.new
@text_input.filter = @filter
@text_input.text = text @text_input.text = text
@last_text_value = text @last_text_value = text
@last_caret_position = @text_input.caret_pos
if @filter && @filter.respond_to?(:call)
@text_input.instance_variable_set(:@filter, @filter)
def @text_input.filter(text_in)
@filter.call(text_in)
end
end
@offset_x = 0 @offset_x = 0
@offset_y = 0 @offset_y = 0
@@ -75,6 +83,12 @@ module CyberarmEngine
publish(:changed, value) publish(:changed, value)
end end
if @last_caret_position != @text_input.caret_pos
@last_caret_position = @text_input.caret_pos
@show_caret = true
@caret_last_interval = Gosu.milliseconds
end
if Gosu.milliseconds >= @caret_last_interval + @caret_interval if Gosu.milliseconds >= @caret_last_interval + @caret_interval
@caret_last_interval = Gosu.milliseconds @caret_last_interval = Gosu.milliseconds
@@ -98,20 +112,20 @@ module CyberarmEngine
@text_input.caret_pos = @text_input.text.length @text_input.caret_pos = @text_input.text.length
when Gosu::KB_C when Gosu::KB_C
if @text_input.selection_start < @text_input.caret_pos Gosu.clipboard = if @text_input.selection_start < @text_input.caret_pos
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos]) @text_input.text[@text_input.selection_start...@text_input.caret_pos]
else else
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start]) @text_input.text[@text_input.caret_pos...@text_input.selection_start]
end end
when Gosu::KB_X when Gosu::KB_X
chars = @text_input.text.chars chars = @text_input.text.chars
if @text_input.selection_start < @text_input.caret_pos if @text_input.selection_start < @text_input.caret_pos
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos]) Gosu.clipboard = @text_input.text[@text_input.selection_start...@text_input.caret_pos]
chars.slice!(@text_input.selection_start, @text_input.caret_pos) chars.slice!(@text_input.selection_start, @text_input.caret_pos)
else else
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start]) Gosu.clipboard = @text_input.text[@text_input.caret_pos...@text_input.selection_start]
chars.slice!(@text_input.caret_pos, @text_input.selection_start) chars.slice!(@text_input.caret_pos, @text_input.selection_start)
end end
@@ -119,10 +133,9 @@ module CyberarmEngine
when Gosu::KB_V when Gosu::KB_V
if instance_of?(EditLine) # EditLine assumes a single line of text if instance_of?(EditLine) # EditLine assumes a single line of text
@text_input.text = @text_input.text.insert(@text_input.caret_pos, @text_input.insert_text(Gosu.clipboard.gsub("\n", ""))
Clipboard.paste.encode("UTF-8").gsub("\n", ""))
else else
@text_input.text = @text_input.text.insert(@text_input.caret_pos, Clipboard.paste.encode("UTF-8")) @text_input.insert_text(Gosu.clipboard)
end end
end end
end end
@@ -180,7 +193,7 @@ module CyberarmEngine
if @type == :password if @type == :password
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length) @text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
else else
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) @text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
end end
end end
@@ -197,20 +210,35 @@ module CyberarmEngine
end end
def focus(sender) def focus(sender)
super @focus = true
window.text_input = @text_input window.text_input = @text_input
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length @text_input.caret_pos = @text_input.selection_start = @text_input.text.length
update_styles(:active)
:handled :handled
end end
def enter(sender) def enter(sender)
_has_focus = @focus if @enabled && @focus
update_styles(:active)
elsif @enabled && !@focus
update_styles(:hover)
else
update_styles(:disabled)
end
super :handled
end
@focus = _has_focus def leave(sender)
if @enabled && @focus
update_styles(:active)
elsif @enabled && !@focus
update_styles
else
update_styles(:disabled)
end
:handled :handled
end end

View File

@@ -45,8 +45,8 @@ module CyberarmEngine
@scale_y = 1 @scale_y = 1
end end
@width = _width || @image.width.round * @scale_x @width = _width || @image.width.floor * @scale_x
@height = _height || @image.height.round * @scale_y @height = _height || @image.height.floor * @scale_y
update_background update_background
end end

View File

@@ -13,10 +13,12 @@ module CyberarmEngine
@style.background_canvas.background = default(:background) @style.background_canvas.background = default(:background)
# TODO: "Clean Up" into own class? # TODO: "Clean Up" into own class?
@menu = Stack.new(parent: parent, width: @options[:width], theme: @options[:theme]) @menu = Stack.new(parent: self, theme: @options[:theme])
@menu.define_singleton_method(:recalculate_menu) do @menu.define_singleton_method(:recalculate_menu) do
@x = @__list_box.x @x = @__list_box.x
@y = @__list_box.y + @__list_box.height @y = @__list_box.y + @__list_box.height
@y = @__list_box.y - height if @y + height > window.height
end end
@menu.instance_variable_set(:"@__list_box", self) @menu.instance_variable_set(:"@__list_box", self)
@@ -29,6 +31,13 @@ module CyberarmEngine
self.choose = @choose self.choose = @choose
end end
def render
super
w = @text.textobject.text_width("")
@text.textobject.draw_text("", @x + content_width - w, @y + @style.padding_top, @z, 1, 1, @text.color)
end
def choose=(item) def choose=(item)
valid = @items.detect { |i| i == item } valid = @items.detect { |i| i == item }
raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid
@@ -55,6 +64,8 @@ module CyberarmEngine
def show_menu def show_menu
@menu.clear @menu.clear
@menu.style.width = width
@items.each do |item| @items.each do |item|
next if item == self.value next if item == self.value

View File

@@ -1,9 +1,16 @@
module CyberarmEngine module CyberarmEngine
class Element class Element
class Progress < Element class Progress < Element
attr_reader :type
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
super(options, block) 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) @fraction_background = Background.new(background: @style.fraction_background)
self.value = options[:fraction] || 0.0 self.value = options[:fraction] || 0.0
end end
@@ -24,15 +31,45 @@ module CyberarmEngine
def update_background def update_background
super 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.y = @style.border_thickness_top + @style.padding_top + @y
@fraction_background.z = @z @fraction_background.z = @z
@fraction_background.width = @width * @fraction @fraction_background.width = @width * (@type == :marquee ? @marquee_width : @fraction)
@fraction_background.height = @height @fraction_background.height = @height
@fraction_background.background = @style.fraction_background @fraction_background.background = @style.fraction_background
end 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 def value
@fraction @fraction
end end

View File

@@ -42,7 +42,7 @@ module CyberarmEngine
@step_size = @options[:step] || 0.1 @step_size = @options[:step] || 0.1
@value = @options[:value] || (@range.first + @range.last) / 2 @value = @options[:value] || (@range.first + @range.last) / 2
@handle = Handle.new("", parent: self, width: 8, height: 1.0) { close } @handle = Handle.new("", parent: self, theme: options[:theme], width: 8, height: 1.0) { close }
add(@handle) add(@handle)
end end
@@ -61,10 +61,10 @@ module CyberarmEngine
end end
def position_handle def position_handle
@handle.x = @x + @style.padding_left + @style.border_thickness_left + @handle.x = @x + @handle.style.margin_left + @style.padding_left + @style.border_thickness_left +
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f) ((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
@handle.y = @y + @style.border_thickness_top + @style.padding_top @handle.y = @y + @handle.style.margin_top + @style.border_thickness_top + @style.padding_top
end end
def draw def draw

View File

@@ -7,6 +7,7 @@ module CyberarmEngine
@text = Text.new( @text = Text.new(
text, font: @options[:font], z: @z, color: @options[:color], text, font: @options[:font], z: @z, color: @options[:color],
size: @options[:text_size], shadow: @options[:text_shadow], size: @options[:text_size], shadow: @options[:text_shadow],
static: @options[:text_static],
shadow_size: @options[:text_shadow_size], shadow_size: @options[:text_shadow_size],
shadow_color: @options[:text_shadow_color], shadow_color: @options[:text_shadow_color],
border: @options[:text_border], border: @options[:text_border],
@@ -18,8 +19,15 @@ module CyberarmEngine
end end
def render def render
# Gosu.clip_to is too expensive to always use so check if we actually need it.
if @text.width > width || @text.height > height
Gosu.clip_to(@x, @y, width, height) do
@text.draw @text.draw
end end
else
@text.draw
end
end
def recalculate def recalculate
unless @enabled unless @enabled
@@ -36,13 +44,13 @@ module CyberarmEngine
handle_text_wrapping(_width) handle_text_wrapping(_width)
@width = _width || @text.width.round @width = _width || @text.width.floor
@height = _height || @text.height.round @height = _height || @text.height.floor
@text.y = @style.border_thickness_top + @style.padding_top + @y @text.y = @style.border_thickness_top + @style.padding_top + @y
@text.z = @z + 3 @text.z = @z + 3
if (text_alignment = @options[:text_align]) if (text_alignment = @options[:text_align] || @options[:text_h_align])
case text_alignment case text_alignment
when :left when :left
@text.x = @style.border_thickness_left + @style.padding_left + @x @text.x = @style.border_thickness_left + @style.padding_left + @x
@@ -57,6 +65,19 @@ module CyberarmEngine
end end
end end
if (vertical_alignment = @options[:text_v_align])
case vertical_alignment
when :center
@text.y = if @text.height <= height
@y + height / 2 - @text.height / 2
else
@style.border_thickness_top + @style.padding_top + @y
end
when :bottom
@text.y = @y + outer_height - (@text.height + @style.border_thickness_bottom + @style.padding_bottom)
end
end
update_background update_background
end end
@@ -142,9 +163,12 @@ module CyberarmEngine
old_width = width old_width = width
old_height = height 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) publish(:changed, self.value)
end end

View File

@@ -5,7 +5,7 @@ module CyberarmEngine
def initialize(options, block = nil) def initialize(options, block = nil)
if options.dig(:theme, :ToggleButton, :checkmark_image) if options.dig(:theme, :ToggleButton, :checkmark_image)
options[:theme][:ToggleButton][:image_width] ||= options[:theme][:Label][:text_size] options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size]
super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block) super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)
@_image = @image @_image = @image

View File

@@ -12,7 +12,7 @@ module CyberarmEngine
@root_container = Element::Stack.new(gui_state: self) @root_container = Element::Stack.new(gui_state: self)
@game_objects << @root_container @game_objects << @root_container
$__current_container__ = @root_container CyberarmEngine::Element::Container.current_container = @root_container
@active_width = window.width @active_width = window.width
@active_height = window.height @active_height = window.height
@@ -75,10 +75,10 @@ module CyberarmEngine
@root_container.recalculate @root_container.recalculate
@pending_recalculate_request = false @pending_recalculate_request = false
@pending_element_recalculate_requests.clear # GUI has already been recalculated
end end
@pending_element_recalculate_requests.each(&:recalculate) @pending_element_recalculate_requests.each(&:recalculate)
@pending_element_recalculate_requests.clear
if @pending_focus_request if @pending_focus_request
@pending_focus_request = false @pending_focus_request = false
@@ -90,6 +90,8 @@ module CyberarmEngine
@menu&.update @menu&.update
super super
return unless window.has_focus?
new_mouse_over = @menu.hit_element?(window.mouse_x, window.mouse_y) if @menu new_mouse_over = @menu.hit_element?(window.mouse_x, window.mouse_y) if @menu
new_mouse_over ||= @root_container.hit_element?(window.mouse_x, window.mouse_y) new_mouse_over ||= @root_container.hit_element?(window.mouse_x, window.mouse_y)
@@ -135,10 +137,6 @@ module CyberarmEngine
@active_height = window.height @active_height = window.height
end end
def tool_tip_delay
250 # ms
end
def button_down(id) def button_down(id)
super super
@@ -173,6 +171,13 @@ module CyberarmEngine
end end
@focus.button_up(id) if @focus.respond_to?(:button_up) @focus.button_up(id) if @focus.respond_to?(:button_up)
# Prevents menu from popping back up if the listbox is clicked to hide it.
@hid_menu_for = nil
end
def tool_tip_delay
@tip.style.delay || 250 # ms
end end
def redirect_mouse_button(button) def redirect_mouse_button(button)
@@ -183,7 +188,7 @@ module CyberarmEngine
@focus = nil @focus = nil
end end
if @mouse_over if @mouse_over && @hid_menu_for != @mouse_over
@mouse_down_position[button] = Vector.new(window.mouse_x, window.mouse_y) @mouse_down_position[button] = Vector.new(window.mouse_x, window.mouse_y)
@mouse_down_on[button] = @mouse_over @mouse_down_on[button] = @mouse_over
@@ -197,7 +202,7 @@ module CyberarmEngine
def redirect_released_mouse_button(button) def redirect_released_mouse_button(button)
hide_menu if @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu) hide_menu if @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)
if @mouse_over if @mouse_over && @hid_menu_for != @mouse_over
@mouse_over.publish(:"released_#{button}_mouse_button", window.mouse_x, window.mouse_y) @mouse_over.publish(:"released_#{button}_mouse_button", window.mouse_x, window.mouse_y)
if @mouse_over == @mouse_down_on[button] if @mouse_over == @mouse_down_on[button]
@mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x, @mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x,
@@ -253,6 +258,9 @@ module CyberarmEngine
end end
def hide_menu def hide_menu
return unless @menu
@hid_menu_for = @menu.parent
@menu = nil @menu = nil
end end

View File

@@ -76,6 +76,7 @@ module CyberarmEngine
border_radius: 0, border_radius: 0,
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)], background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
text_align: :center, text_align: :center,
text_v_align: :center,
text_wrap: :none, text_wrap: :none,
hover: { hover: {
@@ -102,7 +103,12 @@ module CyberarmEngine
caret_color: Gosu::Color::WHITE, caret_color: Gosu::Color::WHITE,
caret_interval: 500, caret_interval: 500,
selection_color: Gosu::Color.rgba(255, 128, 50, 200), selection_color: Gosu::Color.rgba(255, 128, 50, 200),
text_align: :left text_align: :left,
text_static: false # static text causes issues correctly displaying caret position
},
EditBox: { # < EditLine
text_v_align: :top
}, },
Image: { # < Element Image: { # < Element
@@ -154,6 +160,7 @@ module CyberarmEngine
}, },
ToolTip: { # < TextBlock ToolTip: { # < TextBlock
delay: 100, # ms
color: Gosu::Color::WHITE, color: Gosu::Color::WHITE,
padding_top: 4, padding_top: 4,
padding_bottom: 4, padding_bottom: 4,

View File

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

View File

@@ -8,21 +8,33 @@ module CyberarmEngine
attr_accessor :show_cursor attr_accessor :show_cursor
attr_writer :exit_on_opengl_error attr_writer :exit_on_opengl_error
attr_reader :last_frame_time attr_reader :last_frame_time, :states
def self.now def self.now
Gosu.milliseconds Gosu.milliseconds
end end
def self.dt def self.dt
$window.last_frame_time / 1000.0 instance.last_frame_time / 1000.0
end
def self.instance=(window)
raise ArgumentError, "Expected window to be a subclass of CyberarmEngine::Window, got: #{window.class}" unless window.is_a?(CyberarmEngine::Window)
@@instance = window
end
def self.instance
@@instance
end end
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false) def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false)
@show_cursor = false @show_cursor = false
@has_focus = false
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable, borderless: borderless) super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable, borderless: borderless)
$window = self Window.instance = self
@last_frame_time = Gosu.milliseconds - 1 @last_frame_time = Gosu.milliseconds - 1
@current_frame_time = Gosu.milliseconds @current_frame_time = Gosu.milliseconds
self.caption = "CyberarmEngine #{CyberarmEngine::VERSION} #{Gosu.language}" self.caption = "CyberarmEngine #{CyberarmEngine::VERSION} #{Gosu.language}"
@@ -34,13 +46,14 @@ module CyberarmEngine
end end
def draw def draw
current_state.draw if current_state current_state&.draw
end end
def update def update
Stats.clear Stats.clear
current_state.update if current_state current_state&.update
@last_frame_time = Gosu.milliseconds - @current_frame_time @last_frame_time = Gosu.milliseconds - @current_frame_time
@current_frame_time = Gosu.milliseconds @current_frame_time = Gosu.milliseconds
end end
@@ -49,6 +62,48 @@ module CyberarmEngine
@show_cursor @show_cursor
end end
def needs_redraw?
current_state ? current_state.needs_redraw? : true
end
def drop(filename)
current_state&.drop(filename)
end
def gamepad_connected(index)
current_state&.gamepad_connected(index)
end
def gamepad_disconnected(index)
current_state&.gamepad_disconnected(index)
end
def gain_focus
@has_focus = true
current_state&.gain_focus
end
def lose_focus
@has_focus = false
current_state&.lose_focus
end
def button_down(id)
super
current_state&.button_down(id)
end
def button_up(id)
super
current_state&.button_up(id)
end
def close
current_state ? current_state.close : super
end
def dt def dt
@last_frame_time / 1000.0 @last_frame_time / 1000.0
end end
@@ -61,16 +116,6 @@ module CyberarmEngine
@exit_on_opengl_error @exit_on_opengl_error
end end
def button_down(id)
super
current_state.button_down(id) if current_state
end
def button_up(id)
super
current_state.button_up(id) if current_state
end
def push_state(klass, options = {}) def push_state(klass, options = {})
options = { setup: true }.merge(options) options = { setup: true }.merge(options)
@@ -94,12 +139,6 @@ module CyberarmEngine
@states.last @states.last
end end
def previous_state
if @states.size > 1 && (state = @states[@states.size - 2])
state
end
end
def pop_state def pop_state
@states.pop @states.pop
end end
@@ -108,6 +147,10 @@ module CyberarmEngine
@states.shift @states.shift
end end
def has_focus?
@has_focus
end
# Sourced from https://gist.github.com/ippa/662583 # Sourced from https://gist.github.com/ippa/662583
def draw_circle(cx, cy, r, z = 9999, color = Gosu::Color::GREEN, step = 10) def draw_circle(cx, cy, r, z = 9999, color = Gosu::Color::GREEN, step = 10)
0.step(360, step) do |a1| 0.step(360, step) do |a1|