Compare commits

..

32 Commits

Author SHA1 Message Date
d735edaec0 Make changing an element's styles trigger element to re-stylize 2026-03-21 22:42:57 -05:00
d2440b50a1 Added every and after timers to DSL 2026-03-21 21:11:51 -05:00
362fafa6fd Fixed HOME/END PAGE UP/DOWN keys incorrectly scrolling container when an EditLine or EditBox has focus 2026-03-21 21:11:51 -05:00
907257879c Fixed EditBox causing crash if parent container is scrolled and EditBox is clicked 2026-03-21 21:11:50 -05:00
e031039a77 Refactored GuiState Element style handling to simplify mutating them 2026-03-21 21:03:58 -05:00
16bd9168e4 Bump version 2026-01-28 18:02:18 -06:00
b946d5efa6 Added Result class to make writing failure resistant code easier 2026-01-28 18:02:18 -06:00
4ce6c1f499 Update README 2026-01-09 09:39:40 -06:00
958d4e65f9 Bump version 2026-01-09 09:36:08 -06:00
0519253e03 Improved GuiState to fully recalculate before returning from draw 2026-01-09 09:29:41 -06:00
97055885a6 Cache TextBlock text width and height 2026-01-09 08:59:06 -06:00
b5912de980 Remove unused code from Background#update, improves performance notably. 2026-01-09 08:34:59 -06:00
b0376d85d9 Reduce allocations in Background 2026-01-09 08:26:55 -06:00
a30d66fafb Refactored Style to remove usage of method_missing 2026-01-09 08:20:27 -06:00
0fac4a0397 Add StackProf hooks for profile recalculate 2026-01-08 22:16:54 -06:00
498cf06916 Style fixes, wip layout speed up work 2025-12-01 10:03:45 -06:00
1f57dfd38c Update to support Gosu 2.0.0 (Gosu::Image taking in a Gosu::Image fails) 2025-11-29 11:27:27 -06:00
76a8bf95c7 Update Gosu.draw_arc to support partial segments 2025-11-29 11:11:53 -06:00
1c25eeb32b Bump version 2025-06-24 13:55:40 -05:00
eabad4abd4 Refactored mesh handling, imported AABB tree implementation from I-MIC FPS 2025-05-03 18:21:11 -05:00
b3561f02c1 Fixed edit_line's with prefilled values having an offset_x that hides the text unless caret is manually moved left 2024-04-09 09:27:12 -05:00
9694cc2270 Fix compatibility issue with gosu 2.0 pre-release 2024-03-12 19:20:25 -05:00
a7df9a660d Bump version 2024-03-05 14:16:25 -06:00
d2f757eb23 Fixed hidden Container elements not recalculating when becoming visible 2024-03-05 14:16:07 -06:00
0d70177de7 Bump version 2024-03-05 13:31:17 -06:00
dcefaeae4d Fixed debug_draw for elements, fixed Slider Handle not rendering/positioned properly 2024-03-05 13:17:12 -06:00
c2330ed9a9 Removed excon as explicit dependency, implemented menu's as (mostly) first class citizens, refactoring of gui layout, FIXME: broke Slider Handle..., probably some other stuff. 2024-03-04 20:54:36 -06:00
615d7ec3e4 Added support HOME, END, and PAGE UP/DOWN for scrollable containers, misc. tweaks and bug fixes. 2024-03-03 19:23:00 -06:00
1644ff8a27 Bump version 2024-02-28 20:43:28 -06:00
d6a99b935a Fixed styles not applying correctly for non-default styles 2024-02-28 10:53:49 -06:00
c4e47b8e38 Bump version 2024-01-18 14:10:28 -06:00
d0e1772f33 Fixed container that is scrolled down getting stuck being 'overscrolled' when resized 2024-01-18 14:08:33 -06:00
45 changed files with 987 additions and 516 deletions

25
Gemfile.lock Normal file
View 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

View File

@@ -35,7 +35,7 @@ class Hello < CyberarmEngine::GuiState
background Gosu::Color::GRAY background Gosu::Color::GRAY
stack do stack do
label "Hello World!" banner "Hello World!"
button "close" do button "close" do
window.close window.close

View File

@@ -1,14 +1,15 @@
#version 330 core #version 330 core
@include "light_struct"
out vec4 frag_color; out vec4 frag_color;
@include "light_struct"
const int DIRECTIONAL = 0; const int DIRECTIONAL = 0;
const int POINT = 1; const int POINT = 1;
const int SPOT = 2; const int SPOT = 2;
flat in Light out_lights[7];
in vec2 out_tex_coords; in vec2 out_tex_coords;
flat in int out_light_count; flat in int out_light_count;
flat in Light out_lights[7];
uniform sampler2D diffuse, position, texcoord, normal, depth; uniform sampler2D diffuse, position, texcoord, normal, depth;
@@ -27,43 +28,88 @@ vec4 directionalLight(Light light) {
return vec4(_diffuse + _ambient + _specular, 1.0); return vec4(_diffuse + _ambient + _specular, 1.0);
} }
vec4 pointLight(Light light) { void main() {
return vec4(0.25, 0.25, 0.25, 1); Light light;
light.type = DIRECTIONAL;
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);
} }
vec4 spotLight(Light light) { // #version 330 core
return vec4(0.5, 0.5, 0.5, 1); // @include "light_struct"
}
vec4 calculateLighting(Light light) { // out vec4 frag_color;
vec4 result;
// switch(light.type) { // const int DIRECTIONAL = 0;
// case DIRECTIONAL: { // 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); // result = directionalLight(light);
// } // } else {
// case SPOT: {
// result = spotLight(light);
// }
// default: {
// result = pointLight(light); // result = pointLight(light);
// } // }
// return result;
// } // }
if (light.type == DIRECTIONAL) { // void main() {
result = directionalLight(light); // frag_color = vec4(0.0);
} else {
result = pointLight(light);
}
return result; // for(int i = 0; i < out_light_count; i++)
} // {
// frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
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]);
}
}

View File

@@ -27,9 +27,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = %w[lib assets] spec.require_paths = %w[lib assets]
spec.add_dependency "excon", "~> 0.88"
spec.add_dependency "gosu", "~> 1.1" spec.add_dependency "gosu", "~> 1.1"
spec.add_dependency "gosu_more_drawables", "~> 0.3"
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows # spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
spec.add_development_dependency "bundler", "~> 2.2" spec.add_development_dependency "bundler", "~> 2.2"

View File

@@ -6,10 +6,10 @@ else
require "gosu" require "gosu"
end end
require "json" require "json"
require "excon"
require_relative "cyberarm_engine/version" require_relative "cyberarm_engine/version"
require_relative "cyberarm_engine/stats" require_relative "cyberarm_engine/stats"
require_relative "cyberarm_engine/result"
require_relative "cyberarm_engine/common" require_relative "cyberarm_engine/common"
@@ -62,16 +62,16 @@ require_relative "cyberarm_engine/ui/elements/check_box"
require_relative "cyberarm_engine/ui/elements/radio" require_relative "cyberarm_engine/ui/elements/radio"
require_relative "cyberarm_engine/ui/elements/progress" require_relative "cyberarm_engine/ui/elements/progress"
require_relative "cyberarm_engine/ui/elements/slider" require_relative "cyberarm_engine/ui/elements/slider"
require_relative "cyberarm_engine/ui/elements/menu"
require_relative "cyberarm_engine/ui/elements/menu_item"
require_relative "cyberarm_engine/game_state" require_relative "cyberarm_engine/game_state"
require_relative "cyberarm_engine/ui/gui_state" require_relative "cyberarm_engine/ui/gui_state"
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" require_relative "cyberarm_engine/builtin/intro_state"
if RUBY_ENGINE != "mruby" && defined?(StackProf)
at_exit do
StackProf.results("./_stackprof.dmp")
end
end

View File

@@ -35,36 +35,14 @@ module CyberarmEngine
end end
def update def update
origin_x = (@x + (@width / 2)) @top_left.x = @x
origin_y = (@y + (@height / 2)) @top_left.y = @y
@top_right.x = @x + @width
points = [ @top_right.y = @y
@top_left = Vector.new(@x, @y), @bottom_left.x = @x
@top_right = Vector.new(@x + @width, @y), @bottom_left.y = @y + @height
@bottom_left = Vector.new(@x, @y + @height), @bottom_right.x = @x + @width
@bottom_right = Vector.new(@x + @width, @y + @height) @bottom_right.y = @y + @height
]
[@top_left, @top_right, @bottom_left, @bottom_right].each do |vector|
temp_x = vector.x - origin_x
temp_y = vector.y - origin_y
# 90 is up here, while gosu uses 0 for up.
radians = (@angle + 90).gosu_to_radians
vector.x = (@x + (@width / 2)) + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
vector.y = (@y + (@height / 2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
end
# [
# [:top, @top_left, @top_right],
# [:right, @top_right, @bottom_right],
# [:bottom, @bottom_right, @bottom_left],
# [:left, @bottom_left, @top_left]
# ].each do |edge|
# points.each do |point|
# puts "#{edge.first} -> #{shortest_distance(point, edge[1], edge[2])}"
# end
# end
end end
def shortest_distance(point, la, lb) def shortest_distance(point, la, lb)
@@ -163,17 +141,18 @@ module CyberarmEngine
elsif background.is_a?(Range) elsif background.is_a?(Range)
set([background.begin, background.begin, background.end, background.end]) set([background.begin, background.begin, background.end, background.end])
else else
raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed" raise ArgumentError, "background '#{background.inspect}' of type '#{background.class}' was not able to be processed"
end end
end end
end end
end end
# Add <=> method to support Range based gradients # Add <=> method to support Range based gradients
module Gosu # NOTE: Disabled, causes stack overflow 🙃
class Color # module Gosu
def <=>(_other) # class Color
self # def <=>(_other)
end # self
end # end
end # end
# end

View File

@@ -1,3 +1,5 @@
require "excon"
module CyberarmEngine module CyberarmEngine
module Cache module Cache
class DownloadManager class DownloadManager

View File

@@ -1,5 +1,7 @@
module CyberarmEngine module CyberarmEngine
module Common module Common
ImageBlob = Data.define(:to_blob, :columns, :rows)
def push_state(klass, options = {}) def push_state(klass, options = {})
window.push_state(klass, options) window.push_state(klass, options)
end end
@@ -85,7 +87,8 @@ module CyberarmEngine
unless asset unless asset
instance = nil instance = nil
instance = if klass == Gosu::Image 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 else
klass.new(path) klass.new(path)
end end

View File

@@ -3,11 +3,12 @@ module CyberarmEngine
include Common include Common
attr_accessor :options, :global_pause attr_accessor :options, :global_pause
attr_reader :game_objects attr_reader :game_objects, :timers
def initialize(options = {}) def initialize(options = {})
@options = options @options = options
@game_objects = [] @game_objects = []
@timers = []
@global_pause = false @global_pause = false
window.text_input = nil unless options[:preserve_text_input] window.text_input = nil unless options[:preserve_text_input]
@@ -28,6 +29,8 @@ module CyberarmEngine
def update def update
@game_objects.each(&:update) @game_objects.each(&:update)
@timers.each(&:update)
@timers.delete_if(&:dead?)
end end
def needs_redraw? def needs_redraw?
@@ -120,5 +123,9 @@ module CyberarmEngine
def add_game_object(object) def add_game_object(object)
@game_objects << object @game_objects << object
end end
def add_timer(timer)
@timers << timer
end
end end
end end

View File

@@ -33,12 +33,23 @@ module Gosu
# #
# @return [void] # @return [void]
def self.draw_arc(x, y, radius, percentage = 1.0, segments = 128, thickness = 4, color = Gosu::Color::WHITE, z = 0, mode = :default) 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 return if percentage == 0.0
0.step((359 * percentage), percentage > 0 ? segments : -segments) do |angle| angle_per_segment = 360.0 / segments
angle2 = angle + 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_x = x + Gosu.offset_x(angle, radius - thickness)
point_a_left_y = y + Gosu.offset_y(angle, radius - thickness) point_a_left_y = y + Gosu.offset_y(angle, radius - thickness)
@@ -93,6 +104,8 @@ module Gosu
z, mode z, mode
) )
end end
angle += next_segment_angle
end end
end end
end end

View File

@@ -1,5 +1,8 @@
module CyberarmEngine module CyberarmEngine
class Model class Model
include OpenGL
include CyberarmEngine
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file, attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
:current_material, :current_object, :vertex_count, :smoothing :current_material, :current_object, :vertex_count, :smoothing
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id, 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 } @objects.each { |o| @vertex_count += o.vertices.size }
# start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
# build_collision_tree build_collision_tree
# puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds" puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
end end
def parse(parser) def parse(parser)
@@ -178,7 +181,7 @@ module CyberarmEngine
end end
def build_collision_tree def build_collision_tree
@aabb_tree = IMICFPS::AABBTree.new @aabb_tree = AABBTree.new
@faces.each do |face| @faces.each do |face|
box = BoundingBox.new box = BoundingBox.new

View File

@@ -1,6 +1,6 @@
module CyberarmEngine module CyberarmEngine
class Model class Model
class ModelObject class Mesh
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
attr_accessor :faces, :scale attr_accessor :faces, :scale

View File

@@ -48,12 +48,12 @@ module CyberarmEngine
if _model if _model
@model.current_object = _model @model.current_object = _model
else else
raise "Couldn't find ModelObject!" raise "Couldn't find Mesh!"
end end
end end
def change_object(id, name) 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 @model.current_object = @model.objects.last
end end

View File

@@ -1,5 +1,6 @@
begin begin
require "opengl" require "opengl"
require "glu"
rescue LoadError rescue LoadError
puts "Required gem is not installed, please install 'opengl-bindings' and try again." puts "Required gem is not installed, please install 'opengl-bindings' and try again."
exit(1) exit(1)
@@ -8,7 +9,7 @@ end
module CyberarmEngine module CyberarmEngine
def gl_error? def gl_error?
e = glGetError e = glGetError
if e != GL_NO_ERROR if e != OpenGL::GL_NO_ERROR
warn "OpenGL error detected by handler at: #{caller[0]}" warn "OpenGL error detected by handler at: #{caller[0]}"
warn " #{gluErrorString(e)} (#{e})\n" warn " #{gluErrorString(e)} (#{e})\n"
exit if Window.instance&.exit_on_opengl_error? 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/bounding_box_renderer"
require_relative "opengl/renderer/opengl_renderer" require_relative "opengl/renderer/opengl_renderer"
require_relative "opengl/renderer/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)

View File

@@ -1,5 +1,7 @@
module CyberarmEngine module CyberarmEngine
class Light class Light
include OpenGL
DIRECTIONAL = 0 DIRECTIONAL = 0
POINT = 1 POINT = 1
SPOT = 2 SPOT = 2

View File

@@ -1,5 +1,8 @@
module CyberarmEngine module CyberarmEngine
class PerspectiveCamera class PerspectiveCamera
include OpenGL
include GLU
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view, attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
:min_view_distance, :max_view_distance :min_view_distance, :max_view_distance

View File

@@ -1,5 +1,7 @@
module CyberarmEngine module CyberarmEngine
class GBuffer class GBuffer
include OpenGL
attr_reader :screen_vbo, :vertices, :uvs attr_reader :screen_vbo, :vertices, :uvs
attr_reader :width, :height attr_reader :width, :height

View File

@@ -1,5 +1,8 @@
module CyberarmEngine module CyberarmEngine
class OpenGLRenderer class OpenGLRenderer
include OpenGL
include CyberarmEngine
@@immediate_mode_warning = false @@immediate_mode_warning = false
attr_accessor :show_wireframe attr_accessor :show_wireframe

View File

@@ -2,6 +2,7 @@ module CyberarmEngine
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb # Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
class Shader class Shader
include OpenGL include OpenGL
@@shaders = {} # Cache for {Shader} instances @@shaders = {} # Cache for {Shader} instances
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
@@ -298,6 +299,7 @@ module CyberarmEngine
# @see Shader.use Shader.use # @see Shader.use Shader.use
def use(&block) def use(&block)
return unless compiled? return unless compiled?
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
Shader.active_shader = self Shader.active_shader = self

View File

@@ -0,0 +1,20 @@
module CyberarmEngine
# result pattern
class Result
attr_accessor :error, :data
def initialize(data: nil, error: nil)
@data = data
@error = error
end
def okay?
!@error
end
def error?
@error || @data.nil?
end
end
end

View File

@@ -115,11 +115,11 @@ module CyberarmEngine
@padding = 2 @padding = 2
@text_size = 16 @text_size = 16
@max_timing_label = CyberarmEngine::Text.new("", x: x + @padding + 1, y: y + @padding, 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) @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) @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 = [] @frame_stats = []
@graphs = { @graphs = {
@@ -146,10 +146,15 @@ module CyberarmEngine
slice += 1 slice += 1
end 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| nodes.each_with_index do |cluster, i|
break if cluster.empty? 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
end end
@@ -159,9 +164,9 @@ module CyberarmEngine
calculate_graphs calculate_graphs
@max_timing_label.text = "Max: #{@frame_stats.map { |f| f.frame_timing.duration }.max.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 = "Avg: #{(@frame_stats.map { |f| f.frame_timing.duration }.sum / @frame_stats.size).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 = "Min: #{@frame_stats.map { |f| f.frame_timing.duration }.min.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, @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) 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 @min_timing_label.draw
# TODO: Make this optional # TODO: Make this optional
draw_timings draw_timings_and_counters
end end
def draw_graphs def draw_graphs
Gosu.draw_path(@graphs[:frame_timings], Gosu::Color::WHITE, Float::INFINITY) Gosu.draw_path(@graphs[:frame_timings], Gosu::Color::WHITE, Float::INFINITY)
end end
def draw_timings def draw_timings_and_counters
frame = @frame_stats.last 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")}" @data_label.text = "<c=f8f>COUNTERS:</c>\n#{frame.counters.map { |t, v| "#{t}: #{v}" }.join("\n")}\n\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) "<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")}"
@timings_label.draw Gosu.draw_rect(@data_label.x - @padding, @data_label.y - @padding, @data_label.width + @padding * 2, @data_label.height + @padding * 2, 0xdd_222222, @position.z)
@data_label.draw
# puts "Recalcs this frame: #{frame.counters[:gui_recalculations]} [dt: #{(CyberarmEngine::Window.dt * 1000).round} ms]" if frame.counters[:gui_recalculations] && frame.counters[:gui_recalculations].positive?
end end
end end
end end

View File

@@ -19,5 +19,9 @@ module CyberarmEngine
@block.call if @block @block.call if @block
end end
end end
def dead?
@triggered && !@looping
end
end end
end end

View 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

View 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

View 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

View File

@@ -62,36 +62,36 @@ module CyberarmEngine
def update def update
# TOP # TOP
@top.x = @element.x + @element.style.border_thickness_left @top.x = @element.x + @element.styled(:border_thickness_left)
@top.y = @element.y @top.y = @element.y
@top.z = @element.z @top.z = @element.z
@top.width = @element.width - @element.style.border_thickness_left @top.width = @element.width - @element.styled(:border_thickness_left)
@top.height = @element.style.border_thickness_top @top.height = @element.styled(:border_thickness_top)
# RIGHT # RIGHT
@right.x = @element.x + @element.width @right.x = @element.x + @element.width
@right.y = @element.y + @element.style.border_thickness_top @right.y = @element.y + @element.styled(:border_thickness_top)
@right.z = @element.z @right.z = @element.z
@right.width = -@element.style.border_thickness_right @right.width = -@element.styled(:border_thickness_right)
@right.height = @element.height - @element.style.border_thickness_top @right.height = @element.height - @element.styled(:border_thickness_top)
# BOTTOM # BOTTOM
@bottom.x = @element.x @bottom.x = @element.x
@bottom.y = @element.y + @element.height @bottom.y = @element.y + @element.height
@bottom.z = @element.z @bottom.z = @element.z
@bottom.width = @element.width - @element.style.border_thickness_right @bottom.width = @element.width - @element.styled(:border_thickness_right)
@bottom.height = -@element.style.border_thickness_bottom @bottom.height = -@element.styled(:border_thickness_bottom)
# LEFT # LEFT
@left.x = @element.x @left.x = @element.x
@left.y = @element.y @left.y = @element.y
@left.z = @element.z @left.z = @element.z
@left.width = @element.style.border_thickness_left @left.width = @element.styled(:border_thickness_left)
@left.height = @element.height - @element.style.border_thickness_bottom @left.height = @element.height - @element.styled(:border_thickness_bottom)
@top.update @top.update
@right.update @right.update

View File

@@ -1,5 +1,17 @@
module CyberarmEngine module CyberarmEngine
module DSL module DSL
def every(milliseconds, &block)
element_parent.gui_state.add_timer(
CyberarmEngine::Timer.new(milliseconds, true, &block)
)
end
def after(milliseconds, &block)
element_parent.gui_state.add_timer(
CyberarmEngine::Timer.new(milliseconds, false, &block)
)
end
def flow(options = {}, &block) def flow(options = {}, &block)
container(CyberarmEngine::Element::Flow, options, &block) container(CyberarmEngine::Element::Flow, options, &block)
end end
@@ -8,12 +20,8 @@ module CyberarmEngine
container(CyberarmEngine::Element::Stack, options, &block) container(CyberarmEngine::Element::Stack, options, &block)
end end
# TODO: Remove in version 0.16.0+ def menu(options = {}, &block)
def label(text, options = {}, &block) container(CyberarmEngine::Element::Menu, options, &block)
options[:parent] = element_parent
options[:theme] = current_theme
add_element(Element::TextBlock.new(text, options, block))
end end
[ [
@@ -27,82 +35,89 @@ module CyberarmEngine
"Link" "Link"
].each do |const| ].each do |const|
define_method(:"#{const.downcase}") do |text, options = {}, &block| define_method(:"#{const.downcase}") do |text, options = {}, &block|
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element.const_get(const).new(text, options, block)) add_element(Element.const_get(const).new(text, options, block))
end end
end end
def button(text, options = {}, &block) def button(text, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) }) add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) })
end end
def list_box(options = {}, &block) def list_box(options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) }) add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) })
end end
def menu_item(text, options = {}, &block)
options[:parent] ||= element_parent
options[:theme] ||= current_theme
add_element(Element::MenuItem.new(text, options, block) { block.call if block.is_a?(Proc) })
end
def edit_line(text, options = {}, &block) def edit_line(text, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::EditLine.new(text, options, block)) add_element(Element::EditLine.new(text, options, block))
end end
def edit_box(text, options = {}, &block) def edit_box(text, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::EditBox.new(text, options, block)) add_element(Element::EditBox.new(text, options, block))
end end
def toggle_button(options = {}, &block) def toggle_button(options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::ToggleButton.new(options, block)) add_element(Element::ToggleButton.new(options, block))
end end
def check_box(text, options = {}, &block) def check_box(text, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::CheckBox.new(text, options, block)) add_element(Element::CheckBox.new(text, options, block))
end end
def image(path, options = {}, &block) def image(path, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::Image.new(path, options, block)) add_element(Element::Image.new(path, options, block))
end end
def progress(options = {}, &block) def progress(options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::Progress.new(options, block)) add_element(Element::Progress.new(options, block))
end end
def slider(options = {}, &block) def slider(options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
add_element(Element::Slider.new(options, block)) add_element(Element::Slider.new(options, block))
end end
def background(color = Gosu::Color::NONE) def background(color = Gosu::Color::NONE)
element_parent.style.default[:background] = color element_parent.style.background = color
end end
def theme(theme) def theme(theme)
element_parent.options[:theme] = theme element_parent.options[:theme] ||= theme
end end
def current_theme def current_theme
@@ -120,8 +135,8 @@ module CyberarmEngine
end end
private def container(klass, options = {}, &block) private def container(klass, options = {}, &block)
options[:parent] = element_parent options[:parent] ||= element_parent
options[:theme] = current_theme options[:theme] ||= current_theme
_container = klass.new(options, block) _container = klass.new(options, block)
@@ -129,7 +144,7 @@ module CyberarmEngine
CyberarmEngine::Element::Container.current_container = _container CyberarmEngine::Element::Container.current_container = _container
_container.build _container.build
_container.parent.add(_container) _container.parent.add(_container) unless _container.is_a?(CyberarmEngine::Element::Menu)
CyberarmEngine::Element::Container.current_container = old_parent CyberarmEngine::Element::Container.current_container = old_parent

View File

@@ -38,10 +38,10 @@ module CyberarmEngine
@style.width = default(:width) || nil @style.width = default(:width) || nil
@style.height = default(:height) || nil @style.height = default(:height) || nil
@style.background_canvas = Background.new @background_canvas = Background.new
@style.background_nine_slice_canvas = BackgroundNineSlice.new @background_nine_slice_canvas = BackgroundNineSlice.new
@style.background_image_canvas = BackgroundImage.new @background_image_canvas = BackgroundImage.new
@style.border_canvas = BorderCanvas.new(element: self) @border_canvas = BorderCanvas.new(element: self)
@style_event = :default @style_event = :default
@@ -58,21 +58,43 @@ module CyberarmEngine
set_color set_color
set_font set_font
set_padding
set_margin
set_background set_background
set_background_nine_slice set_background_nine_slice
set_background_image set_background_image
set_border_thickness set_border
set_border_color
root.gui_state.request_repaint root.gui_state.request_repaint
end end
def safe_style_fetch(*args) def styled(key)
@style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args) case key
when :border_color_bottom, :border_color_left, :border_color_right, :border_color_top
safe_style_fetch(key, :border_color)
when :border_thickness_bottom, :border_thickness_left, :border_thickness_right, :border_thickness_top
safe_style_fetch(key, :border_thickness)
when :margin_bottom, :margin_left, :margin_right, :margin_top
safe_style_fetch(key, :margin)
when :padding_bottom, :padding_left, :padding_right, :padding_top
safe_style_fetch(key, :padding)
else
safe_style_fetch(key)
end
end
def safe_style_fetch(key, fallback_key = nil)
# Attempt to return value for requested key
v = @style.send(@style_event)&.send(key) || @style.send(key)
return v if v
# Attempt to return overriding value
if fallback_key
v = @style.send(@style_event)&.send(fallback_key) || @style.send(fallback_key)
return v if v
end
# Fallback to default style
@style.hash.dig(:default, key) || default(key)
end end
def set_static_position def set_static_position
@@ -81,8 +103,7 @@ module CyberarmEngine
end end
def set_color def set_color
@style.color = safe_style_fetch(:color) @text&.color = safe_style_fetch(:color)
@text&.color = @style.color
end end
def set_font def set_font
@@ -90,77 +111,43 @@ module CyberarmEngine
end end
def set_background def set_background
@style.background = safe_style_fetch(:background) @background_canvas.background = safe_style_fetch(:background)
@style.background_canvas.background = @style.background
end end
def set_background_nine_slice def set_background_nine_slice
@style.background_nine_slice = safe_style_fetch(:background_nine_slice) @background_nine_slice_canvas.x = @x
@background_nine_slice_canvas.y = @y
@background_nine_slice_canvas.z = @z
@background_nine_slice_canvas.width = width
@background_nine_slice_canvas.height = height
@style.background_nine_slice_mode = safe_style_fetch(:background_nine_slice_mode) || :stretch @background_nine_slice_canvas.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) @background_nine_slice_canvas.color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
@style.background_nine_slice_left = safe_style_fetch(:background_nine_slice_left) || @style.background_nine_slice_from_edge @background_nine_slice_canvas.left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge)
@style.background_nine_slice_top = safe_style_fetch(:background_nine_slice_top) || @style.background_nine_slice_from_edge @background_nine_slice_canvas.top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge)
@style.background_nine_slice_right = safe_style_fetch(:background_nine_slice_right) || @style.background_nine_slice_from_edge @background_nine_slice_canvas.right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge)
@style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge @background_nine_slice_canvas.bottom = safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge)
@background_nine_slice_canvas.image = safe_style_fetch(:background_nine_slice)
end end
def set_background_image def set_background_image
@style.background_image = safe_style_fetch(:background_image) @background_image_canvas.image = safe_style_fetch(:background_image)
@style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch @background_image_canvas.mode = safe_style_fetch(:background_image_mode) || :stretch
@style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE @background_image_canvas.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 end
def set_border_thickness def set_border
@style.border_thickness = safe_style_fetch(:border_thickness) @border_canvas.color = [
styled(:border_color_top),
@style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness styled(:border_color_right),
@style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness styled(:border_color_bottom),
@style.border_thickness_top = safe_style_fetch(:border_thickness_top) || @style.border_thickness styled(:border_color_left)
@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 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) def update_styles(event = :default)
old_width = width old_width = width
old_height = height old_height = height
@@ -169,9 +156,7 @@ module CyberarmEngine
return if self.is_a?(ToolTip) return if self.is_a?(ToolTip)
if old_width != width || old_height != height root.gui_state.request_recalculate if old_width != width || old_height != height
root.gui_state.request_recalculate
end
stylize stylize
end end
@@ -186,6 +171,10 @@ module CyberarmEngine
event(:mouse_wheel_up) event(:mouse_wheel_up)
event(:mouse_wheel_down) event(:mouse_wheel_down)
event(:scroll_jump_to_top)
event(:scroll_jump_to_end)
event(:scroll_page_up)
event(:scroll_page_down)
event(:enter) event(:enter)
event(:hover) event(:hover)
@@ -309,17 +298,16 @@ module CyberarmEngine
return unless visible? return unless visible?
return unless element_visible? return unless element_visible?
@style.background_canvas.draw @background_canvas.draw
@style.background_nine_slice_canvas.draw @background_nine_slice_canvas.draw
@style.background_image_canvas.draw @background_image_canvas.draw
@style.border_canvas.draw @border_canvas.draw
render render
end end
def debug_draw def debug_draw
# FIXME return if CyberarmEngine.const_defined?("GUI_DEBUG_ONLY_ELEMENT") && self.class == GUI_DEBUG_ONLY_ELEMENT
return# if const_defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
Gosu.draw_line( Gosu.draw_line(
x, y, @debug_color, x, y, @debug_color,
@@ -337,7 +325,7 @@ module CyberarmEngine
Float::INFINITY Float::INFINITY
) )
Gosu.draw_line( Gosu.draw_line(
x, outer_height, @debug_color, x, y + outer_height, @debug_color,
x, y, @debug_color, x, y, @debug_color,
Float::INFINITY Float::INFINITY
) )
@@ -345,6 +333,11 @@ module CyberarmEngine
def update def update
recalculate_if_size_changed recalculate_if_size_changed
if @style.dirty?
@style.mark_clean!
stylize
end
end end
def button_down(id) def button_down(id)
@@ -382,11 +375,11 @@ module CyberarmEngine
end end
def outer_width def outer_width
@style.margin_left + width + @style.margin_right styled(:margin_left) + width + styled(:margin_right)
end end
def inner_width def inner_width
(@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right) (styled(:border_thickness_left) + styled(:padding_left)) + (styled(:padding_right) + styled(:border_thickness_right))
end end
def height def height
@@ -406,11 +399,11 @@ module CyberarmEngine
end end
def outer_height def outer_height
@style.margin_top + height + @style.margin_bottom styled(:margin_top) + height + styled(:margin_bottom)
end end
def inner_height def inner_height
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom) (styled(:border_thickness_top) + styled(:padding_top)) + (styled(:padding_bottom) + styled(:border_thickness_bottom))
end end
def scroll_width def scroll_width
@@ -442,9 +435,9 @@ module CyberarmEngine
pairs_ << a_ unless pairs_.last == a_ 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 @cached_scroll_height = pairs_.sum { |pair| + styled(:padding_top) + styled(:border_thickness_top) + pair.map(&:outer_height).max } + styled(:padding_bottom) + styled(:border_thickness_bottom)
else else
@cached_scroll_height = @style.padding_top + @style.border_thickness_top + @children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom @cached_scroll_height = styled(:padding_top) + styled(:border_thickness_top) + @children.sum(&:outer_height) + styled(:padding_bottom) + styled(:border_thickness_bottom)
end end
end end
@@ -466,79 +459,60 @@ module CyberarmEngine
end end
# Handle fill behavior # Handle fill behavior
if @parent && @style.fill && if @parent && styled(:fill) &&
(dimension == :width && @parent.is_a?(Flow) || (dimension == :width && @parent.is_a?(Flow) ||
dimension == :height && @parent.is_a?(Stack)) dimension == :height && @parent.is_a?(Stack))
new_size = space_available_width - noncontent_width if dimension == :width && @parent.is_a?(Flow) 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) new_size = space_available_height - noncontent_height if dimension == :height && @parent.is_a?(Stack)
end end
return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size.to_f < @style.send(:"min_#{dimension}") return styled(:"min_#{dimension}") if styled(:"min_#{dimension}") && new_size.to_f < styled(:"min_#{dimension}")
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size.to_f > @style.send(:"max_#{dimension}") return styled(:"max_#{dimension}") if styled(:"max_#{dimension}") && new_size.to_f > styled(:"max_#{dimension}")
new_size new_size
end end
def space_available_width def space_available_width
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow # 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 fill_siblings = @parent.children.select { |c| c.styled(: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 = ((@parent.content_width - (@parent.children.reject { |c| c.styled(: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. (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end end
def space_available_height def space_available_height
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow # 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 fill_siblings = @parent.children.select { |c| c.styled(: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 = ((@parent.content_height - (@parent.children.reject { |c| c.styled(: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. (available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
end end
def background=(_background) def background=(_background)
root.gui_state.request_repaint root.gui_state.request_repaint
@style.background_canvas.background = _background @background_canvas.background = _background
update_background update_background
end end
def update_background def update_background
@style.background_canvas.x = @x @background_canvas.x = @x
@style.background_canvas.y = @y @background_canvas.y = @y
@style.background_canvas.z = @z @background_canvas.z = @z
@style.background_canvas.width = width @background_canvas.width = width
@style.background_canvas.height = height @background_canvas.height = height
@style.background_canvas.update @background_canvas.update
update_background_nine_slice set_background_nine_slice
update_background_image update_background_image
@style.border_canvas.update @border_canvas.update
end end
def background_nine_slice=(_image_path) def background_nine_slice=(_image_path)
root.gui_state.request_repaint root.gui_state.request_repaint
@style.background_nine_slice_canvas.image = _image_path @background_nine_slice_canvas.image = _image_path
update_background_nine_slice set_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 end
def background_image=(image_path) def background_image=(image_path)
@@ -549,20 +523,20 @@ module CyberarmEngine
end end
def update_background_image def update_background_image
@style.background_image_canvas.x = @x @background_image_canvas.x = @x
@style.background_image_canvas.y = @y @background_image_canvas.y = @y
@style.background_image_canvas.z = @z @background_image_canvas.z = @z
@style.background_image_canvas.width = width @background_image_canvas.width = width
@style.background_image_canvas.height = height @background_image_canvas.height = height
@style.background_image_canvas.mode = @style.background_image_mode @background_image_canvas.mode = safe_style_fetch(:background_image_mode) || :stretch
@style.background_image_canvas.color = @style.background_image_color @background_image_canvas.color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE
@style.background_image_canvas.image = @style.background_image @background_image_canvas.image = safe_style_fetch(:background_image)
end end
def recalculate_if_size_changed 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 root.gui_state.request_recalculate
@old_width = width @old_width = width
@@ -613,7 +587,19 @@ module CyberarmEngine
end end
def recalculate def recalculate
raise "#{self.class}#recalculate was not overridden!" old_width = width
old_height = height
stylize
layout
root.gui_state.request_recalculate if @parent && !is_a?(ToolTip) && (width != old_width || height != old_height)
root.gui_state.request_repaint if width != old_width || height != old_height
root.gui_state.menu.recalculate if root.gui_state.menu && root.gui_state.menu.parent == self
end
def layout
end end
def reposition def reposition

View File

@@ -10,7 +10,7 @@ module CyberarmEngine
super(text_or_image, options, block) super(text_or_image, options, block)
@style.background_canvas.background = @style.background @background_canvas.background = styled(:background)
end end
def render def render
@@ -23,8 +23,8 @@ module CyberarmEngine
def draw_image def draw_image
@image.draw( @image.draw(
@style.border_thickness_left + @style.padding_left + @x, styled(:border_thickness_left) + styled(:padding_left) + @x,
@style.border_thickness_top + @style.padding_top + @y, styled(:border_thickness_top) + styled(:padding_top) + @y,
@z + 2, @z + 2,
@scale_x, @scale_y, @text.color @scale_x, @scale_y, @text.color
) )
@@ -34,21 +34,21 @@ module CyberarmEngine
@text.draw @text.draw
end end
def recalculate def layout
unless @enabled unless @enabled
@style.background_canvas.background = @style.disabled[:background] @background_canvas.background = @style.disabled.background
@text.color = @style.disabled[:color] @text.color = @style.disabled.color
else else
@style.background_canvas.background = @style.background @background_canvas.background = styled(:background)
@text.color = @style.color @text.color = styled(:color)
end end
if @image if @image
@width = 0 @width = 0
@height = 0 @height = 0
_width = dimensional_size(@style.image_width, :width) _width = dimensional_size(styled(:image_width), :width)
_height = dimensional_size(@style.image_height, :height) _height = dimensional_size(styled(:image_height), :height)
if _width && _height if _width && _height
@scale_x = _width.to_f / @image.width @scale_x = _width.to_f / @image.width

View File

@@ -4,16 +4,16 @@ module CyberarmEngine
include Common include Common
attr_accessor :stroke_color, :fill_color attr_accessor :stroke_color, :fill_color
attr_reader :children, :gui_state, :scroll_position attr_reader :children, :gui_state, :scroll_position, :scroll_target_position
def self.current_container def self.current_container
@@current_container @current_container
end end
def self.current_container=(container) 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) 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 end
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@@ -26,6 +26,11 @@ module CyberarmEngine
@scroll_chunk = 120 @scroll_chunk = 120
@scroll_speed = 40 @scroll_speed = 40
if @gui_state
@width = window.width
@height = window.height
end
@text_color = options[:color] @text_color = options[:color]
@children = [] @children = []
@@ -34,7 +39,7 @@ module CyberarmEngine
end end
def build def build
@block.call(self) if @block @block&.call(self)
root.gui_state.request_recalculate_for(self) root.gui_state.request_recalculate_for(self)
end end
@@ -49,24 +54,24 @@ module CyberarmEngine
root.gui_state.request_recalculate_for(self) if @children.delete(element) root.gui_state.request_recalculate_for(self) if @children.delete(element)
end end
def clear(&block) def append(&block)
@children.clear
old_container = CyberarmEngine::Element::Container.current_container old_container = CyberarmEngine::Element::Container.current_container
CyberarmEngine::Element::Container.current_container = self CyberarmEngine::Element::Container.current_container = self
block.call(self) if block block&.call(self)
CyberarmEngine::Element::Container.current_container = old_container CyberarmEngine::Element::Container.current_container = old_container
root.gui_state.request_recalculate_for(self) root.gui_state.request_recalculate_for(self)
end end
def append(&block) def clear(&block)
@children.clear
old_container = CyberarmEngine::Element::Container.current_container old_container = CyberarmEngine::Element::Container.current_container
CyberarmEngine::Element::Container.current_container = self CyberarmEngine::Element::Container.current_container = self
block.call(self) if block block&.call(self)
CyberarmEngine::Element::Container.current_container = old_container CyberarmEngine::Element::Container.current_container = old_container
@@ -75,8 +80,8 @@ module CyberarmEngine
def render def render
Gosu.clip_to( Gosu.clip_to(
@x + @style.border_thickness_left + @style.padding_left, @x + styled(:border_thickness_left) + styled(:padding_left),
@y + @style.border_thickness_top + @style.padding_top, @y + styled(:border_thickness_top) + styled(:padding_top),
content_width + 1, content_width + 1,
content_height + 1 content_height + 1
) do ) do
@@ -89,13 +94,11 @@ module CyberarmEngine
def debug_draw def debug_draw
super super
@children.each do |child| @children.each(&:debug_draw)
child.debug_draw
end
end end
def update def update
update_scroll update_scroll if styled(:scroll)
@children.each(&:update) @children.each(&:update)
end end
@@ -111,7 +114,7 @@ module CyberarmEngine
case child case child
when Container when Container
if element = child.hit_element?(child_x, child_y) if (element = child.hit_element?(child_x, child_y))
return element return element
end end
else else
@@ -128,23 +131,16 @@ module CyberarmEngine
end end
def update_scroll def update_scroll
dt = window.dt > 1 ? 1.0 : window.dt 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_x_diff = (@scroll_target_position.x - @scroll_position.x) @scroll_position.y += (((@scroll_target_position.y - @scroll_position.y) * (@scroll_speed / 4.0) * 0.98) * dt).round
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 # Scrolled PAST top
if @scroll_position.y > 0 if @scroll_position.y > 0
@scroll_target_position.y = 0 @scroll_target_position.y = 0
# Scrolled PAST bottom # 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 @scroll_target_position.y = -max_scroll_height
end end
@@ -158,20 +154,20 @@ module CyberarmEngine
end end
def recalculate def recalculate
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top) @current_position = Vector.new(styled(:margin_left) + styled(:padding_left), styled(:margin_top) + styled(:padding_top))
return unless visible? return unless visible?
Stats.frame.increment(:gui_recalculations) Stats.frame&.increment(:gui_recalculations)
stylize
# s = Gosu.milliseconds # s = Gosu.milliseconds
stylize
layout layout
old_width = @width # Old sizes MUST be determined AFTER call to layout
old_height = @height old_width = width
old_height = height
@cached_scroll_width = nil @cached_scroll_width = nil
@cached_scroll_height = nil @cached_scroll_height = nil
@@ -192,10 +188,10 @@ module CyberarmEngine
# FIXME: Correctly handle alignment when element has siblings # FIXME: Correctly handle alignment when element has siblings
# FIXME: Enable alignment for any element, not just containers # FIXME: Enable alignment for any element, not just containers
if @style.v_align if styled(:v_align)
space = space_available_height space = space_available_height
case @style.v_align case styled(:v_align)
when :center when :center
@y = parent.height / 2 - height / 2 @y = parent.height / 2 - height / 2
when :bottom when :bottom
@@ -203,10 +199,10 @@ module CyberarmEngine
end end
end end
if @style.h_align if styled(:h_align)
space = space_available_width space = space_available_width
case @style.h_align case styled(:h_align)
when :center when :center
@x = parent.width / 2 - width / 2 @x = parent.width / 2 - width / 2
when :right when :right
@@ -214,39 +210,38 @@ module CyberarmEngine
end end
end end
# t = Gosu.milliseconds
# Move children to parent after positioning # Move children to parent after positioning
@children.each do |child| @children.each do |child|
child.x += (@x + @style.border_thickness_left) - style.margin_left child.x += (@x + styled(:border_thickness_left)) - styled(:margin_left)
child.y += (@y + @style.border_thickness_top) - style.margin_top child.y += (@y + styled(:border_thickness_top)) - styled(:margin_top)
child.stylize child.stylize
child.recalculate child.recalculate
child.reposition # TODO: Implement top,bottom,left,center, and right positioning child.reposition # TODO: Implement top,bottom,left,center, and right positioning
Stats.frame.increment(:gui_recalculations) Stats.frame&.increment(:gui_recalculations)
update_child_element_visibity(child) update_child_element_visibity(child)
end end
# puts "TOOK: #{Gosu.milliseconds - t}ms to recalculate #{self.class}:0x#{object_id.to_s(16)}'s #{@children.count} children" if is_root?
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
update_background update_background
# Fixes resized container scrolled past bottom # Fixes resized container scrolled past bottom
if old_height != @height
self.scroll_top = -@scroll_position.y self.scroll_top = -@scroll_position.y
@scroll_target_position.y = @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 end
root.gui_state.request_repaint if @width != old_width || @height != old_height # Fixes resized container that is scrolled down from being stuck overscrolled when resized
if scroll_height < height
@scroll_target_position.y = 0
end
recalculate_if_size_changed recalculate_if_size_changed
# puts "TOOK: #{Gosu.milliseconds - s}ms to recalculate #{self.class}:0x#{self.object_id.to_s(16)}"
end end
def layout def layout
@@ -254,13 +249,6 @@ module CyberarmEngine
end end
def max_width 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
outer_width outer_width
end end
@@ -270,8 +258,8 @@ module CyberarmEngine
end end
def position_on_current_line(element) # Flow def position_on_current_line(element) # Flow
element.x = element.style.margin_left + @current_position.x element.x = element.styled(:margin_left) + @current_position.x
element.y = element.style.margin_top + @current_position.y element.y = element.styled(:margin_top) + @current_position.y
@current_position.x += element.outer_width @current_position.x += element.outer_width
end end
@@ -287,49 +275,87 @@ module CyberarmEngine
end end
def position_on_next_line(element) # Flow def position_on_next_line(element) # Flow
@current_position.x = @style.margin_left + @style.padding_left @current_position.x = styled(:margin_left) + styled(:padding_left)
@current_position.y += tallest_neighbor(element, @current_position.y).outer_height @current_position.y += tallest_neighbor(element, @current_position.y).outer_height
element.x = element.style.margin_left + @current_position.x element.x = element.styled(:margin_left) + @current_position.x
element.y = element.style.margin_top + @current_position.y element.y = element.styled(:margin_top) + @current_position.y
@current_position.x += element.outer_width @current_position.x += element.outer_width
end end
def move_to_next_line(element) # Stack def move_to_next_line(element) # Stack
element.x = element.style.margin_left + @current_position.x element.x = element.styled(:margin_left) + @current_position.x
element.y = element.style.margin_top + @current_position.y element.y = element.styled(:margin_top) + @current_position.y
@current_position.y += element.outer_height @current_position.y += element.outer_height
end end
def mouse_wheel_up(sender, x, y) def mouse_wheel_up(sender, x, y)
return unless @style.scroll return unless styled(:scroll)
# Allow overscrolling UP, only if one can scroll DOWN # Allow overscrolling UP, only if one can scroll DOWN
if height < scroll_height return unless height < scroll_height
if @scroll_target_position.y > 0
if @scroll_target_position.y.positive?
@scroll_target_position.y = @scroll_chunk @scroll_target_position.y = @scroll_chunk
else else
@scroll_target_position.y += @scroll_chunk @scroll_target_position.y += @scroll_chunk
end end
return :handled :handled
end
end end
def mouse_wheel_down(sender, x, y) def mouse_wheel_down(sender, x, y)
return unless @style.scroll return unless styled(:scroll)
return unless height < scroll_height return unless height < scroll_height
if @scroll_target_position.y > 0 if @scroll_target_position.y.positive?
@scroll_target_position.y = -@scroll_chunk @scroll_target_position.y = -@scroll_chunk
else else
@scroll_target_position.y -= @scroll_chunk @scroll_target_position.y -= @scroll_chunk
end end
return :handled :handled
end
def scroll_jump_to_top(sender, x, y)
return unless styled(:scroll)
@scroll_position.y = 0
@scroll_target_position.y = 0
:handled
end
def scroll_jump_to_end(sender, x, y)
return unless styled(: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 styled(: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 styled(: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 end
def scroll_top def scroll_top

View File

@@ -79,7 +79,19 @@ module CyberarmEngine
end end
def caret_position_under_mouse(mouse_x, mouse_y) def caret_position_under_mouse(mouse_x, mouse_y)
active_line = row_at(mouse_y) # get y scroll offset of element to get EditBox's selected row
y_scroll_offset = 0
e = parent
while (e)
if e.is_a?(Container)
y_scroll_offset += e.scroll_position.y
# root element has no parent so loop will terminate
e = e.parent
end
end
active_line = row_at(mouse_y - y_scroll_offset)
right_offset = column_at(mouse_x, mouse_y) right_offset = column_at(mouse_x, mouse_y)
buffer = @text_input.text.lines[0..active_line].join if active_line != 0 buffer = @text_input.text.lines[0..active_line].join if active_line != 0

View File

@@ -168,7 +168,7 @@ module CyberarmEngine
@last_text = @text.text @last_text = @text.text
@last_pos = caret_pos @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 # Do nothing
elsif caret_pos < @offset_x elsif caret_pos < @offset_x
@@ -197,9 +197,9 @@ module CyberarmEngine
def text_input_position_for(method) def text_input_position_for(method)
if @type == :password 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) - styled(:border_thickness_left)
else else
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left @text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - styled(:border_thickness_left)
end end
end end
@@ -278,7 +278,7 @@ module CyberarmEngine
:handled :handled
end end
def recalculate def layout
super super
@width = dimensional_size(@style.width, :width) || default(:width) @width = dimensional_size(@style.width, :width) || default(:width)

View File

@@ -14,10 +14,10 @@ module CyberarmEngine
def render def render
@image.draw( @image.draw(
@style.border_thickness_left + @style.padding_left + @x, styled(:border_thickness_left) + styled(:padding_left) + @x,
@style.border_thickness_top + @style.padding_top + @y, styled(:border_thickness_top) + styled(:padding_top) + @y,
@z + 2, @z + 2,
@scale_x, @scale_y, @style.color @scale_x, @scale_y, styled(:color)
) )
end end
@@ -27,7 +27,7 @@ module CyberarmEngine
:handled :handled
end end
def recalculate def layout
_width = dimensional_size(@style.width, :width) _width = dimensional_size(@style.width, :width)
_height = dimensional_size(@style.height, :height) _height = dimensional_size(@style.height, :height)

View File

@@ -10,23 +10,9 @@ module CyberarmEngine
super(@choose, options, block) super(@choose, options, block)
@style.background_canvas.background = default(:background) @background_canvas.background = default(:background)
# TODO: "Clean Up" into own class? @menu = Menu.new(parent: self, theme: @options[:theme])
@menu = Stack.new(parent: self, theme: @options[:theme])
@menu.define_singleton_method(:recalculate_menu) do
@x = @__list_box.x
@y = @__list_box.y + @__list_box.height
@y = @__list_box.y - height if @y + height > window.height
end
@menu.instance_variable_set(:"@__list_box", self)
def @menu.recalculate
super
recalculate_menu
end
self.choose = @choose self.choose = @choose
end end
@@ -35,12 +21,18 @@ module CyberarmEngine
super super
w = @text.textobject.text_width("") w = @text.textobject.text_width("")
@text.textobject.draw_text("", @x + content_width - w, @y + @style.padding_top, @z, 1, 1, @text.color) @text.textobject.draw_text("", @x + content_width - w, @y + styled(:padding_top), @z, 1, 1, @text.color)
end end
def choose=(item) def choose=(item)
valid = @items.detect { |i| i == item } valid = @items.detect { |i| i == item }
raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid
unless valid
warn "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}"
item = @items.first
raise "No items list" unless item
end
@choose = item @choose = item
@@ -62,39 +54,25 @@ module CyberarmEngine
end end
def show_menu def show_menu
@menu.clear @menu.clear do
@menu.style.width = width @menu.style.width = width
@items.each do |item| @items.each do |item|
# prevent already selected item from appearing in list
# NOTE: Remove this? Might be kinda confusing...
next if item == self.value next if item == self.value
btn = Button.new( root.gui_state.menu_item(item, width: 1.0, margin: 0, border_color: 0x00ffffff) do
item,
{
parent: @menu,
width: 1.0,
theme: @options[:theme],
margin: 0,
border_color: 0x00ffffff
},
proc do
self.choose = item self.choose = item
@block&.call(self.value) @block&.call(self.value)
end end
)
@menu.add(btn)
end end
end
recalculate recalculate
root.gui_state.show_menu(@menu) @menu.show
end
def recalculate
super
@menu.recalculate
end end
end end
end end

View 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

View File

@@ -0,0 +1,6 @@
module CyberarmEngine
class Element
class MenuItem < Button
end
end
end

View File

@@ -11,7 +11,7 @@ module CyberarmEngine
@marquee_offset = 0 @marquee_offset = 0
@marquee_animation_time = Gosu.milliseconds @marquee_animation_time = Gosu.milliseconds
@type = options[:type] || :linear @type = options[:type] || :linear
@fraction_background = Background.new(background: @style.fraction_background) @fraction_background = Background.new(background: styled(:fraction_background))
self.value = options[:fraction] || 0.0 self.value = options[:fraction] || 0.0
end end
@@ -19,7 +19,7 @@ module CyberarmEngine
@fraction_background.draw @fraction_background.draw
end end
def recalculate def layout
_width = dimensional_size(@style.width, :width) _width = dimensional_size(@style.width, :width)
_height = dimensional_size(@style.height, :height) _height = dimensional_size(@style.height, :height)
@width = _width @width = _width
@@ -31,13 +31,13 @@ module CyberarmEngine
def update_background def update_background
super super
@fraction_background.x = (@style.border_thickness_left + @style.padding_left + @x) + @marquee_offset @fraction_background.x = (styled(:border_thickness_left) + styled(:padding_left) + @x) + @marquee_offset
@fraction_background.y = @style.border_thickness_top + @style.padding_top + @y @fraction_background.y = styled(:border_thickness_top) + styled(:padding_top) + @y
@fraction_background.z = @z @fraction_background.z = @z
@fraction_background.width = @width * (@type == :marquee ? @marquee_width : @fraction) @fraction_background.width = @width * (@type == :marquee ? @marquee_width : @fraction)
@fraction_background.height = @height @fraction_background.height = @height
@fraction_background.background = @style.fraction_background @fraction_background.background = styled(:fraction_background)
end end
def update def update

View File

@@ -33,7 +33,7 @@ module CyberarmEngine
end end
end end
attr_reader :step_size, :value attr_reader :value
attr_accessor :range, :step_size attr_accessor :range, :step_size
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@@ -62,10 +62,10 @@ module CyberarmEngine
end end
def position_handle def position_handle
@handle.x = @x + @handle.style.margin_left + @style.padding_left + @style.border_thickness_left + @handle.x = @x + @handle.styled(:margin_left) + styled(:padding_left) + styled(:border_thickness_left) +
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f) ((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
@handle.y = @y + @handle.style.margin_top + @style.border_thickness_top + @style.padding_top @handle.y = @y + @handle.styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top)
end end
def draw def draw

View File

@@ -16,6 +16,8 @@ module CyberarmEngine
) )
@raw_text = text @raw_text = text
@text_width = @text.width
@text_height = @text.height
end end
def update def update
@@ -29,7 +31,7 @@ module CyberarmEngine
def render def render
# Gosu.clip_to is too expensive to always use so check if we actually need it. # Gosu.clip_to is too expensive to always use so check if we actually need it.
if @text.width > width || @text.height > height if @text_width > width || @text_height > height
Gosu.clip_to(@x, @y, width, height) do Gosu.clip_to(@x, @y, width, height) do
@text.draw @text.draw
end end
@@ -38,16 +40,13 @@ module CyberarmEngine
end end
end end
def recalculate def layout
unless @enabled unless @enabled
@text.color = @style.disabled[:color] @text.color = @style.disabled.color
else else
@text.color = @style.color @text.color = styled(:color)
end end
old_width = @width
old_height = @height
@width = 0 @width = 0
@height = 0 @height = 0
@@ -56,50 +55,51 @@ module CyberarmEngine
handle_text_wrapping(_width) handle_text_wrapping(_width)
@width = _width || @text.width.floor # Update cached text width and height
@height = _height || @text.height.floor @text_width = @text.width
@text_height = @text.height
@text.y = @style.border_thickness_top + @style.padding_top + @y @width = _width || @text_width.floor
@height = _height || @text_height.floor
@text.y = styled(:border_thickness_top) + styled(:padding_top) + @y
@text.z = @z + 3 @text.z = @z + 3
if (text_alignment = @options[:text_align] || @options[:text_h_align]) if (text_alignment = @options[:text_align] || @options[:text_h_align])
case text_alignment case text_alignment
when :left when :left
@text.x = @style.border_thickness_left + @style.padding_left + @x @text.x = styled(:border_thickness_left) + styled(:padding_left) + @x
when :center when :center
@text.x = if @text.width <= width @text.x = if @text_width <= width
@x + width / 2 - @text.width / 2 @x + width / 2 - @text_width / 2
else # Act as left aligned else # Act as left aligned
@style.border_thickness_left + @style.padding_left + @x styled(:border_thickness_left) + styled(:padding_left) + @x
end end
when :right when :right
@text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right) @text.x = @x + outer_width - (@text_width + styled(:border_thickness_right) + styled(:padding_right))
end end
end end
if (vertical_alignment = @options[:text_v_align]) if (vertical_alignment = @options[:text_v_align])
case vertical_alignment case vertical_alignment
when :center when :center
@text.y = if @text.height <= height @text.y = if @text_height <= height
@y + height / 2 - @text.height / 2 @y + height / 2 - @text_height / 2
else else
@style.border_thickness_top + @style.padding_top + @y styled(:border_thickness_top) + styled(:padding_top) + @y
end end
when :bottom when :bottom
@text.y = @y + outer_height - (@text.height + @style.border_thickness_bottom + @style.padding_bottom) @text.y = @y + outer_height - (@text_height + styled(:border_thickness_bottom) + styled(:padding_bottom))
end end
end end
update_background update_background
root.gui_state.request_repaint if @width != old_width || @height != old_height
recalculate_if_size_changed
end end
def handle_text_wrapping(max_width) def handle_text_wrapping(max_width)
max_width ||= @parent&.content_width max_width ||= @parent&.content_width
max_width ||= @x - (window.width + noncontent_width) max_width ||= @x - (window.width + noncontent_width)
wrap_behavior = style.text_wrap wrap_behavior = styled(:text_wrap)
copy = @raw_text.to_s.dup copy = @raw_text.to_s.dup
# Only perform text wrapping: if it is enabled, is possible to wrap, and text is too long to fit on one line # Only perform text wrapping: if it is enabled, is possible to wrap, and text is too long to fit on one line

View File

@@ -7,8 +7,6 @@ module CyberarmEngine
if options.dig(:theme, :ToggleButton, :checkmark_image) if options.dig(:theme, :ToggleButton, :checkmark_image)
options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size] options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size]
super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block) super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)
@_image = @image
else else
super(options[:checkmark], options, block) super(options[:checkmark], options, block)
end end
@@ -16,10 +14,8 @@ module CyberarmEngine
@value = options[:checked] || false @value = options[:checked] || false
if @value if @value
@image = @_image if @_image
@raw_text = @options[:checkmark] @raw_text = @options[:checkmark]
else else
@image = nil
@raw_text = "" @raw_text = ""
end end
end end
@@ -32,6 +28,14 @@ module CyberarmEngine
:handled :handled
end end
def render
if @image
draw_image if @value
else
draw_text
end
end
def recalculate def recalculate
super super
return if @image return if @image
@@ -49,10 +53,8 @@ module CyberarmEngine
@value = boolean @value = boolean
if boolean if boolean
@image = @_image if @_image
@raw_text = @options[:checkmark] @raw_text = @options[:checkmark]
else else
@image = nil
@raw_text = "" @raw_text = ""
end end

View File

@@ -6,6 +6,7 @@ module CyberarmEngine
def initialize(options = {}) def initialize(options = {})
@options = options @options = options
@game_objects = [] @game_objects = []
@timers = []
@global_pause = false @global_pause = false
@down_keys = {} @down_keys = {}
@@ -38,6 +39,10 @@ module CyberarmEngine
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme) @tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
end end
def menu
@menu
end
# throws :blur event to focused element and sets GuiState focused element # throws :blur event to focused element and sets GuiState focused element
# Does NOT throw :focus event at element or set element as focused # Does NOT throw :focus event at element or set element as focused
def focus=(element) def focus=(element)
@@ -50,6 +55,29 @@ module CyberarmEngine
end end
def draw def draw
# t = Gosu.milliseconds
# report_recalculate_time = @pending_recalculate_request || @pending_element_recalculate_requests.size.positive?
StackProf.start(mode: :wall) if RUBY_ENGINE != "mruby" && defined?(StackProf)
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.shift(&:recalculate)
Stats.frame.end_timing(:gui_element_recalculate_requests)
Stats.frame.start_timing(:gui_recalculate)
while(@pending_recalculate_request)
@pending_recalculate_request = false
@root_container.recalculate
end
StackProf.stop if RUBY_ENGINE != "mruby" && defined?(StackProf)
Stats.frame.end_timing(:gui_recalculate)
# puts "TOOK: #{Gosu.milliseconds - t}ms to recalculate #{self.class}:0x#{object_id.to_s(16)}" if report_recalculate_time && Gosu.milliseconds - t > 0
super super
if @menu if @menu
@@ -63,8 +91,7 @@ module CyberarmEngine
@tip.draw @tip.draw
end end
# FIXME if CyberarmEngine.const_defined?("GUI_DEBUG")
if false# defined?(GUI_DEBUG)
Gosu.flush Gosu.flush
@root_container.debug_draw @root_container.debug_draw
@@ -78,24 +105,6 @@ module CyberarmEngine
end end
def update def update
Stats.frame.start_timing(:gui_element_recalculate_requests)
# puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?
@pending_element_recalculate_requests.each(&:recalculate)
@pending_element_recalculate_requests.clear
Stats.frame.end_timing(:gui_element_recalculate_requests)
if @pending_recalculate_request
Stats.frame.start_timing(:gui_recalculate)
@root_container.recalculate
@pending_recalculate_request = false
Stats.frame.end_timing(:gui_recalculate)
end
if @pending_focus_request if @pending_focus_request
@pending_focus_request = false @pending_focus_request = false
@@ -186,6 +195,14 @@ module CyberarmEngine
redirect_mouse_wheel(:up) redirect_mouse_wheel(:up)
when Gosu::MS_WHEEL_DOWN when Gosu::MS_WHEEL_DOWN
redirect_mouse_wheel(:down) redirect_mouse_wheel(:down)
when Gosu::KB_HOME
redirect_scroll_jump_to(:top)
when Gosu::KB_END
redirect_scroll_jump_to(:end)
when Gosu::KB_PAGE_UP
redirect_scroll_page(:up)
when Gosu::KB_PAGE_DOWN
redirect_scroll_page(:down)
end end
@focus.button_up(id) if @focus.respond_to?(:button_up) @focus.button_up(id) if @focus.respond_to?(:button_up)
@@ -251,7 +268,19 @@ module CyberarmEngine
end end
def redirect_mouse_wheel(button) def redirect_mouse_wheel(button)
@mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over @mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
end
def redirect_scroll_jump_to(edge)
return if window.text_input
@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)
return if window.text_input
@mouse_over.publish(:"scroll_page_#{edge}", window.mouse_x, window.mouse_y) if (@mouse_over && !@menu) || (@mouse_over && @mouse_over == @menu)
end end
# Schedule a full GUI recalculation on next update # Schedule a full GUI recalculation on next update

View File

@@ -16,35 +16,71 @@ module Gosu
end end
module CyberarmEngine module CyberarmEngine
class Style class StyleData
attr_reader :hash def initialize(hash = {})
@hash = hash
@dirty = false
end
%i[
x y z width height min_width min_height max_width max_height color background
background_image background_image_mode background_image_color
background_nine_slice background_nine_slice_mode background_nine_slice_color background_nine_slice_from_edge
background_nine_slice_left background_nine_slice_top background_nine_slice_right background_nine_slice_bottom
border_color border_color_left border_color_right border_color_top border_color_bottom
border_thickness border_thickness_left border_thickness_right border_thickness_top border_thickness_bottom
padding padding_left padding_right padding_top padding_bottom
margin margin_left margin_right margin_top margin_bottom
fraction_background scroll fill text_wrap v_align h_align delay tag font text_size
image_width image_height
].each do |item|
define_method(item) do
@hash[item]
end
define_method(:"#{item}=") do |value|
@dirty = true if @hash[item] != value
@hash[item] = value
end
end
# NOTE: do not change return value
def default
nil
end
def dirty?
@dirty
end
def mark_clean!
@dirty = false
end
end
class Style < StyleData
attr_reader :hash, :hover, :active, :disabled
def initialize(hash = {}) def initialize(hash = {})
h = hash @hash = hash
# h = Marshal.load(Marshal.dump(hash))
h[:default] = {} @hover = StyleData.new(hash[:hover] || {})
@active = StyleData.new(hash[:active] || {})
@disabled = StyleData.new(hash[:disabled] || {})
h.each do |key, value| @substyles = [@hover, @active, @disabled]
next if value.is_a?(Hash)
h[:default][key] = value super
end end
@hash = h def dirty?
@dirty || @substyles.any?(&:dirty?)
end end
def method_missing(method, *args) def mark_clean!
if method.to_s.end_with?("=") @substyles.each(&:mark_clean!)
raise "Did not expect more than 1 argument" if args.size > 1 @dirty = false
@hash[method.to_s.sub("=", "").to_sym] = args.first
elsif args.empty?
@hash[method]
else
raise ArgumentError, "Did not expect arguments"
end
end end
end end
end end

View File

@@ -208,6 +208,17 @@ module CyberarmEngine
fraction_background: [0xffc75e61, 0xffe26623], fraction_background: [0xffc75e61, 0xffe26623],
border_thickness: 1, border_thickness: 1,
border_color: [0xffd59674, 0xffff8746] border_color: [0xffd59674, 0xffff8746]
},
Menu: { # < Stack
width: 200,
border_color: 0xaa_efefef,
border_thickness: 1
},
MenuItem: { # < Button
width: 1.0,
text_left: :left,
margin: 0
} }
}.freeze }.freeze
end end

View File

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