Compare commits

16 Commits

Author SHA1 Message Date
1644ff8a27 Bump version 2024-02-28 20:43:28 -06:00
d6a99b935a Fixed styles not applying correctly for non-default styles 2024-02-28 10:53:49 -06:00
c4e47b8e38 Bump version 2024-01-18 14:10:28 -06:00
d0e1772f33 Fixed container that is scrolled down getting stuck being 'overscrolled' when resized 2024-01-18 14:08:33 -06:00
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
5d1c195917 Added support for rendering multiple lights, standardized shaders to use snake case for variables and camel case for functions, stubbed PBR material shader include. 2023-07-29 14:09:23 -05:00
9a6e1df032 Remove need to do a full gui recalc 3 or more times for the layout to work (current implementation slows things down a bit, but seems more reliable then brute forcing 3x+) 2023-06-18 19:42:38 -05:00
b1b8fc8556 Cache scroll width/height 2023-06-18 14:18:15 -05:00
a60b09a110 Further scrolling improvements (should be smoother/more consistent with varied frame time) 2023-06-18 13:50:32 -05:00
81a632942e Round scroll position to prevent rendering issues caused by floats 2023-06-18 12:38:12 -05:00
5ef8023aca Improved scrolling 2023-06-18 12:26:08 -05:00
20 changed files with 743 additions and 126 deletions

View File

@@ -5,25 +5,25 @@ layout (location = 1) out vec4 fragColor;
layout (location = 2) out vec3 fragNormal; layout (location = 2) out vec3 fragNormal;
layout (location = 3) out vec3 fragUV; layout (location = 3) out vec3 fragUV;
in vec3 outPosition, outColor, outNormal, outUV, outFragPos, outCameraPos; in vec3 out_position, out_color, out_normal, out_uv, out_frag_pos, out_camera_pos;
out vec4 outputFragColor; out vec4 outputFragColor;
flat in int outHasTexture; flat in int out_has_texture;
uniform sampler2D diffuse_texture; uniform sampler2D diffuse_texture;
void main() { void main() {
vec3 result; vec3 result;
if (outHasTexture == 0) { if (out_has_texture == 0) {
result = outColor; result = out_color;
} else { } else {
result = texture(diffuse_texture, outUV.xy).xyz + 0.25; result = texture(diffuse_texture, out_uv.xy).xyz + 0.25;
} }
fragPosition = outPosition; fragPosition = out_position;
fragColor = vec4(result, 1.0); fragColor = vec4(result, 1.0);
fragNormal = outNormal; fragNormal = out_normal;
fragUV = outUV; fragUV = out_uv;
float gamma = 2.2; float gamma = 2.2;
outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma)); outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma));

View File

@@ -1,22 +1,23 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 frag_color;
@include "light_struct" @include "light_struct"
const int DIRECTIONAL = 0; const int DIRECTIONAL = 0;
const int POINT = 1; const int POINT = 1;
const int SPOT = 2; const int SPOT = 2;
in vec2 outTexCoords; flat in Light out_lights[7];
flat in Light outLight[1]; in vec2 out_tex_coords;
flat in int out_light_count;
uniform sampler2D diffuse, position, texcoord, normal, depth; uniform sampler2D diffuse, position, texcoord, normal, depth;
vec4 directionalLight(Light light) { vec4 directionalLight(Light light) {
vec3 norm = normalize(texture(normal, outTexCoords).rgb); vec3 norm = normalize(texture(normal, out_tex_coords).rgb);
vec3 diffuse_color = texture(diffuse, outTexCoords).rgb; vec3 diffuse_color = texture(diffuse, out_tex_coords).rgb;
vec3 fragPos = texture(position, outTexCoords).rgb; vec3 frag_pos = texture(position, out_tex_coords).rgb;
vec3 lightDir = normalize(light.position - fragPos); vec3 lightDir = normalize(light.position - frag_pos);
float diff = max(dot(norm, lightDir), 0); float diff = max(dot(norm, lightDir), 0);
vec3 _ambient = light.ambient; vec3 _ambient = light.ambient;
@@ -59,5 +60,10 @@ vec4 calculateLighting(Light light) {
} }
void main() { void main() {
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight[0]); frag_color = vec4(0.0);
for(int i = 0; i < out_light_count; i++)
{
frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
}
} }

View File

@@ -0,0 +1,16 @@
struct Material {
vec3 color;
vec3 roughness;
vec3 metalic;
vec3 specular;
bool use_color_texture;
bool use_roughness_texture;
bool use_metalic_texture;
bool use_specular_texture;
sampler2D color_tex;
sampler2D roughness_tex;
sampler2D metalic_tex;
sampler2D specular_tex;
};

View File

@@ -1,28 +1,28 @@
# version 330 core # version 330 core
layout(location = 0) in vec3 inPosition; layout(location = 0) in vec3 in_position;
layout(location = 1) in vec3 inColor; layout(location = 1) in vec3 in_color;
layout(location = 2) in vec3 inNormal; layout(location = 2) in vec3 in_normal;
layout(location = 3) in vec3 inUV; layout(location = 3) in vec3 in_uv;
uniform mat4 projection, view, model; uniform mat4 projection, view, model;
uniform int hasTexture; uniform int has_texture;
uniform vec3 cameraPos; uniform vec3 camera_pos;
out vec3 outPosition, outColor, outNormal, outUV; out vec3 out_position, out_color, out_normal, out_uv;
out vec3 outFragPos, outViewPos, outCameraPos; out vec3 out_frag_pos, out_view_pos, out_camera_pos;
flat out int outHasTexture; flat out int out_has_texture;
void main() { void main() {
// projection * view * model * position // projection * view * model * position
outPosition = inPosition; out_position = in_position;
outColor = inColor; out_color = in_color;
outNormal= normalize(transpose(inverse(mat3(model))) * inNormal); out_normal= normalize(transpose(inverse(mat3(model))) * in_normal);
outUV = inUV; out_uv = in_uv;
outHasTexture = hasTexture; out_has_texture = has_texture;
outCameraPos = cameraPos; out_camera_pos = camera_pos;
outFragPos = vec3(model * vec4(inPosition, 1.0)); out_frag_pos = vec3(model * vec4(in_position, 1.0));
gl_Position = projection * view * model * vec4(inPosition, 1.0); gl_Position = projection * view * model * vec4(in_position, 1.0);
} }

View File

@@ -1,17 +1,24 @@
#version 330 core #version 330 core
@include "light_struct" @include "light_struct"
layout (location = 0) in vec3 inPosition; layout (location = 0) in vec3 in_position;
layout (location = 1) in vec2 inTexCoords; layout (location = 1) in vec2 in_tex_coords;
uniform sampler2D diffuse, position, texcoord, normal, depth; uniform sampler2D diffuse, position, texcoord, normal, depth;
uniform Light light[1]; uniform int light_count;
uniform Light lights[7];
out vec2 outTexCoords; out vec2 out_tex_coords;
flat out Light outLight[1]; flat out int out_light_count;
flat out Light out_lights[7];
void main() { void main() {
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0); gl_Position = vec4(in_position.x, in_position.y, in_position.z, 1.0);
outTexCoords = inTexCoords; out_tex_coords = in_tex_coords;
outLight = light; out_light_count = light_count;
for(int i = 0; i < light_count; i++)
{
out_lights[i] = lights[i];
}
} }

View File

@@ -7,13 +7,19 @@ else
end end
require "json" require "json"
require "excon" require "excon"
require "gosu_more_drawables"
require_relative "cyberarm_engine/version" require_relative "cyberarm_engine/version"
require_relative "cyberarm_engine/stats" require_relative "cyberarm_engine/stats"
require_relative "cyberarm_engine/common" 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/game_object"
require_relative "cyberarm_engine/window" require_relative "cyberarm_engine/window"

View File

@@ -163,7 +163,7 @@ module CyberarmEngine
elsif background.is_a?(Range) elsif background.is_a?(Range)
set([background.begin, background.begin, background.end, background.end]) set([background.begin, background.begin, background.end, background.end])
else else
raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed" raise ArgumentError, "background '#{background.inspect}' of type '#{background.class}' was not able to be processed"
end end
end end
end end

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

@@ -49,9 +49,9 @@ module CyberarmEngine
@objects.each { |o| @vertex_count += o.vertices.size } @objects.each { |o| @vertex_count += o.vertices.size }
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) # start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
build_collision_tree # build_collision_tree
puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds" # puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
end end
def parse(parser) def parse(parser)
@@ -151,24 +151,24 @@ module CyberarmEngine
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id) glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
gl_error? gl_error?
# inPosition # in_position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
gl_error? gl_error?
# colors # colors
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id) glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
# inColor # in_color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil)
gl_error? gl_error?
# normals # normals
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id) glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
# inNormal # in_normal
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil) glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil)
gl_error? gl_error?
if has_texture? if has_texture?
# uvs # uvs
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id) glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
# inUV # in_uv
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil) glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
gl_error? gl_error?
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

@@ -44,7 +44,7 @@ module CyberarmEngine
shader.uniform_transform("projection", camera.projection_matrix) shader.uniform_transform("projection", camera.projection_matrix)
shader.uniform_transform("view", camera.view_matrix) shader.uniform_transform("view", camera.view_matrix)
shader.uniform_transform("model", entity.model_matrix) shader.uniform_transform("model", entity.model_matrix)
shader.uniform_vec3("cameraPosition", camera.position) shader.uniform_vector3("camera_position", camera.position)
gl_error? gl_error?
draw_model(entity.model, shader) draw_model(entity.model, shader)
@@ -154,16 +154,22 @@ module CyberarmEngine
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth)) glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
shader.uniform_integer("depth", 4) shader.uniform_integer("depth", 4)
lights.each_with_index do |light, _i| # FIXME: Try to figure out how to up this to 32 and/or beyond
shader.uniform_integer("light[0].type", light.type) # (currently fails with more then 7 lights passed in to shader)
shader.uniform_vec3("light[0].direction", light.direction) lights.each_slice(7).each do |light_group|
shader.uniform_vec3("light[0].position", light.position) light_group.each_with_index do |light, _i|
shader.uniform_vec3("light[0].diffuse", light.diffuse) shader.uniform_integer("light_count", light_group.size)
shader.uniform_vec3("light[0].ambient", light.ambient)
shader.uniform_vec3("light[0].specular", light.specular) shader.uniform_integer("lights[#{_i}].type", light.type)
shader.uniform_vector3("lights[#{_i}].direction", light.direction)
shader.uniform_vector3("lights[#{_i}].position", light.position)
shader.uniform_vector3("lights[#{_i}].diffuse", light.diffuse)
shader.uniform_vector3("lights[#{_i}].ambient", light.ambient)
shader.uniform_vector3("lights[#{_i}].specular", light.specular)
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size) glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
end end
end
glBindVertexArray(0) glBindVertexArray(0)
end end
@@ -215,7 +221,7 @@ module CyberarmEngine
offset = 0 offset = 0
model.objects.each do |object| model.objects.each do |object|
shader.uniform_boolean("hasTexture", object.has_texture?) shader.uniform_boolean("has_texture", object.has_texture?)
if object.has_texture? if object.has_texture?
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id) glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)

View File

@@ -385,7 +385,7 @@ module CyberarmEngine
# @param value [Vector] # @param value [Vector]
# @param location [Integer] # @param location [Integer]
# @return [void] # @return [void]
def uniform_vec3(variable, value, location = nil) def uniform_vector3(variable, value, location = nil)
attr_loc = location || attribute_location(variable) attr_loc = location || attribute_location(variable)
glUniform3f(attr_loc, *value.to_a[0..2]) glUniform3f(attr_loc, *value.to_a[0..2])
@@ -397,7 +397,7 @@ module CyberarmEngine
# @param value [Vector] # @param value [Vector]
# @param location [Integer] # @param location [Integer]
# @return [void] # @return [void]
def uniform_vec4(variable, value, location = nil) def uniform_vector4(variable, value, location = nil)
attr_loc = location || attribute_location(variable) attr_loc = location || attribute_location(variable)
glUniform4f(attr_loc, *value.to_a) glUniform4f(attr_loc, *value.to_a)

View File

@@ -30,6 +30,8 @@ module CyberarmEngine
@y = @style.y @y = @style.y
@z = @style.z @z = @style.z
@old_width = 0
@old_height = 0
@width = 0 @width = 0
@height = 0 @height = 0
@@ -69,8 +71,19 @@ module CyberarmEngine
root.gui_state.request_repaint root.gui_state.request_repaint
end end
def safe_style_fetch(*args) def safe_style_fetch(key, fallback_key = nil)
@style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args) # Attempt to return value for requested key
v = @style.hash.dig(@style_event, key)
return v if v
# Attempt to return overriding value
if fallback_key
v = @style.hash.dig(@style_event, fallback_key)
return v if v
end
# Fallback to default style
@style.hash.dig(:default, key) || default(key)
end end
def set_static_position def set_static_position
@@ -102,10 +115,10 @@ module CyberarmEngine
@style.background_nine_slice_from_edge = safe_style_fetch(:background_nine_slice_from_edge) @style.background_nine_slice_from_edge = safe_style_fetch(:background_nine_slice_from_edge)
@style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left) || @style.background_nine_slice_from_edge @style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge)
@style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top) || @style.background_nine_slice_from_edge @style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge)
@style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right) || @style.background_nine_slice_from_edge @style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge)
@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, :background_nine_slice_from_edge)
end end
def set_background_image def set_background_image
@@ -119,19 +132,19 @@ module CyberarmEngine
def set_border_thickness def set_border_thickness
@style.border_thickness = safe_style_fetch(:border_thickness) @style.border_thickness = safe_style_fetch(:border_thickness)
@style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness @style.border_thickness_left = safe_style_fetch(:border_thickness_left, :border_thickness)
@style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness @style.border_thickness_right = safe_style_fetch(:border_thickness_right, :border_thickness)
@style.border_thickness_top = safe_style_fetch(:border_thickness_top) || @style.border_thickness @style.border_thickness_top = safe_style_fetch(:border_thickness_top, :border_thickness)
@style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom) || @style.border_thickness @style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom, :border_thickness)
end end
def set_border_color def set_border_color
@style.border_color = safe_style_fetch(:border_color) @style.border_color = safe_style_fetch(:border_color)
@style.border_color_left = safe_style_fetch(:border_color_left) || @style.border_color @style.border_color_left = safe_style_fetch(:border_color_left, :border_color)
@style.border_color_right = safe_style_fetch(:border_color_right) || @style.border_color @style.border_color_right = safe_style_fetch(:border_color_right, :border_color)
@style.border_color_top = safe_style_fetch(:border_color_top) || @style.border_color @style.border_color_top = safe_style_fetch(:border_color_top, :border_color)
@style.border_color_bottom = safe_style_fetch(:border_color_bottom) || @style.border_color @style.border_color_bottom = safe_style_fetch(:border_color_bottom, :border_color)
@style.border_canvas.color = [ @style.border_canvas.color = [
@style.border_color_top, @style.border_color_top,
@@ -144,19 +157,19 @@ module CyberarmEngine
def set_padding def set_padding
@style.padding = safe_style_fetch(:padding) @style.padding = safe_style_fetch(:padding)
@style.padding_left = safe_style_fetch(:padding_left) || @style.padding @style.padding_left = safe_style_fetch(:padding_left, :padding)
@style.padding_right = safe_style_fetch(:padding_right) || @style.padding @style.padding_right = safe_style_fetch(:padding_right, :padding)
@style.padding_top = safe_style_fetch(:padding_top) || @style.padding @style.padding_top = safe_style_fetch(:padding_top, :padding)
@style.padding_bottom = safe_style_fetch(:padding_bottom) || @style.padding @style.padding_bottom = safe_style_fetch(:padding_bottom, :padding)
end end
def set_margin def set_margin
@style.margin = safe_style_fetch(:margin) @style.margin = safe_style_fetch(:margin)
@style.margin_left = safe_style_fetch(:margin_left) || @style.margin @style.margin_left = safe_style_fetch(:margin_left, :margin)
@style.margin_right = safe_style_fetch(:margin_right) || @style.margin @style.margin_right = safe_style_fetch(:margin_right, :margin)
@style.margin_top = safe_style_fetch(:margin_top) || @style.margin @style.margin_top = safe_style_fetch(:margin_top, :margin)
@style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin @style.margin_bottom = safe_style_fetch(:margin_bottom, :margin)
end end
def update_styles(event = :default) def update_styles(event = :default)
@@ -342,6 +355,7 @@ module CyberarmEngine
end end
def update def update
recalculate_if_size_changed
end end
def button_down(id) def button_down(id)
@@ -411,10 +425,14 @@ module CyberarmEngine
end end
def scroll_width def scroll_width
@children.sum(&:outer_width) return @cached_scroll_width if @cached_scroll_width && is_a?(Container)
@cached_scroll_width = @children.sum(&:outer_width)
end end
def scroll_height def scroll_height
return @cached_scroll_height if @cached_scroll_height && is_a?(Container)
if is_a?(CyberarmEngine::Element::Flow) if is_a?(CyberarmEngine::Element::Flow)
return 0 if @children.size.zero? return 0 if @children.size.zero?
@@ -435,18 +453,18 @@ module CyberarmEngine
pairs_ << a_ unless pairs_.last == a_ pairs_ << a_ unless pairs_.last == a_
pairs_.sum { |pair| + @style.padding_top + @style.border_thickness_top + pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom @cached_scroll_height = pairs_.sum { |pair| + @style.padding_top + @style.border_thickness_top + pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom
else else
@style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom @cached_scroll_height = @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom
end end
end end
def max_scroll_width def max_scroll_width
scroll_width - outer_width (scroll_width - outer_width).positive? ? scroll_width - outer_width : scroll_width
end end
def max_scroll_height def max_scroll_height
scroll_height - outer_height (scroll_height - outer_height).positive? ? scroll_height - outer_height : scroll_height
end end
def dimensional_size(size, dimension) def dimensional_size(size, dimension)
@@ -554,6 +572,15 @@ module CyberarmEngine
@style.background_image_canvas.image = @style.background_image @style.background_image_canvas.image = @style.background_image
end 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 def root
return self if is_root? return self if is_root?
@@ -574,6 +601,22 @@ module CyberarmEngine
@gui_state != nil @gui_state != nil
end end
def child_of?(element)
return element == self if is_root?
return false unless element.is_a?(Container)
return true if element.children.find { |child| child == self }
element.children.find { |child| child.child_of?(element) if child.is_a?(Container) }
end
def parent_of?(element)
return false if element == self
return false unless is_a?(Container)
return true if @children.find { |child| child == element }
@children.find { |child| child.parent_of?(element) if child.is_a?(Container) }
end
def focus(_) def focus(_)
warn "#{self.class}#focus was not overridden!" warn "#{self.class}#focus was not overridden!"

View File

@@ -20,7 +20,10 @@ module CyberarmEngine
@gui_state = options.delete(:gui_state) @gui_state = options.delete(:gui_state)
super super
@last_scroll_position = Vector.new(0, 0)
@scroll_position = Vector.new(0, 0) @scroll_position = Vector.new(0, 0)
@scroll_target_position = Vector.new(0, 0)
@scroll_chunk = 120
@scroll_speed = 40 @scroll_speed = 40
@text_color = options[:color] @text_color = options[:color]
@@ -33,17 +36,17 @@ module CyberarmEngine
def build def build
@block.call(self) if @block @block.call(self) if @block
root.gui_state.request_recalculate root.gui_state.request_recalculate_for(self)
end end
def add(element) def add(element)
@children << element @children << element
root.gui_state.request_recalculate root.gui_state.request_recalculate_for(self)
end end
def remove(element) def remove(element)
root.gui_state.request_recalculate if @children.delete(element) root.gui_state.request_recalculate_for(self) if @children.delete(element)
end end
def clear(&block) def clear(&block)
@@ -56,7 +59,7 @@ module CyberarmEngine
CyberarmEngine::Element::Container.current_container = old_container CyberarmEngine::Element::Container.current_container = old_container
root.gui_state.request_recalculate root.gui_state.request_recalculate_for(self)
end end
def append(&block) def append(&block)
@@ -67,7 +70,7 @@ module CyberarmEngine
CyberarmEngine::Element::Container.current_container = old_container CyberarmEngine::Element::Container.current_container = old_container
root.gui_state.request_recalculate root.gui_state.request_recalculate_for(self)
end end
def render def render
@@ -77,9 +80,11 @@ module CyberarmEngine
content_width + 1, content_width + 1,
content_height + 1 content_height + 1
) do ) do
Gosu.translate(@scroll_position.x, @scroll_position.y) do
@children.each(&:draw) @children.each(&:draw)
end end
end end
end
def debug_draw def debug_draw
super super
@@ -90,31 +95,70 @@ module CyberarmEngine
end end
def update def update
update_scroll
@children.each(&:update) @children.each(&:update)
end end
def hit_element?(x, y) def hit_element?(x, y)
return unless hit?(x, y) return unless hit?(x, y)
# Offset child hit point by scroll position/offset
child_x = x - @scroll_position.x
child_y = y - @scroll_position.y
@children.reverse_each do |child| @children.reverse_each do |child|
next unless child.visible? next unless child.visible?
case child case child
when Container when Container
if element = child.hit_element?(x, y) if element = child.hit_element?(child_x, child_y)
return element return element
end end
else else
return child if child.hit?(x, y) return child if child.hit?(child_x, child_y)
end end
end end
self if hit?(x, y) self if hit?(x, y)
end end
def update_child_element_visibity(child)
child.element_visible = child.x >= (@x - @scroll_position.x) - child.width && child.x <= (@x - @scroll_position.x) + width &&
child.y >= (@y - @scroll_position.y) - child.height && child.y <= (@y - @scroll_position.y) + height
end
def update_scroll
dt = window.dt > 1 ? 1.0 : window.dt
scroll_x_diff = (@scroll_target_position.x - @scroll_position.x)
scroll_y_diff = (@scroll_target_position.y - @scroll_position.y)
@scroll_position.x += (scroll_x_diff * @scroll_speed * 0.25 * dt).round
@scroll_position.y += (scroll_y_diff * @scroll_speed * 0.25 * dt).round
@scroll_position.x = @scroll_target_position.x if scroll_x_diff.abs < 1.0
@scroll_position.y = @scroll_target_position.y if scroll_y_diff.abs < 1.0
# Scrolled PAST top
if @scroll_position.y > 0
@scroll_target_position.y = 0
# Scrolled PAST bottom
elsif @scroll_position.y.abs > max_scroll_height
@scroll_target_position.y = -max_scroll_height
end
if @last_scroll_position != @scroll_position
@children.each { |child| update_child_element_visibity(child) }
root.gui_state.request_repaint
end
@last_scroll_position.x = @scroll_position.x
@last_scroll_position.y = @scroll_position.y
end
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?
@@ -129,6 +173,9 @@ module CyberarmEngine
old_width = @width old_width = @width
old_height = @height old_height = @height
@cached_scroll_width = nil
@cached_scroll_height = nil
if is_root? if is_root?
@width = @style.width = window.width @width = @style.width = window.width
@height = @style.height = window.height @height = @style.height = window.height
@@ -178,8 +225,7 @@ module CyberarmEngine
Stats.frame.increment(:gui_recalculations) Stats.frame.increment(:gui_recalculations)
child.element_visible = child.x >= @x - child.width && child.x <= @x + width && update_child_element_visibity(child)
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)}" # puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
@@ -188,8 +234,24 @@ module CyberarmEngine
# Fixes resized container scrolled past bottom # Fixes resized container scrolled past bottom
self.scroll_top = -@scroll_position.y self.scroll_top = -@scroll_position.y
@scroll_target_position.y = @scroll_position.y
# Fixes resized container that is scrolled down from being stuck overscrolled when resized
if scroll_height < height
@scroll_target_position.y = 0
end
# NOTE: Experiment for removing need to explicitly call gui_state#recalculate at least 3 times for layout to layout...
if old_width != @width || old_height != @height
if @parent
root.gui_state.request_recalculate_for(@parent)
else
root.gui_state.request_recalculate
end
end
root.gui_state.request_repaint if @width != old_width || @height != old_height root.gui_state.request_repaint if @width != old_width || @height != old_height
recalculate_if_size_changed
end end
def layout def layout
@@ -249,12 +311,13 @@ module CyberarmEngine
def mouse_wheel_up(sender, x, y) def mouse_wheel_up(sender, x, y)
return unless @style.scroll return unless @style.scroll
if @scroll_position.y < 0 # Allow overscrolling UP, only if one can scroll DOWN
@scroll_position.y += @scroll_speed if height < scroll_height
@scroll_position.y = 0 if @scroll_position.y > 0 if @scroll_target_position.y > 0
@scroll_target_position.y = @scroll_chunk
root.gui_state.request_recalculate_for(self) else
root.gui_state.request_repaint @scroll_target_position.y += @scroll_chunk
end
return :handled return :handled
end end
@@ -265,16 +328,14 @@ module CyberarmEngine
return unless height < scroll_height return unless height < scroll_height
if @scroll_position.y.abs < max_scroll_height if @scroll_target_position.y > 0
@scroll_position.y -= @scroll_speed @scroll_target_position.y = -@scroll_chunk
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height else
@scroll_target_position.y -= @scroll_chunk
root.gui_state.request_recalculate_for(self) end
root.gui_state.request_repaint
return :handled return :handled
end end
end
def scroll_top def scroll_top
@scroll_position.y @scroll_position.y

View File

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

View File

@@ -78,10 +78,17 @@ module CyberarmEngine
end end
def update def update
Stats.frame.start_timing(:gui_element_recalculate_requests)
# puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?
@pending_element_recalculate_requests.each(&:recalculate)
@pending_element_recalculate_requests.clear
Stats.frame.end_timing(:gui_element_recalculate_requests)
if @pending_recalculate_request if @pending_recalculate_request
Stats.frame.start_timing(:gui_recalculate) Stats.frame.start_timing(:gui_recalculate)
@root_container.recalculate
@root_container.recalculate
@root_container.recalculate @root_container.recalculate
@pending_recalculate_request = false @pending_recalculate_request = false
@@ -89,13 +96,6 @@ module CyberarmEngine
Stats.frame.end_timing(:gui_recalculate) Stats.frame.end_timing(:gui_recalculate)
end end
Stats.frame.start_timing(:gui_element_recalculate_requests)
@pending_element_recalculate_requests.each(&:recalculate)
@pending_element_recalculate_requests.clear
Stats.frame.end_timing(:gui_element_recalculate_requests)
if @pending_focus_request if @pending_focus_request
@pending_focus_request = false @pending_focus_request = false

View File

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