mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
90 Commits
v0.17.1
...
5d1c195917
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 | |||
| 41c0b27937 | |||
| 2fd5d398cf | |||
| 2e66509f87 | |||
| 521b3937dd | |||
| ab9f9e8e7a | |||
| 705138f7ad | |||
| 94a65f447c | |||
| 97296b080c | |||
| ca73a2d8e8 | |||
| 0a62e5180a | |||
| 31e909eb30 | |||
| 300e7c4e59 | |||
| be98fe47ad | |||
| a75afaf47a | |||
| d81df5f4e2 | |||
| f2ea0d9942 | |||
| 08d068f156 | |||
| a4d02038c3 | |||
| 37bdd6ef23 | |||
| 24bd769a32 | |||
| 2be5733bc1 | |||
| c8734ae98b | |||
| c35d587419 | |||
| 153871e7f3 | |||
| cf91d4a083 | |||
| 35ad687d4c | |||
| 54802e1254 | |||
| 6cf4cd73dd | |||
| 5e5f8ba7ea | |||
| 63a51d9d2f | |||
| 6af384536a | |||
| c1b25d2045 | |||
| a915a25699 | |||
| 0aa9b59316 | |||
| d2bf406d29 | |||
| f82c101728 | |||
| 2f727e9bf2 | |||
| 24be9bfb29 | |||
| 5452d149c3 | |||
| cdee6548e3 | |||
| bd54fafc3f | |||
| a92d1ad746 | |||
| 1b080f9fb9 | |||
| 8057bca818 | |||
| c1310f3073 | |||
| 850bb610bb | |||
| 0b63986b64 | |||
| 165fc6f307 | |||
| e3c8e3bcc2 | |||
| 551a55f894 | |||
| 01292ead10 | |||
| 7ca6e1bc58 | |||
| a70f260bc6 | |||
| 8002708695 | |||
| 676545f3c7 | |||
| 4b417f9ab7 | |||
| a70c106387 | |||
| f662fabc56 | |||
| abb989f842 | |||
| 1e0d2004b5 | |||
| 92dd63dc1d |
@@ -1,4 +1,4 @@
|
||||
# CyberarmEngine
|
||||

|
||||
|
||||
Yet Another Game Engine On Top Of Gosu
|
||||
|
||||
@@ -32,6 +32,8 @@ require "cyberarm_engine"
|
||||
|
||||
class Hello < CyberarmEngine::GuiState
|
||||
def setup
|
||||
background Gosu::Color::GRAY
|
||||
|
||||
stack do
|
||||
label "Hello World!"
|
||||
|
||||
@@ -43,15 +45,14 @@ class Hello < CyberarmEngine::GuiState
|
||||
end
|
||||
|
||||
class Window < CyberarmEngine::Window
|
||||
def initialize
|
||||
super
|
||||
def setup
|
||||
self.show_cursor = true
|
||||
|
||||
push_state(Hello)
|
||||
end
|
||||
end
|
||||
|
||||
Window.new.show
|
||||
Window.new(width: 800, height: 600, fullscreen: false, resizable: true).show
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
BIN
assets/textures/logo.png
Normal file
BIN
assets/textures/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
@@ -27,8 +27,7 @@ Gem::Specification.new do |spec|
|
||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = %w[lib assets]
|
||||
|
||||
spec.add_dependency "clipboard", "~> 1.3.5"
|
||||
spec.add_dependency "excon", "~> 0.78.0"
|
||||
spec.add_dependency "excon", "~> 0.88"
|
||||
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
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
CYBERARM_ENGINE_ROOT_PATH = File.expand_path("..", __dir__)
|
||||
|
||||
begin
|
||||
require File.expand_path("../../ffi-gosu/lib/gosu", File.dirname(__FILE__))
|
||||
rescue LoadError => e
|
||||
pp e
|
||||
if ARGV.join.include?("--ffi-gosu")
|
||||
require File.expand_path("../../ffi-gosu/lib/gosu", __dir__)
|
||||
else
|
||||
require "gosu"
|
||||
end
|
||||
require "json"
|
||||
require "excon"
|
||||
require "gosu_more_drawables"
|
||||
require "clipboard"
|
||||
|
||||
require_relative "cyberarm_engine/version"
|
||||
require_relative "cyberarm_engine/stats"
|
||||
@@ -24,12 +22,19 @@ require_relative "cyberarm_engine/vector"
|
||||
require_relative "cyberarm_engine/transform"
|
||||
require_relative "cyberarm_engine/ray"
|
||||
require_relative "cyberarm_engine/background"
|
||||
require_relative "cyberarm_engine/background_nine_slice"
|
||||
require_relative "cyberarm_engine/background_image"
|
||||
require_relative "cyberarm_engine/animator"
|
||||
|
||||
require_relative "cyberarm_engine/text"
|
||||
require_relative "cyberarm_engine/timer"
|
||||
require_relative "cyberarm_engine/config_file"
|
||||
|
||||
require_relative "cyberarm_engine/console"
|
||||
require_relative "cyberarm_engine/console/command"
|
||||
require_relative "cyberarm_engine/console/subcommand"
|
||||
require_relative "cyberarm_engine/console/commands/help_command"
|
||||
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
|
||||
require_relative "cyberarm_engine/ui/theme"
|
||||
@@ -61,4 +66,6 @@ require_relative "cyberarm_engine/model/material"
|
||||
require_relative "cyberarm_engine/model/model_object"
|
||||
require_relative "cyberarm_engine/model/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"
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
module CyberarmEngine
|
||||
class Animator
|
||||
DEFAULT_TWEEN = :linear
|
||||
def initialize(start_time:, duration:, from:, to:, &block)
|
||||
def initialize(start_time:, duration:, from:, to:, tween: :linear, &block)
|
||||
@start_time = start_time
|
||||
@duration = duration
|
||||
@from = from.dup
|
||||
@to = to.dup
|
||||
@tween = tween
|
||||
@block = block
|
||||
end
|
||||
|
||||
@@ -14,18 +14,18 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def progress
|
||||
(@start_time.to_f + (Gosu.milliseconds - @start_time)) / (@start_time + @duration.to_f)
|
||||
((Gosu.milliseconds - @start_time) / @duration.to_f).clamp(0.0, 1.0)
|
||||
end
|
||||
|
||||
def complete?
|
||||
progress >= 1.0
|
||||
end
|
||||
|
||||
def transition(from, to, tween = DEFAULT_TWEEN)
|
||||
def transition(from = @from, to = @to, tween = @tween)
|
||||
from + (to - from) * send("tween_#{tween}", progress)
|
||||
end
|
||||
|
||||
def color_transition(from, to, _tween = DEFAULT_TWEEN)
|
||||
def color_transition(from = @from, to = @to, _tween = @tween)
|
||||
r = transition(from.red, to.red)
|
||||
g = transition(from.green, to.green)
|
||||
b = transition(from.blue, to.blue)
|
||||
@@ -34,7 +34,7 @@ module CyberarmEngine
|
||||
Gosu::Color.rgba(r, g, b, a)
|
||||
end
|
||||
|
||||
def color_hsv_transition(from, to, tween = DEFAULT_TWEEN)
|
||||
def color_hsv_transition(from = @from, to = @to, tween = @tween)
|
||||
hue = transition(from.hue, to.hue, tween)
|
||||
saturation = transition(from.saturation, to.saturation, tween)
|
||||
value = transition(from.value, to.value, tween)
|
||||
@@ -43,14 +43,177 @@ module CyberarmEngine
|
||||
Gosu::Color.from_ahsv(alpha, hue, saturation, value)
|
||||
end
|
||||
|
||||
# NOTE: Use this for future reference? https://github.com/danro/easing-js/blob/master/easing.js
|
||||
# Tween functions based on those provided here: https://github.com/danro/easing-js/blob/master/easing.js
|
||||
# Under MIT / BSD
|
||||
|
||||
def tween_linear(t)
|
||||
t
|
||||
end
|
||||
|
||||
def tween_sine(t)
|
||||
Math.sin(t) * t
|
||||
def tween_ease_in_quad(t)
|
||||
t ** 2
|
||||
end
|
||||
|
||||
def tween_ease_out_quad(t)
|
||||
-((t - 1) ** 2) -1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_quad(t)
|
||||
return 0.5 * (t ** 2) if (t /= 0.5) < 1
|
||||
return -0.5 * ((t -= 2) * t - 2)
|
||||
end
|
||||
|
||||
def tween_ease_in_cubic(t)
|
||||
t ** 3
|
||||
end
|
||||
|
||||
def tween_ease_out_cubic(t)
|
||||
((t - 1) ** 3) + 1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_cubic(t)
|
||||
return 0.5 * (t ** 3) if ((t /= 0.5) < 1)
|
||||
return 0.5 * ((t - 2) ** 3) + 2
|
||||
end
|
||||
|
||||
def tween_ease_in_quart(t)
|
||||
t ** 4
|
||||
end
|
||||
|
||||
def tween_ease_out_quart(t)
|
||||
-((t - 1) ** 4) - 1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_quart(t)
|
||||
return 0.5 * (t ** 4) if ((t /= 0.5) < 1)
|
||||
return -0.5 * ((t -= 2) * (t ** 3) - 2)
|
||||
end
|
||||
|
||||
def tween_ease_in_quint(t)
|
||||
t ** 5
|
||||
end
|
||||
|
||||
def tween_ease_out_quint(t)
|
||||
((t - 1) ** 5) + 1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_quint(t)
|
||||
return 0.5 * (t ** 5) if ((t /= 0.5) < 1)
|
||||
return 0.5 * ((t - 2) ** 5) + 2
|
||||
end
|
||||
|
||||
def tween_ease_in(t) # sine
|
||||
-Math.cos(t * (Math::PI / 2)) + 1
|
||||
end
|
||||
|
||||
def tween_ease_out(t) # sine
|
||||
Math.sin(t * (Math::PI / 2))
|
||||
end
|
||||
|
||||
def tween_ease_in_out(t) # sine
|
||||
(-0.5 * (Math.cos(Math::PI * t) - 1))
|
||||
end
|
||||
|
||||
def tween_ease_in_expo(t)
|
||||
(t == 0) ? 0 : 2 ** 10 * (t - 1)
|
||||
end
|
||||
|
||||
def tween_ease_out_expo(t)
|
||||
(t == 1) ? 1 : -(2 ** -10 * t) + 1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_expo(t)
|
||||
return 0 if (t == 0)
|
||||
return 1 if (t == 1)
|
||||
return 0.5 * (2 ** 10 * (t - 1)) if ((t /= 0.5) < 1)
|
||||
return 0.5 * (-(2 ** -10 * (t -= 1)) + 2)
|
||||
end
|
||||
|
||||
def tween_ease_in_circ(t)
|
||||
-(Math.sqrt(1 - (t * t)) - 1)
|
||||
end
|
||||
|
||||
def tween_ease_out_circ(t)
|
||||
Math.sqrt(1 - ((t - 1) ** 2))
|
||||
end
|
||||
|
||||
def tween_ease_in_out_circ(t)
|
||||
return -0.5 * (Math.sqrt(1 - t * t) - 1) if ((t /= 0.5) < 1)
|
||||
return 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1)
|
||||
end
|
||||
|
||||
def tween_ease_in_back(t)
|
||||
s = 1.70158
|
||||
t * t * ((s + 1) * t - s)
|
||||
end
|
||||
|
||||
def tween_ease_out_back(t)
|
||||
s = 1.70158
|
||||
(t = t - 1) * t * ((s + 1) * t + s) + 1
|
||||
end
|
||||
|
||||
def tween_ease_in_out_back(t)
|
||||
s = 1.70158
|
||||
return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) if ((t /= 0.5) < 1)
|
||||
return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2)
|
||||
end
|
||||
|
||||
def tween_elastic(t)
|
||||
-1 * (4 ** (-8 * t)) * Math.sin((t * 6 - 1) * (2 * Math::PI) / 2) + 1
|
||||
end
|
||||
|
||||
def tween_swing_from_to(t)
|
||||
s = 1.70158
|
||||
return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) if (t /= 0.5) < 1
|
||||
return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2)
|
||||
end
|
||||
|
||||
def tween_swing_from(t)
|
||||
s = 1.70158;
|
||||
t * t * ((s + 1) * t - s)
|
||||
end
|
||||
|
||||
def tween_swing_to(t)
|
||||
s = 1.70158
|
||||
(t -= 1) * t * ((s + 1) * t + s) + 1
|
||||
end
|
||||
|
||||
def tween_bounce(t)
|
||||
if (t < (1 / 2.75))
|
||||
(7.5625 * t * t)
|
||||
elsif (t < (2 / 2.75))
|
||||
(7.5625 * (t -= (1.5 / 2.75)) * t + 0.75)
|
||||
elsif (t < (2.5 / 2.75))
|
||||
(7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375)
|
||||
else
|
||||
(7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375)
|
||||
end
|
||||
end
|
||||
|
||||
def tween_bounce_past(t)
|
||||
if (t < (1 / 2.75))
|
||||
# missing "2 -"?
|
||||
(7.5625 * t * t)
|
||||
elsif (t < (2 / 2.75))
|
||||
2 - (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75)
|
||||
elsif (t < (2.5 / 2.75))
|
||||
2 - (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375)
|
||||
else
|
||||
2 - (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375)
|
||||
end
|
||||
end
|
||||
|
||||
def tween_ease_from_to(t)
|
||||
return 0.5 * (t ** 4) if ((t /= 0.5) < 1)
|
||||
return -0.5 * ((t -= 2) * (t ** 3) - 2)
|
||||
end
|
||||
|
||||
def tween_ease_from(t)
|
||||
t ** 4
|
||||
end
|
||||
|
||||
def tween_ease_to(t)
|
||||
t ** 0.25
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
93
lib/cyberarm_engine/background_image.rb
Normal file
93
lib/cyberarm_engine/background_image.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
module CyberarmEngine
|
||||
class BackgroundImage
|
||||
include CyberarmEngine::Common
|
||||
attr_accessor :x, :y, :z, :mode
|
||||
attr_reader :image, :width, :height, :color
|
||||
|
||||
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :fill, color: Gosu::Color::WHITE)
|
||||
@image_path = image_path
|
||||
@image = get_image(image_path) if image_path
|
||||
|
||||
@x = x
|
||||
@y = y
|
||||
@z = z
|
||||
@width = width
|
||||
@height = height
|
||||
|
||||
@mode = mode
|
||||
|
||||
@color = color
|
||||
|
||||
@cached_tiling = nil
|
||||
end
|
||||
|
||||
def image=(image_path)
|
||||
@cached_tiling = nil if image_path != @image_path
|
||||
@image_path = image_path
|
||||
@image = image_path ? get_image(image_path) : image_path
|
||||
end
|
||||
|
||||
def width=(n)
|
||||
@cached_tiling = nil if @width != n
|
||||
@width = n
|
||||
end
|
||||
|
||||
def height=(n)
|
||||
@cached_tiling = nil if @height != n
|
||||
@height = n
|
||||
end
|
||||
|
||||
def color=(c)
|
||||
@cached_tiling = nil if @color != c
|
||||
@color = c
|
||||
end
|
||||
|
||||
def width_scale
|
||||
(@width.to_f / @image.width).abs
|
||||
end
|
||||
|
||||
def height_scale
|
||||
(@height.to_f / @image.height).abs
|
||||
end
|
||||
|
||||
def draw
|
||||
return unless @image
|
||||
|
||||
Gosu.clip_to(@x, @y, @width, @height) do
|
||||
send(:"draw_#{mode}")
|
||||
end
|
||||
end
|
||||
|
||||
def draw_stretch
|
||||
@image.draw(@x, @y, @z, width_scale, height_scale, @color)
|
||||
end
|
||||
|
||||
def draw_tiled
|
||||
@cached_tiling ||= Gosu.record(@width, @height) do
|
||||
height_scale.ceil.times do |y|
|
||||
width_scale.ceil.times do |x|
|
||||
@image.draw(x * @image.width, y * @image.height, @z, 1, 1, @color)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@cached_tiling.draw(@x, @y, @z)
|
||||
end
|
||||
|
||||
def draw_fill
|
||||
if (@image.width * width_scale) >= @width && (@image.height * width_scale) >= @height
|
||||
draw_fill_width
|
||||
else
|
||||
draw_fill_height
|
||||
end
|
||||
end
|
||||
|
||||
def draw_fill_width
|
||||
@image.draw(@x, @y, @z, width_scale, width_scale, @color)
|
||||
end
|
||||
|
||||
def draw_fill_height
|
||||
@image.draw(@x, @y, @z, height_scale, height_scale, @color)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,10 +1,11 @@
|
||||
module CyberarmEngine
|
||||
class BackgroundNineSlice
|
||||
include CyberarmEngine::Common
|
||||
attr_accessor :x, :y, :z, :width, :height
|
||||
attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color
|
||||
attr_reader :image
|
||||
|
||||
def initialize(image_path:, x: 0, y: 0, z: 0, width: 64, height: 64, mode: :tiled, left: 4, top: 4, right: 56, bottom: 56)
|
||||
@image = get_image(image_path)
|
||||
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE)
|
||||
@image = get_image(image_path) if image_path
|
||||
|
||||
@x = x
|
||||
@y = y
|
||||
@@ -20,23 +21,33 @@ module CyberarmEngine
|
||||
@right = right
|
||||
@bottom = bottom
|
||||
|
||||
nine_slice
|
||||
@color = color
|
||||
|
||||
nine_slice if @image
|
||||
end
|
||||
|
||||
def image=(image_path)
|
||||
old_image = @image
|
||||
@image = image_path ? get_image(image_path) : image_path
|
||||
nine_slice if @image && old_image != @image
|
||||
end
|
||||
|
||||
def nine_slice
|
||||
@segment_top_left = Gosu.render(@left, @top) { @image.draw(0, 0, 0) }
|
||||
@segment_top_right = Gosu.render(@image.width - @right, @top) { @image.draw(-@right, 0, 0) }
|
||||
# pp [@left, @top, @right, @bottom, @image.width]
|
||||
|
||||
@segment_left = Gosu.render(@left, @bottom - @top) { @image.draw(0, -@top, 0) }
|
||||
@segment_right = Gosu.render(@image.width - @right, @bottom - @top) { @image.draw(-@right, -@top, 0) }
|
||||
@segment_top_left = @image.subimage(0, 0, @left, @top)
|
||||
@segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)
|
||||
|
||||
@segment_bottom_left = Gosu.render(@left, @image.height - @bottom) { @image.draw(0, -@bottom, 0) }
|
||||
@segment_bottom_right = Gosu.render(@image.width - @right, @image.height - @bottom) { @image.draw(-@right, -@bottom, 0) }
|
||||
@segment_left = @image.subimage(0, @top, @left, @image.height - (@top + @bottom))
|
||||
@segment_right = @image.subimage(@image.width - @right, @top, @left, @image.height - (@top + @bottom))
|
||||
|
||||
@segment_top = Gosu.render(@right - @left, @top) { @image.draw(-@left, 0, 0) }
|
||||
@segment_bottom = Gosu.render(@right - @left, @image.height - @bottom) { @image.draw(-@left, -@bottom, 0) }
|
||||
@segment_bottom_left = @image.subimage(0, @image.height - @bottom, @left, @bottom)
|
||||
@segment_bottom_right = @image.subimage(@image.width - @right, @image.height - @bottom, @right, @bottom)
|
||||
|
||||
@segment_middle = Gosu.render(@right - @left, @bottom - @top) { @image.draw(-@left, -@top, 0) }
|
||||
@segment_top = @image.subimage(@left, 0, @image.width - (@left + @right), @top)
|
||||
@segment_bottom = @image.subimage(@left, @image.height - @bottom, @image.width - (@left + @right), @bottom)
|
||||
|
||||
@segment_middle = @image.subimage(@left, @top, @image.width - (@left + @right), @image.height - (@top + @bottom))
|
||||
end
|
||||
|
||||
def cx
|
||||
@@ -56,67 +67,73 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def width_scale
|
||||
width_scale = (@width - (@left + (@image.width - @right))).to_f / (@right - @left)
|
||||
scale = (@width.to_f - (@left + @right)) / (@image.width - (@left + @right))
|
||||
scale.abs
|
||||
end
|
||||
|
||||
def height_scale
|
||||
height_scale = (@height - (@top + (@image.height - @bottom))).to_f / (@bottom - @top)
|
||||
scale = (@height - (@top + @bottom)).to_f / (@image.height - (@top + @bottom))
|
||||
scale.abs
|
||||
end
|
||||
|
||||
def draw
|
||||
return unless @image && @segment_top_left
|
||||
|
||||
@mode == :tiled ? draw_tiled : draw_stretched
|
||||
end
|
||||
|
||||
def draw_stretched
|
||||
@segment_top_left.draw(@x, @y, @z)
|
||||
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale) # SCALE X
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color)
|
||||
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color) # SCALE X
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
|
||||
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale) # SCALE Y
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z)
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale) # SCALE X
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z)
|
||||
@segment_left.draw(@x, @y + @top, @z, 1, height_scale) # SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale) # SCALE X and SCALE Y
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color) # SCALE Y
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
|
||||
@segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
|
||||
end
|
||||
|
||||
def draw_tiled
|
||||
@segment_top_left.draw(@x, @y, @z)
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color)
|
||||
|
||||
# p [width_scale, height_scale]
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
|
||||
width_scale.ceil.times do |i|
|
||||
@segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z) # SCALE X
|
||||
@segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z, 1, 1, @color) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
|
||||
|
||||
Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
|
||||
height_scale.ceil.times do |i|
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z) # SCALE Y
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z, 1, 1, @color) # SCALE Y
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z)
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
|
||||
width_scale.ceil.times do |i|
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z) # SCALE X
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z, 1, 1, @color) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z)
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
|
||||
|
||||
Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
|
||||
height_scale.ceil.times do |i|
|
||||
@segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z) # SCALE Y
|
||||
@segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z, 1, 1, @color) # SCALE Y
|
||||
end
|
||||
end
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y + @segment_top.height, @width - (@segment_left.width + @segment_right.width), @height - (@segment_top.height + @segment_bottom.height)) do
|
||||
height_scale.ceil.times do |y|
|
||||
width_scale.ceil.times do |x|
|
||||
@segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z) # SCALE X and SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z, 1, 1, @color) # SCALE X and SCALE Y
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
131
lib/cyberarm_engine/builtin/intro_state.rb
Normal file
131
lib/cyberarm_engine/builtin/intro_state.rb
Normal file
@@ -0,0 +1,131 @@
|
||||
module CyberarmEngine
|
||||
class IntroState < CyberarmEngine::GameState
|
||||
def setup
|
||||
@display_width = 800
|
||||
@display_height = 600
|
||||
|
||||
@title_size = 56
|
||||
@caption_size = 24
|
||||
|
||||
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222, static: true)
|
||||
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222, static: true)
|
||||
|
||||
@spacer_width = 256
|
||||
@spacer_height = 6
|
||||
@padding = 6
|
||||
|
||||
@cyberarm_engine_logo = get_image "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/logo.png"
|
||||
|
||||
@gosu_logo = generate_proxy("Gosu", "Game Library", 0xff_111111)
|
||||
@ruby_logo = generate_proxy("Ruby", "Programming Language", 0xff_880000)
|
||||
@opengl_logo = generate_proxy("OpenGL", "Graphics API", 0xff_5586a4) if RUBY_ENGINE != "mruby" && defined?(OpenGL)
|
||||
|
||||
base_time = Gosu.milliseconds
|
||||
|
||||
@born_time = Gosu.milliseconds
|
||||
@continue_after = 5_000
|
||||
|
||||
@animators = [
|
||||
Animator.new(start_time: base_time += 1000, duration: 100, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += -500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time + 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: Gosu.milliseconds + @continue_after - 1_000, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
|
||||
Animator.new(start_time: Gosu.milliseconds + 250, duration: 500, from: 0.0, to: 1.0, tween: :swing_to) # CyberarmEngine LOGO
|
||||
]
|
||||
end
|
||||
|
||||
def draw
|
||||
Gosu.draw_rect(0, 0, window.width, window.height, 0xff_222222)
|
||||
|
||||
scale = (@display_width - @padding * 2).to_f / @cyberarm_engine_logo.width * @animators.last.transition
|
||||
|
||||
@cyberarm_engine_logo.draw_rot(
|
||||
window.width / 2,
|
||||
(window.height) / 2 - @cyberarm_engine_logo.height / 2 - @padding * 2,
|
||||
2,
|
||||
0,
|
||||
0.5,
|
||||
0.5,
|
||||
scale,
|
||||
scale
|
||||
)
|
||||
|
||||
Gosu.draw_rect(
|
||||
window.width / 2 - (@display_width / 2 + @padding),
|
||||
window.height / 2 - @spacer_height / 2,
|
||||
@display_width + @padding,
|
||||
@spacer_height * @animators[0].transition,
|
||||
Gosu::Color::WHITE
|
||||
)
|
||||
|
||||
@title.x = window.width / 2 - @title.text_width / 2
|
||||
@title.y = (window.height / 2 + (@spacer_height / 2) + @padding) * @animators[1].transition
|
||||
@title.text = "Powered By"
|
||||
|
||||
Gosu.clip_to(0, window.height / 2 + (@spacer_height / 2), window.width, @title.height) do
|
||||
@title.draw
|
||||
end
|
||||
|
||||
y = @title.y + @title.height * 2
|
||||
|
||||
Gosu.clip_to(0, y, window.width, @gosu_logo.height) do
|
||||
Gosu.translate(@opengl_logo.nil? ? @ruby_logo.width / 2 : 0, 0) do
|
||||
@gosu_logo.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2 - (@ruby_logo.width - @padding),
|
||||
y * @animators[2].transition,
|
||||
2
|
||||
)
|
||||
@ruby_logo.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2,
|
||||
y * @animators[3].transition,
|
||||
2
|
||||
)
|
||||
@opengl_logo&.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2 + (@ruby_logo.width - @padding),
|
||||
y * @animators[4].transition,
|
||||
2
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Gosu.draw_rect(0, 0, window.width, window.height, Gosu::Color.rgba(0, 0, 0, 255 * @animators[5].transition), 10_000)
|
||||
end
|
||||
|
||||
def update
|
||||
@animators.each(&:update)
|
||||
|
||||
return unless Gosu.milliseconds - @born_time >= @continue_after
|
||||
|
||||
pop_state
|
||||
push_state(@options[:forward], @options[:forward_options] || {}) if @options[:forward]
|
||||
end
|
||||
|
||||
def button_down(_id)
|
||||
@continue_after = 0
|
||||
end
|
||||
|
||||
def generate_proxy(title, caption, color_hint)
|
||||
@title.text = title
|
||||
@caption.text = caption
|
||||
|
||||
width = @spacer_width + 2 * @padding
|
||||
height = @title_size + @caption_size + @spacer_height + 2 * @padding + @spacer_height
|
||||
|
||||
Gosu.record(width.ceil, height.ceil) do
|
||||
@title.x = (width - @padding * 2) / 2 - @title.text_width / 2
|
||||
@title.y = @padding
|
||||
@title.draw
|
||||
|
||||
Gosu.draw_rect(0, @padding + @title_size + @padding, @spacer_width, @spacer_height, Gosu::Color::WHITE)
|
||||
Gosu.draw_rect(1, @padding + @title_size + @padding + 1, @spacer_width - 2, @spacer_height - 2, color_hint)
|
||||
|
||||
@caption.x = (width - @padding * 2) / 2 - @caption.text_width / 2
|
||||
@caption.y = @padding + @title_size + @padding + @spacer_height + @padding
|
||||
@caption.draw
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,14 +8,21 @@ module CyberarmEngine
|
||||
window.current_state
|
||||
end
|
||||
|
||||
def previous_state
|
||||
window.previous_state
|
||||
def previous_state(state = nil)
|
||||
raise "Only available for CyberarmEngine::GameState and subclasses" unless is_a?(CyberarmEngine::GameState) || state.is_a?(CyberarmEngine::GameState)
|
||||
|
||||
i = window.states.index(state || self)
|
||||
window.states[i - 1] unless (i - 1).negative?
|
||||
end
|
||||
|
||||
def pop_state
|
||||
window.pop_state
|
||||
end
|
||||
|
||||
def shift_state
|
||||
window.shift_state
|
||||
end
|
||||
|
||||
def show_cursor
|
||||
window.show_cursor
|
||||
end
|
||||
@@ -24,8 +31,20 @@ module CyberarmEngine
|
||||
window.show_cursor = boolean
|
||||
end
|
||||
|
||||
def draw_rect(x, y, width, height, color, z = 0)
|
||||
Gosu.draw_rect(x, y, width, height, color, z)
|
||||
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)
|
||||
Gosu.draw_rect(x, y, width, height, color, z, mode)
|
||||
end
|
||||
|
||||
def fill(color, z = 0)
|
||||
@@ -33,7 +52,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
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)
|
||||
else
|
||||
Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
|
||||
@@ -41,7 +60,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
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)
|
||||
else
|
||||
Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
|
||||
@@ -70,6 +89,7 @@ module CyberarmEngine
|
||||
else
|
||||
klass.new(path)
|
||||
end
|
||||
|
||||
hash[path] = instance
|
||||
asset = instance
|
||||
end
|
||||
@@ -90,7 +110,19 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def window
|
||||
$window
|
||||
CyberarmEngine::Window.instance
|
||||
end
|
||||
|
||||
def control_down?
|
||||
Gosu.button_down?(Gosu::KB_LEFT_CONTROL) || Gosu.button_down?(Gosu::KB_RIGHT_CONTROL)
|
||||
end
|
||||
|
||||
def shift_down?
|
||||
Gosu.button_down?(Gosu::KB_LEFT_SHIFT) || Gosu.button_down?(Gosu::KB_RIGHT_SHIFT)
|
||||
end
|
||||
|
||||
def alt_down?
|
||||
Gosu.button_down?(Gosu::KB_LEFT_ALT) || Gosu.button_down?(Gosu::KB_RIGHT_ALT)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
248
lib/cyberarm_engine/console.rb
Normal file
248
lib/cyberarm_engine/console.rb
Normal file
@@ -0,0 +1,248 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class Console
|
||||
Z = 100_000
|
||||
PADDING = 2
|
||||
include Common
|
||||
|
||||
attr_reader :text_input
|
||||
|
||||
def initialize(font: Gosu.default_font_name)
|
||||
@text_input = Gosu::TextInput.new
|
||||
@width = window.width / 4 * 3
|
||||
@height = window.height / 4 * 3
|
||||
|
||||
@input = Text.new("", x: 4, y: @height - (PADDING * 2), z: Console::Z + 1, font: font)
|
||||
@input.y -= @input.height
|
||||
|
||||
@history = Text.new("", x: 4, z: Console::Z + 1, font: font, border: true, border_color: Gosu::Color::BLACK)
|
||||
update_history_y
|
||||
|
||||
@command_history = []
|
||||
@command_history_index = 0
|
||||
|
||||
@memory = ""
|
||||
|
||||
@background_color = Gosu::Color.rgba(0, 0, 0, 200)
|
||||
@foreground_color = Gosu::Color.rgba(100, 100, 100, 100)
|
||||
@input_color = Gosu::Color.rgba(100, 100, 100, 200)
|
||||
|
||||
@showing_cursor = false
|
||||
@active_text_input = nil
|
||||
|
||||
@show_caret = true
|
||||
@caret_last_change = Gosu.milliseconds
|
||||
@caret_interval = 250
|
||||
@caret_color = Gosu::Color::WHITE
|
||||
@selection_color = Gosu::Color.new(0x5522ff22)
|
||||
end
|
||||
|
||||
def draw
|
||||
# Background/Border
|
||||
draw_rect(0, 0, @width, @height, @background_color, Console::Z)
|
||||
# Foregound/History
|
||||
draw_rect(PADDING, PADDING, @width - (PADDING * 2), @height - (PADDING * 2), @foreground_color, Console::Z)
|
||||
# Text bar
|
||||
draw_rect(2, @input.y, @width - (PADDING * 2), @input.height, @input_color, Console::Z)
|
||||
|
||||
@history.draw
|
||||
@input.draw
|
||||
# Caret
|
||||
if @show_caret
|
||||
draw_rect(@input.x + caret_from_left, @input.y, Console::PADDING, @input.height, @caret_color, Console::Z + 2)
|
||||
end
|
||||
# Caret selection
|
||||
if caret_start != caret_end
|
||||
if caret_start < @text_input.selection_start
|
||||
draw_rect(@input.x + caret_from_left, @input.y, caret_selection_width, @input.height, @selection_color, Console::Z)
|
||||
else
|
||||
draw_rect((@input.x + caret_from_left) - caret_selection_width, @input.y, caret_selection_width, @input.height, @selection_color, Console::Z)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def caret_from_left
|
||||
return 0 if @text_input.caret_pos.zero?
|
||||
|
||||
@input.textobject.text_width(@text_input.text[0..@text_input.caret_pos - 1])
|
||||
end
|
||||
|
||||
def caret_selection_width
|
||||
@input.textobject.text_width(@text_input.text[caret_start..(caret_end - 1)])
|
||||
end
|
||||
|
||||
def caret_pos
|
||||
@text_input.caret_pos
|
||||
end
|
||||
|
||||
def caret_start
|
||||
@text_input.selection_start < @text_input.caret_pos ? @text_input.selection_start : @text_input.caret_pos
|
||||
end
|
||||
|
||||
def caret_end
|
||||
@text_input.selection_start > @text_input.caret_pos ? @text_input.selection_start : @text_input.caret_pos
|
||||
end
|
||||
|
||||
def update
|
||||
if Gosu.milliseconds - @caret_last_change >= @caret_interval
|
||||
@caret_last_change = Gosu.milliseconds
|
||||
@show_caret = !@show_caret
|
||||
end
|
||||
|
||||
if @width != window.width || @height != @height
|
||||
@width = window.width / 4 * 3
|
||||
@height = window.height / 4 * 3
|
||||
|
||||
@input.y = @height - (PADDING * 2 + @input.height)
|
||||
update_history_y
|
||||
end
|
||||
|
||||
@input.text = @text_input.text
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
case id
|
||||
when Gosu::KB_ENTER, Gosu::KB_RETURN
|
||||
return unless @text_input.text.length.positive?
|
||||
|
||||
@history.text += "\n<c=999999>> #{@text_input.text}</c>"
|
||||
@command_history << @text_input.text
|
||||
@command_history_index = @command_history.size
|
||||
update_history_y
|
||||
handle_command
|
||||
@text_input.text = ""
|
||||
|
||||
when Gosu::KB_UP
|
||||
@command_history_index -= 1
|
||||
@command_history_index = 0 if @command_history_index.negative?
|
||||
@text_input.text = @command_history[@command_history_index]
|
||||
|
||||
when Gosu::KB_DOWN
|
||||
@command_history_index += 1
|
||||
if @command_history_index > @command_history.size - 1
|
||||
@text_input.text = "" unless @command_history_index > @command_history.size
|
||||
@command_history_index = @command_history.size
|
||||
else
|
||||
@text_input.text = @command_history[@command_history_index]
|
||||
end
|
||||
|
||||
when Gosu::KB_TAB
|
||||
split = @text_input.text.split(" ")
|
||||
|
||||
if !@text_input.text.end_with?(" ") && split.size == 1
|
||||
list = abbrev_search(Console::Command.list_commands.map { |cmd| cmd.command.to_s }, @text_input.text)
|
||||
|
||||
if list.size == 1
|
||||
@text_input.text = "#{list.first} "
|
||||
elsif list.size.positive?
|
||||
stdin("\n#{list.map { |cmd| Console::Style.highlight(cmd) }.join(', ')}")
|
||||
end
|
||||
elsif split.size.positive? && cmd = Console::Command.find(split.first)
|
||||
cmd.autocomplete(self)
|
||||
end
|
||||
|
||||
when Gosu::KB_BACKTICK
|
||||
# Remove backtick character from input
|
||||
@text_input.text = if @text_input.text.size > 1
|
||||
@text_input.text[0..@text_input.text.size - 2]
|
||||
else
|
||||
""
|
||||
end
|
||||
|
||||
# Copy
|
||||
when Gosu::KB_C
|
||||
if control_down? && shift_down?
|
||||
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
||||
p @memory
|
||||
elsif control_down?
|
||||
@text_input.text = ""
|
||||
end
|
||||
|
||||
# Paste
|
||||
when Gosu::KB_V
|
||||
if control_down? && shift_down?
|
||||
string = @text_input.text.chars.insert(caret_pos, @memory).join
|
||||
_caret_pos = caret_pos
|
||||
@text_input.text = string
|
||||
@text_input.caret_pos = _caret_pos + @memory.length
|
||||
@text_input.selection_start = _caret_pos + @memory.length
|
||||
end
|
||||
|
||||
# Cut
|
||||
when Gosu::KB_X
|
||||
if control_down? && shift_down?
|
||||
@memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
|
||||
string = @text_input.text.chars
|
||||
Array(caret_start..caret_end - 1).each_with_index do |i, j|
|
||||
string.delete_at(i - j)
|
||||
end
|
||||
|
||||
@text_input.text = string.join
|
||||
end
|
||||
|
||||
# Delete word to left of caret
|
||||
when Gosu::KB_W
|
||||
if control_down?
|
||||
split = @text_input.text.split(" ")
|
||||
split.delete(split.last)
|
||||
@text_input.text = split.join(" ")
|
||||
end
|
||||
|
||||
# Clear history
|
||||
when Gosu::KB_L
|
||||
@history.text = "" if control_down?
|
||||
end
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
end
|
||||
|
||||
def update_history_y
|
||||
@history.y = @height - (PADDING * 2) - @input.height - (@history.text.lines.count * @history.textobject.height)
|
||||
end
|
||||
|
||||
def handle_command
|
||||
string = @text_input.text
|
||||
split = string.split(" ")
|
||||
command = split.first
|
||||
arguments = split.length.positive? ? split[1..split.length - 1] : []
|
||||
|
||||
CyberarmEngine::Console::Command.use(command, arguments, self)
|
||||
end
|
||||
|
||||
def abbrev_search(array, text)
|
||||
return [] unless text.length.positive?
|
||||
|
||||
list = []
|
||||
Abbrev.abbrev(array).each do |abbrev, value|
|
||||
next unless abbrev&.start_with?(text)
|
||||
|
||||
list << value
|
||||
end
|
||||
|
||||
list.uniq
|
||||
end
|
||||
|
||||
def stdin(string)
|
||||
@history.text += "\n#{string}"
|
||||
update_history_y
|
||||
end
|
||||
|
||||
def focus
|
||||
@active_text_input = window.text_input
|
||||
window.text_input = @text_input
|
||||
|
||||
@showing_cursor = window.needs_cursor
|
||||
window.needs_cursor = true
|
||||
|
||||
@show_caret = true
|
||||
@caret_last_change = Gosu.milliseconds
|
||||
end
|
||||
|
||||
def blur
|
||||
window.text_input = @active_text_input
|
||||
window.needs_cursor = @showing_cursor
|
||||
end
|
||||
end
|
||||
end
|
||||
158
lib/cyberarm_engine/console/command.rb
Normal file
158
lib/cyberarm_engine/console/command.rb
Normal file
@@ -0,0 +1,158 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class Console
|
||||
module Style
|
||||
def self.error(string)
|
||||
"<c=ff5555>#{string}</c>"
|
||||
end
|
||||
|
||||
def self.warn(string)
|
||||
"<c=ff7700>#{string}</c>"
|
||||
end
|
||||
|
||||
def self.notice(string)
|
||||
"<c=55ff55>#{string}</c>"
|
||||
end
|
||||
|
||||
def self.highlight(string, color = "5555ff")
|
||||
"<c=#{color}>#{string}</c>"
|
||||
end
|
||||
end
|
||||
|
||||
class Command
|
||||
def self.inherited(subclass)
|
||||
@list ||= []
|
||||
@commands ||= []
|
||||
@list << subclass
|
||||
end
|
||||
|
||||
def self.setup
|
||||
@list ||= []
|
||||
@commands = []
|
||||
@list.each do |subclass|
|
||||
cmd = subclass.new
|
||||
if @commands.detect { |c| c.command == cmd.command }
|
||||
raise "Command '#{cmd.command}' from '#{cmd.class}' already exists!"
|
||||
end
|
||||
|
||||
@commands << cmd
|
||||
end
|
||||
end
|
||||
|
||||
def self.use(command, arguments, console)
|
||||
found_command = @commands.detect { |cmd| cmd.command == command.to_sym }
|
||||
|
||||
if found_command
|
||||
found_command.handle(arguments, console)
|
||||
else
|
||||
console.stdin("Command #{Style.error(command)} not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def self.find(command)
|
||||
@commands.detect { |cmd| cmd.command == command.to_sym }
|
||||
end
|
||||
|
||||
def self.list_commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def initialize
|
||||
@store = {}
|
||||
@subcommands = []
|
||||
|
||||
setup
|
||||
end
|
||||
|
||||
def setup
|
||||
end
|
||||
|
||||
def subcommand(command, type)
|
||||
if @subcommands.detect { |subcmd| subcmd.command == command.to_sym }
|
||||
raise "Subcommand '#{command}' for '#{self.command}' already exists!"
|
||||
end
|
||||
|
||||
@subcommands << SubCommand.new(self, command, type)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
@store[key]
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
@store[key] = value
|
||||
end
|
||||
|
||||
def group
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def command
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def autocomplete(console)
|
||||
split = console.text_input.text.split(" ")
|
||||
|
||||
if @subcommands.size.positive?
|
||||
if !console.text_input.text.end_with?(" ") && split.size == 2
|
||||
list = console.abbrev_search(@subcommands.map { |cmd| cmd.command.to_s }, split.last)
|
||||
|
||||
if list.size == 1
|
||||
console.text_input.text = "#{split.first} #{list.first} "
|
||||
else
|
||||
return unless list.size.positive?
|
||||
|
||||
console.stdin(list.map { |cmd| Console::Style.highlight(cmd) }.join(", ").to_s)
|
||||
end
|
||||
|
||||
# List available options on subcommand
|
||||
elsif (console.text_input.text.end_with?(" ") && split.size == 2) || !console.text_input.text.end_with?(" ") && split.size == 3
|
||||
subcommand = @subcommands.detect { |cmd| cmd.command.to_s == (split[1]) }
|
||||
|
||||
if subcommand
|
||||
if split.size == 2
|
||||
console.stdin("Available options: #{subcommand.values.map { |value| Console::Style.highlight(value) }.join(',')}")
|
||||
else
|
||||
list = console.abbrev_search(subcommand.values, split.last)
|
||||
if list.size == 1
|
||||
console.text_input.text = "#{split.first} #{split[1]} #{list.first} "
|
||||
elsif list.size.positive?
|
||||
console.stdin("Available options: #{list.map { |value| Console::Style.highlight(value) }.join(',')}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# List available subcommands if command was entered and has only a space after it
|
||||
elsif console.text_input.text.end_with?(" ") && split.size == 1
|
||||
console.stdin("Available subcommands: #{@subcommands.map { |cmd| Console::Style.highlight(cmd.command) }.join(', ')}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def handle_subcommand(arguments, console)
|
||||
if arguments.size.zero?
|
||||
console.stdin(usage)
|
||||
return
|
||||
end
|
||||
subcommand = arguments.delete_at(0)
|
||||
|
||||
found_command = @subcommands.detect { |cmd| cmd.command == subcommand.to_sym }
|
||||
if found_command
|
||||
found_command.handle(arguments, console)
|
||||
else
|
||||
console.stdin("Unknown subcommand #{Style.error(subcommand)} for #{Style.highlight(command)}")
|
||||
end
|
||||
end
|
||||
|
||||
def usage
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
43
lib/cyberarm_engine/console/commands/help_command.rb
Normal file
43
lib/cyberarm_engine/console/commands/help_command.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class Console
|
||||
class HelpCommand < CyberarmEngine::Console::Command
|
||||
def group
|
||||
:global
|
||||
end
|
||||
|
||||
def command
|
||||
:help
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
console.stdin(usage(arguments.first))
|
||||
end
|
||||
|
||||
def autocomplete(console)
|
||||
split = console.text_input.text.split(" ")
|
||||
if !console.text_input.text.start_with?(" ") && split.size == 2
|
||||
list = console.abbrev_search(Command.list_commands.map { |cmd| cmd.command.to_s }, split.last)
|
||||
if list.size == 1
|
||||
console.text_input.text = "#{split.first} #{list.first} "
|
||||
elsif list.size > 1
|
||||
console.stdin(list.map { |cmd| Style.highlight(cmd) }.join(", "))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def usage(command = nil)
|
||||
if command
|
||||
if cmd = Command.find(command)
|
||||
cmd.usage
|
||||
else
|
||||
"#{Style.error(command)} is not a command"
|
||||
end
|
||||
else
|
||||
"Available commands:\n#{Command.list_commands.map { |cmd| Style.highlight(cmd.command).to_s }.join(', ')}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
100
lib/cyberarm_engine/console/subcommand.rb
Normal file
100
lib/cyberarm_engine/console/subcommand.rb
Normal file
@@ -0,0 +1,100 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class Console
|
||||
class Command
|
||||
class SubCommand
|
||||
def initialize(parent, command, type)
|
||||
@parent = parent
|
||||
@command = command
|
||||
@type = type
|
||||
end
|
||||
|
||||
attr_reader :command
|
||||
|
||||
def handle(arguments, console)
|
||||
if arguments.size > 1
|
||||
console.stdin("to many arguments for #{Style.highlight(command.to_s)}, got #{Style.error(arguments.size)} expected #{Style.notice(1)}.")
|
||||
return
|
||||
end
|
||||
|
||||
case @type
|
||||
when :boolean
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) || false
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
when "on"
|
||||
var = @parent.set(command.to_sym, true)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
when "off"
|
||||
var = @parent.set(command.to_sym, false)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
else
|
||||
console.stdin("Invalid argument for #{Style.highlight(command.to_s)}, got #{Style.error(arguments.last)} expected #{Style.notice('on')}, or #{Style.notice('off')}.")
|
||||
end
|
||||
when :string
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) || "\"\""
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
var = @parent.set(command.to_sym, arguments.last)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
end
|
||||
when :integer
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) || "nil"
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
begin
|
||||
var = @parent.set(command.to_sym, Integer(arguments.last))
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
rescue ArgumentError
|
||||
console.stdin("Error: #{Style.error("Expected an integer, got '#{arguments.last}'")}")
|
||||
end
|
||||
end
|
||||
when :decimal
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) || "nil"
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
begin
|
||||
var = @parent.set(command.to_sym, Float(arguments.last))
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
rescue ArgumentError
|
||||
console.stdin("Error: #{Style.error("Expected a decimal or integer, got '#{arguments.last}'")}")
|
||||
end
|
||||
end
|
||||
else
|
||||
raise RuntimeError
|
||||
end
|
||||
end
|
||||
|
||||
def values
|
||||
case @type
|
||||
when :boolean
|
||||
%w[on off]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def usage
|
||||
case @type
|
||||
when :boolean
|
||||
"#{Style.highlight(command)} #{Style.notice('[on|off]')}"
|
||||
when :string
|
||||
"#{Style.highlight(command)} #{Style.notice('[string]')}"
|
||||
when :integer
|
||||
"#{Style.highlight(command)} #{Style.notice('[0]')}"
|
||||
when :decimal
|
||||
"#{Style.highlight(command)} #{Style.notice('[0.0]')}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,7 +7,7 @@ module CyberarmEngine
|
||||
attr_reader :alpha
|
||||
|
||||
def initialize(options = {})
|
||||
$window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil?
|
||||
window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil?
|
||||
|
||||
@options = options
|
||||
@image = options[:image] ? image(options[:image]) : nil
|
||||
@@ -42,7 +42,7 @@ module CyberarmEngine
|
||||
@radius = if options[:radius]
|
||||
options[:radius]
|
||||
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
|
||||
@@ -55,9 +55,9 @@ module CyberarmEngine
|
||||
|
||||
if $debug
|
||||
show_debug_heading
|
||||
$window.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
|
||||
Gosu.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
|
||||
if @debug_text.text != ""
|
||||
$window.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20,
|
||||
Gosu.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20,
|
||||
Gosu::Color.rgba(0, 0, 0, 200), 9999)
|
||||
@debug_text.draw
|
||||
end
|
||||
@@ -102,13 +102,13 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def _x_visible
|
||||
x.between?(($window.width / 2) - @world_center_point.x, ($window.width / 2) + @world_center_point.x) ||
|
||||
x.between?((@world_center_point.x - $window.width / 2), ($window.width / 2) + @world_center_point.x)
|
||||
x.between?((window.width / 2) - @world_center_point.x, (window.width / 2) + @world_center_point.x) ||
|
||||
x.between?((@world_center_point.x - window.width / 2), (window.width / 2) + @world_center_point.x)
|
||||
end
|
||||
|
||||
def _y_visible
|
||||
y.between?(($window.height / 2) - @world_center_point.y, ($window.height / 2) + @world_center_point.y) ||
|
||||
y.between?(@world_center_point.y - ($window.height / 2), ($window.height / 2) + @world_center_point.y)
|
||||
y.between?((window.height / 2) - @world_center_point.y, (window.height / 2) + @world_center_point.y) ||
|
||||
y.between?(@world_center_point.y - (window.height / 2), (window.height / 2) + @world_center_point.y)
|
||||
end
|
||||
|
||||
def heading(ahead_by = 100, _object = nil, angle_only = false)
|
||||
@@ -153,10 +153,6 @@ module CyberarmEngine
|
||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
||||
end
|
||||
|
||||
def draw_rect(x, y, width, height, color, z = 0)
|
||||
$window.draw_rect(x, y, width, height, color, z)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
end
|
||||
|
||||
@@ -190,12 +186,12 @@ module CyberarmEngine
|
||||
# Duplication... so DRY.
|
||||
def each_circle_collision(object, _resolve_with = :width, &block)
|
||||
if object.class != Class && object.instance_of?(object.class)
|
||||
$window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o|
|
||||
window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o|
|
||||
distance = Gosu.distance(x, y, object.x, object.y)
|
||||
block.call(o, object) if distance <= radius + object.radius && block
|
||||
end
|
||||
else
|
||||
list = $window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
list = window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
list.each do |o|
|
||||
next if self == o
|
||||
|
||||
@@ -206,9 +202,9 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def destroy
|
||||
if $window.current_state
|
||||
$window.current_state.game_objects.each do |o|
|
||||
$window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self
|
||||
if window.current_state
|
||||
window.current_state.game_objects.each do |o|
|
||||
window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -220,13 +216,13 @@ module CyberarmEngine
|
||||
|
||||
def self.each_circle_collision(object, _resolve_with = :width, &block)
|
||||
if object.class != Class && object.instance_of?(object.class)
|
||||
$window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o|
|
||||
window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o|
|
||||
distance = Gosu.distance(o.x, o.y, object.x, object.y)
|
||||
block.call(o, object) if distance <= o.radius + object.radius && block
|
||||
end
|
||||
else
|
||||
lista = $window.current_state.game_objects.select { |i| i.instance_of?(self) }
|
||||
listb = $window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
lista = window.current_state.game_objects.select { |i| i.instance_of?(self) }
|
||||
listb = window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
lista.product(listb).each do |o, o2|
|
||||
next if o == o2
|
||||
|
||||
@@ -238,9 +234,9 @@ module CyberarmEngine
|
||||
|
||||
def self.destroy_all
|
||||
INSTANCES.clear
|
||||
if $window.current_state
|
||||
$window.current_state.game_objects.each do |o|
|
||||
$window.current_state.game_objects.delete(o) if o.is_a?(self.class)
|
||||
if window.current_state
|
||||
window.current_state.game_objects.each do |o|
|
||||
window.current_state.game_objects.delete(o) if o.is_a?(self.class)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ module CyberarmEngine
|
||||
@options = options
|
||||
@game_objects = []
|
||||
@global_pause = false
|
||||
$window.text_input = nil unless options[:preserve_text_input]
|
||||
window.text_input = nil unless options[:preserve_text_input]
|
||||
|
||||
@down_keys = {}
|
||||
end
|
||||
@@ -17,6 +17,11 @@ module CyberarmEngine
|
||||
def setup
|
||||
end
|
||||
|
||||
# Called immediately after setup returns.
|
||||
# GuiState uses this to set current_theme for ToolTip
|
||||
def post_setup
|
||||
end
|
||||
|
||||
def draw
|
||||
@game_objects.each(&:draw)
|
||||
end
|
||||
@@ -25,48 +30,27 @@ module CyberarmEngine
|
||||
@game_objects.each(&:update)
|
||||
end
|
||||
|
||||
def draw_bounding_box(box)
|
||||
x = box.x
|
||||
y = box.y
|
||||
max_x = box.max_x
|
||||
max_y = box.max_y
|
||||
|
||||
color = Gosu::Color.rgba(255, 127, 64, 240)
|
||||
|
||||
# pipe = 4
|
||||
# Gosu.draw_rect(x-width, y-height, x+(width*2), y+(height*2), color, Float::INFINITY)
|
||||
# puts "BB render: #{x}:#{y} w:#{x.abs+width} h:#{y.abs+height}"
|
||||
# Gosu.draw_rect(x, y, x.abs+width, y.abs+height, color, Float::INFINITY)
|
||||
|
||||
# TOP LEFT to BOTTOM LEFT
|
||||
$window.draw_line(
|
||||
x, y, color,
|
||||
x, max_y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# BOTTOM LEFT to BOTTOM RIGHT
|
||||
$window.draw_line(
|
||||
x, max_y, color,
|
||||
max_x, max_y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# BOTTOM RIGHT to TOP RIGHT
|
||||
$window.draw_line(
|
||||
max_x, max_y, color,
|
||||
max_x, y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# TOP RIGHT to TOP LEFT
|
||||
$window.draw_line(
|
||||
max_x, y, color,
|
||||
x, y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
def needs_redraw?
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
@options.clear
|
||||
@game_objects.clear
|
||||
def needs_repaint?
|
||||
true
|
||||
end
|
||||
|
||||
def drop(filename)
|
||||
end
|
||||
|
||||
def gamepad_connected(index)
|
||||
end
|
||||
|
||||
def gamepad_disconnected(index)
|
||||
end
|
||||
|
||||
def gain_focus
|
||||
end
|
||||
|
||||
def lose_focus
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
@@ -85,6 +69,54 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
def close
|
||||
window.close!
|
||||
end
|
||||
|
||||
def draw_bounding_box(box)
|
||||
x = box.x
|
||||
y = box.y
|
||||
max_x = box.max_x
|
||||
max_y = box.max_y
|
||||
|
||||
color = Gosu::Color.rgba(255, 127, 64, 240)
|
||||
|
||||
# pipe = 4
|
||||
# Gosu.draw_rect(x-width, y-height, x+(width*2), y+(height*2), color, Float::INFINITY)
|
||||
# puts "BB render: #{x}:#{y} w:#{x.abs+width} h:#{y.abs+height}"
|
||||
# Gosu.draw_rect(x, y, x.abs+width, y.abs+height, color, Float::INFINITY)
|
||||
|
||||
# TOP LEFT to BOTTOM LEFT
|
||||
Gosu.draw_line(
|
||||
x, y, color,
|
||||
x, max_y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# BOTTOM LEFT to BOTTOM RIGHT
|
||||
Gosu.draw_line(
|
||||
x, max_y, color,
|
||||
max_x, max_y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# BOTTOM RIGHT to TOP RIGHT
|
||||
Gosu.draw_line(
|
||||
max_x, max_y, color,
|
||||
max_x, y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
# TOP RIGHT to TOP LEFT
|
||||
Gosu.draw_line(
|
||||
max_x, y, color,
|
||||
x, y, color,
|
||||
Float::INFINITY
|
||||
)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@options.clear
|
||||
@game_objects.clear
|
||||
end
|
||||
|
||||
def add_game_object(object)
|
||||
@game_objects << object
|
||||
end
|
||||
|
||||
@@ -3,7 +3,8 @@ module CyberarmEngine
|
||||
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
||||
:current_material, :current_object, :vertex_count, :smoothing
|
||||
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
||||
:normals_buffer_id, :uvs_buffer_id, :textures_buffer_id, :vertex_array_id, :aabb_tree
|
||||
:normals_buffer_id, :uvs_buffer_id, :textures_buffer_id, :vertex_array_id, :aabb_tree,
|
||||
:vertices_count
|
||||
|
||||
def initialize(file_path:)
|
||||
@file_path = file_path
|
||||
@@ -23,6 +24,8 @@ module CyberarmEngine
|
||||
@bones = []
|
||||
@smoothing = 0
|
||||
|
||||
@vertices_count = 0
|
||||
|
||||
@bounding_box = BoundingBox.new
|
||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||
|
||||
@@ -32,6 +35,8 @@ module CyberarmEngine
|
||||
|
||||
parse(parser)
|
||||
|
||||
@vertices_count = @vertices.size
|
||||
|
||||
@has_texture = false
|
||||
|
||||
@materials.each do |_key, material|
|
||||
@@ -44,8 +49,9 @@ module CyberarmEngine
|
||||
|
||||
@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
|
||||
# puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
||||
end
|
||||
|
||||
def parse(parser)
|
||||
@@ -145,24 +151,24 @@ module CyberarmEngine
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
||||
gl_error?
|
||||
|
||||
# inPosition
|
||||
# in_position
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
# colors
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
|
||||
# inColor
|
||||
# in_color
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
# normals
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
|
||||
# inNormal
|
||||
# in_normal
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
|
||||
if has_texture?
|
||||
# uvs
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||
# inUV
|
||||
# in_uv
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
end
|
||||
@@ -172,7 +178,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def build_collision_tree
|
||||
@aabb_tree = AABBTree.new
|
||||
@aabb_tree = IMICFPS::AABBTree.new
|
||||
|
||||
@faces.each do |face|
|
||||
box = BoundingBox.new
|
||||
|
||||
@@ -11,7 +11,19 @@ module CyberarmEngine
|
||||
if e != GL_NO_ERROR
|
||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||
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
|
||||
|
||||
@@ -3,8 +3,8 @@ module CyberarmEngine
|
||||
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
||||
:min_view_distance, :max_view_distance
|
||||
|
||||
def initialize(position:, aspect_ratio:, orientation: Vector.new(0, 0,
|
||||
0), field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 155.0)
|
||||
def initialize(position:, aspect_ratio:, orientation: Vector.new(0, 0, 0),
|
||||
field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 1024.0)
|
||||
@position = position
|
||||
@orientation = orientation
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module CyberarmEngine
|
||||
class GBuffer
|
||||
attr_reader :screen_vbo, :vertices, :uvs
|
||||
attr_reader :width, :height
|
||||
|
||||
def initialize(width:, height:)
|
||||
@width = width
|
||||
|
||||
@@ -3,12 +3,15 @@ module CyberarmEngine
|
||||
@@immediate_mode_warning = false
|
||||
|
||||
attr_accessor :show_wireframe
|
||||
attr_reader :number_of_vertices
|
||||
|
||||
def initialize(width:, height:, show_wireframe: false)
|
||||
@width = width
|
||||
@height = height
|
||||
@show_wireframe = show_wireframe
|
||||
|
||||
@number_of_vertices = 0
|
||||
|
||||
@g_buffer = GBuffer.new(width: @width, height: @height)
|
||||
end
|
||||
|
||||
@@ -20,6 +23,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def render(camera, lights, entities)
|
||||
@number_of_vertices = 0
|
||||
|
||||
glViewport(0, 0, @width, @height)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
|
||||
@@ -39,11 +44,13 @@ module CyberarmEngine
|
||||
shader.uniform_transform("projection", camera.projection_matrix)
|
||||
shader.uniform_transform("view", camera.view_matrix)
|
||||
shader.uniform_transform("model", entity.model_matrix)
|
||||
shader.uniform_vec3("cameraPosition", camera.position)
|
||||
shader.uniform_vector3("camera_position", camera.position)
|
||||
|
||||
gl_error?
|
||||
draw_model(entity.model, shader)
|
||||
entity.draw
|
||||
|
||||
@number_of_vertices += entity.model.vertices_count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -90,6 +97,8 @@ module CyberarmEngine
|
||||
draw_mesh(entity.model)
|
||||
entity.draw
|
||||
glPopMatrix
|
||||
|
||||
@number_of_vertices += entity.model.vertices_count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -145,15 +154,21 @@ module CyberarmEngine
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
||||
shader.uniform_integer("depth", 4)
|
||||
|
||||
lights.each_with_index do |light, _i|
|
||||
shader.uniform_integer("light[0].type", light.type)
|
||||
shader.uniform_vec3("light[0].direction", light.direction)
|
||||
shader.uniform_vec3("light[0].position", light.position)
|
||||
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
||||
shader.uniform_vec3("light[0].ambient", light.ambient)
|
||||
shader.uniform_vec3("light[0].specular", light.specular)
|
||||
# FIXME: Try to figure out how to up this to 32 and/or beyond
|
||||
# (currently fails with more then 7 lights passed in to shader)
|
||||
lights.each_slice(7).each do |light_group|
|
||||
light_group.each_with_index do |light, _i|
|
||||
shader.uniform_integer("light_count", light_group.size)
|
||||
|
||||
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
|
||||
|
||||
glBindVertexArray(0)
|
||||
@@ -206,7 +221,7 @@ module CyberarmEngine
|
||||
|
||||
offset = 0
|
||||
model.objects.each do |object|
|
||||
shader.uniform_boolean("hasTexture", object.has_texture?)
|
||||
shader.uniform_boolean("has_texture", object.has_texture?)
|
||||
|
||||
if object.has_texture?
|
||||
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
||||
|
||||
@@ -4,12 +4,23 @@ module CyberarmEngine
|
||||
|
||||
def initialize
|
||||
@bounding_box_renderer = BoundingBoxRenderer.new
|
||||
@opengl_renderer = OpenGLRenderer.new(width: $window.width, height: $window.height)
|
||||
@opengl_renderer = OpenGLRenderer.new(width: CyberarmEngine::Window.instance.width, height: CyberarmEngine::Window.instance.height)
|
||||
end
|
||||
|
||||
def draw(camera, lights, entities)
|
||||
Stats.frame.start_timing(:opengl_renderer)
|
||||
|
||||
Stats.frame.start_timing(:opengl_model_renderer)
|
||||
@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
|
||||
|
||||
def canvas_size_changed
|
||||
|
||||
@@ -385,7 +385,7 @@ module CyberarmEngine
|
||||
# @param value [Vector]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_vec3(variable, value, location = nil)
|
||||
def uniform_vector3(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform3f(attr_loc, *value.to_a[0..2])
|
||||
@@ -397,7 +397,7 @@ module CyberarmEngine
|
||||
# @param value [Vector]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_vec4(variable, value, location = nil)
|
||||
def uniform_vector4(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform4f(attr_loc, *value.to_a)
|
||||
|
||||
@@ -54,7 +54,7 @@ module CyberarmEngine
|
||||
texture_id = tex_names_buf.unpack1("L2")
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) if @retro
|
||||
|
||||
@@ -1,20 +1,191 @@
|
||||
module CyberarmEngine
|
||||
class Stats
|
||||
@@hash = {
|
||||
gui_recalculations_last_frame: 0
|
||||
}
|
||||
@frames = []
|
||||
@frame_index = -1
|
||||
@max_frame_history = 1024
|
||||
|
||||
def self.get(key)
|
||||
@@hash.dig(key)
|
||||
def self.new_frame
|
||||
if @frames.size < @max_frame_history
|
||||
@frames << Frame.new
|
||||
else
|
||||
@frames[@frame_index] = Frame.new
|
||||
end
|
||||
end
|
||||
|
||||
def self.increment(key, n)
|
||||
@@hash[key] += n
|
||||
def self.frame
|
||||
@frames[@frame_index]
|
||||
end
|
||||
|
||||
def self.clear
|
||||
@@hash.each do |key, _value|
|
||||
@@hash[key] = 0
|
||||
def self.end_frame
|
||||
frame&.complete
|
||||
|
||||
@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)
|
||||
@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)
|
||||
@min_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @height - (@text_size + @padding / 2), z: z, size: @text_size, border: true)
|
||||
|
||||
@timings_label = CyberarmEngine::Text.new("", x: x + @padding + @width + @padding, y: y + @padding, z: z, size: @text_size, border: 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
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
def draw
|
||||
@frame_stats = CyberarmEngine::Stats.frames.select(&:complete?)
|
||||
return if @frame_stats.empty?
|
||||
|
||||
calculate_graphs
|
||||
|
||||
@max_timing_label.text = "Max: #{@frame_stats.map { |f| f.frame_timing.duration }.max.to_s.rjust(3, " ")}ms"
|
||||
@avg_timing_label.text = "Avg: #{(@frame_stats.map { |f| f.frame_timing.duration }.sum / @frame_stats.size).to_s.rjust(3, " ")}ms"
|
||||
@min_timing_label.text = "Min: #{@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
|
||||
end
|
||||
|
||||
def draw_graphs
|
||||
Gosu.draw_path(@graphs[:frame_timings], Gosu::Color::WHITE, Float::INFINITY)
|
||||
end
|
||||
|
||||
def draw_timings
|
||||
frame = @frame_stats.last
|
||||
|
||||
@timings_label.text = "#{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(@timings_label.x - @padding, @timings_label.y - @padding, @timings_label.width + @padding * 2, @timings_label.height + @padding * 2, 0xdd_222222, @position.z)
|
||||
@timings_label.draw
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,9 @@ module CyberarmEngine
|
||||
CACHE = {}
|
||||
|
||||
attr_accessor :x, :y, :z, :size, :options
|
||||
attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||
attr_reader :text, :textobject, :factor_x, :factor_y, :color,
|
||||
:border, :border_size, :border_alpha, :border_color,
|
||||
:shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||
|
||||
def initialize(text, options = {})
|
||||
@text = text.to_s || ""
|
||||
@@ -15,16 +17,27 @@ module CyberarmEngine
|
||||
@z = options[:z] || 1025
|
||||
@factor_x = options[:factor_x] || 1
|
||||
@factor_y = options[:factor_y] || 1
|
||||
@color = options[:color] || Gosu::Color::WHITE
|
||||
@mode = options[:mode] || :default
|
||||
if options[:color]
|
||||
@color = options[:color].is_a?(Gosu::Color) ? options[:color] : Gosu::Color.new(options[:color])
|
||||
else
|
||||
@color = Gosu::Color::WHITE
|
||||
end
|
||||
@mode = options[:mode] || :default
|
||||
@alignment = options[:alignment] || nil
|
||||
@shadow = true if options[:shadow] == true
|
||||
@shadow = false if options[:shadow] == false
|
||||
@shadow = true if options[:shadow].nil?
|
||||
@shadow_size = options[:shadow_size] || 1
|
||||
|
||||
@border = options[:border]
|
||||
@border = true if options[:border].nil?
|
||||
@border_size = options[:border_size] || 1
|
||||
@border_alpha = options[:border_alpha] || 30
|
||||
@border_color = options[:border_color] || Gosu::Color::BLACK
|
||||
|
||||
@shadow = options[:shadow]
|
||||
@shadow_size = options[:shadow_size] || 2
|
||||
@shadow_alpha = options[:shadow_alpha] || 30
|
||||
@shadow_alpha = options[:shadow_alpha] || 30
|
||||
@shadow_color = options[:shadow_color]
|
||||
@shadow_color = options[:shadow_color] || Gosu::Color::BLACK
|
||||
|
||||
@static = options[:static] || (options[:static].nil? || options[:static] == false ? false : true)
|
||||
|
||||
@textobject = check_cache(@size, @font)
|
||||
|
||||
if @alignment
|
||||
@@ -32,13 +45,11 @@ module CyberarmEngine
|
||||
when :left
|
||||
@x = 0 + BUTTON_PADDING
|
||||
when :center
|
||||
@x = ($window.width / 2) - (@textobject.text_width(@text) / 2)
|
||||
@x = (CyberarmEngine::Window.instance.width / 2) - (@textobject.text_width(@text) / 2)
|
||||
when :right
|
||||
@x = $window.width - BUTTON_PADDING - @textobject.text_width(@text)
|
||||
@x = CyberarmEngine::Window.instance.width - BUTTON_PADDING - @textobject.text_width(@text)
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
def check_cache(size, font_name)
|
||||
@@ -65,85 +76,168 @@ module CyberarmEngine
|
||||
font
|
||||
end
|
||||
|
||||
def swap_font(size, font_name = @font)
|
||||
if @size != size || @font != font_name
|
||||
@size = size
|
||||
@font = font_name
|
||||
|
||||
invalidate_cache!
|
||||
@textobject = check_cache(size, font_name)
|
||||
end
|
||||
end
|
||||
|
||||
def text=(string)
|
||||
@rendered_shadow = nil
|
||||
invalidate_cache! if @text != string
|
||||
@text = string
|
||||
end
|
||||
|
||||
def factor_x=(n)
|
||||
@rendered_shadow = nil
|
||||
invalidate_cache! if @factor_x != n
|
||||
@factor_x = n
|
||||
end
|
||||
|
||||
def factor_y=(n)
|
||||
@rendered_shadow = nil
|
||||
invalidate_cache! if @factor_y != n
|
||||
@factor_y = n
|
||||
end
|
||||
|
||||
def color=(color)
|
||||
@rendered_shadow = nil
|
||||
@color = color
|
||||
old_color = @color
|
||||
|
||||
if color
|
||||
@color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color)
|
||||
else
|
||||
raise "color cannot be nil"
|
||||
end
|
||||
|
||||
invalidate_cache! if old_color != color
|
||||
end
|
||||
|
||||
def shadow=(boolean)
|
||||
@rendered_shadow = nil
|
||||
@shadow = boolean
|
||||
def border=(boolean)
|
||||
invalidate_cache! if @border != boolean
|
||||
@border = boolean
|
||||
end
|
||||
|
||||
def shadow_size=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_size = n
|
||||
def border_size=(n)
|
||||
invalidate_cache! if @border_size != n
|
||||
@border_size = n
|
||||
end
|
||||
|
||||
def shadow_alpha=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_alpha = n
|
||||
def border_alpha=(n)
|
||||
invalidate_cache! if @border_alpha != n
|
||||
@border_alpha = n
|
||||
end
|
||||
|
||||
def shadow_color=(n)
|
||||
@rendered_shadow = nil
|
||||
@shadow_color = n
|
||||
def border_color=(n)
|
||||
invalidate_cache! if @border_color != n
|
||||
@border_color = n
|
||||
end
|
||||
|
||||
def width(text = @text)
|
||||
textobject.text_width(text)
|
||||
markup_width(text)
|
||||
end
|
||||
|
||||
def text_width(text = @text)
|
||||
spacing = 0
|
||||
spacing += @border_size if @border
|
||||
spacing += @shadow_size if @shadow
|
||||
|
||||
if text == @text && @static && @gosu_cached_text_image
|
||||
@gosu_cached_text_image&.width + spacing
|
||||
else
|
||||
textobject.text_width(text) + spacing
|
||||
end
|
||||
end
|
||||
|
||||
def markup_width(text = @text)
|
||||
textobject.markup_width(text)
|
||||
text = text.to_s
|
||||
|
||||
spacing = 0
|
||||
spacing += @border_size if @border
|
||||
spacing += @shadow_size if @shadow
|
||||
|
||||
if text == @text && @static && @gosu_cached_text_image
|
||||
@gosu_cached_text_image&.width + spacing
|
||||
else
|
||||
textobject.markup_width(text) + spacing
|
||||
end
|
||||
end
|
||||
|
||||
def height(text = @text)
|
||||
text.lines.count > 0 ? text.lines.count * textobject.height : @textobject.height
|
||||
if text.lines.count > 0
|
||||
text.lines.count * textobject.height + @border_size + @shadow_size
|
||||
else
|
||||
@textobject.height + @border_size + @shadow_size
|
||||
end
|
||||
end
|
||||
|
||||
def draw(method = :draw_markup)
|
||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||
shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
|
||||
shadow_alpha)
|
||||
white = Gosu::Color::WHITE
|
||||
if @static
|
||||
if @border && !@cached_text_border_image
|
||||
_x = @border_size
|
||||
_y = @border_size
|
||||
_width = method == :draw_markup ? text_width : markup_width
|
||||
img = Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
|
||||
|
||||
_x = @shadow_size
|
||||
_y = @shadow_size
|
||||
@cached_text_border_image = Gosu.render((_width + (@border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
|
||||
img.draw(-_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(-_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
|
||||
@rendered_shadow ||= Gosu.render((width + (shadow_size * 2)).ceil, (height + (@shadow_size * 2)).ceil) do
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
img.draw(0, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
|
||||
@textobject.send(method, @text, _x, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
img.draw(_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
|
||||
@textobject.send(method, @text, _x, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
img.draw(0, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(-_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
end
|
||||
end
|
||||
@rendered_shadow.draw(@x - @shadow_size, @y - @shadow_size, @z, @factor_x, @factor_y, shadow_color)
|
||||
end
|
||||
|
||||
@textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode)
|
||||
@cached_text_shadow_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) if @shadow
|
||||
|
||||
@gosu_cached_text_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
|
||||
|
||||
@cached_text_border_image.draw(@x, @y, @z, @factor_x, @factor_y, @border_color, @mode) if @border
|
||||
|
||||
@cached_text_shadow_image.draw(@x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, @shadow_color, @mode) if @shadow
|
||||
|
||||
@gosu_cached_text_image.draw(@x, @y, @z, @factor_x, @factor_y, @color, @mode)
|
||||
else
|
||||
if @border && !ARGV.join.include?("--no-border")
|
||||
border_alpha = @color.alpha <= 30 ? @color.alpha : @border_alpha
|
||||
border_color = @border_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
|
||||
border_alpha)
|
||||
white = Gosu::Color::WHITE
|
||||
|
||||
_x = @border_size
|
||||
_y = @border_size
|
||||
_width = method == :draw_markup ? text_width : markup_width
|
||||
|
||||
@cached_text_border_image ||= Gosu.render((_width + (border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
|
||||
@textobject.send(method, @text, _x - @border_size, _y, @z, @factor_x, @factor_y, white, @mode)
|
||||
@textobject.send(method, @text, _x - @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
|
||||
@textobject.send(method, @text, _x, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
@textobject.send(method, @text, _x + @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
|
||||
@textobject.send(method, @text, _x, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
@textobject.send(method, @text, _x - @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
|
||||
@textobject.send(method, @text, _x + @border_size, _y, @z, @factor_x, @factor_y, white, @mode)
|
||||
@textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
end
|
||||
|
||||
@cached_text_border_image.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color)
|
||||
end
|
||||
|
||||
if @shadow
|
||||
shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
|
||||
@textobject.send(method, @text, @x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, shadow_color, @mode)
|
||||
end
|
||||
|
||||
@textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode)
|
||||
end
|
||||
end
|
||||
|
||||
def alpha=(n)
|
||||
@@ -156,5 +250,11 @@ module CyberarmEngine
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def invalidate_cache!
|
||||
@cached_text_border_image = nil
|
||||
@cached_text_shadow_image = nil
|
||||
@gosu_cached_text_image = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,11 +62,11 @@ module CyberarmEngine
|
||||
|
||||
def update
|
||||
# TOP
|
||||
@top.x = @element.x # + @element.border_thickness_left
|
||||
@top.x = @element.x + @element.style.border_thickness_left
|
||||
@top.y = @element.y
|
||||
@top.z = @element.z
|
||||
|
||||
@top.width = @element.width
|
||||
@top.width = @element.width - @element.style.border_thickness_left
|
||||
@top.height = @element.style.border_thickness_top
|
||||
|
||||
# RIGHT
|
||||
|
||||
@@ -23,7 +23,8 @@ module CyberarmEngine
|
||||
"Tagline",
|
||||
"Caption",
|
||||
"Para",
|
||||
"Inscription"
|
||||
"Inscription",
|
||||
"Link"
|
||||
].each do |const|
|
||||
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
||||
options[:parent] = element_parent
|
||||
@@ -97,7 +98,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def background(color = Gosu::Color::NONE)
|
||||
element_parent.style.background = color
|
||||
element_parent.style.default[:background] = color
|
||||
end
|
||||
|
||||
def theme(theme)
|
||||
@@ -115,7 +116,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
private def element_parent
|
||||
$__current_container__
|
||||
CyberarmEngine::Element::Container.current_container
|
||||
end
|
||||
|
||||
private def container(klass, options = {}, &block)
|
||||
@@ -125,12 +126,12 @@ module CyberarmEngine
|
||||
_container = klass.new(options, block)
|
||||
|
||||
old_parent = element_parent
|
||||
$__current_container__ = _container
|
||||
CyberarmEngine::Element::Container.current_container = _container
|
||||
|
||||
_container.build
|
||||
_container.parent.add(_container)
|
||||
|
||||
$__current_container__ = old_parent
|
||||
CyberarmEngine::Element::Container.current_container = old_parent
|
||||
|
||||
_container
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module CyberarmEngine
|
||||
include Event
|
||||
include Common
|
||||
|
||||
attr_accessor :x, :y, :z, :enabled, :tip
|
||||
attr_accessor :x, :y, :z, :tip, :element_visible
|
||||
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@@ -13,15 +13,18 @@ module CyberarmEngine
|
||||
@options = options
|
||||
@block = block
|
||||
|
||||
@focus = @options[:focus].nil? ? false : @options[:focus]
|
||||
@enabled = @options[:enabled].nil? ? true : @options[:enabled]
|
||||
@visible = @options[:visible].nil? ? true : @options[:visible]
|
||||
@focus = !@options.key?(:focus) ? false : @options[:focus]
|
||||
@enabled = !@options.key?(:enabled) ? true : @options[:enabled]
|
||||
@visible = !@options.key?(:visible) ? true : @options[:visible]
|
||||
@tip = @options[:tip] || ""
|
||||
|
||||
@debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
|
||||
|
||||
@style = Style.new(options)
|
||||
|
||||
@root ||= nil
|
||||
@gui_state ||= nil
|
||||
@element_visible = true
|
||||
|
||||
@x = @style.x
|
||||
@y = @style.y
|
||||
@@ -34,7 +37,11 @@ module CyberarmEngine
|
||||
@style.height = default(:height) || nil
|
||||
|
||||
@style.background_canvas = Background.new
|
||||
@style.border_canvas = BorderCanvas.new(element: self)
|
||||
@style.background_nine_slice_canvas = BackgroundNineSlice.new
|
||||
@style.background_image_canvas = BackgroundImage.new
|
||||
@style.border_canvas = BorderCanvas.new(element: self)
|
||||
|
||||
@style_event = :default
|
||||
|
||||
stylize
|
||||
|
||||
@@ -45,14 +52,25 @@ module CyberarmEngine
|
||||
|
||||
def stylize
|
||||
set_static_position
|
||||
set_border_thickness(@style.border_thickness)
|
||||
|
||||
set_padding(@style.padding)
|
||||
set_color
|
||||
set_font
|
||||
|
||||
set_margin(@style.margin)
|
||||
set_padding
|
||||
set_margin
|
||||
|
||||
set_background(@style.background)
|
||||
set_border_color(@style.border_color)
|
||||
set_background
|
||||
set_background_nine_slice
|
||||
set_background_image
|
||||
|
||||
set_border_thickness
|
||||
set_border_color
|
||||
|
||||
root.gui_state.request_repaint
|
||||
end
|
||||
|
||||
def safe_style_fetch(*args)
|
||||
@style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args)
|
||||
end
|
||||
|
||||
def set_static_position
|
||||
@@ -60,47 +78,100 @@ module CyberarmEngine
|
||||
@y = @style.y if @style.y != 0
|
||||
end
|
||||
|
||||
def set_background(background)
|
||||
@style.background = background
|
||||
@style.background_canvas.background = background
|
||||
def set_color
|
||||
@style.color = safe_style_fetch(:color)
|
||||
@text&.color = @style.color
|
||||
end
|
||||
|
||||
def set_border_thickness(border_thickness)
|
||||
@style.border_thickness = border_thickness
|
||||
|
||||
@style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
|
||||
@style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
|
||||
@style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
|
||||
@style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
|
||||
def set_font
|
||||
@text&.swap_font(safe_style_fetch(:text_size), safe_style_fetch(:font))
|
||||
end
|
||||
|
||||
def set_border_color(color)
|
||||
@style.border_color = color
|
||||
def set_background
|
||||
@style.background = safe_style_fetch(:background)
|
||||
|
||||
@style.border_color_left = default(:border_color_left) || @style.border_color
|
||||
@style.border_color_right = default(:border_color_right) || @style.border_color
|
||||
@style.border_color_top = default(:border_color_top) || @style.border_color
|
||||
@style.border_color_bottom = default(:border_color_bottom) || @style.border_color
|
||||
|
||||
@style.border_canvas.color = color
|
||||
@style.background_canvas.background = @style.background
|
||||
end
|
||||
|
||||
def set_padding(padding)
|
||||
@style.padding = padding
|
||||
def set_background_nine_slice
|
||||
@style.background_nine_slice = safe_style_fetch(:background_nine_slice)
|
||||
|
||||
@style.padding_left = default(:padding_left) || @style.padding
|
||||
@style.padding_right = default(:padding_right) || @style.padding
|
||||
@style.padding_top = default(:padding_top) || @style.padding
|
||||
@style.padding_bottom = default(:padding_bottom) || @style.padding
|
||||
@style.background_nine_slice_mode = safe_style_fetch(:background_nine_slice_mode) || :stretch
|
||||
@style.background_nine_slice_color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
|
||||
@style.background_nine_slice_canvas.color = @style.background_nine_slice_color
|
||||
|
||||
@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_top = safe_style_fetch(:background_nine_slice_top) || @style.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_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
|
||||
end
|
||||
|
||||
def set_margin(margin)
|
||||
@style.margin = margin
|
||||
def set_background_image
|
||||
@style.background_image = safe_style_fetch(:background_image)
|
||||
@style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch
|
||||
@style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE
|
||||
@style.background_image_canvas.mode = @style.background_image_mode
|
||||
@style.background_image_canvas.color = @style.background_image_color
|
||||
end
|
||||
|
||||
@style.margin_left = default(:margin_left) || @style.margin
|
||||
@style.margin_right = default(:margin_right) || @style.margin
|
||||
@style.margin_top = default(:margin_top) || @style.margin
|
||||
@style.margin_bottom = default(:margin_bottom) || @style.margin
|
||||
def set_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_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness
|
||||
@style.border_thickness_top = safe_style_fetch(:border_thickness_top) || @style.border_thickness
|
||||
@style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom) || @style.border_thickness
|
||||
end
|
||||
|
||||
def set_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_right = safe_style_fetch(:border_color_right) || @style.border_color
|
||||
@style.border_color_top = safe_style_fetch(:border_color_top) || @style.border_color
|
||||
@style.border_color_bottom = safe_style_fetch(:border_color_bottom) || @style.border_color
|
||||
|
||||
@style.border_canvas.color = [
|
||||
@style.border_color_top,
|
||||
@style.border_color_right,
|
||||
@style.border_color_bottom,
|
||||
@style.border_color_left
|
||||
]
|
||||
end
|
||||
|
||||
def set_padding
|
||||
@style.padding = safe_style_fetch(:padding)
|
||||
|
||||
@style.padding_left = safe_style_fetch(:padding_left) || @style.padding
|
||||
@style.padding_right = safe_style_fetch(:padding_right) || @style.padding
|
||||
@style.padding_top = safe_style_fetch(:padding_top) || @style.padding
|
||||
@style.padding_bottom = safe_style_fetch(:padding_bottom) || @style.padding
|
||||
end
|
||||
|
||||
def set_margin
|
||||
@style.margin = safe_style_fetch(:margin)
|
||||
|
||||
@style.margin_left = safe_style_fetch(:margin_left) || @style.margin
|
||||
@style.margin_right = safe_style_fetch(:margin_right) || @style.margin
|
||||
@style.margin_top = safe_style_fetch(:margin_top) || @style.margin
|
||||
@style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin
|
||||
end
|
||||
|
||||
def update_styles(event = :default)
|
||||
old_width = width
|
||||
old_height = height
|
||||
|
||||
@style_event = event
|
||||
|
||||
return if self.is_a?(ToolTip)
|
||||
|
||||
if old_width != width || old_height != height
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
stylize
|
||||
end
|
||||
|
||||
def default_events
|
||||
@@ -124,40 +195,150 @@ module CyberarmEngine
|
||||
event(:changed)
|
||||
end
|
||||
|
||||
def enter(_sender)
|
||||
@focus = false unless Gosu.button_down?(Gosu::MS_LEFT)
|
||||
|
||||
if !@enabled
|
||||
update_styles(:disabled)
|
||||
elsif @focus
|
||||
update_styles(:active)
|
||||
else
|
||||
update_styles(:hover)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def left_mouse_button(_sender, _x, _y)
|
||||
@focus = true
|
||||
|
||||
unless @enabled
|
||||
update_styles(:disabled)
|
||||
else
|
||||
update_styles(:active)
|
||||
end
|
||||
|
||||
window.current_state.focus = self
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender, _x, _y)
|
||||
enter(sender)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block&.call(self) if @enabled && !self.is_a?(Container)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def leave(_sender)
|
||||
if @enabled
|
||||
update_styles
|
||||
else
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def blur(_sender)
|
||||
@focus = false
|
||||
|
||||
if @enabled
|
||||
update_styles
|
||||
else
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def enabled=(boolean)
|
||||
root.gui_state.request_repaint if @enabled != boolean
|
||||
|
||||
@enabled = boolean
|
||||
|
||||
recalculate
|
||||
|
||||
@enabled
|
||||
end
|
||||
|
||||
def enabled?
|
||||
@enabled
|
||||
end
|
||||
|
||||
def focused?
|
||||
@focus
|
||||
end
|
||||
|
||||
def visible?
|
||||
@visible
|
||||
end
|
||||
|
||||
def element_visible?
|
||||
@element_visible
|
||||
end
|
||||
|
||||
def toggle
|
||||
@visible = !@visible
|
||||
root.gui_state.request_recalculate
|
||||
root.gui_state.request_repaint
|
||||
end
|
||||
|
||||
def show
|
||||
bool = visible?
|
||||
@visible = true
|
||||
root.gui_state.request_recalculate unless bool
|
||||
root.gui_state.request_repaint unless bool
|
||||
end
|
||||
|
||||
def hide
|
||||
bool = visible?
|
||||
@visible = false
|
||||
root.gui_state.request_recalculate if bool
|
||||
root.gui_state.request_repaint if bool
|
||||
end
|
||||
|
||||
def draw
|
||||
return unless visible?
|
||||
return unless element_visible?
|
||||
|
||||
@style.background_canvas.draw
|
||||
@style.background_nine_slice_canvas.draw
|
||||
@style.background_image_canvas.draw
|
||||
@style.border_canvas.draw
|
||||
|
||||
Gosu.clip_to(@x, @y, width, height) do
|
||||
render
|
||||
end
|
||||
render
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
# FIXME
|
||||
return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||
|
||||
Gosu.draw_line(
|
||||
x, y, @debug_color,
|
||||
x + outer_width, y, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y, @debug_color,
|
||||
x + outer_width, y + outer_height, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y + outer_height, @debug_color,
|
||||
x, y + outer_height, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x, outer_height, @debug_color,
|
||||
x, y, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -230,35 +411,91 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def scroll_width
|
||||
@children.sum { |c| c.width } + noncontent_width
|
||||
return @cached_scroll_width if @cached_scroll_width && is_a?(Container)
|
||||
|
||||
@cached_scroll_width = @children.sum(&:outer_width)
|
||||
end
|
||||
|
||||
def scroll_height
|
||||
@children.sum { |c| c.height } + noncontent_height
|
||||
return @cached_scroll_height if @cached_scroll_height && is_a?(Container)
|
||||
|
||||
if is_a?(CyberarmEngine::Element::Flow)
|
||||
return 0 if @children.size.zero?
|
||||
|
||||
pairs_ = []
|
||||
sorted_children_ = @children.sort_by(&:y)
|
||||
a_ = []
|
||||
y_position_ = sorted_children_.first.y
|
||||
|
||||
sorted_children_.each do |child|
|
||||
unless child.y == y_position_
|
||||
y_position_ = child.y
|
||||
pairs_ << a_
|
||||
a_ = []
|
||||
end
|
||||
|
||||
a_ << child
|
||||
end
|
||||
|
||||
pairs_ << a_ unless pairs_.last == a_
|
||||
|
||||
@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
|
||||
@cached_scroll_height = @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom
|
||||
end
|
||||
end
|
||||
|
||||
def max_scroll_width
|
||||
scroll_width - width
|
||||
(scroll_width - outer_width).positive? ? scroll_width - outer_width : scroll_width
|
||||
end
|
||||
|
||||
def max_scroll_height
|
||||
scroll_height - height
|
||||
(scroll_height - outer_height).positive? ? scroll_height - outer_height : scroll_height
|
||||
end
|
||||
|
||||
def dimensional_size(size, dimension)
|
||||
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
||||
|
||||
if size && size.is_a?(Numeric)
|
||||
if size.between?(0.0, 1.0)
|
||||
((@parent.send(:"content_#{dimension}") - send(:"noncontent_#{dimension}")) * size).round
|
||||
else
|
||||
size
|
||||
end
|
||||
new_size = if size.is_a?(Float) && size.between?(0.0, 1.0)
|
||||
(@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
|
||||
else
|
||||
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
|
||||
|
||||
return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}")
|
||||
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}")
|
||||
|
||||
new_size
|
||||
end
|
||||
|
||||
def space_available_width
|
||||
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
|
||||
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
|
||||
|
||||
available_space = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings)
|
||||
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
|
||||
end
|
||||
|
||||
def space_available_height
|
||||
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
|
||||
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
|
||||
|
||||
available_space = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings)
|
||||
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
|
||||
end
|
||||
|
||||
def background=(_background)
|
||||
@style.background_canvas.background = (_background)
|
||||
root.gui_state.request_repaint
|
||||
|
||||
@style.background_canvas.background = _background
|
||||
update_background
|
||||
end
|
||||
|
||||
@@ -270,10 +507,57 @@ module CyberarmEngine
|
||||
@style.background_canvas.height = height
|
||||
|
||||
@style.background_canvas.update
|
||||
|
||||
update_background_nine_slice
|
||||
update_background_image
|
||||
@style.border_canvas.update
|
||||
end
|
||||
|
||||
def background_nine_slice=(_image_path)
|
||||
root.gui_state.request_repaint
|
||||
|
||||
@style.background_nine_slice_canvas.image = _image_path
|
||||
update_background_nine_slice
|
||||
end
|
||||
|
||||
def update_background_nine_slice
|
||||
@style.background_nine_slice_canvas.x = @x
|
||||
@style.background_nine_slice_canvas.y = @y
|
||||
@style.background_nine_slice_canvas.z = @z
|
||||
@style.background_nine_slice_canvas.width = width
|
||||
@style.background_nine_slice_canvas.height = height
|
||||
|
||||
@style.background_nine_slice_canvas.mode = @style.background_nine_slice_mode
|
||||
|
||||
@style.background_nine_slice_canvas.color = @style.background_nine_slice_color
|
||||
|
||||
@style.background_nine_slice_canvas.left = @style.background_nine_slice_left
|
||||
@style.background_nine_slice_canvas.top = @style.background_nine_slice_top
|
||||
@style.background_nine_slice_canvas.right = @style.background_nine_slice_right
|
||||
@style.background_nine_slice_canvas.bottom = @style.background_nine_slice_bottom
|
||||
|
||||
@style.background_nine_slice_canvas.image = @style.background_nine_slice
|
||||
end
|
||||
|
||||
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)
|
||||
update_background_image
|
||||
end
|
||||
|
||||
def update_background_image
|
||||
@style.background_image_canvas.x = @x
|
||||
@style.background_image_canvas.y = @y
|
||||
@style.background_image_canvas.z = @z
|
||||
@style.background_image_canvas.width = width
|
||||
@style.background_image_canvas.height = height
|
||||
|
||||
@style.background_image_canvas.mode = @style.background_image_mode
|
||||
@style.background_image_canvas.color = @style.background_image_color
|
||||
|
||||
@style.background_image_canvas.image = @style.background_image
|
||||
end
|
||||
|
||||
def root
|
||||
return self if is_root?
|
||||
|
||||
@@ -281,11 +565,9 @@ module CyberarmEngine
|
||||
@root = parent
|
||||
|
||||
loop do
|
||||
if @root.parent.nil?
|
||||
break
|
||||
else
|
||||
@root = @root.parent
|
||||
end
|
||||
break unless @root&.parent
|
||||
|
||||
@root = @root.parent
|
||||
end
|
||||
end
|
||||
|
||||
@@ -296,6 +578,20 @@ module CyberarmEngine
|
||||
@gui_state != nil
|
||||
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 true if element.children.find { |child| child == self }
|
||||
|
||||
# element.children.find { |child| child.parent}
|
||||
# end
|
||||
|
||||
def focus(_)
|
||||
warn "#{self.class}#focus was not overridden!"
|
||||
|
||||
|
||||
@@ -34,69 +34,6 @@ module CyberarmEngine
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def enter(_sender)
|
||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||
|
||||
if !@enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@text.color = @style.disabled[:color]
|
||||
elsif @focus
|
||||
@style.background_canvas.background = @style.active[:background]
|
||||
@text.color = @style.active[:color]
|
||||
else
|
||||
@style.background_canvas.background = @style.hover[:background]
|
||||
@text.color = @style.hover[:color]
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def left_mouse_button(_sender, _x, _y)
|
||||
@focus = true
|
||||
|
||||
unless @enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@text.color = @style.disabled[:color]
|
||||
else
|
||||
@style.background_canvas.background = @style.active[:background]
|
||||
@text.color = @style.active[:color]
|
||||
end
|
||||
|
||||
window.current_state.focus = self
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender, _x, _y)
|
||||
enter(sender)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block.call(self) if @enabled && @block
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def leave(_sender)
|
||||
unless @enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@text.color = @style.disabled[:color]
|
||||
else
|
||||
@style.background_canvas.background = @style.background
|
||||
@text.color = @style.color
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def blur(_sender)
|
||||
@focus = false
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
unless @enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@@ -127,8 +64,8 @@ module CyberarmEngine
|
||||
@scale_y = 1
|
||||
end
|
||||
|
||||
@width = _width || @image.width.round * @scale_x
|
||||
@height = _height || @image.height.round * @scale_y
|
||||
@width = _width || @image.width.floor * @scale_x
|
||||
@height = _height || @image.height.floor * @scale_y
|
||||
|
||||
update_background
|
||||
else
|
||||
@@ -149,9 +86,12 @@ module CyberarmEngine
|
||||
|
||||
old_width = width
|
||||
old_height = height
|
||||
recalculate
|
||||
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
if old_width != width || old_height != height
|
||||
root.gui_state.request_recalculate
|
||||
else
|
||||
recalculate
|
||||
end
|
||||
|
||||
publish(:changed, self.value)
|
||||
end
|
||||
|
||||
@@ -5,8 +5,11 @@ module CyberarmEngine
|
||||
super(options, block)
|
||||
options[:toggled] = options[:checked]
|
||||
|
||||
options[:parent] = self
|
||||
@toggle_button = ToggleButton.new(options)
|
||||
@label = TextBlock.new(text, options)
|
||||
|
||||
options[:parent] = self
|
||||
@label = TextBlock.new(text, options)
|
||||
|
||||
@label.subscribe(:holding_left_mouse_button) do |sender, x, y|
|
||||
@toggle_button.left_mouse_button(sender, x, y)
|
||||
|
||||
@@ -6,11 +6,24 @@ module CyberarmEngine
|
||||
attr_accessor :stroke_color, :fill_color
|
||||
attr_reader :children, :gui_state, :scroll_position
|
||||
|
||||
def self.current_container
|
||||
@@current_container
|
||||
end
|
||||
|
||||
def self.current_container=(container)
|
||||
raise ArgumentError, "Expected container to an an instance of CyberarmEngine::Element::Container, got #{container.class}" unless container.is_a?(CyberarmEngine::Element::Container)
|
||||
|
||||
@@current_container = container
|
||||
end
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@gui_state = options.delete(:gui_state)
|
||||
super
|
||||
|
||||
@last_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
|
||||
|
||||
@text_color = options[:color]
|
||||
@@ -23,103 +36,146 @@ module CyberarmEngine
|
||||
def build
|
||||
@block.call(self) if @block
|
||||
|
||||
root.gui_state.request_recalculate
|
||||
root.gui_state.request_recalculate_for(self)
|
||||
end
|
||||
|
||||
def add(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 clear(&block)
|
||||
@children.clear
|
||||
|
||||
old_container = $__current_container__
|
||||
old_container = CyberarmEngine::Element::Container.current_container
|
||||
|
||||
$__current_container__ = self
|
||||
CyberarmEngine::Element::Container.current_container = self
|
||||
block.call(self) if block
|
||||
|
||||
$__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 apend(&block)
|
||||
old_container = $__current_container__
|
||||
def append(&block)
|
||||
old_container = CyberarmEngine::Element::Container.current_container
|
||||
|
||||
$__current_container__ = self
|
||||
CyberarmEngine::Element::Container.current_container = self
|
||||
block.call(self) if block
|
||||
|
||||
$__current_container__ = old_container
|
||||
CyberarmEngine::Element::Container.current_container = old_container
|
||||
|
||||
root.gui_state.request_recalculate
|
||||
root.gui_state.request_recalculate_for(self)
|
||||
end
|
||||
|
||||
def render
|
||||
Gosu.clip_to(@x, @y, width, height) do
|
||||
@children.each(&:draw)
|
||||
Gosu.clip_to(
|
||||
@x + @style.border_thickness_left + @style.padding_left,
|
||||
@y + @style.border_thickness_top + @style.padding_top,
|
||||
content_width + 1,
|
||||
content_height + 1
|
||||
) do
|
||||
Gosu.translate(@scroll_position.x, @scroll_position.y) do
|
||||
@children.each(&:draw)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if false # DEBUG
|
||||
Gosu.flush
|
||||
def debug_draw
|
||||
super
|
||||
|
||||
Gosu.draw_line(
|
||||
x, y, Gosu::Color::RED,
|
||||
x + outer_width, y, Gosu::Color::RED,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y, Gosu::Color::RED,
|
||||
x + outer_width, y + outer_height, Gosu::Color::RED,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y + outer_height, Gosu::Color::RED,
|
||||
x, y + outer_height, Gosu::Color::RED,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x, outer_height, Gosu::Color::RED,
|
||||
x, y, Gosu::Color::RED,
|
||||
Float::INFINITY
|
||||
)
|
||||
@children.each do |child|
|
||||
child.debug_draw
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
update_scroll
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
def hit_element?(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|
|
||||
next unless child.visible?
|
||||
|
||||
case child
|
||||
when Container
|
||||
if element = child.hit_element?(x, y)
|
||||
if element = child.hit_element?(child_x, child_y)
|
||||
return element
|
||||
end
|
||||
else
|
||||
return child if child.hit?(x, y)
|
||||
return child if child.hit?(child_x, child_y)
|
||||
end
|
||||
end
|
||||
|
||||
self if hit?(x, y)
|
||||
end
|
||||
|
||||
def update_child_element_visibity(child)
|
||||
child.element_visible = child.x >= (@x - @scroll_position.x) - child.width && child.x <= (@x - @scroll_position.x) + width &&
|
||||
child.y >= (@y - @scroll_position.y) - child.height && child.y <= (@y - @scroll_position.y) + height
|
||||
end
|
||||
|
||||
def update_scroll
|
||||
dt = window.dt > 1 ? 1.0 : window.dt
|
||||
|
||||
scroll_x_diff = (@scroll_target_position.x - @scroll_position.x)
|
||||
scroll_y_diff = (@scroll_target_position.y - @scroll_position.y)
|
||||
|
||||
@scroll_position.x += (scroll_x_diff * @scroll_speed * 0.25 * dt).round
|
||||
@scroll_position.y += (scroll_y_diff * @scroll_speed * 0.25 * dt).round
|
||||
|
||||
@scroll_position.x = @scroll_target_position.x if scroll_x_diff.abs < 1.0
|
||||
@scroll_position.y = @scroll_target_position.y if scroll_y_diff.abs < 1.0
|
||||
|
||||
# Scrolled PAST top
|
||||
if @scroll_position.y > 0
|
||||
@scroll_target_position.y = 0
|
||||
|
||||
# Scrolled PAST bottom
|
||||
elsif @scroll_position.y.abs > max_scroll_height
|
||||
@scroll_target_position.y = -max_scroll_height
|
||||
end
|
||||
|
||||
if @last_scroll_position != @scroll_position
|
||||
@children.each { |child| update_child_element_visibity(child) }
|
||||
root.gui_state.request_repaint
|
||||
end
|
||||
|
||||
@last_scroll_position.x = @scroll_position.x
|
||||
@last_scroll_position.y = @scroll_position.y
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||
@current_position += @scroll_position
|
||||
|
||||
return unless visible?
|
||||
|
||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||
Stats.frame.increment(:gui_recalculations)
|
||||
|
||||
stylize
|
||||
|
||||
# s = Gosu.milliseconds
|
||||
|
||||
layout
|
||||
|
||||
old_width = @width
|
||||
old_height = @height
|
||||
|
||||
@cached_scroll_width = nil
|
||||
@cached_scroll_height = nil
|
||||
|
||||
if is_root?
|
||||
@width = @style.width = window.width
|
||||
@height = @style.height = window.height
|
||||
@@ -130,11 +186,35 @@ module CyberarmEngine
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).round
|
||||
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).round
|
||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
|
||||
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
|
||||
end
|
||||
|
||||
# Move child to parent after positioning
|
||||
# FIXME: Correctly handle alignment when element has siblings
|
||||
# FIXME: Enable alignment for any element, not just containers
|
||||
if @style.v_align
|
||||
space = space_available_height
|
||||
|
||||
case @style.v_align
|
||||
when :center
|
||||
@y = parent.height / 2 - height / 2
|
||||
when :bottom
|
||||
@y = parent.height - height
|
||||
end
|
||||
end
|
||||
|
||||
if @style.h_align
|
||||
space = space_available_width
|
||||
|
||||
case @style.h_align
|
||||
when :center
|
||||
@x = parent.width / 2 - width / 2
|
||||
when :right
|
||||
@x = parent.width - width
|
||||
end
|
||||
end
|
||||
|
||||
# Move children to parent after positioning
|
||||
@children.each do |child|
|
||||
child.x += (@x + @style.border_thickness_left) - style.margin_left
|
||||
child.y += (@y + @style.border_thickness_top) - style.margin_top
|
||||
@@ -143,10 +223,29 @@ module CyberarmEngine
|
||||
child.recalculate
|
||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||
|
||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||
Stats.frame.increment(:gui_recalculations)
|
||||
|
||||
update_child_element_visibity(child)
|
||||
end
|
||||
|
||||
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
|
||||
|
||||
update_background
|
||||
|
||||
# Fixes resized container scrolled past bottom
|
||||
self.scroll_top = -@scroll_position.y
|
||||
@scroll_target_position.y = @scroll_position.y
|
||||
|
||||
# NOTE: Experiment for removing need to explicitly call gui_state#recalculate at least 3 times for layout to layout...
|
||||
if old_width != @width || old_height != @height
|
||||
if @parent
|
||||
root.gui_state.request_recalculate_for(@parent)
|
||||
else
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
end
|
||||
|
||||
root.gui_state.request_repaint if @width != old_width || @height != old_height
|
||||
end
|
||||
|
||||
def layout
|
||||
@@ -154,16 +253,17 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def max_width
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
if _width
|
||||
outer_width
|
||||
else
|
||||
window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
end
|
||||
# _width = dimensional_size(@style.width, :width)
|
||||
# if _width
|
||||
# outer_width
|
||||
# else
|
||||
# window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
# end
|
||||
|
||||
outer_width
|
||||
end
|
||||
|
||||
def fits_on_line?(element) # Flow
|
||||
p [@options[:id], @width] if @options[:id]
|
||||
@current_position.x + element.outer_width <= max_width &&
|
||||
@current_position.x + element.outer_width <= window.width
|
||||
end
|
||||
@@ -173,7 +273,6 @@ module CyberarmEngine
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
@current_position.x += element.outer_width
|
||||
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
||||
end
|
||||
|
||||
def tallest_neighbor(querier, _y_position) # Flow
|
||||
@@ -186,14 +285,14 @@ module CyberarmEngine
|
||||
response
|
||||
end
|
||||
|
||||
def position_on_next_line(child) # Flow
|
||||
@current_position.x = @style.margin_left
|
||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
||||
def position_on_next_line(element) # Flow
|
||||
@current_position.x = @style.margin_left + @style.padding_left
|
||||
@current_position.y += tallest_neighbor(element, @current_position.y).outer_height
|
||||
|
||||
child.x = child.style.margin_left + @current_position.x
|
||||
child.y = child.style.margin_top + @current_position.y
|
||||
element.x = element.style.margin_left + @current_position.x
|
||||
element.y = element.style.margin_top + @current_position.y
|
||||
|
||||
@current_position.x += child.outer_width
|
||||
@current_position.x += element.outer_width
|
||||
end
|
||||
|
||||
def move_to_next_line(element) # Stack
|
||||
@@ -205,12 +304,14 @@ module CyberarmEngine
|
||||
|
||||
def mouse_wheel_up(sender, x, y)
|
||||
return unless @style.scroll
|
||||
return if height < max_scroll_height
|
||||
|
||||
if @scroll_position.y < 0
|
||||
@scroll_position.y += @scroll_speed
|
||||
@scroll_position.y = 0 if @scroll_position.y > 0
|
||||
recalculate
|
||||
# Allow overscrolling UP, only if one can scroll DOWN
|
||||
if height < scroll_height
|
||||
if @scroll_target_position.y > 0
|
||||
@scroll_target_position.y = @scroll_chunk
|
||||
else
|
||||
@scroll_target_position.y += @scroll_chunk
|
||||
end
|
||||
|
||||
return :handled
|
||||
end
|
||||
@@ -218,19 +319,35 @@ module CyberarmEngine
|
||||
|
||||
def mouse_wheel_down(sender, x, y)
|
||||
return unless @style.scroll
|
||||
return if height < max_scroll_height
|
||||
|
||||
if @scroll_position.y.abs < max_scroll_height
|
||||
@scroll_position.y -= @scroll_speed
|
||||
return unless height < scroll_height
|
||||
|
||||
if @scroll_target_position.y > 0
|
||||
@scroll_target_position.y = -@scroll_chunk
|
||||
else
|
||||
@scroll_target_position.y -= @scroll_chunk
|
||||
end
|
||||
|
||||
return :handled
|
||||
end
|
||||
|
||||
def scroll_top
|
||||
@scroll_position.y
|
||||
end
|
||||
|
||||
def scroll_top=(n)
|
||||
n = 0 if n <= 0
|
||||
@scroll_position.y = -n
|
||||
|
||||
if max_scroll_height.positive?
|
||||
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
|
||||
recalculate
|
||||
|
||||
return :handled
|
||||
else
|
||||
@scroll_position.y = 0
|
||||
end
|
||||
end
|
||||
|
||||
def value
|
||||
@children.map { |c| c.class }.join(", ")
|
||||
@children.map(&:class).join(", ")
|
||||
end
|
||||
|
||||
def to_s
|
||||
|
||||
@@ -98,7 +98,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def row_at(y)
|
||||
((y - @text.y) / @text.textobject.height).round
|
||||
((y - @text.y) / @text.textobject.height).floor
|
||||
end
|
||||
|
||||
def column_at(x, y)
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class EditLine < Button
|
||||
class TextInput < Gosu::TextInput
|
||||
def filter=(filter)
|
||||
@filter = filter
|
||||
end
|
||||
|
||||
def filter(text_in)
|
||||
if @filter
|
||||
@filter.call(text_in)
|
||||
else
|
||||
text_in
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(text, options = {}, block = nil)
|
||||
@filter = options.delete(:filter)
|
||||
super(text, options, block)
|
||||
@@ -14,17 +28,11 @@ module CyberarmEngine
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
|
||||
@text_input = Gosu::TextInput.new
|
||||
@text_input = TextInput.new
|
||||
@text_input.filter = @filter
|
||||
@text_input.text = text
|
||||
@last_text_value = text
|
||||
|
||||
if @filter && @filter.respond_to?(:call)
|
||||
@text_input.instance_variable_set(:@filter, @filter)
|
||||
|
||||
def @text_input.filter(text_in)
|
||||
@filter.call(text_in)
|
||||
end
|
||||
end
|
||||
@last_caret_position = @text_input.caret_pos
|
||||
|
||||
@offset_x = 0
|
||||
@offset_y = 0
|
||||
@@ -59,6 +67,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def update
|
||||
@style_event = :active if @focus
|
||||
|
||||
@text.text = if @type == :password
|
||||
default(:password_character) * @text_input.text.length
|
||||
else
|
||||
@@ -70,10 +80,22 @@ module CyberarmEngine
|
||||
@show_caret = true
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
|
||||
root.gui_state.request_repaint
|
||||
|
||||
publish(:changed, value)
|
||||
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
|
||||
root.gui_state.request_repaint
|
||||
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
|
||||
@show_caret = !@show_caret
|
||||
@@ -96,20 +118,20 @@ module CyberarmEngine
|
||||
@text_input.caret_pos = @text_input.text.length
|
||||
|
||||
when Gosu::KB_C
|
||||
if @text_input.selection_start < @text_input.caret_pos
|
||||
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
|
||||
else
|
||||
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
|
||||
end
|
||||
Gosu.clipboard = if @text_input.selection_start < @text_input.caret_pos
|
||||
@text_input.text[@text_input.selection_start...@text_input.caret_pos]
|
||||
else
|
||||
@text_input.text[@text_input.caret_pos...@text_input.selection_start]
|
||||
end
|
||||
|
||||
when Gosu::KB_X
|
||||
chars = @text_input.text.chars
|
||||
|
||||
if @text_input.selection_start < @text_input.caret_pos
|
||||
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
|
||||
Gosu.clipboard = @text_input.text[@text_input.selection_start...@text_input.caret_pos]
|
||||
chars.slice!(@text_input.selection_start, @text_input.caret_pos)
|
||||
else
|
||||
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
|
||||
Gosu.clipboard = @text_input.text[@text_input.caret_pos...@text_input.selection_start]
|
||||
chars.slice!(@text_input.caret_pos, @text_input.selection_start)
|
||||
end
|
||||
|
||||
@@ -117,10 +139,9 @@ module CyberarmEngine
|
||||
|
||||
when Gosu::KB_V
|
||||
if instance_of?(EditLine) # EditLine assumes a single line of text
|
||||
@text_input.text = @text_input.text.insert(@text_input.caret_pos,
|
||||
Clipboard.paste.encode("UTF-8").gsub("\n", ""))
|
||||
@text_input.insert_text(Gosu.clipboard.gsub("\n", ""))
|
||||
else
|
||||
@text_input.text = @text_input.text.insert(@text_input.caret_pos, Clipboard.paste.encode("UTF-8"))
|
||||
@text_input.insert_text(Gosu.clipboard)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -178,7 +199,7 @@ module CyberarmEngine
|
||||
if @type == :password
|
||||
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
|
||||
else
|
||||
@text.x + @text.width(@text_input.text[0...@text_input.send(method)])
|
||||
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
|
||||
end
|
||||
end
|
||||
|
||||
@@ -194,38 +215,42 @@ module CyberarmEngine
|
||||
:handled
|
||||
end
|
||||
|
||||
def enter(_sender)
|
||||
if @focus
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
def focus(sender)
|
||||
@focus = true
|
||||
window.text_input = @text_input
|
||||
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
|
||||
|
||||
update_styles(:active)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def enter(sender)
|
||||
if @enabled && @focus
|
||||
update_styles(:active)
|
||||
elsif @enabled && !@focus
|
||||
update_styles(:hover)
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
super unless @focus
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def focus(sender)
|
||||
@focus = true
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
@text.color = default(:active, :color)
|
||||
window.text_input = @text_input
|
||||
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
|
||||
if @enabled && @focus
|
||||
update_styles(:active)
|
||||
elsif @enabled && !@focus
|
||||
update_styles
|
||||
else
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def blur(_sender)
|
||||
@focus = false
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
super
|
||||
window.text_input = nil
|
||||
|
||||
:handled
|
||||
|
||||
@@ -45,8 +45,8 @@ module CyberarmEngine
|
||||
@scale_y = 1
|
||||
end
|
||||
|
||||
@width = _width || @image.width.round * @scale_x
|
||||
@height = _height || @image.height.round * @scale_y
|
||||
@width = _width || (@image.width * @scale_x).floor
|
||||
@height = _height || (@image.height * @scale_y).floor
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class TextBlock < Element
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
@text = Text.new(
|
||||
text, font: @options[:font], z: @z, color: @options[:color],
|
||||
size: @options[:text_size], shadow: @options[:text_shadow],
|
||||
shadow_size: @options[:text_shadow_size],
|
||||
shadow_color: @options[:text_shadow_color]
|
||||
)
|
||||
|
||||
@raw_text = text
|
||||
end
|
||||
|
||||
def render
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block&.call(self) if @enabled
|
||||
|
||||
# return :handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
handle_text_wrapping(_width)
|
||||
|
||||
@width = _width || @text.width.round
|
||||
@height = _height || @text.height.round
|
||||
|
||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
if (text_alignment = @options[:text_align])
|
||||
case text_alignment
|
||||
when :left
|
||||
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
when :center
|
||||
@text.x = if @text.width <= outer_width
|
||||
@x + outer_width / 2 - @text.width / 2
|
||||
else # Act as left aligned
|
||||
@style.border_thickness_left + @style.padding_left + @x
|
||||
end
|
||||
when :right
|
||||
@text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right)
|
||||
end
|
||||
end
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def handle_text_wrapping(max_width)
|
||||
max_width ||= @parent&.width
|
||||
max_width ||= @x - (window.width + noncontent_width)
|
||||
wrap_behavior = style.text_wrap
|
||||
copy = @raw_text.to_s.dup
|
||||
|
||||
if max_width >= line_width(copy[0]) && line_width(copy) > max_width && wrap_behavior != :none
|
||||
breaks = []
|
||||
line_start = 0
|
||||
line_end = copy.length
|
||||
|
||||
while line_start != copy.length
|
||||
if line_width(copy[line_start...line_end]) > max_width
|
||||
line_end = ((line_end - line_start) / 2.0)
|
||||
elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
|
||||
# To small, grow!
|
||||
# TODO: find a more efficient way
|
||||
line_end += 1
|
||||
|
||||
else # FOUND IT!
|
||||
entering_line_end = line_end.floor
|
||||
max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63
|
||||
reach = 0
|
||||
|
||||
if wrap_behavior == :word_wrap
|
||||
max_reach.times do |i|
|
||||
reach = i
|
||||
break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
|
||||
end
|
||||
|
||||
puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}"
|
||||
line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation
|
||||
end
|
||||
|
||||
breaks << line_end.floor
|
||||
line_start = line_end.floor
|
||||
line_end = copy.length
|
||||
|
||||
break if entering_line_end == copy.length || reach == max_reach
|
||||
end
|
||||
end
|
||||
|
||||
breaks.each_with_index do |pos, index|
|
||||
copy.insert(pos + index, "\n") if pos + index >= 0 && pos + index < copy.length
|
||||
end
|
||||
end
|
||||
|
||||
@text.text = copy
|
||||
end
|
||||
|
||||
def line_width(text)
|
||||
(@x + @text.textobject.markup_width(text) + noncontent_width)
|
||||
end
|
||||
|
||||
def value
|
||||
@raw_text
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
@raw_text = value.to_s.chomp
|
||||
|
||||
old_width = width
|
||||
old_height = height
|
||||
recalculate
|
||||
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
|
||||
publish(:changed, self.value)
|
||||
end
|
||||
end
|
||||
|
||||
class Banner < TextBlock
|
||||
end
|
||||
|
||||
class Title < TextBlock
|
||||
end
|
||||
|
||||
class Subtitle < TextBlock
|
||||
end
|
||||
|
||||
class Tagline < TextBlock
|
||||
end
|
||||
|
||||
class Caption < TextBlock
|
||||
end
|
||||
|
||||
class Para < TextBlock
|
||||
end
|
||||
|
||||
class Inscription < TextBlock
|
||||
end
|
||||
|
||||
# TODO: Remove in version 0.16.0+
|
||||
class Label < TextBlock
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -13,10 +13,12 @@ module CyberarmEngine
|
||||
@style.background_canvas.background = default(:background)
|
||||
|
||||
# TODO: "Clean Up" into own class?
|
||||
@menu = Stack.new(parent: parent, width: @options[:width], theme: @options[:theme])
|
||||
@menu = Stack.new(parent: self, theme: @options[:theme])
|
||||
@menu.define_singleton_method(:recalculate_menu) do
|
||||
@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)
|
||||
|
||||
@@ -29,6 +31,13 @@ module CyberarmEngine
|
||||
self.choose = @choose
|
||||
end
|
||||
|
||||
def render
|
||||
super
|
||||
|
||||
w = @text.textobject.text_width("▼")
|
||||
@text.textobject.draw_text("▼", @x + content_width - w, @y + @style.padding_top, @z, 1, 1, @text.color)
|
||||
end
|
||||
|
||||
def choose=(item)
|
||||
valid = @items.detect { |i| i == item }
|
||||
raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid
|
||||
@@ -46,10 +55,20 @@ module CyberarmEngine
|
||||
:handled
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
# @block&.call(self.value) if @enabled
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def show_menu
|
||||
@menu.clear
|
||||
|
||||
@menu.style.width = width
|
||||
|
||||
@items.each do |item|
|
||||
next if item == self.value
|
||||
|
||||
btn = Button.new(
|
||||
item,
|
||||
{
|
||||
@@ -61,7 +80,7 @@ module CyberarmEngine
|
||||
},
|
||||
proc do
|
||||
self.choose = item
|
||||
@block&.call(item)
|
||||
@block&.call(self.value)
|
||||
end
|
||||
)
|
||||
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Progress < Element
|
||||
attr_reader :type
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
@animation_speed = options[:animation_speed] || 3_000
|
||||
@marquee_width = options[:marquee_width] || 0.25
|
||||
@marquee_offset = 0
|
||||
@marquee_animation_time = Gosu.milliseconds
|
||||
@type = options[:type] || :linear
|
||||
@fraction_background = Background.new(background: @style.fraction_background)
|
||||
self.value = options[:fraction] || 0.0
|
||||
end
|
||||
@@ -24,15 +31,46 @@ module CyberarmEngine
|
||||
def update_background
|
||||
super
|
||||
|
||||
@fraction_background.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
@fraction_background.x = (@style.border_thickness_left + @style.padding_left + @x) + @marquee_offset
|
||||
@fraction_background.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@fraction_background.z = @z
|
||||
@fraction_background.width = @width * @fraction
|
||||
@fraction_background.width = @width * (@type == :marquee ? @marquee_width : @fraction)
|
||||
@fraction_background.height = @height
|
||||
|
||||
@fraction_background.background = @style.fraction_background
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
|
||||
return unless @type == :marquee
|
||||
|
||||
marquee_width = @width * @marquee_width
|
||||
range = @width + marquee_width
|
||||
|
||||
@marquee_offset = (@width * (Gosu.milliseconds - @marquee_animation_time) / @animation_speed) - marquee_width
|
||||
@marquee_animation_time = Gosu.milliseconds if @marquee_offset > range
|
||||
|
||||
update_background
|
||||
root.gui_state.request_repaint
|
||||
end
|
||||
|
||||
def type=(type)
|
||||
@type = type
|
||||
|
||||
case type
|
||||
when :linear
|
||||
@marquee_offset = 0
|
||||
when :marquee
|
||||
@marquee_offset = 0
|
||||
@marquee_animation_time = Gosu.milliseconds
|
||||
else
|
||||
raise ArgumentError, "Only types :linear and :marquee are supported"
|
||||
end
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@fraction
|
||||
end
|
||||
@@ -40,9 +78,13 @@ module CyberarmEngine
|
||||
def value=(decimal)
|
||||
raise "value must be number" unless decimal.is_a?(Numeric)
|
||||
|
||||
old_value = @fraction
|
||||
|
||||
@fraction = decimal.clamp(0.0, 1.0)
|
||||
update_background
|
||||
|
||||
root.gui_state.request_repaint if @fraction != old_value
|
||||
|
||||
publish(:changed, @fraction)
|
||||
@fraction
|
||||
end
|
||||
|
||||
@@ -33,7 +33,8 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :range, :step_size, :value
|
||||
attr_reader :step_size, :value
|
||||
attr_accessor :range, :step_size
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
super(options, block)
|
||||
@@ -42,7 +43,7 @@ module CyberarmEngine
|
||||
@step_size = @options[:step] || 0.1
|
||||
@value = @options[:value] || (@range.first + @range.last) / 2
|
||||
|
||||
@handle = Handle.new("", parent: self, width: 8, height: 1.0) { close }
|
||||
@handle = Handle.new("", parent: self, theme: options[:theme], width: 8, height: 1.0) { close }
|
||||
add(@handle)
|
||||
end
|
||||
|
||||
@@ -61,10 +62,10 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def position_handle
|
||||
@handle.x = @x + @style.padding_left + @style.border_thickness_left +
|
||||
@handle.x = @x + @handle.style.margin_left + @style.padding_left + @style.border_thickness_left +
|
||||
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
|
||||
|
||||
@handle.y = @y + @style.border_thickness_top + @style.padding_top
|
||||
@handle.y = @y + @handle.style.margin_top + @style.border_thickness_top + @style.padding_top
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -76,7 +77,7 @@ module CyberarmEngine
|
||||
def update
|
||||
super
|
||||
|
||||
@tip = value.to_s
|
||||
@tip = format("%.2f", value.to_f)
|
||||
@handle.tip = @tip
|
||||
end
|
||||
|
||||
@@ -87,7 +88,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
@@ -97,6 +98,8 @@ module CyberarmEngine
|
||||
position_handle
|
||||
@handle.recalculate
|
||||
|
||||
root.gui_state.request_repaint
|
||||
|
||||
publish(:changed, @value)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,24 +7,47 @@ module CyberarmEngine
|
||||
@text = Text.new(
|
||||
text, font: @options[:font], z: @z, color: @options[:color],
|
||||
size: @options[:text_size], shadow: @options[:text_shadow],
|
||||
static: @options[:text_static],
|
||||
shadow_size: @options[:text_shadow_size],
|
||||
shadow_color: @options[:text_shadow_color]
|
||||
shadow_color: @options[:text_shadow_color],
|
||||
border: @options[:text_border],
|
||||
border_size: @options[:text_border_size],
|
||||
border_color: @options[:text_border_color]
|
||||
)
|
||||
|
||||
@raw_text = text
|
||||
end
|
||||
|
||||
def render
|
||||
@text.draw
|
||||
def update
|
||||
super
|
||||
|
||||
if @text.textobject.name != safe_style_fetch(:font)
|
||||
set_font
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block&.call(self) if @enabled
|
||||
|
||||
# return :handled
|
||||
def render
|
||||
# Gosu.clip_to is too expensive to always use so check if we actually need it.
|
||||
if @text.width > width || @text.height > height
|
||||
Gosu.clip_to(@x, @y, width, height) do
|
||||
@text.draw
|
||||
end
|
||||
else
|
||||
@text.draw
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
unless @enabled
|
||||
@text.color = @style.disabled[:color]
|
||||
else
|
||||
@text.color = @style.color
|
||||
end
|
||||
|
||||
old_width = @width
|
||||
old_height = @height
|
||||
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
@@ -33,19 +56,19 @@ module CyberarmEngine
|
||||
|
||||
handle_text_wrapping(_width)
|
||||
|
||||
@width = _width || @text.width.round
|
||||
@height = _height || @text.height.round
|
||||
@width = _width || @text.width.floor
|
||||
@height = _height || @text.height.floor
|
||||
|
||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
if (text_alignment = @options[:text_align])
|
||||
if (text_alignment = @options[:text_align] || @options[:text_h_align])
|
||||
case text_alignment
|
||||
when :left
|
||||
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
when :center
|
||||
@text.x = if @text.width <= outer_width
|
||||
@x + outer_width / 2 - @text.width / 2
|
||||
@text.x = if @text.width <= width
|
||||
@x + width / 2 - @text.width / 2
|
||||
else # Act as left aligned
|
||||
@style.border_thickness_left + @style.padding_left + @x
|
||||
end
|
||||
@@ -54,50 +77,83 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
if (vertical_alignment = @options[:text_v_align])
|
||||
case vertical_alignment
|
||||
when :center
|
||||
@text.y = if @text.height <= height
|
||||
@y + height / 2 - @text.height / 2
|
||||
else
|
||||
@style.border_thickness_top + @style.padding_top + @y
|
||||
end
|
||||
when :bottom
|
||||
@text.y = @y + outer_height - (@text.height + @style.border_thickness_bottom + @style.padding_bottom)
|
||||
end
|
||||
end
|
||||
|
||||
update_background
|
||||
|
||||
root.gui_state.request_repaint if @width != old_width || @height != old_height
|
||||
end
|
||||
|
||||
def handle_text_wrapping(max_width)
|
||||
max_width ||= @parent&.width
|
||||
max_width ||= @parent&.content_width
|
||||
max_width ||= @x - (window.width + noncontent_width)
|
||||
wrap_behavior = style.text_wrap
|
||||
copy = @raw_text.to_s.dup
|
||||
|
||||
if line_width(copy[0]) <= max_width && line_width(copy) > max_width && wrap_behavior != :none
|
||||
breaks = []
|
||||
# Only perform text wrapping: if it is enabled, is possible to wrap, and text is too long to fit on one line
|
||||
if wrap_behavior != :none && line_width(copy[0]) <= max_width && line_width(copy) > max_width
|
||||
breaks = [] # list of indexes to insert a line break
|
||||
line_start = 0
|
||||
line_end = copy.length
|
||||
line_end = copy.length
|
||||
|
||||
while line_start != copy.length
|
||||
if line_width(copy[line_start...line_end]) > max_width
|
||||
line_end = ((line_end - line_start) / 2.0)
|
||||
line_end = 1.0 if line_end <= 1
|
||||
elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
|
||||
# To small, grow!
|
||||
# TODO: find a more efficient way
|
||||
line_end += 1
|
||||
stalled = false
|
||||
stalled_interations = 0
|
||||
max_stalled_iterations = 10
|
||||
checked_copy_length = line_width(copy[line_start..line_end])
|
||||
|
||||
else # FOUND IT!
|
||||
entering_line_end = line_end.floor
|
||||
max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63
|
||||
reach = 0
|
||||
# find length of lines
|
||||
while line_width(copy[line_start..line_end]) > max_width && stalled_interations < max_stalled_iterations
|
||||
search_start = line_start
|
||||
search_end = line_end
|
||||
|
||||
if wrap_behavior == :word_wrap
|
||||
max_reach.times do |i|
|
||||
reach = i
|
||||
break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
|
||||
# Perform a binary search to find length of line
|
||||
while search_start < search_end
|
||||
midpoint = ((search_start.to_f + search_end) / 2.0).floor
|
||||
|
||||
if line_width(copy[line_start..midpoint]) > max_width
|
||||
search_end = midpoint
|
||||
else
|
||||
search_start = midpoint + 1
|
||||
end
|
||||
end
|
||||
|
||||
if wrap_behavior == :word_wrap
|
||||
word_search_end = search_end
|
||||
failed = false
|
||||
|
||||
until(copy[word_search_end].to_s.match(/[[:punct:]]| /))
|
||||
word_search_end -= 1
|
||||
|
||||
if word_search_end <= 1 || word_search_end < line_start
|
||||
failed = true
|
||||
break
|
||||
end
|
||||
|
||||
# puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}"
|
||||
line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation
|
||||
end
|
||||
|
||||
breaks << line_end.floor
|
||||
line_start = line_end.floor
|
||||
line_end = copy.length
|
||||
|
||||
break if entering_line_end == copy.length || reach == max_reach
|
||||
line_start = failed ? search_end : word_search_end + 1 # walk in front of punctuation
|
||||
else
|
||||
line_start = search_end
|
||||
end
|
||||
|
||||
breaks << line_start
|
||||
|
||||
# Prevent locking up due to outer while loop text width < max_width check not being satisfied.
|
||||
stalled = checked_copy_length == line_width(copy[line_start..line_end])
|
||||
checked_copy_length = line_width(copy[line_start..line_end])
|
||||
|
||||
stalled_interations += 1 if stalled
|
||||
stalled_interations = 0 unless stalled
|
||||
end
|
||||
|
||||
breaks.each_with_index do |pos, index|
|
||||
@@ -109,7 +165,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def line_width(text)
|
||||
(@text.textobject.markup_width(text) + noncontent_width)
|
||||
(@text.textobject.markup_width(text.to_s) + noncontent_width)
|
||||
end
|
||||
|
||||
def value
|
||||
@@ -117,13 +173,19 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
old_value = @raw_text
|
||||
@raw_text = value.to_s.chomp
|
||||
|
||||
old_width = width
|
||||
old_height = height
|
||||
recalculate
|
||||
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
if old_width != width || old_height != height
|
||||
root.gui_state.request_recalculate
|
||||
else
|
||||
recalculate
|
||||
end
|
||||
|
||||
root.gui_state.request_repaint if old_value != @raw_text
|
||||
|
||||
publish(:changed, self.value)
|
||||
end
|
||||
@@ -152,5 +214,8 @@ module CyberarmEngine
|
||||
|
||||
class ToolTip < TextBlock
|
||||
end
|
||||
|
||||
class Link < TextBlock
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ module CyberarmEngine
|
||||
|
||||
def initialize(options, block = nil)
|
||||
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)
|
||||
|
||||
@_image = @image
|
||||
|
||||
@@ -15,12 +15,19 @@ module CyberarmEngine
|
||||
|
||||
return unless enabled?
|
||||
|
||||
return :handled if respond_to?(event) && (send(event, self, *args) == :handled)
|
||||
was_handled = false
|
||||
|
||||
was_handled = true if respond_to?(event) && (send(event, self, *args) == :handled)
|
||||
|
||||
@event_handler[event].reverse_each do |handler|
|
||||
return :handled if handler.call(self, *args) == :handled
|
||||
if handler.call(self, *args) == :handled
|
||||
was_handled = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return :handled if was_handled
|
||||
|
||||
parent.publish(event, *args) if parent
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -12,7 +12,7 @@ module CyberarmEngine
|
||||
|
||||
@root_container = Element::Stack.new(gui_state: self)
|
||||
@game_objects << @root_container
|
||||
$__current_container__ = @root_container
|
||||
CyberarmEngine::Element::Container.current_container = @root_container
|
||||
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
@@ -25,13 +25,19 @@ module CyberarmEngine
|
||||
@last_mouse_pos = nil
|
||||
@dragging_element = nil
|
||||
@pending_recalculate_request = false
|
||||
@pending_element_recalculate_requests = []
|
||||
|
||||
@needs_repaint = false
|
||||
|
||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY)
|
||||
@menu = nil
|
||||
@min_drag_distance = 0
|
||||
@mouse_pos = Vector.new
|
||||
end
|
||||
|
||||
def post_setup
|
||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
||||
end
|
||||
|
||||
# throws :blur event to focused element and sets GuiState focused element
|
||||
# Does NOT throw :focus event at element or set element as focused
|
||||
def focus=(element)
|
||||
@@ -51,21 +57,44 @@ module CyberarmEngine
|
||||
@menu.draw
|
||||
end
|
||||
|
||||
if @tip.value.length.positive?
|
||||
if @tip && @tip.value.length.positive?
|
||||
Gosu.flush
|
||||
|
||||
@tip.draw
|
||||
end
|
||||
|
||||
# FIXME
|
||||
if false# defined?(GUI_DEBUG)
|
||||
Gosu.flush
|
||||
|
||||
@root_container.debug_draw
|
||||
end
|
||||
|
||||
@needs_repaint = false
|
||||
end
|
||||
|
||||
def needs_repaint?
|
||||
@needs_repaint
|
||||
end
|
||||
|
||||
def update
|
||||
if @pending_recalculate_request
|
||||
@root_container.recalculate
|
||||
@root_container.recalculate
|
||||
Stats.frame.start_timing(:gui_recalculate)
|
||||
@root_container.recalculate
|
||||
|
||||
@pending_recalculate_request = false
|
||||
|
||||
Stats.frame.end_timing(:gui_recalculate)
|
||||
end
|
||||
|
||||
Stats.frame.start_timing(:gui_element_recalculate_requests)
|
||||
|
||||
# puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?
|
||||
@pending_element_recalculate_requests.each(&:recalculate)
|
||||
@pending_element_recalculate_requests.clear
|
||||
|
||||
Stats.frame.end_timing(:gui_element_recalculate_requests)
|
||||
|
||||
if @pending_focus_request
|
||||
@pending_focus_request = false
|
||||
|
||||
@@ -74,8 +103,20 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
@menu&.update
|
||||
|
||||
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.current_state == self
|
||||
|
||||
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)
|
||||
|
||||
@@ -87,17 +128,17 @@ module CyberarmEngine
|
||||
@mouse_over.publish(:leave) if @mouse_over && new_mouse_over != @mouse_over
|
||||
@mouse_over = new_mouse_over
|
||||
|
||||
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
|
||||
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
|
||||
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
|
||||
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::MS_MIDDLE)
|
||||
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 @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
||||
@tip.value = @mouse_over.tip if @mouse_over
|
||||
@tip.x = window.mouse_x - @tip.width / 2
|
||||
@tip.x = window.mouse_x
|
||||
@tip.x = 0 if @tip.x < 0
|
||||
@tip.x = window.width - @tip.width if @tip.x + @tip.width > window.width
|
||||
@tip.y = window.mouse_y - @tip.height
|
||||
@tip.y = window.mouse_y - (@tip.height + 5)
|
||||
@tip.y = 0 if @tip.y < 0
|
||||
@tip.y = window.height - @tip.height if @tip.y + @tip.height > window.height
|
||||
@tip.update
|
||||
@@ -111,31 +152,19 @@ module CyberarmEngine
|
||||
|
||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||
@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
|
||||
|
||||
def tool_tip_delay
|
||||
250 # ms
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
super
|
||||
|
||||
case id
|
||||
when Gosu::MsLeft
|
||||
when Gosu::MS_LEFT
|
||||
redirect_mouse_button(:left)
|
||||
when Gosu::MsMiddle
|
||||
when Gosu::MS_MIDDLE
|
||||
redirect_mouse_button(:middle)
|
||||
when Gosu::MsRight
|
||||
when Gosu::MS_RIGHT
|
||||
redirect_mouse_button(:right)
|
||||
when Gosu::KbF5
|
||||
when Gosu::KB_F5
|
||||
request_recalculate
|
||||
end
|
||||
|
||||
@@ -146,19 +175,26 @@ module CyberarmEngine
|
||||
super
|
||||
|
||||
case id
|
||||
when Gosu::MsLeft
|
||||
when Gosu::MS_LEFT
|
||||
redirect_released_mouse_button(:left)
|
||||
when Gosu::MsMiddle
|
||||
when Gosu::MS_MIDDLE
|
||||
redirect_released_mouse_button(:middle)
|
||||
when Gosu::MsRight
|
||||
when Gosu::MS_RIGHT
|
||||
redirect_released_mouse_button(:right)
|
||||
when Gosu::MsWheelUp
|
||||
when Gosu::MS_WHEEL_UP
|
||||
redirect_mouse_wheel(:up)
|
||||
when Gosu::MsWheelDown
|
||||
when Gosu::MS_WHEEL_DOWN
|
||||
redirect_mouse_wheel(:down)
|
||||
end
|
||||
|
||||
@focus.button_up(id) if @focus.respond_to?(:button_up)
|
||||
|
||||
# Prevents menu from popping back up if the listbox is clicked to hide it.
|
||||
@hid_menu_for = nil
|
||||
end
|
||||
|
||||
def tool_tip_delay
|
||||
@tip.style.delay || 250 # ms
|
||||
end
|
||||
|
||||
def redirect_mouse_button(button)
|
||||
@@ -169,7 +205,7 @@ module CyberarmEngine
|
||||
@focus = nil
|
||||
end
|
||||
|
||||
if @mouse_over
|
||||
if @mouse_over && @hid_menu_for != @mouse_over
|
||||
@mouse_down_position[button] = Vector.new(window.mouse_x, window.mouse_y)
|
||||
@mouse_down_on[button] = @mouse_over
|
||||
|
||||
@@ -183,7 +219,7 @@ module CyberarmEngine
|
||||
def redirect_released_mouse_button(button)
|
||||
hide_menu if @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)
|
||||
|
||||
if @mouse_over
|
||||
if @mouse_over && @hid_menu_for != @mouse_over
|
||||
@mouse_over.publish(:"released_#{button}_mouse_button", window.mouse_x, window.mouse_y)
|
||||
if @mouse_over == @mouse_down_on[button]
|
||||
@mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x,
|
||||
@@ -222,16 +258,34 @@ module CyberarmEngine
|
||||
@pending_recalculate_request = true
|
||||
end
|
||||
|
||||
def request_recalculate_for(element)
|
||||
# element is already queued
|
||||
return if @pending_element_recalculate_requests.detect { |e| e == element }
|
||||
|
||||
@pending_element_recalculate_requests << element
|
||||
end
|
||||
|
||||
def request_focus(element)
|
||||
@pending_focus_request = true
|
||||
@pending_focus_element = element
|
||||
end
|
||||
|
||||
def request_repaint
|
||||
# puts caller[0..4]
|
||||
# puts
|
||||
@needs_repaint = true
|
||||
end
|
||||
|
||||
def show_menu(list_box)
|
||||
@menu = list_box
|
||||
end
|
||||
|
||||
def hide_menu
|
||||
return unless @menu
|
||||
|
||||
request_repaint
|
||||
|
||||
@hid_menu_for = @menu.parent
|
||||
@menu = nil
|
||||
end
|
||||
|
||||
|
||||
@@ -17,8 +17,21 @@ end
|
||||
|
||||
module CyberarmEngine
|
||||
class Style
|
||||
attr_reader :hash
|
||||
|
||||
def initialize(hash = {})
|
||||
@hash = Marshal.load(Marshal.dump(hash))
|
||||
h = hash
|
||||
# h = Marshal.load(Marshal.dump(hash))
|
||||
|
||||
h[:default] = {}
|
||||
|
||||
h.each do |key, value|
|
||||
next if value.is_a?(Hash)
|
||||
|
||||
h[:default][key] = value
|
||||
end
|
||||
|
||||
@hash = h
|
||||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
@@ -27,9 +40,8 @@ module CyberarmEngine
|
||||
|
||||
@hash[method.to_s.sub("=", "").to_sym] = args.first
|
||||
|
||||
elsif args.size == 0
|
||||
elsif args.empty?
|
||||
@hash[method]
|
||||
|
||||
else
|
||||
raise ArgumentError, "Did not expect arguments"
|
||||
end
|
||||
|
||||
@@ -64,6 +64,10 @@ module CyberarmEngine
|
||||
border_radius: 0
|
||||
},
|
||||
|
||||
Container: { # < Element (Base class for Stack and Flow)
|
||||
debug_color: Gosu::Color::YELLOW
|
||||
},
|
||||
|
||||
Button: { # < Label
|
||||
margin: 1,
|
||||
padding: 4,
|
||||
@@ -72,6 +76,7 @@ module CyberarmEngine
|
||||
border_radius: 0,
|
||||
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
||||
text_align: :center,
|
||||
text_v_align: :center,
|
||||
text_wrap: :none,
|
||||
|
||||
hover: {
|
||||
@@ -98,7 +103,12 @@ module CyberarmEngine
|
||||
caret_color: Gosu::Color::WHITE,
|
||||
caret_interval: 500,
|
||||
selection_color: Gosu::Color.rgba(255, 128, 50, 200),
|
||||
text_align: :left
|
||||
text_align: :left,
|
||||
text_static: false # static text causes issues correctly displaying caret position
|
||||
},
|
||||
|
||||
EditBox: { # < EditLine
|
||||
text_v_align: :top
|
||||
},
|
||||
|
||||
Image: { # < Element
|
||||
@@ -111,10 +121,14 @@ module CyberarmEngine
|
||||
text_size: 28,
|
||||
text_wrap: :word_wrap, # :word_wrap, :break_word, :none
|
||||
text_shadow: false,
|
||||
text_border: false,
|
||||
text_align: :left,
|
||||
font: "Arial",
|
||||
margin: 0,
|
||||
padding: 2
|
||||
padding: 2,
|
||||
disabled: {
|
||||
color: Gosu::Color.rgb(175, 175, 175),
|
||||
}
|
||||
},
|
||||
|
||||
Banner: { # < TextBlock
|
||||
@@ -146,6 +160,7 @@ module CyberarmEngine
|
||||
},
|
||||
|
||||
ToolTip: { # < TextBlock
|
||||
delay: 100, # ms
|
||||
color: Gosu::Color::WHITE,
|
||||
padding_top: 4,
|
||||
padding_bottom: 4,
|
||||
@@ -155,11 +170,28 @@ module CyberarmEngine
|
||||
border_color: 0xffaaaaaa,
|
||||
background: 0xff404040
|
||||
},
|
||||
Link: { # < TextBlock
|
||||
color: Gosu::Color::BLUE,
|
||||
border_thickness: 1,
|
||||
border_bottom_color: Gosu::Color::BLUE,
|
||||
hover: {
|
||||
color: 0xff_ff00ff,
|
||||
border_bottom_color: 0xff_ff00ff
|
||||
},
|
||||
active: {
|
||||
color: 0xff_ff0000,
|
||||
border_bottom_color: 0xff_ff0000
|
||||
}
|
||||
},
|
||||
|
||||
ToggleButton: { # < Button
|
||||
checkmark: "√"
|
||||
},
|
||||
|
||||
CheckBox: { # < Flow
|
||||
text_wrap: :none
|
||||
},
|
||||
|
||||
Progress: { # < Element
|
||||
width: 250,
|
||||
height: 36,
|
||||
|
||||
@@ -95,39 +95,58 @@ module CyberarmEngine
|
||||
Vector.new(@x, @y)
|
||||
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}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
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
|
||||
|
||||
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
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
|
||||
|
||||
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
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
|
||||
|
||||
def multiply_transform(transform)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev".freeze
|
||||
VERSION = "0.17.1".freeze
|
||||
VERSION = "0.23.0".freeze
|
||||
end
|
||||
|
||||
@@ -6,49 +6,130 @@ module CyberarmEngine
|
||||
SAMPLES = {}
|
||||
SONGS = {}
|
||||
|
||||
attr_accessor :show_cursor
|
||||
attr_accessor :show_cursor, :show_stats_plotter
|
||||
attr_writer :exit_on_opengl_error
|
||||
attr_reader :last_frame_time
|
||||
attr_reader :last_frame_time, :delta_time, :states
|
||||
|
||||
def self.now
|
||||
Gosu.milliseconds
|
||||
end
|
||||
|
||||
def self.dt
|
||||
$window.last_frame_time / 1000.0
|
||||
instance.dt
|
||||
end
|
||||
|
||||
def self.instance=(window)
|
||||
raise ArgumentError, "Expected window to be a subclass of CyberarmEngine::Window, got: #{window.class}" unless window.is_a?(CyberarmEngine::Window)
|
||||
|
||||
@@instance = window
|
||||
end
|
||||
|
||||
def self.instance
|
||||
@@instance
|
||||
end
|
||||
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false)
|
||||
@show_cursor = false
|
||||
@has_focus = false
|
||||
@show_stats_plotter = false
|
||||
|
||||
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable, borderless: borderless)
|
||||
$window = self
|
||||
Window.instance = self
|
||||
|
||||
@last_frame_time = Gosu.milliseconds - 1
|
||||
@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 = []
|
||||
@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
|
||||
|
||||
def draw
|
||||
current_state.draw if current_state
|
||||
Stats.frame.start_timing(: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
|
||||
|
||||
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
|
||||
|
||||
Stats.new_frame
|
||||
|
||||
@delta_time = (Gosu.milliseconds - @current_frame_time) * 0.001
|
||||
|
||||
current_state.update if current_state
|
||||
@last_frame_time = Gosu.milliseconds - @current_frame_time
|
||||
@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
|
||||
|
||||
def needs_cursor?
|
||||
@show_cursor
|
||||
end
|
||||
|
||||
def needs_redraw?
|
||||
current_state ? current_state.needs_redraw? : true
|
||||
end
|
||||
|
||||
def drop(filename)
|
||||
current_state&.drop(filename)
|
||||
end
|
||||
|
||||
def gamepad_connected(index)
|
||||
current_state&.gamepad_connected(index)
|
||||
end
|
||||
|
||||
def gamepad_disconnected(index)
|
||||
current_state&.gamepad_disconnected(index)
|
||||
end
|
||||
|
||||
def gain_focus
|
||||
@has_focus = true
|
||||
|
||||
current_state&.gain_focus
|
||||
end
|
||||
|
||||
def lose_focus
|
||||
@has_focus = false
|
||||
|
||||
current_state&.lose_focus
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
super
|
||||
current_state&.button_down(id)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
super
|
||||
current_state&.button_up(id)
|
||||
end
|
||||
|
||||
def close
|
||||
current_state ? current_state.close : super
|
||||
end
|
||||
|
||||
def dt
|
||||
@last_frame_time / 1000.0
|
||||
end
|
||||
@@ -61,30 +142,24 @@ module CyberarmEngine
|
||||
@exit_on_opengl_error
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
super
|
||||
current_state.button_down(id) if current_state
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
super
|
||||
current_state.button_up(id) if current_state
|
||||
end
|
||||
|
||||
def push_state(klass, options = {})
|
||||
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
|
||||
klass.setup if options[:setup]
|
||||
klass.post_setup if options[:setup]
|
||||
else
|
||||
@states << klass.new(options) if child_of?(klass, GameState)
|
||||
@states << klass.new if child_of?(klass, Element::Container)
|
||||
current_state.setup if current_state.instance_of?(klass) && options[:setup]
|
||||
current_state.post_setup if current_state.instance_of?(klass) && options[:setup]
|
||||
end
|
||||
end
|
||||
|
||||
private def child_of?(input, klass)
|
||||
return false unless input
|
||||
|
||||
input.ancestors.detect { |c| c == klass }
|
||||
end
|
||||
|
||||
@@ -92,14 +167,20 @@ module CyberarmEngine
|
||||
@states.last
|
||||
end
|
||||
|
||||
def previous_state
|
||||
if @states.size > 1 && state = @states[@states.size - 2]
|
||||
state
|
||||
end
|
||||
end
|
||||
|
||||
def pop_state
|
||||
@states.pop
|
||||
|
||||
current_state.request_repaint if current_state&.is_a?(GuiState)
|
||||
end
|
||||
|
||||
def shift_state
|
||||
@states.shift
|
||||
|
||||
current_state.request_repaint if current_state&.is_a?(GuiState)
|
||||
end
|
||||
|
||||
def has_focus?
|
||||
@has_focus
|
||||
end
|
||||
|
||||
# Sourced from https://gist.github.com/ippa/662583
|
||||
|
||||
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