mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-17 21:42:34 +00:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d8551c7428 | |||
| 0017c841dd | |||
| 8cedb40283 | |||
| 93a4e9a2b8 | |||
| a0c0180411 | |||
| 7f25cd49fe | |||
| 7e6a17fe9f | |||
| 0ea1e5c2ff | |||
| 8c34293307 | |||
| 8172dafa8c | |||
| 9a3caabc7e | |||
| 4b25d724b5 | |||
| a267695b1c | |||
| d76da62ee1 | |||
| 3a83e616d3 | |||
| 52940d1cfe | |||
| e59771e412 | |||
| b382bf6960 | |||
| a5ed650970 | |||
| 935d6e9178 | |||
| cc813c023f | |||
| 5f359a8313 | |||
| c67d6f9d26 | |||
| 7f97ec85dd | |||
| 89a54e90a1 | |||
| 48d2924c9b | |||
| 7cc733dee4 | |||
| 28c4acdedf | |||
| 57313a33a6 | |||
| 5d16500edd | |||
| 51dd3803fa | |||
| c6beab3e99 | |||
| 8ce594753e | |||
| ae44083cf2 | |||
| b25f76e124 | |||
| 8f9e671340 | |||
| bb482f4463 | |||
| 788d987da1 | |||
| 6fafe98008 | |||
| 002a01abfc | |||
| 6f7bac3880 | |||
| 993e59aa94 | |||
| e30ed7c6be | |||
| a172c24d97 | |||
| 003715f685 | |||
| 1458736f7d | |||
| a2e0c07c6a | |||
| 3ead2f5daf | |||
| 8f3d9ff193 | |||
| bed78e7dc8 | |||
| bdce85613b | |||
| b3bfa0d654 | |||
| a972cfac98 | |||
| 2fe8e6042b | |||
| f68a8383af | |||
| 72df060059 | |||
| e95b4c05e2 | |||
| 4642056576 | |||
| 3d2402b7f7 |
36
README.md
36
README.md
@@ -1,8 +1,13 @@
|
|||||||
# CyberarmEngine
|
# CyberarmEngine
|
||||||
|
|
||||||
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cyberarm_engine`. To experiment with that code, run `bin/console` for an interactive prompt.
|
Yet Another Game Engine On Top Of Gosu
|
||||||
|
|
||||||
TODO: Delete this and the text above, and describe your gem
|
## Features
|
||||||
|
* [Shoes-like](http://shoesrb.com) GUI support
|
||||||
|
* OpenGL Shader support (requires [opengl-bindings](https://github.com/vaiorabbit/ruby-opengl) gem)
|
||||||
|
* Includes classes for handling Vectors, Rays, Bounding Boxes, and Transforms
|
||||||
|
* GameState system
|
||||||
|
* Monolithic GameObjects
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -22,7 +27,32 @@ Or install it yourself as:
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
TODO: Write usage instructions here
|
```ruby
|
||||||
|
require "cyberarm_engine"
|
||||||
|
|
||||||
|
class Hello < CyberarmEngine::GuiState
|
||||||
|
def setup
|
||||||
|
stack do
|
||||||
|
label "Hello World!"
|
||||||
|
|
||||||
|
button "close" do
|
||||||
|
window.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Window < CyberarmEngine::Engine
|
||||||
|
def initialize
|
||||||
|
super
|
||||||
|
self.show_cursor = true
|
||||||
|
|
||||||
|
push_state(Hello)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Window.new.show
|
||||||
|
```
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -1,35 +1,49 @@
|
|||||||
|
begin
|
||||||
|
require File.expand_path("../../ffi-gosu/lib/gosu", File.dirname(__FILE__))
|
||||||
|
rescue LoadError => e
|
||||||
|
pp e
|
||||||
require "gosu"
|
require "gosu"
|
||||||
|
end
|
||||||
|
require "json"
|
||||||
|
|
||||||
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/transform"
|
||||||
|
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/config_file"
|
||||||
|
|
||||||
|
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"
|
||||||
require_relative "cyberarm_engine/ui/style"
|
require_relative "cyberarm_engine/ui/style"
|
||||||
require_relative "cyberarm_engine/ui/border_canvas"
|
require_relative "cyberarm_engine/ui/border_canvas"
|
||||||
require_relative "cyberarm_engine/ui/element"
|
require_relative "cyberarm_engine/ui/element"
|
||||||
require_relative "cyberarm_engine/ui/label"
|
require_relative "cyberarm_engine/ui/elements/label"
|
||||||
require_relative "cyberarm_engine/ui/button"
|
require_relative "cyberarm_engine/ui/elements/button"
|
||||||
require_relative "cyberarm_engine/ui/toggle_button"
|
require_relative "cyberarm_engine/ui/elements/toggle_button"
|
||||||
require_relative "cyberarm_engine/ui/edit_line"
|
require_relative "cyberarm_engine/ui/elements/edit_line"
|
||||||
require_relative "cyberarm_engine/ui/image"
|
require_relative "cyberarm_engine/ui/elements/image"
|
||||||
require_relative "cyberarm_engine/ui/container"
|
require_relative "cyberarm_engine/ui/elements/container"
|
||||||
|
require_relative "cyberarm_engine/ui/elements/flow"
|
||||||
require_relative "cyberarm_engine/ui/flow"
|
require_relative "cyberarm_engine/ui/elements/stack"
|
||||||
require_relative "cyberarm_engine/ui/stack"
|
require_relative "cyberarm_engine/ui/elements/check_box"
|
||||||
require_relative "cyberarm_engine/ui/check_box"
|
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
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
require "gosu"
|
|
||||||
|
|
||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Background
|
class Background
|
||||||
attr_accessor :x, :y, :z, :width, :height, :angle, :debug
|
attr_accessor :x, :y, :z, :width, :height, :angle, :debug
|
||||||
@@ -158,9 +156,20 @@ module CyberarmEngine
|
|||||||
@top_right = background[:top_right]
|
@top_right = background[:top_right]
|
||||||
@bottom_left = background[:bottom_left]
|
@bottom_left = background[:bottom_left]
|
||||||
@bottom_right = background[:bottom_right]
|
@bottom_right = background[:bottom_right]
|
||||||
|
elsif background.is_a?(Range)
|
||||||
|
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}' 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
|
||||||
|
module Gosu
|
||||||
|
class Color
|
||||||
|
def <=>(other)
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
@@ -49,11 +49,17 @@ module CyberarmEngine
|
|||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns whether both bounding boxes intersect
|
# returns whether bounding box intersects other
|
||||||
def intersect?(other)
|
def intersect?(other)
|
||||||
|
if other.is_a?(Ray)
|
||||||
|
other.intersect?(self)
|
||||||
|
elsif other.is_a?(BoundingBox)
|
||||||
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
||||||
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
||||||
(@min.z <= other.max.z && @max.z >= other.min.z)
|
(@min.z <= other.max.z && @max.z >= other.min.z)
|
||||||
|
else
|
||||||
|
raise "Unknown collider: #{other.class}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# does this bounding box envelop other bounding box? (inclusive of border)
|
# does this bounding box envelop other bounding box? (inclusive of border)
|
||||||
@@ -62,11 +68,17 @@ module CyberarmEngine
|
|||||||
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
|
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns whether the vector is inside of the bounding box
|
# returns whether the 3D vector is inside of the bounding box
|
||||||
|
def inside?(vector)
|
||||||
|
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
|
||||||
|
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y)) &&
|
||||||
|
(vector.z.between?(@min.z, @max.z) || vector.z.between?(@max.z, @min.z))
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns whether the 2D vector is inside of the bounding box
|
||||||
def point?(vector)
|
def point?(vector)
|
||||||
vector.x.between?(@min.x, @max.x) &&
|
(vector.x.between?(@min.x, @max.x) || vector.x.between?(@max.x, @min.x)) &&
|
||||||
vector.y.between?(@min.y, @max.y) &&
|
(vector.y.between?(@min.y, @max.y) || vector.y.between?(@max.y, @min.y))
|
||||||
vector.z.between?(@min.z, @max.z)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def volume
|
def volume
|
||||||
@@ -87,26 +99,26 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def normalize(entity)
|
def normalize(entity)
|
||||||
temp = BoundingBox.new
|
temp = BoundingBox.new
|
||||||
temp.min.x = @min.x.to_f * entity.scale
|
temp.min.x = @min.x.to_f * entity.scale.x
|
||||||
temp.min.y = @min.y.to_f * entity.scale
|
temp.min.y = @min.y.to_f * entity.scale.y
|
||||||
temp.min.z = @min.z.to_f * entity.scale
|
temp.min.z = @min.z.to_f * entity.scale.z
|
||||||
|
|
||||||
temp.max.x = @max.x.to_f * entity.scale
|
temp.max.x = @max.x.to_f * entity.scale.x
|
||||||
temp.max.y = @max.y.to_f * entity.scale
|
temp.max.y = @max.y.to_f * entity.scale.y
|
||||||
temp.max.z = @max.z.to_f * entity.scale
|
temp.max.z = @max.z.to_f * entity.scale.z
|
||||||
|
|
||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_with_offset(entity)
|
def normalize_with_offset(entity)
|
||||||
temp = BoundingBox.new
|
temp = BoundingBox.new
|
||||||
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
|
temp.min.x = @min.x.to_f * entity.scale.x + entity.position.x
|
||||||
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
|
temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y
|
||||||
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
|
temp.min.z = @min.z.to_f * entity.scale.z + entity.position.z
|
||||||
|
|
||||||
temp.max.x = @max.x.to_f * entity.scale + entity.position.x
|
temp.max.x = @max.x.to_f * entity.scale.x + entity.position.x
|
||||||
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
|
temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y
|
||||||
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
|
temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z
|
||||||
|
|
||||||
return temp
|
return temp
|
||||||
end
|
end
|
||||||
@@ -1,35 +1,35 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
module Common
|
module Common
|
||||||
def push_state(klass, options={})
|
def push_state(klass, options={})
|
||||||
$window.push_state(klass, options)
|
window.push_state(klass, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_state
|
def current_state
|
||||||
$window.current_state
|
window.current_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def previous_state
|
def previous_state
|
||||||
$window.previous_state
|
window.previous_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop_state
|
def pop_state
|
||||||
$window.pop_state
|
window.pop_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_cursor
|
def show_cursor
|
||||||
$window.show_cursor
|
window.show_cursor
|
||||||
end
|
end
|
||||||
|
|
||||||
def show_cursor=boolean
|
def show_cursor=boolean
|
||||||
$window.show_cursor = boolean
|
window.show_cursor = boolean
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw_rect(x, y, width, height, color, z = 0)
|
def draw_rect(x, y, width, height, color, z = 0)
|
||||||
$window.draw_rect(x,y,width,height,color,z)
|
Gosu.draw_rect(x, y, width, height, color, z)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill(color, z = 0)
|
def fill(color, z = 0)
|
||||||
draw_rect(0, 0, $window.width, $window.height, color, z)
|
draw_rect(0, 0, window.width, window.height, color, z)
|
||||||
end
|
end
|
||||||
|
|
||||||
def lighten(color, amount = 25)
|
def lighten(color, amount = 25)
|
||||||
@@ -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 = nil
|
||||||
|
if klass == Gosu::Image
|
||||||
|
instance = klass.new(path, retro: retro, tileable: tileable)
|
||||||
|
else
|
||||||
instance = klass.new(path)
|
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)
|
||||||
|
|||||||
46
lib/cyberarm_engine/config_file.rb
Normal file
46
lib/cyberarm_engine/config_file.rb
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class ConfigFile
|
||||||
|
def initialize(file:)
|
||||||
|
@file = file
|
||||||
|
|
||||||
|
if File.exist?(@file)
|
||||||
|
deserialize
|
||||||
|
else
|
||||||
|
@data = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def []= *keys, value
|
||||||
|
last_key = keys.last
|
||||||
|
|
||||||
|
if keys.size == 1
|
||||||
|
hash = @data
|
||||||
|
else
|
||||||
|
keys.pop
|
||||||
|
hash = @data[keys.shift] ||= {}
|
||||||
|
|
||||||
|
keys.each do |key|
|
||||||
|
hash[key] ||= {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
hash[last_key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(*keys)
|
||||||
|
@data.dig(*keys)
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialize
|
||||||
|
JSON.dump(@data)
|
||||||
|
end
|
||||||
|
|
||||||
|
def deserialize
|
||||||
|
@data = JSON.parse(File.read(@file), symbolize_names: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def save!
|
||||||
|
File.open(@file, "w") { |f| f.write(serialize) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -5,7 +5,7 @@ module CyberarmEngine
|
|||||||
SONGS = {}
|
SONGS = {}
|
||||||
|
|
||||||
attr_accessor :show_cursor
|
attr_accessor :show_cursor
|
||||||
attr_reader :current_state, :last_frame_time
|
attr_reader :last_frame_time
|
||||||
|
|
||||||
def self.now
|
def self.now
|
||||||
Gosu.milliseconds
|
Gosu.milliseconds
|
||||||
@@ -58,11 +58,15 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def push_state(klass, options={})
|
def push_state(klass, options={})
|
||||||
|
options = {setup: true}.merge(options)
|
||||||
|
|
||||||
if klass.instance_of?(klass.class) && defined?(klass.options)
|
if klass.instance_of?(klass.class) && defined?(klass.options)
|
||||||
@states << klass
|
@states << klass
|
||||||
|
klass.setup if options[:setup]
|
||||||
else
|
else
|
||||||
@states << klass.new(options) if child_of?(klass, GameState)
|
@states << klass.new(options) if child_of?(klass, GameState)
|
||||||
@states << klass.new if child_of?(klass, Container)
|
@states << klass.new if child_of?(klass, Element::Container)
|
||||||
|
current_state.setup if current_state.class == klass && options[:setup]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
|
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
|
||||||
:color, :alpha, :mode, :options, :paused, :radius, :last_position
|
:color, :mode, :options, :paused, :radius, :last_position
|
||||||
|
attr_reader :alpha
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
if options[:auto_manage] || options[:auto_manage] == nil
|
if options[:auto_manage] || options[:auto_manage] == nil
|
||||||
$window.current_state.add_game_object(self)
|
$window.current_state.add_game_object(self)
|
||||||
@@ -141,7 +142,7 @@ module CyberarmEngine
|
|||||||
self.angle%=360
|
self.angle%=360
|
||||||
end
|
end
|
||||||
|
|
||||||
def alpha=int # 0-255
|
def alpha=(int) # 0-255
|
||||||
@alpha = int
|
@alpha = int
|
||||||
@alpha = 255 if @alpha > 255
|
@alpha = 255 if @alpha > 255
|
||||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
||||||
@@ -154,7 +155,7 @@ module CyberarmEngine
|
|||||||
def button_up(id)
|
def button_up(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down?(id)
|
def button_down(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_closest(game_object_class)
|
def find_closest(game_object_class)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :options, :global_pause
|
attr_accessor :options, :global_pause
|
||||||
attr_reader :game_objects, :containers
|
attr_reader :game_objects
|
||||||
|
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
@options = options
|
@options = options
|
||||||
@@ -12,8 +12,6 @@ module CyberarmEngine
|
|||||||
$window.text_input = nil unless options[:preserve_text_input]
|
$window.text_input = nil unless options[:preserve_text_input]
|
||||||
|
|
||||||
@down_keys = {}
|
@down_keys = {}
|
||||||
|
|
||||||
setup
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
|||||||
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
|
||||||
56
lib/cyberarm_engine/ray.rb
Normal file
56
lib/cyberarm_engine/ray.rb
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Ray
|
||||||
|
def initialize(origin, direction, range = Float::INFINITY)
|
||||||
|
raise "Origin must be a Vector!" unless origin.is_a?(Vector)
|
||||||
|
raise "Direction must be a Vector!" unless direction.is_a?(Vector)
|
||||||
|
|
||||||
|
@origin = origin
|
||||||
|
@direction = direction
|
||||||
|
@range = range
|
||||||
|
|
||||||
|
@inverse_direction = @direction.inverse
|
||||||
|
end
|
||||||
|
|
||||||
|
def intersect?(intersectable)
|
||||||
|
if intersectable.is_a?(BoundingBox)
|
||||||
|
intersect_bounding_box?(intersectable)
|
||||||
|
else
|
||||||
|
raise NotImplementedError, "Ray intersection test for #{intersectable.class} not implemented."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Based on: https://tavianator.com/fast-branchless-raybounding-box-intersections/
|
||||||
|
def intersect_bounding_box?(box)
|
||||||
|
tmin = -@range
|
||||||
|
tmax = @range
|
||||||
|
|
||||||
|
tx1 = (box.min.x - @origin.x) * @inverse_direction.x
|
||||||
|
tx2 = (box.max.x - @origin.x) * @inverse_direction.x
|
||||||
|
|
||||||
|
tmin = max(tmin, min(tx1, tx2))
|
||||||
|
tmax = min(tmax, max(tx1, tx2))
|
||||||
|
|
||||||
|
ty1 = (box.min.y - @origin.y) * @inverse_direction.y
|
||||||
|
ty2 = (box.max.y - @origin.y) * @inverse_direction.y
|
||||||
|
|
||||||
|
tmin = max(tmin, min(ty1, ty2))
|
||||||
|
tmax = min(tmax, max(ty1, ty2))
|
||||||
|
|
||||||
|
tz1 = (box.min.z - @origin.z) * @inverse_direction.z
|
||||||
|
tz2 = (box.max.z - @origin.z) * @inverse_direction.z
|
||||||
|
|
||||||
|
tmin = max(tmin, min(tz1, tz2))
|
||||||
|
tmax = min(tmax, max(tz1, tz2))
|
||||||
|
|
||||||
|
return tmax >= max(tmin, 0.0);
|
||||||
|
end
|
||||||
|
|
||||||
|
def min(x, y)
|
||||||
|
((x) < (y) ? (x) : (y))
|
||||||
|
end
|
||||||
|
|
||||||
|
def max(x, y)
|
||||||
|
((x) > (y) ? (x) : (y))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
398
lib/cyberarm_engine/shader.rb
Normal file
398
lib/cyberarm_engine/shader.rb
Normal file
@@ -0,0 +1,398 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||||
|
class Shader
|
||||||
|
include OpenGL
|
||||||
|
@@shaders = {} # Cache for {Shader} instances
|
||||||
|
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
||||||
|
|
||||||
|
# add instance of {Shader} to cache
|
||||||
|
#
|
||||||
|
# @param name [String]
|
||||||
|
# @param instance [Shader]
|
||||||
|
def self.add(name, instance)
|
||||||
|
@@shaders[name] = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
# removes {Shader} from cache and cleans up
|
||||||
|
#
|
||||||
|
# @param name [String]
|
||||||
|
def self.delete(name)
|
||||||
|
shader = @@shaders.dig(name)
|
||||||
|
|
||||||
|
if shader
|
||||||
|
@@shaders.delete(name)
|
||||||
|
|
||||||
|
if shader.compiled?
|
||||||
|
glDeleteProgram(shader.program)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# runs _block_ using {Shader} with _name_
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
#
|
||||||
|
# CyberarmEngine::Shader.use("blur") do |shader|
|
||||||
|
# shader.uniform_float("radius", 20.0)
|
||||||
|
# # OpenGL Code that uses shader
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# @param name [String] name of {Shader} to use
|
||||||
|
# @return [void]
|
||||||
|
def self.use(name, &block)
|
||||||
|
shader = @@shaders.dig(name)
|
||||||
|
if shader
|
||||||
|
shader.use(&block)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Shader '#{name}' not found!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns whether {Shader} with _name_ is in cache
|
||||||
|
#
|
||||||
|
# @param name [String]
|
||||||
|
# @return [Boolean]
|
||||||
|
def self.available?(name)
|
||||||
|
@@shaders.dig(name).is_a?(Shader)
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns instance of {Shader}, if it exists
|
||||||
|
#
|
||||||
|
# @param name [String]
|
||||||
|
# @return [Shader?]
|
||||||
|
def self.get(name)
|
||||||
|
@@shaders.dig(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns currently active {Shader}, if one is active
|
||||||
|
#
|
||||||
|
# @return [Shader?]
|
||||||
|
def self.active_shader
|
||||||
|
@active_shader
|
||||||
|
end
|
||||||
|
|
||||||
|
# sets currently active {Shader}
|
||||||
|
#
|
||||||
|
# @param instance [Shader] instance of {Shader} to set as active
|
||||||
|
def self.active_shader=(instance)
|
||||||
|
@active_shader = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
# stops using currently active {Shader}
|
||||||
|
def self.stop
|
||||||
|
shader = Shader.active_shader
|
||||||
|
|
||||||
|
if shader
|
||||||
|
shader.stop
|
||||||
|
else
|
||||||
|
raise ArgumentError, "No active shader to stop!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns location of OpenGL Shader uniform
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
def self.attribute_location(variable)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.attribute_location(variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
# sets _variable_ to _value_
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value
|
||||||
|
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 * 8
|
||||||
|
@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
|
||||||
|
|
||||||
|
@data[:shaders].each { |key, id| glDeleteShader(id) }
|
||||||
|
|
||||||
|
# Only add shader if it successfully compiles
|
||||||
|
if @compiled
|
||||||
|
puts "compiled!"
|
||||||
|
puts "Compiled shader: #{@name}"
|
||||||
|
Shader.add(@name, self)
|
||||||
|
else
|
||||||
|
glDeleteProgram(@program)
|
||||||
|
warn "FAILED to compile shader: #{@name}", ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# whether vertex and fragment files exist on disk
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
def shader_files_exist?(vertex:, fragment:)
|
||||||
|
File.exist?(vertex) && File.exist?(fragment)
|
||||||
|
end
|
||||||
|
|
||||||
|
# creates an OpenGL Shader of _type_ using _source_
|
||||||
|
#
|
||||||
|
# @param type [Symbol] valid values are: :vertex, :fragment
|
||||||
|
# @param source [String] source code for shader
|
||||||
|
def create_shader(type:, source:)
|
||||||
|
_shader = nil
|
||||||
|
|
||||||
|
case type
|
||||||
|
when :vertex
|
||||||
|
_shader = glCreateShader(GL_VERTEX_SHADER)
|
||||||
|
when :fragment
|
||||||
|
_shader = glCreateShader(GL_FRAGMENT_SHADER)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "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
|
||||||
|
|
||||||
|
# evaluates shader preprocessors
|
||||||
|
#
|
||||||
|
# currently supported preprocessors:
|
||||||
|
#
|
||||||
|
# @include "file/path" "another/file/path" # => Replace line with contents of file; Shader includes_dir must be specified in constructor
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# # Example Vertex Shader #
|
||||||
|
# # #version 330 core
|
||||||
|
# # @include "material_struct"
|
||||||
|
# # void main() {
|
||||||
|
# # gl_Position = vec4(1, 1, 1, 1);
|
||||||
|
# # }
|
||||||
|
#
|
||||||
|
# Shader.new(name: "model_renderer", includes_dir: "path/to/includes", vertex: "path/to/vertex_shader.glsl")
|
||||||
|
#
|
||||||
|
# @param source shader source code
|
||||||
|
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
|
||||||
|
|
||||||
|
# compile OpenGL Shader of _type_
|
||||||
|
#
|
||||||
|
# @return [Boolean] whether compilation succeeded
|
||||||
|
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
|
||||||
|
|
||||||
|
# link compiled OpenGL Shaders in to a OpenGL Program
|
||||||
|
#
|
||||||
|
# @note linking must succeed or shader cannot be used
|
||||||
|
#
|
||||||
|
# @return [Boolean] whether linking succeeded
|
||||||
|
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_
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @return [Integer] location of uniform
|
||||||
|
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
|
||||||
|
|
||||||
|
# @see Shader.use Shader.use
|
||||||
|
def use(&block)
|
||||||
|
return unless compiled?
|
||||||
|
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||||
|
Shader.active_shader=self
|
||||||
|
|
||||||
|
glUseProgram(@program)
|
||||||
|
|
||||||
|
if block
|
||||||
|
block.call(self)
|
||||||
|
stop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# stop using shader, if shader is active
|
||||||
|
def stop
|
||||||
|
Shader.active_shader = nil if Shader.active_shader == self
|
||||||
|
glUseProgram(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Boolean] whether {Shader} successfully compiled
|
||||||
|
def compiled?
|
||||||
|
@compiled
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns location of a uniform _variable_
|
||||||
|
#
|
||||||
|
# @note Use {#variable} for friendly error handling
|
||||||
|
# @see #variable Shader#variable
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @return [Integer]
|
||||||
|
def attribute_location(variable)
|
||||||
|
glGetUniformLocation(@program, variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
# send {Transform} to {Shader}
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Transform]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
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
|
||||||
|
|
||||||
|
# send Boolean to {Shader}
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Boolean]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
def uniform_boolean(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1i(attr_loc, value ? 1 : 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
# send Integer to {Shader}
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Integer]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
def uniform_integer(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1i(attr_loc, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# send Float to {Shader}
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Float]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
def uniform_float(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform1f(attr_loc, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# send {Vector} (x, y, z) to {Shader}
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Vector]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
def uniform_vec3(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform3f(attr_loc, *value.to_a[0..2])
|
||||||
|
end
|
||||||
|
|
||||||
|
# send {Vector} to {Shader}
|
||||||
|
#
|
||||||
|
# @param variable [String]
|
||||||
|
# @param value [Vector]
|
||||||
|
# @param location [Integer]
|
||||||
|
# @return [void]
|
||||||
|
def uniform_vec4(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
glUniform4f(attr_loc, *value.to_a)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,8 +2,8 @@ module CyberarmEngine
|
|||||||
class Text
|
class Text
|
||||||
CACHE = {}
|
CACHE = {}
|
||||||
|
|
||||||
attr_accessor :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
attr_accessor :x, :y, :z, :size, :options
|
||||||
attr_reader :text, :textobject
|
attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||||
|
|
||||||
def initialize(text, options={})
|
def initialize(text, options={})
|
||||||
@text = text.to_s || ""
|
@text = text.to_s || ""
|
||||||
@@ -22,6 +22,8 @@ module CyberarmEngine
|
|||||||
@shadow = true if options[:shadow] == nil
|
@shadow = true if options[:shadow] == nil
|
||||||
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
||||||
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||||
|
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
||||||
|
@shadow_color= options[:shadow_color]
|
||||||
@textobject = check_cache(@size, @font)
|
@textobject = check_cache(@size, @font)
|
||||||
|
|
||||||
if @alignment
|
if @alignment
|
||||||
@@ -67,6 +69,35 @@ module CyberarmEngine
|
|||||||
@text = string
|
@text = string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def factor_x=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@factor_x = n
|
||||||
|
end
|
||||||
|
def factor_y=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@factor_y = n
|
||||||
|
end
|
||||||
|
def color=(color)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@color = color
|
||||||
|
end
|
||||||
|
def shadow=(boolean)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow = boolean
|
||||||
|
end
|
||||||
|
def shadow_size=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_size = n
|
||||||
|
end
|
||||||
|
def shadow_alpha=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_alpha = n
|
||||||
|
end
|
||||||
|
def shadow_color=(n)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@shadow_color = n
|
||||||
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
textobject.text_width(@text)
|
textobject.text_width(@text)
|
||||||
end
|
end
|
||||||
@@ -77,9 +108,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def draw
|
def draw
|
||||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||||
@shadow_alpha = 30 if @color.alpha > 30
|
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||||
@shadow_alpha = @color.alpha if @color.alpha <= 30
|
shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha)
|
||||||
shadow_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha)
|
|
||||||
|
|
||||||
_x = @shadow_size
|
_x = @shadow_size
|
||||||
_y = @shadow_size
|
_y = @shadow_size
|
||||||
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,53 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Button < Label
|
|
||||||
def initialize(text, options = {}, block = nil)
|
|
||||||
super(text, options, block)
|
|
||||||
|
|
||||||
@style.background_canvas.background = default(:background)
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
draw_text
|
|
||||||
end
|
|
||||||
|
|
||||||
def draw_text
|
|
||||||
@text.draw
|
|
||||||
end
|
|
||||||
|
|
||||||
def enter(sender)
|
|
||||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
|
||||||
|
|
||||||
if @focus
|
|
||||||
@style.background_canvas.background = default(:active, :background)
|
|
||||||
@text.color = default(:active, :color)
|
|
||||||
else
|
|
||||||
@style.background_canvas.background = default(:hover, :background)
|
|
||||||
@text.color = default(:hover, :color)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
|
||||||
@focus = true
|
|
||||||
@style.background_canvas.background = default(:active, :background)
|
|
||||||
window.current_state.focus = self
|
|
||||||
@text.color = default(:active, :color)
|
|
||||||
end
|
|
||||||
|
|
||||||
def released_left_mouse_button(sender,x, y)
|
|
||||||
enter(sender)
|
|
||||||
end
|
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
|
||||||
@block.call(self) if @block
|
|
||||||
end
|
|
||||||
|
|
||||||
def leave(sender)
|
|
||||||
@style.background_canvas.background = default(:background)
|
|
||||||
@text.color = default(:color)
|
|
||||||
end
|
|
||||||
|
|
||||||
def blur(sender)
|
|
||||||
@focus = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class CheckBox < Flow
|
|
||||||
def initialize(text, options, block = nil)
|
|
||||||
super({}, block = nil)
|
|
||||||
options[:toggled] = options[:checked]
|
|
||||||
|
|
||||||
@toggle_button = ToggleButton.new(options)
|
|
||||||
@label = Label.new(text, options)
|
|
||||||
|
|
||||||
define_label_singletons
|
|
||||||
|
|
||||||
add(@toggle_button)
|
|
||||||
add(@label)
|
|
||||||
|
|
||||||
@style.width = @toggle_button.width + @label.width
|
|
||||||
@style.height = @toggle_button.height + @label.height
|
|
||||||
end
|
|
||||||
|
|
||||||
def text=(text)
|
|
||||||
@label.text = text
|
|
||||||
recalculate
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@toggle_button.value
|
|
||||||
end
|
|
||||||
|
|
||||||
def value=(bool)
|
|
||||||
@toggle_button.vlaue = bool
|
|
||||||
end
|
|
||||||
|
|
||||||
def define_label_singletons
|
|
||||||
@label.define_singleton_method(:_toggle_button) do |button|
|
|
||||||
@_toggle_button = button
|
|
||||||
end
|
|
||||||
|
|
||||||
@label._toggle_button(@toggle_button)
|
|
||||||
|
|
||||||
@label.define_singleton_method(:holding_left_mouse_button) do |sender, x, y|
|
|
||||||
@_toggle_button.left_mouse_button(sender, x, y)
|
|
||||||
end
|
|
||||||
|
|
||||||
@label.define_singleton_method(:released_left_mouse_button) do |sender, x, y|
|
|
||||||
@_toggle_button.released_left_mouse_button(sender, x, y)
|
|
||||||
end
|
|
||||||
|
|
||||||
@label.define_singleton_method(:clicked_left_mouse_button) do |sender, x, y|
|
|
||||||
@_toggle_button.clicked_left_mouse_button(sender, x, y)
|
|
||||||
end
|
|
||||||
|
|
||||||
@label.define_singleton_method(:enter) do |sender|
|
|
||||||
@_toggle_button.enter(sender)
|
|
||||||
end
|
|
||||||
|
|
||||||
@label.define_singleton_method(:leave) do |sender|
|
|
||||||
@_toggle_button.leave(sender)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Container < Element
|
|
||||||
include Common
|
|
||||||
|
|
||||||
attr_accessor :stroke_color, :fill_color
|
|
||||||
attr_reader :children
|
|
||||||
attr_reader :scroll_x, :scroll_y
|
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
|
||||||
super
|
|
||||||
|
|
||||||
@scroll_x, @scroll_y = 0, 0
|
|
||||||
@scroll_speed = 10
|
|
||||||
|
|
||||||
@text_color = options[:color]
|
|
||||||
|
|
||||||
@children = []
|
|
||||||
end
|
|
||||||
|
|
||||||
def build
|
|
||||||
@block.call(self) if @block
|
|
||||||
|
|
||||||
recalculate
|
|
||||||
end
|
|
||||||
|
|
||||||
def add(element)
|
|
||||||
@children << element
|
|
||||||
|
|
||||||
recalculate
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
@children.each(&:draw)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update
|
|
||||||
@children.each(&:update)
|
|
||||||
end
|
|
||||||
|
|
||||||
def hit_element?(x, y)
|
|
||||||
@children.reverse_each do |child|
|
|
||||||
case child
|
|
||||||
when Container
|
|
||||||
if element = child.hit_element?(x, y)
|
|
||||||
return element
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return child if child.hit?(x, y)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self if hit?(x, y)
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
|
||||||
return unless visible?
|
|
||||||
stylize
|
|
||||||
|
|
||||||
layout
|
|
||||||
|
|
||||||
@style.width = @max_width ? @max_width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
|
|
||||||
@style.height = @max_height ? @max_height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
|
||||||
|
|
||||||
# Move child to parent after positioning
|
|
||||||
@children.each do |child|
|
|
||||||
child.x += @x
|
|
||||||
child.y += @y
|
|
||||||
|
|
||||||
child.stylize
|
|
||||||
child.recalculate
|
|
||||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
|
||||||
end
|
|
||||||
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def layout
|
|
||||||
raise "Not overridden"
|
|
||||||
end
|
|
||||||
|
|
||||||
def max_width
|
|
||||||
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
|
||||||
end
|
|
||||||
|
|
||||||
def fits_on_line?(element) # Flow
|
|
||||||
@current_position.x + element.outer_width <= max_width &&
|
|
||||||
@current_position.x + element.outer_width <= window.width
|
|
||||||
end
|
|
||||||
|
|
||||||
def position_on_current_line(element) # Flow
|
|
||||||
element.x = element.style.margin_left + @current_position.x
|
|
||||||
element.y = element.style.margin_top + @current_position.y
|
|
||||||
|
|
||||||
element.recalculate
|
|
||||||
|
|
||||||
@current_position.x += element.outer_width
|
|
||||||
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
|
||||||
end
|
|
||||||
|
|
||||||
def tallest_neighbor(querier, y_position) # Flow
|
|
||||||
response = querier
|
|
||||||
@children.each do |child|
|
|
||||||
response = child if child.outer_height > response.outer_height
|
|
||||||
break if child == querier
|
|
||||||
end
|
|
||||||
|
|
||||||
return response
|
|
||||||
end
|
|
||||||
|
|
||||||
def position_on_next_line(child) # Flow
|
|
||||||
@current_position.x = @style.margin_left
|
|
||||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
|
||||||
|
|
||||||
child.x = child.style.margin_left + @current_position.x
|
|
||||||
child.y = child.style.margin_top + @current_position.y
|
|
||||||
|
|
||||||
child.recalculate
|
|
||||||
|
|
||||||
@current_position.x += child.outer_width
|
|
||||||
end
|
|
||||||
|
|
||||||
def move_to_next_line(element) # Stack
|
|
||||||
element.x = element.style.margin_left + @current_position.x
|
|
||||||
element.y = element.style.margin_top + @current_position.y
|
|
||||||
|
|
||||||
element.recalculate
|
|
||||||
|
|
||||||
@current_position.y += element.outer_height
|
|
||||||
end
|
|
||||||
|
|
||||||
# def mouse_wheel_up(sender, x, y)
|
|
||||||
# @children.each {|c| c.y -= @scroll_speed}
|
|
||||||
# @children.each {|c| c.recalculate}
|
|
||||||
# end
|
|
||||||
|
|
||||||
# def mouse_wheel_down(sender, x, y)
|
|
||||||
# @children.each {|c| c.y += @scroll_speed}
|
|
||||||
# @children.each {|c| c.recalculate}
|
|
||||||
# end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@children.map {|c| c.class}.join(", ")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,93 +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 = Flow.new(options, block)
|
|
||||||
@containers << _container
|
|
||||||
_container.build
|
|
||||||
options[: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 = Stack.new(options, block)
|
|
||||||
@containers << _container
|
|
||||||
_container.build
|
|
||||||
options[: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 = 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 = 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 = 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 = 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 = 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 = Image.new(path, options, block)
|
|
||||||
@containers.last.add(_element)
|
|
||||||
|
|
||||||
return _element
|
add_element( Element::Image.new(path, options, block) )
|
||||||
|
end
|
||||||
|
|
||||||
|
def progress(options = {}, &block)
|
||||||
|
options[:parent] = element_parent
|
||||||
|
options[:theme] = current_theme
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class EditLine < Button
|
|
||||||
def initialize(text, options = {}, block = nil)
|
|
||||||
super(text, options, block)
|
|
||||||
|
|
||||||
@type = default(:type)
|
|
||||||
|
|
||||||
|
|
||||||
@caret_width = default(:caret_width)
|
|
||||||
@caret_height= @text.height
|
|
||||||
@caret_color = default(:caret_color)
|
|
||||||
@caret_interval = default(:caret_interval)
|
|
||||||
@caret_last_interval = Gosu.milliseconds
|
|
||||||
@show_caret = true
|
|
||||||
|
|
||||||
@text_input = Gosu::TextInput.new
|
|
||||||
@text_input.text = text
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
|
|
||||||
draw_text
|
|
||||||
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def update
|
|
||||||
if @type == :password
|
|
||||||
@text.text = default(:password_character) * @text_input.text.length
|
|
||||||
else
|
|
||||||
@text.text = @text_input.text
|
|
||||||
end
|
|
||||||
|
|
||||||
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
|
|
||||||
@caret_last_interval = Gosu.milliseconds
|
|
||||||
|
|
||||||
@show_caret = !@show_caret
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
|
||||||
super
|
|
||||||
window.text_input = @text_input
|
|
||||||
end
|
|
||||||
|
|
||||||
def enter(sender)
|
|
||||||
if @focus
|
|
||||||
@style.background_canvas.background = default(:active, :background)
|
|
||||||
@text.color = default(:active, :color)
|
|
||||||
else
|
|
||||||
@style.background_canvas.background = default(:hover, :background)
|
|
||||||
@text.color = default(:hover, :color)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def leave(sender)
|
|
||||||
unless @focus
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def blur(sender)
|
|
||||||
@focus = false
|
|
||||||
@style.background_canvas.background = default(:background)
|
|
||||||
@text.color = default(:color)
|
|
||||||
window.text_input = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
|
|
||||||
def caret_position
|
|
||||||
if @type == :password
|
|
||||||
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
|
|
||||||
else
|
|
||||||
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
super
|
|
||||||
|
|
||||||
@style.width = default(:width)
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@text_input.text
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -8,7 +8,7 @@ module CyberarmEngine
|
|||||||
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
def initialize(options = {}, block = nil)
|
||||||
@parent = options[:parent] # parent Container (i.e. flow/stack)
|
@parent = options.delete(:parent) # parent Container (i.e. flow/stack)
|
||||||
options = theme_defaults(options)
|
options = theme_defaults(options)
|
||||||
@options = options
|
@options = options
|
||||||
@block = block
|
@block = block
|
||||||
@@ -23,27 +23,30 @@ module CyberarmEngine
|
|||||||
@y = @style.y
|
@y = @style.y
|
||||||
@z = @style.z
|
@z = @style.z
|
||||||
|
|
||||||
|
@width = 0
|
||||||
|
@height = 0
|
||||||
|
|
||||||
@fixed_x = @x if @x != 0
|
@fixed_x = @x if @x != 0
|
||||||
@fixed_y = @y if @y != 0
|
@fixed_y = @y if @y != 0
|
||||||
|
|
||||||
|
@style.width = default(:width) || nil
|
||||||
|
@style.height = default(:height) || nil
|
||||||
|
|
||||||
|
@style.background_canvas = Background.new
|
||||||
|
@style.border_canvas = BorderCanvas.new(element: self)
|
||||||
|
|
||||||
stylize
|
stylize
|
||||||
|
|
||||||
default_events
|
default_events
|
||||||
end
|
end
|
||||||
|
|
||||||
def stylize
|
def stylize
|
||||||
@style.width = @style.width || $window.width
|
|
||||||
@style.height = @style.height || $window.height
|
|
||||||
|
|
||||||
set_border_thickness(@style.border_thickness)
|
set_border_thickness(@style.border_thickness)
|
||||||
|
|
||||||
set_padding(@style.padding)
|
set_padding(@style.padding)
|
||||||
|
|
||||||
set_margin(@style.margin)
|
set_margin(@style.margin)
|
||||||
|
|
||||||
@style.background_canvas = Background.new
|
|
||||||
@style.border_canvas = BorderCanvas.new(element: self)
|
|
||||||
|
|
||||||
set_background(@style.background)
|
set_background(@style.background)
|
||||||
set_border_color(@style.border_color)
|
set_border_color(@style.border_color)
|
||||||
end
|
end
|
||||||
@@ -119,17 +122,17 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def toggle
|
def toggle
|
||||||
@visible = !@visible
|
@visible = !@visible
|
||||||
root.recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@visible = true
|
@visible = true
|
||||||
root.recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide
|
def hide
|
||||||
@visible = false
|
@visible = false
|
||||||
root.recalculate
|
root.gui_state.request_recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@@ -159,28 +162,65 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def width
|
def width
|
||||||
if visible?
|
if visible?
|
||||||
(@style.border_thickness_left + @style.padding_left) + @style.width + (@style.padding_right + @style.border_thickness_right)
|
inner_width + @width
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
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
|
||||||
|
|
||||||
|
def inner_width
|
||||||
|
(@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right)
|
||||||
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
if visible?
|
if visible?
|
||||||
(@style.border_thickness_top + @style.padding_top) + @style.height + (@style.padding_bottom + @style.border_thickness_bottom)
|
inner_height + @height
|
||||||
else
|
else
|
||||||
0
|
0
|
||||||
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
|
||||||
|
|
||||||
|
def inner_height
|
||||||
|
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||||
|
end
|
||||||
|
|
||||||
|
private def dimensional_size(size, dimension)
|
||||||
|
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||||
|
if size && size.is_a?(Numeric)
|
||||||
|
if size.between?(0.0, 1.0)
|
||||||
|
((@parent.send(:"content_#{dimension}") - self.send(:"noncontent_#{dimension}") - 1) * size).round
|
||||||
|
else
|
||||||
|
size
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def background=(_background)
|
def background=(_background)
|
||||||
@style.background_canvas.background=(_background)
|
@style.background_canvas.background=(_background)
|
||||||
update_background
|
update_background
|
||||||
@@ -214,6 +254,10 @@ module CyberarmEngine
|
|||||||
@root
|
@root
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_root?
|
||||||
|
@gui_state != nil
|
||||||
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
raise "#{self.class}#recalculate was not overridden!"
|
raise "#{self.class}#recalculate was not overridden!"
|
||||||
end
|
end
|
||||||
|
|||||||
67
lib/cyberarm_engine/ui/elements/button.rb
Normal file
67
lib/cyberarm_engine/ui/elements/button.rb
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Button < Label
|
||||||
|
def initialize(text, options = {}, block = nil)
|
||||||
|
super(text, options, block)
|
||||||
|
|
||||||
|
@style.background_canvas.background = default(:background)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
draw_text
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_text
|
||||||
|
@text.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
def enter(sender)
|
||||||
|
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||||
|
|
||||||
|
if @focus
|
||||||
|
@style.background_canvas.background = default(:active, :background)
|
||||||
|
@text.color = default(:active, :color)
|
||||||
|
else
|
||||||
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
|
@text.color = default(:hover, :color)
|
||||||
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def left_mouse_button(sender, x, y)
|
||||||
|
@focus = true
|
||||||
|
@style.background_canvas.background = default(:active, :background)
|
||||||
|
window.current_state.focus = self
|
||||||
|
@text.color = default(:active, :color)
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def released_left_mouse_button(sender,x, y)
|
||||||
|
enter(sender)
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def leave(sender)
|
||||||
|
@style.background_canvas.background = default(:background)
|
||||||
|
@text.color = default(:color)
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def blur(sender)
|
||||||
|
@focus = false
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
59
lib/cyberarm_engine/ui/elements/check_box.rb
Normal file
59
lib/cyberarm_engine/ui/elements/check_box.rb
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class CheckBox < Flow
|
||||||
|
def initialize(text, options, block = nil)
|
||||||
|
super({}, block = nil)
|
||||||
|
options[:toggled] = options[:checked]
|
||||||
|
|
||||||
|
@toggle_button = ToggleButton.new(options)
|
||||||
|
@label = Label.new(text, options)
|
||||||
|
|
||||||
|
define_label_singletons
|
||||||
|
|
||||||
|
add(@toggle_button)
|
||||||
|
add(@label)
|
||||||
|
end
|
||||||
|
|
||||||
|
def text=(text)
|
||||||
|
@label.text = text
|
||||||
|
recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@toggle_button.value
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(bool)
|
||||||
|
@toggle_button.vlaue = bool
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_label_singletons
|
||||||
|
@label.define_singleton_method(:_toggle_button) do |button|
|
||||||
|
@_toggle_button = button
|
||||||
|
end
|
||||||
|
|
||||||
|
@label._toggle_button(@toggle_button)
|
||||||
|
|
||||||
|
@label.define_singleton_method(:holding_left_mouse_button) do |sender, x, y|
|
||||||
|
@_toggle_button.left_mouse_button(sender, x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
@label.define_singleton_method(:released_left_mouse_button) do |sender, x, y|
|
||||||
|
@_toggle_button.released_left_mouse_button(sender, x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
@label.define_singleton_method(:clicked_left_mouse_button) do |sender, x, y|
|
||||||
|
@_toggle_button.clicked_left_mouse_button(sender, x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
@label.define_singleton_method(:enter) do |sender|
|
||||||
|
@_toggle_button.enter(sender)
|
||||||
|
end
|
||||||
|
|
||||||
|
@label.define_singleton_method(:leave) do |sender|
|
||||||
|
@_toggle_button.leave(sender)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
176
lib/cyberarm_engine/ui/elements/container.rb
Normal file
176
lib/cyberarm_engine/ui/elements/container.rb
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Container < Element
|
||||||
|
include Common
|
||||||
|
|
||||||
|
attr_accessor :stroke_color, :fill_color
|
||||||
|
attr_reader :children, :gui_state
|
||||||
|
attr_reader :scroll_x, :scroll_y
|
||||||
|
|
||||||
|
def initialize(options = {}, block = nil)
|
||||||
|
@gui_state = options.delete(:gui_state)
|
||||||
|
super
|
||||||
|
|
||||||
|
@scroll_x, @scroll_y = 0, 0
|
||||||
|
@scroll_speed = 10
|
||||||
|
|
||||||
|
@text_color = options[:color]
|
||||||
|
|
||||||
|
@children = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def build
|
||||||
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def add(element)
|
||||||
|
@children << element
|
||||||
|
|
||||||
|
recalculate
|
||||||
|
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
|
||||||
|
Gosu.clip_to(@x, @y, width, height) do
|
||||||
|
@children.each(&:draw)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@children.each(&:update)
|
||||||
|
end
|
||||||
|
|
||||||
|
def hit_element?(x, y)
|
||||||
|
@children.reverse_each do |child|
|
||||||
|
case child
|
||||||
|
when Container
|
||||||
|
if element = child.hit_element?(x, y)
|
||||||
|
return element
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return child if child.hit?(x, y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self if hit?(x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||||
|
return unless visible?
|
||||||
|
stylize
|
||||||
|
|
||||||
|
layout
|
||||||
|
|
||||||
|
if is_root?
|
||||||
|
@width = @style.width = window.width
|
||||||
|
@height = @style.height = window.height
|
||||||
|
else
|
||||||
|
@width, @height = 0, 0
|
||||||
|
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
|
||||||
|
@width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
|
||||||
|
@height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# Move child to parent after positioning
|
||||||
|
@children.each do |child|
|
||||||
|
child.x += @x
|
||||||
|
child.y += @y
|
||||||
|
|
||||||
|
child.stylize
|
||||||
|
child.recalculate
|
||||||
|
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||||
|
end
|
||||||
|
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def layout
|
||||||
|
raise "Not overridden"
|
||||||
|
end
|
||||||
|
|
||||||
|
def max_width
|
||||||
|
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||||
|
end
|
||||||
|
|
||||||
|
def fits_on_line?(element) # Flow
|
||||||
|
@current_position.x + element.outer_width <= max_width &&
|
||||||
|
@current_position.x + element.outer_width <= window.width
|
||||||
|
end
|
||||||
|
|
||||||
|
def position_on_current_line(element) # Flow
|
||||||
|
element.x = element.style.margin_left + @current_position.x
|
||||||
|
element.y = element.style.margin_top + @current_position.y
|
||||||
|
|
||||||
|
element.recalculate
|
||||||
|
|
||||||
|
@current_position.x += element.outer_width
|
||||||
|
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
||||||
|
end
|
||||||
|
|
||||||
|
def tallest_neighbor(querier, y_position) # Flow
|
||||||
|
response = querier
|
||||||
|
@children.each do |child|
|
||||||
|
response = child if child.outer_height > response.outer_height
|
||||||
|
break if child == querier
|
||||||
|
end
|
||||||
|
|
||||||
|
return response
|
||||||
|
end
|
||||||
|
|
||||||
|
def position_on_next_line(child) # Flow
|
||||||
|
@current_position.x = @style.margin_left
|
||||||
|
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
||||||
|
|
||||||
|
child.x = child.style.margin_left + @current_position.x
|
||||||
|
child.y = child.style.margin_top + @current_position.y
|
||||||
|
|
||||||
|
child.recalculate
|
||||||
|
|
||||||
|
@current_position.x += child.outer_width
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_to_next_line(element) # Stack
|
||||||
|
element.x = element.style.margin_left + @current_position.x
|
||||||
|
element.y = element.style.margin_top + @current_position.y
|
||||||
|
|
||||||
|
element.recalculate
|
||||||
|
|
||||||
|
@current_position.y += element.outer_height
|
||||||
|
end
|
||||||
|
|
||||||
|
# def mouse_wheel_up(sender, x, y)
|
||||||
|
# @children.each {|c| c.y -= @scroll_speed}
|
||||||
|
# @children.each {|c| c.recalculate}
|
||||||
|
# end
|
||||||
|
|
||||||
|
# def mouse_wheel_down(sender, x, y)
|
||||||
|
# @children.each {|c| c.y += @scroll_speed}
|
||||||
|
# @children.each {|c| c.recalculate}
|
||||||
|
# end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@children.map {|c| c.class}.join(", ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
172
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
172
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class EditLine < Button
|
||||||
|
def initialize(text, options = {}, block = nil)
|
||||||
|
super(text, options, block)
|
||||||
|
|
||||||
|
@type = default(:type)
|
||||||
|
|
||||||
|
@caret_width = default(:caret_width)
|
||||||
|
@caret_height= @text.height
|
||||||
|
@caret_color = default(:caret_color)
|
||||||
|
@caret_interval = default(:caret_interval)
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
@show_caret = true
|
||||||
|
|
||||||
|
@text_input = Gosu::TextInput.new
|
||||||
|
@text_input.text = text
|
||||||
|
|
||||||
|
@offset_x = 0
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
|
||||||
|
Gosu.translate(-@offset_x, 0) do
|
||||||
|
draw_selection
|
||||||
|
draw_caret if @focus && @show_caret
|
||||||
|
draw_text
|
||||||
|
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
|
||||||
|
if @type == :password
|
||||||
|
@text.text = default(:password_character) * @text_input.text.length
|
||||||
|
else
|
||||||
|
@text.text = @text_input.text
|
||||||
|
end
|
||||||
|
|
||||||
|
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
|
||||||
|
@show_caret = !@show_caret
|
||||||
|
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
|
||||||
|
|
||||||
|
def left_mouse_button(sender, x, y)
|
||||||
|
super
|
||||||
|
window.text_input = @text_input
|
||||||
|
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
@show_caret = true
|
||||||
|
|
||||||
|
move_caret_to_mouse(x)
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def enter(sender)
|
||||||
|
if @focus
|
||||||
|
@style.background_canvas.background = default(:active, :background)
|
||||||
|
@text.color = default(:active, :color)
|
||||||
|
else
|
||||||
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
|
@text.color = default(:hover, :color)
|
||||||
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def leave(sender)
|
||||||
|
unless @focus
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def blur(sender)
|
||||||
|
@focus = false
|
||||||
|
@style.background_canvas.background = default(:background)
|
||||||
|
@text.color = default(:color)
|
||||||
|
window.text_input = nil
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
@text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
|
||||||
|
else
|
||||||
|
@text.x + @text.textobject.text_width(@text_input.text[0..@text_input.send(method)])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
super
|
||||||
|
|
||||||
|
@width = dimensional_size(@style.width, :width) || default(:width)
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@text_input.text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
17
lib/cyberarm_engine/ui/elements/flow.rb
Normal file
17
lib/cyberarm_engine/ui/elements/flow.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Flow < Container
|
||||||
|
include Common
|
||||||
|
|
||||||
|
def layout
|
||||||
|
@children.each do |child|
|
||||||
|
if fits_on_line?(child)
|
||||||
|
position_on_current_line(child)
|
||||||
|
else
|
||||||
|
position_on_next_line(child)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
52
lib/cyberarm_engine/ui/elements/image.rb
Normal file
52
lib/cyberarm_engine/ui/elements/image.rb
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Image < Element
|
||||||
|
def initialize(path, options = {}, block = nil)
|
||||||
|
super(options, block)
|
||||||
|
@path = path
|
||||||
|
|
||||||
|
@image = Gosu::Image.new(path, retro: @options[:image_retro])
|
||||||
|
@scale_x, @scale_y = 1, 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@image.draw(
|
||||||
|
@style.border_thickness_left + @style.padding_left + @x,
|
||||||
|
@style.border_thickness_top + @style.padding_top + @y,
|
||||||
|
@z + 2,
|
||||||
|
@scale_x, @scale_y) # TODO: Add color support?
|
||||||
|
end
|
||||||
|
|
||||||
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
|
||||||
|
if _width && _height
|
||||||
|
@scale_x = _width.to_f / @image.width
|
||||||
|
@scale_y = _height.to_f / @image.height
|
||||||
|
elsif _width
|
||||||
|
@scale_x = _width.to_f / @image.width
|
||||||
|
@scale_y = @scale_x
|
||||||
|
elsif _height
|
||||||
|
@scale_y = _height.to_f / @image.height
|
||||||
|
@scale_x = @scale_y
|
||||||
|
else
|
||||||
|
@scale_x, @scale_y = 1, 1
|
||||||
|
end
|
||||||
|
|
||||||
|
@width = _width ? _width : @image.width.round * @scale_x
|
||||||
|
@height= _height ? _height : @image.height.round * @scale_y
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
50
lib/cyberarm_engine/ui/elements/label.rb
Normal file
50
lib/cyberarm_engine/ui/elements/label.rb
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Label < Element
|
||||||
|
def initialize(text, options = {}, block = nil)
|
||||||
|
super(options, block)
|
||||||
|
|
||||||
|
@text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow])
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@text.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
@width, @height = 0, 0
|
||||||
|
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
|
||||||
|
@width = _width ? _width : @text.width.round
|
||||||
|
@height= _height ? _height : @text.height.round
|
||||||
|
|
||||||
|
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||||
|
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||||
|
@text.z = @z + 3
|
||||||
|
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@text.text
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(value)
|
||||||
|
@text.text = value
|
||||||
|
|
||||||
|
old_width, old_height = width, height
|
||||||
|
recalculate
|
||||||
|
|
||||||
|
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
50
lib/cyberarm_engine/ui/elements/progress.rb
Normal file
50
lib/cyberarm_engine/ui/elements/progress.rb
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Progress < Element
|
||||||
|
def initialize(options = {}, block = nil)
|
||||||
|
super(options, block)
|
||||||
|
|
||||||
|
@fraction_background = Background.new(background: @style.fraction_background)
|
||||||
|
self.value = options[:fraction] ? options[:fraction] : 0.0
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@fraction_background.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
@width = _width
|
||||||
|
@height= _height
|
||||||
|
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_background
|
||||||
|
super
|
||||||
|
|
||||||
|
@fraction_background.x = @style.border_thickness_left + @style.padding_left + @x
|
||||||
|
@fraction_background.y = @style.border_thickness_top + @style.padding_top + @y
|
||||||
|
@fraction_background.z = @z
|
||||||
|
@fraction_background.width = @width * @fraction
|
||||||
|
@fraction_background.height = @height
|
||||||
|
|
||||||
|
@fraction_background.background = @style.fraction_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@fraction
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(decimal)
|
||||||
|
raise "value must be number" unless decimal.is_a?(Numeric)
|
||||||
|
|
||||||
|
@fraction = decimal.clamp(0.0, 1.0)
|
||||||
|
update_background
|
||||||
|
|
||||||
|
return @fraction
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
lib/cyberarm_engine/ui/elements/stack.rb
Normal file
13
lib/cyberarm_engine/ui/elements/stack.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class Stack < Container
|
||||||
|
include Common
|
||||||
|
|
||||||
|
def layout
|
||||||
|
@children.each do |child|
|
||||||
|
move_to_next_line(child)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
56
lib/cyberarm_engine/ui/elements/toggle_button.rb
Normal file
56
lib/cyberarm_engine/ui/elements/toggle_button.rb
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Element
|
||||||
|
class ToggleButton < Button
|
||||||
|
attr_reader :toggled
|
||||||
|
|
||||||
|
def initialize(options, block = nil)
|
||||||
|
super(options[:checkmark], options, block)
|
||||||
|
@toggled = options[:toggled] || false
|
||||||
|
if @toggled
|
||||||
|
@text.text = @options[:checkmark]
|
||||||
|
else
|
||||||
|
@text.text = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggled=(boolean)
|
||||||
|
@toggled = !boolean
|
||||||
|
toggle
|
||||||
|
end
|
||||||
|
|
||||||
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
|
toggle
|
||||||
|
|
||||||
|
@block.call(self) if @block
|
||||||
|
|
||||||
|
return :handled
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle
|
||||||
|
if @toggled
|
||||||
|
@toggled = false
|
||||||
|
@text.text = ""
|
||||||
|
else
|
||||||
|
@toggled = true
|
||||||
|
@text.text = @options[:checkmark]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def recalculate
|
||||||
|
super
|
||||||
|
|
||||||
|
_width = dimensional_size(@style.width, :width)
|
||||||
|
_height= dimensional_size(@style.height,:height)
|
||||||
|
@width = _width ? _width : @text.textobject.text_width(@options[:checkmark])
|
||||||
|
@height = _height ? _height : @text.height
|
||||||
|
update_background
|
||||||
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@toggled
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Flow < Container
|
|
||||||
include Common
|
|
||||||
|
|
||||||
def layout
|
|
||||||
@children.each do |child|
|
|
||||||
if fits_on_line?(child)
|
|
||||||
position_on_current_line(child)
|
|
||||||
else
|
|
||||||
position_on_next_line(child)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -10,17 +10,18 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@down_keys = {}
|
@down_keys = {}
|
||||||
|
|
||||||
@root_container = Stack.new
|
@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_height = window.height
|
||||||
|
|
||||||
@focus = nil
|
@focus = nil
|
||||||
@mouse_over = nil
|
@mouse_over = nil
|
||||||
@mouse_down_on = {}
|
@mouse_down_on = {}
|
||||||
@mouse_down_position = {}
|
@mouse_down_position = {}
|
||||||
|
@pending_recalculate_request = false
|
||||||
|
|
||||||
setup
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# throws :blur event to focused element and sets GuiState focused element
|
# throws :blur event to focused element and sets GuiState focused element
|
||||||
@@ -35,6 +36,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
if @pending_recalculate_request
|
||||||
|
@root_container.recalculate
|
||||||
|
@pending_recalculate_request = false
|
||||||
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
|
|
||||||
new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
|
new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
|
||||||
@@ -49,6 +55,11 @@ module CyberarmEngine
|
|||||||
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
|
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
|
||||||
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
|
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
|
||||||
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
|
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
|
||||||
|
|
||||||
|
request_recalculate if @active_width != window.width || @active_height != window.height
|
||||||
|
|
||||||
|
@active_width = window.width
|
||||||
|
@active_height = window.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
@@ -115,5 +126,10 @@ module CyberarmEngine
|
|||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Schedule a full GUI recalculation on next update
|
||||||
|
def request_recalculate
|
||||||
|
@pending_recalculate_request = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Image < Element
|
|
||||||
def initialize(path, options = {}, block = nil)
|
|
||||||
super(options, block)
|
|
||||||
@path = path
|
|
||||||
|
|
||||||
@image = Gosu::Image.new(path, retro: @options[:image_retro])
|
|
||||||
if @options[:width] && @options[:height]
|
|
||||||
@scale_x = @options[:width].to_f / @image.width
|
|
||||||
@scale_y = @options[:height].to_f / @image.height
|
|
||||||
elsif @options[:width]
|
|
||||||
@scale_x = @options[:width].to_f / @image.width
|
|
||||||
@scale_y = @scale_x
|
|
||||||
elsif @options[:height]
|
|
||||||
@scale_y = @options[:height].to_f / @image.height
|
|
||||||
@scale_x = @scale_y
|
|
||||||
else
|
|
||||||
@scale_x, @scale_y = 1, 1
|
|
||||||
end
|
|
||||||
|
|
||||||
raise "Scale X" unless @scale_x.is_a?(Numeric)
|
|
||||||
raise "Scale Y" unless @scale_y.is_a?(Numeric)
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
@image.draw(
|
|
||||||
@style.border_thickness_left + @style.padding_left + @x,
|
|
||||||
@style.border_thickness_top + @style.padding_top + @y,
|
|
||||||
@z + 2,
|
|
||||||
@scale_x, @scale_y) # TODO: Add color support?
|
|
||||||
end
|
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
|
||||||
@block.call(self) if @block
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
@style.width = @image.width * @scale_x
|
|
||||||
@style.height = @image.height * @scale_y
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Label < Element
|
|
||||||
def initialize(text, options = {}, block = nil)
|
|
||||||
super(options, block)
|
|
||||||
|
|
||||||
@text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow])
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
@text.draw
|
|
||||||
end
|
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
|
||||||
@block.call(self) if @block
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
@style.width = @text.width.round
|
|
||||||
@style.height= @text.height.round
|
|
||||||
|
|
||||||
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
|
||||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
|
||||||
@text.z = @z + 3
|
|
||||||
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@text.text
|
|
||||||
end
|
|
||||||
|
|
||||||
def value=(value)
|
|
||||||
@text.text = value
|
|
||||||
|
|
||||||
old_width, old_height = width, height
|
|
||||||
recalculate
|
|
||||||
|
|
||||||
root.recalculate if old_width != width || old_height != height
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Stack < Container
|
|
||||||
include Common
|
|
||||||
|
|
||||||
def layout
|
|
||||||
@children.each do |child|
|
|
||||||
move_to_next_line(child)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,7 +1,24 @@
|
|||||||
|
module Gosu
|
||||||
|
class Color
|
||||||
|
def _dump(level)
|
||||||
|
[
|
||||||
|
"%02X" % self.alpha,
|
||||||
|
"%02X" % self.red,
|
||||||
|
"%02X" % self.green,
|
||||||
|
"%02X" % self.blue
|
||||||
|
].join
|
||||||
|
end
|
||||||
|
|
||||||
|
def self._load(hex)
|
||||||
|
argb(hex.to_i(16))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Style
|
class Style
|
||||||
def initialize(hash = {})
|
def initialize(hash = {})
|
||||||
@hash = hash
|
@hash = Marshal.load(Marshal.dump(hash))
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(method, *args, &block)
|
def method_missing(method, *args, &block)
|
||||||
|
|||||||
@@ -26,7 +26,23 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
hash.merge(options)
|
deep_merge(hash, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Derived from Rails Hash#deep_merge!
|
||||||
|
# Enables passing partial themes through Element options without issue
|
||||||
|
def deep_merge(original, intergrate, &block)
|
||||||
|
hash = original.merge(intergrate) do |key, this_val, other_val|
|
||||||
|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
||||||
|
deep_merge(this_val, other_val, &block)
|
||||||
|
elsif block_given?
|
||||||
|
block.call(key, this_val, other_val)
|
||||||
|
else
|
||||||
|
other_val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return hash
|
||||||
end
|
end
|
||||||
|
|
||||||
THEME = {
|
THEME = {
|
||||||
@@ -49,7 +65,7 @@ module CyberarmEngine
|
|||||||
Button: { # < Label
|
Button: { # < Label
|
||||||
margin: 1,
|
margin: 1,
|
||||||
padding: 4,
|
padding: 4,
|
||||||
border_thickness: 4,
|
border_thickness: 1,
|
||||||
border_color: ["ffd59674".hex, "ffff8746".hex],
|
border_color: ["ffd59674".hex, "ffff8746".hex],
|
||||||
border_radius: 0,
|
border_radius: 0,
|
||||||
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
||||||
@@ -72,9 +88,10 @@ 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: {
|
Image: { # < Element
|
||||||
retro: false
|
retro: false
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -88,6 +105,15 @@ module CyberarmEngine
|
|||||||
|
|
||||||
ToggleButton: { # < Button
|
ToggleButton: { # < Button
|
||||||
checkmark: "√"
|
checkmark: "√"
|
||||||
|
},
|
||||||
|
|
||||||
|
Progress: { # < Element
|
||||||
|
width: 250,
|
||||||
|
height: 36,
|
||||||
|
background: 0xff111111,
|
||||||
|
fraction_background: [0xffc75e61, 0xffe26623],
|
||||||
|
border_thickness: 1,
|
||||||
|
border_color: [0xffd59674, 0xffff8746]
|
||||||
}
|
}
|
||||||
}.freeze
|
}.freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class ToggleButton < Button
|
|
||||||
attr_reader :toggled
|
|
||||||
|
|
||||||
def initialize(options, block = nil)
|
|
||||||
super(options[:checkmark], options, block)
|
|
||||||
@toggled = options[:toggled] || false
|
|
||||||
if @toggled
|
|
||||||
@text.text = @options[:checkmark]
|
|
||||||
else
|
|
||||||
@text.text = ""
|
|
||||||
end
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
def toggled=(boolean)
|
|
||||||
@toggled = !boolean
|
|
||||||
toggle
|
|
||||||
end
|
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
|
||||||
toggle
|
|
||||||
|
|
||||||
@block.call(self) if @block
|
|
||||||
end
|
|
||||||
|
|
||||||
def toggle
|
|
||||||
if @toggled
|
|
||||||
@toggled = false
|
|
||||||
@text.text = ""
|
|
||||||
else
|
|
||||||
@toggled = true
|
|
||||||
@text.text = @options[:checkmark]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def recalculate
|
|
||||||
super
|
|
||||||
|
|
||||||
@style.width = @text.textobject.text_width(@options[:checkmark])
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@toggled
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,5 +1,65 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Vector
|
class Vector
|
||||||
|
##
|
||||||
|
# Creates a up vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 1, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.up
|
||||||
|
Vector.new(0, 1, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a down vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, -1, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.down
|
||||||
|
Vector.new(0, -1, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a left vector
|
||||||
|
#
|
||||||
|
# Vector.new(-1, 0, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.left
|
||||||
|
Vector.new(-1, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a right vector
|
||||||
|
#
|
||||||
|
# Vector.new(1, 0, 0)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.right
|
||||||
|
Vector.new(1, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a forward vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 0, 1)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.forward
|
||||||
|
Vector.new(0, 0, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Creates a backward vector
|
||||||
|
#
|
||||||
|
# Vector.new(0, 0, -1)
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def self.backward
|
||||||
|
Vector.new(0, 0, -1)
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||||
@x, @y, @z, @weight = x, y, z, weight
|
@x, @y, @z, @weight = x, y, z, weight
|
||||||
end
|
end
|
||||||
@@ -16,21 +76,33 @@ 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=
|
||||||
|
|
||||||
|
# @return [Boolean]
|
||||||
def ==(other)
|
def ==(other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@x == other &&
|
@x == other &&
|
||||||
@y == other &&
|
@y == other &&
|
||||||
@z == other &&
|
@z == other &&
|
||||||
@weight == other
|
@weight == other
|
||||||
else
|
elsif other.is_a?(Vector)
|
||||||
@x == other.x &&
|
@x == other.x &&
|
||||||
@y == other.y &&
|
@y == other.y &&
|
||||||
@z == other.z &&
|
@z == other.z &&
|
||||||
@weight == other.weight
|
@weight == other.weight
|
||||||
|
else
|
||||||
|
other == self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs math operation, excluding @weight
|
# Create a new vector using {x} and {y} values
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def xy
|
||||||
|
Vector.new(@x, @y)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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)
|
||||||
Vector.new(
|
Vector.new(
|
||||||
@@ -47,22 +119,26 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds Vector and Numberic or Vector and Vector, excluding @weight
|
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def +(other)
|
def +(other)
|
||||||
operator("+", other)
|
operator("+", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Subtracts Vector and Numberic or Vector and Vector, excluding @weight
|
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def -(other)
|
def -(other)
|
||||||
operator("-", other)
|
operator("-", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Multiplies Vector and Numberic or Vector and Vector, excluding @weight
|
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def *(other)
|
def *(other)
|
||||||
operator("*", other)
|
operator("*", other)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Divides Vector and Numberic or Vector and Vector, excluding @weight
|
# Divides Vector and Numeric or Vector and Vector, excluding {weight}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def /(other)
|
def /(other)
|
||||||
# Duplicated to protect from DivideByZero
|
# Duplicated to protect from DivideByZero
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@@ -80,6 +156,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# dot product of {Vector}
|
||||||
|
# @return [Integer|Float]
|
||||||
def dot(other)
|
def dot(other)
|
||||||
product = 0
|
product = 0
|
||||||
|
|
||||||
@@ -93,6 +171,8 @@ module CyberarmEngine
|
|||||||
return product
|
return product
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# cross product of {Vector}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def cross(other)
|
def cross(other)
|
||||||
a = self.to_a
|
a = self.to_a
|
||||||
b = other.to_a
|
b = other.to_a
|
||||||
@@ -105,24 +185,40 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
# returns degrees
|
# returns degrees
|
||||||
|
# @return [Float]
|
||||||
def angle(other)
|
def angle(other)
|
||||||
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns magnitude of Vector, ignoring #weight
|
# returns magnitude of Vector, ignoring #weight
|
||||||
|
# @return [Float]
|
||||||
def magnitude
|
def magnitude
|
||||||
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# returns normalized {Vector}
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# CyberarmEngine::Vector.new(50, 21.2, 45).normalized
|
||||||
|
# # => <CyberarmEngine::Vector:0x001 @x=0.7089... @y=0.3005... @z=0.6380... @weight=0>
|
||||||
|
#
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def normalized
|
def normalized
|
||||||
mag = magnitude
|
mag = magnitude
|
||||||
self / Vector.new(mag, mag, mag)
|
self / Vector.new(mag, mag, mag)
|
||||||
end
|
end
|
||||||
|
|
||||||
def direction
|
|
||||||
|
# returns a direction {Vector}
|
||||||
|
#
|
||||||
# z is pitch
|
# z is pitch
|
||||||
|
#
|
||||||
# y is yaw
|
# y is yaw
|
||||||
|
#
|
||||||
# x is roll
|
# x is roll
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def direction
|
||||||
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
_y = Math.sin(@z.degrees_to_radians)
|
_y = Math.sin(@z.degrees_to_radians)
|
||||||
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
@@ -130,37 +226,63 @@ module CyberarmEngine
|
|||||||
Vector.new(_x, _y, _z)
|
Vector.new(_x, _y, _z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns an inverse {Vector}
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
def inverse
|
def inverse
|
||||||
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Adds up values of {x}, {y}, and {z}
|
||||||
|
# @return [Integer|Float]
|
||||||
def sum
|
def sum
|
||||||
@x + @y + @z
|
@x + @y + @z
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Linear interpolation: smoothly transition between two {Vector}
|
||||||
|
#
|
||||||
|
# CyberarmEngine::Vector.new(100, 100, 100).lerp( CyberarmEngine::Vector.new(0, 0, 0), 0.75 )
|
||||||
|
# # => <CyberarmEngine::Vector:0x0001 @x=75.0, @y=75.0, @z=75.0, @weight=0>
|
||||||
|
#
|
||||||
|
# @param other [CyberarmEngine::Vector | Integer | Float] value to subtract from
|
||||||
|
# @param factor [Float] how complete transition to _other_ is, in range [0.0..1.0]
|
||||||
|
# @return [CyberarmEngine::Vector]
|
||||||
|
def lerp(other, factor)
|
||||||
|
(self - other) * factor.clamp(0.0, 1.0)
|
||||||
|
end
|
||||||
|
|
||||||
# 2D distance using X and Y
|
# 2D distance using X and Y
|
||||||
|
# @return [Float]
|
||||||
def distance(other)
|
def distance(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2D distance using X and Z
|
# 2D distance using X and Z
|
||||||
|
# @return [Float]
|
||||||
def gl_distance2d(other)
|
def gl_distance2d(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 3D distance using X, Y, and Z
|
# 3D distance using X, Y, and Z
|
||||||
|
# @return [Float]
|
||||||
def distance3d(other)
|
def distance3d(other)
|
||||||
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to Array
|
||||||
|
# @return [Array]
|
||||||
def to_a
|
def to_a
|
||||||
[@x, @y, @z, @weight]
|
[@x, @y, @z, @weight]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to String
|
||||||
|
# @return [String]
|
||||||
def to_s
|
def to_s
|
||||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Converts {Vector} to Hash
|
||||||
|
# @return [Hash]
|
||||||
def to_h
|
def to_h
|
||||||
{x: @x, y: @y, z: @z, weight: @weight}
|
{x: @x, y: @y, z: @z, weight: @weight}
|
||||||
end
|
end
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev"
|
NAME = "InDev"
|
||||||
VERSION = "0.8.0"
|
VERSION = "0.13.1"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user