mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 13:12:34 +00:00
Compare commits
114 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9068a418c0 | |||
| 7ca6e1bc58 | |||
| a70f260bc6 | |||
| 8002708695 | |||
| 676545f3c7 | |||
| 4b417f9ab7 | |||
| a70c106387 | |||
| f662fabc56 | |||
| abb989f842 | |||
| 1e0d2004b5 | |||
| 92dd63dc1d | |||
| 20970e5aa9 | |||
| 76eb1a85d5 | |||
| e9d75d17bf | |||
| e8bb2cac17 | |||
| 29fbac7140 | |||
| d050f63c2b | |||
| af24fc8690 | |||
| f63b893c70 | |||
| 5adc27feef | |||
| 62636158f7 | |||
| 2179e11ba1 | |||
| 1ac5e0695e | |||
| 732dc2c957 | |||
| 886680ab31 | |||
| 0268a8a5fb | |||
| 1c22c36d6b | |||
| 3e56d48556 | |||
| 0c9874c53b | |||
| 164a46c1fb | |||
| 26dd688124 | |||
| 2447dde1af | |||
| d3ff7c1fc1 | |||
| 2d57d62bc2 | |||
| fb36436d79 | |||
| 246e0c54b2 | |||
| d02c001989 | |||
| 3ba635f157 | |||
| 695c77b183 | |||
| b06ceaabce | |||
| ce64b8a205 | |||
| da4188764c | |||
| d01e91c3fd | |||
| 041cfcccaa | |||
| d7dbcf8511 | |||
| 2a8e852b15 | |||
| d392d8249d | |||
| 5d7e2028b1 | |||
| d902e5d111 | |||
| 39964e5bd4 | |||
| 5b2a015421 | |||
| eb4d5d4d21 | |||
| c83b204447 | |||
| d0449c7b65 | |||
| a05ee57f6f | |||
| c3b227d6e7 | |||
| 7aaf37d43e | |||
| 0850336e55 | |||
| 8b6d7b6eb2 | |||
| 3226eb2bda | |||
| a98bb4ec82 | |||
| da5d740c6e | |||
| 4055f645f3 | |||
| 185ab000d6 | |||
| ed061c8408 | |||
| d8551c7428 | |||
| 0017c841dd | |||
| 8cedb40283 | |||
| 93a4e9a2b8 | |||
| a0c0180411 | |||
| 7f25cd49fe | |||
| 7e6a17fe9f | |||
| 0ea1e5c2ff | |||
| 8c34293307 | |||
| 8172dafa8c | |||
| 9a3caabc7e | |||
| 4b25d724b5 | |||
| a267695b1c | |||
| d76da62ee1 | |||
| 3a83e616d3 | |||
| 52940d1cfe | |||
| e59771e412 | |||
| b382bf6960 | |||
| a5ed650970 | |||
| 935d6e9178 | |||
| cc813c023f | |||
| 5f359a8313 | |||
| c67d6f9d26 | |||
| 7f97ec85dd | |||
| 89a54e90a1 | |||
| 48d2924c9b | |||
| 7cc733dee4 | |||
| 28c4acdedf | |||
| 57313a33a6 | |||
| 5d16500edd | |||
| 51dd3803fa | |||
| c6beab3e99 | |||
| 8ce594753e | |||
| ae44083cf2 | |||
| b25f76e124 | |||
| 8f9e671340 | |||
| bb482f4463 | |||
| 788d987da1 | |||
| 6fafe98008 | |||
| 002a01abfc | |||
| 6f7bac3880 | |||
| 993e59aa94 | |||
| e30ed7c6be | |||
| a172c24d97 | |||
| 003715f685 | |||
| 1458736f7d | |||
| a2e0c07c6a | |||
| 3ead2f5daf | |||
| 8f3d9ff193 |
8
.rubocop.yml
Normal file
8
.rubocop.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
Style/StringLiterals:
|
||||
EnforcedStyle: double_quotes
|
||||
|
||||
Metrics/MethodLength:
|
||||
Max: 40
|
||||
|
||||
Style/EmptyMethod:
|
||||
EnforcedStyle: expanded
|
||||
2
Gemfile
2
Gemfile
@@ -1,6 +1,6 @@
|
||||
source "https://rubygems.org"
|
||||
|
||||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
||||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
||||
|
||||
# Specify your gem's dependencies in cyberarm_engine.gemspec
|
||||
gemspec
|
||||
|
||||
39
README.md
39
README.md
@@ -1,8 +1,13 @@
|
||||
# CyberarmEngine
|
||||

|
||||
|
||||
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cyberarm_engine`. To experiment with that code, run `bin/console` for an interactive prompt.
|
||||
Yet Another Game Engine On Top Of Gosu
|
||||
|
||||
TODO: Delete this and the text above, and describe your gem
|
||||
## Features
|
||||
* [Shoes-like](http://shoesrb.com) GUI support
|
||||
* OpenGL Shader support (requires [opengl-bindings](https://github.com/vaiorabbit/ruby-opengl) gem)
|
||||
* Includes classes for handling Vectors, Rays, Bounding Boxes, and Transforms
|
||||
* GameState system
|
||||
* Monolithic GameObjects
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -22,7 +27,33 @@ Or install it yourself as:
|
||||
|
||||
## Usage
|
||||
|
||||
TODO: Write usage instructions here
|
||||
```ruby
|
||||
require "cyberarm_engine"
|
||||
|
||||
class Hello < CyberarmEngine::GuiState
|
||||
def setup
|
||||
background Gosu::Color::GRAY
|
||||
|
||||
stack do
|
||||
label "Hello World!"
|
||||
|
||||
button "close" do
|
||||
window.close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Window < CyberarmEngine::Window
|
||||
def setup
|
||||
self.show_cursor = true
|
||||
|
||||
push_state(Hello)
|
||||
end
|
||||
end
|
||||
|
||||
Window.new(width: 800, height: 600, fullscreen: false, resizable: true).show
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
|
||||
2
Rakefile
2
Rakefile
@@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |t|
|
||||
t.test_files = FileList["test/**/*_test.rb"]
|
||||
end
|
||||
|
||||
task :default => :test
|
||||
task default: :test
|
||||
|
||||
BIN
assets/textures/default.png
Normal file
BIN
assets/textures/default.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 681 B |
BIN
assets/textures/logo.png
Normal file
BIN
assets/textures/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
@@ -1,5 +1,4 @@
|
||||
|
||||
lib = File.expand_path("../lib", __FILE__)
|
||||
lib = File.expand_path("lib", __dir__)
|
||||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||
require "cyberarm_engine/version"
|
||||
|
||||
@@ -9,8 +8,8 @@ Gem::Specification.new do |spec|
|
||||
spec.authors = ["Cyberarm"]
|
||||
spec.email = ["matthewlikesrobots@gmail.com"]
|
||||
|
||||
spec.summary = %q{Make games quickly and easily with gosu}
|
||||
spec.description = %q{Yet another game making framework around gosu}
|
||||
spec.summary = "Make games quickly and easily with gosu"
|
||||
spec.description = "Yet another game making framework around gosu"
|
||||
spec.homepage = "https://github.com/cyberarm/cyberarm_engine"
|
||||
spec.license = "MIT"
|
||||
|
||||
@@ -21,16 +20,20 @@ Gem::Specification.new do |spec|
|
||||
"public gem pushes."
|
||||
end
|
||||
|
||||
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
||||
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
||||
f.match(%r{^(test|spec|features)/})
|
||||
end
|
||||
spec.bindir = "exe"
|
||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ["lib"]
|
||||
spec.require_paths = %w[lib assets]
|
||||
|
||||
spec.add_dependency "gosu", "~> 0.14.0"
|
||||
spec.add_dependency "clipboard", "~> 1.3.5"
|
||||
spec.add_dependency "excon", "~> 0.78.0"
|
||||
spec.add_dependency "gosu", "~> 1.1"
|
||||
spec.add_dependency "gosu_more_drawables", "~> 0.3"
|
||||
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 1.16"
|
||||
spec.add_development_dependency "rake", "~> 10.0"
|
||||
spec.add_development_dependency "bundler", "~> 2.2"
|
||||
spec.add_development_dependency "minitest", "~> 5.0"
|
||||
spec.add_development_dependency "rake", "~> 13.0"
|
||||
end
|
||||
|
||||
@@ -1,36 +1,65 @@
|
||||
require "gosu"
|
||||
CYBERARM_ENGINE_ROOT_PATH = File.expand_path("..", __dir__)
|
||||
|
||||
if ARGV.join.include?("--ffi-gosu")
|
||||
require File.expand_path("../../ffi-gosu/lib/gosu", __dir__)
|
||||
else
|
||||
require "gosu"
|
||||
end
|
||||
require "json"
|
||||
require "excon"
|
||||
require "gosu_more_drawables"
|
||||
require "clipboard"
|
||||
|
||||
require_relative "cyberarm_engine/version"
|
||||
require_relative "cyberarm_engine/stats"
|
||||
|
||||
require_relative "cyberarm_engine/common"
|
||||
|
||||
require_relative "cyberarm_engine/game_object"
|
||||
require_relative "cyberarm_engine/engine"
|
||||
require_relative "cyberarm_engine/window"
|
||||
|
||||
require_relative "cyberarm_engine/lib/bounding_box"
|
||||
require_relative "cyberarm_engine/lib/vector"
|
||||
require_relative "cyberarm_engine/bounding_box"
|
||||
require_relative "cyberarm_engine/vector"
|
||||
require_relative "cyberarm_engine/transform"
|
||||
require_relative "cyberarm_engine/ray"
|
||||
require_relative "cyberarm_engine/background"
|
||||
require_relative "cyberarm_engine/animator"
|
||||
|
||||
require_relative "cyberarm_engine/objects/text"
|
||||
require_relative "cyberarm_engine/objects/timer"
|
||||
require_relative "cyberarm_engine/text"
|
||||
require_relative "cyberarm_engine/timer"
|
||||
require_relative "cyberarm_engine/config_file"
|
||||
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
|
||||
require_relative "cyberarm_engine/ui/theme"
|
||||
require_relative "cyberarm_engine/ui/event"
|
||||
require_relative "cyberarm_engine/ui/style"
|
||||
require_relative "cyberarm_engine/ui/border_canvas"
|
||||
require_relative "cyberarm_engine/ui/element"
|
||||
require_relative "cyberarm_engine/ui/elements/label"
|
||||
require_relative "cyberarm_engine/ui/elements/text_block"
|
||||
require_relative "cyberarm_engine/ui/elements/button"
|
||||
require_relative "cyberarm_engine/ui/elements/toggle_button"
|
||||
require_relative "cyberarm_engine/ui/elements/list_box"
|
||||
require_relative "cyberarm_engine/ui/elements/edit_line"
|
||||
require_relative "cyberarm_engine/ui/elements/edit_box"
|
||||
require_relative "cyberarm_engine/ui/elements/image"
|
||||
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/radio"
|
||||
require_relative "cyberarm_engine/ui/elements/progress"
|
||||
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
require_relative "cyberarm_engine/ui/elements/slider"
|
||||
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
require_relative "cyberarm_engine/ui/gui_state"
|
||||
|
||||
require_relative "cyberarm_engine/model"
|
||||
require_relative "cyberarm_engine/model_cache"
|
||||
require_relative "cyberarm_engine/model/material"
|
||||
require_relative "cyberarm_engine/model/model_object"
|
||||
require_relative "cyberarm_engine/model/parser"
|
||||
require_relative "cyberarm_engine/model/parsers/wavefront_parser"
|
||||
require_relative "cyberarm_engine/model/parsers/collada_parser" if defined?(Nokogiri)
|
||||
|
||||
require_relative "cyberarm_engine/builtin/intro_state"
|
||||
|
||||
56
lib/cyberarm_engine/animator.rb
Normal file
56
lib/cyberarm_engine/animator.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
module CyberarmEngine
|
||||
class Animator
|
||||
def initialize(start_time:, duration:, from:, to:, tween: :linear, &block)
|
||||
@start_time = start_time
|
||||
@duration = duration
|
||||
@from = from.dup
|
||||
@to = to.dup
|
||||
@tween = tween
|
||||
@block = block
|
||||
end
|
||||
|
||||
def update
|
||||
@block.call(self, @from, @to) if @block
|
||||
end
|
||||
|
||||
def progress
|
||||
((Gosu.milliseconds - @start_time) / @duration.to_f).clamp(0.0, 1.0)
|
||||
end
|
||||
|
||||
def complete?
|
||||
progress >= 1.0
|
||||
end
|
||||
|
||||
def transition(from = @from, to = @to, tween = @tween)
|
||||
from + (to - from) * send("tween_#{tween}", progress)
|
||||
end
|
||||
|
||||
def color_transition(from = @from, to = @to, _tween = @tween)
|
||||
r = transition(from.red, to.red)
|
||||
g = transition(from.green, to.green)
|
||||
b = transition(from.blue, to.blue)
|
||||
a = transition(from.alpha, to.alpha)
|
||||
|
||||
Gosu::Color.rgba(r, g, b, a)
|
||||
end
|
||||
|
||||
def color_hsv_transition(from = @from, to = @to, tween = @tween)
|
||||
hue = transition(from.hue, to.hue, tween)
|
||||
saturation = transition(from.saturation, to.saturation, tween)
|
||||
value = transition(from.value, to.value, tween)
|
||||
alpha = transition(from.alpha, to.alpha, tween)
|
||||
|
||||
Gosu::Color.from_ahsv(alpha, hue, saturation, value)
|
||||
end
|
||||
|
||||
# NOTE: Use this for future reference? https://github.com/danro/easing-js/blob/master/easing.js
|
||||
|
||||
def tween_linear(t)
|
||||
t
|
||||
end
|
||||
|
||||
def tween_ease_in_out(t)
|
||||
(-0.5 * (Math.cos(Math::PI * t) - 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,14 @@
|
||||
require "gosu"
|
||||
|
||||
module CyberarmEngine
|
||||
class Background
|
||||
attr_accessor :x, :y, :z, :width, :height, :angle, :debug
|
||||
attr_reader :background
|
||||
|
||||
def initialize(x: 0, y: 0, z: 0, width: 0, height: 0, background: Gosu::Color::BLACK, angle: 0, debug: false)
|
||||
@x,@y,@z = x,y,z
|
||||
@width,@height = width,height
|
||||
@x = x
|
||||
@y = y
|
||||
@z = z
|
||||
@width = width
|
||||
@height = height
|
||||
@debug = debug
|
||||
|
||||
@paint = Paint.new(background)
|
||||
@@ -33,8 +35,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def update
|
||||
origin_x = (@x + (@width/2))
|
||||
origin_y = (@y + (@height/2))
|
||||
origin_x = (@x + (@width / 2))
|
||||
origin_y = (@y + (@height / 2))
|
||||
|
||||
points = [
|
||||
@top_left = Vector.new(@x, @y),
|
||||
@@ -49,8 +51,8 @@ module CyberarmEngine
|
||||
|
||||
# 90 is up here, while gosu uses 0 for up.
|
||||
radians = (@angle + 90).gosu_to_radians
|
||||
vector.x = (@x + (@width/2)) + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
|
||||
vector.y = (@y + (@height/2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
|
||||
vector.x = (@x + (@width / 2)) + ((temp_x * Math.cos(radians)) - (temp_y * Math.sin(radians)))
|
||||
vector.y = (@y + (@height / 2)) + ((temp_x * Math.sin(radians)) + (temp_y * Math.cos(radians)))
|
||||
end
|
||||
|
||||
# [
|
||||
@@ -69,11 +71,11 @@ module CyberarmEngine
|
||||
a = la.x - lb.x
|
||||
b = la.y - lb.y
|
||||
c = Gosu.distance(la.x, la.y, lb.x, lb.y)
|
||||
p a,b,c
|
||||
d = (a * point.x + b * point.y + c).abs / (Math.sqrt(a * a + b * b))
|
||||
p a, b, c
|
||||
d = (a * point.x + b * point.y + c).abs / Math.sqrt(a * a + b * b)
|
||||
puts "Distance: #{d}"
|
||||
exit!
|
||||
return d
|
||||
d
|
||||
end
|
||||
|
||||
def debug_outline
|
||||
@@ -119,6 +121,7 @@ module CyberarmEngine
|
||||
|
||||
class Paint
|
||||
attr_accessor :top_left, :top_right, :bottom_left, :bottom_right
|
||||
|
||||
def initialize(background)
|
||||
set(background)
|
||||
end
|
||||
@@ -126,7 +129,6 @@ module CyberarmEngine
|
||||
def set(background)
|
||||
@background = background
|
||||
|
||||
|
||||
if background.is_a?(Numeric)
|
||||
@top_left = background
|
||||
@top_right = background
|
||||
@@ -169,9 +171,9 @@ end
|
||||
|
||||
# Add <=> method to support Range based gradients
|
||||
module Gosu
|
||||
class Color
|
||||
def <=>(other)
|
||||
self
|
||||
end
|
||||
end
|
||||
class Color
|
||||
def <=>(_other)
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
125
lib/cyberarm_engine/background_nine_slice.rb
Normal file
125
lib/cyberarm_engine/background_nine_slice.rb
Normal file
@@ -0,0 +1,125 @@
|
||||
module CyberarmEngine
|
||||
class BackgroundNineSlice
|
||||
include CyberarmEngine::Common
|
||||
attr_accessor :x, :y, :z, :width, :height
|
||||
|
||||
def initialize(image_path:, x: 0, y: 0, z: 0, width: 64, height: 64, mode: :tiled, left: 4, top: 4, right: 56, bottom: 56)
|
||||
@image = get_image(image_path)
|
||||
|
||||
@x = x
|
||||
@y = y
|
||||
@z = z
|
||||
|
||||
@width = width
|
||||
@height = height
|
||||
|
||||
@mode = mode
|
||||
|
||||
@left = left
|
||||
@top = top
|
||||
@right = right
|
||||
@bottom = bottom
|
||||
|
||||
nine_slice
|
||||
end
|
||||
|
||||
def nine_slice
|
||||
@segment_top_left = Gosu.render(@left, @top) { @image.draw(0, 0, 0) }
|
||||
@segment_top_right = Gosu.render(@image.width - @right, @top) { @image.draw(-@right, 0, 0) }
|
||||
|
||||
@segment_left = Gosu.render(@left, @bottom - @top) { @image.draw(0, -@top, 0) }
|
||||
@segment_right = Gosu.render(@image.width - @right, @bottom - @top) { @image.draw(-@right, -@top, 0) }
|
||||
|
||||
@segment_bottom_left = Gosu.render(@left, @image.height - @bottom) { @image.draw(0, -@bottom, 0) }
|
||||
@segment_bottom_right = Gosu.render(@image.width - @right, @image.height - @bottom) { @image.draw(-@right, -@bottom, 0) }
|
||||
|
||||
@segment_top = Gosu.render(@right - @left, @top) { @image.draw(-@left, 0, 0) }
|
||||
@segment_bottom = Gosu.render(@right - @left, @image.height - @bottom) { @image.draw(-@left, -@bottom, 0) }
|
||||
|
||||
@segment_middle = Gosu.render(@right - @left, @bottom - @top) { @image.draw(-@left, -@top, 0) }
|
||||
end
|
||||
|
||||
def cx
|
||||
@x + @left
|
||||
end
|
||||
|
||||
def cy
|
||||
@y + @top
|
||||
end
|
||||
|
||||
def cwidth
|
||||
@cx - @width
|
||||
end
|
||||
|
||||
def cheight
|
||||
@cy - @height
|
||||
end
|
||||
|
||||
def width_scale
|
||||
width_scale = (@width - (@left + (@image.width - @right))).to_f / (@right - @left)
|
||||
end
|
||||
|
||||
def height_scale
|
||||
height_scale = (@height - (@top + (@image.height - @bottom))).to_f / (@bottom - @top)
|
||||
end
|
||||
|
||||
def draw
|
||||
@mode == :tiled ? draw_tiled : draw_stretched
|
||||
end
|
||||
|
||||
def draw_stretched
|
||||
@segment_top_left.draw(@x, @y, @z)
|
||||
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale) # SCALE X
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
|
||||
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale) # SCALE Y
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z)
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale) # SCALE X
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z)
|
||||
@segment_left.draw(@x, @y + @top, @z, 1, height_scale) # SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale) # SCALE X and SCALE Y
|
||||
end
|
||||
|
||||
def draw_tiled
|
||||
@segment_top_left.draw(@x, @y, @z)
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
|
||||
width_scale.ceil.times do |i|
|
||||
@segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
|
||||
|
||||
Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
|
||||
height_scale.ceil.times do |i|
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z) # SCALE Y
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z)
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
|
||||
width_scale.ceil.times do |i|
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z)
|
||||
|
||||
Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
|
||||
height_scale.ceil.times do |i|
|
||||
@segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z) # SCALE Y
|
||||
end
|
||||
end
|
||||
|
||||
Gosu.clip_to(@x + @segment_top_left.width, @y + @segment_top.height, @width - (@segment_left.width + @segment_right.width), @height - (@segment_top.height + @segment_bottom.height)) do
|
||||
height_scale.ceil.times do |y|
|
||||
width_scale.ceil.times do |x|
|
||||
@segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z) # SCALE X and SCALE Y
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -23,7 +23,7 @@ module CyberarmEngine
|
||||
|
||||
def ==(other)
|
||||
@min == other.min &&
|
||||
@max == other.max
|
||||
@max == other.max
|
||||
end
|
||||
|
||||
# returns a new bounding box that includes both bounding boxes
|
||||
@@ -37,7 +37,7 @@ module CyberarmEngine
|
||||
temp.max.y = [@max.y, other.max.y].max
|
||||
temp.max.z = [@max.z, other.max.z].max
|
||||
|
||||
return temp
|
||||
temp
|
||||
end
|
||||
|
||||
# returns the difference between both bounding boxes
|
||||
@@ -46,27 +46,39 @@ module CyberarmEngine
|
||||
temp.min = @min - other.min
|
||||
temp.max = @max - other.max
|
||||
|
||||
return temp
|
||||
temp
|
||||
end
|
||||
|
||||
# returns whether both bounding boxes intersect
|
||||
# returns whether bounding box intersects other
|
||||
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)
|
||||
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
|
||||
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
|
||||
# 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.y.between?(@min.y, @max.y) &&
|
||||
vector.z.between?(@min.z, @max.z)
|
||||
(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
|
||||
@@ -87,44 +99,44 @@ module CyberarmEngine
|
||||
|
||||
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.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
|
||||
temp.max.y = @max.y.to_f * entity.scale
|
||||
temp.max.z = @max.z.to_f * entity.scale
|
||||
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
|
||||
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.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 + 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
|
||||
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
|
||||
temp
|
||||
end
|
||||
|
||||
def +(other)
|
||||
box = BoundingBox.new
|
||||
box.min = self.min + other.min
|
||||
box.min = self.max + other.max
|
||||
box.min = min + other.min
|
||||
box.min = max + other.max
|
||||
|
||||
return box
|
||||
box
|
||||
end
|
||||
|
||||
def -(other)
|
||||
box = BoundingBox.new
|
||||
box.min = self.min - other.min
|
||||
box.min = self.max - other.max
|
||||
box.min = min - other.min
|
||||
box.min = max - other.max
|
||||
|
||||
return box
|
||||
box
|
||||
end
|
||||
|
||||
def sum
|
||||
@@ -135,4 +147,4 @@ module CyberarmEngine
|
||||
BoundingBox.new(@min.x, @min.y, @min.z, @max.x, @max.y, @max.z)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
128
lib/cyberarm_engine/builtin/intro_state.rb
Normal file
128
lib/cyberarm_engine/builtin/intro_state.rb
Normal file
@@ -0,0 +1,128 @@
|
||||
module CyberarmEngine
|
||||
class IntroState < CyberarmEngine::GameState
|
||||
def setup
|
||||
@display_width = 800
|
||||
@display_height = 600
|
||||
|
||||
@title_size = 56
|
||||
@caption_size = 24
|
||||
|
||||
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222)
|
||||
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222)
|
||||
|
||||
@spacer_width = 256
|
||||
@spacer_height = 6
|
||||
@padding = 6
|
||||
|
||||
@cyberarm_engine_logo = get_image "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/logo.png"
|
||||
|
||||
@gosu_logo = generate_proxy("Gosu", "Game Library", 0xff_111111)
|
||||
@ruby_logo = generate_proxy("Ruby", "Programming Language", 0xff_880000)
|
||||
@opengl_logo = generate_proxy("OpenGL", "Graphics API", 0xff_5586a4) if defined?(OpenGL)
|
||||
|
||||
base_time = Gosu.milliseconds
|
||||
|
||||
@animators = [
|
||||
Animator.new(start_time: base_time += 1000, duration: 100, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += -500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time += 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
Animator.new(start_time: base_time + 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out),
|
||||
|
||||
Animator.new(start_time: Gosu.milliseconds + 250, duration: 500, from: 0.0, to: 1.0, tween: :ease_in_out) # CyberarmEngine LOGO
|
||||
]
|
||||
|
||||
@born_time = Gosu.milliseconds
|
||||
@continue_after = 5_000
|
||||
end
|
||||
|
||||
def draw
|
||||
Gosu.draw_rect(0, 0, window.width, window.height, 0xff_222222)
|
||||
|
||||
scale = (@display_width - @padding * 2).to_f / @cyberarm_engine_logo.width * @animators.last.transition
|
||||
|
||||
@cyberarm_engine_logo.draw_rot(
|
||||
window.width / 2,
|
||||
(window.height) / 2 - @cyberarm_engine_logo.height / 2 - @padding * 2,
|
||||
2,
|
||||
0,
|
||||
0.5,
|
||||
0.5,
|
||||
scale,
|
||||
scale
|
||||
)
|
||||
|
||||
Gosu.draw_rect(
|
||||
window.width / 2 - (@display_width / 2 + @padding),
|
||||
window.height / 2 - @spacer_height / 2,
|
||||
@display_width + @padding,
|
||||
@spacer_height * @animators[0].transition,
|
||||
Gosu::Color::WHITE
|
||||
)
|
||||
|
||||
@title.x = window.width / 2 - @title.width / 2
|
||||
@title.y = (window.height / 2 + (@spacer_height / 2) + @padding) * @animators[1].transition
|
||||
@title.text = "Powered By"
|
||||
|
||||
Gosu.clip_to(0, window.height / 2 + (@spacer_height / 2), window.width, @title.height) do
|
||||
@title.draw
|
||||
end
|
||||
|
||||
y = @title.y + @title.height * 2
|
||||
|
||||
Gosu.clip_to(0, y, window.width, @gosu_logo.height) do
|
||||
Gosu.translate(@opengl_logo.nil? ? @ruby_logo.width / 2 : 0, 0) do
|
||||
@gosu_logo.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2 - (@ruby_logo.width - @padding),
|
||||
y * @animators[2].transition,
|
||||
2
|
||||
)
|
||||
@ruby_logo.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2,
|
||||
y * @animators[3].transition,
|
||||
2
|
||||
)
|
||||
@opengl_logo&.draw(
|
||||
window.width.to_f / 2 - @ruby_logo.width / 2 + (@ruby_logo.width - @padding),
|
||||
y * @animators[4].transition,
|
||||
2
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@animators.each(&:update)
|
||||
|
||||
return unless Gosu.milliseconds - @born_time >= @continue_after
|
||||
|
||||
pop_state
|
||||
push_state(@options[:forward], @options[:forward_options] || {}) if @options[:forward]
|
||||
end
|
||||
|
||||
def button_down(_id)
|
||||
@continue_after = 0
|
||||
end
|
||||
|
||||
def generate_proxy(title, caption, color_hint)
|
||||
@title.text = title
|
||||
@caption.text = caption
|
||||
|
||||
width = @spacer_width + 2 * @padding
|
||||
height = @title_size + @caption_size + @spacer_height + 2 * @padding + @spacer_height
|
||||
|
||||
Gosu.record(width.ceil, height.ceil) do
|
||||
@title.x = (width - @padding * 2) / 2 - @title.width / 2
|
||||
@title.y = @padding
|
||||
@title.draw
|
||||
|
||||
Gosu.draw_rect(0, @padding + @title_size + @padding, @spacer_width, @spacer_height, Gosu::Color::WHITE)
|
||||
Gosu.draw_rect(1, @padding + @title_size + @padding + 1, @spacer_width - 2, @spacer_height - 2, color_hint)
|
||||
|
||||
@caption.x = (width - @padding * 2) / 2 - @caption.width / 2
|
||||
@caption.y = @padding + @title_size + @padding + @spacer_height + @padding
|
||||
@caption.draw
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
4
lib/cyberarm_engine/cache.rb
Normal file
4
lib/cyberarm_engine/cache.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module CyberarmEngine
|
||||
module Cache
|
||||
end
|
||||
end
|
||||
121
lib/cyberarm_engine/cache/download_manager.rb
vendored
Normal file
121
lib/cyberarm_engine/cache/download_manager.rb
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
module CyberarmEngine
|
||||
module Cache
|
||||
class DownloadManager
|
||||
attr_reader :downloads
|
||||
|
||||
def initialize(max_parallel_downloads: 4)
|
||||
@max_parallel_downloads = max_parallel_downloads
|
||||
@downloads = []
|
||||
end
|
||||
|
||||
def download(url:, save_as: nil, &callback)
|
||||
uri = URI(url)
|
||||
save_as ||= "filename_path" # TODO: if no save_as path is provided, then get one from the Cache controller
|
||||
|
||||
@downloads << Download.new(uri: uri, save_as: save_as, callback: callback)
|
||||
end
|
||||
|
||||
def status
|
||||
if active_downloads > 0
|
||||
:busy
|
||||
else
|
||||
:idle
|
||||
end
|
||||
end
|
||||
|
||||
def progress
|
||||
remaining_bytes = @downloads.map { |d| d.remaining_bytes }.sum
|
||||
total_bytes = @downloads.map { |d| d.total_bytes }.sum
|
||||
|
||||
v = 1.0 - (remaining_bytes.to_f / total_bytes)
|
||||
return 0.0 if v.nan?
|
||||
|
||||
v
|
||||
end
|
||||
|
||||
def active_downloads
|
||||
@downloads.select { |d| %i[pending downloading].include?(d.status) }
|
||||
end
|
||||
|
||||
def update
|
||||
@downloads.each do |download|
|
||||
if download.status == :pending && active_downloads.size <= @max_parallel_downloads
|
||||
download.status = :downloading
|
||||
Thread.start { download.download }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def prune
|
||||
@downloads.delete_if { |d| d.status == :finished || d.status == :failed }
|
||||
end
|
||||
|
||||
class Download
|
||||
attr_accessor :status
|
||||
attr_reader :uri, :save_as, :callback, :remaining_bytes, :total_downloaded_bytes, :total_bytes,
|
||||
:error_message, :started_at, :finished_at
|
||||
|
||||
def initialize(uri:, save_as:, callback: nil)
|
||||
@uri = uri
|
||||
@save_as = save_as
|
||||
@callback = callback
|
||||
|
||||
@status = :pending
|
||||
|
||||
@remaining_bytes = 0.0
|
||||
@total_downloaded_bytes = 0.0
|
||||
@total_bytes = 0.0
|
||||
|
||||
@error_message = ""
|
||||
end
|
||||
|
||||
def progress
|
||||
v = 1.0 - (@remaining_bytes.to_f / total_bytes)
|
||||
return 0.0 if v.nan?
|
||||
|
||||
v
|
||||
end
|
||||
|
||||
def download
|
||||
@status = :downloading
|
||||
@started_at = Time.now # TODO: monotonic time
|
||||
|
||||
io = File.open(@save_as, "w")
|
||||
streamer = lambda do |chunk, remaining_bytes, total_bytes|
|
||||
io.write(chunk)
|
||||
|
||||
@remaining_bytes = remaining_bytes.to_f
|
||||
@total_downloaded_bytes += chunk.size
|
||||
@total_bytes = total_bytes.to_f
|
||||
end
|
||||
|
||||
begin
|
||||
response = Excon.get(
|
||||
@uri.to_s,
|
||||
middlewares: Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower],
|
||||
response_block: streamer
|
||||
)
|
||||
|
||||
if response.status == 200
|
||||
@status = :finished
|
||||
@finished_at = Time.now # TODO: monotonic time
|
||||
@callback.call(self) if @callback
|
||||
else
|
||||
@error_message = "Got a non 200 HTTP status of #{response.status}"
|
||||
@status = :failed
|
||||
@finished_at = Time.now # TODO: monotonic time
|
||||
@callback.call(self) if @callback
|
||||
end
|
||||
rescue StandardError => e # TODO: cherrypick errors to cature
|
||||
@status = :failed
|
||||
@finished_at = Time.now # TODO: monotonic time
|
||||
@error_message = e.message
|
||||
@callback.call(self) if @callback
|
||||
end
|
||||
ensure
|
||||
io.close if io
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,60 +1,64 @@
|
||||
module CyberarmEngine
|
||||
module Common
|
||||
def push_state(klass, options={})
|
||||
$window.push_state(klass, options)
|
||||
def push_state(klass, options = {})
|
||||
window.push_state(klass, options)
|
||||
end
|
||||
|
||||
def current_state
|
||||
$window.current_state
|
||||
window.current_state
|
||||
end
|
||||
|
||||
def previous_state
|
||||
$window.previous_state
|
||||
window.previous_state
|
||||
end
|
||||
|
||||
def pop_state
|
||||
$window.pop_state
|
||||
window.pop_state
|
||||
end
|
||||
|
||||
def shift_state
|
||||
window.shift_state
|
||||
end
|
||||
|
||||
def show_cursor
|
||||
$window.show_cursor
|
||||
window.show_cursor
|
||||
end
|
||||
|
||||
def show_cursor=boolean
|
||||
$window.show_cursor = boolean
|
||||
def show_cursor=(boolean)
|
||||
window.show_cursor = boolean
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
def lighten(color, amount = 25)
|
||||
if defined?(color.alpha)
|
||||
return Gosu::Color.rgba(color.red+amount, color.green+amount, color.blue+amount, color.alpha)
|
||||
Gosu::Color.rgba(color.red + amount, color.green + amount, color.blue + amount, color.alpha)
|
||||
else
|
||||
return Gosu::Color.rgb(color.red+amount, color.green+amount, color.blue+amount)
|
||||
Gosu::Color.rgb(color.red + amount, color.green + amount, color.blue + amount)
|
||||
end
|
||||
end
|
||||
|
||||
def darken(color, amount = 25)
|
||||
if defined?(color.alpha)
|
||||
return Gosu::Color.rgba(color.red-amount, color.green-amount, color.blue-amount, color.alpha)
|
||||
Gosu::Color.rgba(color.red - amount, color.green - amount, color.blue - amount, color.alpha)
|
||||
else
|
||||
return Gosu::Color.rgb(color.red-amount, color.green-amount, color.blue-amount)
|
||||
Gosu::Color.rgb(color.red - amount, color.green - amount, color.blue - amount)
|
||||
end
|
||||
end
|
||||
|
||||
def opacity(color, ratio = 1.0)
|
||||
alpha = 255 * ratio
|
||||
|
||||
return Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||
Gosu::Color.rgba(color.red, color.green, color.blue, alpha)
|
||||
end
|
||||
|
||||
def get_asset(path, hash, klass)
|
||||
def get_asset(path, hash, klass, retro = false, tileable = false)
|
||||
asset = nil
|
||||
hash.detect do |_asset, instance|
|
||||
if _asset == path
|
||||
@@ -64,28 +68,34 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
unless asset
|
||||
instance = klass.new(path)
|
||||
instance = nil
|
||||
instance = if klass == Gosu::Image
|
||||
klass.new(path, retro: retro, tileable: tileable)
|
||||
else
|
||||
klass.new(path)
|
||||
end
|
||||
|
||||
hash[path] = instance
|
||||
asset = instance
|
||||
end
|
||||
|
||||
return asset
|
||||
asset
|
||||
end
|
||||
|
||||
def get_image(path)
|
||||
get_asset(path, Engine::IMAGES, Gosu::Image)
|
||||
def get_image(path, retro: false, tileable: false)
|
||||
get_asset(path, Window::IMAGES, Gosu::Image, retro, tileable)
|
||||
end
|
||||
|
||||
def get_sample(path)
|
||||
get_asset(path, Engine::SAMPLES, Gosu::Sample)
|
||||
get_asset(path, Window::SAMPLES, Gosu::Sample)
|
||||
end
|
||||
|
||||
def get_song(path)
|
||||
get_asset(path, Engine::SONGS, Gosu::Song)
|
||||
get_asset(path, Window::SONGS, Gosu::Song)
|
||||
end
|
||||
|
||||
def window
|
||||
$window
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
46
lib/cyberarm_engine/config_file.rb
Normal file
46
lib/cyberarm_engine/config_file.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
module CyberarmEngine
|
||||
class ConfigFile
|
||||
def initialize(file:)
|
||||
@file = file
|
||||
|
||||
if File.exist?(@file)
|
||||
deserialize
|
||||
else
|
||||
@data = {}
|
||||
end
|
||||
end
|
||||
|
||||
def []=(*keys, value)
|
||||
last_key = keys.last
|
||||
|
||||
if keys.size == 1
|
||||
hash = @data
|
||||
else
|
||||
keys.pop
|
||||
hash = @data[keys.shift] ||= {}
|
||||
|
||||
keys.each do |key|
|
||||
hash = hash[key] ||= {}
|
||||
end
|
||||
end
|
||||
|
||||
hash[last_key] = value
|
||||
end
|
||||
|
||||
def get(*keys)
|
||||
@data.dig(*keys)
|
||||
end
|
||||
|
||||
def serialize
|
||||
JSON.dump(@data)
|
||||
end
|
||||
|
||||
def deserialize
|
||||
@data = JSON.parse(File.read(@file), symbolize_names: true)
|
||||
end
|
||||
|
||||
def save!
|
||||
File.open(@file, "w") { |f| f.write(serialize) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,56 +3,62 @@ module CyberarmEngine
|
||||
include Common
|
||||
|
||||
attr_accessor :image, :angle, :position, :velocity, :center_x, :center_y, :scale_x, :scale_y,
|
||||
:color, :alpha, :mode, :options, :paused, :radius, :last_position
|
||||
def initialize(options={})
|
||||
if options[:auto_manage] || options[:auto_manage] == nil
|
||||
$window.current_state.add_game_object(self)
|
||||
end
|
||||
:color, :mode, :options, :paused, :radius, :last_position
|
||||
attr_reader :alpha
|
||||
|
||||
def initialize(options = {})
|
||||
$window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil?
|
||||
|
||||
@options = options
|
||||
@image = options[:image] ? image(options[:image]) : nil
|
||||
x = options[:x] ? options[:x] : 0
|
||||
y = options[:y] ? options[:y] : 0
|
||||
z = options[:z] ? options[:z] : 0
|
||||
x = options[:x] || 0
|
||||
y = options[:y] || 0
|
||||
z = options[:z] || 0
|
||||
@position = Vector.new(x, y, z)
|
||||
@velocity = Vector.new
|
||||
@last_position = Vector.new
|
||||
@angle = options[:angle] ? options[:angle] : 0
|
||||
@angle = options[:angle] || 0
|
||||
|
||||
@center_x = options[:center_x] ? options[:center_x] : 0.5
|
||||
@center_y = options[:center_y] ? options[:center_y] : 0.5
|
||||
@center_x = options[:center_x] || 0.5
|
||||
@center_y = options[:center_y] || 0.5
|
||||
|
||||
@scale_x = options[:scale_x] ? options[:scale_x] : 1
|
||||
@scale_y = options[:scale_y] ? options[:scale_y] : 1
|
||||
@scale_x = options[:scale_x] || 1
|
||||
@scale_y = options[:scale_y] || 1
|
||||
|
||||
@color = options[:color] ? options[:color] : Gosu::Color.argb(0xff_ffffff)
|
||||
@alpha = options[:alpha] ? options[:alpha] : 255
|
||||
@mode = options[:mode] ? options[:mode] : :default
|
||||
@color = options[:color] || Gosu::Color.argb(0xff_ffffff)
|
||||
@alpha = options[:alpha] || 255
|
||||
@mode = options[:mode] || :default
|
||||
|
||||
@paused = false
|
||||
@speed = 0
|
||||
@debug_color = Gosu::Color::GREEN
|
||||
@world_center_point = Vector.new(0,0)
|
||||
@world_center_point = Vector.new(0, 0)
|
||||
|
||||
setup
|
||||
|
||||
@debug_text = Text.new("", color: @debug_color, y: @position.y-(self.height*self.scale), z: 9999)
|
||||
@debug_text = Text.new("", color: @debug_color, y: @position.y - (height * scale), z: 9999)
|
||||
@debug_text.x = @position.x
|
||||
if @radius == 0 || @radius == nil
|
||||
@radius = options[:radius] ? options[:radius] : defined?(@image.width) ? ((@image.width+@image.height)/4)*scale : 1
|
||||
if @radius == 0 || @radius.nil?
|
||||
@radius = if options[:radius]
|
||||
options[:radius]
|
||||
else
|
||||
defined?(@image.width) ? ((@image.width + @image.height) / 4) * scale : 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def draw
|
||||
if @image
|
||||
@image.draw_rot(@position.x, @position.y, @position.z, @angle, @center_x, @center_y, @scale_x, @scale_y, @color, @mode)
|
||||
@image.draw_rot(@position.x, @position.y, @position.z, @angle, @center_x, @center_y, @scale_x, @scale_y,
|
||||
@color, @mode)
|
||||
end
|
||||
|
||||
if $debug
|
||||
show_debug_heading
|
||||
$window.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
|
||||
if @debug_text.text != ""
|
||||
$window.draw_rect(@debug_text.x-10, (@debug_text.y-10), @debug_text.width+20, @debug_text.height+20, Gosu::Color.rgba(0,0,0,200), 9999)
|
||||
$window.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20,
|
||||
Gosu::Color.rgba(0, 0, 0, 200), 9999)
|
||||
@debug_text.draw
|
||||
end
|
||||
end
|
||||
@@ -63,13 +69,13 @@ module CyberarmEngine
|
||||
|
||||
def debug_text(text)
|
||||
@debug_text.text = text
|
||||
@debug_text.x = @position.x-(@debug_text.width / 2)
|
||||
@debug_text.y = @position.y-(@debug_text.height + self.radius + self.height)
|
||||
@debug_text.x = @position.x - (@debug_text.width / 2)
|
||||
@debug_text.y = @position.y - (@debug_text.height + radius + height)
|
||||
end
|
||||
|
||||
def scale
|
||||
if @scale_x == @scale_y
|
||||
return @scale_x
|
||||
@scale_x
|
||||
else
|
||||
false
|
||||
# maths?
|
||||
@@ -79,7 +85,7 @@ module CyberarmEngine
|
||||
def scale=(int)
|
||||
self.scale_x = int
|
||||
self.scale_y = int
|
||||
self.radius = ((@image.width+@image.height)/4)*self.scale
|
||||
self.radius = ((@image.width + @image.height) / 4) * scale
|
||||
end
|
||||
|
||||
def visible
|
||||
@@ -96,16 +102,16 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def _x_visible
|
||||
self.x.between?(($window.width/2)-(@world_center_point.x), ($window.width/2)+@world_center_point.x) ||
|
||||
self.x.between?(((@world_center_point.x)-$window.width/2), ($window.width/2)+@world_center_point.x)
|
||||
x.between?(($window.width / 2) - @world_center_point.x, ($window.width / 2) + @world_center_point.x) ||
|
||||
x.between?((@world_center_point.x - $window.width / 2), ($window.width / 2) + @world_center_point.x)
|
||||
end
|
||||
|
||||
def _y_visible
|
||||
self.y.between?(($window.height/2)-(@world_center_point.y), ($window.height/2)+@world_center_point.y) ||
|
||||
self.y.between?((@world_center_point.y)-($window.height/2), ($window.height/2)+@world_center_point.y)
|
||||
y.between?(($window.height / 2) - @world_center_point.y, ($window.height / 2) + @world_center_point.y) ||
|
||||
y.between?(@world_center_point.y - ($window.height / 2), ($window.height / 2) + @world_center_point.y)
|
||||
end
|
||||
|
||||
def heading(ahead_by = 100, object = nil, angle_only = false)
|
||||
def heading(ahead_by = 100, _object = nil, angle_only = false)
|
||||
direction = Gosu.angle(@last_position.x, @last_position.x, @position.x, position.y).gosu_to_radians
|
||||
|
||||
_x = @position.x + (ahead_by * Math.cos(direction))
|
||||
@@ -121,11 +127,11 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def width
|
||||
@image ? @image.width * self.scale : 0
|
||||
@image ? @image.width * scale : 0
|
||||
end
|
||||
|
||||
def height
|
||||
@image ? @image.height * self.scale : 0
|
||||
@image ? @image.height * scale : 0
|
||||
end
|
||||
|
||||
def pause
|
||||
@@ -137,24 +143,24 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def rotate(int)
|
||||
self.angle+=int
|
||||
self.angle%=360
|
||||
self.angle += int
|
||||
self.angle %= 360
|
||||
end
|
||||
|
||||
def alpha=int # 0-255
|
||||
def alpha=(int) # 0-255
|
||||
@alpha = int
|
||||
@alpha = 255 if @alpha > 255
|
||||
@color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, int)
|
||||
end
|
||||
|
||||
def draw_rect(x, y, width, height, color, z = 0)
|
||||
$window.draw_rect(x,y,width,height,color,z)
|
||||
$window.draw_rect(x, y, width, height, color, z)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
end
|
||||
|
||||
def button_down?(id)
|
||||
def button_down(id)
|
||||
end
|
||||
|
||||
def find_closest(game_object_class)
|
||||
@@ -162,14 +168,14 @@ module CyberarmEngine
|
||||
best_distance = 100_000_000_000 # Huge default number
|
||||
|
||||
game_object_class.all.each do |object|
|
||||
distance = Gosu::distance(self.x, self.y, object.x, object.y)
|
||||
distance = Gosu.distance(x, y, object.x, object.y)
|
||||
if distance <= best_distance
|
||||
best_object = object
|
||||
best_distance = distance
|
||||
end
|
||||
end
|
||||
|
||||
return best_object
|
||||
best_object
|
||||
end
|
||||
|
||||
def look_at(object)
|
||||
@@ -177,31 +183,24 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def circle_collision?(object)
|
||||
distance = Gosu.distance(self.x, self.y, object.x, object.y)
|
||||
if distance <= self.radius+object.radius
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
distance = Gosu.distance(x, y, object.x, object.y)
|
||||
distance <= radius + object.radius
|
||||
end
|
||||
|
||||
# Duplication... so DRY.
|
||||
def each_circle_collision(object, resolve_with = :width, &block)
|
||||
def each_circle_collision(object, _resolve_with = :width, &block)
|
||||
if object.class != Class && object.instance_of?(object.class)
|
||||
$window.current_state.game_objects.select {|i| i.class == object.class}.each do |o|
|
||||
distance = Gosu.distance(self.x, self.y, object.x, object.y)
|
||||
if distance <= self.radius+object.radius
|
||||
block.call(o, object) if block
|
||||
end
|
||||
$window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o|
|
||||
distance = Gosu.distance(x, y, object.x, object.y)
|
||||
block.call(o, object) if distance <= radius + object.radius && block
|
||||
end
|
||||
else
|
||||
list = $window.current_state.game_objects.select {|i| i.class == object}
|
||||
list = $window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
list.each do |o|
|
||||
next if self == o
|
||||
distance = Gosu.distance(self.x, self.y, o.x, o.y)
|
||||
if distance <= self.radius+o.radius
|
||||
block.call(self, o) if block
|
||||
end
|
||||
|
||||
distance = Gosu.distance(x, y, o.x, o.y)
|
||||
block.call(self, o) if distance <= radius + o.radius && block
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -209,35 +208,30 @@ module CyberarmEngine
|
||||
def destroy
|
||||
if $window.current_state
|
||||
$window.current_state.game_objects.each do |o|
|
||||
if o.is_a?(self.class) && o == self
|
||||
$window.current_state.game_objects.delete(o)
|
||||
end
|
||||
$window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# NOTE: This could be implemented more reliably
|
||||
def all
|
||||
INSTANCES.select {|i| i.class == self}
|
||||
INSTANCES.select { |i| i.instance_of?(self) }
|
||||
end
|
||||
|
||||
def self.each_circle_collision(object, resolve_with = :width, &block)
|
||||
def self.each_circle_collision(object, _resolve_with = :width, &block)
|
||||
if object.class != Class && object.instance_of?(object.class)
|
||||
$window.current_state.game_objects.select {|i| i.class == self}.each do |o|
|
||||
$window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o|
|
||||
distance = Gosu.distance(o.x, o.y, object.x, object.y)
|
||||
if distance <= o.radius+object.radius
|
||||
block.call(o, object) if block
|
||||
end
|
||||
block.call(o, object) if distance <= o.radius + object.radius && block
|
||||
end
|
||||
else
|
||||
lista = $window.current_state.game_objects.select {|i| i.class == self}
|
||||
listb = $window.current_state.game_objects.select {|i| i.class == object}
|
||||
lista = $window.current_state.game_objects.select { |i| i.instance_of?(self) }
|
||||
listb = $window.current_state.game_objects.select { |i| i.instance_of?(object) }
|
||||
lista.product(listb).each do |o, o2|
|
||||
next if o == o2
|
||||
|
||||
distance = Gosu.distance(o.x, o.y, o2.x, o2.y)
|
||||
if distance <= o.radius+o2.radius
|
||||
block.call(o, o2) if block
|
||||
end
|
||||
block.call(o, o2) if distance <= o.radius + o2.radius && block
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -246,11 +240,9 @@ module CyberarmEngine
|
||||
INSTANCES.clear
|
||||
if $window.current_state
|
||||
$window.current_state.game_objects.each do |o|
|
||||
if o.is_a?(self.class)
|
||||
$window.current_state.game_objects.delete(o)
|
||||
end
|
||||
$window.current_state.game_objects.delete(o) if o.is_a?(self.class)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,22 +3,25 @@ module CyberarmEngine
|
||||
include Common
|
||||
|
||||
attr_accessor :options, :global_pause
|
||||
attr_reader :game_objects, :containers
|
||||
attr_reader :game_objects
|
||||
|
||||
def initialize(options={})
|
||||
def initialize(options = {})
|
||||
@options = options
|
||||
@game_objects = []
|
||||
@global_pause = false
|
||||
$window.text_input = nil unless options[:preserve_text_input]
|
||||
|
||||
@down_keys = {}
|
||||
|
||||
setup
|
||||
end
|
||||
|
||||
def setup
|
||||
end
|
||||
|
||||
# Called immediately after setup returns.
|
||||
# GuiState uses this to set current_theme for ToolTip
|
||||
def post_setup
|
||||
end
|
||||
|
||||
def draw
|
||||
@game_objects.each(&:draw)
|
||||
end
|
||||
@@ -28,7 +31,10 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def draw_bounding_box(box)
|
||||
x,y, max_x, max_y = box.x, box.y, box.max_x, box.max_y
|
||||
x = box.x
|
||||
y = box.y
|
||||
max_x = box.max_x
|
||||
max_y = box.max_y
|
||||
|
||||
color = Gosu::Color.rgba(255, 127, 64, 240)
|
||||
|
||||
@@ -88,4 +94,4 @@ module CyberarmEngine
|
||||
@game_objects << object
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,168 +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 ==(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
|
||||
|
||||
# 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
|
||||
207
lib/cyberarm_engine/model.rb
Normal file
207
lib/cyberarm_engine/model.rb
Normal file
@@ -0,0 +1,207 @@
|
||||
module CyberarmEngine
|
||||
class Model
|
||||
attr_accessor :objects, :materials, :vertices, :uvs, :texures, :normals, :faces, :colors, :bones, :material_file,
|
||||
:current_material, :current_object, :vertex_count, :smoothing
|
||||
attr_reader :position, :bounding_box, :textured_material, :file_path, :positions_buffer_id, :colors_buffer_id,
|
||||
:normals_buffer_id, :uvs_buffer_id, :textures_buffer_id, :vertex_array_id, :aabb_tree
|
||||
|
||||
def initialize(file_path:)
|
||||
@file_path = file_path
|
||||
|
||||
@material_file = nil
|
||||
@current_object = nil
|
||||
@current_material = nil
|
||||
@vertex_count = 0
|
||||
|
||||
@objects = []
|
||||
@materials = {}
|
||||
@vertices = []
|
||||
@colors = []
|
||||
@uvs = []
|
||||
@normals = []
|
||||
@faces = []
|
||||
@bones = []
|
||||
@smoothing = 0
|
||||
|
||||
@bounding_box = BoundingBox.new
|
||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||
|
||||
type = File.basename(file_path).split(".").last.to_sym
|
||||
parser = Model::Parser.find(type)
|
||||
raise "Unsupported model type '.#{type}', supported models are: #{Model::Parser.supported_formats}" unless parser
|
||||
|
||||
parse(parser)
|
||||
|
||||
@has_texture = false
|
||||
|
||||
@materials.each do |_key, material|
|
||||
@has_texture = true if material.texture_id
|
||||
end
|
||||
|
||||
allocate_gl_objects
|
||||
populate_vertex_buffer
|
||||
configure_vao
|
||||
|
||||
@objects.each { |o| @vertex_count += o.vertices.size }
|
||||
|
||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||
# build_collision_tree
|
||||
end
|
||||
|
||||
def parse(parser)
|
||||
parser.new(self).parse
|
||||
end
|
||||
|
||||
def calculate_bounding_box(vertices, bounding_box)
|
||||
unless bounding_box.min.x.is_a?(Float)
|
||||
vertex = vertices.last
|
||||
bounding_box.min.x = vertex.x
|
||||
bounding_box.min.y = vertex.y
|
||||
bounding_box.min.z = vertex.z
|
||||
|
||||
bounding_box.max.x = vertex.x
|
||||
bounding_box.max.y = vertex.y
|
||||
bounding_box.max.z = vertex.z
|
||||
end
|
||||
|
||||
vertices.each do |vertex|
|
||||
bounding_box.min.x = vertex.x if vertex.x <= bounding_box.min.x
|
||||
bounding_box.min.y = vertex.y if vertex.y <= bounding_box.min.y
|
||||
bounding_box.min.z = vertex.z if vertex.z <= bounding_box.min.z
|
||||
|
||||
bounding_box.max.x = vertex.x if vertex.x >= bounding_box.max.x
|
||||
bounding_box.max.y = vertex.y if vertex.y >= bounding_box.max.y
|
||||
bounding_box.max.z = vertex.z if vertex.z >= bounding_box.max.z
|
||||
end
|
||||
end
|
||||
|
||||
def allocate_gl_objects
|
||||
# Allocate arrays for future use
|
||||
@vertex_array_id = nil
|
||||
buffer = " " * 4
|
||||
glGenVertexArrays(1, buffer)
|
||||
@vertex_array_id = buffer.unpack1("L2")
|
||||
|
||||
# Allocate buffers for future use
|
||||
@positions_buffer_id = nil
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@positions_buffer_id = buffer.unpack1("L2")
|
||||
|
||||
@colors_buffer_id = nil
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@colors_buffer_id = buffer.unpack1("L2")
|
||||
|
||||
@normals_buffer_id = nil
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@normals_buffer_id = buffer.unpack1("L2")
|
||||
|
||||
@uvs_buffer_id = nil
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@uvs_buffer_id = buffer.unpack1("L2")
|
||||
end
|
||||
|
||||
def populate_vertex_buffer
|
||||
pos = []
|
||||
colors = []
|
||||
norms = []
|
||||
uvs = []
|
||||
|
||||
@faces.each do |face|
|
||||
pos << face.vertices.map { |vert| [vert.x, vert.y, vert.z] }
|
||||
colors << face.colors.map { |color| [color.red, color.green, color.blue] }
|
||||
norms << face.normals.map { |vert| [vert.x, vert.y, vert.z, vert.weight] }
|
||||
|
||||
uvs << face.uvs.map { |vert| [vert.x, vert.y, vert.z] } if has_texture?
|
||||
end
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, pos.flatten.size * Fiddle::SIZEOF_FLOAT, pos.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, colors.flatten.size * Fiddle::SIZEOF_FLOAT, colors.flatten.pack("f*"),
|
||||
GL_STATIC_DRAW)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, norms.flatten.size * Fiddle::SIZEOF_FLOAT, norms.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||
|
||||
if has_texture?
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, uvs.flatten.size * Fiddle::SIZEOF_FLOAT, uvs.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||
end
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
end
|
||||
|
||||
def configure_vao
|
||||
glBindVertexArray(@vertex_array_id)
|
||||
gl_error?
|
||||
|
||||
# index, size, type, normalized, stride, pointer
|
||||
# vertices (positions)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
||||
gl_error?
|
||||
|
||||
# inPosition
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
# colors
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
|
||||
# inColor
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
# normals
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
|
||||
# inNormal
|
||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
|
||||
if has_texture?
|
||||
# uvs
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||
# inUV
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
end
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
|
||||
def build_collision_tree
|
||||
@aabb_tree = AABBTree.new
|
||||
|
||||
@faces.each do |face|
|
||||
box = BoundingBox.new
|
||||
box.min = face.vertices.first.dup
|
||||
box.max = face.vertices.first.dup
|
||||
|
||||
face.vertices.each do |vertex|
|
||||
if vertex.sum < box.min.sum
|
||||
box.min = vertex.dup
|
||||
elsif vertex.sum > box.max.sum
|
||||
box.max = vertex.dup
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME: Handle negatives
|
||||
box.min *= 1.5
|
||||
box.max *= 1.5
|
||||
@aabb_tree.insert(face, box)
|
||||
end
|
||||
end
|
||||
|
||||
def has_texture?
|
||||
@has_texture
|
||||
end
|
||||
|
||||
def release_gl_resources
|
||||
if @vertex_array_id
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
21
lib/cyberarm_engine/model/material.rb
Normal file
21
lib/cyberarm_engine/model/material.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module CyberarmEngine
|
||||
class Model
|
||||
class Material
|
||||
attr_accessor :name, :ambient, :diffuse, :specular
|
||||
attr_reader :texture_id
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@ambient = Color.new(1, 1, 1, 1)
|
||||
@diffuse = Color.new(1, 1, 1, 1)
|
||||
@specular = Color.new(1, 1, 1, 1)
|
||||
@texture = nil
|
||||
@texture_id = nil
|
||||
end
|
||||
|
||||
def set_texture(texture_path)
|
||||
@texture_id = Texture.new(path: texture_path).id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
131
lib/cyberarm_engine/model/model_object.rb
Normal file
131
lib/cyberarm_engine/model/model_object.rb
Normal file
@@ -0,0 +1,131 @@
|
||||
module CyberarmEngine
|
||||
class Model
|
||||
class ModelObject
|
||||
attr_reader :id, :name, :vertices, :uvs, :normals, :materials, :bounding_box, :debug_color
|
||||
attr_accessor :faces, :scale
|
||||
|
||||
def initialize(id, name)
|
||||
@id = id
|
||||
@name = name
|
||||
@vertices = []
|
||||
@uvs = []
|
||||
@normals = []
|
||||
@faces = []
|
||||
@materials = []
|
||||
@bounding_box = BoundingBox.new
|
||||
@debug_color = Color.new(1.0, 1.0, 1.0)
|
||||
|
||||
@scale = 1.0
|
||||
|
||||
# Faces array packs everything:
|
||||
# vertex = index[0]
|
||||
# uv = index[1]
|
||||
# normal = index[2]
|
||||
# material = index[3]
|
||||
end
|
||||
|
||||
def has_texture?
|
||||
@materials.find { |mat| mat.texture_id } ? true : false
|
||||
end
|
||||
|
||||
def reflatten
|
||||
@vertices_list = nil
|
||||
@uvs_list = nil
|
||||
@normals_list = nil
|
||||
|
||||
flattened_vertices
|
||||
flattened_uvs
|
||||
flattened_normals
|
||||
end
|
||||
|
||||
def flattened_vertices
|
||||
unless @vertices_list
|
||||
@debug_color = @faces.first.material.diffuse
|
||||
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.vertices.each do |v|
|
||||
next unless v
|
||||
|
||||
list << v.x * @scale
|
||||
list << v.y * @scale
|
||||
list << v.z * @scale
|
||||
list << v.weight
|
||||
end
|
||||
end
|
||||
|
||||
@vertices_list_size = list.size
|
||||
@vertices_list = list.pack("f*")
|
||||
end
|
||||
|
||||
@vertices_list
|
||||
end
|
||||
|
||||
def flattened_vertices_size
|
||||
@vertices_list_size
|
||||
end
|
||||
|
||||
def flattened_uvs
|
||||
unless @uvs_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.uvs.each do |v|
|
||||
next unless v
|
||||
|
||||
list << v.x
|
||||
list << v.y
|
||||
list << v.z
|
||||
end
|
||||
end
|
||||
|
||||
@uvs_list_size = list.size
|
||||
@uvs_list = list.pack("f*")
|
||||
end
|
||||
|
||||
@uvs_list
|
||||
end
|
||||
|
||||
def flattened_normals
|
||||
unless @normals_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
face.normals.each do |n|
|
||||
next unless n
|
||||
|
||||
list << n.x
|
||||
list << n.y
|
||||
list << n.z
|
||||
end
|
||||
end
|
||||
|
||||
@normals_list_size = list.size
|
||||
@normals_list = list.pack("f*")
|
||||
end
|
||||
|
||||
@normals_list
|
||||
end
|
||||
|
||||
def flattened_materials
|
||||
unless @materials_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
material = face.material
|
||||
next unless material
|
||||
|
||||
face.vertices.each do # Add material to each vertex
|
||||
list << material.diffuse.red
|
||||
list << material.diffuse.green
|
||||
list << material.diffuse.blue
|
||||
# list << material.alpha
|
||||
end
|
||||
end
|
||||
|
||||
@materials_list_size = list.size
|
||||
@materials_list = list.pack("f*")
|
||||
end
|
||||
|
||||
@materials_list
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
74
lib/cyberarm_engine/model/parser.rb
Normal file
74
lib/cyberarm_engine/model/parser.rb
Normal file
@@ -0,0 +1,74 @@
|
||||
module CyberarmEngine
|
||||
TextureCoordinate = Struct.new(:u, :v, :weight)
|
||||
Point = Struct.new(:x, :y)
|
||||
Color = Struct.new(:red, :green, :blue, :alpha)
|
||||
Face = Struct.new(:vertices, :uvs, :normals, :colors, :material, :smoothing)
|
||||
|
||||
class Model
|
||||
class Parser
|
||||
@@parsers = []
|
||||
|
||||
def self.handles
|
||||
raise NotImplementedError,
|
||||
"Model::Parser#handles must return an array of file extensions that this parser supports"
|
||||
end
|
||||
|
||||
def self.inherited(parser)
|
||||
@@parsers << parser
|
||||
end
|
||||
|
||||
def self.find(file_type)
|
||||
@@parsers.find do |parser|
|
||||
parser.handles.include?(file_type)
|
||||
end
|
||||
end
|
||||
|
||||
def self.supported_formats
|
||||
@@parsers.map { |parser| parser.handles }.flatten.map { |s| ".#{s}" }.join(", ")
|
||||
end
|
||||
|
||||
def initialize(model)
|
||||
@model = model
|
||||
end
|
||||
|
||||
def parse
|
||||
end
|
||||
|
||||
def set_object(id: nil, name: nil)
|
||||
_model = nil
|
||||
|
||||
if id
|
||||
_model = @model.objects.find { |o| o.id == id }
|
||||
elsif name
|
||||
_model = @model.objects.find { |o| o.name == name }
|
||||
else
|
||||
raise "Must provide either an id: or name:"
|
||||
end
|
||||
|
||||
if _model
|
||||
@model.current_object = _model
|
||||
else
|
||||
raise "Couldn't find ModelObject!"
|
||||
end
|
||||
end
|
||||
|
||||
def change_object(id, name)
|
||||
@model.objects << Model::ModelObject.new(id, name)
|
||||
@model.current_object = @model.objects.last
|
||||
end
|
||||
|
||||
def set_material(name)
|
||||
@model.current_material = name
|
||||
@model.current_object.materials << current_material
|
||||
end
|
||||
|
||||
def add_material(name, material)
|
||||
@model.materials[name] = material
|
||||
end
|
||||
|
||||
def current_material
|
||||
@model.materials[@model.current_material]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
138
lib/cyberarm_engine/model/parsers/collada_parser.rb
Normal file
138
lib/cyberarm_engine/model/parsers/collada_parser.rb
Normal file
@@ -0,0 +1,138 @@
|
||||
module CyberarmEngine
|
||||
class ColladaParser < Model::Parser
|
||||
def self.handles
|
||||
[:dae]
|
||||
end
|
||||
|
||||
def parse
|
||||
@collada = Nokogiri::XML(File.read(@model.file_path))
|
||||
|
||||
@collada.css("library_materials material").each do |material|
|
||||
parse_material(material)
|
||||
end
|
||||
|
||||
@collada.css("library_geometries geometry").each do |geometry|
|
||||
parse_geometry(geometry)
|
||||
end
|
||||
|
||||
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
||||
@model.objects.each do |o|
|
||||
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_material(material)
|
||||
name = material.attributes["id"].value
|
||||
effect_id = material.at_css("instance_effect").attributes["url"].value
|
||||
|
||||
mat = Model::Material.new(name)
|
||||
effect = @collada.at_css("[id=\"#{effect_id.sub('#', '')}\"]")
|
||||
|
||||
emission = effect.at_css("emission color")
|
||||
diffuse = effect.at_css("diffuse color").children.first.to_s.split(" ").map { |c| Float(c) }
|
||||
|
||||
mat.diffuse = Color.new(*diffuse[0..2])
|
||||
|
||||
add_material(name, mat)
|
||||
end
|
||||
|
||||
def parse_geometry(geometry)
|
||||
geometry_id = geometry.attributes["id"].value
|
||||
geometry_name = geometry.attributes["name"].value
|
||||
|
||||
change_object(geometry_id, geometry_name)
|
||||
|
||||
mesh = geometry.at_css("mesh")
|
||||
|
||||
get_positions(geometry_id, mesh)
|
||||
get_normals(geometry_id, mesh)
|
||||
get_texture_coordinates(geometry_id, mesh)
|
||||
|
||||
project_node(geometry_name)
|
||||
build_faces(geometry_id, mesh)
|
||||
end
|
||||
|
||||
def get_positions(id, mesh)
|
||||
positions = mesh.at_css("[id=\"#{id}-positions\"]")
|
||||
array = positions.at_css("[id=\"#{id}-positions-array\"]")
|
||||
|
||||
stride = Integer(positions.at_css("[source=\"##{id}-positions-array\"]").attributes["stride"].value)
|
||||
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
||||
position = Vector.new(*slice)
|
||||
@model.current_object.vertices << position
|
||||
@model.vertices << position
|
||||
end
|
||||
end
|
||||
|
||||
def get_normals(id, mesh)
|
||||
normals = mesh.at_css("[id=\"#{id}-normals\"]")
|
||||
array = normals.at_css("[id=\"#{id}-normals-array\"]")
|
||||
|
||||
stride = Integer(normals.at_css("[source=\"##{id}-normals-array\"]").attributes["stride"].value)
|
||||
list = array.children.first.to_s.split(" ").map { |f| Float(f) }.each_slice(stride).each do |slice|
|
||||
normal = Vector.new(*slice)
|
||||
@model.current_object.normals << normal
|
||||
@model.normals << normal
|
||||
end
|
||||
end
|
||||
|
||||
def get_texture_coordinates(id, mesh)
|
||||
end
|
||||
|
||||
def project_node(name)
|
||||
@collada.css("library_visual_scenes visual_scene node").each do |node|
|
||||
next unless node.attributes["name"].value == name
|
||||
|
||||
transform = Transform.new(node.at_css("matrix").children.first.to_s.split(" ").map { |f| Float(f) })
|
||||
|
||||
@model.current_object.vertices.each do |vert|
|
||||
v = vert.multiply_transform(transform)
|
||||
vert.x = v.x
|
||||
vert.y = v.y
|
||||
vert.z = v.z
|
||||
vert.w = v.w
|
||||
end
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
def build_faces(_id, mesh)
|
||||
material_name = mesh.at_css("triangles").attributes["material"].value
|
||||
set_material(material_name)
|
||||
|
||||
positions_index = []
|
||||
normals_index = []
|
||||
uvs_index = []
|
||||
|
||||
mesh.at_css("triangles p").children.first.to_s.split(" ").map { |i| Integer(i) }.each_slice(3).each do |slice|
|
||||
positions_index << slice[0]
|
||||
normals_index << slice[1]
|
||||
uvs_index << slice[2]
|
||||
end
|
||||
|
||||
norm_index = 0
|
||||
positions_index.each_slice(3) do |slice|
|
||||
face = Face.new
|
||||
face.vertices = []
|
||||
face.uvs = []
|
||||
face.normals = []
|
||||
face.colors = []
|
||||
face.material = current_material
|
||||
face.smoothing = @model.smoothing
|
||||
|
||||
slice.each do |index|
|
||||
face.vertices << @model.vertices[index]
|
||||
# face.uvs << @model.uvs[index]
|
||||
face.normals << @model.normals[normals_index[norm_index]]
|
||||
face.colors << current_material.diffuse
|
||||
|
||||
norm_index += 1
|
||||
end
|
||||
|
||||
@model.current_object.faces << face
|
||||
@model.faces << face
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
154
lib/cyberarm_engine/model/parsers/wavefront_parser.rb
Normal file
154
lib/cyberarm_engine/model/parsers/wavefront_parser.rb
Normal file
@@ -0,0 +1,154 @@
|
||||
module CyberarmEngine
|
||||
class WavefrontParser < Model::Parser
|
||||
def self.handles
|
||||
[:obj]
|
||||
end
|
||||
|
||||
def parse
|
||||
lines = 0
|
||||
list = File.read(@model.file_path).split("\n")
|
||||
list.each do |line|
|
||||
lines += 1
|
||||
line = line.strip
|
||||
|
||||
array = line.split(" ")
|
||||
case array[0]
|
||||
when "mtllib"
|
||||
@model.material_file = array[1]
|
||||
parse_mtllib
|
||||
when "usemtl"
|
||||
set_material(array[1])
|
||||
when "o"
|
||||
change_object(nil, array[1])
|
||||
when "s"
|
||||
set_smoothing(array[1])
|
||||
when "v"
|
||||
add_vertex(array)
|
||||
when "vt"
|
||||
add_texture_coordinate(array)
|
||||
|
||||
when "vn"
|
||||
add_normal(array)
|
||||
|
||||
when "f"
|
||||
verts = []
|
||||
uvs = []
|
||||
norms = []
|
||||
array[1..3].each do |f|
|
||||
verts << f.split("/")[0]
|
||||
uvs << f.split("/")[1]
|
||||
norms << f.split("/")[2]
|
||||
end
|
||||
|
||||
face = Face.new
|
||||
face.vertices = []
|
||||
face.uvs = []
|
||||
face.normals = []
|
||||
face.colors = []
|
||||
face.material = current_material
|
||||
face.smoothing = @model.smoothing
|
||||
|
||||
mat = face.material.diffuse
|
||||
color = mat
|
||||
|
||||
verts.each_with_index do |v, index|
|
||||
if uvs.first != ""
|
||||
face.vertices << @model.vertices[Integer(v) - 1]
|
||||
face.uvs << @model.uvs[Integer(uvs[index]) - 1]
|
||||
face.normals << @model.normals[Integer(norms[index]) - 1]
|
||||
face.colors << color
|
||||
else
|
||||
face.vertices << @model.vertices[Integer(v) - 1]
|
||||
face.uvs << nil
|
||||
face.normals << @model.normals[Integer(norms[index]) - 1]
|
||||
face.colors << color
|
||||
end
|
||||
end
|
||||
|
||||
@model.current_object.faces << face
|
||||
@model.faces << face
|
||||
end
|
||||
end
|
||||
|
||||
@model.calculate_bounding_box(@model.vertices, @model.bounding_box)
|
||||
@model.objects.each do |o|
|
||||
@model.calculate_bounding_box(o.vertices, o.bounding_box)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mtllib
|
||||
file = File.open(@model.file_path.sub(File.basename(@model.file_path), "") + @model.material_file, "r")
|
||||
file.readlines.each do |line|
|
||||
array = line.strip.split(" ")
|
||||
case array.first
|
||||
when "newmtl"
|
||||
material = Model::Material.new(array.last)
|
||||
@model.current_material = array.last
|
||||
@model.materials[array.last] = material
|
||||
when "Ns" # Specular Exponent
|
||||
when "Ka" # Ambient color
|
||||
@model.materials[@model.current_material].ambient = Color.new(Float(array[1]), Float(array[2]),
|
||||
Float(array[3]))
|
||||
when "Kd" # Diffuse color
|
||||
@model.materials[@model.current_material].diffuse = Color.new(Float(array[1]), Float(array[2]),
|
||||
Float(array[3]))
|
||||
when "Ks" # Specular color
|
||||
@model.materials[@model.current_material].specular = Color.new(Float(array[1]), Float(array[2]),
|
||||
Float(array[3]))
|
||||
when "Ke" # Emissive
|
||||
when "Ni" # Unknown (Blender Specific?)
|
||||
when "d" # Dissolved (Transparency)
|
||||
when "illum" # Illumination model
|
||||
when "map_Kd" # Diffuse texture
|
||||
texture = File.basename(array[1])
|
||||
texture_path = "#{File.expand_path('../../', @model.file_path)}/textures/#{texture}"
|
||||
@model.materials[@model.current_material].set_texture(texture_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def set_smoothing(value)
|
||||
@model.smoothing = value == "1"
|
||||
end
|
||||
|
||||
def add_vertex(array)
|
||||
@model.vertex_count += 1
|
||||
vert = nil
|
||||
if array.size == 5
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
||||
elsif array.size == 4
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.current_object.vertices << vert
|
||||
@model.vertices << vert
|
||||
end
|
||||
|
||||
def add_normal(array)
|
||||
vert = nil
|
||||
if array.size == 5
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), Float(array[4]))
|
||||
elsif array.size == 4
|
||||
vert = Vector.new(Float(array[1]), Float(array[2]), Float(array[3]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.current_object.normals << vert
|
||||
@model.normals << vert
|
||||
end
|
||||
|
||||
def add_texture_coordinate(array)
|
||||
texture = nil
|
||||
if array.size == 4
|
||||
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), Float(array[3]))
|
||||
elsif array.size == 3
|
||||
texture = Vector.new(Float(array[1]), 1 - Float(array[2]), 1.0)
|
||||
else
|
||||
raise
|
||||
end
|
||||
@model.uvs << texture
|
||||
@model.current_object.uvs << texture
|
||||
end
|
||||
end
|
||||
end
|
||||
31
lib/cyberarm_engine/model_cache.rb
Normal file
31
lib/cyberarm_engine/model_cache.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
module CyberarmEngine
|
||||
module ModelCache
|
||||
CACHE = {}
|
||||
|
||||
def self.find_or_cache(manifest:)
|
||||
model_file = manifest.file_path + "/model/#{manifest.model}"
|
||||
|
||||
type = File.basename(model_file).split(".").last.to_sym
|
||||
|
||||
if model = load_model_from_cache(type, model_file)
|
||||
model
|
||||
else
|
||||
model = CyberarmEngine::Model.new(file_path: model_file)
|
||||
cache_model(type, model_file, model)
|
||||
|
||||
model
|
||||
end
|
||||
end
|
||||
|
||||
def self.load_model_from_cache(type, model_file)
|
||||
return CACHE[type][model_file] if CACHE[type].is_a?(Hash) && (CACHE[type][model_file])
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def self.cache_model(type, model_file, model)
|
||||
CACHE[type] = {} unless CACHE[type].is_a?(Hash)
|
||||
CACHE[type][model_file] = model
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,116 +0,0 @@
|
||||
module CyberarmEngine
|
||||
class Text
|
||||
CACHE = {}
|
||||
|
||||
attr_accessor :x, :y, :z, :size, :factor_x, :factor_y, :color, :shadow, :shadow_size, :options
|
||||
attr_reader :text, :textobject
|
||||
|
||||
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
|
||||
@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 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 = 30 if @color.alpha > 30
|
||||
@shadow_alpha = @color.alpha if @color.alpha <= 30
|
||||
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
|
||||
28
lib/cyberarm_engine/opengl.rb
Normal file
28
lib/cyberarm_engine/opengl.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
begin
|
||||
require "opengl"
|
||||
rescue LoadError
|
||||
puts "Required gem is not installed, please install 'opengl-bindings' and try again."
|
||||
exit(1)
|
||||
end
|
||||
|
||||
module CyberarmEngine
|
||||
def gl_error?
|
||||
e = glGetError
|
||||
if e != GL_NO_ERROR
|
||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||
warn " #{gluErrorString(e)} (#{e})\n"
|
||||
exit if window.exit_on_opengl_error?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require_relative "opengl/shader"
|
||||
require_relative "opengl/texture"
|
||||
require_relative "opengl/light"
|
||||
require_relative "opengl/perspective_camera"
|
||||
require_relative "opengl/orthographic_camera"
|
||||
|
||||
require_relative "opengl/renderer/g_buffer"
|
||||
require_relative "opengl/renderer/bounding_box_renderer"
|
||||
require_relative "opengl/renderer/opengl_renderer"
|
||||
require_relative "opengl/renderer/renderer"
|
||||
50
lib/cyberarm_engine/opengl/light.rb
Normal file
50
lib/cyberarm_engine/opengl/light.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module CyberarmEngine
|
||||
class Light
|
||||
DIRECTIONAL = 0
|
||||
POINT = 1
|
||||
SPOT = 2
|
||||
|
||||
attr_reader :light_id
|
||||
attr_accessor :type, :ambient, :diffuse, :specular, :position, :direction, :intensity
|
||||
|
||||
def initialize(
|
||||
id:,
|
||||
type: Light::POINT,
|
||||
ambient: Vector.new(0.5, 0.5, 0.5),
|
||||
diffuse: Vector.new(1, 1, 1),
|
||||
specular: Vector.new(0.2, 0.2, 0.2),
|
||||
position: Vector.new(0, 0, 0),
|
||||
direction: Vector.new(0, 0, 0),
|
||||
intensity: 1
|
||||
)
|
||||
@light_id = id
|
||||
@type = type
|
||||
|
||||
@ambient = ambient
|
||||
@diffuse = diffuse
|
||||
@specular = specular
|
||||
@position = position
|
||||
@direction = direction
|
||||
|
||||
@intensity = intensity
|
||||
end
|
||||
|
||||
def draw
|
||||
glLightfv(@light_id, GL_AMBIENT, convert(@ambient).pack("f*"))
|
||||
glLightfv(@light_id, GL_DIFFUSE, convert(@diffuse, true).pack("f*"))
|
||||
glLightfv(@light_id, GL_SPECULAR, convert(@specular, true).pack("f*"))
|
||||
glLightfv(@light_id, GL_POSITION, convert(@position).pack("f*"))
|
||||
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(@light_id)
|
||||
end
|
||||
|
||||
def convert(struct, apply_intensity = false)
|
||||
if apply_intensity
|
||||
struct.to_a.compact.map { |i| i * @intensity }
|
||||
else
|
||||
struct.to_a.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
46
lib/cyberarm_engine/opengl/orthographic_camera.rb
Normal file
46
lib/cyberarm_engine/opengl/orthographic_camera.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
module CyberarmEngine
|
||||
class OrthographicCamera
|
||||
attr_accessor :position, :orientation, :zoom, :left, :right, :bottom, :top,
|
||||
:min_view_distance, :max_view_distance
|
||||
|
||||
def initialize(
|
||||
position:, right:, top:, orientation: Vector.new(0, 0, 0),
|
||||
zoom: 1, left: 0, bottom: 0,
|
||||
min_view_distance: 0.1, max_view_distance: 200.0
|
||||
)
|
||||
@position = position
|
||||
@orientation = orientation
|
||||
|
||||
@zoom = zoom
|
||||
|
||||
@left = left
|
||||
@right = right
|
||||
@bottom = bottom
|
||||
@top = top
|
||||
|
||||
@min_view_distance = min_view_distance
|
||||
@max_view_distance = max_view_distance
|
||||
end
|
||||
|
||||
# Immediate mode renderering fallback
|
||||
def draw
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glLoadIdentity
|
||||
glOrtho(@left, @right, @bottom, @top, @min_view_distance, @max_view_distance)
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glRotatef(@orientation.x, 1, 0, 0)
|
||||
glRotatef(@orientation.y, 0, 1, 0)
|
||||
glTranslatef(-@position.x, -@position.y, -@position.z)
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
glLoadIdentity
|
||||
end
|
||||
|
||||
def projection_matrix
|
||||
Transform.orthographic(@left, @right, @bottom, @top, @min_view_distance, @max_view_distance)
|
||||
end
|
||||
|
||||
def view_matrix
|
||||
Transform.translate_3d(@position * -1) * Transform.rotate_3d(@orientation)
|
||||
end
|
||||
end
|
||||
end
|
||||
38
lib/cyberarm_engine/opengl/perspective_camera.rb
Normal file
38
lib/cyberarm_engine/opengl/perspective_camera.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
module CyberarmEngine
|
||||
class PerspectiveCamera
|
||||
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
|
||||
:min_view_distance, :max_view_distance
|
||||
|
||||
def initialize(position:, aspect_ratio:, orientation: Vector.new(0, 0,
|
||||
0), field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 155.0)
|
||||
@position = position
|
||||
@orientation = orientation
|
||||
|
||||
@aspect_ratio = aspect_ratio
|
||||
@field_of_view = field_of_view
|
||||
|
||||
@min_view_distance = min_view_distance
|
||||
@max_view_distance = max_view_distance
|
||||
end
|
||||
|
||||
def draw
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glLoadIdentity
|
||||
gluPerspective(@field_of_view, @aspect_ratio, @min_view_distance, @max_view_distance)
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glRotatef(@orientation.x, 1, 0, 0)
|
||||
glRotatef(@orientation.y, 0, 1, 0)
|
||||
glTranslatef(-@position.x, -@position.y, -@position.z)
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
glLoadIdentity
|
||||
end
|
||||
|
||||
def projection_matrix
|
||||
Transform.perspective(@field_of_view, @aspect_ratio, @min_view_distance, @max_view_distance)
|
||||
end
|
||||
|
||||
def view_matrix
|
||||
Transform.translate_3d(@position * -1) * Transform.rotate_3d(@orientation)
|
||||
end
|
||||
end
|
||||
end
|
||||
249
lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb
Normal file
249
lib/cyberarm_engine/opengl/renderer/bounding_box_renderer.rb
Normal file
@@ -0,0 +1,249 @@
|
||||
module CyberarmEngine
|
||||
class BoundingBoxRenderer
|
||||
attr_reader :bounding_boxes, :vertex_count
|
||||
|
||||
def initialize
|
||||
@bounding_boxes = {}
|
||||
@vertex_count = 0
|
||||
end
|
||||
|
||||
def render(entities)
|
||||
entities.each do |entity|
|
||||
create_bounding_box(entity, color = nil)
|
||||
draw_bounding_boxes
|
||||
end
|
||||
|
||||
(@bounding_boxes.keys - entities.map { |e| e.object_id }).each do |key|
|
||||
@bounding_boxes.delete(key)
|
||||
end
|
||||
end
|
||||
|
||||
def create_bounding_box(entity, color = nil)
|
||||
color ||= entity.debug_color
|
||||
entity_id = entity.object_id
|
||||
|
||||
if @bounding_boxes[entity_id]
|
||||
if @bounding_boxes[entity_id][:color] != color
|
||||
@bounding_boxes[entity_id][:colors] = mesh_colors(color).pack("f*")
|
||||
@bounding_boxes[entity_id][:color] = color
|
||||
return
|
||||
else
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
@bounding_boxes[entity_id] = {
|
||||
entity: entity,
|
||||
color: color,
|
||||
objects: []
|
||||
}
|
||||
|
||||
box = entity.normalize_bounding_box
|
||||
|
||||
normals = mesh_normals
|
||||
colors = mesh_colors(color)
|
||||
vertices = mesh_vertices(box)
|
||||
|
||||
@vertex_count += vertices.size
|
||||
|
||||
@bounding_boxes[entity_id][:vertices_size] = vertices.size
|
||||
@bounding_boxes[entity_id][:vertices] = vertices.pack("f*")
|
||||
@bounding_boxes[entity_id][:normals] = normals.pack("f*")
|
||||
@bounding_boxes[entity_id][:colors] = colors.pack("f*")
|
||||
|
||||
entity.model.objects.each do |mesh|
|
||||
data = {}
|
||||
box = mesh.bounding_box.normalize(entity)
|
||||
|
||||
normals = mesh_normals
|
||||
colors = mesh_colors(mesh.debug_color)
|
||||
vertices = mesh_vertices(box)
|
||||
|
||||
@vertex_count += vertices.size
|
||||
|
||||
data[:vertices_size] = vertices.size
|
||||
data[:vertices] = vertices.pack("f*")
|
||||
data[:normals] = normals.pack("f*")
|
||||
data[:colors] = colors.pack("f*")
|
||||
|
||||
@bounding_boxes[entity_id][:objects] << data
|
||||
end
|
||||
end
|
||||
|
||||
def mesh_normals
|
||||
[
|
||||
0, 1, 0,
|
||||
0, 1, 0,
|
||||
0, 1, 0,
|
||||
0, 1, 0,
|
||||
0, 1, 0,
|
||||
0, 1, 0,
|
||||
|
||||
0, -1, 0,
|
||||
0, -1, 0,
|
||||
0, -1, 0,
|
||||
0, -1, 0,
|
||||
0, -1, 0,
|
||||
0, -1, 0,
|
||||
|
||||
0, 0, 1,
|
||||
0, 0, 1,
|
||||
0, 0, 1,
|
||||
0, 0, 1,
|
||||
0, 0, 1,
|
||||
0, 0, 1,
|
||||
|
||||
1, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 0,
|
||||
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
|
||||
-1, 0, 0,
|
||||
-1, 0, 0,
|
||||
-1, 0, 0
|
||||
]
|
||||
end
|
||||
|
||||
def mesh_colors(color)
|
||||
[
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue,
|
||||
color.red, color.green, color.blue
|
||||
]
|
||||
end
|
||||
|
||||
def mesh_vertices(box)
|
||||
[
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.min.z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.max.y, box.max.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
|
||||
box.min.x, box.max.y, box.max.z,
|
||||
box.max.x, box.min.y, box.max.z,
|
||||
box.min.x, box.min.y, box.max.z,
|
||||
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
|
||||
box.max.x, box.min.y, box.min.z,
|
||||
box.min.x, box.max.y, box.min.z,
|
||||
box.max.x, box.max.y, box.min.z
|
||||
]
|
||||
end
|
||||
|
||||
def draw_bounding_boxes
|
||||
@bounding_boxes.each do |key, bounding_box|
|
||||
glPushMatrix
|
||||
|
||||
glTranslatef(
|
||||
bounding_box[:entity].position.x,
|
||||
bounding_box[:entity].position.y,
|
||||
bounding_box[:entity].position.z
|
||||
)
|
||||
draw_bounding_box(bounding_box)
|
||||
@bounding_boxes[key][:objects].each { |o| draw_bounding_box(o) }
|
||||
|
||||
glPopMatrix
|
||||
end
|
||||
end
|
||||
|
||||
def draw_bounding_box(bounding_box)
|
||||
glEnableClientState(GL_VERTEX_ARRAY)
|
||||
glEnableClientState(GL_COLOR_ARRAY)
|
||||
glEnableClientState(GL_NORMAL_ARRAY)
|
||||
|
||||
glVertexPointer(3, GL_FLOAT, 0, bounding_box[:vertices])
|
||||
glColorPointer(3, GL_FLOAT, 0, bounding_box[:colors])
|
||||
glNormalPointer(GL_FLOAT, 0, bounding_box[:normals])
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||
glDisable(GL_LIGHTING)
|
||||
glDrawArrays(GL_TRIANGLES, 0, bounding_box[:vertices_size] / 3)
|
||||
glEnable(GL_LIGHTING)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||
|
||||
glDisableClientState(GL_VERTEX_ARRAY)
|
||||
glDisableClientState(GL_COLOR_ARRAY)
|
||||
glDisableClientState(GL_NORMAL_ARRAY)
|
||||
end
|
||||
end
|
||||
end
|
||||
164
lib/cyberarm_engine/opengl/renderer/g_buffer.rb
Normal file
164
lib/cyberarm_engine/opengl/renderer/g_buffer.rb
Normal file
@@ -0,0 +1,164 @@
|
||||
module CyberarmEngine
|
||||
class GBuffer
|
||||
attr_reader :screen_vbo, :vertices, :uvs
|
||||
|
||||
def initialize(width:, height:)
|
||||
@width = width
|
||||
@height = height
|
||||
|
||||
@framebuffer = nil
|
||||
@buffers = %i[position diffuse normal texcoord]
|
||||
@textures = {}
|
||||
@screen_vbo = nil
|
||||
@ready = false
|
||||
|
||||
@vertices = [
|
||||
-1.0, -1.0, 0,
|
||||
1.0, -1.0, 0,
|
||||
-1.0, 1.0, 0,
|
||||
|
||||
-1.0, 1.0, 0,
|
||||
1.0, -1.0, 0,
|
||||
1.0, 1.0, 0
|
||||
].freeze
|
||||
|
||||
@uvs = [
|
||||
0, 0,
|
||||
1, 0,
|
||||
0, 1,
|
||||
|
||||
0, 1,
|
||||
1, 0,
|
||||
1, 1
|
||||
].freeze
|
||||
|
||||
create_framebuffer
|
||||
create_screen_vbo
|
||||
end
|
||||
|
||||
def create_framebuffer
|
||||
buffer = " " * 4
|
||||
glGenFramebuffers(1, buffer)
|
||||
@framebuffer = buffer.unpack1("L2")
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
|
||||
|
||||
create_textures
|
||||
|
||||
status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
|
||||
|
||||
if status != GL_FRAMEBUFFER_COMPLETE
|
||||
message = ""
|
||||
|
||||
message = case status
|
||||
when GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
|
||||
"GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
|
||||
when GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
|
||||
"GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
|
||||
when GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
|
||||
"GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"
|
||||
when GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER
|
||||
"GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"
|
||||
when GL_FRAMEBUFFER_UNSUPPORTED
|
||||
"GL_FRAMEBUFFER_UNSUPPORTED"
|
||||
else
|
||||
"Unknown error!"
|
||||
end
|
||||
puts "Incomplete framebuffer: #{status}\nError: #{message}"
|
||||
else
|
||||
@ready = true
|
||||
end
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
||||
end
|
||||
|
||||
def create_textures
|
||||
@buffers.size.times do |i|
|
||||
buffer = " " * 4
|
||||
glGenTextures(1, buffer)
|
||||
texture_id = buffer.unpack1("L2")
|
||||
@textures[@buffers[i]] = texture_id
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, @width, @height, 0, GL_RGBA, GL_FLOAT, nil)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
|
||||
end
|
||||
|
||||
buffer = " " * 4
|
||||
glGenTextures(1, buffer)
|
||||
texture_id = buffer.unpack1("L2")
|
||||
@textures[:depth] = texture_id
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, @width, @height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nil)
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0)
|
||||
|
||||
draw_buffers = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3]
|
||||
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
|
||||
end
|
||||
|
||||
def create_screen_vbo
|
||||
buffer = " " * 4
|
||||
glGenVertexArrays(1, buffer)
|
||||
@screen_vbo = buffer.unpack1("L2")
|
||||
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@positions_buffer_id = buffer.unpack1("L2")
|
||||
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@uvs_buffer_id = buffer.unpack1("L2")
|
||||
|
||||
glBindVertexArray(@screen_vbo)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, @vertices.size * Fiddle::SIZEOF_FLOAT, @vertices.pack("f*"), GL_STATIC_DRAW)
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, @uvs.size * Fiddle::SIZEOF_FLOAT, @uvs.pack("f*"), GL_STATIC_DRAW)
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
|
||||
glEnableVertexAttribArray(0)
|
||||
glEnableVertexAttribArray(1)
|
||||
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
|
||||
def bind_for_writing
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
|
||||
end
|
||||
|
||||
def bind_for_reading
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, @framebuffer)
|
||||
end
|
||||
|
||||
def set_read_buffer(buffer)
|
||||
glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
|
||||
end
|
||||
|
||||
def set_read_buffer_depth
|
||||
glReadBuffer(GL_DEPTH_ATTACHMENT)
|
||||
end
|
||||
|
||||
def unbind_framebuffer
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0)
|
||||
end
|
||||
|
||||
def texture(type)
|
||||
@textures[type]
|
||||
end
|
||||
|
||||
def clean_up
|
||||
glDeleteFramebuffers(1, [@framebuffer].pack("L"))
|
||||
|
||||
glDeleteTextures(@textures.values.size, @textures.values.pack("L*"))
|
||||
|
||||
glDeleteBuffers(2, [@positions_buffer_id, @uvs_buffer_id].pack("L*"))
|
||||
glDeleteVertexArrays(1, [@screen_vbo].pack("L"))
|
||||
gl_error?
|
||||
end
|
||||
end
|
||||
end
|
||||
289
lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb
Normal file
289
lib/cyberarm_engine/opengl/renderer/opengl_renderer.rb
Normal file
@@ -0,0 +1,289 @@
|
||||
module CyberarmEngine
|
||||
class OpenGLRenderer
|
||||
@@immediate_mode_warning = false
|
||||
|
||||
attr_accessor :show_wireframe
|
||||
|
||||
def initialize(width:, height:, show_wireframe: false)
|
||||
@width = width
|
||||
@height = height
|
||||
@show_wireframe = show_wireframe
|
||||
|
||||
@g_buffer = GBuffer.new(width: @width, height: @height)
|
||||
end
|
||||
|
||||
def canvas_size_changed
|
||||
@g_buffer.unbind_framebuffer
|
||||
@g_buffer.clean_up
|
||||
|
||||
@g_buffer = GBuffer.new(width: @width, height: @height)
|
||||
end
|
||||
|
||||
def render(camera, lights, entities)
|
||||
glViewport(0, 0, @width, @height)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
|
||||
if Shader.available?("g_buffer") && Shader.available?("lighting")
|
||||
@g_buffer.bind_for_writing
|
||||
gl_error?
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0)
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
|
||||
Shader.use("g_buffer") do |shader|
|
||||
gl_error?
|
||||
|
||||
entities.each do |entity|
|
||||
next unless entity.visible && entity.renderable
|
||||
|
||||
shader.uniform_transform("projection", camera.projection_matrix)
|
||||
shader.uniform_transform("view", camera.view_matrix)
|
||||
shader.uniform_transform("model", entity.model_matrix)
|
||||
shader.uniform_vec3("cameraPosition", camera.position)
|
||||
|
||||
gl_error?
|
||||
draw_model(entity.model, shader)
|
||||
entity.draw
|
||||
end
|
||||
end
|
||||
|
||||
@g_buffer.unbind_framebuffer
|
||||
gl_error?
|
||||
|
||||
@g_buffer.bind_for_reading
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
||||
|
||||
lighting(lights)
|
||||
gl_error?
|
||||
|
||||
post_processing
|
||||
gl_error?
|
||||
|
||||
# render_framebuffer
|
||||
gl_error?
|
||||
|
||||
@g_buffer.unbind_framebuffer
|
||||
gl_error?
|
||||
else
|
||||
unless @@immediate_mode_warning
|
||||
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..."
|
||||
end
|
||||
@@immediate_mode_warning = true
|
||||
|
||||
gl_error?
|
||||
lights.each(&:draw)
|
||||
camera.draw
|
||||
|
||||
glEnable(GL_NORMALIZE)
|
||||
entities.each do |entity|
|
||||
next unless entity.visible && entity.renderable
|
||||
|
||||
glPushMatrix
|
||||
|
||||
glTranslatef(entity.position.x, entity.position.y, entity.position.z)
|
||||
glScalef(entity.scale.x, entity.scale.y, entity.scale.z)
|
||||
glRotatef(entity.orientation.x, 1.0, 0, 0)
|
||||
glRotatef(entity.orientation.y, 0, 1.0, 0)
|
||||
glRotatef(entity.orientation.z, 0, 0, 1.0)
|
||||
|
||||
gl_error?
|
||||
draw_mesh(entity.model)
|
||||
entity.draw
|
||||
glPopMatrix
|
||||
end
|
||||
end
|
||||
|
||||
gl_error?
|
||||
end
|
||||
|
||||
def copy_g_buffer_to_screen
|
||||
@g_buffer.set_read_buffer(:position)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
|
||||
@g_buffer.set_read_buffer(:diffuse)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
|
||||
@g_buffer.set_read_buffer(:normal)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
@g_buffer.width / 2, @g_buffer.height / 2, @g_buffer.width, @g_buffer.height,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
|
||||
@g_buffer.set_read_buffer(:texcoord)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
@g_buffer.width / 2, 0, @g_buffer.width, @g_buffer.height / 2,
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
end
|
||||
|
||||
def lighting(lights)
|
||||
Shader.use("lighting") do |shader|
|
||||
glBindVertexArray(@g_buffer.screen_vbo)
|
||||
|
||||
glDisable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
||||
shader.uniform_integer("diffuse", 0)
|
||||
|
||||
glActiveTexture(GL_TEXTURE1)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
|
||||
shader.uniform_integer("position", 1)
|
||||
|
||||
glActiveTexture(GL_TEXTURE2)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
|
||||
shader.uniform_integer("texcoord", 2)
|
||||
|
||||
glActiveTexture(GL_TEXTURE3)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
|
||||
shader.uniform_integer("normal", 3)
|
||||
|
||||
glActiveTexture(GL_TEXTURE4)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
||||
shader.uniform_integer("depth", 4)
|
||||
|
||||
lights.each_with_index do |light, _i|
|
||||
shader.uniform_integer("light[0].type", light.type)
|
||||
shader.uniform_vec3("light[0].direction", light.direction)
|
||||
shader.uniform_vec3("light[0].position", light.position)
|
||||
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
||||
shader.uniform_vec3("light[0].ambient", light.ambient)
|
||||
shader.uniform_vec3("light[0].specular", light.specular)
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
end
|
||||
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
end
|
||||
|
||||
def post_processing
|
||||
end
|
||||
|
||||
def render_framebuffer
|
||||
if Shader.available?("lighting")
|
||||
Shader.use("lighting") do |shader|
|
||||
glBindVertexArray(@g_buffer.screen_vbo)
|
||||
|
||||
glDisable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
||||
shader.uniform_integer("diffuse_texture", 0)
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def draw_model(model, shader)
|
||||
glBindVertexArray(model.vertex_array_id)
|
||||
glEnableVertexAttribArray(0)
|
||||
glEnableVertexAttribArray(1)
|
||||
glEnableVertexAttribArray(2)
|
||||
if model.has_texture?
|
||||
glEnableVertexAttribArray(3)
|
||||
glEnableVertexAttribArray(4)
|
||||
end
|
||||
|
||||
if @show_wireframe
|
||||
glLineWidth(2)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||
Shader.active_shader.uniform_boolean("disableLighting", true)
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, model.faces.count * 3)
|
||||
|
||||
Shader.active_shader.uniform_boolean("disableLighting", false)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||
glLineWidth(1)
|
||||
end
|
||||
|
||||
offset = 0
|
||||
model.objects.each do |object|
|
||||
shader.uniform_boolean("hasTexture", object.has_texture?)
|
||||
|
||||
if object.has_texture?
|
||||
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
||||
else
|
||||
glBindTexture(GL_TEXTURE_2D, 0)
|
||||
end
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, offset, object.faces.count * 3)
|
||||
offset += object.faces.count * 3
|
||||
end
|
||||
|
||||
if model.has_texture?
|
||||
glDisableVertexAttribArray(4)
|
||||
glDisableVertexAttribArray(3)
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0)
|
||||
end
|
||||
glDisableVertexAttribArray(2)
|
||||
glDisableVertexAttribArray(1)
|
||||
glDisableVertexAttribArray(0)
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
|
||||
def draw_mesh(model)
|
||||
model.objects.each_with_index do |o, _i|
|
||||
glEnable(GL_COLOR_MATERIAL)
|
||||
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
||||
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
||||
glShadeModel(GL_SMOOTH) if o.faces.first[4]
|
||||
glEnableClientState(GL_VERTEX_ARRAY)
|
||||
glEnableClientState(GL_COLOR_ARRAY)
|
||||
glEnableClientState(GL_NORMAL_ARRAY)
|
||||
|
||||
if o.has_texture?
|
||||
glEnable(GL_TEXTURE_2D)
|
||||
glBindTexture(GL_TEXTURE_2D, o.materials.find { |mat| mat.texture_id }.texture_id)
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_uvs)
|
||||
end
|
||||
|
||||
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
|
||||
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials)
|
||||
glNormalPointer(GL_FLOAT, 0, o.flattened_normals)
|
||||
|
||||
if @show_wireframe # This is kinda expensive
|
||||
glDisable(GL_LIGHTING)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||
glPolygonOffset(2, 0.5)
|
||||
glLineWidth(3)
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
||||
|
||||
glLineWidth(1)
|
||||
glPolygonOffset(0, 0)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||
glEnable(GL_LIGHTING)
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
||||
else
|
||||
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
|
||||
end
|
||||
|
||||
# glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
|
||||
glDisableClientState(GL_VERTEX_ARRAY)
|
||||
glDisableClientState(GL_COLOR_ARRAY)
|
||||
glDisableClientState(GL_NORMAL_ARRAY)
|
||||
|
||||
if o.has_texture?
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||
glDisable(GL_TEXTURE_2D)
|
||||
end
|
||||
|
||||
glDisable(GL_COLOR_MATERIAL)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
22
lib/cyberarm_engine/opengl/renderer/renderer.rb
Normal file
22
lib/cyberarm_engine/opengl/renderer/renderer.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
module CyberarmEngine
|
||||
class Renderer
|
||||
attr_reader :opengl_renderer, :bounding_box_renderer
|
||||
|
||||
def initialize
|
||||
@bounding_box_renderer = BoundingBoxRenderer.new
|
||||
@opengl_renderer = OpenGLRenderer.new(width: $window.width, height: $window.height)
|
||||
end
|
||||
|
||||
def draw(camera, lights, entities)
|
||||
@opengl_renderer.render(camera, lights, entities)
|
||||
@bounding_box_renderer.render(entities) if @show_bounding_boxes
|
||||
end
|
||||
|
||||
def canvas_size_changed
|
||||
@opengl_renderer.canvas_size_changed
|
||||
end
|
||||
|
||||
def finalize # cleanup
|
||||
end
|
||||
end
|
||||
end
|
||||
406
lib/cyberarm_engine/opengl/shader.rb
Normal file
406
lib/cyberarm_engine/opengl/shader.rb
Normal file
@@ -0,0 +1,406 @@
|
||||
module CyberarmEngine
|
||||
# Ref: https://github.com/vaiorabbit/ruby-opengl/blob/master/sample/OrangeBook/brick.rb
|
||||
class Shader
|
||||
include OpenGL
|
||||
@@shaders = {} # Cache for {Shader} instances
|
||||
PREPROCESSOR_CHARACTER = "@".freeze # magic character for preprocessor phase of {Shader} compilation
|
||||
|
||||
# add instance of {Shader} to cache
|
||||
#
|
||||
# @param name [String]
|
||||
# @param instance [Shader]
|
||||
def self.add(name, instance)
|
||||
@@shaders[name] = instance
|
||||
end
|
||||
|
||||
# removes {Shader} from cache and cleans up
|
||||
#
|
||||
# @param name [String]
|
||||
def self.delete(name)
|
||||
shader = @@shaders.dig(name)
|
||||
|
||||
if shader
|
||||
@@shaders.delete(name)
|
||||
|
||||
glDeleteProgram(shader.program) if shader.compiled?
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# runs _block_ using {Shader} with _name_
|
||||
#
|
||||
# @example
|
||||
#
|
||||
# CyberarmEngine::Shader.use("blur") do |shader|
|
||||
# shader.uniform_float("radius", 20.0)
|
||||
# # OpenGL Code that uses shader
|
||||
# end
|
||||
#
|
||||
# @param name [String] name of {Shader} to use
|
||||
# @return [void]
|
||||
def self.use(name, &block)
|
||||
shader = @@shaders.dig(name)
|
||||
if shader
|
||||
shader.use(&block)
|
||||
else
|
||||
raise ArgumentError, "Shader '#{name}' not found!"
|
||||
end
|
||||
end
|
||||
|
||||
# returns whether {Shader} with _name_ is in cache
|
||||
#
|
||||
# @param name [String]
|
||||
# @return [Boolean]
|
||||
def self.available?(name)
|
||||
@@shaders.dig(name).is_a?(Shader)
|
||||
end
|
||||
|
||||
# returns instance of {Shader}, if it exists
|
||||
#
|
||||
# @param name [String]
|
||||
# @return [Shader?]
|
||||
def self.get(name)
|
||||
@@shaders.dig(name)
|
||||
end
|
||||
|
||||
# returns currently active {Shader}, if one is active
|
||||
#
|
||||
# @return [Shader?]
|
||||
class << self
|
||||
attr_reader :active_shader
|
||||
end
|
||||
|
||||
# sets currently active {Shader}
|
||||
#
|
||||
# @param instance [Shader] instance of {Shader} to set as active
|
||||
class << self
|
||||
attr_writer :active_shader
|
||||
end
|
||||
|
||||
# stops using currently active {Shader}
|
||||
def self.stop
|
||||
shader = Shader.active_shader
|
||||
|
||||
if shader
|
||||
shader.stop
|
||||
else
|
||||
raise ArgumentError, "No active shader to stop!"
|
||||
end
|
||||
end
|
||||
|
||||
# returns location of OpenGL Shader uniform
|
||||
#
|
||||
# @param variable [String]
|
||||
def self.attribute_location(variable)
|
||||
raise "No active shader!" unless Shader.active_shader
|
||||
|
||||
Shader.active_shader.attribute_location(variable)
|
||||
end
|
||||
|
||||
# sets _variable_ to _value_
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value
|
||||
def self.set_uniform(variable, value)
|
||||
raise "No active shader!" unless Shader.active_shader
|
||||
|
||||
Shader.active_shader.set_uniform(variable, value)
|
||||
end
|
||||
|
||||
attr_reader :name, :program
|
||||
|
||||
def initialize(name:, fragment:, includes_dir: nil, vertex: "shaders/default.vert")
|
||||
raise "Shader name can not be blank" if name.length == 0
|
||||
|
||||
@name = name
|
||||
@includes_dir = includes_dir
|
||||
@compiled = false
|
||||
|
||||
@program = nil
|
||||
|
||||
@error_buffer_size = 1024 * 8
|
||||
@variable_missing = {}
|
||||
|
||||
@data = { shaders: {} }
|
||||
|
||||
unless shader_files_exist?(vertex: vertex, fragment: fragment)
|
||||
raise ArgumentError, "Shader files not found: #{vertex} or #{fragment}"
|
||||
end
|
||||
|
||||
create_shader(type: :vertex, source: File.read(vertex))
|
||||
create_shader(type: :fragment, source: File.read(fragment))
|
||||
|
||||
compile_shader(type: :vertex)
|
||||
compile_shader(type: :fragment)
|
||||
link_shaders
|
||||
|
||||
@data[:shaders].each { |_key, id| glDeleteShader(id) }
|
||||
|
||||
# Only add shader if it successfully compiles
|
||||
if @compiled
|
||||
puts "compiled!"
|
||||
puts "Compiled shader: #{@name}"
|
||||
Shader.add(@name, self)
|
||||
else
|
||||
glDeleteProgram(@program)
|
||||
warn "FAILED to compile shader: #{@name}", ""
|
||||
end
|
||||
end
|
||||
|
||||
# whether vertex and fragment files exist on disk
|
||||
#
|
||||
# @return [Boolean]
|
||||
def shader_files_exist?(vertex:, fragment:)
|
||||
File.exist?(vertex) && File.exist?(fragment)
|
||||
end
|
||||
|
||||
# creates an OpenGL Shader of _type_ using _source_
|
||||
#
|
||||
# @param type [Symbol] valid values are: :vertex, :fragment
|
||||
# @param source [String] source code for shader
|
||||
def create_shader(type:, source:)
|
||||
_shader = nil
|
||||
|
||||
case type
|
||||
when :vertex
|
||||
_shader = glCreateShader(GL_VERTEX_SHADER)
|
||||
when :fragment
|
||||
_shader = glCreateShader(GL_FRAGMENT_SHADER)
|
||||
else
|
||||
raise ArgumentError, "Unsupported shader type: #{type.inspect}"
|
||||
end
|
||||
|
||||
processed_source = preprocess_source(source: source)
|
||||
|
||||
_source = [processed_source].pack("p")
|
||||
_size = [processed_source.length].pack("I")
|
||||
glShaderSource(_shader, 1, _source, _size)
|
||||
|
||||
@data[:shaders][type] = _shader
|
||||
end
|
||||
|
||||
# evaluates shader preprocessors
|
||||
#
|
||||
# currently supported preprocessors:
|
||||
#
|
||||
# @include "file/path" "another/file/path" # => Replace line with contents of file; Shader includes_dir must be specified in constructor
|
||||
#
|
||||
# @example
|
||||
# # Example Vertex Shader #
|
||||
# # #version 330 core
|
||||
# # @include "material_struct"
|
||||
# # void main() {
|
||||
# # gl_Position = vec4(1, 1, 1, 1);
|
||||
# # }
|
||||
#
|
||||
# Shader.new(name: "model_renderer", includes_dir: "path/to/includes", vertex: "path/to/vertex_shader.glsl")
|
||||
#
|
||||
# @param source shader source code
|
||||
def preprocess_source(source:)
|
||||
lines = source.lines
|
||||
|
||||
lines.each_with_index do |line, i|
|
||||
next unless line.start_with?(PREPROCESSOR_CHARACTER)
|
||||
|
||||
preprocessor = line.strip.split(" ")
|
||||
lines.delete(line)
|
||||
|
||||
case preprocessor.first
|
||||
when "@include"
|
||||
unless @includes_dir
|
||||
raise ArgumentError,
|
||||
"Shader preprocessor include directory was not given for shader #{@name}"
|
||||
end
|
||||
|
||||
preprocessor[1..preprocessor.length - 1].join.scan(/"([^"]*)"/).flatten.each do |file|
|
||||
source = File.read("#{@includes_dir}/#{file}.glsl")
|
||||
|
||||
lines.insert(i, source)
|
||||
end
|
||||
else
|
||||
warn "Unsupported preprocessor #{preprocessor.first} for #{@name}"
|
||||
end
|
||||
end
|
||||
|
||||
lines.join
|
||||
end
|
||||
|
||||
# compile OpenGL Shader of _type_
|
||||
#
|
||||
# @return [Boolean] whether compilation succeeded
|
||||
def compile_shader(type:)
|
||||
_compiled = false
|
||||
_shader = @data[:shaders][type]
|
||||
raise ArgumentError, "No shader for #{type.inspect}" unless _shader
|
||||
|
||||
glCompileShader(_shader)
|
||||
buffer = " "
|
||||
glGetShaderiv(_shader, GL_COMPILE_STATUS, buffer)
|
||||
compiled = buffer.unpack1("L")
|
||||
|
||||
if compiled == 0
|
||||
log = " " * @error_buffer_size
|
||||
glGetShaderInfoLog(_shader, @error_buffer_size, nil, log)
|
||||
puts "Shader Error: Program \"#{@name}\""
|
||||
puts " #{type.to_s.capitalize} Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
|
||||
puts " Shader Compiled status: #{compiled}"
|
||||
puts " NOTE: assignment of uniforms in shaders is illegal!"
|
||||
puts
|
||||
else
|
||||
_compiled = true
|
||||
end
|
||||
|
||||
_compiled
|
||||
end
|
||||
|
||||
# link compiled OpenGL Shaders in to a OpenGL Program
|
||||
#
|
||||
# @note linking must succeed or shader cannot be used
|
||||
#
|
||||
# @return [Boolean] whether linking succeeded
|
||||
def link_shaders
|
||||
@program = glCreateProgram
|
||||
@data[:shaders].values.each do |_shader|
|
||||
glAttachShader(@program, _shader)
|
||||
end
|
||||
glLinkProgram(@program)
|
||||
|
||||
buffer = " "
|
||||
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
|
||||
linked = buffer.unpack1("L")
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
# Returns the location of a uniform _variable_
|
||||
#
|
||||
# @param variable [String]
|
||||
# @return [Integer] location of uniform
|
||||
def variable(variable)
|
||||
loc = glGetUniformLocation(@program, variable)
|
||||
if loc == -1
|
||||
unless @variable_missing[variable]
|
||||
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?"
|
||||
end
|
||||
@variable_missing[variable] = true
|
||||
end
|
||||
loc
|
||||
end
|
||||
|
||||
# @see Shader.use Shader.use
|
||||
def use(&block)
|
||||
return unless compiled?
|
||||
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
|
||||
|
||||
Shader.active_shader = self
|
||||
|
||||
glUseProgram(@program)
|
||||
|
||||
if block
|
||||
block.call(self)
|
||||
stop
|
||||
end
|
||||
end
|
||||
|
||||
# stop using shader, if shader is active
|
||||
def stop
|
||||
Shader.active_shader = nil if Shader.active_shader == self
|
||||
glUseProgram(0)
|
||||
end
|
||||
|
||||
# @return [Boolean] whether {Shader} successfully compiled
|
||||
def compiled?
|
||||
@compiled
|
||||
end
|
||||
|
||||
# returns location of a uniform _variable_
|
||||
#
|
||||
# @note Use {#variable} for friendly error handling
|
||||
# @see #variable Shader#variable
|
||||
#
|
||||
# @param variable [String]
|
||||
# @return [Integer]
|
||||
def attribute_location(variable)
|
||||
glGetUniformLocation(@program, variable)
|
||||
end
|
||||
|
||||
# send {Transform} to {Shader}
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value [Transform]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_transform(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniformMatrix4fv(attr_loc, 1, GL_FALSE, value.to_gl.pack("F16"))
|
||||
end
|
||||
|
||||
# send Boolean to {Shader}
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value [Boolean]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_boolean(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform1i(attr_loc, value ? 1 : 0)
|
||||
end
|
||||
|
||||
# send Integer to {Shader}
|
||||
# @param variable [String]
|
||||
# @param value [Integer]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_integer(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform1i(attr_loc, value)
|
||||
end
|
||||
|
||||
# send Float to {Shader}
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value [Float]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_float(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform1f(attr_loc, value)
|
||||
end
|
||||
|
||||
# send {Vector} (x, y, z) to {Shader}
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value [Vector]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_vec3(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform3f(attr_loc, *value.to_a[0..2])
|
||||
end
|
||||
|
||||
# send {Vector} to {Shader}
|
||||
#
|
||||
# @param variable [String]
|
||||
# @param value [Vector]
|
||||
# @param location [Integer]
|
||||
# @return [void]
|
||||
def uniform_vec4(variable, value, location = nil)
|
||||
attr_loc = location || attribute_location(variable)
|
||||
|
||||
glUniform4f(attr_loc, *value.to_a)
|
||||
end
|
||||
end
|
||||
end
|
||||
69
lib/cyberarm_engine/opengl/texture.rb
Normal file
69
lib/cyberarm_engine/opengl/texture.rb
Normal file
@@ -0,0 +1,69 @@
|
||||
module CyberarmEngine
|
||||
class Texture
|
||||
DEFAULT_TEXTURE = "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/default.png".freeze
|
||||
|
||||
CACHE = {}
|
||||
|
||||
def self.release_textures
|
||||
CACHE.values.each do |id|
|
||||
glDeleteTextures(id)
|
||||
end
|
||||
end
|
||||
|
||||
def self.from_cache(path, retro)
|
||||
CACHE.dig("#{path}?retro=#{retro}")
|
||||
end
|
||||
|
||||
attr_reader :id
|
||||
|
||||
def initialize(path: nil, image: nil, retro: false)
|
||||
raise "keyword :path or :image must be provided!" if path.nil? && image.nil?
|
||||
|
||||
@retro = retro
|
||||
@path = path
|
||||
|
||||
if @path
|
||||
unless File.exist?(@path)
|
||||
warn "Missing texture at: #{@path}"
|
||||
@retro = true # override retro setting
|
||||
@path = DEFAULT_TEXTURE
|
||||
end
|
||||
|
||||
if texture = Texture.from_cache(@path, @retro)
|
||||
@id = texture.id
|
||||
return
|
||||
end
|
||||
|
||||
image = load_image(@path)
|
||||
@id = create_from_image(image)
|
||||
else
|
||||
@id = create_from_image(image)
|
||||
end
|
||||
end
|
||||
|
||||
def load_image(path)
|
||||
CACHE["#{path}?retro=#{@retro}"] = self
|
||||
Gosu::Image.new(path, retro: @retro)
|
||||
end
|
||||
|
||||
def create_from_image(image)
|
||||
array_of_pixels = image.to_blob
|
||||
|
||||
tex_names_buf = " " * 4
|
||||
glGenTextures(1, tex_names_buf)
|
||||
texture_id = tex_names_buf.unpack1("L2")
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) if @retro
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) unless @retro
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
|
||||
glGenerateMipmap(GL_TEXTURE_2D)
|
||||
gl_error?
|
||||
|
||||
texture_id
|
||||
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))
|
||||
|
||||
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
|
||||
21
lib/cyberarm_engine/stats.rb
Normal file
21
lib/cyberarm_engine/stats.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module CyberarmEngine
|
||||
class Stats
|
||||
@@hash = {
|
||||
gui_recalculations_last_frame: 0
|
||||
}
|
||||
|
||||
def self.get(key)
|
||||
@@hash.dig(key)
|
||||
end
|
||||
|
||||
def self.increment(key, n)
|
||||
@@hash[key] += n
|
||||
end
|
||||
|
||||
def self.clear
|
||||
@@hash.each do |key, _value|
|
||||
@@hash[key] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
170
lib/cyberarm_engine/text.rb
Normal file
170
lib/cyberarm_engine/text.rb
Normal file
@@ -0,0 +1,170 @@
|
||||
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] || 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
|
||||
@mode = options[:mode] || :default
|
||||
@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] || 1
|
||||
@shadow_alpha = options[:shadow_alpha] || 30
|
||||
@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
|
||||
|
||||
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
|
||||
|
||||
font
|
||||
end
|
||||
|
||||
def swap_font(size, font_name = @font)
|
||||
if @size != size || @font != font_name
|
||||
@size = size
|
||||
@font = font_name
|
||||
|
||||
@textobject = check_cache(size, font_name)
|
||||
end
|
||||
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(text = @text)
|
||||
textobject.text_width(text)
|
||||
end
|
||||
|
||||
def markup_width(text = @text)
|
||||
textobject.markup_width(text)
|
||||
end
|
||||
|
||||
def height(text = @text)
|
||||
text.lines.count > 0 ? text.lines.count * textobject.height : @textobject.height
|
||||
end
|
||||
|
||||
def draw(method = :draw_markup)
|
||||
if @shadow && !ARGV.join.include?("--no-shadow")
|
||||
shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
|
||||
shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue,
|
||||
shadow_alpha)
|
||||
white = Gosu::Color::WHITE
|
||||
|
||||
_x = @shadow_size
|
||||
_y = @shadow_size
|
||||
|
||||
@rendered_shadow ||= Gosu.render((width + (shadow_size * 2)).ceil, (height + (@shadow_size * 2)).ceil) do
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
|
||||
@textobject.send(method, @text, _x, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
|
||||
@textobject.send(method, @text, _x, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x - @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y, @z, @factor_x, @factor_y, white, :add)
|
||||
@textobject.send(method, @text, _x + @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add)
|
||||
end
|
||||
|
||||
@rendered_shadow.draw(@x - @shadow_size, @y - @shadow_size, @z, @factor_x, @factor_y, shadow_color)
|
||||
end
|
||||
|
||||
@textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode)
|
||||
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
|
||||
@@ -20,4 +20,4 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
296
lib/cyberarm_engine/transform.rb
Normal file
296
lib/cyberarm_engine/transform.rb
Normal file
@@ -0,0 +1,296 @@
|
||||
module CyberarmEngine
|
||||
# Basic 4x4 matrix operations
|
||||
class Transform
|
||||
attr_reader :elements
|
||||
|
||||
def initialize(matrix)
|
||||
@elements = matrix
|
||||
|
||||
raise "Transform is wrong size! Got #{@elements.size}, expected 16" if 16 != @elements.size
|
||||
raise "Invalid value for matrix, must all be numeric!" if @elements.any? { |e| e.nil? || !e.is_a?(Numeric) }
|
||||
end
|
||||
|
||||
def self.identity
|
||||
Transform.new(
|
||||
[
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
### 2D Operations meant for interacting with Gosu ###
|
||||
|
||||
# 2d rotate operation, replicates Gosu's Gosu.rotate function
|
||||
def self.rotate(angle, rotate_around = nil)
|
||||
double c = Math.cos(angle).degrees_to_radians
|
||||
double s = Math.sin(angle).degrees_to_radians
|
||||
matrix = [
|
||||
+c, +s, 0, 0,
|
||||
-s, +c, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
|
||||
rotate_matrix = Transform.new(matrix, rows: 4, columns: 4)
|
||||
|
||||
if rotate_around && (rotate_around.x != 0.0 || rotate_around.y != 0.0)
|
||||
negative_rotate_around = Vector.new(-rotate_around.x, -rotate_around.y, -rotate_around.z)
|
||||
|
||||
rotate_matrix = concat(
|
||||
concat(translate(negative_rotate_around), rotate_matrix),
|
||||
translate(rotate_around)
|
||||
)
|
||||
end
|
||||
|
||||
rotate_matrix
|
||||
end
|
||||
|
||||
# 2d translate operation, replicates Gosu's Gosu.translate function
|
||||
def self.translate(vector)
|
||||
x, y, z = vector.to_a[0..2]
|
||||
matrix = [
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
x, y, z, 1
|
||||
]
|
||||
|
||||
Transform.new(matrix)
|
||||
end
|
||||
|
||||
# 2d scale operation, replicates Gosu's Gosu.rotate function
|
||||
def self.scale(vector, center_around = nil)
|
||||
scale_x, scale_y, scale_z = vector.to_a[0..2]
|
||||
matrix = [
|
||||
scale_x, 0, 0, 0,
|
||||
0, scale_y, 0, 0,
|
||||
0, 0, scale_z, 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
|
||||
scale_matrix = Transform.new(matrix)
|
||||
|
||||
if center_around && (center_around.x != 0.0 || center_around.y != 0.0)
|
||||
negative_center_around = Vector.new(-center_around.x, -center_around.y, -center_around.z)
|
||||
|
||||
scale_matrix = concat(
|
||||
concat(translate(negative_center_around), scale_matrix),
|
||||
translate(center_around)
|
||||
)
|
||||
end
|
||||
|
||||
scale_matrix
|
||||
end
|
||||
|
||||
def self.concat(left, right)
|
||||
matrix = Array.new(left.elements.size)
|
||||
rows = 4
|
||||
|
||||
matrix.size.times do |i|
|
||||
matrix[i] = 0
|
||||
|
||||
rows.times do |j|
|
||||
matrix[i] += left.elements[i / rows * rows + j] * right.elements[i % rows + j * rows]
|
||||
end
|
||||
end
|
||||
|
||||
Transform.new(matrix)
|
||||
end
|
||||
|
||||
#### 3D Operations meant for OpenGL ###
|
||||
|
||||
def self.translate_3d(vector)
|
||||
x, y, z = vector.to_a[0..2]
|
||||
matrix = [
|
||||
1, 0, 0, x,
|
||||
0, 1, 0, y,
|
||||
0, 0, 1, z,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
|
||||
Transform.new(matrix)
|
||||
end
|
||||
|
||||
def self.rotate_3d(vector, _order = "zyx")
|
||||
x, y, z = vector.to_a[0..2].map { |axis| axis * Math::PI / 180.0 }
|
||||
|
||||
rotation_x = Transform.new(
|
||||
[
|
||||
1, 0, 0, 0,
|
||||
0, Math.cos(x), -Math.sin(x), 0,
|
||||
0, Math.sin(x), Math.cos(x), 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
)
|
||||
|
||||
rotation_y = Transform.new(
|
||||
[
|
||||
Math.cos(y), 0, Math.sin(y), 0,
|
||||
0, 1, 0, 0,
|
||||
-Math.sin(y), 0, Math.cos(y), 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
)
|
||||
|
||||
rotation_z = Transform.new(
|
||||
[
|
||||
Math.cos(z), -Math.sin(z), 0, 0,
|
||||
Math.sin(z), Math.cos(z), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
)
|
||||
|
||||
rotation_z * rotation_y * rotation_x
|
||||
end
|
||||
|
||||
# Implements glRotatef
|
||||
# https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml
|
||||
def self.rotate_gl(angle, axis)
|
||||
radians = angle * Math::PI / 180.0
|
||||
s = Math.sin(radians)
|
||||
c = Math.cos(radians)
|
||||
|
||||
axis = axis.normalized
|
||||
x, y, z = axis.to_a[0..2]
|
||||
|
||||
n = (1.0 - c)
|
||||
|
||||
Transform.new(
|
||||
[
|
||||
x * x * n + c, x * y * n - z * s, x * z * n + y * s, 0,
|
||||
y * x * n + z * s, y * y * n + c, y * z * n - x * s, 0,
|
||||
x * z * n - y * s, y * z * n + x * s, z * z * n + c, 0,
|
||||
0, 0, 0, 1.0
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def self.scale_3d(vector)
|
||||
x, y, z = vector.to_a[0..2]
|
||||
|
||||
Transform.new(
|
||||
[
|
||||
x, 0, 0, 0,
|
||||
0, y, 0, 0,
|
||||
0, 0, z, 0,
|
||||
0, 0, 0, 1
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def self.perspective(fov_y, aspect_ratio, near, far)
|
||||
f = 1.0 / Math.tan(fov_y.degrees_to_radians / 2.0) # cotangent
|
||||
zn = (far + near.to_f) / (near - far.to_f)
|
||||
zf = (2.0 * far * near.to_f) / (near - far.to_f)
|
||||
|
||||
Transform.new(
|
||||
[
|
||||
f / aspect_ratio, 0.0, 0.0, 0.0,
|
||||
0.0, f, 0.0, 0.0,
|
||||
0.0, 0.0, zn, zf,
|
||||
0.0, 0.0, -1.0, 0.0
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def self.orthographic(left, right, bottom, top, near, far)
|
||||
s = Vector.new(
|
||||
2 / (right - left.to_f),
|
||||
2 / (top - bottom.to_f),
|
||||
-2 / (far - near.to_f)
|
||||
)
|
||||
|
||||
t = Vector.new(
|
||||
(right + left.to_f) / (right - left.to_f),
|
||||
(top + bottom.to_f) / (top - bottom.to_f),
|
||||
(far + near.to_f) / (far - near.to_f)
|
||||
)
|
||||
|
||||
Transform.new(
|
||||
[
|
||||
s.x, 0.0, 0.0, t.x,
|
||||
0.0, s.y, 0.0, t.y,
|
||||
0.0, 0.0, s.z, t.z,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def self.view(eye, orientation)
|
||||
# https://www.3dgep.com/understanding-the-view-matrix/#The_View_Matrix
|
||||
cosPitch = Math.cos(orientation.z * Math::PI / 180.0)
|
||||
sinPitch = Math.sin(orientation.z * Math::PI / 180.0)
|
||||
cosYaw = Math.cos(orientation.y * Math::PI / 180.0)
|
||||
sinYaw = Math.sin(orientation.y * Math::PI / 180.0)
|
||||
|
||||
x_axis = Vector.new(cosYaw, 0, -sinYaw)
|
||||
y_axis = Vector.new(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch)
|
||||
z_axis = Vector.new(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw)
|
||||
|
||||
Transform.new(
|
||||
[
|
||||
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||
x_axis.x, y_axis.y, z_axis.z, 0,
|
||||
-x_axis.dot(eye), -y_axis.dot(eye), -z_axis.dot(eye), 1
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def *(other)
|
||||
case other
|
||||
when CyberarmEngine::Vector
|
||||
matrix = @elements.clone
|
||||
list = other.to_a
|
||||
|
||||
@elements.each_with_index do |e, i|
|
||||
matrix[i] = e + list[i % 4]
|
||||
end
|
||||
|
||||
Transform.new(matrix)
|
||||
|
||||
when CyberarmEngine::Transform
|
||||
multiply_matrices(other)
|
||||
else
|
||||
p other.class
|
||||
raise TypeError, "Expected CyberarmEngine::Vector or CyberarmEngine::Transform got #{other.class}"
|
||||
end
|
||||
end
|
||||
|
||||
def get(x, y)
|
||||
width = 4
|
||||
|
||||
# puts "Transform|#{self.object_id} -> #{@elements[width * y + x].inspect} (index: #{width * y + x})"
|
||||
@elements[width * y + x]
|
||||
end
|
||||
|
||||
def multiply_matrices(other)
|
||||
matrix = Array.new(16, 0)
|
||||
|
||||
4.times do |x|
|
||||
4.times do |y|
|
||||
4.times do |k|
|
||||
matrix[4 * y + x] += get(x, k) * other.get(k, y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Transform.new(matrix)
|
||||
end
|
||||
|
||||
# arranges Matrix in column major form
|
||||
def to_gl
|
||||
e = @elements
|
||||
[
|
||||
e[0], e[4], e[8], e[12],
|
||||
e[1], e[5], e[9], e[13],
|
||||
e[2], e[6], e[10], e[14],
|
||||
e[3], e[7], e[11], e[15]
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,7 @@
|
||||
module CyberarmEngine
|
||||
class BorderCanvas
|
||||
attr_reader :element, :top, :right, :bottom, :left
|
||||
|
||||
def initialize(element:)
|
||||
@element = element
|
||||
|
||||
@@ -25,7 +26,7 @@ module CyberarmEngine
|
||||
|
||||
elsif color.is_a?(Array)
|
||||
if color.size == 1
|
||||
color=color.first
|
||||
color = color.first
|
||||
|
||||
elsif color.size == 2
|
||||
@top.background = color.first
|
||||
@@ -61,7 +62,7 @@ module CyberarmEngine
|
||||
|
||||
def update
|
||||
# TOP
|
||||
@top.x = @element.x# + @element.border_thickness_left
|
||||
@top.x = @element.x # + @element.border_thickness_left
|
||||
@top.y = @element.y
|
||||
@top.z = @element.z
|
||||
|
||||
@@ -98,4 +99,4 @@ module CyberarmEngine
|
||||
@left.update
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,102 +1,139 @@
|
||||
module CyberarmEngine
|
||||
module DSL
|
||||
def flow(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_container = Element::Flow.new(options, block)
|
||||
@containers << _container
|
||||
_container.build
|
||||
_container.parent.add(_container)
|
||||
@containers.pop
|
||||
|
||||
return _container
|
||||
container(CyberarmEngine::Element::Flow, options, &block)
|
||||
end
|
||||
|
||||
def stack(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:theme] = current_theme
|
||||
_container = Element::Stack.new(options, block)
|
||||
@containers << _container
|
||||
_container.build
|
||||
_container.parent.add(_container)
|
||||
@containers.pop
|
||||
|
||||
return _container
|
||||
container(CyberarmEngine::Element::Stack, options, &block)
|
||||
end
|
||||
|
||||
# TODO: Remove in version 0.16.0+
|
||||
def label(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::Label.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::TextBlock.new(text, options, block))
|
||||
end
|
||||
|
||||
[
|
||||
"Banner",
|
||||
"Title",
|
||||
"Subtitle",
|
||||
"Tagline",
|
||||
"Caption",
|
||||
"Para",
|
||||
"Inscription",
|
||||
"Link"
|
||||
].each do |const|
|
||||
define_method(:"#{const.downcase}") do |text, options = {}, &block|
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
add_element(Element.const_get(const).new(text, options, block))
|
||||
end
|
||||
end
|
||||
|
||||
def button(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::Button.new(text, options, block) { block.call if block.is_a?(Proc) })
|
||||
end
|
||||
|
||||
def list_box(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
add_element(Element::ListBox.new(options, block) { block.call if block.is_a?(Proc) })
|
||||
end
|
||||
|
||||
def edit_line(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::EditLine.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::EditLine.new(text, options, block))
|
||||
end
|
||||
|
||||
def edit_box(text, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
add_element(Element::EditBox.new(text, options, block))
|
||||
end
|
||||
|
||||
def toggle_button(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::ToggleButton.new(options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::ToggleButton.new(options, block))
|
||||
end
|
||||
|
||||
def check_box(text, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::CheckBox.new(text, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::CheckBox.new(text, options, block))
|
||||
end
|
||||
|
||||
def image(path, options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::Image.new(path, options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::Image.new(path, options, block))
|
||||
end
|
||||
|
||||
def progress(options = {}, &block)
|
||||
options[:parent] = @containers.last
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
_element = Element::Progress.new(options, block)
|
||||
@containers.last.add(_element)
|
||||
|
||||
return _element
|
||||
add_element(Element::Progress.new(options, block))
|
||||
end
|
||||
|
||||
def slider(options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
add_element(Element::Slider.new(options, block))
|
||||
end
|
||||
|
||||
def background(color = Gosu::Color::NONE)
|
||||
@containers.last.style.background = color
|
||||
element_parent.style.default[:background] = color
|
||||
end
|
||||
|
||||
def theme(theme)
|
||||
@containers.last.options[:theme] = theme
|
||||
element_parent.options[:theme] = theme
|
||||
end
|
||||
|
||||
def current_theme
|
||||
@containers.last.options[:theme]
|
||||
element_parent.options[:theme]
|
||||
end
|
||||
|
||||
private def add_element(element)
|
||||
element_parent.add(element)
|
||||
|
||||
element
|
||||
end
|
||||
|
||||
private def element_parent
|
||||
$__current_container__
|
||||
end
|
||||
|
||||
private def container(klass, options = {}, &block)
|
||||
options[:parent] = element_parent
|
||||
options[:theme] = current_theme
|
||||
|
||||
_container = klass.new(options, block)
|
||||
|
||||
old_parent = element_parent
|
||||
$__current_container__ = _container
|
||||
|
||||
_container.build
|
||||
_container.parent.add(_container)
|
||||
|
||||
$__current_container__ = old_parent
|
||||
|
||||
_container
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module CyberarmEngine
|
||||
include Event
|
||||
include Common
|
||||
|
||||
attr_accessor :x, :y, :z, :enabled
|
||||
attr_accessor :x, :y, :z, :enabled, :tip
|
||||
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@@ -13,12 +13,18 @@ module CyberarmEngine
|
||||
@options = options
|
||||
@block = block
|
||||
|
||||
@focus = false
|
||||
@enabled = true
|
||||
@visible = true
|
||||
@focus = @options[:focus].nil? ? false : @options[:focus]
|
||||
@enabled = @options[:enabled].nil? ? true : @options[:enabled]
|
||||
@visible = @options[:visible].nil? ? true : @options[:visible]
|
||||
@tip = @options[:tip] || ""
|
||||
|
||||
@debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
|
||||
|
||||
@style = Style.new(options)
|
||||
|
||||
@root ||= nil
|
||||
@gui_state ||= nil
|
||||
|
||||
@x = @style.x
|
||||
@y = @style.y
|
||||
@z = @style.z
|
||||
@@ -26,76 +32,105 @@ module CyberarmEngine
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
@fixed_x = @x if @x != 0
|
||||
@fixed_y = @y if @y != 0
|
||||
|
||||
@style.width = default(:width) || nil
|
||||
@style.height = default(:height) || nil
|
||||
|
||||
stylize
|
||||
|
||||
default_events
|
||||
end
|
||||
|
||||
def stylize
|
||||
set_border_thickness(@style.border_thickness)
|
||||
|
||||
set_padding(@style.padding)
|
||||
|
||||
set_margin(@style.margin)
|
||||
|
||||
@style.background_canvas = Background.new
|
||||
@style.border_canvas = BorderCanvas.new(element: self)
|
||||
|
||||
set_background(@style.background)
|
||||
set_border_color(@style.border_color)
|
||||
@style_event = :default
|
||||
|
||||
stylize
|
||||
|
||||
default_events
|
||||
|
||||
root.gui_state.request_focus(self) if @options[:autofocus]
|
||||
end
|
||||
|
||||
def set_background(background)
|
||||
@style.background = background
|
||||
@style.background_canvas.background = background
|
||||
def stylize
|
||||
set_static_position
|
||||
|
||||
set_padding
|
||||
set_margin
|
||||
|
||||
set_background
|
||||
|
||||
set_border_thickness
|
||||
set_border_color
|
||||
end
|
||||
|
||||
def set_border_thickness(border_thickness)
|
||||
@style.border_thickness = border_thickness
|
||||
|
||||
@style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
|
||||
@style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
|
||||
@style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
|
||||
@style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
|
||||
def safe_style_fetch(*args)
|
||||
@style.hash.dig(@style_event, *args) || @style.hash.dig(:default, *args) || default(*args)
|
||||
end
|
||||
|
||||
def set_border_color(color)
|
||||
@style.border_color = color
|
||||
|
||||
@style.border_color_left = default(:border_color_left) || @style.border_color
|
||||
@style.border_color_right = default(:border_color_right) || @style.border_color
|
||||
@style.border_color_top = default(:border_color_top) || @style.border_color
|
||||
@style.border_color_bottom = default(:border_color_bottom) || @style.border_color
|
||||
|
||||
@style.border_canvas.color = color
|
||||
def set_static_position
|
||||
@x = @style.x if @style.x != 0
|
||||
@y = @style.y if @style.y != 0
|
||||
end
|
||||
|
||||
def set_padding(padding)
|
||||
@style.padding = padding
|
||||
def set_background
|
||||
@style.background = safe_style_fetch(:background)
|
||||
|
||||
@style.padding_left = default(:padding_left) || @style.padding
|
||||
@style.padding_right = default(:padding_right) || @style.padding
|
||||
@style.padding_top = default(:padding_top) || @style.padding
|
||||
@style.padding_bottom = default(:padding_bottom) || @style.padding
|
||||
@style.background_canvas.background = @style.background
|
||||
end
|
||||
|
||||
def set_margin(margin)
|
||||
@style.margin = margin
|
||||
def set_border_thickness
|
||||
@style.border_thickness = safe_style_fetch(:border_thickness)
|
||||
|
||||
@style.margin_left = default(:margin_left) || @style.margin
|
||||
@style.margin_right = default(:margin_right) || @style.margin
|
||||
@style.margin_top = default(:margin_top) || @style.margin
|
||||
@style.margin_bottom = default(:margin_bottom) || @style.margin
|
||||
@style.border_thickness_left = safe_style_fetch(:border_thickness_left) || @style.border_thickness
|
||||
@style.border_thickness_right = safe_style_fetch(:border_thickness_right) || @style.border_thickness
|
||||
@style.border_thickness_top = safe_style_fetch(:border_thickness_top) || @style.border_thickness
|
||||
@style.border_thickness_bottom = safe_style_fetch(:border_thickness_bottom) || @style.border_thickness
|
||||
end
|
||||
|
||||
def set_border_color
|
||||
@style.border_color = safe_style_fetch(:border_color)
|
||||
|
||||
@style.border_color_left = safe_style_fetch(:border_color_left) || @style.border_color
|
||||
@style.border_color_right = safe_style_fetch(:border_color_right) || @style.border_color
|
||||
@style.border_color_top = safe_style_fetch(:border_color_top) || @style.border_color
|
||||
@style.border_color_bottom = safe_style_fetch(:border_color_bottom) || @style.border_color
|
||||
|
||||
@style.border_canvas.color = [
|
||||
@style.border_color_top,
|
||||
@style.border_color_right,
|
||||
@style.border_color_bottom,
|
||||
@style.border_color_left
|
||||
]
|
||||
end
|
||||
|
||||
def set_padding
|
||||
@style.padding = safe_style_fetch(:padding)
|
||||
|
||||
@style.padding_left = safe_style_fetch(:padding_left) || @style.padding
|
||||
@style.padding_right = safe_style_fetch(:padding_right) || @style.padding
|
||||
@style.padding_top = safe_style_fetch(:padding_top) || @style.padding
|
||||
@style.padding_bottom = safe_style_fetch(:padding_bottom) || @style.padding
|
||||
end
|
||||
|
||||
def set_margin
|
||||
@style.margin = safe_style_fetch(:margin)
|
||||
|
||||
@style.margin_left = safe_style_fetch(:margin_left) || @style.margin
|
||||
@style.margin_right = safe_style_fetch(:margin_right) || @style.margin
|
||||
@style.margin_top = safe_style_fetch(:margin_top) || @style.margin
|
||||
@style.margin_bottom = safe_style_fetch(:margin_bottom) || @style.margin
|
||||
end
|
||||
|
||||
def update_styles(event = :default)
|
||||
_style = @style.send(event)
|
||||
@style_event = event
|
||||
|
||||
if @text.is_a?(CyberarmEngine::Text)
|
||||
@text.color = _style&.dig(:color) || @style.default[:color]
|
||||
@text.swap_font(_style&.dig(:text_size) || @style.default[:text_size], _style&.dig(:font) || @style.default[:font])
|
||||
end
|
||||
|
||||
(root&.gui_state || @gui_state).request_recalculate
|
||||
end
|
||||
|
||||
def default_events
|
||||
[:left, :middle, :right].each do |button|
|
||||
%i[left middle right].each do |button|
|
||||
event(:"#{button}_mouse_button")
|
||||
event(:"released_#{button}_mouse_button")
|
||||
event(:"clicked_#{button}_mouse_button")
|
||||
@@ -109,7 +144,72 @@ module CyberarmEngine
|
||||
event(:hover)
|
||||
event(:leave)
|
||||
|
||||
event(:focus)
|
||||
event(:blur)
|
||||
|
||||
event(:changed)
|
||||
end
|
||||
|
||||
def enter(_sender)
|
||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||
|
||||
if !@enabled
|
||||
update_styles(:disabled)
|
||||
elsif @focus
|
||||
update_styles(:active)
|
||||
else
|
||||
update_styles(:hover)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def left_mouse_button(_sender, _x, _y)
|
||||
@focus = true
|
||||
|
||||
unless @enabled
|
||||
update_styles(:disabled)
|
||||
else
|
||||
update_styles(:active)
|
||||
end
|
||||
|
||||
window.current_state.focus = self
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender, _x, _y)
|
||||
enter(sender)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block&.call(self) if @enabled && !self.is_a?(Container)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def leave(_sender)
|
||||
if @enabled
|
||||
update_styles
|
||||
else
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def blur(_sender)
|
||||
@focus = false
|
||||
|
||||
if @enabled
|
||||
update_styles
|
||||
else
|
||||
update_styles(:disabled)
|
||||
end
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def enabled?
|
||||
@@ -126,21 +226,51 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def show
|
||||
bool = visible?
|
||||
@visible = true
|
||||
root.gui_state.request_recalculate
|
||||
root.gui_state.request_recalculate unless bool
|
||||
end
|
||||
|
||||
def hide
|
||||
bool = visible?
|
||||
@visible = false
|
||||
root.gui_state.request_recalculate
|
||||
root.gui_state.request_recalculate if bool
|
||||
end
|
||||
|
||||
def draw
|
||||
return unless @visible
|
||||
return unless visible?
|
||||
|
||||
@style.background_canvas.draw
|
||||
@style.border_canvas.draw
|
||||
render
|
||||
|
||||
Gosu.clip_to(@x, @y, width, height) do
|
||||
render
|
||||
end
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
return if defined?(GUI_DEBUG_ONLY_ELEMENT) && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||
|
||||
Gosu.draw_line(
|
||||
x, y, @debug_color,
|
||||
x + outer_width, y, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y, @debug_color,
|
||||
x + outer_width, y + outer_height, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x + outer_width, y + outer_height, @debug_color,
|
||||
x, y + outer_height, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
Gosu.draw_line(
|
||||
x, outer_height, @debug_color,
|
||||
x, y, @debug_color,
|
||||
Float::INFINITY
|
||||
)
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -152,53 +282,96 @@ module CyberarmEngine
|
||||
def button_up(id)
|
||||
end
|
||||
|
||||
def draggable?(_button)
|
||||
false
|
||||
end
|
||||
|
||||
def render
|
||||
end
|
||||
|
||||
def hit?(x, y)
|
||||
x.between?(@x, @x + width) &&
|
||||
y.between?(@y, @y + height)
|
||||
y.between?(@y, @y + height)
|
||||
end
|
||||
|
||||
def width
|
||||
if visible?
|
||||
(@style.border_thickness_left + @style.padding_left) + @width + (@style.padding_right + @style.border_thickness_right)
|
||||
inner_width + @width
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
def content_width
|
||||
@width
|
||||
end
|
||||
|
||||
def noncontent_width
|
||||
(inner_width + outer_width) - width
|
||||
end
|
||||
|
||||
def outer_width
|
||||
@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
|
||||
|
||||
def height
|
||||
if visible?
|
||||
(@style.border_thickness_top + @style.padding_top) + @height + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||
inner_height + @height
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
def content_height
|
||||
@height
|
||||
end
|
||||
|
||||
def noncontent_height
|
||||
(inner_height + outer_height) - height
|
||||
end
|
||||
|
||||
def outer_height
|
||||
@style.margin_top + height + @style.margin_bottom
|
||||
end
|
||||
|
||||
private def dimensional_size(size, dimension)
|
||||
raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
|
||||
def inner_height
|
||||
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||
end
|
||||
|
||||
def scroll_width
|
||||
@children.sum { |c| c.width } + noncontent_width
|
||||
end
|
||||
|
||||
def scroll_height
|
||||
@children.sum { |c| c.height } + noncontent_height
|
||||
end
|
||||
|
||||
def max_scroll_width
|
||||
scroll_width - width
|
||||
end
|
||||
|
||||
def max_scroll_height
|
||||
scroll_height - height
|
||||
end
|
||||
|
||||
def dimensional_size(size, dimension)
|
||||
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
||||
|
||||
if size && size.is_a?(Numeric)
|
||||
if size.between?(0.0, 1.0)
|
||||
@parent.send(:"#{dimension}") * size
|
||||
((@parent.send(:"content_#{dimension}") - send(:"noncontent_#{dimension}")) * size).round
|
||||
else
|
||||
size
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def background=(_background)
|
||||
@style.background_canvas.background=(_background)
|
||||
@style.background_canvas.background = _background
|
||||
update_background
|
||||
end
|
||||
|
||||
@@ -215,15 +388,15 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def root
|
||||
return self if is_root?
|
||||
|
||||
unless @root && @root.parent.nil?
|
||||
@root = parent
|
||||
|
||||
loop do
|
||||
if @root.parent.nil?
|
||||
break
|
||||
else
|
||||
@root = @root.parent
|
||||
end
|
||||
break unless @root&.parent
|
||||
|
||||
@root = @root.parent
|
||||
end
|
||||
end
|
||||
|
||||
@@ -234,6 +407,12 @@ module CyberarmEngine
|
||||
@gui_state != nil
|
||||
end
|
||||
|
||||
def focus(_)
|
||||
warn "#{self.class}#focus was not overridden!"
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
raise "#{self.class}#recalculate was not overridden!"
|
||||
end
|
||||
@@ -245,8 +424,16 @@ module CyberarmEngine
|
||||
raise "#{self.class}#value was not overridden!"
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
def value=(_value)
|
||||
raise "#{self.class}#value= was not overridden!"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
|
||||
end
|
||||
|
||||
def inspect
|
||||
to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,55 +1,97 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Button < Label
|
||||
def initialize(text, options = {}, block = nil)
|
||||
super(text, options, block)
|
||||
class Button < TextBlock
|
||||
def initialize(text_or_image, options = {}, block = nil)
|
||||
@image = nil
|
||||
@scale_x = 1
|
||||
@scale_y = 1
|
||||
|
||||
@style.background_canvas.background = default(:background)
|
||||
@image = text_or_image if text_or_image.is_a?(Gosu::Image)
|
||||
|
||||
super(text_or_image, options, block)
|
||||
|
||||
@style.background_canvas.background = @style.background
|
||||
end
|
||||
|
||||
def render
|
||||
draw_text
|
||||
if @image
|
||||
draw_image
|
||||
else
|
||||
draw_text
|
||||
end
|
||||
end
|
||||
|
||||
def draw_image
|
||||
@image.draw(
|
||||
@style.border_thickness_left + @style.padding_left + @x,
|
||||
@style.border_thickness_top + @style.padding_top + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y, @text.color
|
||||
)
|
||||
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)
|
||||
def recalculate
|
||||
unless @enabled
|
||||
@style.background_canvas.background = @style.disabled[:background]
|
||||
@text.color = @style.disabled[:color]
|
||||
else
|
||||
@style.background_canvas.background = default(:hover, :background)
|
||||
@text.color = default(:hover, :color)
|
||||
@style.background_canvas.background = @style.background
|
||||
@text.color = @style.color
|
||||
end
|
||||
|
||||
if @image
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
_width = dimensional_size(@style.image_width, :width)
|
||||
_height = dimensional_size(@style.image_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 = 1
|
||||
@scale_y = 1
|
||||
end
|
||||
|
||||
@width = _width || @image.width.round * @scale_x
|
||||
@height = _height || @image.height.round * @scale_y
|
||||
|
||||
update_background
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
@focus = true
|
||||
@style.background_canvas.background = default(:active, :background)
|
||||
window.current_state.focus = self
|
||||
@text.color = default(:active, :color)
|
||||
def value
|
||||
@image || super
|
||||
end
|
||||
|
||||
def released_left_mouse_button(sender,x, y)
|
||||
enter(sender)
|
||||
end
|
||||
def value=(value)
|
||||
if value.is_a?(Gosu::Image)
|
||||
@image = value
|
||||
else
|
||||
super
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
@block.call(self) if @block
|
||||
end
|
||||
old_width = width
|
||||
old_height = height
|
||||
recalculate
|
||||
|
||||
def leave(sender)
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
end
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
publish(:changed, self.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,13 +2,35 @@ module CyberarmEngine
|
||||
class Element
|
||||
class CheckBox < Flow
|
||||
def initialize(text, options, block = nil)
|
||||
super({}, block = nil)
|
||||
super(options, block)
|
||||
options[:toggled] = options[:checked]
|
||||
|
||||
options[:parent] = self
|
||||
@toggle_button = ToggleButton.new(options)
|
||||
@label = Label.new(text, options)
|
||||
|
||||
define_label_singletons
|
||||
options[:parent] = self
|
||||
@label = TextBlock.new(text, options)
|
||||
|
||||
@label.subscribe(:holding_left_mouse_button) do |sender, x, y|
|
||||
@toggle_button.left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.subscribe(:released_left_mouse_button) do |sender, x, y|
|
||||
@toggle_button.released_left_mouse_button(sender, x, y)
|
||||
end
|
||||
|
||||
@label.subscribe(:clicked_left_mouse_button) do |sender, x, y|
|
||||
@toggle_button.clicked_left_mouse_button(sender, x, y)
|
||||
publish(:changed, @toggle_button.value)
|
||||
end
|
||||
|
||||
@label.subscribe(:enter) do |sender|
|
||||
@toggle_button.enter(sender)
|
||||
end
|
||||
|
||||
@label.subscribe(:leave) do |sender|
|
||||
@toggle_button.leave(sender)
|
||||
end
|
||||
|
||||
add(@toggle_button)
|
||||
add(@label)
|
||||
@@ -24,36 +46,9 @@ module CyberarmEngine
|
||||
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
|
||||
@toggle_button.value = bool
|
||||
publish(:changed, @toggle_button.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,31 +4,56 @@ module CyberarmEngine
|
||||
include Common
|
||||
|
||||
attr_accessor :stroke_color, :fill_color
|
||||
attr_reader :children, :gui_state
|
||||
attr_reader :scroll_x, :scroll_y
|
||||
attr_reader :children, :gui_state, :scroll_position
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@gui_state = options.delete(:gui_state)
|
||||
super
|
||||
|
||||
@scroll_x, @scroll_y = 0, 0
|
||||
@scroll_speed = 10
|
||||
@scroll_position = Vector.new(0, 0)
|
||||
@scroll_speed = 40
|
||||
|
||||
@text_color = options[:color]
|
||||
|
||||
@children = []
|
||||
|
||||
event(:window_size_changed)
|
||||
end
|
||||
|
||||
def build
|
||||
@block.call(self) if @block
|
||||
|
||||
recalculate
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def add(element)
|
||||
@children << element
|
||||
|
||||
recalculate
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def clear(&block)
|
||||
@children.clear
|
||||
|
||||
old_container = $__current_container__
|
||||
|
||||
$__current_container__ = self
|
||||
block.call(self) if block
|
||||
|
||||
$__current_container__ = old_container
|
||||
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def apend(&block)
|
||||
old_container = $__current_container__
|
||||
|
||||
$__current_container__ = self
|
||||
block.call(self) if block
|
||||
|
||||
$__current_container__ = old_container
|
||||
|
||||
root.gui_state.request_recalculate
|
||||
end
|
||||
|
||||
def render
|
||||
@@ -37,12 +62,22 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
super
|
||||
|
||||
@children.each do |child|
|
||||
child.debug_draw
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
def hit_element?(x, y)
|
||||
@children.reverse_each do |child|
|
||||
next unless child.visible?
|
||||
|
||||
case child
|
||||
when Container
|
||||
if element = child.hit_element?(x, y)
|
||||
@@ -58,7 +93,12 @@ module CyberarmEngine
|
||||
|
||||
def recalculate
|
||||
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||
@current_position += @scroll_position
|
||||
|
||||
return unless visible?
|
||||
|
||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||
|
||||
stylize
|
||||
|
||||
layout
|
||||
@@ -67,21 +107,26 @@ module CyberarmEngine
|
||||
@width = @style.width = window.width
|
||||
@height = @style.height = window.height
|
||||
else
|
||||
_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
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).round
|
||||
@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.x += (@x + @style.border_thickness_left) - style.margin_left
|
||||
child.y += (@y + @style.border_thickness_top) - style.margin_top
|
||||
|
||||
child.stylize
|
||||
child.recalculate
|
||||
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||
|
||||
Stats.increment(:gui_recalculations_last_frame, 1)
|
||||
end
|
||||
|
||||
update_background
|
||||
@@ -92,32 +137,36 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def max_width
|
||||
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
if _width
|
||||
outer_width
|
||||
else
|
||||
window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||
end
|
||||
end
|
||||
|
||||
def fits_on_line?(element) # Flow
|
||||
p [@options[:id], @width] if @options[:id]
|
||||
@current_position.x + element.outer_width <= max_width &&
|
||||
@current_position.x + element.outer_width <= window.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
|
||||
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
|
||||
response
|
||||
end
|
||||
|
||||
def position_on_next_line(child) # Flow
|
||||
@@ -127,8 +176,6 @@ module CyberarmEngine
|
||||
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
|
||||
|
||||
@@ -136,24 +183,72 @@ module CyberarmEngine
|
||||
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_up(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
# def mouse_wheel_down(sender, x, y)
|
||||
# @children.each {|c| c.y += @scroll_speed}
|
||||
# @children.each {|c| c.recalculate}
|
||||
# end
|
||||
if @scroll_position.y < 0
|
||||
@scroll_position.y += @scroll_speed
|
||||
@scroll_position.y = 0 if @scroll_position.y > 0
|
||||
recalculate
|
||||
|
||||
return :handled
|
||||
end
|
||||
end
|
||||
|
||||
def mouse_wheel_down(sender, x, y)
|
||||
return unless @style.scroll
|
||||
|
||||
return unless height < scroll_height
|
||||
|
||||
if @scroll_position.y.abs < max_scroll_height
|
||||
@scroll_position.y -= @scroll_speed
|
||||
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
|
||||
recalculate
|
||||
|
||||
return :handled
|
||||
end
|
||||
end
|
||||
|
||||
def scroll_top
|
||||
@scroll_position.y
|
||||
end
|
||||
|
||||
def scroll_top=(n)
|
||||
n = 0 if n <= 0
|
||||
@scroll_position.y = -n
|
||||
|
||||
if max_scroll_height.positive?
|
||||
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
|
||||
else
|
||||
@scroll_position.y = 0
|
||||
end
|
||||
end
|
||||
|
||||
def value
|
||||
@children.map {|c| c.class}.join(", ")
|
||||
@children.map { |c| c.class }.join(", ")
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} children=#{@children.size}"
|
||||
end
|
||||
|
||||
def write_tree(indent = "", _index = 0)
|
||||
puts self
|
||||
|
||||
indent += " "
|
||||
@children.each_with_index do |child, i|
|
||||
print "#{indent}#{i}: "
|
||||
|
||||
if child.is_a?(Container)
|
||||
child.write_tree(indent)
|
||||
else
|
||||
puts child
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
179
lib/cyberarm_engine/ui/elements/edit_box.rb
Normal file
179
lib/cyberarm_engine/ui/elements/edit_box.rb
Normal file
@@ -0,0 +1,179 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class EditBox < EditLine
|
||||
def initialize(*args)
|
||||
super(*args)
|
||||
|
||||
@active_line = 0
|
||||
|
||||
@repeatable_keys = [
|
||||
{
|
||||
key: Gosu::KB_UP,
|
||||
down: false,
|
||||
repeat_delay: 50,
|
||||
last_repeat: 0,
|
||||
action: proc { move(:up) }
|
||||
},
|
||||
{
|
||||
key: Gosu::KB_DOWN,
|
||||
down: false,
|
||||
repeat_delay: 50,
|
||||
last_repeat: 0,
|
||||
action: proc { move(:down) }
|
||||
}
|
||||
]
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
|
||||
caret_stay_left_of_last_newline
|
||||
calculate_active_line
|
||||
|
||||
@repeatable_keys.each do |key|
|
||||
if key[:down] && (Gosu.milliseconds > key[:last_repeat] + key[:repeat_delay])
|
||||
key[:action].call
|
||||
key[:last_repeat] = Gosu.milliseconds
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def draw_caret
|
||||
Gosu.draw_rect(caret_position, @text.y + @active_line * @text.textobject.height, @caret_width, @caret_height,
|
||||
@caret_color, @z)
|
||||
end
|
||||
|
||||
def draw_selection
|
||||
selection_width = caret_position - selection_start_position
|
||||
|
||||
Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.textobject.height,
|
||||
default(:selection_color), @z)
|
||||
end
|
||||
|
||||
def text_input_position_for(_method)
|
||||
line = @text_input.text[0...@text_input.caret_pos].lines.last
|
||||
_x = @text.x + @offset_x
|
||||
|
||||
if @type == :password
|
||||
_x + @text.width(default(:password_character) * line.length)
|
||||
else
|
||||
_x + @text.width(line)
|
||||
end
|
||||
end
|
||||
|
||||
def set_position(int)
|
||||
int = 0 if int < 0
|
||||
@text_input.selection_start = @text_input.caret_pos = int
|
||||
end
|
||||
|
||||
def calculate_active_line
|
||||
sub_text = @text_input.text[0...@text_input.caret_pos]
|
||||
@active_line = sub_text.lines.size - 1
|
||||
end
|
||||
|
||||
def caret_stay_left_of_last_newline
|
||||
@text_input.text += "\n" unless @text_input.text.end_with?("\n")
|
||||
|
||||
eof = @text_input.text.chomp.length
|
||||
set_position(eof) if @text_input.caret_pos > eof
|
||||
end
|
||||
|
||||
def caret_position_under_mouse(mouse_x, mouse_y)
|
||||
active_line = row_at(mouse_y)
|
||||
right_offset = column_at(mouse_x, mouse_y)
|
||||
|
||||
buffer = @text_input.text.lines[0..active_line].join if active_line != 0
|
||||
buffer = @text_input.text.lines.first if active_line == 0
|
||||
line = buffer.lines.last
|
||||
|
||||
if buffer.chars.last == "\n"
|
||||
(buffer.length - line.length) + right_offset - 1
|
||||
else
|
||||
(buffer.length - line.length) + right_offset
|
||||
end
|
||||
end
|
||||
|
||||
def move_caret_to_mouse(mouse_x, mouse_y)
|
||||
set_position(caret_position_under_mouse(mouse_x, mouse_y))
|
||||
end
|
||||
|
||||
def row_at(y)
|
||||
((y - @text.y) / @text.textobject.height).round
|
||||
end
|
||||
|
||||
def column_at(x, y)
|
||||
row = row_at(y)
|
||||
|
||||
buffer = @text_input.text.lines[0..row].join if row != 0
|
||||
buffer = @text_input.text.lines.first if row == 0
|
||||
|
||||
line = @text_input.text.lines[row]
|
||||
line ||= ""
|
||||
column = 0
|
||||
|
||||
line.length.times do |_i|
|
||||
break if @text.textobject.text_width(line[0...column]) >= (x - @text.x).clamp(0.0, Float::INFINITY)
|
||||
|
||||
column += 1
|
||||
end
|
||||
|
||||
column
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
super
|
||||
|
||||
@repeatable_keys.detect do |key|
|
||||
next unless key[:key] == id
|
||||
|
||||
key[:down] = true
|
||||
key[:last_repeat] = Gosu.milliseconds + key[:repeat_delay]
|
||||
return true
|
||||
end
|
||||
|
||||
case id
|
||||
when Gosu::KB_ENTER, Gosu::KB_RETURN
|
||||
caret_pos = @text_input.caret_pos
|
||||
@text_input.text = @text_input.text.insert(@text_input.caret_pos, "\n")
|
||||
@text_input.caret_pos = @text_input.selection_start = caret_pos + 1
|
||||
end
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
super
|
||||
|
||||
@repeatable_keys.detect do |key|
|
||||
if key[:key] == id
|
||||
key[:down] = false
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def move(direction)
|
||||
pos = @text_input.caret_pos
|
||||
line = nil
|
||||
|
||||
case direction
|
||||
when :up
|
||||
return if @active_line == 0
|
||||
when :down
|
||||
return if @active_line == @text_input.text.chomp.lines
|
||||
|
||||
text = @text_input.text.chomp.lines[0..@active_line].join("\n")
|
||||
pos = text.length
|
||||
end
|
||||
|
||||
set_position(pos)
|
||||
end
|
||||
|
||||
def drag_update(_sender, x, y, _button)
|
||||
int = caret_position_under_mouse(x, y)
|
||||
int = 0 if int < 0
|
||||
@text_input.caret_pos = int
|
||||
|
||||
:handled
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,35 +2,77 @@ module CyberarmEngine
|
||||
class Element
|
||||
class EditLine < Button
|
||||
def initialize(text, options = {}, block = nil)
|
||||
@filter = options.delete(:filter)
|
||||
super(text, options, block)
|
||||
|
||||
@type = default(:type)
|
||||
|
||||
@caret_width = default(:caret_width)
|
||||
@caret_height= @text.height
|
||||
@caret_height = @text.textobject.height
|
||||
@caret_color = default(:caret_color)
|
||||
@caret_interval = default(:caret_interval)
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
@show_caret = true
|
||||
|
||||
@text_input = Gosu::TextInput.new
|
||||
@text_input.text = text
|
||||
@last_text_value = text
|
||||
|
||||
return self
|
||||
if @filter && @filter.respond_to?(:call)
|
||||
@text_input.instance_variable_set(:@filter, @filter)
|
||||
|
||||
def @text_input.filter(text_in)
|
||||
@filter.call(text_in)
|
||||
end
|
||||
end
|
||||
|
||||
@offset_x = 0
|
||||
@offset_y = 0
|
||||
|
||||
event(:begin_drag)
|
||||
event(:drag_update)
|
||||
event(:end_drag)
|
||||
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
|
||||
Gosu.clip_to(@text.x, @text.y, @width, @height) do
|
||||
Gosu.translate(-@offset_x, -@offset_y) do
|
||||
draw_selection
|
||||
draw_caret if @focus && @show_caret
|
||||
draw_text
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def draw_text
|
||||
@text.draw(:draw_text)
|
||||
end
|
||||
|
||||
def draw_caret
|
||||
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z)
|
||||
end
|
||||
|
||||
def draw_selection
|
||||
selection_width = caret_position - selection_start_position
|
||||
|
||||
Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.height, default(:selection_color), @z)
|
||||
end
|
||||
|
||||
def update
|
||||
if @type == :password
|
||||
@text.text = default(:password_character) * @text_input.text.length
|
||||
else
|
||||
@text.text = @text_input.text
|
||||
@style_event = :active if @focus
|
||||
|
||||
@text.text = if @type == :password
|
||||
default(:password_character) * @text_input.text.length
|
||||
else
|
||||
@text_input.text
|
||||
end
|
||||
|
||||
if @last_text_value != value
|
||||
@last_text_value = value
|
||||
@show_caret = true
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
|
||||
publish(:changed, value)
|
||||
end
|
||||
|
||||
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
|
||||
@@ -38,43 +80,168 @@ module CyberarmEngine
|
||||
|
||||
@show_caret = !@show_caret
|
||||
end
|
||||
|
||||
keep_caret_visible
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
handle_keyboard_shortcuts(id)
|
||||
end
|
||||
|
||||
def handle_keyboard_shortcuts(id)
|
||||
return unless @focus && @enabled
|
||||
|
||||
if Gosu.button_down?(Gosu::KB_LEFT_CONTROL) || Gosu.button_down?(Gosu::KB_RIGHT_CONTROL)
|
||||
case id
|
||||
when Gosu::KB_A
|
||||
@text_input.selection_start = 0
|
||||
@text_input.caret_pos = @text_input.text.length
|
||||
|
||||
when Gosu::KB_C
|
||||
if @text_input.selection_start < @text_input.caret_pos
|
||||
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
|
||||
else
|
||||
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
|
||||
end
|
||||
|
||||
when Gosu::KB_X
|
||||
chars = @text_input.text.chars
|
||||
|
||||
if @text_input.selection_start < @text_input.caret_pos
|
||||
Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
|
||||
chars.slice!(@text_input.selection_start, @text_input.caret_pos)
|
||||
else
|
||||
Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
|
||||
chars.slice!(@text_input.caret_pos, @text_input.selection_start)
|
||||
end
|
||||
|
||||
@text_input.text = chars.join
|
||||
|
||||
when Gosu::KB_V
|
||||
if instance_of?(EditLine) # EditLine assumes a single line of text
|
||||
@text_input.text = @text_input.text.insert(@text_input.caret_pos,
|
||||
Clipboard.paste.encode("UTF-8").gsub("\n", ""))
|
||||
else
|
||||
@text_input.text = @text_input.text.insert(@text_input.caret_pos, Clipboard.paste.encode("UTF-8"))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def caret_position_under_mouse(mouse_x)
|
||||
1.upto(@text.text.length) do |i|
|
||||
return i - 1 if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i])
|
||||
end
|
||||
|
||||
@text_input.text.length
|
||||
end
|
||||
|
||||
def move_caret_to_mouse(mouse_x, _mouse_y)
|
||||
@text_input.caret_pos = @text_input.selection_start = caret_position_under_mouse(mouse_x)
|
||||
end
|
||||
|
||||
def keep_caret_visible
|
||||
caret_pos = (caret_position - @text.x) + @caret_width
|
||||
|
||||
@last_text ||= "/\\"
|
||||
@last_pos ||= -1
|
||||
|
||||
@last_text = @text.text
|
||||
@last_pos = caret_pos
|
||||
|
||||
if caret_pos.between?(@offset_x, @width + @offset_x)
|
||||
# Do nothing
|
||||
|
||||
elsif caret_pos < @offset_x
|
||||
@offset_x = if caret_pos > @width
|
||||
caret_pos + @width
|
||||
else
|
||||
0
|
||||
end
|
||||
|
||||
elsif caret_pos > @width
|
||||
@offset_x = caret_pos - @width
|
||||
|
||||
else
|
||||
# Reset to Zero
|
||||
@offset_x = 0
|
||||
end
|
||||
end
|
||||
|
||||
def caret_position
|
||||
text_input_position_for(:caret_pos)
|
||||
end
|
||||
|
||||
def selection_start_position
|
||||
text_input_position_for(:selection_start)
|
||||
end
|
||||
|
||||
def text_input_position_for(method)
|
||||
if @type == :password
|
||||
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
|
||||
else
|
||||
@text.x + @text.width(@text_input.text[0...@text_input.send(method)])
|
||||
end
|
||||
end
|
||||
|
||||
def left_mouse_button(sender, x, y)
|
||||
super
|
||||
window.text_input = @text_input
|
||||
|
||||
@caret_last_interval = Gosu.milliseconds
|
||||
@show_caret = true
|
||||
|
||||
move_caret_to_mouse(x, y)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def focus(sender)
|
||||
super
|
||||
|
||||
window.text_input = @text_input
|
||||
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
|
||||
|
||||
: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
|
||||
_has_focus = @focus
|
||||
|
||||
super
|
||||
|
||||
@focus = _has_focus
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def leave(sender)
|
||||
unless @focus
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def blur(sender)
|
||||
@focus = false
|
||||
@style.background_canvas.background = default(:background)
|
||||
@text.color = default(:color)
|
||||
def blur(_sender)
|
||||
super
|
||||
window.text_input = nil
|
||||
|
||||
: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
|
||||
def draggable?(button)
|
||||
button == :left
|
||||
end
|
||||
|
||||
def begin_drag(_sender, x, _y, _button)
|
||||
@drag_start = x
|
||||
@offset_drag_start = @offset_x
|
||||
@drag_caret_position = @text_input.caret_pos
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def drag_update(_sender, x, _y, _button)
|
||||
@text_input.caret_pos = caret_position_under_mouse(x)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def end_drag(_sender, _x, _y, _button)
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@@ -87,6 +254,10 @@ module CyberarmEngine
|
||||
def value
|
||||
@text_input.text
|
||||
end
|
||||
|
||||
def value=(string)
|
||||
@text_input.text = string
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Flow < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
if fits_on_line?(child)
|
||||
@@ -14,4 +12,4 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Image < Element
|
||||
def initialize(path, options = {}, block = nil)
|
||||
def initialize(path_or_image, options = {}, block = nil)
|
||||
super(options, block)
|
||||
@path = path
|
||||
@path = path_or_image if path_or_image.is_a?(String)
|
||||
|
||||
@image = Gosu::Image.new(path, retro: @options[:image_retro])
|
||||
@scale_x, @scale_y = 1, 1
|
||||
@image = Gosu::Image.new(path_or_image, retro: @options[:retro], tileable: @options[:tileable]) if @path
|
||||
@image = path_or_image unless @path
|
||||
|
||||
@scale_x = 1
|
||||
@scale_y = 1
|
||||
end
|
||||
|
||||
def render
|
||||
@@ -14,16 +17,19 @@ module CyberarmEngine
|
||||
@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?
|
||||
@scale_x, @scale_y, @style.color
|
||||
)
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
@block.call(self) if @block
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height= dimensional_size(@style.height,:height)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
if _width && _height
|
||||
@scale_x = _width.to_f / @image.width
|
||||
@@ -35,16 +41,32 @@ module CyberarmEngine
|
||||
@scale_y = _height.to_f / @image.height
|
||||
@scale_x = @scale_y
|
||||
else
|
||||
@scale_x, @scale_y = 1, 1
|
||||
@scale_x = 1
|
||||
@scale_y = 1
|
||||
end
|
||||
|
||||
@width = _width ? _width : @image.width.round * @scale_x
|
||||
@height= _height ? _height : @image.height.round * @scale_y
|
||||
@width = _width || @image.width.round * @scale_x
|
||||
@height = _height || @image.height.round * @scale_y
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def value
|
||||
@image
|
||||
end
|
||||
|
||||
def value=(path_or_image, retro: false, tileable: false)
|
||||
@path = path_or_image if path_or_image.is_a?(String)
|
||||
|
||||
@image = Gosu::Image.new(path_or_image, retro: retro, tileable: tileable) if @path
|
||||
@image = path_or_image unless @path
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def path
|
||||
@path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
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
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_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
|
||||
82
lib/cyberarm_engine/ui/elements/list_box.rb
Normal file
82
lib/cyberarm_engine/ui/elements/list_box.rb
Normal file
@@ -0,0 +1,82 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class ListBox < Button
|
||||
attr_accessor :items
|
||||
attr_reader :choose
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
@items = options[:items] || []
|
||||
@choose = options[:choose] || @items.first
|
||||
|
||||
super(@choose, options, block)
|
||||
|
||||
@style.background_canvas.background = default(:background)
|
||||
|
||||
# TODO: "Clean Up" into own class?
|
||||
@menu = Stack.new(parent: parent, width: @options[:width], theme: @options[:theme])
|
||||
@menu.define_singleton_method(:recalculate_menu) do
|
||||
@x = @__list_box.x
|
||||
@y = @__list_box.y + @__list_box.height
|
||||
end
|
||||
@menu.instance_variable_set(:"@__list_box", self)
|
||||
|
||||
def @menu.recalculate
|
||||
super
|
||||
|
||||
recalculate_menu
|
||||
end
|
||||
|
||||
self.choose = @choose
|
||||
end
|
||||
|
||||
def choose=(item)
|
||||
valid = @items.detect { |i| i == item }
|
||||
raise "Invalid value '#{item}' for choose, valid options were: #{@items.map { |i| "#{i.inspect}" }.join(", ")}" unless valid
|
||||
|
||||
@choose = item
|
||||
|
||||
self.value = item.to_s
|
||||
|
||||
recalculate
|
||||
end
|
||||
|
||||
def released_left_mouse_button(_sender, _x, _y)
|
||||
show_menu
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def show_menu
|
||||
@menu.clear
|
||||
|
||||
@items.each do |item|
|
||||
btn = Button.new(
|
||||
item,
|
||||
{
|
||||
parent: @menu,
|
||||
width: 1.0,
|
||||
theme: @options[:theme],
|
||||
margin: 0,
|
||||
border_color: 0x00ffffff
|
||||
},
|
||||
proc do
|
||||
self.choose = item
|
||||
@block&.call(item)
|
||||
end
|
||||
)
|
||||
|
||||
@menu.add(btn)
|
||||
end
|
||||
recalculate
|
||||
|
||||
root.gui_state.show_menu(@menu)
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
|
||||
@menu.recalculate
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,7 +5,7 @@ module CyberarmEngine
|
||||
super(options, block)
|
||||
|
||||
@fraction_background = Background.new(background: @style.fraction_background)
|
||||
self.value = options[:fraction] ? options[:fraction] : 0.0
|
||||
self.value = options[:fraction] || 0.0
|
||||
end
|
||||
|
||||
def render
|
||||
@@ -14,9 +14,9 @@ module CyberarmEngine
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height= dimensional_size(@style.height,:height)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
@width = _width
|
||||
@height= _height
|
||||
@height = _height
|
||||
|
||||
update_background
|
||||
end
|
||||
@@ -43,8 +43,9 @@ module CyberarmEngine
|
||||
@fraction = decimal.clamp(0.0, 1.0)
|
||||
update_background
|
||||
|
||||
return @fraction
|
||||
publish(:changed, @fraction)
|
||||
@fraction
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
6
lib/cyberarm_engine/ui/elements/radio.rb
Normal file
6
lib/cyberarm_engine/ui/elements/radio.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Radio < Element
|
||||
end
|
||||
end
|
||||
end
|
||||
104
lib/cyberarm_engine/ui/elements/slider.rb
Normal file
104
lib/cyberarm_engine/ui/elements/slider.rb
Normal file
@@ -0,0 +1,104 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Slider < Container
|
||||
class Handle < Button
|
||||
def initialize(*args)
|
||||
super(*args)
|
||||
|
||||
event(:begin_drag)
|
||||
event(:drag_update)
|
||||
event(:end_drag)
|
||||
|
||||
subscribe :begin_drag do |_sender, x, y, _button|
|
||||
@drag_start_pos = Vector.new(x, y)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
subscribe :drag_update do |_sender, x, y, _button|
|
||||
@parent.handle_dragged_to(x, y)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
subscribe :end_drag do
|
||||
@drag_start_pos = nil
|
||||
|
||||
:handled
|
||||
end
|
||||
end
|
||||
|
||||
def draggable?(button)
|
||||
button == :left
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :range, :step_size, :value
|
||||
|
||||
def initialize(options = {}, block = nil)
|
||||
super(options, block)
|
||||
|
||||
@range = @options[:range] || (0.0..1.0)
|
||||
@step_size = @options[:step] || 0.1
|
||||
@value = @options[:value] || (@range.first + @range.last) / 2
|
||||
|
||||
@handle = Handle.new("", parent: self, width: 8, height: 1.0) { close }
|
||||
add(@handle)
|
||||
end
|
||||
|
||||
def recalculate
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
@width = _width
|
||||
@height = _height
|
||||
|
||||
position_handle
|
||||
@handle.recalculate
|
||||
@handle.update_background
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def position_handle
|
||||
@handle.x = @x + @style.padding_left + @style.border_thickness_left +
|
||||
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
|
||||
|
||||
@handle.y = @y + @style.border_thickness_top + @style.padding_top
|
||||
end
|
||||
|
||||
def draw
|
||||
super
|
||||
|
||||
@handle.draw
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
|
||||
@tip = value.to_s
|
||||
@handle.tip = @tip
|
||||
end
|
||||
|
||||
def holding_left_mouse_button(_sender, x, y)
|
||||
handle_dragged_to(x, y)
|
||||
|
||||
:handled
|
||||
end
|
||||
|
||||
def handle_dragged_to(x, _y)
|
||||
@ratio = ((x - @handle.width / 2) - @x) / (content_width - @handle.outer_width)
|
||||
|
||||
self.value = @ratio.clamp(0.0, 1.0) * (@range.max - @range.min) + @range.min
|
||||
end
|
||||
|
||||
def value=(n)
|
||||
@value = n
|
||||
position_handle
|
||||
@handle.recalculate
|
||||
|
||||
publish(:changed, @value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,8 +1,6 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class Stack < Container
|
||||
include Common
|
||||
|
||||
def layout
|
||||
@children.each do |child|
|
||||
move_to_next_line(child)
|
||||
@@ -10,4 +8,4 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
153
lib/cyberarm_engine/ui/elements/text_block.rb
Normal file
153
lib/cyberarm_engine/ui/elements/text_block.rb
Normal file
@@ -0,0 +1,153 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class TextBlock < 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],
|
||||
shadow_size: @options[:text_shadow_size],
|
||||
shadow_color: @options[:text_shadow_color]
|
||||
)
|
||||
|
||||
@raw_text = text
|
||||
end
|
||||
|
||||
def render
|
||||
@text.draw
|
||||
end
|
||||
|
||||
def recalculate
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
handle_text_wrapping(_width)
|
||||
|
||||
@width = _width || @text.width.round
|
||||
@height = _height || @text.height.round
|
||||
|
||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
if (text_alignment = @options[:text_align])
|
||||
case text_alignment
|
||||
when :left
|
||||
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||
when :center
|
||||
@text.x = if @text.width <= outer_width
|
||||
@x + outer_width / 2 - @text.width / 2
|
||||
else # Act as left aligned
|
||||
@style.border_thickness_left + @style.padding_left + @x
|
||||
end
|
||||
when :right
|
||||
@text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right)
|
||||
end
|
||||
end
|
||||
|
||||
update_background
|
||||
end
|
||||
|
||||
def handle_text_wrapping(max_width)
|
||||
max_width ||= @parent&.width
|
||||
max_width ||= @x - (window.width + noncontent_width)
|
||||
wrap_behavior = style.text_wrap
|
||||
copy = @raw_text.to_s.dup
|
||||
|
||||
if line_width(copy[0]) <= max_width && line_width(copy) > max_width && wrap_behavior != :none
|
||||
breaks = []
|
||||
line_start = 0
|
||||
line_end = copy.length
|
||||
|
||||
while line_start != copy.length
|
||||
if line_width(copy[line_start...line_end]) > max_width
|
||||
line_end = ((line_end - line_start) / 2.0)
|
||||
line_end = 1.0 if line_end <= 1
|
||||
elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
|
||||
# To small, grow!
|
||||
# TODO: find a more efficient way
|
||||
line_end += 1
|
||||
|
||||
else # FOUND IT!
|
||||
entering_line_end = line_end.floor
|
||||
max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63
|
||||
reach = 0
|
||||
|
||||
if wrap_behavior == :word_wrap
|
||||
max_reach.times do |i|
|
||||
reach = i
|
||||
break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
|
||||
end
|
||||
|
||||
# puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}"
|
||||
line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation
|
||||
end
|
||||
|
||||
breaks << line_end.floor
|
||||
line_start = line_end.floor
|
||||
line_end = copy.length
|
||||
|
||||
break if entering_line_end == copy.length || reach == max_reach
|
||||
end
|
||||
end
|
||||
|
||||
breaks.each_with_index do |pos, index|
|
||||
copy.insert(pos + index, "\n") if pos + index >= 0 && pos + index < copy.length
|
||||
end
|
||||
end
|
||||
|
||||
@text.text = copy
|
||||
end
|
||||
|
||||
def line_width(text)
|
||||
(@text.textobject.markup_width(text) + noncontent_width)
|
||||
end
|
||||
|
||||
def value
|
||||
@raw_text
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
@raw_text = value.to_s.chomp
|
||||
|
||||
old_width = width
|
||||
old_height = height
|
||||
recalculate
|
||||
|
||||
root.gui_state.request_recalculate if old_width != width || old_height != height
|
||||
|
||||
publish(:changed, self.value)
|
||||
end
|
||||
end
|
||||
|
||||
class Banner < TextBlock
|
||||
end
|
||||
|
||||
class Title < TextBlock
|
||||
end
|
||||
|
||||
class Subtitle < TextBlock
|
||||
end
|
||||
|
||||
class Tagline < TextBlock
|
||||
end
|
||||
|
||||
class Caption < TextBlock
|
||||
end
|
||||
|
||||
class Para < TextBlock
|
||||
end
|
||||
|
||||
class Inscription < TextBlock
|
||||
end
|
||||
|
||||
class ToolTip < TextBlock
|
||||
end
|
||||
|
||||
class Link < TextBlock
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,54 +1,65 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
class ToggleButton < Button
|
||||
attr_reader :toggled
|
||||
attr_reader :toggled, :value
|
||||
|
||||
def initialize(options, block = nil)
|
||||
super(options[:checkmark], options, block)
|
||||
@toggled = options[:toggled] || false
|
||||
if @toggled
|
||||
@text.text = @options[:checkmark]
|
||||
if options.dig(:theme, :ToggleButton, :checkmark_image)
|
||||
options[:theme][:ToggleButton][:image_width] ||= options[:theme][:Label][:text_size]
|
||||
super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)
|
||||
|
||||
@_image = @image
|
||||
else
|
||||
@text.text = ""
|
||||
super(options[:checkmark], options, block)
|
||||
end
|
||||
|
||||
return self
|
||||
@value = options[:checked] || false
|
||||
|
||||
if @value
|
||||
@image = @_image if @_image
|
||||
@raw_text = @options[:checkmark]
|
||||
else
|
||||
@image = nil
|
||||
@raw_text = ""
|
||||
end
|
||||
end
|
||||
|
||||
def toggled=(boolean)
|
||||
@toggled = !boolean
|
||||
toggle
|
||||
end
|
||||
|
||||
def clicked_left_mouse_button(sender, x, y)
|
||||
toggle
|
||||
def clicked_left_mouse_button(_sender, _x, _y)
|
||||
self.value = !@value
|
||||
|
||||
@block.call(self) if @block
|
||||
end
|
||||
|
||||
def toggle
|
||||
if @toggled
|
||||
@toggled = false
|
||||
@text.text = ""
|
||||
else
|
||||
@toggled = true
|
||||
@text.text = @options[:checkmark]
|
||||
end
|
||||
:handled
|
||||
end
|
||||
|
||||
def recalculate
|
||||
super
|
||||
return if @image
|
||||
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
@width = _width || @text.textobject.text_width(@options[:checkmark])
|
||||
@height = _height || @text.height
|
||||
|
||||
_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
|
||||
def value=(boolean)
|
||||
@value = boolean
|
||||
|
||||
if boolean
|
||||
@image = @_image if @_image
|
||||
@raw_text = @options[:checkmark]
|
||||
else
|
||||
@image = nil
|
||||
@raw_text = ""
|
||||
end
|
||||
|
||||
recalculate
|
||||
|
||||
publish(:changed, @value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,19 +15,18 @@ module CyberarmEngine
|
||||
|
||||
return unless enabled?
|
||||
|
||||
if respond_to?(event)
|
||||
return :handled if send(event, self, *args) == :handled
|
||||
end
|
||||
return :handled if respond_to?(event) && (send(event, self, *args) == :handled)
|
||||
|
||||
@event_handler[event].reverse_each do |handler|
|
||||
return :handled if handler.call(self, *args) == :handled
|
||||
end
|
||||
|
||||
return nil
|
||||
parent.publish(event, *args) if parent
|
||||
nil
|
||||
end
|
||||
|
||||
def event(event)
|
||||
@event_handler ||= Hash.new
|
||||
@event_handler ||= {}
|
||||
@event_handler[event] ||= []
|
||||
end
|
||||
end
|
||||
@@ -36,11 +35,13 @@ module CyberarmEngine
|
||||
attr_reader :publisher, :event, :handler
|
||||
|
||||
def initialize(publisher, event, handler)
|
||||
@publisher, @event, @handler = publisher, event, handler
|
||||
@publisher = publisher
|
||||
@event = event
|
||||
@handler = handler
|
||||
end
|
||||
|
||||
def unsubscribe
|
||||
@publisher.unsubscribe(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,24 +12,33 @@ module CyberarmEngine
|
||||
|
||||
@root_container = Element::Stack.new(gui_state: self)
|
||||
@game_objects << @root_container
|
||||
@containers = [@root_container]
|
||||
$__current_container__ = @root_container
|
||||
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
|
||||
@menu = nil
|
||||
@focus = nil
|
||||
@mouse_over = nil
|
||||
@mouse_down_on = {}
|
||||
@mouse_down_position = {}
|
||||
@last_mouse_pos = nil
|
||||
@dragging_element = nil
|
||||
@pending_recalculate_request = false
|
||||
|
||||
setup
|
||||
@menu = nil
|
||||
@min_drag_distance = 0
|
||||
@mouse_pos = Vector.new
|
||||
end
|
||||
|
||||
def post_setup
|
||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
||||
end
|
||||
|
||||
# throws :blur event to focused element and sets GuiState focused element
|
||||
# Does NOT throw :focus event at element or set element as focused
|
||||
def focus=(element)
|
||||
@focus.publish(:blur) if @focus and element && @focus != element
|
||||
@focus.publish(:blur) if @focus && element && @focus != element
|
||||
@focus = element
|
||||
end
|
||||
|
||||
@@ -37,15 +46,48 @@ module CyberarmEngine
|
||||
@focus
|
||||
end
|
||||
|
||||
def draw
|
||||
super
|
||||
|
||||
if @menu
|
||||
Gosu.flush
|
||||
@menu.draw
|
||||
end
|
||||
|
||||
if @tip.value.length.positive?
|
||||
Gosu.flush
|
||||
@tip.draw
|
||||
end
|
||||
|
||||
if defined?(GUI_DEBUG)
|
||||
Gosu.flush
|
||||
|
||||
@root_container.debug_draw
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @pending_recalculate_request
|
||||
@root_container.recalculate
|
||||
@root_container.recalculate
|
||||
@root_container.recalculate
|
||||
|
||||
@pending_recalculate_request = false
|
||||
end
|
||||
|
||||
if @pending_focus_request
|
||||
@pending_focus_request = false
|
||||
|
||||
self.focus = @pending_focus_element
|
||||
@pending_focus_element.publish(:focus)
|
||||
end
|
||||
|
||||
@menu&.update
|
||||
super
|
||||
|
||||
new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
|
||||
new_mouse_over = @menu.hit_element?(window.mouse_x, window.mouse_y) if @menu
|
||||
new_mouse_over ||= @root_container.hit_element?(window.mouse_x, window.mouse_y)
|
||||
|
||||
if new_mouse_over
|
||||
new_mouse_over.publish(:enter) if new_mouse_over != @mouse_over
|
||||
new_mouse_over.publish(:hover)
|
||||
@@ -58,12 +100,40 @@ module CyberarmEngine
|
||||
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)
|
||||
|
||||
request_recalculate if @active_width != window.width || @active_height != window.height
|
||||
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
||||
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
||||
@tip.value = @mouse_over.tip if @mouse_over
|
||||
@tip.x = window.mouse_x - @tip.width / 2
|
||||
@tip.x = 0 if @tip.x < 0
|
||||
@tip.x = window.width - @tip.width if @tip.x + @tip.width > window.width
|
||||
@tip.y = window.mouse_y - (@tip.height + 5)
|
||||
@tip.y = 0 if @tip.y < 0
|
||||
@tip.y = window.height - @tip.height if @tip.y + @tip.height > window.height
|
||||
@tip.update
|
||||
@tip.recalculate
|
||||
else
|
||||
@tip.value = ""
|
||||
end
|
||||
else
|
||||
@mouse_moved_at = Gosu.milliseconds
|
||||
end
|
||||
|
||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||
@mouse_pos = @last_mouse_pos.clone
|
||||
|
||||
if @active_width != window.width || @active_height != window.height
|
||||
request_recalculate
|
||||
@root_container.publish(:window_size_changed)
|
||||
end
|
||||
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
end
|
||||
|
||||
def tool_tip_delay
|
||||
250 # ms
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
super
|
||||
|
||||
@@ -74,7 +144,11 @@ module CyberarmEngine
|
||||
redirect_mouse_button(:middle)
|
||||
when Gosu::MsRight
|
||||
redirect_mouse_button(:right)
|
||||
when Gosu::KbF5
|
||||
request_recalculate
|
||||
end
|
||||
|
||||
@focus.button_down(id) if @focus.respond_to?(:button_down)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
@@ -92,9 +166,13 @@ module CyberarmEngine
|
||||
when Gosu::MsWheelDown
|
||||
redirect_mouse_wheel(:down)
|
||||
end
|
||||
|
||||
@focus.button_up(id) if @focus.respond_to?(:button_up)
|
||||
end
|
||||
|
||||
def redirect_mouse_button(button)
|
||||
hide_menu unless @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)
|
||||
|
||||
if @focus && @mouse_over != @focus
|
||||
@focus.publish(:blur)
|
||||
@focus = nil
|
||||
@@ -112,9 +190,19 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def redirect_released_mouse_button(button)
|
||||
hide_menu if @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)
|
||||
|
||||
if @mouse_over
|
||||
@mouse_over.publish(:"released_#{button}_mouse_button", window.mouse_x, window.mouse_y)
|
||||
@mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x, window.mouse_y) if @mouse_over == @mouse_down_on[button]
|
||||
if @mouse_over == @mouse_down_on[button]
|
||||
@mouse_over.publish(:"clicked_#{button}_mouse_button", window.mouse_x,
|
||||
window.mouse_y)
|
||||
end
|
||||
end
|
||||
|
||||
if @dragging_element
|
||||
@dragging_element.publish(:end_drag, window.mouse_x, window.mouse_y, button)
|
||||
@dragging_element = nil
|
||||
end
|
||||
|
||||
@mouse_down_position[button] = nil
|
||||
@@ -122,7 +210,16 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def redirect_holding_mouse_button(button)
|
||||
@mouse_over.publish(:"holding_#{button}_mouse_button", window.mouse_x, window.mouse_y) if @mouse_over
|
||||
if !@dragging_element && @mouse_down_on[button] && @mouse_down_on[button].draggable?(button) && @mouse_pos.distance(@mouse_down_position[button]) > @min_drag_distance
|
||||
@dragging_element = @mouse_down_on[button]
|
||||
@dragging_element.publish(:begin_drag, window.mouse_x, window.mouse_y, button)
|
||||
end
|
||||
|
||||
if @dragging_element
|
||||
@dragging_element.publish(:drag_update, window.mouse_x, window.mouse_y, button) if @dragging_element
|
||||
elsif @mouse_over
|
||||
@mouse_over.publish(:"holding_#{button}_mouse_button", window.mouse_x, window.mouse_y)
|
||||
end
|
||||
end
|
||||
|
||||
def redirect_mouse_wheel(button)
|
||||
@@ -133,5 +230,27 @@ module CyberarmEngine
|
||||
def request_recalculate
|
||||
@pending_recalculate_request = true
|
||||
end
|
||||
|
||||
def request_focus(element)
|
||||
@pending_focus_request = true
|
||||
@pending_focus_element = element
|
||||
end
|
||||
|
||||
def show_menu(list_box)
|
||||
@menu = list_box
|
||||
end
|
||||
|
||||
def hide_menu
|
||||
@menu = nil
|
||||
end
|
||||
|
||||
def to_s
|
||||
# "#{self.class} children=#{@children.map { |c| c.to_s }}"
|
||||
@root_container.to_s
|
||||
end
|
||||
|
||||
def inspect
|
||||
to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
module Gosu
|
||||
class Color
|
||||
def _dump(level)
|
||||
def _dump(_level)
|
||||
[
|
||||
"%02X" % self.alpha,
|
||||
"%02X" % self.red,
|
||||
"%02X" % self.green,
|
||||
"%02X" % self.blue
|
||||
"%02X" % alpha,
|
||||
"%02X" % red,
|
||||
"%02X" % green,
|
||||
"%02X" % blue
|
||||
].join
|
||||
end
|
||||
|
||||
@@ -17,21 +17,33 @@ end
|
||||
|
||||
module CyberarmEngine
|
||||
class Style
|
||||
attr_reader :hash
|
||||
|
||||
def initialize(hash = {})
|
||||
@hash = Marshal.load(Marshal.dump(hash))
|
||||
h = Marshal.load(Marshal.dump(hash))
|
||||
|
||||
h[:default] = {}
|
||||
|
||||
h.each do |key, value|
|
||||
next if value.is_a?(Hash)
|
||||
|
||||
h[:default][key] = value
|
||||
end
|
||||
|
||||
@hash = h
|
||||
end
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
def method_missing(method, *args)
|
||||
if method.to_s.end_with?("=")
|
||||
raise "Did not expect more than 1 argument" if args.size > 1
|
||||
return @hash[method.to_s.sub("=", "").to_sym] = args.first
|
||||
|
||||
elsif args.size == 0
|
||||
return @hash[method]
|
||||
@hash[method.to_s.sub("=", "").to_sym] = args.first
|
||||
|
||||
elsif args.empty?
|
||||
@hash[method]
|
||||
else
|
||||
raise ArgumentError, "Did not expect arguments"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,17 +11,21 @@ module CyberarmEngine
|
||||
|
||||
def theme_defaults(options)
|
||||
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
||||
|
||||
_theme = THEME
|
||||
_theme = _theme.merge(options[:theme]) if options[:theme]
|
||||
_theme = deep_merge(_theme, options[:theme]) if options[:theme]
|
||||
_theme.delete(:theme) if options[:theme]
|
||||
|
||||
hash = {}
|
||||
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! do |c|
|
||||
c.to_s.split("::").last.to_sym
|
||||
end.reverse!
|
||||
|
||||
class_names.each do |klass|
|
||||
next unless data = _theme.dig(klass)
|
||||
data.each do |key, value|
|
||||
|
||||
data.each do |_key, _value|
|
||||
hash.merge!(data)
|
||||
end
|
||||
end
|
||||
@@ -32,7 +36,7 @@ module CyberarmEngine
|
||||
# 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|
|
||||
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?
|
||||
@@ -41,8 +45,6 @@ module CyberarmEngine
|
||||
other_val
|
||||
end
|
||||
end
|
||||
|
||||
return hash
|
||||
end
|
||||
|
||||
THEME = {
|
||||
@@ -51,33 +53,44 @@ module CyberarmEngine
|
||||
y: 0,
|
||||
z: 30,
|
||||
|
||||
width: nil,
|
||||
width: nil,
|
||||
height: nil,
|
||||
color: Gosu::Color::WHITE,
|
||||
color: Gosu::Color::WHITE,
|
||||
background: Gosu::Color::NONE,
|
||||
margin: 0,
|
||||
padding: 0,
|
||||
margin: 0,
|
||||
padding: 0,
|
||||
border_thickness: 0,
|
||||
border_color: Gosu::Color::NONE,
|
||||
border_radius: 0,
|
||||
border_radius: 0
|
||||
},
|
||||
|
||||
Container: { # < Element (Base class for Stack and Flow)
|
||||
debug_color: Gosu::Color::YELLOW
|
||||
},
|
||||
|
||||
Button: { # < Label
|
||||
margin: 1,
|
||||
padding: 4,
|
||||
border_thickness: 4,
|
||||
margin: 1,
|
||||
padding: 4,
|
||||
border_thickness: 1,
|
||||
border_color: ["ffd59674".hex, "ffff8746".hex],
|
||||
border_radius: 0,
|
||||
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
||||
text_align: :center,
|
||||
text_wrap: :none,
|
||||
|
||||
hover: {
|
||||
color: Gosu::Color.rgb(200,200,200),
|
||||
background: ["ffB23E41".to_i(16), "ffFF7C00".to_i(16)],
|
||||
color: Gosu::Color.rgb(200, 200, 200),
|
||||
background: ["ffB23E41".to_i(16), "ffFF7C00".to_i(16)]
|
||||
},
|
||||
|
||||
active: {
|
||||
color: Gosu::Color::BLACK,
|
||||
background: ["ffB23E41".to_i(16)]
|
||||
},
|
||||
|
||||
disabled: {
|
||||
color: Gosu::Color::GRAY,
|
||||
background: 0xff303030
|
||||
}
|
||||
},
|
||||
|
||||
@@ -88,30 +101,101 @@ module CyberarmEngine
|
||||
caret_width: 2,
|
||||
caret_color: Gosu::Color::WHITE,
|
||||
caret_interval: 500,
|
||||
selection_color: Gosu::Color.rgba(255, 128, 50, 200),
|
||||
text_align: :left
|
||||
},
|
||||
|
||||
Image: { # < Element
|
||||
color: Gosu::Color::WHITE,
|
||||
tileable: false,
|
||||
retro: false
|
||||
},
|
||||
|
||||
Label: { # < Element
|
||||
text_size: 28,
|
||||
text_shadow: false,
|
||||
font: "Arial",
|
||||
margin: 0,
|
||||
padding: 2
|
||||
TextBlock: { # < Element
|
||||
text_size: 28,
|
||||
text_wrap: :word_wrap, # :word_wrap, :break_word, :none
|
||||
text_shadow: false,
|
||||
text_align: :left,
|
||||
font: "Arial",
|
||||
margin: 0,
|
||||
padding: 2
|
||||
},
|
||||
|
||||
Banner: { # < TextBlock
|
||||
text_size: 48
|
||||
},
|
||||
|
||||
Title: { # < TextBlock
|
||||
text_size: 34
|
||||
},
|
||||
|
||||
Subtitle: { # < TextBlock
|
||||
text_size: 26
|
||||
},
|
||||
|
||||
Tagline: { # < TextBlock
|
||||
text_size: 24
|
||||
},
|
||||
|
||||
Caption: { # < TextBlock
|
||||
text_size: 22
|
||||
},
|
||||
|
||||
Para: { # < TextBlock
|
||||
text_size: 18
|
||||
},
|
||||
|
||||
Inscription: { # < TextBlock
|
||||
text_size: 16
|
||||
},
|
||||
|
||||
ToolTip: { # < TextBlock
|
||||
color: Gosu::Color::WHITE,
|
||||
padding_top: 4,
|
||||
padding_bottom: 4,
|
||||
padding_left: 8,
|
||||
padding_right: 8,
|
||||
border_thickness: 1,
|
||||
border_color: 0xffaaaaaa,
|
||||
background: 0xff404040
|
||||
},
|
||||
Link: { # < TextBlock
|
||||
color: Gosu::Color::BLUE,
|
||||
border_thickness: 1,
|
||||
border_bottom_color: Gosu::Color::BLUE,
|
||||
hover: {
|
||||
color: 0xff_ff00ff,
|
||||
border_bottom_color: 0xff_ff00ff
|
||||
},
|
||||
active: {
|
||||
color: 0xff_ff0000,
|
||||
border_bottom_color: 0xff_ff0000
|
||||
}
|
||||
},
|
||||
|
||||
ToggleButton: { # < Button
|
||||
checkmark: "√"
|
||||
},
|
||||
|
||||
CheckBox: { # < Flow
|
||||
text_wrap: :none
|
||||
},
|
||||
|
||||
Progress: { # < Element
|
||||
width: 250,
|
||||
height: 36,
|
||||
background: 0xff111111,
|
||||
fraction_background: [0xffc75e61, 0xffe26623],
|
||||
border_thickness: 4,
|
||||
border_thickness: 1,
|
||||
border_color: [0xffd59674, 0xffff8746]
|
||||
},
|
||||
|
||||
Slider: { # < Element
|
||||
width: 250,
|
||||
height: 36,
|
||||
background: 0xff111111,
|
||||
fraction_background: [0xffc75e61, 0xffe26623],
|
||||
border_thickness: 1,
|
||||
border_color: [0xffd59674, 0xffff8746]
|
||||
}
|
||||
}.freeze
|
||||
|
||||
293
lib/cyberarm_engine/vector.rb
Normal file
293
lib/cyberarm_engine/vector.rb
Normal file
@@ -0,0 +1,293 @@
|
||||
module CyberarmEngine
|
||||
class Vector
|
||||
##
|
||||
# Creates a up vector
|
||||
#
|
||||
# Vector.new(0, 1, 0)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.up
|
||||
Vector.new(0, 1, 0)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a down vector
|
||||
#
|
||||
# Vector.new(0, -1, 0)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.down
|
||||
Vector.new(0, -1, 0)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a left vector
|
||||
#
|
||||
# Vector.new(-1, 0, 0)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.left
|
||||
Vector.new(-1, 0, 0)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a right vector
|
||||
#
|
||||
# Vector.new(1, 0, 0)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.right
|
||||
Vector.new(1, 0, 0)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a forward vector
|
||||
#
|
||||
# Vector.new(0, 0, 1)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.forward
|
||||
Vector.new(0, 0, 1)
|
||||
end
|
||||
|
||||
##
|
||||
# Creates a backward vector
|
||||
#
|
||||
# Vector.new(0, 0, -1)
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def self.backward
|
||||
Vector.new(0, 0, -1)
|
||||
end
|
||||
|
||||
attr_accessor :x, :y, :z, :weight
|
||||
|
||||
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||
@x = x
|
||||
@y = y
|
||||
@z = z
|
||||
@weight = weight
|
||||
end
|
||||
|
||||
alias w weight
|
||||
alias w= weight=
|
||||
|
||||
# @return [Boolean]
|
||||
def ==(other)
|
||||
if other.is_a?(Numeric)
|
||||
@x == other &&
|
||||
@y == other &&
|
||||
@z == other &&
|
||||
@weight == other
|
||||
elsif other.is_a?(Vector)
|
||||
@x == other.x &&
|
||||
@y == other.y &&
|
||||
@z == other.z &&
|
||||
@weight == other.weight
|
||||
else
|
||||
other == self
|
||||
end
|
||||
end
|
||||
|
||||
# Create a new vector using {x} and {y} values
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def xy
|
||||
Vector.new(@x, @y)
|
||||
end
|
||||
|
||||
# Performs math operation, excluding {weight}
|
||||
private def operator(function, other)
|
||||
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 Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def +(other)
|
||||
operator("+", other)
|
||||
end
|
||||
|
||||
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def -(other)
|
||||
operator("-", other)
|
||||
end
|
||||
|
||||
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def *(other)
|
||||
operator("*", other)
|
||||
end
|
||||
|
||||
def multiply_transform(transform)
|
||||
e = transform.elements
|
||||
|
||||
x = @x * e[0] + @y * e[1] + @z * e[2] + 1 * e[3]
|
||||
y = @x * e[4] + @y * e[5] + @z * e[6] + 1 * e[7]
|
||||
z = @x * e[8] + @y * e[9] + @z * e[10] + 1 * e[11]
|
||||
w = @x * e[12] + @y * e[13] + @z * e[14] + 1 * e[15]
|
||||
|
||||
Vector.new(x / 1, y / 1, z / 1, w / 1)
|
||||
end
|
||||
|
||||
# Divides Vector and Numeric or Vector and Vector, excluding {weight}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
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
|
||||
|
||||
# dot product of {Vector}
|
||||
# @return [Integer|Float]
|
||||
def dot(other)
|
||||
product = 0
|
||||
|
||||
a = to_a
|
||||
b = other.to_a
|
||||
|
||||
3.times do |i|
|
||||
product += (a[i] * b[i])
|
||||
end
|
||||
|
||||
product
|
||||
end
|
||||
|
||||
# cross product of {Vector}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def cross(other)
|
||||
a = 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
|
||||
# @return [Float]
|
||||
def angle(other)
|
||||
Math.acos(normalized.dot(other.normalized)) * 180 / Math::PI
|
||||
end
|
||||
|
||||
# returns magnitude of Vector, ignoring #weight
|
||||
# @return [Float]
|
||||
def magnitude
|
||||
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
||||
end
|
||||
|
||||
##
|
||||
# returns normalized {Vector}
|
||||
#
|
||||
# @example
|
||||
# CyberarmEngine::Vector.new(50, 21.2, 45).normalized
|
||||
# # => <CyberarmEngine::Vector:0x001 @x=0.7089... @y=0.3005... @z=0.6380... @weight=0>
|
||||
#
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def normalized
|
||||
mag = magnitude
|
||||
self / Vector.new(mag, mag, mag)
|
||||
end
|
||||
|
||||
# returns a direction {Vector}
|
||||
#
|
||||
# z is pitch
|
||||
#
|
||||
# y is yaw
|
||||
#
|
||||
# x is roll
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def direction
|
||||
_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
|
||||
|
||||
# returns an inverse {Vector}
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def inverse
|
||||
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||
end
|
||||
|
||||
# Adds up values of {x}, {y}, and {z}
|
||||
# @return [Integer|Float]
|
||||
def sum
|
||||
@x + @y + @z
|
||||
end
|
||||
|
||||
##
|
||||
# Linear interpolation: smoothly transition between two {Vector}
|
||||
#
|
||||
# CyberarmEngine::Vector.new(100, 100, 100).lerp( CyberarmEngine::Vector.new(0, 0, 0), 0.75 )
|
||||
# # => <CyberarmEngine::Vector:0x0001 @x=75.0, @y=75.0, @z=75.0, @weight=0>
|
||||
#
|
||||
# @param other [CyberarmEngine::Vector | Integer | Float] value to subtract from
|
||||
# @param factor [Float] how complete transition to _other_ is, in range [0.0..1.0]
|
||||
# @return [CyberarmEngine::Vector]
|
||||
def lerp(other, factor)
|
||||
(self - other) * factor.clamp(0.0, 1.0)
|
||||
end
|
||||
|
||||
# 2D distance using X and Y
|
||||
# @return [Float]
|
||||
def distance(other)
|
||||
Math.sqrt((@x - other.x)**2 + (@y - other.y)**2)
|
||||
end
|
||||
|
||||
# 2D distance using X and Z
|
||||
# @return [Float]
|
||||
def gl_distance2d(other)
|
||||
Math.sqrt((@x - other.x)**2 + (@z - other.z)**2)
|
||||
end
|
||||
|
||||
# 3D distance using X, Y, and Z
|
||||
# @return [Float]
|
||||
def distance3d(other)
|
||||
Math.sqrt((@x - other.x)**2 + (@y - other.y)**2 + (@z - other.z)**2)
|
||||
end
|
||||
|
||||
# Converts {Vector} to Array
|
||||
# @return [Array]
|
||||
def to_a
|
||||
[@x, @y, @z, @weight]
|
||||
end
|
||||
|
||||
# Converts {Vector} to String
|
||||
# @return [String]
|
||||
def to_s
|
||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||
end
|
||||
|
||||
# Converts {Vector} to Hash
|
||||
# @return [Hash]
|
||||
def to_h
|
||||
{ x: @x, y: @y, z: @z, weight: @weight }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev"
|
||||
VERSION = "0.9.0"
|
||||
NAME = "InDev".freeze
|
||||
VERSION = "0.18.0".freeze
|
||||
end
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
module CyberarmEngine
|
||||
class Engine < Gosu::Window
|
||||
class Window < Gosu::Window
|
||||
include Common
|
||||
|
||||
IMAGES = {}
|
||||
SAMPLES= {}
|
||||
SONGS = {}
|
||||
SAMPLES = {}
|
||||
SONGS = {}
|
||||
|
||||
attr_accessor :show_cursor
|
||||
attr_reader :current_state, :last_frame_time
|
||||
attr_writer :exit_on_opengl_error
|
||||
attr_reader :last_frame_time
|
||||
|
||||
def self.now
|
||||
Gosu.milliseconds
|
||||
end
|
||||
|
||||
def self.dt
|
||||
$window.last_frame_time/1000.0
|
||||
$window.last_frame_time / 1000.0
|
||||
end
|
||||
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0/60, resizable: false)
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false)
|
||||
@show_cursor = false
|
||||
|
||||
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable)
|
||||
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable, borderless: borderless)
|
||||
$window = self
|
||||
@last_frame_time = Gosu.milliseconds-1
|
||||
@last_frame_time = Gosu.milliseconds - 1
|
||||
@current_frame_time = Gosu.milliseconds
|
||||
self.caption = "CyberarmEngine #{CyberarmEngine::VERSION} #{Gosu.language}"
|
||||
|
||||
@states = []
|
||||
@exit_on_opengl_error = false
|
||||
|
||||
setup if defined?(setup)
|
||||
end
|
||||
@@ -34,8 +38,10 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def update
|
||||
Stats.clear
|
||||
|
||||
current_state.update if current_state
|
||||
@last_frame_time = Gosu.milliseconds-@current_frame_time
|
||||
@last_frame_time = Gosu.milliseconds - @current_frame_time
|
||||
@current_frame_time = Gosu.milliseconds
|
||||
end
|
||||
|
||||
@@ -44,7 +50,15 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def dt
|
||||
@last_frame_time/1000.0
|
||||
@last_frame_time / 1000.0
|
||||
end
|
||||
|
||||
def aspect_ratio
|
||||
width / height.to_f
|
||||
end
|
||||
|
||||
def exit_on_opengl_error?
|
||||
@exit_on_opengl_error
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
@@ -57,17 +71,23 @@ module CyberarmEngine
|
||||
current_state.button_up(id) if current_state
|
||||
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)
|
||||
@states << klass
|
||||
klass.setup if options[:setup]
|
||||
klass.post_setup if options[:setup]
|
||||
else
|
||||
@states << klass.new(options) if child_of?(klass, GameState)
|
||||
@states << klass.new if child_of?(klass, Element::Container)
|
||||
current_state.setup if current_state.instance_of?(klass) && options[:setup]
|
||||
current_state.post_setup if current_state.instance_of?(klass) && options[:setup]
|
||||
end
|
||||
end
|
||||
|
||||
private def child_of?(input, klass)
|
||||
input.ancestors.detect {|c| c == klass}
|
||||
input.ancestors.detect { |c| c == klass }
|
||||
end
|
||||
|
||||
def current_state
|
||||
@@ -75,10 +95,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def previous_state
|
||||
if @states.size > 1 && state = @states[@states.size-2]
|
||||
return state
|
||||
else
|
||||
return nil
|
||||
if @states.size > 1 && (state = @states[@states.size - 2])
|
||||
state
|
||||
end
|
||||
end
|
||||
|
||||
@@ -86,12 +104,17 @@ module CyberarmEngine
|
||||
@states.pop
|
||||
end
|
||||
|
||||
def shift_state
|
||||
@states.shift
|
||||
end
|
||||
|
||||
# Sourced from https://gist.github.com/ippa/662583
|
||||
def draw_circle(cx,cy,r, z = 9999,color = Gosu::Color::GREEN, step = 10)
|
||||
def 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)
|
||||
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
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
||||
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
||||
require "cyberarm_engine"
|
||||
|
||||
require "minitest/autorun"
|
||||
|
||||
Reference in New Issue
Block a user