mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 | |||
| f4a783b371 | |||
| d1dec5791b | |||
| 8c5c5e1b7b | |||
| f9324448ee | |||
| 34d53ae1cb | |||
| 74d1ddd16b | |||
| da69f057a0 | |||
| 4f4770db0e | |||
| b6730204e8 | |||
| d8dc621c62 | |||
| 739ae86966 | |||
| 3be16c644f | |||
| 805077d232 | |||
| db1a0683e2 | |||
| f489ad162f | |||
| df2dc25e34 | |||
| 6b319935d3 | |||
| f9d551a419 | |||
| f34dd21644 | |||
| 025f54a752 | |||
| cbc27b5d5d | |||
| 4af377b93c | |||
| cc97077b0f |
@@ -1,35 +1,45 @@
|
|||||||
require "gosu"
|
begin
|
||||||
|
require File.expand_path("../../ffi-gosu/lib/gosu", File.dirname(__FILE__))
|
||||||
|
rescue LoadError => e
|
||||||
|
pp e
|
||||||
|
require "gosu"
|
||||||
|
end
|
||||||
|
|
||||||
require_relative "cyberarm_engine/version"
|
require_relative "cyberarm_engine/version"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/common"
|
require_relative "cyberarm_engine/common"
|
||||||
|
|
||||||
|
require_relative "cyberarm_engine/gosu_ext/circle"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/game_object"
|
require_relative "cyberarm_engine/game_object"
|
||||||
require_relative "cyberarm_engine/engine"
|
require_relative "cyberarm_engine/engine"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/lib/bounding_box"
|
require_relative "cyberarm_engine/bounding_box"
|
||||||
require_relative "cyberarm_engine/lib/vector"
|
require_relative "cyberarm_engine/vector"
|
||||||
|
require_relative "cyberarm_engine/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/objects/text"
|
require_relative "cyberarm_engine/text"
|
||||||
require_relative "cyberarm_engine/objects/timer"
|
require_relative "cyberarm_engine/timer"
|
||||||
require_relative "cyberarm_engine/objects/multi_line_text"
|
|
||||||
|
|
||||||
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/elements/stack"
|
||||||
|
require_relative "cyberarm_engine/ui/elements/check_box"
|
||||||
|
require_relative "cyberarm_engine/ui/elements/progress"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/ui/flow"
|
|
||||||
require_relative "cyberarm_engine/ui/stack"
|
|
||||||
require_relative "cyberarm_engine/ui/check_box"
|
|
||||||
require_relative "cyberarm_engine/ui/dsl"
|
require_relative "cyberarm_engine/ui/dsl"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/game_state"
|
require_relative "cyberarm_engine/game_state"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
150
lib/cyberarm_engine/bounding_box.rb
Normal file
150
lib/cyberarm_engine/bounding_box.rb
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class BoundingBox
|
||||||
|
attr_accessor :min, :max
|
||||||
|
|
||||||
|
def initialize(*args)
|
||||||
|
case args.size
|
||||||
|
when 0
|
||||||
|
@min = Vector.new(0, 0, 0)
|
||||||
|
@max = Vector.new(0, 0, 0)
|
||||||
|
when 2
|
||||||
|
@min = args.first.clone
|
||||||
|
@max = args.last.clone
|
||||||
|
when 4
|
||||||
|
@min = Vector.new(args[0], args[1], 0)
|
||||||
|
@max = Vector.new(args[2], args[3], 0)
|
||||||
|
when 6
|
||||||
|
@min = Vector.new(args[0], args[1], args[2])
|
||||||
|
@max = Vector.new(args[3], args[4], args[5])
|
||||||
|
else
|
||||||
|
raise "Invalid number of arguments! Got: #{args.size}, expected: 0, 2, 4, or 6."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
@min == other.min &&
|
||||||
|
@max == other.max
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns a new bounding box that includes both bounding boxes
|
||||||
|
def union(other)
|
||||||
|
temp = BoundingBox.new
|
||||||
|
temp.min.x = [@min.x, other.min.x].min
|
||||||
|
temp.min.y = [@min.y, other.min.y].min
|
||||||
|
temp.min.z = [@min.z, other.min.z].min
|
||||||
|
|
||||||
|
temp.max.x = [@max.x, other.max.x].max
|
||||||
|
temp.max.y = [@max.y, other.max.y].max
|
||||||
|
temp.max.z = [@max.z, other.max.z].max
|
||||||
|
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns the difference between both bounding boxes
|
||||||
|
def difference(other)
|
||||||
|
temp = BoundingBox.new
|
||||||
|
temp.min = @min - other.min
|
||||||
|
temp.max = @max - other.max
|
||||||
|
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns whether bounding box intersects 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.y <= other.max.y && @max.y >= other.min.y) &&
|
||||||
|
(@min.z <= other.max.z && @max.z >= other.min.z)
|
||||||
|
else
|
||||||
|
raise "Unknown collider: #{other.class}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# does this bounding box envelop other bounding box? (inclusive of border)
|
||||||
|
def contains?(other)
|
||||||
|
other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z &&
|
||||||
|
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
(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))
|
||||||
|
end
|
||||||
|
|
||||||
|
def volume
|
||||||
|
width * height * depth
|
||||||
|
end
|
||||||
|
|
||||||
|
def width
|
||||||
|
@max.x - @min.x
|
||||||
|
end
|
||||||
|
|
||||||
|
def height
|
||||||
|
@max.y - @min.y
|
||||||
|
end
|
||||||
|
|
||||||
|
def depth
|
||||||
|
@max.z - @min.z
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize(entity)
|
||||||
|
temp = BoundingBox.new
|
||||||
|
temp.min.x = @min.x.to_f * entity.scale.x
|
||||||
|
temp.min.y = @min.y.to_f * entity.scale.y
|
||||||
|
temp.min.z = @min.z.to_f * entity.scale.z
|
||||||
|
|
||||||
|
temp.max.x = @max.x.to_f * entity.scale.x
|
||||||
|
temp.max.y = @max.y.to_f * entity.scale.y
|
||||||
|
temp.max.z = @max.z.to_f * entity.scale.z
|
||||||
|
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_with_offset(entity)
|
||||||
|
temp = BoundingBox.new
|
||||||
|
temp.min.x = @min.x.to_f * entity.scale.x + entity.position.x
|
||||||
|
temp.min.y = @min.y.to_f * entity.scale.y + entity.position.y
|
||||||
|
temp.min.z = @min.z.to_f * entity.scale.z + entity.position.z
|
||||||
|
|
||||||
|
temp.max.x = @max.x.to_f * entity.scale.x + entity.position.x
|
||||||
|
temp.max.y = @max.y.to_f * entity.scale.y + entity.position.y
|
||||||
|
temp.max.z = @max.z.to_f * entity.scale.z + entity.position.z
|
||||||
|
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
def +(other)
|
||||||
|
box = BoundingBox.new
|
||||||
|
box.min = self.min + other.min
|
||||||
|
box.min = self.max + other.max
|
||||||
|
|
||||||
|
return box
|
||||||
|
end
|
||||||
|
|
||||||
|
def -(other)
|
||||||
|
box = BoundingBox.new
|
||||||
|
box.min = self.min - other.min
|
||||||
|
box.min = self.max - other.max
|
||||||
|
|
||||||
|
return box
|
||||||
|
end
|
||||||
|
|
||||||
|
def sum
|
||||||
|
@min.sum + @max.sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def clone
|
||||||
|
BoundingBox.new(@min.x, @min.y, @min.z, @max.x, @max.y, @max.z)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,54 +1,60 @@
|
|||||||
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)
|
||||||
if defined?(color.alpha)
|
if defined?(color.alpha)
|
||||||
return Gosu::Color.rgba(color.red+amount, color.green+amount, color.blue+amount, color.alpha)
|
return Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
|
||||||
else
|
else
|
||||||
return Gosu::Color.rgb(color.red+amount, color.green+amount, color.blue+amount)
|
return Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def darken(color, amount = 25)
|
def darken(color, amount = 25)
|
||||||
if defined?(color.alpha)
|
if defined?(color.alpha)
|
||||||
return Gosu::Color.rgba(color.red-amount, color.green-amount, color.blue-amount, color.alpha)
|
return Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
|
||||||
else
|
else
|
||||||
return Gosu::Color.rgb(color.red-amount, color.green-amount, color.blue-amount)
|
return Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_asset(path, hash, klass)
|
def opacity(color, ratio = 1.0)
|
||||||
|
alpha = 255 * ratio
|
||||||
|
|
||||||
|
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
@@ -58,7 +64,12 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
unless asset
|
unless asset
|
||||||
instance = klass.new(path)
|
instance = nil
|
||||||
|
if klass == Gosu::Image
|
||||||
|
instance = klass.new(path, retro: retro, tileable: tileable)
|
||||||
|
else
|
||||||
|
instance = klass.new(path)
|
||||||
|
end
|
||||||
hash[path] = instance
|
hash[path] = instance
|
||||||
asset = instance
|
asset = instance
|
||||||
end
|
end
|
||||||
@@ -66,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)
|
||||||
@@ -82,4 +93,4 @@ module CyberarmEngine
|
|||||||
$window
|
$window
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
@@ -15,10 +15,10 @@ module CyberarmEngine
|
|||||||
$window.last_frame_time/1000.0
|
$window.last_frame_time/1000.0
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(width = 800, height = 600, fullscreen = false, update_interval = 1000.0/60)
|
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0/60, resizable: false)
|
||||||
@show_cursor = false
|
@show_cursor = false
|
||||||
|
|
||||||
super(width, height, fullscreen, update_interval)
|
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable)
|
||||||
$window = self
|
$window = self
|
||||||
@last_frame_time = Gosu.milliseconds-1
|
@last_frame_time = Gosu.milliseconds-1
|
||||||
@current_frame_time = Gosu.milliseconds
|
@current_frame_time = Gosu.milliseconds
|
||||||
@@ -48,19 +48,25 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
|
super
|
||||||
current_state.button_down(id) if current_state
|
current_state.button_down(id) if current_state
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_up(id)
|
def button_up(id)
|
||||||
|
super
|
||||||
current_state.button_up(id) if current_state
|
current_state.button_up(id) if current_state
|
||||||
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
|
||||||
|
|
||||||
@@ -92,4 +98,4 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
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)
|
||||||
@@ -36,7 +37,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
setup
|
setup
|
||||||
|
|
||||||
@debug_text = MultiLineText.new("", color: @debug_color, y: @position.y-(self.height*self.scale), z: 9999)
|
@debug_text = Text.new("", color: @debug_color, y: @position.y-(self.height*self.scale), z: 9999)
|
||||||
@debug_text.x = @position.x
|
@debug_text.x = @position.x
|
||||||
if @radius == 0 || @radius == nil
|
if @radius == 0 || @radius == nil
|
||||||
@radius = options[:radius] ? options[:radius] : defined?(@image.width) ? ((@image.width+@image.height)/4)*scale : 1
|
@radius = options[:radius] ? options[:radius] : defined?(@image.width) ? ((@image.width+@image.height)/4)*scale : 1
|
||||||
@@ -121,11 +122,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
@image ? @image.width : 0
|
@image ? @image.width * self.scale : 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
@image ? @image.height : 0
|
@image ? @image.height * self.scale : 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def pause
|
def pause
|
||||||
@@ -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)
|
||||||
|
|||||||
@@ -9,10 +9,9 @@ module CyberarmEngine
|
|||||||
@options = options
|
@options = options
|
||||||
@game_objects = []
|
@game_objects = []
|
||||||
@global_pause = false
|
@global_pause = false
|
||||||
|
$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
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class BoundingBox
|
|
||||||
attr_accessor :min, :max
|
|
||||||
|
|
||||||
def initialize(minx = 0, miny = 0, minz = 0, maxx = 0, maxy = 0, maxz = 0)
|
|
||||||
@min = Vector.new(minx, miny, minz)
|
|
||||||
@max = Vector.new(maxx, maxy, maxz)
|
|
||||||
end
|
|
||||||
|
|
||||||
def ==(other)
|
|
||||||
@min == other.min &&
|
|
||||||
@max == other.max
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns a new bounding box that includes both bounding boxes
|
|
||||||
def union(other)
|
|
||||||
temp = BoundingBox.new
|
|
||||||
temp.min.x = [@min.x, other.min.x].min
|
|
||||||
temp.min.y = [@min.y, other.min.y].min
|
|
||||||
temp.min.z = [@min.z, other.min.z].min
|
|
||||||
|
|
||||||
temp.max.x = [@max.x, other.max.x].max
|
|
||||||
temp.max.y = [@max.y, other.max.y].max
|
|
||||||
temp.max.z = [@max.z, other.max.z].max
|
|
||||||
|
|
||||||
return temp
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns the difference between both bounding boxes
|
|
||||||
def difference(other)
|
|
||||||
temp = BoundingBox.new
|
|
||||||
temp.min = @min - other.min
|
|
||||||
temp.max = @max - other.max
|
|
||||||
|
|
||||||
return temp
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns whether both bounding boxes intersect
|
|
||||||
def intersect?(other)
|
|
||||||
(@min.x <= other.max.x && @max.x >= other.min.x) &&
|
|
||||||
(@min.y <= other.max.y && @max.y >= other.min.y) &&
|
|
||||||
(@min.z <= other.max.z && @max.z >= other.min.z)
|
|
||||||
end
|
|
||||||
|
|
||||||
# does this bounding box envelop other bounding box? (inclusive of border)
|
|
||||||
def contains?(other)
|
|
||||||
other.min.x >= min.x && other.min.y >= min.y && other.min.z >= min.z &&
|
|
||||||
other.max.x <= max.x && other.max.y <= max.y && other.max.z <= max.z
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns whether the vector is inside of the bounding box
|
|
||||||
def point?(vector)
|
|
||||||
vector.x.between?(@min.x, @max.x) &&
|
|
||||||
vector.y.between?(@min.y, @max.y) &&
|
|
||||||
vector.z.between?(@min.z, @max.z)
|
|
||||||
end
|
|
||||||
|
|
||||||
def volume
|
|
||||||
width * height * depth
|
|
||||||
end
|
|
||||||
|
|
||||||
def width
|
|
||||||
@max.x - @min.x
|
|
||||||
end
|
|
||||||
|
|
||||||
def height
|
|
||||||
@max.y - @min.y
|
|
||||||
end
|
|
||||||
|
|
||||||
def depth
|
|
||||||
@max.z - @min.z
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize(entity)
|
|
||||||
temp = BoundingBox.new
|
|
||||||
temp.min.x = @min.x.to_f * entity.scale
|
|
||||||
temp.min.y = @min.y.to_f * entity.scale
|
|
||||||
temp.min.z = @min.z.to_f * entity.scale
|
|
||||||
|
|
||||||
temp.max.x = @max.x.to_f * entity.scale
|
|
||||||
temp.max.y = @max.y.to_f * entity.scale
|
|
||||||
temp.max.z = @max.z.to_f * entity.scale
|
|
||||||
|
|
||||||
return temp
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalize_with_offset(entity)
|
|
||||||
temp = BoundingBox.new
|
|
||||||
temp.min.x = @min.x.to_f * entity.scale + entity.position.x
|
|
||||||
temp.min.y = @min.y.to_f * entity.scale + entity.position.y
|
|
||||||
temp.min.z = @min.z.to_f * entity.scale + entity.position.z
|
|
||||||
|
|
||||||
temp.max.x = @max.x.to_f * entity.scale + entity.position.x
|
|
||||||
temp.max.y = @max.y.to_f * entity.scale + entity.position.y
|
|
||||||
temp.max.z = @max.z.to_f * entity.scale + entity.position.z
|
|
||||||
|
|
||||||
return temp
|
|
||||||
end
|
|
||||||
|
|
||||||
def +(other)
|
|
||||||
box = BoundingBox.new
|
|
||||||
box.min = self.min + other.min
|
|
||||||
box.min = self.max + other.max
|
|
||||||
|
|
||||||
return box
|
|
||||||
end
|
|
||||||
|
|
||||||
def -(other)
|
|
||||||
box = BoundingBox.new
|
|
||||||
box.min = self.min - other.min
|
|
||||||
box.min = self.max - other.max
|
|
||||||
|
|
||||||
return box
|
|
||||||
end
|
|
||||||
|
|
||||||
def sum
|
|
||||||
@min.sum + @max.sum
|
|
||||||
end
|
|
||||||
|
|
||||||
def clone
|
|
||||||
BoundingBox.new(@min.x, @min.y, @min.z, @max.x, @max.y, @max.z)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Vector
|
|
||||||
|
|
||||||
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
|
||||||
@x, @y, @z, @weight = x, y, z, weight
|
|
||||||
end
|
|
||||||
|
|
||||||
def x; @x; end
|
|
||||||
def x=(n); @x = n; end
|
|
||||||
|
|
||||||
def y; @y; end
|
|
||||||
def y=(n); @y = n; end
|
|
||||||
|
|
||||||
def z; @z; end
|
|
||||||
def z=(n); @z = n; end
|
|
||||||
|
|
||||||
def weight; @weight; end
|
|
||||||
def weight=(n); @weight = n; end
|
|
||||||
|
|
||||||
# def xy=(nx, ny); @x = nx; @y = ny; end
|
|
||||||
# def xyz=(nx, ny, nz); @x = nx; @y = ny; @z = nz; end
|
|
||||||
# def xyzw=(nx, ny, nz, nw); @x = nx; @y = ny; @z = nz; @weight = nw; end
|
|
||||||
|
|
||||||
def ==(other)
|
|
||||||
if other.is_a?(Numeric)
|
|
||||||
@x == other &&
|
|
||||||
@y == other &&
|
|
||||||
@z == other &&
|
|
||||||
@weight == other
|
|
||||||
else
|
|
||||||
@x == other.x &&
|
|
||||||
@y == other.y &&
|
|
||||||
@z == other.z &&
|
|
||||||
@weight == other.weight
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def +(other)
|
|
||||||
Vector.new(
|
|
||||||
@x + other.x,
|
|
||||||
@y + other.y,
|
|
||||||
@z + other.z,
|
|
||||||
@weight + other.weight
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def -(other)
|
|
||||||
Vector.new(
|
|
||||||
@x - other.x,
|
|
||||||
@y - other.y,
|
|
||||||
@z - other.z,
|
|
||||||
@weight - other.weight
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def *(other)
|
|
||||||
Vector.new(
|
|
||||||
@x * other.x,
|
|
||||||
@y * other.y,
|
|
||||||
@z * other.z,
|
|
||||||
@weight * other.weight
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def /(other)
|
|
||||||
# Endeavors to prevent division by zero
|
|
||||||
Vector.new(
|
|
||||||
@x == 0 || other.x == 0 ? 0 : @x / other.x,
|
|
||||||
@y == 0 || other.y == 0 ? 0 : @y / other.y,
|
|
||||||
@z == 0 || other.z == 0 ? 0 : @z / other.z,
|
|
||||||
@weight == 0 || other.weight == 0 ? 0 : @weight / other.weight
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns magnitude of Vector, ignoring #weight
|
|
||||||
def magnitude
|
|
||||||
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
|
||||||
end
|
|
||||||
|
|
||||||
def normalized
|
|
||||||
mag = magnitude
|
|
||||||
self / Vector.new(mag, mag, mag)
|
|
||||||
end
|
|
||||||
|
|
||||||
def sum
|
|
||||||
@x + @y + @z + @weight
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_a
|
|
||||||
[@x, @y, @z, @weight]
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class MultiLineText
|
|
||||||
attr_accessor :options, :x, :y, :width, :height
|
|
||||||
|
|
||||||
def initialize(text, options={})
|
|
||||||
@texts = []
|
|
||||||
text.split("\n").each_with_index do |line, i|
|
|
||||||
_options = options
|
|
||||||
_options[:y]+=_options[:size]
|
|
||||||
@texts << Text.new(line, _options)
|
|
||||||
end
|
|
||||||
@options = options
|
|
||||||
@x = @texts.first ? @texts.first.x : 0
|
|
||||||
@y = @texts.first ? @texts.first.y : 0
|
|
||||||
@width = 0
|
|
||||||
@height = 0
|
|
||||||
calculate_boundry
|
|
||||||
end
|
|
||||||
|
|
||||||
def draw
|
|
||||||
@texts.each(&:draw)
|
|
||||||
end
|
|
||||||
|
|
||||||
def text
|
|
||||||
string = ""
|
|
||||||
@texts.each {|t| string << t.text}
|
|
||||||
return string
|
|
||||||
end
|
|
||||||
|
|
||||||
def text=(text)
|
|
||||||
|
|
||||||
if text.lines.count < @texts.count
|
|
||||||
range = ((@texts.count - @text.lines.count)-1)..@texts.count-1
|
|
||||||
p range
|
|
||||||
@texts.slice!(range)
|
|
||||||
end
|
|
||||||
|
|
||||||
text.split("\n").each_with_index do |line, i|
|
|
||||||
if @texts[i]
|
|
||||||
@texts[i].text = line
|
|
||||||
else
|
|
||||||
@texts << Text.new(line, @options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.y = @y
|
|
||||||
calculate_boundry
|
|
||||||
end
|
|
||||||
|
|
||||||
def x=(int)
|
|
||||||
@x = int
|
|
||||||
@texts.each {|t| t.x = int}
|
|
||||||
end
|
|
||||||
|
|
||||||
def y=(int)
|
|
||||||
@y = int
|
|
||||||
@texts.each_with_index {|t, i| t.y=int+(i*t.size)}
|
|
||||||
end
|
|
||||||
|
|
||||||
def calculate_boundry
|
|
||||||
@width = 0
|
|
||||||
@height= 0
|
|
||||||
@texts.each {|t| @width = t.width if t.width > @width}
|
|
||||||
@texts.each {|t| @height+=t.height}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Text
|
|
||||||
CACHE = {}
|
|
||||||
|
|
||||||
attr_accessor :text, :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
|
||||||
attr_reader :textobject
|
|
||||||
|
|
||||||
def initialize(text, options={})
|
|
||||||
@text = text || ""
|
|
||||||
@options = options
|
|
||||||
@size = options[:size] || 18
|
|
||||||
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
|
||||||
@x = options[:x] || 0
|
|
||||||
@y = options[:y] || 0
|
|
||||||
@z = options[:z] || 1025
|
|
||||||
@factor_x = options[:factor_x] || 1
|
|
||||||
@factor_y = options[:factor_y] || 1
|
|
||||||
@color = options[:color] || Gosu::Color::WHITE
|
|
||||||
@alignment= options[:alignment] || nil
|
|
||||||
@shadow = true if options[:shadow] == true
|
|
||||||
@shadow = false if options[:shadow] == false
|
|
||||||
@shadow = true if options[:shadow] == nil
|
|
||||||
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
|
||||||
@shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
|
|
||||||
@textobject = check_cache(@size, @font)
|
|
||||||
|
|
||||||
if @alignment
|
|
||||||
case @alignment
|
|
||||||
when :left
|
|
||||||
@x = 0+BUTTON_PADDING
|
|
||||||
when :center
|
|
||||||
@x = ($window.width/2)-(@textobject.text_width(@text)/2)
|
|
||||||
when :right
|
|
||||||
@x = $window.width-BUTTON_PADDING-@textobject.text_width(@text)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_cache(size, font_name)
|
|
||||||
available = false
|
|
||||||
font = nil
|
|
||||||
|
|
||||||
if CACHE[size]
|
|
||||||
if CACHE[size][font_name]
|
|
||||||
font = CACHE[size][font_name]
|
|
||||||
available = true
|
|
||||||
else
|
|
||||||
available = false
|
|
||||||
end
|
|
||||||
else
|
|
||||||
available = false
|
|
||||||
end
|
|
||||||
|
|
||||||
unless available
|
|
||||||
font = Gosu::Font.new(@size, name: @font)
|
|
||||||
CACHE[@size] = {} unless CACHE[@size].is_a?(Hash)
|
|
||||||
CACHE[@size][@font] = font
|
|
||||||
end
|
|
||||||
|
|
||||||
return font
|
|
||||||
end
|
|
||||||
|
|
||||||
def width
|
|
||||||
textobject.text_width(@text)
|
|
||||||
end
|
|
||||||
|
|
||||||
def height
|
|
||||||
textobject.height
|
|
||||||
end
|
|
||||||
|
|
||||||
def draw
|
|
||||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
|
||||||
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) if @shadow_alpha <= @color.alpha
|
|
||||||
_color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, @color.alpha) unless @shadow_alpha <= @color.alpha
|
|
||||||
|
|
||||||
@textobject.draw_markup(@text, @x-@shadow_size, @y, @z, @factor_x, @factor_y, _color)
|
|
||||||
@textobject.draw_markup(@text, @x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
|
|
||||||
@textobject.draw_markup(@text, @x, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
@textobject.draw_markup(@text, @x+@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
|
|
||||||
@textobject.draw_markup(@text, @x, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
@textobject.draw_markup(@text, @x-@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
|
|
||||||
@textobject.draw_markup(@text, @x+@shadow_size, @y, @z, @factor_x, @factor_y, _color)
|
|
||||||
@textobject.draw_markup(@text, @x+@shadow_size, @y+@shadow_size, @z, @factor_x, @factor_y, _color)
|
|
||||||
end
|
|
||||||
|
|
||||||
@textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update; 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
|
||||||
205
lib/cyberarm_engine/shader.rb
Normal file
205
lib/cyberarm_engine/shader.rb
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||||
|
class Shader
|
||||||
|
include OpenGL
|
||||||
|
@@shaders = {}
|
||||||
|
|
||||||
|
def self.add(name, instance)
|
||||||
|
@@shaders[name] = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.use(name, &block)
|
||||||
|
shader = @@shaders.dig(name)
|
||||||
|
if shader
|
||||||
|
shader.use(&block)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Shader '#{name}' not found!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.available?(name)
|
||||||
|
@@shaders.dig(name).is_a?(Shader)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get(name)
|
||||||
|
@@shaders.dig(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.active_shader
|
||||||
|
@active_shader
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.active_shader=(instance)
|
||||||
|
@active_shader = instance
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.stop
|
||||||
|
shader = Shader.active_shader
|
||||||
|
|
||||||
|
if shader
|
||||||
|
shader.stop
|
||||||
|
else
|
||||||
|
raise ArgumentError, "No active shader to stop!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.attribute_location(variable)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.attribute_location(variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.set_uniform(variable, value)
|
||||||
|
raise RuntimeError, "No active shader!" unless Shader.active_shader
|
||||||
|
Shader.active_shader.set_uniform(variable, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :name, :program
|
||||||
|
def initialize(name:, vertex: "shaders/default.vert", fragment:)
|
||||||
|
@name = name
|
||||||
|
@vertex_file = vertex
|
||||||
|
@fragment_file = fragment
|
||||||
|
@compiled = false
|
||||||
|
|
||||||
|
@program = nil
|
||||||
|
|
||||||
|
@error_buffer_size = 1024
|
||||||
|
@variable_missing = {}
|
||||||
|
|
||||||
|
raise ArgumentError, "Shader files not found: #{@vertex_file} or #{@fragment_file}" unless shader_files_exist?
|
||||||
|
|
||||||
|
create_shaders
|
||||||
|
compile_shaders
|
||||||
|
|
||||||
|
# Only add shader if it successfully compiles
|
||||||
|
if @compiled
|
||||||
|
Shader.add(@name, self)
|
||||||
|
else
|
||||||
|
puts "FAILED to compile shader: #{@name}", ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def shader_files_exist?
|
||||||
|
File.exist?(@vertex_file) && File.exist?(@fragment_file)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_shaders
|
||||||
|
@vertex = glCreateShader(GL_VERTEX_SHADER)
|
||||||
|
@fragment = glCreateShader(GL_FRAGMENT_SHADER)
|
||||||
|
|
||||||
|
source = [File.read(@vertex_file)].pack('p')
|
||||||
|
size = [File.size(@vertex_file)].pack('I')
|
||||||
|
glShaderSource(@vertex, 1, source, size)
|
||||||
|
|
||||||
|
source = [File.read(@fragment_file)].pack('p')
|
||||||
|
size = [File.size(@fragment_file)].pack('I')
|
||||||
|
glShaderSource(@fragment, 1, source, size)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile_shaders
|
||||||
|
return unless shader_files_exist?
|
||||||
|
|
||||||
|
glCompileShader(@vertex)
|
||||||
|
buffer = ' '
|
||||||
|
glGetShaderiv(@vertex, GL_COMPILE_STATUS, buffer)
|
||||||
|
compiled = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if compiled == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetShaderInfoLog(@vertex, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Vectex Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
puts " Shader Compiled status: #{compiled}"
|
||||||
|
puts " NOTE: assignment of uniforms in shaders is illegal!"
|
||||||
|
puts
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
glCompileShader(@fragment)
|
||||||
|
buffer = ' '
|
||||||
|
glGetShaderiv(@fragment, GL_COMPILE_STATUS, buffer)
|
||||||
|
compiled = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if compiled == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetShaderInfoLog(@fragment, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Fragment Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
puts " Shader Compiled status: #{compiled}"
|
||||||
|
puts " NOTE: assignment of uniforms in shader is illegal!"
|
||||||
|
puts
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
@program = glCreateProgram
|
||||||
|
glAttachShader(@program, @vertex)
|
||||||
|
glAttachShader(@program, @fragment)
|
||||||
|
glLinkProgram(@program)
|
||||||
|
|
||||||
|
buffer = ' '
|
||||||
|
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
|
||||||
|
linked = buffer.unpack('L')[0]
|
||||||
|
|
||||||
|
if linked == 0
|
||||||
|
log = ' ' * @error_buffer_size
|
||||||
|
glGetProgramInfoLog(@program, @error_buffer_size, nil, log)
|
||||||
|
puts "Shader Error: Program \"#{@name}\""
|
||||||
|
puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
@compiled = linked == 0 ? false : true
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the location of a uniform variable
|
||||||
|
def variable(variable)
|
||||||
|
loc = glGetUniformLocation(@program, variable)
|
||||||
|
if (loc == -1)
|
||||||
|
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable]
|
||||||
|
@variable_missing[variable] = true
|
||||||
|
end
|
||||||
|
return loc
|
||||||
|
end
|
||||||
|
|
||||||
|
def use(&block)
|
||||||
|
return unless compiled?
|
||||||
|
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||||
|
Shader.active_shader=self
|
||||||
|
|
||||||
|
glUseProgram(@program)
|
||||||
|
|
||||||
|
if block
|
||||||
|
block.call(self)
|
||||||
|
stop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
Shader.active_shader = nil if Shader.active_shader == self
|
||||||
|
glUseProgram(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compiled?
|
||||||
|
@compiled
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_location(variable)
|
||||||
|
glGetUniformLocation(@program, variable)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_uniform(variable, value, location = nil)
|
||||||
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
|
case value.class.to_s.downcase.to_sym
|
||||||
|
when :integer
|
||||||
|
glUniform1i(attr_loc, value)
|
||||||
|
when :float
|
||||||
|
glUniform1f(attr_loc, value)
|
||||||
|
when :string
|
||||||
|
when :array
|
||||||
|
else
|
||||||
|
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
||||||
|
end
|
||||||
|
|
||||||
|
Window.handle_gl_error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
146
lib/cyberarm_engine/text.rb
Normal file
146
lib/cyberarm_engine/text.rb
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Text
|
||||||
|
CACHE = {}
|
||||||
|
|
||||||
|
attr_accessor :x, :y, :z, :size, :options
|
||||||
|
attr_reader :text, :textobject, :factor_x, :factor_y, :color, :shadow, :shadow_size, :shadow_alpha, :shadow_color
|
||||||
|
|
||||||
|
def initialize(text, options={})
|
||||||
|
@text = text.to_s || ""
|
||||||
|
@options = options
|
||||||
|
@size = options[:size] || 18
|
||||||
|
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
||||||
|
@x = options[:x] || 0
|
||||||
|
@y = options[:y] || 0
|
||||||
|
@z = options[:z] || 1025
|
||||||
|
@factor_x = options[:factor_x] || 1
|
||||||
|
@factor_y = options[:factor_y] || 1
|
||||||
|
@color = options[:color] || Gosu::Color::WHITE
|
||||||
|
@alignment= options[:alignment] || nil
|
||||||
|
@shadow = true if options[:shadow] == true
|
||||||
|
@shadow = false if options[:shadow] == false
|
||||||
|
@shadow = true if options[:shadow] == nil
|
||||||
|
@shadow_size = options[:shadow_size] ? options[:shadow_size] : 1
|
||||||
|
@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)
|
||||||
|
|
||||||
|
if @alignment
|
||||||
|
case @alignment
|
||||||
|
when :left
|
||||||
|
@x = 0+BUTTON_PADDING
|
||||||
|
when :center
|
||||||
|
@x = ($window.width/2)-(@textobject.text_width(@text)/2)
|
||||||
|
when :right
|
||||||
|
@x = $window.width-BUTTON_PADDING-@textobject.text_width(@text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_cache(size, font_name)
|
||||||
|
available = false
|
||||||
|
font = nil
|
||||||
|
|
||||||
|
if CACHE[size]
|
||||||
|
if CACHE[size][font_name]
|
||||||
|
font = CACHE[size][font_name]
|
||||||
|
available = true
|
||||||
|
else
|
||||||
|
available = false
|
||||||
|
end
|
||||||
|
else
|
||||||
|
available = false
|
||||||
|
end
|
||||||
|
|
||||||
|
unless available
|
||||||
|
font = Gosu::Font.new(@size, name: @font)
|
||||||
|
CACHE[@size] = {} unless CACHE[@size].is_a?(Hash)
|
||||||
|
CACHE[@size][@font] = font
|
||||||
|
end
|
||||||
|
|
||||||
|
return font
|
||||||
|
end
|
||||||
|
|
||||||
|
def text=(string)
|
||||||
|
@rendered_shadow = nil
|
||||||
|
@text = string
|
||||||
|
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
|
||||||
|
textobject.text_width(@text)
|
||||||
|
end
|
||||||
|
|
||||||
|
def height
|
||||||
|
@text.lines.count > 0 ? (@text.lines.count) * textobject.height : @textobject.height
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||||
|
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||||
|
shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha)
|
||||||
|
|
||||||
|
_x = @shadow_size
|
||||||
|
_y = @shadow_size
|
||||||
|
|
||||||
|
@rendered_shadow ||= Gosu.render((self.width+(shadow_size*2)).ceil, (self.height+(@shadow_size*2)).ceil) do
|
||||||
|
@textobject.draw_markup(@text, _x-@shadow_size, _y, @z)
|
||||||
|
@textobject.draw_markup(@text, _x-@shadow_size, _y-@shadow_size, @z)
|
||||||
|
|
||||||
|
@textobject.draw_markup(@text, _x, _y-@shadow_size, @z, @factor_x)
|
||||||
|
@textobject.draw_markup(@text, _x+@shadow_size, _y-@shadow_size, @z)
|
||||||
|
|
||||||
|
@textobject.draw_markup(@text, _x, _y+@shadow_size, @z)
|
||||||
|
@textobject.draw_markup(@text, _x-@shadow_size, _y+@shadow_size, @z)
|
||||||
|
|
||||||
|
@textobject.draw_markup(@text, _x+@shadow_size, _y, @z)
|
||||||
|
@textobject.draw_markup(@text, _x+@shadow_size, _y+@shadow_size, @z)
|
||||||
|
end
|
||||||
|
@rendered_shadow.draw(@x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, shadow_color)
|
||||||
|
end
|
||||||
|
|
||||||
|
@textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
|
||||||
|
end
|
||||||
|
|
||||||
|
def alpha=(n)
|
||||||
|
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n)
|
||||||
|
end
|
||||||
|
|
||||||
|
def alpha
|
||||||
|
@color.alpha
|
||||||
|
end
|
||||||
|
|
||||||
|
def update; end
|
||||||
|
end
|
||||||
|
end
|
||||||
84
lib/cyberarm_engine/transform.rb
Normal file
84
lib/cyberarm_engine/transform.rb
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Transform
|
||||||
|
attr_reader :elements
|
||||||
|
def initialize(matrix)
|
||||||
|
@elements = matrix
|
||||||
|
|
||||||
|
raise "Transform is wrong size! Got #{@elements.size}, expected 16" if 16 != @elements.size
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -66,31 +66,31 @@ module CyberarmEngine
|
|||||||
@top.z = @element.z
|
@top.z = @element.z
|
||||||
|
|
||||||
@top.width = @element.width
|
@top.width = @element.width
|
||||||
@top.height = @element.border_thickness_top
|
@top.height = @element.style.border_thickness_top
|
||||||
|
|
||||||
# RIGHT
|
# RIGHT
|
||||||
@right.x = @element.x + @element.width
|
@right.x = @element.x + @element.width
|
||||||
@right.y = @element.y + @element.border_thickness_top
|
@right.y = @element.y + @element.style.border_thickness_top
|
||||||
@right.z = @element.z
|
@right.z = @element.z
|
||||||
|
|
||||||
@right.width = -@element.border_thickness_right
|
@right.width = -@element.style.border_thickness_right
|
||||||
@right.height = @element.height - @element.border_thickness_top
|
@right.height = @element.height - @element.style.border_thickness_top
|
||||||
|
|
||||||
# BOTTOM
|
# BOTTOM
|
||||||
@bottom.x = @element.x
|
@bottom.x = @element.x
|
||||||
@bottom.y = @element.y + @element.height
|
@bottom.y = @element.y + @element.height
|
||||||
@bottom.z = @element.z
|
@bottom.z = @element.z
|
||||||
|
|
||||||
@bottom.width = @element.width - @element.border_thickness_right
|
@bottom.width = @element.width - @element.style.border_thickness_right
|
||||||
@bottom.height = -@element.border_thickness_bottom
|
@bottom.height = -@element.style.border_thickness_bottom
|
||||||
|
|
||||||
# LEFT
|
# LEFT
|
||||||
@left.x = @element.x
|
@left.x = @element.x
|
||||||
@left.y = @element.y
|
@left.y = @element.y
|
||||||
@left.z = @element.z
|
@left.z = @element.z
|
||||||
|
|
||||||
@left.width = @element.border_thickness_left
|
@left.width = @element.style.border_thickness_left
|
||||||
@left.height = @element.height - @element.border_thickness_bottom
|
@left.height = @element.height - @element.style.border_thickness_bottom
|
||||||
|
|
||||||
@top.update
|
@top.update
|
||||||
@right.update
|
@right.update
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
module CyberarmEngine
|
|
||||||
class Button < Label
|
|
||||||
def initialize(text, options = {}, block = nil)
|
|
||||||
super(text, options, block)
|
|
||||||
|
|
||||||
@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
|
|
||||||
@background_canvas.background = default(:active, :background)
|
|
||||||
@text.color = default(:active, :color)
|
|
||||||
else
|
|
||||||
@background_canvas.background = default(:hover, :background)
|
|
||||||
@text.color = default(:hover, :color)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
|
||||||
@focus = true
|
|
||||||
@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)
|
|
||||||
@background_canvas.background = default(:background)
|
|
||||||
@text.color = default(:color)
|
|
||||||
end
|
|
||||||
|
|
||||||
def blur(sender)
|
|
||||||
@focus = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,52 +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)
|
|
||||||
|
|
||||||
@width = @toggle_button.width + @label.width
|
|
||||||
@height = @toggle_button.height + @label.height
|
|
||||||
end
|
|
||||||
|
|
||||||
def text=(text)
|
|
||||||
@label.text = text
|
|
||||||
recalculate
|
|
||||||
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,154 +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 = []
|
|
||||||
|
|
||||||
@theme = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def build
|
|
||||||
@theme.merge(@parent.theme) if @parent
|
|
||||||
@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 theme
|
|
||||||
@theme
|
|
||||||
end
|
|
||||||
|
|
||||||
def color(color)
|
|
||||||
@theme[:color] = color
|
|
||||||
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(@margin_left, @margin_top)
|
|
||||||
|
|
||||||
layout
|
|
||||||
|
|
||||||
@width = @max_width ? @max_width : (@children.map {|c| c.x + c.width + c.margin_right }.max || 0).round
|
|
||||||
@height = @max_height ? @max_height : (@children.map {|c| c.y + c.height + c.margin_bottom}.max || 0).round
|
|
||||||
|
|
||||||
# Move child to parent after positioning
|
|
||||||
@children.each do |child|
|
|
||||||
child.x += @x
|
|
||||||
child.y += @y
|
|
||||||
|
|
||||||
# Fix child being displaced
|
|
||||||
child.recalculate
|
|
||||||
end
|
|
||||||
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def layout
|
|
||||||
raise "Not overridden"
|
|
||||||
end
|
|
||||||
|
|
||||||
def max_width
|
|
||||||
@max_width ? @max_width : window.width - (@parent ? @parent.margin_right + @margin_right : @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.margin_left + @current_position.x
|
|
||||||
element.y = element.margin_top + @current_position.y
|
|
||||||
|
|
||||||
element.recalculate
|
|
||||||
|
|
||||||
@current_position.x += element.outer_width
|
|
||||||
@current_position.x = @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 = @margin_left
|
|
||||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
|
||||||
|
|
||||||
child.x = child.margin_left + @current_position.x
|
|
||||||
child.y = child.margin_top + @current_position.y
|
|
||||||
|
|
||||||
child.recalculate
|
|
||||||
|
|
||||||
@current_position.x += child.outer_width
|
|
||||||
end
|
|
||||||
|
|
||||||
def move_to_next_line(element) # Stack
|
|
||||||
element.x = element.margin_left + @current_position.x
|
|
||||||
element.y = element.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
|
|
||||||
@@ -2,10 +2,11 @@ module CyberarmEngine
|
|||||||
module DSL
|
module DSL
|
||||||
def flow(options = {}, &block)
|
def flow(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_container = Flow.new(options, block)
|
options[:theme] = current_theme
|
||||||
|
_container = Element::Flow.new(options, block)
|
||||||
@containers << _container
|
@containers << _container
|
||||||
_container.build
|
_container.build
|
||||||
options[:parent].add(_container)
|
_container.parent.add(_container)
|
||||||
@containers.pop
|
@containers.pop
|
||||||
|
|
||||||
return _container
|
return _container
|
||||||
@@ -13,10 +14,11 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def stack(options = {}, &block)
|
def stack(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_container = Stack.new(options, block)
|
options[:theme] = current_theme
|
||||||
|
_container = Element::Stack.new(options, block)
|
||||||
@containers << _container
|
@containers << _container
|
||||||
_container.build
|
_container.build
|
||||||
options[:parent].add(_container)
|
_container.parent.add(_container)
|
||||||
@containers.pop
|
@containers.pop
|
||||||
|
|
||||||
return _container
|
return _container
|
||||||
@@ -24,7 +26,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def label(text, options = {}, &block)
|
def label(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = Label.new(text, options, block)
|
options[:theme] = current_theme
|
||||||
|
_element = Element::Label.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
@@ -32,7 +35,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def button(text, options = {}, &block)
|
def button(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
options[:theme] = current_theme
|
||||||
|
_element = Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
@@ -40,7 +44,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def edit_line(text, options = {}, &block)
|
def edit_line(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = EditLine.new(text, options, block)
|
options[:theme] = current_theme
|
||||||
|
_element = Element::EditLine.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
@@ -48,7 +53,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def toggle_button(options = {}, &block)
|
def toggle_button(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = ToggleButton.new(options, block)
|
options[:theme] = current_theme
|
||||||
|
_element = Element::ToggleButton.new(options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
@@ -56,7 +62,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def check_box(text, options = {}, &block)
|
def check_box(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = CheckBox.new(text, options, block)
|
options[:theme] = current_theme
|
||||||
|
_element = Element::CheckBox.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
@@ -64,19 +71,32 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def image(path, options = {}, &block)
|
def image(path, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
_element = Image.new(path, options, block)
|
options[:theme] = current_theme
|
||||||
|
_element = Element::Image.new(path, options, block)
|
||||||
|
@containers.last.add(_element)
|
||||||
|
|
||||||
|
return _element
|
||||||
|
end
|
||||||
|
|
||||||
|
def progress(options = {}, &block)
|
||||||
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
|
_element = Element::Progress.new(options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
return _element
|
return _element
|
||||||
end
|
end
|
||||||
|
|
||||||
def background(color = Gosu::Color::NONE)
|
def background(color = Gosu::Color::NONE)
|
||||||
@containers.last.background = color
|
@containers.last.style.background = color
|
||||||
end
|
end
|
||||||
|
|
||||||
# Foreground color, e.g. Text
|
def theme(theme)
|
||||||
def color(color)
|
@containers.last.options[:theme] = theme
|
||||||
@containers.last.color(color)
|
end
|
||||||
|
|
||||||
|
def current_theme
|
||||||
|
@containers.last.options[:theme]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1,88 +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, @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
|
|
||||||
@background_canvas.background = default(:active, :background)
|
|
||||||
@text.color = default(:active, :color)
|
|
||||||
else
|
|
||||||
@background_canvas.background = default(:hover, :background)
|
|
||||||
@text.color = default(:hover, :color)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def leave(sender)
|
|
||||||
end
|
|
||||||
|
|
||||||
def blur(sender)
|
|
||||||
@focus = false
|
|
||||||
@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
|
|
||||||
|
|
||||||
@width = default(:width)
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@text_input.text
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -5,99 +5,93 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :x, :y, :z, :enabled
|
attr_accessor :x, :y, :z, :enabled
|
||||||
attr_reader :width, :height, :parent, :options, :event_handler, :background_canvas, :border_canvas
|
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||||
|
|
||||||
attr_reader :border_thickness, :border_thickness_left, :border_thickness_right, :border_thickness_top, :border_thickness_bottom
|
|
||||||
attr_reader :border_color, :border_color_left, :border_color_right, :border_color_top, :border_color_bottom
|
|
||||||
|
|
||||||
attr_reader :padding, :padding_left, :padding_right, :padding_top, :padding_bottom
|
|
||||||
attr_reader :margin, :margin_left, :margin_right, :margin_top, :margin_bottom
|
|
||||||
|
|
||||||
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.merge(options)
|
options = theme_defaults(options)
|
||||||
@options = options
|
@options = options
|
||||||
@block = block
|
@block = block
|
||||||
|
|
||||||
@style = Style.new(options)
|
@focus = false
|
||||||
@focus = false
|
@enabled = true
|
||||||
@background_canvas = Background.new
|
@visible = true
|
||||||
@border_canvas = BorderCanvas.new(element: self)
|
|
||||||
|
|
||||||
@x = default(:x)
|
@style = Style.new(options)
|
||||||
@y = default(:y)
|
|
||||||
@z = default(:z)
|
@x = @style.x
|
||||||
|
@y = @style.y
|
||||||
|
@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
|
||||||
|
|
||||||
@width = default(:width) || $window.width
|
@style.width = default(:width) || nil
|
||||||
@height = default(:height) || $window.height
|
@style.height = default(:height) || nil
|
||||||
|
|
||||||
set_border_thickness(default(:border_thickness))
|
@style.background_canvas = Background.new
|
||||||
|
@style.border_canvas = BorderCanvas.new(element: self)
|
||||||
|
|
||||||
set_padding(default(:padding))
|
stylize
|
||||||
|
|
||||||
set_margin(default(:margin))
|
|
||||||
|
|
||||||
set_background(default(:background))
|
|
||||||
set_border_color(default(:border_color))
|
|
||||||
|
|
||||||
raise "#{self.class} 'x' must be a number" unless @x.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'y' must be a number" unless @y.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'z' must be a number" unless @z.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'width' must be a number" unless @width.is_a?(Numeric) || @width.nil?
|
|
||||||
raise "#{self.class} 'height' must be a number" unless @height.is_a?(Numeric) || @height.nil?
|
|
||||||
raise "#{self.class} 'options' must be a Hash" unless @options.is_a?(Hash)
|
|
||||||
|
|
||||||
# raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric)
|
|
||||||
|
|
||||||
@enabled = true
|
|
||||||
|
|
||||||
default_events
|
default_events
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def stylize
|
||||||
|
set_border_thickness(@style.border_thickness)
|
||||||
|
|
||||||
|
set_padding(@style.padding)
|
||||||
|
|
||||||
|
set_margin(@style.margin)
|
||||||
|
|
||||||
|
set_background(@style.background)
|
||||||
|
set_border_color(@style.border_color)
|
||||||
|
end
|
||||||
|
|
||||||
def set_background(background)
|
def set_background(background)
|
||||||
@background = background
|
@style.background = background
|
||||||
@background_canvas.background = background
|
@style.background_canvas.background = background
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_border_thickness(border_thickness)
|
def set_border_thickness(border_thickness)
|
||||||
@border_thickness = border_thickness
|
@style.border_thickness = border_thickness
|
||||||
|
|
||||||
@border_thickness_left = default(:border_thickness_left) || @border_thickness
|
@style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
|
||||||
@border_thickness_right = default(:border_thickness_right) || @border_thickness
|
@style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
|
||||||
@border_thickness_top = default(:border_thickness_top) || @border_thickness
|
@style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
|
||||||
@border_thickness_bottom = default(:border_thickness_bottom) || @border_thickness
|
@style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_border_color(color)
|
def set_border_color(color)
|
||||||
@border_color = color
|
@style.border_color = color
|
||||||
|
|
||||||
@border_color_left = default(:border_color_left) || @border_color
|
@style.border_color_left = default(:border_color_left) || @style.border_color
|
||||||
@border_color_right = default(:border_color_right) || @border_color
|
@style.border_color_right = default(:border_color_right) || @style.border_color
|
||||||
@border_color_top = default(:border_color_top) || @border_color
|
@style.border_color_top = default(:border_color_top) || @style.border_color
|
||||||
@border_color_bottom = default(:border_color_bottom) || @border_color
|
@style.border_color_bottom = default(:border_color_bottom) || @style.border_color
|
||||||
|
|
||||||
@border_canvas.color = color
|
@style.border_canvas.color = color
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_padding(padding)
|
def set_padding(padding)
|
||||||
@padding = padding
|
@style.padding = padding
|
||||||
|
|
||||||
@padding_left = default(:padding_left) || @padding
|
@style.padding_left = default(:padding_left) || @style.padding
|
||||||
@padding_right = default(:padding_right) || @padding
|
@style.padding_right = default(:padding_right) || @style.padding
|
||||||
@padding_top = default(:padding_top) || @padding
|
@style.padding_top = default(:padding_top) || @style.padding
|
||||||
@padding_bottom = default(:padding_bottom) || @padding
|
@style.padding_bottom = default(:padding_bottom) || @style.padding
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_margin(margin)
|
def set_margin(margin)
|
||||||
@margin = margin
|
@style.margin = margin
|
||||||
|
|
||||||
@margin_left = default(:margin_left) || @margin
|
@style.margin_left = default(:margin_left) || @style.margin
|
||||||
@margin_right = default(:margin_right) || @margin
|
@style.margin_right = default(:margin_right) || @style.margin
|
||||||
@margin_top = default(:margin_top) || @margin
|
@style.margin_top = default(:margin_top) || @style.margin
|
||||||
@margin_bottom = default(:margin_bottom) || @margin
|
@style.margin_bottom = default(:margin_bottom) || @style.margin
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_events
|
def default_events
|
||||||
@@ -122,9 +116,30 @@ module CyberarmEngine
|
|||||||
@enabled
|
@enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def visible?
|
||||||
|
@visible
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle
|
||||||
|
@visible = !@visible
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@visible = true
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def hide
|
||||||
|
@visible = false
|
||||||
|
root.gui_state.request_recalculate
|
||||||
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@background_canvas.draw
|
return unless @visible
|
||||||
@border_canvas.draw
|
|
||||||
|
@style.background_canvas.draw
|
||||||
|
@style.border_canvas.draw
|
||||||
render
|
render
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -146,52 +161,100 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
(@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right)
|
if visible?
|
||||||
|
inner_width + @width
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def outer_width
|
def outer_width
|
||||||
@margin_left + width + @margin_right
|
@style.margin_left + width + @style.margin_right
|
||||||
|
end
|
||||||
|
|
||||||
|
def inner_width
|
||||||
|
(@style.border_thickness_left + @style.padding_left) + (@style.padding_right + @style.border_thickness_right)
|
||||||
end
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
(@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom)
|
if visible?
|
||||||
|
inner_height + @height
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def outer_height
|
def outer_height
|
||||||
@margin_top + height + @margin_bottom
|
@style.margin_top + height + @style.margin_bottom
|
||||||
end
|
end
|
||||||
|
|
||||||
def style(hash)
|
def inner_height
|
||||||
if hash
|
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||||
@style.set(hash)
|
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(:"#{dimension}") * size
|
||||||
|
else
|
||||||
|
size
|
||||||
|
end
|
||||||
else
|
else
|
||||||
@style.hash
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def background=(_background)
|
def background=(_background)
|
||||||
@background_canvas.background=(_background)
|
@style.background_canvas.background=(_background)
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_background
|
def update_background
|
||||||
@background_canvas.x = @x
|
@style.background_canvas.x = @x
|
||||||
@background_canvas.y = @y
|
@style.background_canvas.y = @y
|
||||||
@background_canvas.z = @z
|
@style.background_canvas.z = @z
|
||||||
@background_canvas.width = width
|
@style.background_canvas.width = width
|
||||||
@background_canvas.height = height
|
@style.background_canvas.height = height
|
||||||
|
|
||||||
@background_canvas.update
|
@style.background_canvas.update
|
||||||
|
|
||||||
@border_canvas.update
|
@style.border_canvas.update
|
||||||
|
end
|
||||||
|
|
||||||
|
def root
|
||||||
|
unless @root && @root.parent.nil?
|
||||||
|
@root = parent
|
||||||
|
|
||||||
|
loop do
|
||||||
|
if @root.parent.nil?
|
||||||
|
break
|
||||||
|
else
|
||||||
|
@root = @root.parent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@root
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_root?
|
||||||
|
@gui_state != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
raise "#{self.class}#recalculate was not overridden!"
|
raise "#{self.class}#recalculate was not overridden!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reposition
|
||||||
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
raise "#{self.class}#value was not overridden!"
|
raise "#{self.class}#value was not overridden!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def value=(value)
|
||||||
|
raise "#{self.class}#value= was not overridden!"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
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
|
||||||
162
lib/cyberarm_engine/ui/elements/container.rb
Normal file
162
lib/cyberarm_engine/ui/elements/container.rb
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
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 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
|
||||||
103
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
103
lib/cyberarm_engine/ui/elements/edit_line.rb
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
@caret_last_interval = Gosu.milliseconds
|
||||||
|
@show_caret = true
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
@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
|
||||||
@@ -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]
|
@containers = [@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,42 +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(@border_thickness_left + @padding_left + @x, @border_thickness_top + @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
|
|
||||||
@width = @image.width * @scale_x
|
|
||||||
@height = @image.height * @scale_y
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,34 +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
|
|
||||||
@width = @text.width.round
|
|
||||||
@height= @text.height.round
|
|
||||||
|
|
||||||
@text.x = @border_thickness_left + @padding_left + @x
|
|
||||||
@text.y = @border_thickness_top + @padding_top + @y
|
|
||||||
@text.z = @z + 3
|
|
||||||
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@text.text
|
|
||||||
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,15 +1,37 @@
|
|||||||
|
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 hash
|
def method_missing(method, *args, &block)
|
||||||
@hash
|
if method.to_s.end_with?("=")
|
||||||
end
|
raise "Did not expect more than 1 argument" if args.size > 1
|
||||||
|
return @hash[method.to_s.sub("=", "").to_sym] = args.first
|
||||||
|
|
||||||
def set(hash)
|
elsif args.size == 0
|
||||||
@hash.merge!(hash)
|
return @hash[method]
|
||||||
|
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Did not expect arguments"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -9,21 +9,40 @@ module CyberarmEngine
|
|||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def theme_defaults
|
def theme_defaults(options)
|
||||||
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
||||||
|
_theme = THEME
|
||||||
|
_theme = _theme.merge(options[:theme]) if options[:theme]
|
||||||
|
_theme.delete(:theme) if options[:theme]
|
||||||
|
|
||||||
hash = {}
|
hash = {}
|
||||||
class_names = self.class.ancestors
|
class_names = self.class.ancestors
|
||||||
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
||||||
|
|
||||||
class_names.each do |klass|
|
class_names.each do |klass|
|
||||||
next unless data = THEME.dig(klass)
|
next unless data = _theme.dig(klass)
|
||||||
data.each do |key, value|
|
data.each do |key, value|
|
||||||
hash.merge!(data)
|
hash.merge!(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
hash
|
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 = {
|
||||||
@@ -46,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)],
|
||||||
@@ -71,7 +90,7 @@ module CyberarmEngine
|
|||||||
caret_interval: 500,
|
caret_interval: 500,
|
||||||
},
|
},
|
||||||
|
|
||||||
Image: {
|
Image: { # < Element
|
||||||
retro: false
|
retro: false
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -85,7 +104,16 @@ module CyberarmEngine
|
|||||||
|
|
||||||
ToggleButton: { # < Button
|
ToggleButton: { # < Button
|
||||||
checkmark: "√"
|
checkmark: "√"
|
||||||
|
},
|
||||||
|
|
||||||
|
Progress: { # < Element
|
||||||
|
width: 250,
|
||||||
|
height: 36,
|
||||||
|
background: 0xff111111,
|
||||||
|
fraction_background: [0xffc75e61, 0xffe26623],
|
||||||
|
border_thickness: 4,
|
||||||
|
border_color: [0xffd59674, 0xffff8746]
|
||||||
}
|
}
|
||||||
}
|
}.freeze
|
||||||
end
|
end
|
||||||
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
|
|
||||||
|
|
||||||
@width = @text.textobject.text_width(@options[:checkmark])
|
|
||||||
update_background
|
|
||||||
end
|
|
||||||
|
|
||||||
def value
|
|
||||||
@toggled
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
199
lib/cyberarm_engine/vector.rb
Normal file
199
lib/cyberarm_engine/vector.rb
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class Vector
|
||||||
|
def self.up
|
||||||
|
Vector.new(0, 1, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
Vector.new(0, -1, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.left
|
||||||
|
Vector.new(-1, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.right
|
||||||
|
Vector.new(1, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.forward
|
||||||
|
Vector.new(0, 0, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.backward
|
||||||
|
Vector.new(0, 0, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||||
|
@x, @y, @z, @weight = x, y, z, weight
|
||||||
|
end
|
||||||
|
|
||||||
|
def x; @x; end
|
||||||
|
def x=(n); @x = n; end
|
||||||
|
|
||||||
|
def y; @y; end
|
||||||
|
def y=(n); @y = n; end
|
||||||
|
|
||||||
|
def z; @z; end
|
||||||
|
def z=(n); @z = n; end
|
||||||
|
|
||||||
|
def weight; @weight; end
|
||||||
|
def weight=(n); @weight = n; end
|
||||||
|
|
||||||
|
alias w weight
|
||||||
|
alias w= weight=
|
||||||
|
|
||||||
|
def ==(other)
|
||||||
|
if other.is_a?(Numeric)
|
||||||
|
@x == other &&
|
||||||
|
@y == other &&
|
||||||
|
@z == other &&
|
||||||
|
@weight == other
|
||||||
|
else
|
||||||
|
@x == other.x &&
|
||||||
|
@y == other.y &&
|
||||||
|
@z == other.z &&
|
||||||
|
@weight == other.weight
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Performs math operation, excluding @weight
|
||||||
|
private def operator(function, other)
|
||||||
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
@x.send(:"#{function}", other),
|
||||||
|
@y.send(:"#{function}", other),
|
||||||
|
@z.send(:"#{function}", other)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
@x.send(:"#{function}", other.x),
|
||||||
|
@y.send(:"#{function}", other.y),
|
||||||
|
@z.send(:"#{function}", other.z)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adds Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def +(other)
|
||||||
|
operator("+", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Subtracts Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def -(other)
|
||||||
|
operator("-", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Multiplies Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def *(other)
|
||||||
|
operator("*", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Divides Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def /(other)
|
||||||
|
# Duplicated to protect from DivideByZero
|
||||||
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
(@x == 0 ? 0 : @x / other),
|
||||||
|
(@y == 0 ? 0 : @y / other),
|
||||||
|
(@z == 0 ? 0 : @z / other)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
(@x == 0 ? 0 : @x / other.x),
|
||||||
|
(@y == 0 ? 0 : @y / other.y),
|
||||||
|
(@z == 0 ? 0 : @z / other.z)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dot(other)
|
||||||
|
product = 0
|
||||||
|
|
||||||
|
a = self.to_a
|
||||||
|
b = other.to_a
|
||||||
|
|
||||||
|
3.times do |i|
|
||||||
|
product = product + (a[i] * b[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
return product
|
||||||
|
end
|
||||||
|
|
||||||
|
def cross(other)
|
||||||
|
a = self.to_a
|
||||||
|
b = other.to_a
|
||||||
|
|
||||||
|
Vector.new(
|
||||||
|
b[2] * a[1] - b[1] * a[2],
|
||||||
|
b[0] * a[2] - b[2] * a[0],
|
||||||
|
b[1] * a[0] - b[0] * a[1]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns degrees
|
||||||
|
def angle(other)
|
||||||
|
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
||||||
|
end
|
||||||
|
|
||||||
|
# returns magnitude of Vector, ignoring #weight
|
||||||
|
def magnitude
|
||||||
|
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalized
|
||||||
|
mag = magnitude
|
||||||
|
self / Vector.new(mag, mag, mag)
|
||||||
|
end
|
||||||
|
|
||||||
|
def direction
|
||||||
|
# z is pitch
|
||||||
|
# y is yaw
|
||||||
|
# x is roll
|
||||||
|
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
|
_y = Math.sin(@z.degrees_to_radians)
|
||||||
|
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
|
|
||||||
|
Vector.new(_x, _y, _z)
|
||||||
|
end
|
||||||
|
|
||||||
|
def inverse
|
||||||
|
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||||
|
end
|
||||||
|
|
||||||
|
def sum
|
||||||
|
@x + @y + @z
|
||||||
|
end
|
||||||
|
|
||||||
|
def lerp(other, factor)
|
||||||
|
(self - other) * factor.clamp(0.0, 1.0)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2D distance using X and Y
|
||||||
|
def distance(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2D distance using X and Z
|
||||||
|
def gl_distance2d(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 3D distance using X, Y, and Z
|
||||||
|
def distance3d(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_a
|
||||||
|
[@x, @y, @z, @weight]
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{x: @x, y: @y, z: @z, weight: @weight}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev"
|
NAME = "InDev"
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.12.1"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user