Compare commits

...

6 Commits

Author SHA1 Message Date
00e47d61a2 Bump version 2023-11-17 17:11:09 -06:00
1882673fff Improvements to layout recalculating, reduces need to manually force a refresh (F5) 2023-11-17 17:10:53 -06:00
286efa2059 Update required files 2023-11-17 15:07:08 -06:00
6b9af5c87c Merged in gosu_notifications 2023-11-17 15:06:38 -06:00
af3a3ff938 Merged in gosu_more_drawables 2023-11-17 15:06:21 -06:00
bcff35bd7b Sync 2023-11-17 14:08:21 -06:00
11 changed files with 509 additions and 15 deletions

View File

@@ -7,13 +7,19 @@ else
end
require "json"
require "excon"
require "gosu_more_drawables"
require_relative "cyberarm_engine/version"
require_relative "cyberarm_engine/stats"
require_relative "cyberarm_engine/common"
require_relative "cyberarm_engine/gosu_ext/draw_arc"
require_relative "cyberarm_engine/gosu_ext/draw_circle"
require_relative "cyberarm_engine/gosu_ext/draw_path"
require_relative "cyberarm_engine/notification"
require_relative "cyberarm_engine/notification_manager"
require_relative "cyberarm_engine/game_object"
require_relative "cyberarm_engine/window"

View File

@@ -0,0 +1,98 @@
module Gosu
# Draw an arc around the point x and y.
#
# Color accepts the following: *Gosu::Color*, *Array* (with 2 colors), or a *Hash* with keys: _from:_ and _to:_ both colors.
#
# With a *Gosu::Color* the arc will be painted with color
#
# With an *Array* the first *Gosu::Color* with be the innermost color and the last *Gosu::Color* with be the outermost color
#
# With a *Hash* the arc will smoothly transition from the start of the arc to the end
# @example
# # Using a Hash
# Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, {from: Gosu::Color::BLUE, to: Gosu::Color::GREEN}, 0, :default)
#
# # Using an Array
# Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, [Gosu::Color::BLUE, Gosu::Color::GREEN], 0, :default)
#
# # Using a Gosu::Color
# Gosu.draw_arc(100, 100, 50, 0.5, 128, 4, Gosu::Color::BLUE, 0, :default)
#
#
# @param x X position.
# @param y Y position.
# @param radius radius of arc, in pixels.
# @param percentage how complete the segment is, _0.0_ is 0% and _1.0_ is 100%.
# @param segments how many segments for arc, more will appear smoother, less will appear jagged.
# @param thickness how thick arc will be.
# @param color [Gosu::Color, Array<Gosu::Color, Gosu::Color>, Hash{from: start_color, to: end_color}] color or colors to draw the arc with.
# @param z Z position.
# @param mode blend mode.
#
# @note _thickness_ is subtracted from radius, meaning that the arc will grow towards the origin, not away from it.
#
# @return [void]
def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default)
segments = 360.0 / segments
return if percentage == 0.0
0.step((359 * percentage), percentage > 0 ? segments : -segments) do |angle|
angle2 = angle + segments
point_a_left_x = x + Gosu.offset_x(angle, radius - thickness)
point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)
point_a_right_x = x + Gosu.offset_x(angle2, radius - thickness)
point_a_right_y = y + Gosu.offset_y(angle2, radius - thickness)
point_b_left_x = x + Gosu.offset_x(angle, radius)
point_b_left_y = y + Gosu.offset_y(angle, radius)
point_b_right_x = x + Gosu.offset_x(angle2, radius)
point_b_right_y = y + Gosu.offset_y(angle2, radius)
if color.is_a?(Array)
Gosu.draw_quad(
point_a_left_x, point_a_left_y, color.first,
point_b_left_x, point_b_left_y, color.last,
point_a_right_x, point_a_right_y, color.first,
point_b_right_x, point_b_right_y, color.last,
z, mode
)
elsif color.is_a?(Hash)
start_color = color[:from]
end_color = color[:to]
color_a = Gosu::Color.rgba(
(end_color.red - start_color.red) * (angle / 360.0) + start_color.red,
(end_color.green - start_color.green) * (angle / 360.0) + start_color.green,
(end_color.blue - start_color.blue) * (angle / 360.0) + start_color.blue,
(end_color.alpha - start_color.alpha) * (angle / 360.0) + start_color.alpha,
)
color_b = Gosu::Color.rgba(
(end_color.red - start_color.red) * (angle2 / 360.0) + start_color.red,
(end_color.green - start_color.green) * (angle2 / 360.0) + start_color.green,
(end_color.blue - start_color.blue) * (angle2 / 360.0) + start_color.blue,
(end_color.alpha - start_color.alpha) * (angle2 / 360.0) + start_color.alpha,
)
Gosu.draw_quad(
point_a_left_x, point_a_left_y, color_a,
point_b_left_x, point_b_left_y, color_a,
point_a_right_x, point_a_right_y, color_b,
point_b_right_x, point_b_right_y, color_b,
z, mode
)
else
Gosu.draw_quad(
point_a_left_x, point_a_left_y, color,
point_b_left_x, point_b_left_y, color,
point_a_right_x, point_a_right_y, color,
point_b_right_x, point_b_right_y, color,
z, mode
)
end
end
end
end

View File

@@ -0,0 +1,31 @@
module Gosu
##
# Draw a filled circled around point X and Y.
#
# @param x X position.
# @param y Y position.
# @param radius radius of circle, in pixels.
# @param step_size resolution of circle, more steps will apear smoother, less will appear jagged.
# @param color color to draw circle with.
# @param mode blend mode.
#
# @return [void]
def self.draw_circle(x, y, radius, step_size = 36, color = Gosu::Color::WHITE, z = 0, mode = :default)
step_size = (360.0 / step_size).floor
0.step(359, step_size) do |angle|
angle2 = angle + step_size
point_lx = x + Gosu.offset_x(angle, radius)
point_ly = y + Gosu.offset_y(angle, radius)
point_rx = x + Gosu.offset_x(angle2, radius)
point_ry = y + Gosu.offset_y(angle2, radius)
Gosu.draw_triangle(
point_lx, point_ly, color,
point_rx, point_ry, color,
x, y, color, z, mode
)
end
end
end

View File

@@ -0,0 +1,17 @@
module Gosu
PathNode = Struct.new(:x, :y)
def self.draw_path(nodes, color = Gosu::Color::WHITE, z = 0, mode = :default)
last_node = nodes.first
nodes[1..nodes.size - 1].each do |current_node|
Gosu.draw_line(
last_node.x, last_node.y, color,
current_node.x, current_node.y, color,
z, mode
)
last_node = current_node
end
end
end

View File

@@ -0,0 +1,83 @@
module CyberarmEngine
class Notification
WIDTH = 500
HEIGHT = 64
EDGE_WIDTH = 8
TRANSITION_DURATION = 750
PADDING = 8
TTL_LONG = 5_000
TTL_MEDIUM = 3_250
TTL_SHORT = 1_500
TIME_TO_LIVE = TTL_MEDIUM
BACKGROUND_COLOR = Gosu::Color.new(0xaa313533)
EDGE_COLOR = Gosu::Color.new(0xaa010101)
ICON_COLOR = Gosu::Color.new(0xddffffff)
TITLE_COLOR = Gosu::Color.new(0xddffffff)
TAGLINE_COLOR = Gosu::Color.new(0xddaaaaaa)
TITLE_SIZE = 28
TAGLINE_SIZE = 18
ICON_SIZE = HEIGHT - PADDING * 2
TITLE_FONT = Gosu::Font.new(TITLE_SIZE, bold: true)
TAGLINE_FONT = Gosu::Font.new(TAGLINE_SIZE)
PRIORITY_HIGH = 1.0
PRIORITY_MEDIUM = 0.5
PRIORITY_LOW = 0.0
LINEAR_TRANSITION = :linear
EASE_IN_OUT_TRANSITION = :ease_in_out
attr_reader :priority, :title, :tagline, :icon, :time_to_live, :transition_duration, :transition_type
def initialize(
host:, priority:, title:, title_color: TITLE_COLOR, tagline: "", tagline_color: TAGLINE_COLOR, icon: nil, icon_color: ICON_COLOR,
edge_color: EDGE_COLOR, background_color: BACKGROUND_COLOR, time_to_live: TIME_TO_LIVE, transition_duration: TRANSITION_DURATION,
transition_type: EASE_IN_OUT_TRANSITION
)
@host = host
@priority = priority
@title = title
@title_color = title_color
@tagline = tagline
@tagline_color = tagline_color
@icon = icon
@icon_color = icon_color
@edge_color = edge_color
@background_color = background_color
@time_to_live = time_to_live
@transition_duration = transition_duration
@transition_type = transition_type
@icon_scale = ICON_SIZE.to_f / @icon.width if @icon
end
def draw
Gosu.draw_rect(0, 0, WIDTH, HEIGHT, @background_color)
if @host.edge == :top
Gosu.draw_rect(0, HEIGHT - EDGE_WIDTH, WIDTH, EDGE_WIDTH, @edge_color)
@icon.draw(EDGE_WIDTH + PADDING, PADDING, 0, @icon_scale, @icon_scale, @icon_color) if @icon
elsif @host.edge == :bottom
Gosu.draw_rect(0, 0, WIDTH, EDGE_WIDTH, @edge_color)
@icon.draw(EDGE_WIDTH + PADDING, PADDING, 0, @icon_scale, @icon_scale, @icon_color) if @icon
elsif @host.edge == :right
Gosu.draw_rect(0, 0, EDGE_WIDTH, HEIGHT, @edge_color)
@icon.draw(EDGE_WIDTH + PADDING, PADDING, 0, @icon_scale, @icon_scale, @icon_color) if @icon
else
Gosu.draw_rect(WIDTH - EDGE_WIDTH, 0, EDGE_WIDTH, HEIGHT, @edge_color)
@icon.draw(PADDING, PADDING, 0, @icon_scale, @icon_scale, @icon_color) if @icon
end
icon_space = @icon ? ICON_SIZE + PADDING : 0
TITLE_FONT.draw_text(@title, PADDING + EDGE_WIDTH + icon_space, PADDING, 0, 1, 1, @title_color)
TAGLINE_FONT.draw_text(@tagline, PADDING + EDGE_WIDTH + icon_space, PADDING + TITLE_FONT.height, 0, 1, 1, @tagline_color)
end
end
end

View File

@@ -0,0 +1,242 @@
module CyberarmEngine
class NotificationManager
EDGE_TOP = :top
EDGE_BOTTOM = :bottom
EDGE_RIGHT = :right
EDGE_LEFT = :left
MODE_DEFAULT = :slide
MODE_CIRCLE = :circle
attr_reader :edge, :mode, :max_visible, :notifications
def initialize(edge: EDGE_RIGHT, mode: MODE_DEFAULT, window:, max_visible: 1)
@edge = edge
@mode = mode
@window = window
@max_visible = max_visible
@notifications = []
@drivers = []
@slots = Array.new(max_visible, nil)
end
def draw
@drivers.each do |driver|
case @edge
when :left, :right
x = @edge == :right ? @window.width + driver.x : -Notification::WIDTH + driver.x
y = driver.y + Notification::HEIGHT / 2
Gosu.translate(x, y + (Notification::HEIGHT + Notification::PADDING) * driver.slot) do
driver.draw
end
when :top, :bottom
x = @window.width / 2 - Notification::WIDTH / 2
y = @edge == :top ? driver.y - Notification::HEIGHT : @window.height + driver.y
slot_position = (Notification::HEIGHT + Notification::PADDING) * driver.slot
slot_position *= -1 if @edge == :bottom
Gosu.translate(x, y + slot_position) do
driver.draw
end
end
end
end
def update
show_next_notification if @drivers.size < @max_visible
@drivers.each do |driver|
if driver.done?
@slots[driver.slot] = nil
@drivers.delete(driver)
end
end
@drivers.each(&:update)
end
def show_next_notification
notification = @notifications.sort { |n| n.priority }.reverse.shift
return unless notification
return if available_slot_index < lowest_used_slot
@notifications.delete(notification)
@drivers << Driver.new(edge: @edge, mode: @mode, notification: notification, slot: available_slot_index)
slot = @slots[available_slot_index] = @drivers.last
end
def available_slot_index
@slots.each_with_index do |slot, i|
return i unless slot
end
return -1
end
def lowest_used_slot
@slots.each_with_index do |slot, i|
return i if slot
end
return -1
end
def highest_used_slot
_slot = -1
@slots.each_with_index do |slot, i|
_slot = i if slot
end
return _slot
end
def create_notification(**args)
notification = Notification.new(host: self, **args)
@notifications << notification
end
class Driver
attr_reader :x, :y, :notification, :slot
def initialize(edge:, mode:, notification:, slot:)
@edge = edge
@mode = mode
@notification = notification
@slot = slot
@x, @y = 0, 0
@delta = Gosu.milliseconds
@accumulator = 0.0
@born_at = Gosu.milliseconds
@duration_completed_at = Float::INFINITY
@transition_completed_at = Float::INFINITY
end
def transition_in_complete?
Gosu.milliseconds - @born_at >= @notification.transition_duration
end
def duration_completed?
Gosu.milliseconds - @transition_completed_at >= @notification.time_to_live
end
def done?
Gosu.milliseconds - @duration_completed_at >= @notification.transition_duration
end
def draw
ratio = 0.0
if not transition_in_complete?
ratio = animation_ratio
elsif transition_in_complete? and not duration_completed?
ratio = 1.0
elsif duration_completed?
ratio = 1.0 - animation_ratio
end
case @mode
when MODE_DEFAULT
Gosu.clip_to(0, 0, Notification::WIDTH, Notification::HEIGHT * ratio) do
@notification.draw
end
when MODE_CIRCLE
half = Notification::WIDTH / 2
Gosu.clip_to(half - (half * ratio), 0, Notification::WIDTH * ratio, Notification::HEIGHT) do
@notification.draw
end
end
end
def update
case @mode
when MODE_DEFAULT
update_default
when MODE_CIRCLE
update_circle
end
@accumulator += Gosu.milliseconds - @delta
@delta = Gosu.milliseconds
end
def update_default
case @edge
when :left, :right
if not transition_in_complete? # Slide In
@x = @edge == :right ? -x_offset : x_offset
elsif transition_in_complete? and not duration_completed?
@x = @edge == :right ? -Notification::WIDTH : Notification::WIDTH if @x.abs != Notification::WIDTH
@transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
@accumulator = 0.0
elsif duration_completed? # Slide Out
@x = @edge == :right ? x_offset - Notification::WIDTH : Notification::WIDTH - x_offset
@x = 0 if @edge == :left and @x <= 0
@x = 0 if @edge == :right and @x >= 0
@duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
end
when :top, :bottom
if not transition_in_complete? # Slide In
@y = @edge == :top ? y_offset : -y_offset
elsif transition_in_complete? and not duration_completed?
@y = @edge == :top ? Notification::HEIGHT : -Notification::HEIGHT if @x.abs != Notification::HEIGHT
@transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
@accumulator = 0.0
elsif duration_completed? # Slide Out
@y = @edge == :top ? Notification::HEIGHT - y_offset : y_offset - Notification::HEIGHT
@y = 0 if @edge == :top and @y <= 0
@y = 0 if @edge == :bottom and @y >= 0
@duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
end
end
end
def update_circle
case @edge
when :top, :bottom
@y = @edge == :top ? Notification::HEIGHT : -Notification::HEIGHT
when :left, :right
@x = @edge == :right ? -Notification::WIDTH : Notification::WIDTH
end
if transition_in_complete? and not duration_completed?
@transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
@accumulator = 0.0
elsif duration_completed?
@duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
end
end
def animation_ratio
x = (@accumulator / @notification.transition_duration)
case @notification.transition_type
when Notification::LINEAR_TRANSITION
x.clamp(0.0, 1.0)
when Notification::EASE_IN_OUT_TRANSITION # https://easings.net/#easeInOutQuint
(x < 0.5 ? 16 * x * x * x * x * x : 1 - ((-2 * x + 2) ** 5) / 2).clamp(0.0, 1.0)
end
end
def x_offset
if not transition_in_complete? or duration_completed?
Notification::WIDTH * animation_ratio
else
0
end
end
def y_offset
if not transition_in_complete? or duration_completed?
Notification::HEIGHT * animation_ratio
else
0
end
end
end
end
end

View File

@@ -30,6 +30,8 @@ module CyberarmEngine
@y = @style.y
@z = @style.z
@old_width = 0
@old_height = 0
@width = 0
@height = 0
@@ -342,6 +344,7 @@ module CyberarmEngine
end
def update
recalculate_if_size_changed
end
def button_down(id)
@@ -558,6 +561,15 @@ module CyberarmEngine
@style.background_image_canvas.image = @style.background_image
end
def recalculate_if_size_changed
if !is_a?(ToolTip) && (@old_width != width || @old_height != height)
root.gui_state.request_recalculate
@old_width = width
@old_height = height
end
end
def root
return self if is_root?
@@ -586,11 +598,13 @@ module CyberarmEngine
element.children.find { |child| child.child_of?(element) if child.is_a?(Container) }
end
# def parent_of?(element)
# return true if element.children.find { |child| child == self }
def parent_of?(element)
return false if element == self
return false unless is_a?(Container)
return true if @children.find { |child| child == element }
# element.children.find { |child| child.parent}
# end
@children.find { |child| child.parent_of?(element) if child.is_a?(Container) }
end
def focus(_)
warn "#{self.class}#focus was not overridden!"

View File

@@ -246,6 +246,7 @@ module CyberarmEngine
end
root.gui_state.request_repaint if @width != old_width || @height != old_height
recalculate_if_size_changed
end
def layout

View File

@@ -93,6 +93,7 @@ module CyberarmEngine
update_background
root.gui_state.request_repaint if @width != old_width || @height != old_height
recalculate_if_size_changed
end
def handle_text_wrapping(max_width)

View File

@@ -78,15 +78,6 @@ module CyberarmEngine
end
def update
if @pending_recalculate_request
Stats.frame.start_timing(:gui_recalculate)
@root_container.recalculate
@pending_recalculate_request = false
Stats.frame.end_timing(:gui_recalculate)
end
Stats.frame.start_timing(:gui_element_recalculate_requests)
# puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?
@@ -95,6 +86,16 @@ module CyberarmEngine
Stats.frame.end_timing(:gui_element_recalculate_requests)
if @pending_recalculate_request
Stats.frame.start_timing(:gui_recalculate)
@root_container.recalculate
@pending_recalculate_request = false
Stats.frame.end_timing(:gui_recalculate)
end
if @pending_focus_request
@pending_focus_request = false

View File

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