mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-17 05:22:35 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c34293307 | |||
| 8172dafa8c | |||
| 9a3caabc7e | |||
| 4b25d724b5 | |||
| a267695b1c | |||
| d76da62ee1 | |||
| 3a83e616d3 | |||
| 52940d1cfe | |||
| e59771e412 | |||
| b382bf6960 | |||
| a5ed650970 | |||
| 935d6e9178 | |||
| cc813c023f | |||
| 5f359a8313 | |||
| c67d6f9d26 | |||
| 7f97ec85dd | |||
| 89a54e90a1 | |||
| 48d2924c9b | |||
| 7cc733dee4 | |||
| 28c4acdedf | |||
| 57313a33a6 | |||
| 5d16500edd | |||
| 51dd3803fa | |||
| c6beab3e99 |
@@ -28,7 +28,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 = ["lib"]
|
spec.require_paths = ["lib"]
|
||||||
|
|
||||||
spec.add_dependency "gosu", "~> 0.14.0"
|
spec.add_dependency "gosu", "~> 0.15.0"
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 1.16"
|
spec.add_development_dependency "bundler", "~> 1.16"
|
||||||
spec.add_development_dependency "rake", "~> 10.0"
|
spec.add_development_dependency "rake", "~> 10.0"
|
||||||
|
|||||||
@@ -5,22 +5,27 @@ rescue LoadError => e
|
|||||||
require "gosu"
|
require "gosu"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
require_relative "cyberarm_engine/version"
|
require_relative "cyberarm_engine/version"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/common"
|
require_relative "cyberarm_engine/common"
|
||||||
|
|
||||||
|
require_relative "cyberarm_engine/gosu_ext/circle"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/game_object"
|
require_relative "cyberarm_engine/game_object"
|
||||||
require_relative "cyberarm_engine/engine"
|
require_relative "cyberarm_engine/engine"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/lib/bounding_box"
|
require_relative "cyberarm_engine/bounding_box"
|
||||||
require_relative "cyberarm_engine/lib/vector"
|
require_relative "cyberarm_engine/vector"
|
||||||
require_relative "cyberarm_engine/lib/ray"
|
require_relative "cyberarm_engine/transform"
|
||||||
require_relative "cyberarm_engine/lib/shader" if defined?(OpenGL)
|
require_relative "cyberarm_engine/ray"
|
||||||
|
require_relative "cyberarm_engine/shader" if defined?(OpenGL)
|
||||||
require_relative "cyberarm_engine/background"
|
require_relative "cyberarm_engine/background"
|
||||||
|
require_relative "cyberarm_engine/animator"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/objects/text"
|
require_relative "cyberarm_engine/text"
|
||||||
require_relative "cyberarm_engine/objects/timer"
|
require_relative "cyberarm_engine/timer"
|
||||||
|
|
||||||
|
require_relative "cyberarm_engine/ui/dsl"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/ui/theme"
|
require_relative "cyberarm_engine/ui/theme"
|
||||||
require_relative "cyberarm_engine/ui/event"
|
require_relative "cyberarm_engine/ui/event"
|
||||||
@@ -38,7 +43,5 @@ require_relative "cyberarm_engine/ui/elements/stack"
|
|||||||
require_relative "cyberarm_engine/ui/elements/check_box"
|
require_relative "cyberarm_engine/ui/elements/check_box"
|
||||||
require_relative "cyberarm_engine/ui/elements/progress"
|
require_relative "cyberarm_engine/ui/elements/progress"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/ui/dsl"
|
|
||||||
|
|
||||||
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"
|
||||||
|
|||||||
54
lib/cyberarm_engine/animator.rb
Normal file
54
lib/cyberarm_engine/animator.rb
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Animator
|
||||||
|
DEFAULT_TWEEN = :linear
|
||||||
|
def initialize(start_time:, duration:, from:, to:, &block)
|
||||||
|
@start_time, @duration = start_time, duration
|
||||||
|
@from, @to = from.dup, to.dup
|
||||||
|
@block = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@block.call(self, @from, @to) if @block
|
||||||
|
end
|
||||||
|
|
||||||
|
def progress
|
||||||
|
(@start_time.to_f + (Gosu.milliseconds - @start_time)) / (@start_time + @duration.to_f)
|
||||||
|
end
|
||||||
|
|
||||||
|
def complete?
|
||||||
|
progress >= 1.0
|
||||||
|
end
|
||||||
|
|
||||||
|
def transition(from, to, tween = DEFAULT_TWEEN)
|
||||||
|
from + (to - from) * send("tween_#{tween}", progress)
|
||||||
|
end
|
||||||
|
|
||||||
|
def color_transition(from, to, tween = DEFAULT_TWEEN)
|
||||||
|
r = transition(from.red, to.red)
|
||||||
|
g = transition(from.green, to.green)
|
||||||
|
b = transition(from.blue, to.blue)
|
||||||
|
a = transition(from.alpha, to.alpha)
|
||||||
|
|
||||||
|
Gosu::Color.rgba(r, g, b, a)
|
||||||
|
end
|
||||||
|
|
||||||
|
def color_hsv_transition(from, to, tween = DEFAULT_TWEEN)
|
||||||
|
hue = transition(from.hue, to.hue, tween)
|
||||||
|
saturation = transition(from.saturation, to.saturation, tween)
|
||||||
|
value = transition(from.value, to.value, tween)
|
||||||
|
alpha = transition(from.alpha, to.alpha, tween)
|
||||||
|
|
||||||
|
Gosu::Color.from_ahsv(alpha, hue, saturation, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# NOTE: Use this for future reference? https://github.com/danro/easing-js/blob/master/easing.js
|
||||||
|
|
||||||
|
def tween_linear(t)
|
||||||
|
t
|
||||||
|
end
|
||||||
|
|
||||||
|
def tween_sine(t)
|
||||||
|
Math.sin(t) * t
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -54,7 +54,7 @@ module CyberarmEngine
|
|||||||
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_asset(path, hash, klass)
|
def get_asset(path, hash, klass, retro = false, tileable = false)
|
||||||
asset = nil
|
asset = nil
|
||||||
hash.detect do |_asset, instance|
|
hash.detect do |_asset, instance|
|
||||||
if _asset == path
|
if _asset == path
|
||||||
@@ -64,7 +64,12 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
unless asset
|
unless asset
|
||||||
instance = klass.new(path)
|
instance = nil
|
||||||
|
if klass == Gosu::Image
|
||||||
|
instance = klass.new(path, retro: retro, tileable: tileable)
|
||||||
|
else
|
||||||
|
instance = klass.new(path)
|
||||||
|
end
|
||||||
hash[path] = instance
|
hash[path] = instance
|
||||||
asset = instance
|
asset = instance
|
||||||
end
|
end
|
||||||
@@ -72,8 +77,8 @@ module CyberarmEngine
|
|||||||
return asset
|
return asset
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_image(path)
|
def get_image(path, retro: false, tileable: false)
|
||||||
get_asset(path, Engine::IMAGES, Gosu::Image)
|
get_asset(path, Engine::IMAGES, Gosu::Image, retro, tileable)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_sample(path)
|
def get_sample(path)
|
||||||
|
|||||||
9
lib/cyberarm_engine/gosu_ext/circle.rb
Normal file
9
lib/cyberarm_engine/gosu_ext/circle.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
module Gosu
|
||||||
|
# Sourced from https://gist.github.com/ippa/662583
|
||||||
|
def self.draw_circle(cx,cy,r, z = 9999,color = Gosu::Color::GREEN, step = 10)
|
||||||
|
0.step(360, step) do |a1|
|
||||||
|
a2 = a1 + step
|
||||||
|
draw_line(cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, z)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
|
||||||
class Shader
|
|
||||||
include OpenGL
|
|
||||||
@@shaders = {}
|
|
||||||
|
|
||||||
def self.add(name, instance)
|
|
||||||
@@shaders[name] = instance
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.use(name, &block)
|
|
||||||
shader = @@shaders.dig(name)
|
|
||||||
if shader
|
|
||||||
shader.use(&block)
|
|
||||||
else
|
|
||||||
raise ArgumentError, "Shader '#{name}' not found!"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.available?(name)
|
|
||||||
@@shaders.dig(name).is_a?(Shader)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get(name)
|
|
||||||
@@shaders.dig(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.active_shader
|
|
||||||
@active_shader
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.active_shader=(instance)
|
|
||||||
@active_shader = instance
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.stop
|
|
||||||
shader = Shader.active_shader
|
|
||||||
|
|
||||||
if shader
|
|
||||||
shader.stop
|
|
||||||
else
|
|
||||||
raise ArgumentError, "No active shader to stop!"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.attribute_location(variable)
|
|
||||||
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
|
||||||
Shader.active_shader.attribute_location(variable)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.set_uniform(variable, value)
|
|
||||||
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
|
||||||
Shader.active_shader.set_uniform(variable, value)
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_reader :name, :program
|
|
||||||
def initialize(name:, vertex: "shaders/default.vert", fragment:)
|
|
||||||
@name = name
|
|
||||||
@vertex_file = vertex
|
|
||||||
@fragment_file = fragment
|
|
||||||
@compiled = false
|
|
||||||
|
|
||||||
@program = nil
|
|
||||||
|
|
||||||
@error_buffer_size = 1024
|
|
||||||
@variable_missing = {}
|
|
||||||
|
|
||||||
raise ArgumentError, "Shader files not found: #{@vertex_file} or #{@fragment_file}" unless shader_files_exist?
|
|
||||||
|
|
||||||
create_shaders
|
|
||||||
compile_shaders
|
|
||||||
|
|
||||||
# Only add shader if it successfully compiles
|
|
||||||
if @compiled
|
|
||||||
Shader.add(@name, self)
|
|
||||||
else
|
|
||||||
puts "FAILED to compile shader: #{@name}", ""
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def shader_files_exist?
|
|
||||||
File.exist?(@vertex_file) && File.exist?(@fragment_file)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_shaders
|
|
||||||
@vertex = glCreateShader(GL_VERTEX_SHADER)
|
|
||||||
@fragment = glCreateShader(GL_FRAGMENT_SHADER)
|
|
||||||
|
|
||||||
source = [File.read(@vertex_file)].pack('p')
|
|
||||||
size = [File.size(@vertex_file)].pack('I')
|
|
||||||
glShaderSource(@vertex, 1, source, size)
|
|
||||||
|
|
||||||
source = [File.read(@fragment_file)].pack('p')
|
|
||||||
size = [File.size(@fragment_file)].pack('I')
|
|
||||||
glShaderSource(@fragment, 1, source, size)
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile_shaders
|
|
||||||
return unless shader_files_exist?
|
|
||||||
|
|
||||||
glCompileShader(@vertex)
|
|
||||||
buffer = ' '
|
|
||||||
glGetShaderiv(@vertex, GL_COMPILE_STATUS, buffer)
|
|
||||||
compiled = buffer.unpack('L')[0]
|
|
||||||
|
|
||||||
if compiled == 0
|
|
||||||
log = ' ' * @error_buffer_size
|
|
||||||
glGetShaderInfoLog(@vertex, @error_buffer_size, nil, log)
|
|
||||||
puts "Shader Error: Program \"#{@name}\""
|
|
||||||
puts " Vectex Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
|
||||||
puts " Shader Compiled status: #{compiled}"
|
|
||||||
puts " NOTE: assignment of uniforms in shaders is illegal!"
|
|
||||||
puts
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
glCompileShader(@fragment)
|
|
||||||
buffer = ' '
|
|
||||||
glGetShaderiv(@fragment, GL_COMPILE_STATUS, buffer)
|
|
||||||
compiled = buffer.unpack('L')[0]
|
|
||||||
|
|
||||||
if compiled == 0
|
|
||||||
log = ' ' * @error_buffer_size
|
|
||||||
glGetShaderInfoLog(@fragment, @error_buffer_size, nil, log)
|
|
||||||
puts "Shader Error: Program \"#{@name}\""
|
|
||||||
puts " Fragment Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
|
||||||
puts " Shader Compiled status: #{compiled}"
|
|
||||||
puts " NOTE: assignment of uniforms in shader is illegal!"
|
|
||||||
puts
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
@program = glCreateProgram
|
|
||||||
glAttachShader(@program, @vertex)
|
|
||||||
glAttachShader(@program, @fragment)
|
|
||||||
glLinkProgram(@program)
|
|
||||||
|
|
||||||
buffer = ' '
|
|
||||||
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
|
|
||||||
linked = buffer.unpack('L')[0]
|
|
||||||
|
|
||||||
if linked == 0
|
|
||||||
log = ' ' * @error_buffer_size
|
|
||||||
glGetProgramInfoLog(@program, @error_buffer_size, nil, log)
|
|
||||||
puts "Shader Error: Program \"#{@name}\""
|
|
||||||
puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
@compiled = linked == 0 ? false : true
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns the location of a uniform variable
|
|
||||||
def variable(variable)
|
|
||||||
loc = glGetUniformLocation(@program, variable)
|
|
||||||
if (loc == -1)
|
|
||||||
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable]
|
|
||||||
@variable_missing[variable] = true
|
|
||||||
end
|
|
||||||
return loc
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
glUseProgram(@program)
|
|
||||||
|
|
||||||
if block
|
|
||||||
block.call(self)
|
|
||||||
stop
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def stop
|
|
||||||
Shader.active_shader = nil if Shader.active_shader == self
|
|
||||||
glUseProgram(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
def compiled?
|
|
||||||
@compiled
|
|
||||||
end
|
|
||||||
|
|
||||||
def attribute_location(variable)
|
|
||||||
glGetUniformLocation(@program, variable)
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_uniform(variable, value, location = nil)
|
|
||||||
attr_loc = location ? location : attribute_location(variable)
|
|
||||||
|
|
||||||
case value.class.to_s.downcase.to_sym
|
|
||||||
when :integer
|
|
||||||
glUniform1i(attr_loc, value)
|
|
||||||
when :float
|
|
||||||
glUniform1f(attr_loc, value)
|
|
||||||
when :string
|
|
||||||
when :array
|
|
||||||
else
|
|
||||||
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
|
||||||
end
|
|
||||||
|
|
||||||
Window.handle_gl_error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
262
lib/cyberarm_engine/shader.rb
Normal file
262
lib/cyberarm_engine/shader.rb
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||||
|
class Shader
|
||||||
|
include OpenGL
|
||||||
|
@@shaders = {}
|
||||||
|
PREPROCESSOR_CHARACTER = "@"
|
||||||
|
|
||||||
|
def self.add(name, instance)
|
||||||
|
@@shaders[name] = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.use(name, &block)
|
||||||
|
shader = @@shaders.dig(name)
|
||||||
|
if shader
|
||||||
|
shader.use(&block)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Shader '#{name}' not found!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.available?(name)
|
||||||
|
@@shaders.dig(name).is_a?(Shader)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get(name)
|
||||||
|
@@shaders.dig(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.active_shader
|
||||||
|
@active_shader
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.active_shader=(instance)
|
||||||
|
@active_shader = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.stop
|
||||||
|
shader = Shader.active_shader
|
||||||
|
|
||||||
|
if shader
|
||||||
|
shader.stop
|
||||||
|
else
|
||||||
|
raise ArgumentError, "No active shader to stop!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.attribute_location(variable)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.attribute_location(variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set_uniform(variable, value)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.set_uniform(variable, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :name, :program
|
||||||
|
def initialize(name:, includes_dir: nil, vertex: "shaders/default.vert", fragment:)
|
||||||
|
raise "Shader name can not be blank" if name.length == 0
|
||||||
|
|
||||||
|
@name = name
|
||||||
|
@includes_dir = includes_dir
|
||||||
|
@compiled = false
|
||||||
|
|
||||||
|
@program = nil
|
||||||
|
|
||||||
|
@error_buffer_size = 1024
|
||||||
|
@variable_missing = {}
|
||||||
|
|
||||||
|
@data = {shaders: {}}
|
||||||
|
|
||||||
|
unless shader_files_exist?(vertex: vertex, fragment: fragment)
|
||||||
|
raise ArgumentError, "Shader files not found: #{vertex} or #{fragment}"
|
||||||
|
end
|
||||||
|
|
||||||
|
create_shader(type: :vertex, source: File.read(vertex))
|
||||||
|
create_shader(type: :fragment, source: File.read(fragment))
|
||||||
|
|
||||||
|
compile_shader(type: :vertex)
|
||||||
|
compile_shader(type: :fragment)
|
||||||
|
link_shaders
|
||||||
|
|
||||||
|
# Only add shader if it successfully compiles
|
||||||
|
if @compiled
|
||||||
|
puts "compiled!"
|
||||||
|
puts "Compiled shader: #{@name}"
|
||||||
|
Shader.add(@name, self)
|
||||||
|
else
|
||||||
|
warn "FAILED to compile shader: #{@name}", ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def shader_files_exist?(vertex:, fragment:)
|
||||||
|
File.exist?(vertex) && File.exist?(fragment)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_shader(type:, source:)
|
||||||
|
_shader = nil
|
||||||
|
|
||||||
|
case type
|
||||||
|
when :vertex
|
||||||
|
_shader = glCreateShader(GL_VERTEX_SHADER)
|
||||||
|
when :fragment
|
||||||
|
_shader = glCreateShader(GL_FRAGMENT_SHADER)
|
||||||
|
else
|
||||||
|
warn "Unsupported shader type: #{type.inspect}"
|
||||||
|
end
|
||||||
|
|
||||||
|
processed_source = preprocess_source(source: source)
|
||||||
|
|
||||||
|
_source = [processed_source].pack("p")
|
||||||
|
_size = [processed_source.length].pack("I")
|
||||||
|
glShaderSource(_shader, 1, _source, _size)
|
||||||
|
|
||||||
|
@data[:shaders][type] =_shader
|
||||||
|
end
|
||||||
|
|
||||||
|
def preprocess_source(source:)
|
||||||
|
lines = source.lines
|
||||||
|
|
||||||
|
lines.each_with_index do |line, i|
|
||||||
|
if line.start_with?(PREPROCESSOR_CHARACTER)
|
||||||
|
preprocessor = line.strip.split(" ")
|
||||||
|
lines.delete(line)
|
||||||
|
|
||||||
|
case preprocessor.first
|
||||||
|
when "@include"
|
||||||
|
raise ArgumentError, "Shader preprocessor include directory was not given for shader #{@name}" unless @includes_dir
|
||||||
|
|
||||||
|
preprocessor[1..preprocessor.length - 1].join.scan(/"([^"]*)"/).flatten.each do |file|
|
||||||
|
source = File.read("#{@includes_dir}/#{file}.glsl")
|
||||||
|
|
||||||
|
lines.insert(i, source)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
warn "Unsupported preprocessor #{preprocessor.first} for #{@name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
lines.join
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile_shader(type:)
|
||||||
|
_compiled = false
|
||||||
|
_shader = @data[:shaders][type]
|
||||||
|
raise ArgumentError, "No shader for #{type.inspect}" unless _shader
|
||||||
|
|
||||||
|
glCompileShader(_shader)
|
||||||
|
buffer = ' '
|
||||||
|
glGetShaderiv(_shader, GL_COMPILE_STATUS, buffer)
|
||||||
|
compiled = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if compiled == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetShaderInfoLog(_shader, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " #{type.to_s.capitalize} Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
puts " Shader Compiled status: #{compiled}"
|
||||||
|
puts " NOTE: assignment of uniforms in shaders is illegal!"
|
||||||
|
puts
|
||||||
|
else
|
||||||
|
_compiled = true
|
||||||
|
end
|
||||||
|
|
||||||
|
return _compiled
|
||||||
|
end
|
||||||
|
|
||||||
|
def link_shaders
|
||||||
|
@program = glCreateProgram
|
||||||
|
@data[:shaders].values.each do |_shader|
|
||||||
|
glAttachShader(@program, _shader)
|
||||||
|
end
|
||||||
|
glLinkProgram(@program)
|
||||||
|
|
||||||
|
buffer = ' '
|
||||||
|
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
|
||||||
|
linked = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if linked == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetProgramInfoLog(@program, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@compiled = linked == 0 ? false : true
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the location of a uniform variable
|
||||||
|
def variable(variable)
|
||||||
|
loc = glGetUniformLocation(@program, variable)
|
||||||
|
if (loc == -1)
|
||||||
|
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable]
|
||||||
|
@variable_missing[variable] = true
|
||||||
|
end
|
||||||
|
return loc
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
glUseProgram(@program)
|
||||||
|
|
||||||
|
if block
|
||||||
|
block.call(self)
|
||||||
|
stop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
Shader.active_shader = nil if Shader.active_shader == self
|
||||||
|
glUseProgram(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compiled?
|
||||||
|
@compiled
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_location(variable)
|
||||||
|
glGetUniformLocation(@program, variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_transform(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniformMatrix4fv(attr_loc, 1, GL_FALSE, value.to_gl.pack("F16"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_boolean(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1i(attr_loc, value ? 1 : 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_integer(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1i(attr_loc, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_float(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1f(attr_loc, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_vec3(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform3f(attr_loc, *value.to_a[0..2])
|
||||||
|
end
|
||||||
|
|
||||||
|
def uniform_vec4(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform4f(attr_loc, *value.to_a)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
273
lib/cyberarm_engine/transform.rb
Normal file
273
lib/cyberarm_engine/transform.rb
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
# Basic 4x4 matrix operations
|
||||||
|
class Transform
|
||||||
|
attr_reader :elements
|
||||||
|
def initialize(matrix)
|
||||||
|
@elements = matrix
|
||||||
|
|
||||||
|
raise "Transform is wrong size! Got #{@elements.size}, expected 16" if 16 != @elements.size
|
||||||
|
raise "Invalid value for matrix, must all be numeric!" if @elements.any? { |e| e.nil? || !e.is_a?(Numeric)}
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.identity
|
||||||
|
Transform.new(
|
||||||
|
[
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
### 2D Operations meant for interacting with Gosu ###
|
||||||
|
|
||||||
|
# 2d rotate operation, replicates Gosu's Gosu.rotate function
|
||||||
|
def self.rotate(angle, rotate_around = nil)
|
||||||
|
double c = Math.cos(angle).degrees_to_radians
|
||||||
|
double s = Math.sin(angle).degrees_to_radians
|
||||||
|
matrix = [
|
||||||
|
+c, +s, 0, 0,
|
||||||
|
-s, +c, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
rotate_matrix = Transform.new(matrix, rows: 4, columns: 4)
|
||||||
|
|
||||||
|
if rotate_around && (rotate_around.x != 0.0 || rotate_around.y != 0.0)
|
||||||
|
negative_rotate_around = Vector.new(-rotate_around.x, -rotate_around.y, -rotate_around.z)
|
||||||
|
|
||||||
|
rotate_matrix = concat(
|
||||||
|
concat(translate(negative_rotate_around), rotate_matrix),
|
||||||
|
translate(rotate_around)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return rotate_matrix
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2d translate operation, replicates Gosu's Gosu.translate function
|
||||||
|
def self.translate(vector)
|
||||||
|
x, y, z = vector.to_a[0..2]
|
||||||
|
matrix = [
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
x, y, z, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2d scale operation, replicates Gosu's Gosu.rotate function
|
||||||
|
def self.scale(vector, center_around = nil)
|
||||||
|
scale_x, scale_y, scale_z = vector.to_a[0..2]
|
||||||
|
matrix = [
|
||||||
|
scale_x, 0, 0, 0,
|
||||||
|
0, scale_y, 0, 0,
|
||||||
|
0, 0, scale_z, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
scale_matrix = Transform.new(matrix)
|
||||||
|
|
||||||
|
if center_around && (center_around.x != 0.0 || center_around.y != 0.0)
|
||||||
|
negative_center_around = Vector.new(-center_around.x, -center_around.y, -center_around.z)
|
||||||
|
|
||||||
|
scale_matrix = concat(
|
||||||
|
concat(translate(negative_center_around), scale_matrix),
|
||||||
|
translate(center_around)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return scale_matrix
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.concat(left, right)
|
||||||
|
matrix = Array.new(left.elements.size)
|
||||||
|
rows = 4
|
||||||
|
|
||||||
|
matrix.size.times do |i|
|
||||||
|
matrix[i] = 0
|
||||||
|
|
||||||
|
rows.times do |j|
|
||||||
|
matrix[i] += left.elements[i / rows * rows + j] * right.elements[i % rows + j * rows]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
end
|
||||||
|
|
||||||
|
#### 3D Operations meant for OpenGL ###
|
||||||
|
|
||||||
|
def self.translate_3d(vector)
|
||||||
|
x, y, z = vector.to_a[0..2]
|
||||||
|
matrix = [
|
||||||
|
1, 0, 0, x,
|
||||||
|
0, 1, 0, y,
|
||||||
|
0, 0, 1, z,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.rotate_3d(vector, order = "zyx")
|
||||||
|
x, y, z = vector.to_a[0..2].map { |axis| axis * Math::PI / 180.0 }
|
||||||
|
|
||||||
|
rotation_x = Transform.new(
|
||||||
|
[
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, Math.cos(x), -Math.sin(x), 0,
|
||||||
|
0, Math.sin(x), Math.cos(x), 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
rotation_y = Transform.new(
|
||||||
|
[
|
||||||
|
Math.cos(y), 0, Math.sin(y), 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
-Math.sin(y), 0, Math.cos(y), 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
rotation_z = Transform.new(
|
||||||
|
[
|
||||||
|
Math.cos(z), -Math.sin(z), 0, 0,
|
||||||
|
Math.sin(z), Math.cos(z), 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
rotation_z * rotation_y * rotation_x
|
||||||
|
end
|
||||||
|
|
||||||
|
# Implements glRotatef
|
||||||
|
# https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml
|
||||||
|
def self.rotate_gl(angle, axis)
|
||||||
|
radians = angle * Math::PI / 180.0
|
||||||
|
s = Math.sin(radians)
|
||||||
|
c = Math.cos(radians)
|
||||||
|
|
||||||
|
axis = axis.normalized
|
||||||
|
x, y, z = axis.to_a[0..2]
|
||||||
|
|
||||||
|
n = (1.0 - c)
|
||||||
|
|
||||||
|
Transform.new(
|
||||||
|
[
|
||||||
|
x * x * n + c, x * y * n - z * s, x * z * n + y * s, 0,
|
||||||
|
y * x * n + z * s, y * y * n + c, y * z * n - x * s, 0,
|
||||||
|
x * z * n - y * s, y * z * n + x * s, z * z * n + c, 0,
|
||||||
|
0, 0, 0, 1.0
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.scale_3d(vector)
|
||||||
|
x, y, z = vector.to_a[0..2]
|
||||||
|
|
||||||
|
Transform.new(
|
||||||
|
[
|
||||||
|
x, 0, 0, 0,
|
||||||
|
0, y, 0, 0,
|
||||||
|
0, 0, z, 0,
|
||||||
|
0, 0, 0, 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.perspective(fov_y, aspect_ratio, near, far)
|
||||||
|
f = 1.0 / Math.tan(fov_y.degrees_to_radians / 2.0) # cotangent
|
||||||
|
zn = (far + near.to_f) / (near - far.to_f)
|
||||||
|
zf = (2.0 * far * near.to_f) / (near - far.to_f)
|
||||||
|
|
||||||
|
Transform.new(
|
||||||
|
[
|
||||||
|
f / aspect_ratio, 0.0, 0.0, 0.0,
|
||||||
|
0.0, f, 0.0, 0.0,
|
||||||
|
0.0, 0.0, zn, zf,
|
||||||
|
0.0, 0.0, -1.0, 0.0
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.view(eye, orientation)
|
||||||
|
# https://www.3dgep.com/understanding-the-view-matrix/#The_View_Matrix
|
||||||
|
cosPitch = Math.cos(orientation.z * Math::PI / 180.0)
|
||||||
|
sinPitch = Math.sin(orientation.z * Math::PI / 180.0)
|
||||||
|
cosYaw = Math.cos(orientation.y * Math::PI / 180.0)
|
||||||
|
sinYaw = Math.sin(orientation.y * Math::PI / 180.0)
|
||||||
|
|
||||||
|
x_axis = Vector.new(cosYaw, 0, -sinYaw)
|
||||||
|
y_axis = Vector.new(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch)
|
||||||
|
z_axis = Vector.new(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw)
|
||||||
|
|
||||||
|
Transform.new(
|
||||||
|
[
|
||||||
|
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||||
|
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||||
|
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||||
|
-x_axis.dot(eye), -y_axis.dot(eye), -z_axis.dot(eye), 1
|
||||||
|
]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def *(other)
|
||||||
|
|
||||||
|
case other
|
||||||
|
when CyberarmEngine::Vector
|
||||||
|
matrix = @elements.clone
|
||||||
|
list = other.to_a
|
||||||
|
|
||||||
|
@elements.each_with_index do |e, i|
|
||||||
|
matrix[i] = e + list[i % 4]
|
||||||
|
end
|
||||||
|
|
||||||
|
Transform.new(matrix)
|
||||||
|
|
||||||
|
when CyberarmEngine::Transform
|
||||||
|
return multiply_matrices(other)
|
||||||
|
else
|
||||||
|
p other.class
|
||||||
|
raise TypeError, "Expected CyberarmEngine::Vector or CyberarmEngine::Transform got #{other.class}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(x, y)
|
||||||
|
width = 4
|
||||||
|
|
||||||
|
# puts "Transform|#{self.object_id} -> #{@elements[width * y + x].inspect} (index: #{width * y + x})"
|
||||||
|
@elements[width * y + x]
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiply_matrices(other)
|
||||||
|
matrix = Array.new(16, 0)
|
||||||
|
|
||||||
|
4.times do |x|
|
||||||
|
4.times do |y|
|
||||||
|
4.times do |k|
|
||||||
|
matrix[4 * y + x] += get(x, k) * other.get(k, y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Transform.new(matrix)
|
||||||
|
end
|
||||||
|
|
||||||
|
# arranges Matrix in column major form
|
||||||
|
def to_gl
|
||||||
|
e = @elements
|
||||||
|
[
|
||||||
|
e[0], e[4], e[8], e[12],
|
||||||
|
e[1], e[5], e[9], e[13],
|
||||||
|
e[2], e[6], e[10], e[14],
|
||||||
|
e[3], e[7], e[11], e[15]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,102 +1,99 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
module DSL
|
module DSL
|
||||||
def flow(options = {}, &block)
|
def flow(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
container(CyberarmEngine::Element::Flow, options, &block)
|
||||||
options[:theme] = current_theme
|
|
||||||
_container = Element::Flow.new(options, block)
|
|
||||||
@containers << _container
|
|
||||||
_container.build
|
|
||||||
_container.parent.add(_container)
|
|
||||||
@containers.pop
|
|
||||||
|
|
||||||
return _container
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def stack(options = {}, &block)
|
def stack(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
container(CyberarmEngine::Element::Stack, options, &block)
|
||||||
options[:theme] = current_theme
|
|
||||||
_container = Element::Stack.new(options, block)
|
|
||||||
@containers << _container
|
|
||||||
_container.build
|
|
||||||
_container.parent.add(_container)
|
|
||||||
@containers.pop
|
|
||||||
|
|
||||||
return _container
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def label(text, options = {}, &block)
|
def label(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::Label.new(text, options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::Label.new(text, options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def button(text, options = {}, &block)
|
def button(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end } )
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit_line(text, options = {}, &block)
|
def edit_line(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::EditLine.new(text, options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::EditLine.new(text, options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle_button(options = {}, &block)
|
def toggle_button(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::ToggleButton.new(options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::ToggleButton.new(options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_box(text, options = {}, &block)
|
def check_box(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::CheckBox.new(text, options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::CheckBox.new(text, options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def image(path, options = {}, &block)
|
def image(path, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::Image.new(path, options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::Image.new(path, options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def progress(options = {}, &block)
|
def progress(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = element_parent
|
||||||
options[:theme] = current_theme
|
options[:theme] = current_theme
|
||||||
_element = Element::Progress.new(options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::Progress.new(options, block) )
|
||||||
end
|
end
|
||||||
|
|
||||||
def background(color = Gosu::Color::NONE)
|
def background(color = Gosu::Color::NONE)
|
||||||
@containers.last.style.background = color
|
element_parent.style.background = color
|
||||||
end
|
end
|
||||||
|
|
||||||
def theme(theme)
|
def theme(theme)
|
||||||
@containers.last.options[:theme] = theme
|
element_parent.options[:theme] = theme
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_theme
|
def current_theme
|
||||||
@containers.last.options[:theme]
|
element_parent.options[:theme]
|
||||||
|
end
|
||||||
|
|
||||||
|
private def add_element(element)
|
||||||
|
element_parent.add(element)
|
||||||
|
|
||||||
|
return element
|
||||||
|
end
|
||||||
|
|
||||||
|
private def element_parent
|
||||||
|
$__current_container__
|
||||||
|
end
|
||||||
|
|
||||||
|
private def container(klass, options = {}, &block)
|
||||||
|
options[:parent] = element_parent
|
||||||
|
options[:theme] = current_theme
|
||||||
|
|
||||||
|
_container = klass.new(options, block)
|
||||||
|
|
||||||
|
old_parent = element_parent
|
||||||
|
$__current_container__ = _container
|
||||||
|
|
||||||
|
_container.build
|
||||||
|
_container.parent.add(_container)
|
||||||
|
|
||||||
|
$__current_container__ = old_parent
|
||||||
|
|
||||||
|
return _container
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -168,6 +168,14 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def content_width
|
||||||
|
@width
|
||||||
|
end
|
||||||
|
|
||||||
|
def noncontent_width
|
||||||
|
(inner_width + outer_width) - width
|
||||||
|
end
|
||||||
|
|
||||||
def outer_width
|
def outer_width
|
||||||
@style.margin_left + width + @style.margin_right
|
@style.margin_left + width + @style.margin_right
|
||||||
end
|
end
|
||||||
@@ -184,6 +192,14 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def content_height
|
||||||
|
@height
|
||||||
|
end
|
||||||
|
|
||||||
|
def noncontent_height
|
||||||
|
(inner_height + outer_height) - height
|
||||||
|
end
|
||||||
|
|
||||||
def outer_height
|
def outer_height
|
||||||
@style.margin_top + height + @style.margin_bottom
|
@style.margin_top + height + @style.margin_bottom
|
||||||
end
|
end
|
||||||
@@ -196,7 +212,7 @@ module CyberarmEngine
|
|||||||
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||||
if size && size.is_a?(Numeric)
|
if size && size.is_a?(Numeric)
|
||||||
if size.between?(0.0, 1.0)
|
if size.between?(0.0, 1.0)
|
||||||
@parent.send(:"#{dimension}") * size
|
((@parent.send(:"content_#{dimension}") - self.send(:"noncontent_#{dimension}") - 1) * size).round
|
||||||
else
|
else
|
||||||
size
|
size
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -31,6 +31,20 @@ module CyberarmEngine
|
|||||||
recalculate
|
recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def clear(&block)
|
||||||
|
@children.clear
|
||||||
|
|
||||||
|
old_container = $__current_container__
|
||||||
|
|
||||||
|
$__current_container__ = self
|
||||||
|
block.call(self) if block
|
||||||
|
|
||||||
|
$__current_container__ = old_container
|
||||||
|
|
||||||
|
recalculate
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
Gosu.clip_to(@x, @y, width, height) do
|
Gosu.clip_to(@x, @y, width, height) do
|
||||||
@children.each(&:draw)
|
@children.each(&:draw)
|
||||||
@@ -159,4 +173,4 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,16 +16,31 @@ module CyberarmEngine
|
|||||||
@text_input = Gosu::TextInput.new
|
@text_input = Gosu::TextInput.new
|
||||||
@text_input.text = text
|
@text_input.text = text
|
||||||
|
|
||||||
|
@offset_x = 0
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
|
Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
|
||||||
draw_text
|
Gosu.translate(-@offset_x, 0) do
|
||||||
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
|
draw_selection
|
||||||
|
draw_caret if @focus && @show_caret
|
||||||
|
draw_text
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def draw_caret
|
||||||
|
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_selection
|
||||||
|
selection_width = caret_position - selection_start_position
|
||||||
|
|
||||||
|
Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.height, default(:selection_color), @z)
|
||||||
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
if @type == :password
|
if @type == :password
|
||||||
@text.text = default(:password_character) * @text_input.text.length
|
@text.text = default(:password_character) * @text_input.text.length
|
||||||
@@ -38,6 +53,51 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@show_caret = !@show_caret
|
@show_caret = !@show_caret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
keep_caret_visible
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_caret_to_mouse(mouse_x)
|
||||||
|
1.upto(@text.text.length) do |i|
|
||||||
|
if mouse_x < @text.x + @text.textobject.text_width(@text.text[0...i])
|
||||||
|
@text_input.caret_pos = @text_input.selection_start = i - 1;
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
|
||||||
|
end
|
||||||
|
|
||||||
|
def keep_caret_visible
|
||||||
|
caret_pos = (caret_position - @text.x) + @caret_width
|
||||||
|
|
||||||
|
@last_text ||= "/\\"
|
||||||
|
@last_pos ||= -1
|
||||||
|
|
||||||
|
puts "caret pos: #{caret_pos}, width: #{@width}, offset: #{@offset_x}" if (@last_text != @text.text) || (@last_pos != caret_pos)
|
||||||
|
|
||||||
|
@last_text = @text.text
|
||||||
|
@last_pos = caret_pos
|
||||||
|
|
||||||
|
|
||||||
|
if caret_pos.between?(@offset_x, @width + @offset_x)
|
||||||
|
# Do nothing
|
||||||
|
|
||||||
|
elsif caret_pos < @offset_x
|
||||||
|
if caret_pos > @width
|
||||||
|
@offset_x = caret_pos + @width
|
||||||
|
else
|
||||||
|
@offset_x = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
elsif caret_pos > @width
|
||||||
|
@offset_x = caret_pos - @width
|
||||||
|
puts "triggered"
|
||||||
|
|
||||||
|
else
|
||||||
|
# Reset to Zero
|
||||||
|
@offset_x = 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
def left_mouse_button(sender, x, y)
|
||||||
@@ -47,6 +107,8 @@ module CyberarmEngine
|
|||||||
@caret_last_interval = Gosu.milliseconds
|
@caret_last_interval = Gosu.milliseconds
|
||||||
@show_caret = true
|
@show_caret = true
|
||||||
|
|
||||||
|
move_caret_to_mouse(x)
|
||||||
|
|
||||||
return :handled
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -79,12 +141,19 @@ module CyberarmEngine
|
|||||||
return :handled
|
return :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
|
||||||
def caret_position
|
def caret_position
|
||||||
|
text_input_position_for(:caret_pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
def selection_start_position
|
||||||
|
text_input_position_for(:selection_start)
|
||||||
|
end
|
||||||
|
|
||||||
|
def text_input_position_for(method)
|
||||||
if @type == :password
|
if @type == :password
|
||||||
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
|
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
|
||||||
else
|
else
|
||||||
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
|
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.send(method)])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ module CyberarmEngine
|
|||||||
return :handled if handler.call(self, *args) == :handled
|
return :handled if handler.call(self, *args) == :handled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
parent.publish(event, *args) if parent
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@root_container = Element::Stack.new(gui_state: self)
|
@root_container = Element::Stack.new(gui_state: self)
|
||||||
@game_objects << @root_container
|
@game_objects << @root_container
|
||||||
@containers = [@root_container]
|
$__current_container__ = @root_container
|
||||||
|
|
||||||
@active_width = window.width
|
@active_width = window.width
|
||||||
@active_height = window.height
|
@active_height = window.height
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ module CyberarmEngine
|
|||||||
caret_width: 2,
|
caret_width: 2,
|
||||||
caret_color: Gosu::Color::WHITE,
|
caret_color: Gosu::Color::WHITE,
|
||||||
caret_interval: 500,
|
caret_interval: 500,
|
||||||
|
selection_color: Gosu::Color::GREEN,
|
||||||
},
|
},
|
||||||
|
|
||||||
Image: { # < Element
|
Image: { # < Element
|
||||||
@@ -111,7 +112,7 @@ module CyberarmEngine
|
|||||||
height: 36,
|
height: 36,
|
||||||
background: 0xff111111,
|
background: 0xff111111,
|
||||||
fraction_background: [0xffc75e61, 0xffe26623],
|
fraction_background: [0xffc75e61, 0xffe26623],
|
||||||
border_thickness: 4,
|
border_thickness: 1,
|
||||||
border_color: [0xffd59674, 0xffff8746]
|
border_color: [0xffd59674, 0xffff8746]
|
||||||
}
|
}
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ module CyberarmEngine
|
|||||||
def weight; @weight; end
|
def weight; @weight; end
|
||||||
def weight=(n); @weight = n; end
|
def weight=(n); @weight = n; end
|
||||||
|
|
||||||
|
alias w weight
|
||||||
|
alias w= weight=
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@x == other &&
|
@x == other &&
|
||||||
@@ -54,6 +57,10 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def xy
|
||||||
|
Vector.new(@x, @y)
|
||||||
|
end
|
||||||
|
|
||||||
# Performs math operation, excluding @weight
|
# Performs math operation, excluding @weight
|
||||||
private def operator(function, other)
|
private def operator(function, other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev"
|
NAME = "InDev"
|
||||||
VERSION = "0.11.1"
|
VERSION = "0.13.0"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user