mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Compare commits
15 Commits
c4e47b8e38
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 498cf06916 | |||
| 1f57dfd38c | |||
| 76a8bf95c7 | |||
| 1c25eeb32b | |||
| eabad4abd4 | |||
| b3561f02c1 | |||
| 9694cc2270 | |||
| a7df9a660d | |||
| d2f757eb23 | |||
| 0d70177de7 | |||
| dcefaeae4d | |||
| c2330ed9a9 | |||
| 615d7ec3e4 | |||
| 1644ff8a27 | |||
| d6a99b935a |
25
Gemfile.lock
Normal file
25
Gemfile.lock
Normal file
@@ -0,0 +1,25 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
cyberarm_engine (0.24.5)
|
||||
gosu (~> 1.1)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
gosu (1.4.6)
|
||||
minitest (5.25.5)
|
||||
rake (13.2.1)
|
||||
|
||||
PLATFORMS
|
||||
x64-mingw-ucrt
|
||||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
bundler (~> 2.2)
|
||||
cyberarm_engine!
|
||||
minitest (~> 5.0)
|
||||
rake (~> 13.0)
|
||||
|
||||
BUNDLED WITH
|
||||
2.6.8
|
||||
@@ -1,14 +1,15 @@
|
||||
#version 330 core
|
||||
@include "light_struct"
|
||||
|
||||
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;
|
||||
flat in Light out_lights[7];
|
||||
|
||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||
|
||||
@@ -27,43 +28,88 @@ vec4 directionalLight(Light light) {
|
||||
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);
|
||||
Light light;
|
||||
light.type = DIRECTIONAL;
|
||||
|
||||
for(int i = 0; i < out_light_count; i++)
|
||||
{
|
||||
frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
||||
}
|
||||
light.position = vec3(100, 100, 100);
|
||||
|
||||
light.diffuse = vec3(0.5, 0.5, 0.5);
|
||||
light.ambient = vec3(0.8, 0.8, 0.8);
|
||||
light.specular = vec3(0.2, 0.2, 0.2);
|
||||
|
||||
light.intensity = 1.0;
|
||||
|
||||
frag_color = texture(diffuse, out_tex_coords) * directionalLight(light);
|
||||
}
|
||||
|
||||
// #version 330 core
|
||||
// @include "light_struct"
|
||||
|
||||
// out vec4 frag_color;
|
||||
|
||||
// const int DIRECTIONAL = 0;
|
||||
// const int POINT = 1;
|
||||
// const int SPOT = 2;
|
||||
|
||||
// in vec2 out_tex_coords;
|
||||
// flat in int out_light_count;
|
||||
// flat in Light out_lights[7];
|
||||
|
||||
// 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]);
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# version 330 core
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 in_position;
|
||||
layout(location = 1) in vec3 in_color;
|
||||
|
||||
@@ -27,9 +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 "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
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 2.2"
|
||||
|
||||
@@ -6,7 +6,6 @@ else
|
||||
require "gosu"
|
||||
end
|
||||
require "json"
|
||||
require "excon"
|
||||
|
||||
require_relative "cyberarm_engine/version"
|
||||
require_relative "cyberarm_engine/stats"
|
||||
@@ -62,16 +61,10 @@ require_relative "cyberarm_engine/ui/elements/check_box"
|
||||
require_relative "cyberarm_engine/ui/elements/radio"
|
||||
require_relative "cyberarm_engine/ui/elements/progress"
|
||||
require_relative "cyberarm_engine/ui/elements/slider"
|
||||
require_relative "cyberarm_engine/ui/elements/menu"
|
||||
require_relative "cyberarm_engine/ui/elements/menu_item"
|
||||
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
require_relative "cyberarm_engine/ui/gui_state"
|
||||
|
||||
require_relative "cyberarm_engine/model"
|
||||
require_relative "cyberarm_engine/model_cache"
|
||||
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 RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
||||
|
||||
require_relative "cyberarm_engine/builtin/intro_state"
|
||||
|
||||
@@ -163,17 +163,18 @@ module CyberarmEngine
|
||||
elsif background.is_a?(Range)
|
||||
set([background.begin, background.begin, background.end, background.end])
|
||||
else
|
||||
raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed"
|
||||
raise ArgumentError, "background '#{background.inspect}' of type '#{background.class}' was not able to be processed"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Add <=> method to support Range based gradients
|
||||
module Gosu
|
||||
class Color
|
||||
def <=>(_other)
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
# NOTE: Disabled, causes stack overflow 🙃
|
||||
# module Gosu
|
||||
# class Color
|
||||
# def <=>(_other)
|
||||
# self
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require "excon"
|
||||
|
||||
module CyberarmEngine
|
||||
module Cache
|
||||
class DownloadManager
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
module CyberarmEngine
|
||||
module Common
|
||||
ImageBlob = Data.define(:to_blob, :columns, :rows)
|
||||
|
||||
def push_state(klass, options = {})
|
||||
window.push_state(klass, options)
|
||||
end
|
||||
@@ -85,7 +87,8 @@ module CyberarmEngine
|
||||
unless asset
|
||||
instance = nil
|
||||
instance = if klass == Gosu::Image
|
||||
klass.new(path, retro: retro, tileable: tileable)
|
||||
path_or_blob = path.is_a?(String) ? path : ImageBlob.new(path.to_blob, path.width, path.height)
|
||||
klass.new(path_or_blob, retro: retro, tileable: tileable)
|
||||
else
|
||||
klass.new(path)
|
||||
end
|
||||
|
||||
@@ -33,12 +33,23 @@ module Gosu
|
||||
#
|
||||
# @return [void]
|
||||
def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default)
|
||||
segments = 360.0 / segments
|
||||
|
||||
return if percentage == 0.0
|
||||
|
||||
0.step((359 * percentage), percentage > 0 ? segments : -segments) do |angle|
|
||||
angle2 = angle + segments
|
||||
angle_per_segment = 360.0 / segments
|
||||
arc_completion = 360 * percentage
|
||||
next_segment_angle = angle_per_segment
|
||||
|
||||
angle = 0
|
||||
loop do
|
||||
break if angle >= arc_completion
|
||||
|
||||
if angle + angle_per_segment > arc_completion
|
||||
next_segment_angle = arc_completion - angle
|
||||
else
|
||||
next_segment_angle = angle_per_segment
|
||||
end
|
||||
|
||||
angle2 = angle + next_segment_angle
|
||||
|
||||
point_a_left_x = x + Gosu.offset_x(angle, radius - thickness)
|
||||
point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)
|
||||
@@ -93,6 +104,8 @@ module Gosu
|
||||
z, mode
|
||||
)
|
||||
end
|
||||
|
||||
angle += next_segment_angle
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module CyberarmEngine
|
||||
class Model
|
||||
include OpenGL
|
||||
include 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,
|
||||
@@ -49,9 +52,9 @@ module CyberarmEngine
|
||||
|
||||
@objects.each { |o| @vertex_count += o.vertices.size }
|
||||
|
||||
# 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"
|
||||
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)
|
||||
@@ -178,7 +181,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def build_collision_tree
|
||||
@aabb_tree = IMICFPS::AABBTree.new
|
||||
@aabb_tree = AABBTree.new
|
||||
|
||||
@faces.each do |face|
|
||||
box = BoundingBox.new
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module CyberarmEngine
|
||||
class Model
|
||||
class ModelObject
|
||||
class Mesh
|
||||
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
||||
attr_accessor :faces, :scale
|
||||
|
||||
@@ -48,12 +48,12 @@ module CyberarmEngine
|
||||
if _model
|
||||
@model.current_object = _model
|
||||
else
|
||||
raise "Couldn't find ModelObject!"
|
||||
raise "Couldn't find Mesh!"
|
||||
end
|
||||
end
|
||||
|
||||
def change_object(id, name)
|
||||
@model.objects << Model::ModelObject.new(id, name)
|
||||
@model.objects << Model::Mesh.new(id, name)
|
||||
@model.current_object = @model.objects.last
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
begin
|
||||
require "opengl"
|
||||
require "glu"
|
||||
rescue LoadError
|
||||
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
||||
exit(1)
|
||||
@@ -8,7 +9,7 @@ end
|
||||
module CyberarmEngine
|
||||
def gl_error?
|
||||
e = glGetError
|
||||
if e != GL_NO_ERROR
|
||||
if e != OpenGL::GL_NO_ERROR
|
||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||
warn " #{gluErrorString(e)} (#{e})\n"
|
||||
exit if Window.instance&.exit_on_opengl_error?
|
||||
@@ -38,3 +39,15 @@ require_relative "opengl/renderer/g_buffer"
|
||||
require_relative "opengl/renderer/bounding_box_renderer"
|
||||
require_relative "opengl/renderer/opengl_renderer"
|
||||
require_relative "opengl/renderer/renderer"
|
||||
|
||||
require_relative "trees/aabb_tree_debug"
|
||||
require_relative "trees/aabb_node"
|
||||
require_relative "trees/aabb_tree"
|
||||
|
||||
require_relative "model"
|
||||
require_relative "model_cache"
|
||||
require_relative "model/material"
|
||||
require_relative "model/mesh"
|
||||
require_relative "model/parser"
|
||||
require_relative "model/parsers/wavefront_parser"
|
||||
require_relative "model/parsers/collada_parser" if RUBY_ENGINE != "mruby" && defined?(Nokogiri)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
module CyberarmEngine
|
||||
class Light
|
||||
include OpenGL
|
||||
|
||||
DIRECTIONAL = 0
|
||||
POINT = 1
|
||||
SPOT = 2
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module CyberarmEngine
|
||||
class PerspectiveCamera
|
||||
include OpenGL
|
||||
include GLU
|
||||
|
||||
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
||||
:min_view_distance, :max_view_distance
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
module CyberarmEngine
|
||||
class GBuffer
|
||||
include OpenGL
|
||||
|
||||
attr_reader :screen_vbo, :vertices, :uvs
|
||||
attr_reader :width, :height
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
module CyberarmEngine
|
||||
class OpenGLRenderer
|
||||
include OpenGL
|
||||
include CyberarmEngine
|
||||
|
||||
@@immediate_mode_warning = false
|
||||
|
||||
attr_accessor :show_wireframe
|
||||
|
||||
@@ -2,6 +2,7 @@ module CyberarmEngine
|
||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||
class Shader
|
||||
include OpenGL
|
||||
|
||||
@@shaders = {} # Cache for {Shader} instances
|
||||
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
||||
|
||||
@@ -298,6 +299,7 @@ module CyberarmEngine
|
||||
# @see Shader.use Shader.use
|
||||
def use(&block)
|
||||
return unless compiled?
|
||||
|
||||
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||
|
||||
Shader.active_shader = self
|
||||
|
||||
@@ -115,11 +115,11 @@ module CyberarmEngine
|
||||
@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)
|
||||
@max_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @padding, z: z, size: @text_size, border: true, static: true)
|
||||
@avg_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @padding + @height / 2 - @text_size / 2, z: z, size: @text_size, border: true, static: true)
|
||||
@min_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @height - (@text_size + @padding / 2), z: z, size: @text_size, border: true, static: true)
|
||||
|
||||
@timings_label = CyberarmEngine::Text.new("", x: x + @padding + @width + @padding, y: y + @padding, z: z, size: @text_size, border: true)
|
||||
@data_label = CyberarmEngine::Text.new("", x: x + @padding + @width + @padding, y: y + @padding, z: z, size: @text_size, border: true, static: true)
|
||||
|
||||
@frame_stats = []
|
||||
@graphs = {
|
||||
@@ -146,10 +146,15 @@ module CyberarmEngine
|
||||
slice += 1
|
||||
end
|
||||
|
||||
max_node = CyberarmEngine::Stats.frames.select(&:complete?).map { |f| f.frame_timing.duration }.max
|
||||
scale = 1
|
||||
scale = (@height - @padding).to_f / max_node
|
||||
scale = 1 if scale > 1
|
||||
|
||||
nodes.each_with_index do |cluster, i|
|
||||
break if cluster.empty?
|
||||
|
||||
@graphs[:frame_timings] << CyberarmEngine::Vector.new(@position.x + @padding + 1 * i, (@position.y + @height - @padding) - cluster.max)
|
||||
@graphs[:frame_timings] << CyberarmEngine::Vector.new(@position.x + @padding + 1 * i, (@position.y + @height - @padding) - cluster.max * scale)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -159,9 +164,9 @@ module CyberarmEngine
|
||||
|
||||
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"
|
||||
@max_timing_label.text = "<c=d44>Max:</c> #{@frame_stats.map { |f| f.frame_timing.duration }.max.to_s.rjust(3, " ")}ms"
|
||||
@avg_timing_label.text = "<c=f80>Avg:</c> #{(@frame_stats.map { |f| f.frame_timing.duration }.sum / @frame_stats.size).to_s.rjust(3, " ")}ms"
|
||||
@min_timing_label.text = "<c=0d0>Min:</c> #{@frame_stats.map { |f| f.frame_timing.duration }.min.to_s.rjust(3, " ")}ms"
|
||||
|
||||
Gosu.draw_rect(@position.x, @position.y, @width, @height, 0xaa_222222, @position.z)
|
||||
Gosu.draw_rect(@position.x + @padding, @position.y + @padding, @width - @padding * 2, @height - @padding * 2, 0xaa_222222, @position.z)
|
||||
@@ -173,19 +178,22 @@ module CyberarmEngine
|
||||
@min_timing_label.draw
|
||||
|
||||
# TODO: Make this optional
|
||||
draw_timings
|
||||
draw_timings_and_counters
|
||||
end
|
||||
|
||||
def draw_graphs
|
||||
Gosu.draw_path(@graphs[:frame_timings], Gosu::Color::WHITE, Float::INFINITY)
|
||||
end
|
||||
|
||||
def draw_timings
|
||||
def draw_timings_and_counters
|
||||
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
|
||||
@data_label.text = "<c=f8f>COUNTERS:</c>\n#{frame.counters.map { |t, v| "#{t}: #{v}" }.join("\n")}\n\n"\
|
||||
"<c=f80>TIMINGS:</c>\n#{frame.attempted_multitiming? ? "<c=d00>Attempted Multitiming!\nTimings may be inaccurate for:\n#{frame.multitimings.map { |m, _| m}.join("\n") }</c>\n\n" : ''}#{frame.timings.map { |t, v| "#{t}: #{v.duration}ms" }.join("\n")}"
|
||||
Gosu.draw_rect(@data_label.x - @padding, @data_label.y - @padding, @data_label.width + @padding * 2, @data_label.height + @padding * 2, 0xdd_222222, @position.z)
|
||||
@data_label.draw
|
||||
|
||||
# puts "Recalcs this frame: #{frame.counters[:gui_recalculations]} [dt: #{(CyberarmEngine::Window.dt * 1000).round} ms]" if frame.counters[:gui_recalculations] && frame.counters[:gui_recalculations].positive?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
126
lib/cyberarm_engine/trees/aabb_node.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class AABBTree
|
||||
class AABBNode
|
||||
attr_accessor :bounding_box, :parent, :object
|
||||
attr_reader :a, :b
|
||||
|
||||
def initialize(parent:, object:, bounding_box:)
|
||||
@parent = parent
|
||||
@object = object
|
||||
@bounding_box = bounding_box
|
||||
|
||||
@a = nil
|
||||
@b = nil
|
||||
end
|
||||
|
||||
def a=(leaf)
|
||||
@a = leaf
|
||||
@a.parent = self
|
||||
end
|
||||
|
||||
def b=(leaf)
|
||||
@b = leaf
|
||||
@b.parent = self
|
||||
end
|
||||
|
||||
def leaf?
|
||||
@object
|
||||
end
|
||||
|
||||
def insert_subtree(leaf)
|
||||
if leaf?
|
||||
new_node = AABBNode.new(parent: nil, object: nil, bounding_box: @bounding_box.union(leaf.bounding_box))
|
||||
|
||||
new_node.a = self
|
||||
new_node.b = leaf
|
||||
|
||||
new_node
|
||||
else
|
||||
cost_a = @a.bounding_box.volume + @b.bounding_box.union(leaf.bounding_box).volume
|
||||
cost_b = @b.bounding_box.volume + @a.bounding_box.union(leaf.bounding_box).volume
|
||||
|
||||
if cost_a == cost_b
|
||||
cost_a = @a.proximity(leaf)
|
||||
cost_b = @b.proximity(leaf)
|
||||
end
|
||||
|
||||
if cost_b < cost_a
|
||||
self.b = @b.insert_subtree(leaf)
|
||||
else
|
||||
self.a = @a.insert_subtree(leaf)
|
||||
end
|
||||
|
||||
@bounding_box = @bounding_box.union(leaf.bounding_box)
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def search_subtree(collider, items = [])
|
||||
if @bounding_box.intersect?(collider)
|
||||
if leaf?
|
||||
items << self
|
||||
else
|
||||
@a.search_subtree(collider, items)
|
||||
@b.search_subtree(collider, items)
|
||||
end
|
||||
end
|
||||
|
||||
items
|
||||
end
|
||||
|
||||
def remove_subtree(leaf)
|
||||
if leaf
|
||||
self
|
||||
elsif leaf.parent == self
|
||||
other_child = other(leaf)
|
||||
other_child.parent = @parent
|
||||
other_child
|
||||
else
|
||||
leaf.parent.disown_child(leaf)
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def other(leaf)
|
||||
@a == leaf ? @b : @a
|
||||
end
|
||||
|
||||
def disown_child(leaf)
|
||||
value = other(leaf)
|
||||
raise "Can not replace child of a leaf!" if @parent.leaf?
|
||||
raise "Node is not a child of parent!" unless leaf.child_of?(@parent)
|
||||
|
||||
if @parent.a == self
|
||||
@parent.a = value
|
||||
else
|
||||
@parent.b = value
|
||||
end
|
||||
|
||||
@parent.update_bounding_box
|
||||
end
|
||||
|
||||
def child_of?(leaf)
|
||||
self == leaf.a || self == leaf.b
|
||||
end
|
||||
|
||||
def proximity(leaf)
|
||||
(@bounding_box - leaf.bounding_box).sum.abs
|
||||
end
|
||||
|
||||
def update_bounding_box
|
||||
node = self
|
||||
|
||||
unless node.leaf?
|
||||
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||
|
||||
while (node = node.parent)
|
||||
node.bounding_box = node.a.bounding_box.union(node.b.bounding_box)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
55
lib/cyberarm_engine/trees/aabb_tree.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
class AABBTree
|
||||
include AABBTreeDebug
|
||||
|
||||
attr_reader :root, :objects, :branches, :leaves
|
||||
|
||||
def initialize
|
||||
@objects = {}
|
||||
@root = nil
|
||||
@branches = 0
|
||||
@leaves = 0
|
||||
end
|
||||
|
||||
def insert(object, bounding_box)
|
||||
raise "BoundingBox can't be nil!" unless bounding_box
|
||||
raise "Object can't be nil!" unless object
|
||||
# raise "Object already in tree!" if @objects[object] # FIXME
|
||||
|
||||
leaf = AABBNode.new(parent: nil, object: object, bounding_box: bounding_box.dup)
|
||||
@objects[object] = leaf
|
||||
|
||||
insert_leaf(leaf)
|
||||
end
|
||||
|
||||
def insert_leaf(leaf)
|
||||
@root = @root ? @root.insert_subtree(leaf) : leaf
|
||||
end
|
||||
|
||||
def update(object, bounding_box)
|
||||
leaf = remove(object)
|
||||
leaf.bounding_box = bounding_box
|
||||
insert_leaf(leaf)
|
||||
end
|
||||
|
||||
# Returns a list of all objects that collided with collider
|
||||
def search(collider, return_nodes = false)
|
||||
items = []
|
||||
if @root
|
||||
items = @root.search_subtree(collider)
|
||||
items.map!(&:object) unless return_nodes
|
||||
end
|
||||
|
||||
items
|
||||
end
|
||||
|
||||
def remove(object)
|
||||
leaf = @objects.delete(object)
|
||||
@root = @root.remove_subtree(leaf) if leaf
|
||||
|
||||
leaf
|
||||
end
|
||||
end
|
||||
end
|
||||
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
29
lib/cyberarm_engine/trees/aabb_tree_debug.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module CyberarmEngine
|
||||
# Gets included into AABBTree
|
||||
module AABBTreeDebug
|
||||
def inspect
|
||||
@branches = 0
|
||||
@leaves = 0
|
||||
if @root
|
||||
node = @root
|
||||
|
||||
debug_search(node.a)
|
||||
debug_search(node.b)
|
||||
end
|
||||
|
||||
puts "<#{self.class}:#{object_id}> has #{@branches} branches and #{@leaves} leaves"
|
||||
end
|
||||
|
||||
def debug_search(node)
|
||||
if node.leaf?
|
||||
@leaves += 1
|
||||
else
|
||||
@branches += 1
|
||||
debug_search(node.a)
|
||||
debug_search(node.b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,12 +8,8 @@ module CyberarmEngine
|
||||
container(CyberarmEngine::Element::Stack, options, &block)
|
||||
end
|
||||
|
||||
# TODO: Remove in version 0.16.0+
|
||||
def label(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
add_element(Element::TextBlock.new(text, options, block))
|
||||
def menu(options = {}, &block)
|
||||
container(CyberarmEngine::Element::Menu, options, &block)
|
||||
end
|
||||
|
||||
[
|
||||
@@ -27,72 +23,79 @@ module CyberarmEngine
|
||||
"Link"
|
||||
].each do |const|
|
||||
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element.const_get(const).new(text, options, block))
|
||||
end
|
||||
end
|
||||
|
||||
def button(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) })
|
||||
end
|
||||
|
||||
def list_box(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) })
|
||||
end
|
||||
|
||||
def menu_item(text, options = {}, &block)
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::MenuItem.new(text, options, block) { block.call if block.is_a?(Proc) })
|
||||
end
|
||||
|
||||
def edit_line(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::EditLine.new(text, options, block))
|
||||
end
|
||||
|
||||
def edit_box(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::EditBox.new(text, options, block))
|
||||
end
|
||||
|
||||
def toggle_button(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::ToggleButton.new(options, block))
|
||||
end
|
||||
|
||||
def check_box(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::CheckBox.new(text, options, block))
|
||||
end
|
||||
|
||||
def image(path, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::Image.new(path, options, block))
|
||||
end
|
||||
|
||||
def progress(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::Progress.new(options, block))
|
||||
end
|
||||
|
||||
def slider(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
add_element(Element::Slider.new(options, block))
|
||||
end
|
||||
@@ -102,7 +105,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def theme(theme)
|
||||
element_parent.options[:theme] = theme
|
||||
element_parent.options[:theme] ||= theme
|
||||
end
|
||||
|
||||
def current_theme
|
||||
@@ -120,8 +123,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
private def container(klass, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
options[:parent] ||= element_parent
|
||||
options[:theme] ||= current_theme
|
||||
|
||||
_container = klass.new(options, block)
|
||||
|
||||
@@ -129,7 +132,7 @@ module CyberarmEngine
|
||||
CyberarmEngine::Element::Container.current_container = _container
|
||||
|
||||
_container.build
|
||||
_container.parent.add(_container)
|
||||
_container.parent.add(_container) unless _container.is_a?(CyberarmEngine::Element::Menu)
|
||||
|
||||
CyberarmEngine::Element::Container.current_container = old_parent
|
||||
|
||||
|
||||
@@ -71,8 +71,19 @@ module CyberarmEngine
|
||||
root.gui_state.request_repaint
|
||||
end
|
||||
|
||||
def safe_style_fetch(*args)
|
||||
@style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args)
|
||||
def safe_style_fetch(key, fallback_key = nil)
|
||||
# Attempt to return value for requested key
|
||||
v = @style.hash.dig(@style_event, key)
|
||||
return v if v
|
||||
|
||||
# Attempt to return overriding value
|
||||
if fallback_key
|
||||
v = @style.hash.dig(@style_event, fallback_key)
|
||||
return v if v
|
||||
end
|
||||
|
||||
# Fallback to default style
|
||||
@style.hash.dig(:default, key) || default(key)
|
||||
end
|
||||
|
||||
def set_static_position
|
||||
@@ -104,10 +115,10 @@ module CyberarmEngine
|
||||
|
||||
@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
|
||||
@style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge)
|
||||
@style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge)
|
||||
@style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge)
|
||||
@style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge)
|
||||
end
|
||||
|
||||
def set_background_image
|
||||
@@ -121,19 +132,19 @@ module CyberarmEngine
|
||||
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
|
||||
@style.border_thickness_left = safe_style_fetch(:border_thickness_left, :border_thickness)
|
||||
@style.border_thickness_right = safe_style_fetch(:border_thickness_right, :border_thickness)
|
||||
@style.border_thickness_top = safe_style_fetch(:border_thickness_top, :border_thickness)
|
||||
@style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom, :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_color_left = safe_style_fetch(:border_color_left, :border_color)
|
||||
@style.border_color_right = safe_style_fetch(:border_color_right, :border_color)
|
||||
@style.border_color_top = safe_style_fetch(:border_color_top, :border_color)
|
||||
@style.border_color_bottom = safe_style_fetch(:border_color_bottom, :border_color)
|
||||
|
||||
@style.border_canvas.color = [
|
||||
@style.border_color_top,
|
||||
@@ -146,19 +157,19 @@ module CyberarmEngine
|
||||
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
|
||||
@style.padding_left = safe_style_fetch(:padding_left, :padding)
|
||||
@style.padding_right = safe_style_fetch(:padding_right, :padding)
|
||||
@style.padding_top = safe_style_fetch(:padding_top, :padding)
|
||||
@style.padding_bottom = safe_style_fetch(:padding_bottom, :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
|
||||
@style.margin_left = safe_style_fetch(:margin_left, :margin)
|
||||
@style.margin_right = safe_style_fetch(:margin_right, :margin)
|
||||
@style.margin_top = safe_style_fetch(:margin_top, :margin)
|
||||
@style.margin_bottom = safe_style_fetch(:margin_bottom, :margin)
|
||||
end
|
||||
|
||||
def update_styles(event = :default)
|
||||
@@ -169,9 +180,7 @@ module CyberarmEngine
|
||||
|
||||
return if self.is_a?(ToolTip)
|
||||
|
||||
if old_width != width || old_height != height
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
|
||||
stylize
|
||||
end
|
||||
@@ -186,6 +195,10 @@ module CyberarmEngine
|
||||
|
||||
event(:mouse_wheel_up)
|
||||
event(:mouse_wheel_down)
|
||||
event(:scroll_jump_to_top)
|
||||
event(:scroll_jump_to_end)
|
||||
event(:scroll_page_up)
|
||||
event(:scroll_page_down)
|
||||
|
||||
event(:enter)
|
||||
event(:hover)
|
||||
@@ -318,8 +331,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
# FIXME
|
||||
return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||
return if CyberarmEngine.const_defined?("GUI_DEBUG_ONLY_ELEMENT") && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||
|
||||
Gosu.draw_line(
|
||||
x, y, @debug_color,
|
||||
@@ -337,7 +349,7 @@ module CyberarmEngine
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x, outer_height, @debug_color,
|
||||
x, y + outer_height, @debug_color,
|
||||
x, y, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
@@ -562,7 +574,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def recalculate_if_size_changed
|
||||
if !is_a?(ToolTip) && (@old_width != width || @old_height != height)
|
||||
if @parent && !is_a?(ToolTip) && (@old_width != width || @old_height != height)
|
||||
root.gui_state.request_recalculate
|
||||
|
||||
@old_width = width
|
||||
@@ -613,7 +625,19 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def recalculate
|
||||
raise "#{self.class}#recalculate was not overridden!"
|
||||
old_width = width
|
||||
old_height = height
|
||||
|
||||
stylize
|
||||
layout
|
||||
|
||||
root.gui_state.request_recalculate if @parent && !is_a?(ToolTip) && (width != old_width || height != old_height)
|
||||
root.gui_state.request_repaint if width != old_width || height != old_height
|
||||
|
||||
root.gui_state.menu.recalculate if root.gui_state.menu && root.gui_state.menu.parent == self
|
||||
end
|
||||
|
||||
def layout
|
||||
end
|
||||
|
||||
def reposition
|
||||
|
||||
@@ -34,7 +34,7 @@ module CyberarmEngine
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def recalculate
|
||||
def layout
|
||||
unless @enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@text.color = @style.disabled[:color]
|
||||
|
||||
@@ -4,16 +4,16 @@ module CyberarmEngine
|
||||
include Common
|
||||
|
||||
attr_accessor :stroke_color, :fill_color
|
||||
attr_reader :children, :gui_state, :scroll_position
|
||||
attr_reader :children, :gui_state, :scroll_position, :scroll_target_position
|
||||
|
||||
def self.current_container
|
||||
@@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
|
||||
@current_container = container
|
||||
end
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@@ -26,6 +26,11 @@ module CyberarmEngine
|
||||
@scroll_chunk = 120
|
||||
@scroll_speed = 40
|
||||
|
||||
if @gui_state
|
||||
@width = window.width
|
||||
@height = window.height
|
||||
end
|
||||
|
||||
@text_color = options[:color]
|
||||
|
||||
@children = []
|
||||
@@ -34,7 +39,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def build
|
||||
@block.call(self) if @block
|
||||
@block&.call(self)
|
||||
|
||||
root.gui_state.request_recalculate_for(self)
|
||||
end
|
||||
@@ -49,24 +54,24 @@ module CyberarmEngine
|
||||
root.gui_state.request_recalculate_for(self) if @children.delete(element)
|
||||
end
|
||||
|
||||
def clear(&block)
|
||||
@children.clear
|
||||
|
||||
def append(&block)
|
||||
old_container = CyberarmEngine::Element::Container.current_container
|
||||
|
||||
CyberarmEngine::Element::Container.current_container = self
|
||||
block.call(self) if block
|
||||
block&.call(self)
|
||||
|
||||
CyberarmEngine::Element::Container.current_container = old_container
|
||||
|
||||
root.gui_state.request_recalculate_for(self)
|
||||
end
|
||||
|
||||
def append(&block)
|
||||
def clear(&block)
|
||||
@children.clear
|
||||
|
||||
old_container = CyberarmEngine::Element::Container.current_container
|
||||
|
||||
CyberarmEngine::Element::Container.current_container = self
|
||||
block.call(self) if block
|
||||
block&.call(self)
|
||||
|
||||
CyberarmEngine::Element::Container.current_container = old_container
|
||||
|
||||
@@ -89,13 +94,11 @@ module CyberarmEngine
|
||||
def debug_draw
|
||||
super
|
||||
|
||||
@children.each do |child|
|
||||
child.debug_draw
|
||||
end
|
||||
@children.each(&:debug_draw)
|
||||
end
|
||||
|
||||
def update
|
||||
update_scroll
|
||||
update_scroll if @style.scroll
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
@@ -111,7 +114,7 @@ module CyberarmEngine
|
||||
|
||||
case child
|
||||
when Container
|
||||
if element = child.hit_element?(child_x, child_y)
|
||||
if (element = child.hit_element?(child_x, child_y))
|
||||
return element
|
||||
end
|
||||
else
|
||||
@@ -128,23 +131,16 @@ module CyberarmEngine
|
||||
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
|
||||
dt = window.dt.clamp(0.000001, 0.025)
|
||||
@scroll_position.x += (((@scroll_target_position.x - @scroll_position.x) * (@scroll_speed / 4.0) * 0.98) * dt).round
|
||||
@scroll_position.y += (((@scroll_target_position.y - @scroll_position.y) * (@scroll_speed / 4.0) * 0.98) * dt).round
|
||||
|
||||
# Scrolled PAST top
|
||||
if @scroll_position.y > 0
|
||||
@scroll_target_position.y = 0
|
||||
|
||||
# Scrolled PAST bottom
|
||||
elsif @scroll_position.y.abs > max_scroll_height
|
||||
elsif @scroll_position.y < -max_scroll_height
|
||||
@scroll_target_position.y = -max_scroll_height
|
||||
end
|
||||
|
||||
@@ -162,16 +158,16 @@ module CyberarmEngine
|
||||
|
||||
return unless visible?
|
||||
|
||||
Stats.frame.increment(:gui_recalculations)
|
||||
|
||||
stylize
|
||||
Stats.frame&.increment(:gui_recalculations)
|
||||
|
||||
# s = Gosu.milliseconds
|
||||
|
||||
stylize
|
||||
layout
|
||||
|
||||
old_width = @width
|
||||
old_height = @height
|
||||
# Old sizes MUST be determined AFTER call to layout
|
||||
old_width = width
|
||||
old_height = height
|
||||
|
||||
@cached_scroll_width = nil
|
||||
@cached_scroll_height = nil
|
||||
@@ -214,6 +210,7 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
t = Gosu.milliseconds
|
||||
# Move children to parent after positioning
|
||||
@children.each do |child|
|
||||
child.x += (@x + @style.border_thickness_left) - style.margin_left
|
||||
@@ -223,35 +220,28 @@ module CyberarmEngine
|
||||
child.recalculate
|
||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||
|
||||
Stats.frame.increment(:gui_recalculations)
|
||||
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)}"
|
||||
puts "TOOK: #{Gosu.milliseconds - t}ms to recalculate #{self.class}:0x#{object_id.to_s(16)}'s #{@children.count} children"
|
||||
|
||||
update_background
|
||||
|
||||
# Fixes resized container scrolled past bottom
|
||||
self.scroll_top = -@scroll_position.y
|
||||
@scroll_target_position.y = @scroll_position.y
|
||||
if old_height != @height
|
||||
self.scroll_top = -@scroll_position.y
|
||||
@scroll_target_position.y = @scroll_position.y
|
||||
end
|
||||
|
||||
# Fixes resized container that is scrolled down from being stuck overscrolled when resized
|
||||
if scroll_height < height
|
||||
@scroll_target_position.y = 0
|
||||
end
|
||||
|
||||
# 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
|
||||
recalculate_if_size_changed
|
||||
|
||||
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
|
||||
end
|
||||
|
||||
def layout
|
||||
@@ -312,15 +302,15 @@ module CyberarmEngine
|
||||
return unless @style.scroll
|
||||
|
||||
# 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 unless height < scroll_height
|
||||
|
||||
return :handled
|
||||
if @scroll_target_position.y.positive?
|
||||
@scroll_target_position.y = @scroll_chunk
|
||||
else
|
||||
@scroll_target_position.y += @scroll_chunk
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def mouse_wheel_down(sender, x, y)
|
||||
@@ -328,13 +318,51 @@ module CyberarmEngine
|
||||
|
||||
return unless height < scroll_height
|
||||
|
||||
if @scroll_target_position.y > 0
|
||||
if @scroll_target_position.y.positive?
|
||||
@scroll_target_position.y = -@scroll_chunk
|
||||
else
|
||||
@scroll_target_position.y -= @scroll_chunk
|
||||
end
|
||||
|
||||
return :handled
|
||||
:handled
|
||||
end
|
||||
|
||||
def scroll_jump_to_top(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
@scroll_position.y = 0
|
||||
@scroll_target_position.y = 0
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def scroll_jump_to_end(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
@scroll_position.y = -max_scroll_height
|
||||
@scroll_target_position.y = -max_scroll_height
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def scroll_page_up(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
@scroll_position.y += height
|
||||
@scroll_position.y = 0 if @scroll_position.y > 0
|
||||
@scroll_target_position.y = @scroll_position.y
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def scroll_page_down(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
@scroll_position.y -= height
|
||||
@scroll_position.y = -max_scroll_height if @scroll_position.y < -max_scroll_height
|
||||
@scroll_target_position.y = @scroll_position.y
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def scroll_top
|
||||
|
||||
@@ -168,7 +168,7 @@ module CyberarmEngine
|
||||
@last_text = @text.text
|
||||
@last_pos = caret_pos
|
||||
|
||||
if caret_pos.between?(@offset_x, @width + @offset_x)
|
||||
if caret_pos.between?(@offset_x + 1, @width + @offset_x)
|
||||
# Do nothing
|
||||
|
||||
elsif caret_pos < @offset_x
|
||||
@@ -197,7 +197,7 @@ module CyberarmEngine
|
||||
|
||||
def text_input_position_for(method)
|
||||
if @type == :password
|
||||
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
|
||||
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length) - @style.border_thickness_left
|
||||
else
|
||||
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
|
||||
end
|
||||
@@ -278,7 +278,7 @@ module CyberarmEngine
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
def layout
|
||||
super
|
||||
|
||||
@width = dimensional_size(@style.width, :width) || default(:width)
|
||||
|
||||
@@ -27,7 +27,7 @@ module CyberarmEngine
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
def layout
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
|
||||
@@ -12,21 +12,7 @@ module CyberarmEngine
|
||||
|
||||
@style.background_canvas.background = default(:background)
|
||||
|
||||
# TODO: "Clean Up" into own class?
|
||||
@menu = Stack.new(parent: self, theme: @options[:theme])
|
||||
@menu.define_singleton_method(:recalculate_menu) do
|
||||
@x = @__list_box.x
|
||||
@y = @__list_box.y + @__list_box.height
|
||||
|
||||
@y = @__list_box.y - height if @y + height > window.height
|
||||
end
|
||||
@menu.instance_variable_set(:"@__list_box", self)
|
||||
|
||||
def @menu.recalculate
|
||||
super
|
||||
|
||||
recalculate_menu
|
||||
end
|
||||
@menu = Menu.new(parent: self, theme: @options[:theme])
|
||||
|
||||
self.choose = @choose
|
||||
end
|
||||
@@ -40,7 +26,13 @@ module CyberarmEngine
|
||||
|
||||
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
|
||||
|
||||
unless valid
|
||||
warn "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}"
|
||||
item = @items.first
|
||||
|
||||
raise "No items list" unless item
|
||||
end
|
||||
|
||||
@choose = item
|
||||
|
||||
@@ -62,39 +54,25 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def show_menu
|
||||
@menu.clear
|
||||
@menu.clear do
|
||||
|
||||
@menu.style.width = width
|
||||
@menu.style.width = width
|
||||
|
||||
@items.each do |item|
|
||||
next if item == self.value
|
||||
@items.each do |item|
|
||||
# prevent already selected item from appearing in list
|
||||
# NOTE: Remove this? Might be kinda confusing...
|
||||
next if item == self.value
|
||||
|
||||
btn = Button.new(
|
||||
item,
|
||||
{
|
||||
parent: @menu,
|
||||
width: 1.0,
|
||||
theme: @options[:theme],
|
||||
margin: 0,
|
||||
border_color: 0x00ffffff
|
||||
},
|
||||
proc do
|
||||
root.gui_state.menu_item(item, width: 1.0, margin: 0, border_color: 0x00ffffff) do
|
||||
self.choose = item
|
||||
@block&.call(self.value)
|
||||
end
|
||||
)
|
||||
|
||||
@menu.add(btn)
|
||||
end
|
||||
end
|
||||
|
||||
recalculate
|
||||
|
||||
root.gui_state.show_menu(@menu)
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
@menu.recalculate
|
||||
@menu.show
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
27
lib/cyberarm_engine/ui/elements/menu.rb
Normal file
27
lib/cyberarm_engine/ui/elements/menu.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Menu < Stack
|
||||
def recalculate
|
||||
super
|
||||
|
||||
recalculate_menu
|
||||
end
|
||||
|
||||
def recalculate_menu
|
||||
# FIXME: properly find scrollable parent, if any.
|
||||
parent_scroll_top = parent&.parent ? parent.parent.scroll_top : 0
|
||||
|
||||
@x = @parent.x
|
||||
@y = parent_scroll_top + @parent.y + @parent.height
|
||||
|
||||
@y = (parent_scroll_top + @parent.y) - height if @y + height > window.height
|
||||
end
|
||||
|
||||
def show
|
||||
recalculate
|
||||
|
||||
root.gui_state.show_menu(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/cyberarm_engine/ui/elements/menu_item.rb
Normal file
6
lib/cyberarm_engine/ui/elements/menu_item.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class MenuItem < Button
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -19,7 +19,7 @@ module CyberarmEngine
|
||||
@fraction_background.draw
|
||||
end
|
||||
|
||||
def recalculate
|
||||
def layout
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
@width = _width
|
||||
|
||||
@@ -33,7 +33,7 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :step_size, :value
|
||||
attr_reader :value
|
||||
attr_accessor :range, :step_size
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
|
||||
@@ -38,16 +38,13 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
def layout
|
||||
unless @enabled
|
||||
@text.color = @style.disabled[:color]
|
||||
else
|
||||
@text.color = @style.color
|
||||
end
|
||||
|
||||
old_width = @width
|
||||
old_height = @height
|
||||
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
@@ -91,9 +88,6 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
update_background
|
||||
|
||||
root.gui_state.request_repaint if @width != old_width || @height != old_height
|
||||
recalculate_if_size_changed
|
||||
end
|
||||
|
||||
def handle_text_wrapping(max_width)
|
||||
|
||||
@@ -7,8 +7,6 @@ module CyberarmEngine
|
||||
if options.dig(:theme, :ToggleButton, :checkmark_image)
|
||||
options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size]
|
||||
super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)
|
||||
|
||||
@_image = @image
|
||||
else
|
||||
super(options[:checkmark], options, block)
|
||||
end
|
||||
@@ -16,10 +14,8 @@ module CyberarmEngine
|
||||
@value = options[:checked] || false
|
||||
|
||||
if @value
|
||||
@image = @_image if @_image
|
||||
@raw_text = @options[:checkmark]
|
||||
else
|
||||
@image = nil
|
||||
@raw_text = ""
|
||||
end
|
||||
end
|
||||
@@ -32,6 +28,14 @@ module CyberarmEngine
|
||||
:handled
|
||||
end
|
||||
|
||||
def render
|
||||
if @image
|
||||
draw_image if @value
|
||||
else
|
||||
draw_text
|
||||
end
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
return if @image
|
||||
@@ -49,10 +53,8 @@ module CyberarmEngine
|
||||
@value = boolean
|
||||
|
||||
if boolean
|
||||
@image = @_image if @_image
|
||||
@raw_text = @options[:checkmark]
|
||||
else
|
||||
@image = nil
|
||||
@raw_text = ""
|
||||
end
|
||||
|
||||
|
||||
@@ -38,6 +38,10 @@ module CyberarmEngine
|
||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
||||
end
|
||||
|
||||
def menu
|
||||
@menu
|
||||
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)
|
||||
@@ -50,38 +54,11 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def draw
|
||||
super
|
||||
|
||||
if @menu
|
||||
Gosu.flush
|
||||
@menu.draw
|
||||
end
|
||||
|
||||
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
|
||||
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_recalculate_request = true 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)
|
||||
@@ -96,6 +73,33 @@ module CyberarmEngine
|
||||
Stats.frame.end_timing(:gui_recalculate)
|
||||
end
|
||||
|
||||
super
|
||||
|
||||
if @menu
|
||||
Gosu.flush
|
||||
@menu.draw
|
||||
end
|
||||
|
||||
if @tip && @tip.value.length.positive?
|
||||
Gosu.flush
|
||||
|
||||
@tip.draw
|
||||
end
|
||||
|
||||
if CyberarmEngine.const_defined?("GUI_DEBUG")
|
||||
Gosu.flush
|
||||
|
||||
@root_container.debug_draw
|
||||
end
|
||||
|
||||
@needs_repaint = false
|
||||
end
|
||||
|
||||
def needs_repaint?
|
||||
@needs_repaint
|
||||
end
|
||||
|
||||
def update
|
||||
if @pending_focus_request
|
||||
@pending_focus_request = false
|
||||
|
||||
@@ -186,6 +190,14 @@ module CyberarmEngine
|
||||
redirect_mouse_wheel(:up)
|
||||
when Gosu::MS_WHEEL_DOWN
|
||||
redirect_mouse_wheel(:down)
|
||||
when Gosu::KB_HOME
|
||||
redirect_scroll_jump_to(:top)
|
||||
when Gosu::KB_END
|
||||
redirect_scroll_jump_to(:end)
|
||||
when Gosu::KB_PAGE_UP
|
||||
redirect_scroll_page(:up)
|
||||
when Gosu::KB_PAGE_DOWN
|
||||
redirect_scroll_page(:down)
|
||||
end
|
||||
|
||||
@focus.button_up(id) if @focus.respond_to?(:button_up)
|
||||
@@ -251,7 +263,15 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def redirect_mouse_wheel(button)
|
||||
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
|
||||
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||
end
|
||||
|
||||
def redirect_scroll_jump_to(edge)
|
||||
@mouse_over.publish(:"scroll_jump_to_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||
end
|
||||
|
||||
def redirect_scroll_page(edge)
|
||||
@mouse_over.publish(:"scroll_page_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
|
||||
end
|
||||
|
||||
# Schedule a full GUI recalculation on next update
|
||||
|
||||
@@ -208,6 +208,17 @@ module CyberarmEngine
|
||||
fraction_background: [0xffc75e61, 0xffe26623],
|
||||
border_thickness: 1,
|
||||
border_color: [0xffd59674, 0xffff8746]
|
||||
},
|
||||
Menu: { # < Stack
|
||||
width: 200,
|
||||
border_color: 0xaa_efefef,
|
||||
border_thickness: 1
|
||||
},
|
||||
|
||||
MenuItem: { # < Button
|
||||
width: 1.0,
|
||||
text_left: :left,
|
||||
margin: 0
|
||||
}
|
||||
}.freeze
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev".freeze
|
||||
VERSION = "0.24.1".freeze
|
||||
VERSION = "0.24.5".freeze
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user