mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-17 21:42:34 +00:00
Compare commits
43 Commits
41c0b27937
...
v0.24.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d70177de7 | |||
| dcefaeae4d | |||
| c2330ed9a9 | |||
| 615d7ec3e4 | |||
| 1644ff8a27 | |||
| d6a99b935a | |||
| c4e47b8e38 | |||
| d0e1772f33 | |||
| 00e47d61a2 | |||
| 1882673fff | |||
| 286efa2059 | |||
| 6b9af5c87c | |||
| af3a3ff938 | |||
| bcff35bd7b | |||
| 5d1c195917 | |||
| 9a6e1df032 | |||
| b1b8fc8556 | |||
| a60b09a110 | |||
| 81a632942e | |||
| 5ef8023aca | |||
| 2e690d7d33 | |||
| a9f9e20235 | |||
| 25c36d3788 | |||
| c26ddeef4d | |||
| 5e3e06b74e | |||
| 72037efc73 | |||
| 1462f89e24 | |||
| 98948c891a | |||
| da16ab1ec9 | |||
| eb5d170733 | |||
| 14e9d4946f | |||
| e3b8a9b102 | |||
| 458731a534 | |||
| d1d87db070 | |||
| 6e8948bd81 | |||
| 01a9187a57 | |||
| 186ad220cc | |||
| f82c0953b2 | |||
| a2d44ea2dc | |||
| c46664778a | |||
| c597a67ca6 | |||
| 55382a7c14 | |||
| 883de3db9f |
30
assets/shaders/fragment/g_buffer.glsl
Normal file
30
assets/shaders/fragment/g_buffer.glsl
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# version 330 core
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 fragPosition;
|
||||||
|
layout (location = 1) out vec4 fragColor;
|
||||||
|
layout (location = 2) out vec3 fragNormal;
|
||||||
|
layout (location = 3) out vec3 fragUV;
|
||||||
|
|
||||||
|
in vec3 out_position, out_color, out_normal, out_uv, out_frag_pos, out_camera_pos;
|
||||||
|
out vec4 outputFragColor;
|
||||||
|
flat in int out_has_texture;
|
||||||
|
|
||||||
|
uniform sampler2D diffuse_texture;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec3 result;
|
||||||
|
|
||||||
|
if (out_has_texture == 0) {
|
||||||
|
result = out_color;
|
||||||
|
} else {
|
||||||
|
result = texture(diffuse_texture, out_uv.xy).xyz + 0.25;
|
||||||
|
}
|
||||||
|
|
||||||
|
fragPosition = out_position;
|
||||||
|
fragColor = vec4(result, 1.0);
|
||||||
|
fragNormal = out_normal;
|
||||||
|
fragUV = out_uv;
|
||||||
|
|
||||||
|
float gamma = 2.2;
|
||||||
|
outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma));
|
||||||
|
}
|
||||||
69
assets/shaders/fragment/lighting.glsl
Normal file
69
assets/shaders/fragment/lighting.glsl
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 frag_color;
|
||||||
|
|
||||||
|
@include "light_struct"
|
||||||
|
const int DIRECTIONAL = 0;
|
||||||
|
const int POINT = 1;
|
||||||
|
const int SPOT = 2;
|
||||||
|
|
||||||
|
flat in Light out_lights[7];
|
||||||
|
in vec2 out_tex_coords;
|
||||||
|
flat in int out_light_count;
|
||||||
|
|
||||||
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
|
vec4 directionalLight(Light light) {
|
||||||
|
vec3 norm = normalize(texture(normal, out_tex_coords).rgb);
|
||||||
|
vec3 diffuse_color = texture(diffuse, out_tex_coords).rgb;
|
||||||
|
vec3 frag_pos = texture(position, out_tex_coords).rgb;
|
||||||
|
|
||||||
|
vec3 lightDir = normalize(light.position - frag_pos);
|
||||||
|
float diff = max(dot(norm, lightDir), 0);
|
||||||
|
|
||||||
|
vec3 _ambient = light.ambient;
|
||||||
|
vec3 _diffuse = light.diffuse * diff;
|
||||||
|
vec3 _specular = light.specular;
|
||||||
|
|
||||||
|
return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec4 pointLight(Light light) {
|
||||||
|
return vec4(0.25, 0.25, 0.25, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec4 spotLight(Light light) {
|
||||||
|
return vec4(0.5, 0.5, 0.5, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec4 calculateLighting(Light light) {
|
||||||
|
vec4 result;
|
||||||
|
|
||||||
|
// switch(light.type) {
|
||||||
|
// case DIRECTIONAL: {
|
||||||
|
// result = directionalLight(light);
|
||||||
|
// }
|
||||||
|
// case SPOT: {
|
||||||
|
// result = spotLight(light);
|
||||||
|
// }
|
||||||
|
// default: {
|
||||||
|
// result = pointLight(light);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (light.type == DIRECTIONAL) {
|
||||||
|
result = directionalLight(light);
|
||||||
|
} else {
|
||||||
|
result = pointLight(light);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
assets/shaders/include/light_struct.glsl
Normal file
11
assets/shaders/include/light_struct.glsl
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
struct Light {
|
||||||
|
int type;
|
||||||
|
vec3 direction;
|
||||||
|
vec3 position;
|
||||||
|
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 specular;
|
||||||
|
|
||||||
|
float intensity;
|
||||||
|
};
|
||||||
16
assets/shaders/include/material_struct.glsl
Normal file
16
assets/shaders/include/material_struct.glsl
Normal 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;
|
||||||
|
};
|
||||||
28
assets/shaders/vertex/g_buffer.glsl
Normal file
28
assets/shaders/vertex/g_buffer.glsl
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# version 330 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 in_position;
|
||||||
|
layout(location = 1) in vec3 in_color;
|
||||||
|
layout(location = 2) in vec3 in_normal;
|
||||||
|
layout(location = 3) in vec3 in_uv;
|
||||||
|
|
||||||
|
uniform mat4 projection, view, model;
|
||||||
|
uniform int has_texture;
|
||||||
|
uniform vec3 camera_pos;
|
||||||
|
|
||||||
|
out vec3 out_position, out_color, out_normal, out_uv;
|
||||||
|
out vec3 out_frag_pos, out_view_pos, out_camera_pos;
|
||||||
|
flat out int out_has_texture;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
// projection * view * model * position
|
||||||
|
out_position = in_position;
|
||||||
|
out_color = in_color;
|
||||||
|
out_normal= normalize(transpose(inverse(mat3(model))) * in_normal);
|
||||||
|
out_uv = in_uv;
|
||||||
|
out_has_texture = has_texture;
|
||||||
|
out_camera_pos = camera_pos;
|
||||||
|
|
||||||
|
out_frag_pos = vec3(model * vec4(in_position, 1.0));
|
||||||
|
|
||||||
|
gl_Position = projection * view * model * vec4(in_position, 1.0);
|
||||||
|
}
|
||||||
24
assets/shaders/vertex/lighting.glsl
Normal file
24
assets/shaders/vertex/lighting.glsl
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#version 330 core
|
||||||
|
@include "light_struct"
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 in_position;
|
||||||
|
layout (location = 1) in vec2 in_tex_coords;
|
||||||
|
|
||||||
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
uniform int light_count;
|
||||||
|
uniform Light lights[7];
|
||||||
|
|
||||||
|
out vec2 out_tex_coords;
|
||||||
|
flat out int out_light_count;
|
||||||
|
flat out Light out_lights[7];
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(in_position.x, in_position.y, in_position.z, 1.0);
|
||||||
|
out_tex_coords = in_tex_coords;
|
||||||
|
out_light_count = light_count;
|
||||||
|
|
||||||
|
for(int i = 0; i < light_count; i++)
|
||||||
|
{
|
||||||
|
out_lights[i] = lights[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,9 +27,7 @@ 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 "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 "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 2.2"
|
spec.add_development_dependency "bundler", "~> 2.2"
|
||||||
|
|||||||
@@ -6,14 +6,19 @@ else
|
|||||||
require "gosu"
|
require "gosu"
|
||||||
end
|
end
|
||||||
require "json"
|
require "json"
|
||||||
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"
|
||||||
|
|
||||||
@@ -56,6 +61,8 @@ require_relative "cyberarm_engine/ui/elements/check_box"
|
|||||||
require_relative "cyberarm_engine/ui/elements/radio"
|
require_relative "cyberarm_engine/ui/elements/radio"
|
||||||
require_relative "cyberarm_engine/ui/elements/progress"
|
require_relative "cyberarm_engine/ui/elements/progress"
|
||||||
require_relative "cyberarm_engine/ui/elements/slider"
|
require_relative "cyberarm_engine/ui/elements/slider"
|
||||||
|
require_relative "cyberarm_engine/ui/elements/menu"
|
||||||
|
require_relative "cyberarm_engine/ui/elements/menu_item"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/game_state"
|
require_relative "cyberarm_engine/game_state"
|
||||||
require_relative "cyberarm_engine/ui/gui_state"
|
require_relative "cyberarm_engine/ui/gui_state"
|
||||||
@@ -66,6 +73,6 @@ require_relative "cyberarm_engine/model/material"
|
|||||||
require_relative "cyberarm_engine/model/model_object"
|
require_relative "cyberarm_engine/model/model_object"
|
||||||
require_relative "cyberarm_engine/model/parser"
|
require_relative "cyberarm_engine/model/parser"
|
||||||
require_relative "cyberarm_engine/model/parsers/wavefront_parser"
|
require_relative "cyberarm_engine/model/parsers/wavefront_parser"
|
||||||
require_relative "cyberarm_engine/model/parsers/collada_parser" if defined?(Nokogiri)
|
require_relative "cyberarm_engine/model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
||||||
|
|
||||||
require_relative "cyberarm_engine/builtin/intro_state"
|
require_relative "cyberarm_engine/builtin/intro_state"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def draw_fill
|
def draw_fill
|
||||||
if @width * width_scale > height * height_scale
|
if (@image.width * width_scale) >= @width && (@image.height * width_scale) >= @height
|
||||||
draw_fill_width
|
draw_fill_width
|
||||||
else
|
else
|
||||||
draw_fill_height
|
draw_fill_height
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ module CyberarmEngine
|
|||||||
@title_size = 56
|
@title_size = 56
|
||||||
@caption_size = 24
|
@caption_size = 24
|
||||||
|
|
||||||
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222)
|
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222, static: true)
|
||||||
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222)
|
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222, static: true)
|
||||||
|
|
||||||
@spacer_width = 256
|
@spacer_width = 256
|
||||||
@spacer_height = 6
|
@spacer_height = 6
|
||||||
@@ -18,7 +18,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@gosu_logo = generate_proxy("Gosu", "Game Library", 0xff_111111)
|
@gosu_logo = generate_proxy("Gosu", "Game Library", 0xff_111111)
|
||||||
@ruby_logo = generate_proxy("Ruby", "Programming Language", 0xff_880000)
|
@ruby_logo = generate_proxy("Ruby", "Programming Language", 0xff_880000)
|
||||||
@opengl_logo = generate_proxy("OpenGL", "Graphics API", 0xff_5586a4) if defined?(OpenGL)
|
@opengl_logo = generate_proxy("OpenGL", "Graphics API", 0xff_5586a4) if RUBY_ENGINE != "mruby" && defined?(OpenGL)
|
||||||
|
|
||||||
base_time = Gosu.milliseconds
|
base_time = Gosu.milliseconds
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
require "excon"
|
||||||
|
|
||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
module Cache
|
module Cache
|
||||||
class DownloadManager
|
class DownloadManager
|
||||||
|
|||||||
@@ -31,6 +31,18 @@ module CyberarmEngine
|
|||||||
window.show_cursor = boolean
|
window.show_cursor = boolean
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_element_by_tag(container, tag, list = [])
|
||||||
|
return unless container
|
||||||
|
|
||||||
|
container.children.each do |child|
|
||||||
|
list << child if child.style.tag == tag
|
||||||
|
|
||||||
|
find_element_by_tag(child, tag, list) if child.is_a?(CyberarmEngine::Element::Container)
|
||||||
|
end
|
||||||
|
|
||||||
|
list.first
|
||||||
|
end
|
||||||
|
|
||||||
def draw_rect(x, y, width, height, color, z = 0, mode = :default)
|
def draw_rect(x, y, width, height, color, z = 0, mode = :default)
|
||||||
Gosu.draw_rect(x, y, width, height, color, z, mode)
|
Gosu.draw_rect(x, y, width, height, color, z, mode)
|
||||||
end
|
end
|
||||||
@@ -40,7 +52,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lighten(color, amount = 25)
|
def lighten(color, amount = 25)
|
||||||
if defined?(color.alpha)
|
if color.respond_to?(:alpha)
|
||||||
Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
|
Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
|
||||||
else
|
else
|
||||||
Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
|
Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
|
||||||
@@ -48,7 +60,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def darken(color, amount = 25)
|
def darken(color, amount = 25)
|
||||||
if defined?(color.alpha)
|
if color.respond_to?(:alpha)
|
||||||
Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
|
Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
|
||||||
else
|
else
|
||||||
Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
|
Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
case id
|
case id
|
||||||
when Gosu::KbEnter, Gosu::KbReturn
|
when Gosu::KB_ENTER, Gosu::KB_RETURN
|
||||||
return unless @text_input.text.length.positive?
|
return unless @text_input.text.length.positive?
|
||||||
|
|
||||||
@history.text += "\n<c=999999>> #{@text_input.text}</c>"
|
@history.text += "\n<c=999999>> #{@text_input.text}</c>"
|
||||||
@@ -113,12 +113,12 @@ module CyberarmEngine
|
|||||||
handle_command
|
handle_command
|
||||||
@text_input.text = ""
|
@text_input.text = ""
|
||||||
|
|
||||||
when Gosu::KbUp
|
when Gosu::KB_UP
|
||||||
@command_history_index -= 1
|
@command_history_index -= 1
|
||||||
@command_history_index = 0 if @command_history_index.negative?
|
@command_history_index = 0 if @command_history_index.negative?
|
||||||
@text_input.text = @command_history[@command_history_index]
|
@text_input.text = @command_history[@command_history_index]
|
||||||
|
|
||||||
when Gosu::KbDown
|
when Gosu::KB_DOWN
|
||||||
@command_history_index += 1
|
@command_history_index += 1
|
||||||
if @command_history_index > @command_history.size - 1
|
if @command_history_index > @command_history.size - 1
|
||||||
@text_input.text = "" unless @command_history_index > @command_history.size
|
@text_input.text = "" unless @command_history_index > @command_history.size
|
||||||
@@ -127,7 +127,7 @@ module CyberarmEngine
|
|||||||
@text_input.text = @command_history[@command_history_index]
|
@text_input.text = @command_history[@command_history_index]
|
||||||
end
|
end
|
||||||
|
|
||||||
when Gosu::KbTab
|
when Gosu::KB_TAB
|
||||||
split = @text_input.text.split(" ")
|
split = @text_input.text.split(" ")
|
||||||
|
|
||||||
if !@text_input.text.end_with?(" ") && split.size == 1
|
if !@text_input.text.end_with?(" ") && split.size == 1
|
||||||
@@ -142,7 +142,7 @@ module CyberarmEngine
|
|||||||
cmd.autocomplete(self)
|
cmd.autocomplete(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
when Gosu::KbBacktick
|
when Gosu::KB_BACKTICK
|
||||||
# Remove backtick character from input
|
# Remove backtick character from input
|
||||||
@text_input.text = if @text_input.text.size > 1
|
@text_input.text = if @text_input.text.size > 1
|
||||||
@text_input.text[0..@text_input.text.size - 2]
|
@text_input.text[0..@text_input.text.size - 2]
|
||||||
@@ -151,7 +151,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Copy
|
# Copy
|
||||||
when Gosu::KbC
|
when Gosu::KB_C
|
||||||
if control_down? && shift_down?
|
if control_down? && shift_down?
|
||||||
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
||||||
p @memory
|
p @memory
|
||||||
@@ -160,7 +160,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Paste
|
# Paste
|
||||||
when Gosu::KbV
|
when Gosu::KB_V
|
||||||
if control_down? && shift_down?
|
if control_down? && shift_down?
|
||||||
string = @text_input.text.chars.insert(caret_pos, @memory).join
|
string = @text_input.text.chars.insert(caret_pos, @memory).join
|
||||||
_caret_pos = caret_pos
|
_caret_pos = caret_pos
|
||||||
@@ -170,7 +170,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Cut
|
# Cut
|
||||||
when Gosu::KbX
|
when Gosu::KB_X
|
||||||
if control_down? && shift_down?
|
if control_down? && shift_down?
|
||||||
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
||||||
string = @text_input.text.chars
|
string = @text_input.text.chars
|
||||||
@@ -182,7 +182,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Delete word to left of caret
|
# Delete word to left of caret
|
||||||
when Gosu::KbW
|
when Gosu::KB_W
|
||||||
if control_down?
|
if control_down?
|
||||||
split = @text_input.text.split(" ")
|
split = @text_input.text.split(" ")
|
||||||
split.delete(split.last)
|
split.delete(split.last)
|
||||||
@@ -190,7 +190,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Clear history
|
# Clear history
|
||||||
when Gosu::KbL
|
when Gosu::KB_L
|
||||||
@history.text = "" if control_down?
|
@history.text = "" if control_down?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ module CyberarmEngine
|
|||||||
@radius = if options[:radius]
|
@radius = if options[:radius]
|
||||||
options[:radius]
|
options[:radius]
|
||||||
else
|
else
|
||||||
defined?(@image.width) ? ((@image.width + @image.height) / 4) * scale : 1
|
@image.respond_to?(:width) ? ((@image.width + @image.height) / 4) * scale : 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ module CyberarmEngine
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def needs_repaint?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
def drop(filename)
|
def drop(filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
98
lib/cyberarm_engine/gosu_ext/draw_arc.rb
Normal file
98
lib/cyberarm_engine/gosu_ext/draw_arc.rb
Normal 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
|
||||||
31
lib/cyberarm_engine/gosu_ext/draw_circle.rb
Normal file
31
lib/cyberarm_engine/gosu_ext/draw_circle.rb
Normal 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
|
||||||
17
lib/cyberarm_engine/gosu_ext/draw_path.rb
Normal file
17
lib/cyberarm_engine/gosu_ext/draw_path.rb
Normal 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
|
||||||
@@ -49,8 +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"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(parser)
|
def parse(parser)
|
||||||
@@ -150,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
|
||||||
@@ -177,7 +178,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def build_collision_tree
|
def build_collision_tree
|
||||||
@aabb_tree = AABBTree.new
|
@aabb_tree = IMICFPS::AABBTree.new
|
||||||
|
|
||||||
@faces.each do |face|
|
@faces.each do |face|
|
||||||
box = BoundingBox.new
|
box = BoundingBox.new
|
||||||
|
|||||||
83
lib/cyberarm_engine/notification.rb
Normal file
83
lib/cyberarm_engine/notification.rb
Normal 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
|
||||||
242
lib/cyberarm_engine/notification_manager.rb
Normal file
242
lib/cyberarm_engine/notification_manager.rb
Normal 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
|
||||||
@@ -11,7 +11,19 @@ module CyberarmEngine
|
|||||||
if e != GL_NO_ERROR
|
if e != GL_NO_ERROR
|
||||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||||
warn " #{gluErrorString(e)} (#{e})\n"
|
warn " #{gluErrorString(e)} (#{e})\n"
|
||||||
exit if window.exit_on_opengl_error?
|
exit if Window.instance&.exit_on_opengl_error?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def preload_default_shaders
|
||||||
|
shaders = %w[g_buffer lighting]
|
||||||
|
shaders.each do |shader|
|
||||||
|
Shader.new(
|
||||||
|
name: shader,
|
||||||
|
includes_dir: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/include",
|
||||||
|
vertex: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/vertex/#{shader}.glsl",
|
||||||
|
fragment: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/fragment/#{shader}.glsl"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class GBuffer
|
class GBuffer
|
||||||
attr_reader :screen_vbo, :vertices, :uvs
|
attr_reader :screen_vbo, :vertices, :uvs
|
||||||
|
attr_reader :width, :height
|
||||||
|
|
||||||
def initialize(width:, height:)
|
def initialize(width:, height:)
|
||||||
@width = width
|
@width = width
|
||||||
|
|||||||
@@ -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,15 +154,21 @@ 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)
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
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)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
glBindVertexArray(0)
|
glBindVertexArray(0)
|
||||||
@@ -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)
|
||||||
|
|||||||
@@ -8,8 +8,19 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def draw(camera, lights, entities)
|
def draw(camera, lights, entities)
|
||||||
|
Stats.frame.start_timing(:opengl_renderer)
|
||||||
|
|
||||||
|
Stats.frame.start_timing(:opengl_model_renderer)
|
||||||
@opengl_renderer.render(camera, lights, entities)
|
@opengl_renderer.render(camera, lights, entities)
|
||||||
@bounding_box_renderer.render(entities) if @show_bounding_boxes
|
Stats.frame.end_timing(:opengl_model_renderer)
|
||||||
|
|
||||||
|
if @show_bounding_boxes
|
||||||
|
Stats.frame.start_timing(:opengl_boundingbox_renderer)
|
||||||
|
@bounding_box_renderer.render(entities)
|
||||||
|
Stats.frame.end_timing(:opengl_boundingbox_renderer)
|
||||||
|
end
|
||||||
|
|
||||||
|
Stats.frame.end_timing(:opengl_renderer)
|
||||||
end
|
end
|
||||||
|
|
||||||
def canvas_size_changed
|
def canvas_size_changed
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -1,20 +1,199 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Stats
|
class Stats
|
||||||
@@hash = {
|
@frames = []
|
||||||
gui_recalculations_last_frame: 0
|
@frame_index = -1
|
||||||
}
|
@max_frame_history = 1024
|
||||||
|
|
||||||
def self.get(key)
|
def self.new_frame
|
||||||
@@hash.dig(key)
|
if @frames.size < @max_frame_history
|
||||||
|
@frames << Frame.new
|
||||||
|
else
|
||||||
|
@frames[@frame_index] = Frame.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.increment(key, n)
|
def self.frame
|
||||||
@@hash[key] += n
|
@frames[@frame_index]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.clear
|
def self.end_frame
|
||||||
@@hash.each do |key, _value|
|
frame&.complete
|
||||||
@@hash[key] = 0
|
|
||||||
|
@frame_index += 1
|
||||||
|
@frame_index %= @max_frame_history
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.frames
|
||||||
|
if @frames.size < @max_frame_history
|
||||||
|
@frames
|
||||||
|
else
|
||||||
|
@frames.rotate(@frame_index - (@max_frame_history - (@frames.size - 1)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.frame_index
|
||||||
|
@frame_index
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.max_frame_history
|
||||||
|
@max_frame_history
|
||||||
|
end
|
||||||
|
|
||||||
|
class Frame
|
||||||
|
Timing = Struct.new(:start_time, :end_time, :duration)
|
||||||
|
|
||||||
|
attr_reader :frame_timing, :counters, :timings, :multitimings
|
||||||
|
def initialize
|
||||||
|
@frame_timing = Timing.new(Gosu.milliseconds, -1, -1)
|
||||||
|
@attempted_multitiming = false
|
||||||
|
|
||||||
|
@counters = {
|
||||||
|
gui_recalculations: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@timings = {}
|
||||||
|
@multitimings = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def increment(key, number = 1)
|
||||||
|
@counters[key] ||= 0
|
||||||
|
@counters[key] += number
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_timing(key)
|
||||||
|
raise "key must be a symbol!" unless key.is_a?(Symbol)
|
||||||
|
if @timings[key]
|
||||||
|
# FIXME: Make it not spammy...
|
||||||
|
# warn "Only one timing per key per frame. (Timing for #{key.inspect} already exists!)"
|
||||||
|
@attempted_multitiming = true
|
||||||
|
@multitimings[key] = true
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
@timings[key] = Timing.new(Gosu.milliseconds, -1, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def end_timing(key)
|
||||||
|
timing = @timings[key]
|
||||||
|
|
||||||
|
# FIXME: Make it not spammy...
|
||||||
|
# warn "Timing #{key.inspect} already ended!" if timing.end_time != -1
|
||||||
|
|
||||||
|
timing.end_time = Gosu.milliseconds
|
||||||
|
timing.duration = timing.end_time - timing.start_time
|
||||||
|
end
|
||||||
|
|
||||||
|
def complete
|
||||||
|
@frame_timing.end_time = Gosu.milliseconds
|
||||||
|
@frame_timing.duration = @frame_timing.end_time - @frame_timing.start_time
|
||||||
|
|
||||||
|
# Lock data structures
|
||||||
|
@frame_timing.freeze
|
||||||
|
@counters.freeze
|
||||||
|
@timings.freeze
|
||||||
|
@multitimings.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
def complete?
|
||||||
|
@frame_timing.duration != -1
|
||||||
|
end
|
||||||
|
|
||||||
|
def attempted_multitiming?
|
||||||
|
@attempted_multitiming
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class StatsPlotter
|
||||||
|
attr_reader :position
|
||||||
|
|
||||||
|
def initialize(x, y, z = Float::INFINITY, width = 128, height = 128)
|
||||||
|
@position = Vector.new(x, y, z)
|
||||||
|
@width = width
|
||||||
|
@height = height
|
||||||
|
|
||||||
|
@padding = 2
|
||||||
|
@text_size = 16
|
||||||
|
|
||||||
|
@max_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @padding, z: z, size: @text_size, border: true, static: true)
|
||||||
|
@avg_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @padding + @height / 2 - @text_size / 2, z: z, size: @text_size, border: true, static: true)
|
||||||
|
@min_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @height - (@text_size + @padding / 2), z: z, size: @text_size, border: true, static: true)
|
||||||
|
|
||||||
|
@data_label = CyberarmEngine::Text.new("", x: x + @padding + @width + @padding, y: y + @padding, z: z, size: @text_size, border: true, static: true)
|
||||||
|
|
||||||
|
@frame_stats = []
|
||||||
|
@graphs = {
|
||||||
|
frame_timings: []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def calculate_graphs
|
||||||
|
calculate_frame_timings_graph
|
||||||
|
end
|
||||||
|
|
||||||
|
def calculate_frame_timings_graph
|
||||||
|
@graphs[:frame_timings].clear
|
||||||
|
|
||||||
|
samples = @width - @padding
|
||||||
|
nodes = Array.new(samples.ceil) { [] }
|
||||||
|
|
||||||
|
slice = 0
|
||||||
|
@frame_stats.each_slice((CyberarmEngine::Stats.max_frame_history / samples.to_f).ceil) do |bucket|
|
||||||
|
bucket.each do |frame|
|
||||||
|
nodes[slice] << frame.frame_timing.duration
|
||||||
|
end
|
||||||
|
|
||||||
|
slice += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
max_node = CyberarmEngine::Stats.frames.select(&:complete?).map { |f| f.frame_timing.duration }.max
|
||||||
|
scale = 1
|
||||||
|
scale = (@height - @padding).to_f / max_node
|
||||||
|
scale = 1 if scale > 1
|
||||||
|
|
||||||
|
nodes.each_with_index do |cluster, i|
|
||||||
|
break if cluster.empty?
|
||||||
|
|
||||||
|
@graphs[:frame_timings] << CyberarmEngine::Vector.new(@position.x + @padding + 1 * i, (@position.y + @height - @padding) - cluster.max * scale)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
@frame_stats = CyberarmEngine::Stats.frames.select(&:complete?)
|
||||||
|
return if @frame_stats.empty?
|
||||||
|
|
||||||
|
calculate_graphs
|
||||||
|
|
||||||
|
@max_timing_label.text = "<c=d44>Max:</c> #{@frame_stats.map { |f| f.frame_timing.duration }.max.to_s.rjust(3, " ")}ms"
|
||||||
|
@avg_timing_label.text = "<c=f80>Avg:</c> #{(@frame_stats.map { |f| f.frame_timing.duration }.sum / @frame_stats.size).to_s.rjust(3, " ")}ms"
|
||||||
|
@min_timing_label.text = "<c=0d0>Min:</c> #{@frame_stats.map { |f| f.frame_timing.duration }.min.to_s.rjust(3, " ")}ms"
|
||||||
|
|
||||||
|
Gosu.draw_rect(@position.x, @position.y, @width, @height, 0xaa_222222, @position.z)
|
||||||
|
Gosu.draw_rect(@position.x + @padding, @position.y + @padding, @width - @padding * 2, @height - @padding * 2, 0xaa_222222, @position.z)
|
||||||
|
|
||||||
|
draw_graphs
|
||||||
|
|
||||||
|
@max_timing_label.draw
|
||||||
|
@avg_timing_label.draw
|
||||||
|
@min_timing_label.draw
|
||||||
|
|
||||||
|
# TODO: Make this optional
|
||||||
|
draw_timings_and_counters
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_graphs
|
||||||
|
Gosu.draw_path(@graphs[:frame_timings], Gosu::Color::WHITE, Float::INFINITY)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_timings_and_counters
|
||||||
|
frame = @frame_stats.last
|
||||||
|
|
||||||
|
@data_label.text = "<c=f8f>COUNTERS:</c>\n#{frame.counters.map { |t, v| "#{t}: #{v}" }.join("\n")}\n\n"\
|
||||||
|
"<c=f80>TIMINGS:</c>\n#{frame.attempted_multitiming? ? "<c=d00>Attempted Multitiming!\nTimings may be inaccurate for:\n#{frame.multitimings.map { |m, _| m}.join("\n") }</c>\n\n" : ''}#{frame.timings.map { |t, v| "#{t}: #{v.duration}ms" }.join("\n")}"
|
||||||
|
Gosu.draw_rect(@data_label.x - @padding, @data_label.y - @padding, @data_label.width + @padding * 2, @data_label.height + @padding * 2, 0xdd_222222, @position.z)
|
||||||
|
@data_label.draw
|
||||||
|
|
||||||
|
# puts "Recalcs this frame: #{frame.counters[:gui_recalculations]} [dt: #{(CyberarmEngine::Window.dt * 1000).round} ms]" if frame.counters[:gui_recalculations] && frame.counters[:gui_recalculations].positive?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ module CyberarmEngine
|
|||||||
@size = size
|
@size = size
|
||||||
@font = font_name
|
@font = font_name
|
||||||
|
|
||||||
|
invalidate_cache!
|
||||||
@textobject = check_cache(size, font_name)
|
@textobject = check_cache(size, font_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -149,6 +150,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def markup_width(text = @text)
|
def markup_width(text = @text)
|
||||||
|
text = text.to_s
|
||||||
|
|
||||||
spacing = 0
|
spacing = 0
|
||||||
spacing += @border_size if @border
|
spacing += @border_size if @border
|
||||||
spacing += @shadow_size if @shadow
|
spacing += @shadow_size if @shadow
|
||||||
|
|||||||
@@ -62,11 +62,11 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
# TOP
|
# TOP
|
||||||
@top.x = @element.x # + @element.border_thickness_left
|
@top.x = @element.x + @element.style.border_thickness_left
|
||||||
@top.y = @element.y
|
@top.y = @element.y
|
||||||
@top.z = @element.z
|
@top.z = @element.z
|
||||||
|
|
||||||
@top.width = @element.width
|
@top.width = @element.width - @element.style.border_thickness_left
|
||||||
@top.height = @element.style.border_thickness_top
|
@top.height = @element.style.border_thickness_top
|
||||||
|
|
||||||
# RIGHT
|
# RIGHT
|
||||||
|
|||||||
@@ -8,12 +8,8 @@ module CyberarmEngine
|
|||||||
container(CyberarmEngine::Element::Stack, options, &block)
|
container(CyberarmEngine::Element::Stack, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Remove in version 0.16.0+
|
def menu(options = {}, &block)
|
||||||
def label(text, options = {}, &block)
|
container(CyberarmEngine::Element::Menu, options, &block)
|
||||||
options[:parent] = element_parent
|
|
||||||
options[:theme] = current_theme
|
|
||||||
|
|
||||||
add_element(Element::TextBlock.new(text, options, block))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
[
|
[
|
||||||
@@ -27,72 +23,79 @@ module CyberarmEngine
|
|||||||
"Link"
|
"Link"
|
||||||
].each do |const|
|
].each do |const|
|
||||||
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element.const_get(const).new(text, options, block))
|
add_element(Element.const_get(const).new(text, options, block))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def button(text, options = {}, &block)
|
def button(text, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) })
|
add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) })
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_box(options = {}, &block)
|
def list_box(options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) })
|
add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def menu_item(text, options = {}, &block)
|
||||||
|
options[:parent] ||= element_parent
|
||||||
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
|
add_element(Element::MenuItem.new(text, options, block) { block.call if block.is_a?(Proc) })
|
||||||
|
end
|
||||||
|
|
||||||
def edit_line(text, options = {}, &block)
|
def edit_line(text, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::EditLine.new(text, options, block))
|
add_element(Element::EditLine.new(text, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit_box(text, options = {}, &block)
|
def edit_box(text, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::EditBox.new(text, options, block))
|
add_element(Element::EditBox.new(text, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle_button(options = {}, &block)
|
def toggle_button(options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::ToggleButton.new(options, block))
|
add_element(Element::ToggleButton.new(options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_box(text, options = {}, &block)
|
def check_box(text, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::CheckBox.new(text, options, block))
|
add_element(Element::CheckBox.new(text, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def image(path, options = {}, &block)
|
def image(path, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::Image.new(path, options, block))
|
add_element(Element::Image.new(path, options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def progress(options = {}, &block)
|
def progress(options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::Progress.new(options, block))
|
add_element(Element::Progress.new(options, block))
|
||||||
end
|
end
|
||||||
|
|
||||||
def slider(options = {}, &block)
|
def slider(options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
add_element(Element::Slider.new(options, block))
|
add_element(Element::Slider.new(options, block))
|
||||||
end
|
end
|
||||||
@@ -102,7 +105,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def theme(theme)
|
def theme(theme)
|
||||||
element_parent.options[:theme] = theme
|
element_parent.options[:theme] ||= theme
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_theme
|
def current_theme
|
||||||
@@ -120,8 +123,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
private def container(klass, options = {}, &block)
|
private def container(klass, options = {}, &block)
|
||||||
options[:parent] = element_parent
|
options[:parent] ||= element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] ||= current_theme
|
||||||
|
|
||||||
_container = klass.new(options, block)
|
_container = klass.new(options, block)
|
||||||
|
|
||||||
@@ -129,7 +132,7 @@ module CyberarmEngine
|
|||||||
CyberarmEngine::Element::Container.current_container = _container
|
CyberarmEngine::Element::Container.current_container = _container
|
||||||
|
|
||||||
_container.build
|
_container.build
|
||||||
_container.parent.add(_container)
|
_container.parent.add(_container) unless _container.is_a?(CyberarmEngine::Element::Menu)
|
||||||
|
|
||||||
CyberarmEngine::Element::Container.current_container = old_parent
|
CyberarmEngine::Element::Container.current_container = old_parent
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -65,10 +67,23 @@ module CyberarmEngine
|
|||||||
|
|
||||||
set_border_thickness
|
set_border_thickness
|
||||||
set_border_color
|
set_border_color
|
||||||
|
|
||||||
|
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
|
||||||
@@ -100,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
|
||||||
@@ -117,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,
|
||||||
@@ -142,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)
|
||||||
@@ -165,11 +180,9 @@ module CyberarmEngine
|
|||||||
|
|
||||||
return if self.is_a?(ToolTip)
|
return if self.is_a?(ToolTip)
|
||||||
|
|
||||||
if old_width != width || old_height != height
|
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||||
(root&.gui_state || @gui_state).request_recalculate
|
|
||||||
else
|
stylize
|
||||||
stylize
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_events
|
def default_events
|
||||||
@@ -182,6 +195,10 @@ module CyberarmEngine
|
|||||||
|
|
||||||
event(:mouse_wheel_up)
|
event(:mouse_wheel_up)
|
||||||
event(:mouse_wheel_down)
|
event(:mouse_wheel_down)
|
||||||
|
event(:scroll_jump_to_top)
|
||||||
|
event(:scroll_jump_to_end)
|
||||||
|
event(:scroll_page_up)
|
||||||
|
event(:scroll_page_down)
|
||||||
|
|
||||||
event(:enter)
|
event(:enter)
|
||||||
event(:hover)
|
event(:hover)
|
||||||
@@ -194,7 +211,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def enter(_sender)
|
def enter(_sender)
|
||||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
@focus = false unless Gosu.button_down?(Gosu::MS_LEFT)
|
||||||
|
|
||||||
if !@enabled
|
if !@enabled
|
||||||
update_styles(:disabled)
|
update_styles(:disabled)
|
||||||
@@ -256,6 +273,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def enabled=(boolean)
|
def enabled=(boolean)
|
||||||
|
root.gui_state.request_repaint if @enabled != boolean
|
||||||
|
|
||||||
@enabled = boolean
|
@enabled = boolean
|
||||||
|
|
||||||
recalculate
|
recalculate
|
||||||
@@ -267,6 +286,10 @@ module CyberarmEngine
|
|||||||
@enabled
|
@enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def focused?
|
||||||
|
@focus
|
||||||
|
end
|
||||||
|
|
||||||
def visible?
|
def visible?
|
||||||
@visible
|
@visible
|
||||||
end
|
end
|
||||||
@@ -278,18 +301,21 @@ module CyberarmEngine
|
|||||||
def toggle
|
def toggle
|
||||||
@visible = !@visible
|
@visible = !@visible
|
||||||
root.gui_state.request_recalculate
|
root.gui_state.request_recalculate
|
||||||
|
root.gui_state.request_repaint
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
bool = visible?
|
bool = visible?
|
||||||
@visible = true
|
@visible = true
|
||||||
root.gui_state.request_recalculate unless bool
|
root.gui_state.request_recalculate unless bool
|
||||||
|
root.gui_state.request_repaint unless bool
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide
|
def hide
|
||||||
bool = visible?
|
bool = visible?
|
||||||
@visible = false
|
@visible = false
|
||||||
root.gui_state.request_recalculate if bool
|
root.gui_state.request_recalculate if bool
|
||||||
|
root.gui_state.request_repaint if bool
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@@ -305,7 +331,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def debug_draw
|
def debug_draw
|
||||||
return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
|
return if CyberarmEngine.const_defined?("GUI_DEBUG_ONLY_ELEMENT") && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||||
|
|
||||||
Gosu.draw_line(
|
Gosu.draw_line(
|
||||||
x, y, @debug_color,
|
x, y, @debug_color,
|
||||||
@@ -323,13 +349,14 @@ module CyberarmEngine
|
|||||||
Float::INFINITY
|
Float::INFINITY
|
||||||
)
|
)
|
||||||
Gosu.draw_line(
|
Gosu.draw_line(
|
||||||
x, outer_height, @debug_color,
|
x, y + outer_height, @debug_color,
|
||||||
x, y, @debug_color,
|
x, y, @debug_color,
|
||||||
Float::INFINITY
|
Float::INFINITY
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
recalculate_if_size_changed
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
@@ -399,10 +426,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?
|
||||||
|
|
||||||
@@ -423,41 +454,39 @@ module CyberarmEngine
|
|||||||
|
|
||||||
pairs_ << a_ unless pairs_.last == a_
|
pairs_ << a_ unless pairs_.last == a_
|
||||||
|
|
||||||
pairs_.sum { |pair| 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
|
||||||
@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)
|
||||||
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)
|
||||||
|
|
||||||
new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0)
|
new_size = if size.is_a?(Float) && size.between?(0.0, 1.0)
|
||||||
(@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
|
(@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
|
||||||
else
|
else
|
||||||
size
|
size
|
||||||
|
end
|
||||||
|
|
||||||
|
# Handle fill behavior
|
||||||
|
if @parent && @style.fill &&
|
||||||
|
(dimension == :width && @parent.is_a?(Flow) ||
|
||||||
|
dimension == :height && @parent.is_a?(Stack))
|
||||||
|
new_size = space_available_width - noncontent_width if dimension == :width && @parent.is_a?(Flow)
|
||||||
|
new_size = space_available_height - noncontent_height if dimension == :height && @parent.is_a?(Stack)
|
||||||
end
|
end
|
||||||
|
|
||||||
if @parent && @style.fill # Handle fill behavior
|
return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}")
|
||||||
if dimension == :width && @parent.is_a?(Flow)
|
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}")
|
||||||
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 < @style.send(:"min_#{dimension}")
|
|
||||||
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size > @style.send(:"max_#{dimension}")
|
|
||||||
end
|
|
||||||
|
|
||||||
new_size
|
new_size
|
||||||
end
|
end
|
||||||
@@ -479,6 +508,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def background=(_background)
|
def background=(_background)
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
@style.background_canvas.background = _background
|
@style.background_canvas.background = _background
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
@@ -497,6 +528,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def background_nine_slice=(_image_path)
|
def background_nine_slice=(_image_path)
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
@style.background_nine_slice_canvas.image = _image_path
|
@style.background_nine_slice_canvas.image = _image_path
|
||||||
update_background_nine_slice
|
update_background_nine_slice
|
||||||
end
|
end
|
||||||
@@ -521,6 +554,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def background_image=(image_path)
|
def background_image=(image_path)
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
@style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
|
@style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
|
||||||
update_background_image
|
update_background_image
|
||||||
end
|
end
|
||||||
@@ -538,6 +573,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 @parent && !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?
|
||||||
|
|
||||||
@@ -558,6 +602,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!"
|
||||||
|
|
||||||
@@ -565,7 +625,19 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
raise "#{self.class}#recalculate was not overridden!"
|
old_width = width
|
||||||
|
old_height = height
|
||||||
|
|
||||||
|
stylize
|
||||||
|
layout
|
||||||
|
|
||||||
|
root.gui_state.request_recalculate if @parent && !is_a?(ToolTip) && (width != old_width || height != old_height)
|
||||||
|
root.gui_state.request_repaint if width != old_width || height != old_height
|
||||||
|
|
||||||
|
root.gui_state.menu.recalculate if root.gui_state.menu && root.gui_state.menu.parent == self
|
||||||
|
end
|
||||||
|
|
||||||
|
def layout
|
||||||
end
|
end
|
||||||
|
|
||||||
def reposition
|
def reposition
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ module CyberarmEngine
|
|||||||
@text.draw
|
@text.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def layout
|
||||||
unless @enabled
|
unless @enabled
|
||||||
@style.background_canvas.background = @style.disabled[:background]
|
@style.background_canvas.background = @style.disabled[:background]
|
||||||
@text.color = @style.disabled[:color]
|
@text.color = @style.disabled[:color]
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
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, :scroll_target_position
|
||||||
|
|
||||||
def self.current_container
|
def self.current_container
|
||||||
@@current_container
|
@@current_container
|
||||||
@@ -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,13 +36,28 @@ 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
|
||||||
|
|
||||||
|
def remove(element)
|
||||||
|
root.gui_state.request_recalculate_for(self) if @children.delete(element)
|
||||||
|
end
|
||||||
|
|
||||||
|
def append(&block)
|
||||||
|
old_container = CyberarmEngine::Element::Container.current_container
|
||||||
|
|
||||||
|
CyberarmEngine::Element::Container.current_container = self
|
||||||
|
block.call(self) if block
|
||||||
|
|
||||||
|
CyberarmEngine::Element::Container.current_container = old_container
|
||||||
|
|
||||||
|
root.gui_state.request_recalculate_for(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear(&block)
|
def clear(&block)
|
||||||
@@ -52,18 +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
|
|
||||||
|
|
||||||
def append(&block)
|
|
||||||
old_container = CyberarmEngine::Element::Container.current_container
|
|
||||||
|
|
||||||
CyberarmEngine::Element::Container.current_container = self
|
|
||||||
block.call(self) if block
|
|
||||||
|
|
||||||
CyberarmEngine::Element::Container.current_container = old_container
|
|
||||||
|
|
||||||
root.gui_state.request_recalculate
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
@@ -73,7 +80,9 @@ module CyberarmEngine
|
|||||||
content_width + 1,
|
content_width + 1,
|
||||||
content_height + 1
|
content_height + 1
|
||||||
) do
|
) do
|
||||||
@children.each(&:draw)
|
Gosu.translate(@scroll_position.x, @scroll_position.y) do
|
||||||
|
@children.each(&:draw)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -86,42 +95,84 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
update_scroll if @style.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.clamp(0.000001, 0.025)
|
||||||
|
@scroll_position.x += (((@scroll_target_position.x - @scroll_position.x) * (@scroll_speed / 4.0) * 0.98) * dt).round
|
||||||
|
@scroll_position.y += (((@scroll_target_position.y - @scroll_position.y) * (@scroll_speed / 4.0) * 0.98) * dt).round
|
||||||
|
|
||||||
|
# Scrolled PAST top
|
||||||
|
if @scroll_position.y > 0
|
||||||
|
@scroll_target_position.y = 0
|
||||||
|
|
||||||
|
# Scrolled PAST bottom
|
||||||
|
elsif @scroll_position.y < -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
|
||||||
|
return if @in_recalculate
|
||||||
|
|
||||||
|
@in_recalculate = true
|
||||||
|
|
||||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||||
@current_position += @scroll_position
|
|
||||||
|
|
||||||
return unless visible?
|
return unless visible?
|
||||||
|
|
||||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
Stats.frame&.increment(:gui_recalculations)
|
||||||
|
|
||||||
stylize
|
|
||||||
|
|
||||||
# s = Gosu.milliseconds
|
# s = Gosu.milliseconds
|
||||||
|
|
||||||
|
stylize
|
||||||
layout
|
layout
|
||||||
|
|
||||||
|
# Old sizes MUST be determined AFTER call to layout
|
||||||
|
old_width = width
|
||||||
|
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
|
||||||
@@ -160,6 +211,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# t = Gosu.milliseconds
|
||||||
# Move children to parent after positioning
|
# 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
|
||||||
@@ -169,15 +221,30 @@ module CyberarmEngine
|
|||||||
child.recalculate
|
child.recalculate
|
||||||
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.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 - t}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}'s #{@children.count} children"
|
||||||
|
|
||||||
|
update_background
|
||||||
|
|
||||||
|
# Fixes resized container scrolled past bottom
|
||||||
|
if old_height != @height
|
||||||
|
self.scroll_top = -@scroll_position.y
|
||||||
|
@scroll_target_position.y = @scroll_position.y
|
||||||
|
end
|
||||||
|
|
||||||
|
# Fixes resized container that is scrolled down from being stuck overscrolled when resized
|
||||||
|
if scroll_height < height
|
||||||
|
@scroll_target_position.y = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
recalculate_if_size_changed
|
||||||
|
|
||||||
# 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)}"
|
||||||
|
|
||||||
update_background
|
@in_recalculate = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def layout
|
def layout
|
||||||
@@ -237,11 +304,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
|
||||||
|
@scroll_target_position.y += @scroll_chunk
|
||||||
|
end
|
||||||
|
|
||||||
return :handled
|
return :handled
|
||||||
end
|
end
|
||||||
@@ -252,14 +321,51 @@ 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)
|
|
||||||
|
|
||||||
return :handled
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def scroll_jump_to_top(sender, x, y)
|
||||||
|
return unless @style.scroll
|
||||||
|
|
||||||
|
@scroll_position.y = 0
|
||||||
|
@scroll_target_position.y = 0
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def scroll_jump_to_end(sender, x, y)
|
||||||
|
return unless @style.scroll
|
||||||
|
|
||||||
|
@scroll_position.y = -max_scroll_height
|
||||||
|
@scroll_target_position.y = -max_scroll_height
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def scroll_page_up(sender, x, y)
|
||||||
|
return unless @style.scroll
|
||||||
|
|
||||||
|
@scroll_position.y += height
|
||||||
|
@scroll_position.y = 0 if @scroll_position.y > 0
|
||||||
|
@scroll_target_position.y = @scroll_position.y
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def scroll_page_down(sender, x, y)
|
||||||
|
return unless @style.scroll
|
||||||
|
|
||||||
|
@scroll_position.y -= height
|
||||||
|
@scroll_position.y = -max_scroll_height if @scroll_position.y < -max_scroll_height
|
||||||
|
@scroll_target_position.y = @scroll_position.y
|
||||||
|
|
||||||
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def scroll_top
|
def scroll_top
|
||||||
@@ -278,7 +384,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
@children.map { |c| c.class }.join(", ")
|
@children.map(&:class).join(", ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ module CyberarmEngine
|
|||||||
@text_input.filter = @filter
|
@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
|
||||||
|
|
||||||
@offset_x = 0
|
@offset_x = 0
|
||||||
@offset_y = 0
|
@offset_y = 0
|
||||||
@@ -79,10 +80,22 @@ module CyberarmEngine
|
|||||||
@show_caret = true
|
@show_caret = true
|
||||||
@caret_last_interval = Gosu.milliseconds
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
publish(:changed, value)
|
publish(:changed, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @last_caret_position != @text_input.caret_pos
|
||||||
|
@last_caret_position = @text_input.caret_pos
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
|
@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
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
@caret_last_interval = Gosu.milliseconds
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
|
||||||
@show_caret = !@show_caret
|
@show_caret = !@show_caret
|
||||||
@@ -203,20 +216,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
|
||||||
@@ -250,7 +278,7 @@ module CyberarmEngine
|
|||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def layout
|
||||||
super
|
super
|
||||||
|
|
||||||
@width = dimensional_size(@style.width, :width) || default(:width)
|
@width = dimensional_size(@style.width, :width) || default(:width)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ module CyberarmEngine
|
|||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def layout
|
||||||
_width = dimensional_size(@style.width, :width)
|
_width = dimensional_size(@style.width, :width)
|
||||||
_height = dimensional_size(@style.height, :height)
|
_height = dimensional_size(@style.height, :height)
|
||||||
|
|
||||||
@@ -45,8 +45,8 @@ module CyberarmEngine
|
|||||||
@scale_y = 1
|
@scale_y = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@width = _width || @image.width.floor * @scale_x
|
@width = _width || (@image.width * @scale_x).floor
|
||||||
@height = _height || @image.height.floor * @scale_y
|
@height = _height || (@image.height * @scale_y).floor
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,21 +12,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@style.background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
|
|
||||||
# TODO: "Clean Up" into own class?
|
@menu = Menu.new(parent: self, theme: @options[:theme])
|
||||||
@menu = Stack.new(parent: self, theme: @options[:theme])
|
|
||||||
@menu.define_singleton_method(:recalculate_menu) do
|
|
||||||
@x = @__list_box.x
|
|
||||||
@y = @__list_box.y + @__list_box.height
|
|
||||||
|
|
||||||
@y = @__list_box.y - height if @y + height > window.height
|
|
||||||
end
|
|
||||||
@menu.instance_variable_set(:"@__list_box", self)
|
|
||||||
|
|
||||||
def @menu.recalculate
|
|
||||||
super
|
|
||||||
|
|
||||||
recalculate_menu
|
|
||||||
end
|
|
||||||
|
|
||||||
self.choose = @choose
|
self.choose = @choose
|
||||||
end
|
end
|
||||||
@@ -40,7 +26,13 @@ module CyberarmEngine
|
|||||||
|
|
||||||
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
|
|
||||||
|
unless valid
|
||||||
|
warn "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}"
|
||||||
|
item = @items.first
|
||||||
|
|
||||||
|
raise "No items list" unless item
|
||||||
|
end
|
||||||
|
|
||||||
@choose = item
|
@choose = item
|
||||||
|
|
||||||
@@ -62,39 +54,25 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show_menu
|
def show_menu
|
||||||
@menu.clear
|
@menu.clear do
|
||||||
|
|
||||||
@menu.style.width = width
|
@menu.style.width = width
|
||||||
|
|
||||||
@items.each do |item|
|
@items.each do |item|
|
||||||
next if item == self.value
|
# prevent already selected item from appearing in list
|
||||||
|
# NOTE: Remove this? Might be kinda confusing...
|
||||||
|
next if item == self.value
|
||||||
|
|
||||||
btn = Button.new(
|
root.gui_state.menu_item(item, width: 1.0, margin: 0, border_color: 0x00ffffff) do
|
||||||
item,
|
|
||||||
{
|
|
||||||
parent: @menu,
|
|
||||||
width: 1.0,
|
|
||||||
theme: @options[:theme],
|
|
||||||
margin: 0,
|
|
||||||
border_color: 0x00ffffff
|
|
||||||
},
|
|
||||||
proc do
|
|
||||||
self.choose = item
|
self.choose = item
|
||||||
@block&.call(self.value)
|
@block&.call(self.value)
|
||||||
end
|
end
|
||||||
)
|
end
|
||||||
|
|
||||||
@menu.add(btn)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
recalculate
|
recalculate
|
||||||
|
|
||||||
root.gui_state.show_menu(@menu)
|
@menu.show
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
super
|
|
||||||
|
|
||||||
@menu.recalculate
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
27
lib/cyberarm_engine/ui/elements/menu.rb
Normal file
27
lib/cyberarm_engine/ui/elements/menu.rb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Menu < Stack
|
||||||
|
def recalculate
|
||||||
|
super
|
||||||
|
|
||||||
|
recalculate_menu
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate_menu
|
||||||
|
# FIXME: properly find scrollable parent, if any.
|
||||||
|
parent_scroll_top = parent&.parent ? parent.parent.scroll_top : 0
|
||||||
|
|
||||||
|
@x = @parent.x
|
||||||
|
@y = parent_scroll_top + @parent.y + @parent.height
|
||||||
|
|
||||||
|
@y = (parent_scroll_top + @parent.y) - height if @y + height > window.height
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
recalculate
|
||||||
|
|
||||||
|
root.gui_state.show_menu(self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
6
lib/cyberarm_engine/ui/elements/menu_item.rb
Normal file
6
lib/cyberarm_engine/ui/elements/menu_item.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class MenuItem < Button
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -19,7 +19,7 @@ module CyberarmEngine
|
|||||||
@fraction_background.draw
|
@fraction_background.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def layout
|
||||||
_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
|
@width = _width
|
||||||
@@ -52,6 +52,7 @@ module CyberarmEngine
|
|||||||
@marquee_animation_time = Gosu.milliseconds if @marquee_offset > range
|
@marquee_animation_time = Gosu.milliseconds if @marquee_offset > range
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
|
root.gui_state.request_repaint
|
||||||
end
|
end
|
||||||
|
|
||||||
def type=(type)
|
def type=(type)
|
||||||
@@ -77,9 +78,13 @@ module CyberarmEngine
|
|||||||
def value=(decimal)
|
def value=(decimal)
|
||||||
raise "value must be number" unless decimal.is_a?(Numeric)
|
raise "value must be number" unless decimal.is_a?(Numeric)
|
||||||
|
|
||||||
|
old_value = @fraction
|
||||||
|
|
||||||
@fraction = decimal.clamp(0.0, 1.0)
|
@fraction = decimal.clamp(0.0, 1.0)
|
||||||
update_background
|
update_background
|
||||||
|
|
||||||
|
root.gui_state.request_repaint if @fraction != old_value
|
||||||
|
|
||||||
publish(:changed, @fraction)
|
publish(:changed, @fraction)
|
||||||
@fraction
|
@fraction
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :range, :step_size, :value
|
attr_reader :value
|
||||||
|
attr_accessor :range, :step_size
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
def initialize(options = {}, block = nil)
|
||||||
super(options, block)
|
super(options, block)
|
||||||
@@ -76,7 +77,7 @@ module CyberarmEngine
|
|||||||
def update
|
def update
|
||||||
super
|
super
|
||||||
|
|
||||||
@tip = value.to_s
|
@tip = format("%.2f", value.to_f)
|
||||||
@handle.tip = @tip
|
@handle.tip = @tip
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -87,7 +88,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_dragged_to(x, _y)
|
def handle_dragged_to(x, _y)
|
||||||
@ratio = ((x - @handle.width / 2) - @x) / (content_width - @handle.outer_width)
|
@ratio = ((x - @handle.width / 2.0) - @x) / (content_width - @handle.outer_width.to_f)
|
||||||
|
|
||||||
self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
|
self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
|
||||||
end
|
end
|
||||||
@@ -97,6 +98,8 @@ module CyberarmEngine
|
|||||||
position_handle
|
position_handle
|
||||||
@handle.recalculate
|
@handle.recalculate
|
||||||
|
|
||||||
|
root.gui_state.request_repaint
|
||||||
|
|
||||||
publish(:changed, @value)
|
publish(:changed, @value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ module CyberarmEngine
|
|||||||
@raw_text = text
|
@raw_text = text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
super
|
||||||
|
|
||||||
|
if @text.textobject.name != safe_style_fetch(:font)
|
||||||
|
set_font
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
# Gosu.clip_to is too expensive to always use so check if we actually need it.
|
# Gosu.clip_to is too expensive to always use so check if we actually need it.
|
||||||
if @text.width > width || @text.height > height
|
if @text.width > width || @text.height > height
|
||||||
@@ -29,7 +38,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def layout
|
||||||
unless @enabled
|
unless @enabled
|
||||||
@text.color = @style.disabled[:color]
|
@text.color = @style.disabled[:color]
|
||||||
else
|
else
|
||||||
@@ -50,7 +59,7 @@ module CyberarmEngine
|
|||||||
@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
|
||||||
@@ -65,8 +74,17 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_a?(Button)
|
if (vertical_alignment = @options[:text_v_align])
|
||||||
@text.y = @y + height / 2 - @text.height / 2
|
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
|
end
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
@@ -142,7 +160,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def line_width(text)
|
def line_width(text)
|
||||||
(@text.textobject.markup_width(text) + noncontent_width)
|
(@text.textobject.markup_width(text.to_s) + noncontent_width)
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
@@ -150,6 +168,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def value=(value)
|
def value=(value)
|
||||||
|
old_value = @raw_text
|
||||||
@raw_text = value.to_s.chomp
|
@raw_text = value.to_s.chomp
|
||||||
|
|
||||||
old_width = width
|
old_width = width
|
||||||
@@ -161,6 +180,8 @@ module CyberarmEngine
|
|||||||
recalculate
|
recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
root.gui_state.request_repaint if old_value != @raw_text
|
||||||
|
|
||||||
publish(:changed, self.value)
|
publish(:changed, self.value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ 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
|
|
||||||
else
|
else
|
||||||
super(options[:checkmark], options, block)
|
super(options[:checkmark], options, block)
|
||||||
end
|
end
|
||||||
@@ -16,10 +14,8 @@ module CyberarmEngine
|
|||||||
@value = options[:checked] || false
|
@value = options[:checked] || false
|
||||||
|
|
||||||
if @value
|
if @value
|
||||||
@image = @_image if @_image
|
|
||||||
@raw_text = @options[:checkmark]
|
@raw_text = @options[:checkmark]
|
||||||
else
|
else
|
||||||
@image = nil
|
|
||||||
@raw_text = ""
|
@raw_text = ""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -32,6 +28,14 @@ module CyberarmEngine
|
|||||||
:handled
|
:handled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
if @image
|
||||||
|
draw_image if @value
|
||||||
|
else
|
||||||
|
draw_text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
super
|
super
|
||||||
return if @image
|
return if @image
|
||||||
@@ -49,10 +53,8 @@ module CyberarmEngine
|
|||||||
@value = boolean
|
@value = boolean
|
||||||
|
|
||||||
if boolean
|
if boolean
|
||||||
@image = @_image if @_image
|
|
||||||
@raw_text = @options[:checkmark]
|
@raw_text = @options[:checkmark]
|
||||||
else
|
else
|
||||||
@image = nil
|
|
||||||
@raw_text = ""
|
@raw_text = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ module CyberarmEngine
|
|||||||
@pending_recalculate_request = false
|
@pending_recalculate_request = false
|
||||||
@pending_element_recalculate_requests = []
|
@pending_element_recalculate_requests = []
|
||||||
|
|
||||||
|
@needs_repaint = false
|
||||||
|
|
||||||
@menu = nil
|
@menu = nil
|
||||||
@min_drag_distance = 0
|
@min_drag_distance = 0
|
||||||
@mouse_pos = Vector.new
|
@mouse_pos = Vector.new
|
||||||
@@ -36,6 +38,10 @@ module CyberarmEngine
|
|||||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def menu
|
||||||
|
@menu
|
||||||
|
end
|
||||||
|
|
||||||
# throws :blur event to focused element and sets GuiState focused element
|
# throws :blur event to focused element and sets GuiState focused element
|
||||||
# Does NOT throw :focus event at element or set element as focused
|
# Does NOT throw :focus event at element or set element as focused
|
||||||
def focus=(element)
|
def focus=(element)
|
||||||
@@ -48,6 +54,24 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
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
|
||||||
|
Stats.frame.start_timing(:gui_recalculate)
|
||||||
|
|
||||||
|
@root_container.recalculate
|
||||||
|
|
||||||
|
@pending_recalculate_request = false
|
||||||
|
|
||||||
|
Stats.frame.end_timing(:gui_recalculate)
|
||||||
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
|
|
||||||
if @menu
|
if @menu
|
||||||
@@ -55,31 +79,26 @@ module CyberarmEngine
|
|||||||
@menu.draw
|
@menu.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
if @tip.value.length.positive?
|
if @tip && @tip.value.length.positive?
|
||||||
Gosu.flush
|
Gosu.flush
|
||||||
|
|
||||||
@tip.draw
|
@tip.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
if defined?(GUI_DEBUG)
|
if CyberarmEngine.const_defined?("GUI_DEBUG")
|
||||||
Gosu.flush
|
Gosu.flush
|
||||||
|
|
||||||
@root_container.debug_draw
|
@root_container.debug_draw
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@needs_repaint = false
|
||||||
|
end
|
||||||
|
|
||||||
|
def needs_repaint?
|
||||||
|
@needs_repaint
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
if @pending_recalculate_request
|
|
||||||
@root_container.recalculate
|
|
||||||
@root_container.recalculate
|
|
||||||
@root_container.recalculate
|
|
||||||
|
|
||||||
@pending_recalculate_request = false
|
|
||||||
end
|
|
||||||
|
|
||||||
@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
|
||||||
|
|
||||||
@@ -88,9 +107,19 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
@menu&.update
|
@menu&.update
|
||||||
|
|
||||||
super
|
super
|
||||||
|
|
||||||
|
if @active_width != window.width || @active_height != window.height
|
||||||
|
request_recalculate
|
||||||
|
@root_container.publish(:window_size_changed)
|
||||||
|
end
|
||||||
|
|
||||||
|
@active_width = window.width
|
||||||
|
@active_height = window.height
|
||||||
|
|
||||||
return unless window.has_focus?
|
return unless window.has_focus?
|
||||||
|
return unless window.current_state == self
|
||||||
|
|
||||||
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)
|
||||||
@@ -103,9 +132,9 @@ module CyberarmEngine
|
|||||||
@mouse_over.publish(:leave) if @mouse_over && new_mouse_over != @mouse_over
|
@mouse_over.publish(:leave) if @mouse_over && new_mouse_over != @mouse_over
|
||||||
@mouse_over = new_mouse_over
|
@mouse_over = new_mouse_over
|
||||||
|
|
||||||
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
|
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MS_LEFT)
|
||||||
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
|
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MS_MIDDLE)
|
||||||
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
|
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MS_RIGHT)
|
||||||
|
|
||||||
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
||||||
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
||||||
@@ -127,27 +156,19 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||||
@mouse_pos = @last_mouse_pos.clone
|
@mouse_pos = @last_mouse_pos.clone
|
||||||
|
|
||||||
if @active_width != window.width || @active_height != window.height
|
|
||||||
request_recalculate
|
|
||||||
@root_container.publish(:window_size_changed)
|
|
||||||
end
|
|
||||||
|
|
||||||
@active_width = window.width
|
|
||||||
@active_height = window.height
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
super
|
super
|
||||||
|
|
||||||
case id
|
case id
|
||||||
when Gosu::MsLeft
|
when Gosu::MS_LEFT
|
||||||
redirect_mouse_button(:left)
|
redirect_mouse_button(:left)
|
||||||
when Gosu::MsMiddle
|
when Gosu::MS_MIDDLE
|
||||||
redirect_mouse_button(:middle)
|
redirect_mouse_button(:middle)
|
||||||
when Gosu::MsRight
|
when Gosu::MS_RIGHT
|
||||||
redirect_mouse_button(:right)
|
redirect_mouse_button(:right)
|
||||||
when Gosu::KbF5
|
when Gosu::KB_F5
|
||||||
request_recalculate
|
request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -158,16 +179,24 @@ module CyberarmEngine
|
|||||||
super
|
super
|
||||||
|
|
||||||
case id
|
case id
|
||||||
when Gosu::MsLeft
|
when Gosu::MS_LEFT
|
||||||
redirect_released_mouse_button(:left)
|
redirect_released_mouse_button(:left)
|
||||||
when Gosu::MsMiddle
|
when Gosu::MS_MIDDLE
|
||||||
redirect_released_mouse_button(:middle)
|
redirect_released_mouse_button(:middle)
|
||||||
when Gosu::MsRight
|
when Gosu::MS_RIGHT
|
||||||
redirect_released_mouse_button(:right)
|
redirect_released_mouse_button(:right)
|
||||||
when Gosu::MsWheelUp
|
when Gosu::MS_WHEEL_UP
|
||||||
redirect_mouse_wheel(:up)
|
redirect_mouse_wheel(:up)
|
||||||
when Gosu::MsWheelDown
|
when Gosu::MS_WHEEL_DOWN
|
||||||
redirect_mouse_wheel(:down)
|
redirect_mouse_wheel(:down)
|
||||||
|
when Gosu::KB_HOME
|
||||||
|
redirect_scroll_jump_to(:top)
|
||||||
|
when Gosu::KB_END
|
||||||
|
redirect_scroll_jump_to(:end)
|
||||||
|
when Gosu::KB_PAGE_UP
|
||||||
|
redirect_scroll_page(:up)
|
||||||
|
when Gosu::KB_PAGE_DOWN
|
||||||
|
redirect_scroll_page(:down)
|
||||||
end
|
end
|
||||||
|
|
||||||
@focus.button_up(id) if @focus.respond_to?(:button_up)
|
@focus.button_up(id) if @focus.respond_to?(:button_up)
|
||||||
@@ -233,7 +262,15 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def redirect_mouse_wheel(button)
|
def redirect_mouse_wheel(button)
|
||||||
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
|
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||||
|
end
|
||||||
|
|
||||||
|
def redirect_scroll_jump_to(edge)
|
||||||
|
@mouse_over.publish(:"scroll_jump_to_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||||
|
end
|
||||||
|
|
||||||
|
def redirect_scroll_page(edge)
|
||||||
|
@mouse_over.publish(:"scroll_page_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Schedule a full GUI recalculation on next update
|
# Schedule a full GUI recalculation on next update
|
||||||
@@ -253,6 +290,12 @@ module CyberarmEngine
|
|||||||
@pending_focus_element = element
|
@pending_focus_element = element
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def request_repaint
|
||||||
|
# puts caller[0..4]
|
||||||
|
# puts
|
||||||
|
@needs_repaint = true
|
||||||
|
end
|
||||||
|
|
||||||
def show_menu(list_box)
|
def show_menu(list_box)
|
||||||
@menu = list_box
|
@menu = list_box
|
||||||
end
|
end
|
||||||
@@ -260,6 +303,8 @@ module CyberarmEngine
|
|||||||
def hide_menu
|
def hide_menu
|
||||||
return unless @menu
|
return unless @menu
|
||||||
|
|
||||||
|
request_repaint
|
||||||
|
|
||||||
@hid_menu_for = @menu.parent
|
@hid_menu_for = @menu.parent
|
||||||
@menu = nil
|
@menu = nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ module CyberarmEngine
|
|||||||
attr_reader :hash
|
attr_reader :hash
|
||||||
|
|
||||||
def initialize(hash = {})
|
def initialize(hash = {})
|
||||||
h = Marshal.load(Marshal.dump(hash))
|
h = hash
|
||||||
|
# h = Marshal.load(Marshal.dump(hash))
|
||||||
|
|
||||||
h[:default] = {}
|
h[:default] = {}
|
||||||
|
|
||||||
|
|||||||
@@ -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: {
|
||||||
@@ -106,6 +107,10 @@ module CyberarmEngine
|
|||||||
text_static: false # static text causes issues correctly displaying caret position
|
text_static: false # static text causes issues correctly displaying caret position
|
||||||
},
|
},
|
||||||
|
|
||||||
|
EditBox: { # < EditLine
|
||||||
|
text_v_align: :top
|
||||||
|
},
|
||||||
|
|
||||||
Image: { # < Element
|
Image: { # < Element
|
||||||
color: Gosu::Color::WHITE,
|
color: Gosu::Color::WHITE,
|
||||||
tileable: false,
|
tileable: false,
|
||||||
@@ -203,6 +208,17 @@ module CyberarmEngine
|
|||||||
fraction_background: [0xffc75e61, 0xffe26623],
|
fraction_background: [0xffc75e61, 0xffe26623],
|
||||||
border_thickness: 1,
|
border_thickness: 1,
|
||||||
border_color: [0xffd59674, 0xffff8746]
|
border_color: [0xffd59674, 0xffff8746]
|
||||||
|
},
|
||||||
|
Menu: { # < Stack
|
||||||
|
width: 200,
|
||||||
|
border_color: 0xaa_efefef,
|
||||||
|
border_thickness: 1
|
||||||
|
},
|
||||||
|
|
||||||
|
MenuItem: { # < Button
|
||||||
|
width: 1.0,
|
||||||
|
text_left: :left,
|
||||||
|
margin: 0
|
||||||
}
|
}
|
||||||
}.freeze
|
}.freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,39 +95,58 @@ module CyberarmEngine
|
|||||||
Vector.new(@x, @y)
|
Vector.new(@x, @y)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs math operation, excluding {weight}
|
|
||||||
private def operator(function, other)
|
|
||||||
if other.is_a?(Numeric)
|
|
||||||
Vector.new(
|
|
||||||
@x.send(:"#{function}", other),
|
|
||||||
@y.send(:"#{function}", other),
|
|
||||||
@z.send(:"#{function}", other)
|
|
||||||
)
|
|
||||||
else
|
|
||||||
Vector.new(
|
|
||||||
@x.send(:"#{function}", other.x),
|
|
||||||
@y.send(:"#{function}", other.y),
|
|
||||||
@z.send(:"#{function}", other.z)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def +(other)
|
def +(other)
|
||||||
operator("+", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x + other,
|
||||||
|
@y + other,
|
||||||
|
@z + other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x + other.x,
|
||||||
|
@y + other.y,
|
||||||
|
@z + other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def -(other)
|
def -(other)
|
||||||
operator("-", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x - other,
|
||||||
|
@y - other,
|
||||||
|
@z - other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x - other.x,
|
||||||
|
@y - other.y,
|
||||||
|
@z - other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
# @return [CyberarmEngine::Vector]
|
# @return [CyberarmEngine::Vector]
|
||||||
def *(other)
|
def *(other)
|
||||||
operator("*", other)
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x * other,
|
||||||
|
@y * other,
|
||||||
|
@z * other
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x * other.x,
|
||||||
|
@y * other.y,
|
||||||
|
@z * other.z
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def multiply_transform(transform)
|
def multiply_transform(transform)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev".freeze
|
NAME = "InDev".freeze
|
||||||
VERSION = "0.21.0".freeze
|
VERSION = "0.24.3".freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,16 +6,16 @@ module CyberarmEngine
|
|||||||
SAMPLES = {}
|
SAMPLES = {}
|
||||||
SONGS = {}
|
SONGS = {}
|
||||||
|
|
||||||
attr_accessor :show_cursor
|
attr_accessor :show_cursor, :show_stats_plotter
|
||||||
attr_writer :exit_on_opengl_error
|
attr_writer :exit_on_opengl_error
|
||||||
attr_reader :last_frame_time, :states
|
attr_reader :last_frame_time, :delta_time, :states
|
||||||
|
|
||||||
def self.now
|
def self.now
|
||||||
Gosu.milliseconds
|
Gosu.milliseconds
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.dt
|
def self.dt
|
||||||
instance.last_frame_time / 1000.0
|
instance.dt
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.instance=(window)
|
def self.instance=(window)
|
||||||
@@ -31,31 +31,57 @@ module CyberarmEngine
|
|||||||
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
|
@has_focus = false
|
||||||
|
@show_stats_plotter = 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.instance = 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}"
|
@delta_time = @last_frame_time
|
||||||
|
self.caption = "CyberarmEngine #{CyberarmEngine::VERSION} #{Gosu.user_languages.join(', ')}"
|
||||||
|
|
||||||
@states = []
|
@states = []
|
||||||
@exit_on_opengl_error = false
|
@exit_on_opengl_error = false
|
||||||
|
preload_default_shaders if respond_to?(:preload_default_shaders)
|
||||||
|
@stats_plotter = Stats::StatsPlotter.new(2, 28) # FIXME: Make positioning easy
|
||||||
|
|
||||||
setup if defined?(setup)
|
setup
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
Stats.frame.start_timing(:draw)
|
||||||
|
|
||||||
current_state&.draw
|
current_state&.draw
|
||||||
|
Stats.frame.start_timing(:engine_stats_renderer)
|
||||||
|
@stats_plotter&.draw if @show_stats_plotter
|
||||||
|
Stats.frame.end_timing(:engine_stats_renderer)
|
||||||
|
|
||||||
|
Stats.frame.end_timing(:draw)
|
||||||
|
Stats.frame.start_timing(:interframe_sleep)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
Stats.clear
|
# Gosu calls update() then (optionally) draw(),
|
||||||
|
# so always end last frame and start next frame when update() is called.
|
||||||
|
Stats.frame&.end_timing(:interframe_sleep)
|
||||||
|
Stats.end_frame
|
||||||
|
|
||||||
current_state&.update
|
Stats.new_frame
|
||||||
|
|
||||||
|
@delta_time = (Gosu.milliseconds - @current_frame_time) * 0.001
|
||||||
|
|
||||||
@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
|
||||||
|
|
||||||
|
Stats.frame.start_timing(:update)
|
||||||
|
current_state&.update
|
||||||
|
Stats.frame.end_timing(:update)
|
||||||
|
|
||||||
|
Stats.frame.start_timing(:interframe_sleep) unless needs_redraw?
|
||||||
end
|
end
|
||||||
|
|
||||||
def needs_cursor?
|
def needs_cursor?
|
||||||
@@ -119,7 +145,7 @@ module CyberarmEngine
|
|||||||
def push_state(klass, options = {})
|
def push_state(klass, options = {})
|
||||||
options = { setup: true }.merge(options)
|
options = { setup: true }.merge(options)
|
||||||
|
|
||||||
if klass.instance_of?(klass.class) && defined?(klass.options)
|
if klass.instance_of?(klass.class) && klass.respond_to?(:options)
|
||||||
@states << klass
|
@states << klass
|
||||||
klass.setup if options[:setup]
|
klass.setup if options[:setup]
|
||||||
klass.post_setup if options[:setup]
|
klass.post_setup if options[:setup]
|
||||||
@@ -132,6 +158,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
private def child_of?(input, klass)
|
private def child_of?(input, klass)
|
||||||
|
return false unless input
|
||||||
|
|
||||||
input.ancestors.detect { |c| c == klass }
|
input.ancestors.detect { |c| c == klass }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -141,10 +169,14 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def pop_state
|
def pop_state
|
||||||
@states.pop
|
@states.pop
|
||||||
|
|
||||||
|
current_state.request_repaint if current_state&.is_a?(GuiState)
|
||||||
end
|
end
|
||||||
|
|
||||||
def shift_state
|
def shift_state
|
||||||
@states.shift
|
@states.shift
|
||||||
|
|
||||||
|
current_state.request_repaint if current_state&.is_a?(GuiState)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_focus?
|
def has_focus?
|
||||||
|
|||||||
29
mrbgem.rake
Normal file
29
mrbgem.rake
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
MRuby::Gem::Specification.new("mruby-cyberarm_engine") do |spec|
|
||||||
|
spec.license = "MIT"
|
||||||
|
spec.authors = "cyberarm"
|
||||||
|
spec.summary = " Yet another framework for building games with Gosu"
|
||||||
|
|
||||||
|
lib_rbfiles = []
|
||||||
|
# Dir.glob("#{File.expand_path("..", __FILE__)}/lib/**/*.rb").reject do |f|
|
||||||
|
# File.basename(f.downcase, ".rb") == "cyberarm_engine" ||
|
||||||
|
# File.basename(f.downcase, ".rb") == "opengl" ||
|
||||||
|
# f.downcase.include?("/opengl/")
|
||||||
|
# end.reverse!
|
||||||
|
|
||||||
|
local_path = File.expand_path("..", __FILE__)
|
||||||
|
File.read("#{local_path}/lib/cyberarm_engine.rb").each_line do |line|
|
||||||
|
line = line.strip
|
||||||
|
|
||||||
|
next unless line.start_with?("require_relative")
|
||||||
|
|
||||||
|
file = line.split("require_relative").last.strip.gsub("\"", "")
|
||||||
|
|
||||||
|
next if file.include?(" if ")
|
||||||
|
|
||||||
|
lib_rbfiles << "#{local_path}/lib/#{file}.rb"
|
||||||
|
end
|
||||||
|
|
||||||
|
pp lib_rbfiles
|
||||||
|
|
||||||
|
spec.rbfiles = lib_rbfiles
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user