Compare commits
33 Commits
971ac5ff34
...
v0.30.0
| Author | SHA1 | Date | |
|---|---|---|---|
| a1513be709 | |||
| d0d5bbac3a | |||
| 0ae3ecc145 | |||
| afa8a9dbb6 | |||
| 0ec55bef55 | |||
| 904ac78d89 | |||
| 70d270688f | |||
| 685c94bd1b | |||
| d6d3c07bbc | |||
| 2cc1382696 | |||
| 9fca64b643 | |||
| abe99ce949 | |||
| 66fb136c83 | |||
| b23e5c2a42 | |||
| 747822aa61 | |||
| 140e7a3619 | |||
| 349222f387 | |||
| 9d4bc89174 | |||
| 8e7c6f8a19 | |||
| cd5bd51a02 | |||
| adf517f374 | |||
| 1c868522b7 | |||
| a79b9cc20e | |||
| c229329542 | |||
| 512d8a5c0f | |||
| d158d2fdd2 | |||
| 9f654129ef | |||
| b694269409 | |||
| 3375d2d224 | |||
| a47505e7fa | |||
| e572679f0b | |||
| 4286ca06b0 | |||
| a5636c82b3 |
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/.idea
|
||||
/.bundle/
|
||||
/.yardoc
|
||||
/_yardoc/
|
||||
|
||||
28
Gemfile.lock
@@ -1,25 +1,37 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
cyberarm_engine (0.24.5)
|
||||
cyberarm_engine (0.30.0)
|
||||
gosu (~> 1.1)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
gosu (1.4.6)
|
||||
minitest (5.25.5)
|
||||
rake (13.2.1)
|
||||
ffi (1.17.4-x86_64-linux-gnu)
|
||||
ffi (1.17.4-x86_64-linux-musl)
|
||||
gosu (1.5.0-x86_64-linux)
|
||||
ffi (~> 1.12)
|
||||
minitest (5.27.0)
|
||||
rake (13.4.2)
|
||||
|
||||
PLATFORMS
|
||||
x64-mingw-ucrt
|
||||
x86_64-linux
|
||||
x86_64-linux-gnu
|
||||
x86_64-linux-musl
|
||||
|
||||
DEPENDENCIES
|
||||
bundler (~> 2.2)
|
||||
bundler (~> 4)
|
||||
cyberarm_engine!
|
||||
minitest (~> 5.0)
|
||||
rake (~> 13.0)
|
||||
|
||||
CHECKSUMS
|
||||
bundler (4.1.0.beta1) sha256=8a2dac8ca276ff4eff5efc2c6457b0044080fc1e431534ad7c5cc6187232b800
|
||||
cyberarm_engine (0.30.0)
|
||||
ffi (1.17.4-x86_64-linux-gnu) sha256=9d3db14c2eae074b382fa9c083fe95aec6e0a1451da249eab096c34002bc752d
|
||||
ffi (1.17.4-x86_64-linux-musl) sha256=3fdf9888483de005f8ef8d1cf2d3b20d86626af206cbf780f6a6a12439a9c49e
|
||||
gosu (1.5.0-x86_64-linux)
|
||||
minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
|
||||
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
|
||||
|
||||
BUNDLED WITH
|
||||
2.6.8
|
||||
4.1.0.beta1
|
||||
|
||||
BIN
assets/textures/ui/circle.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
assets/textures/ui/circle_small.png
Normal file
|
After Width: | Height: | Size: 582 B |
BIN
assets/textures/ui/rounded.png
Normal file
|
After Width: | Height: | Size: 430 B |
BIN
assets/textures/ui/rounded_bottom.png
Normal file
|
After Width: | Height: | Size: 341 B |
BIN
assets/textures/ui/rounded_left.png
Normal file
|
After Width: | Height: | Size: 350 B |
BIN
assets/textures/ui/rounded_left_bottom.png
Normal file
|
After Width: | Height: | Size: 295 B |
BIN
assets/textures/ui/rounded_left_top.png
Normal file
|
After Width: | Height: | Size: 304 B |
BIN
assets/textures/ui/rounded_right.png
Normal file
|
After Width: | Height: | Size: 357 B |
BIN
assets/textures/ui/rounded_right_bottom.png
Normal file
|
After Width: | Height: | Size: 292 B |
BIN
assets/textures/ui/rounded_right_top.png
Normal file
|
After Width: | Height: | Size: 299 B |
BIN
assets/textures/ui/rounded_small.png
Normal file
|
After Width: | Height: | Size: 212 B |
BIN
assets/textures/ui/rounded_tiny.png
Normal file
|
After Width: | Height: | Size: 173 B |
BIN
assets/textures/ui/rounded_top.png
Normal file
|
After Width: | Height: | Size: 361 B |
BIN
assets/textures/ui/square.png
Normal file
|
After Width: | Height: | Size: 239 B |
@@ -28,9 +28,8 @@ Gem::Specification.new do |spec|
|
||||
spec.require_paths = %w[lib assets]
|
||||
|
||||
spec.add_dependency "gosu", "~> 1.1"
|
||||
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 2.2"
|
||||
spec.add_development_dependency "bundler", "~> 4"
|
||||
spec.add_development_dependency "minitest", "~> 5.0"
|
||||
spec.add_development_dependency "rake", "~> 13.0"
|
||||
end
|
||||
|
||||
@@ -10,6 +10,7 @@ require "json"
|
||||
require_relative "cyberarm_engine/version"
|
||||
require_relative "cyberarm_engine/stats"
|
||||
require_relative "cyberarm_engine/result"
|
||||
require_relative "cyberarm_engine/event_bus"
|
||||
|
||||
require_relative "cyberarm_engine/common"
|
||||
|
||||
@@ -41,6 +42,13 @@ require_relative "cyberarm_engine/console/command"
|
||||
require_relative "cyberarm_engine/console/subcommand"
|
||||
require_relative "cyberarm_engine/console/commands/help_command"
|
||||
|
||||
require_relative "cyberarm_engine/pathfinding/graph"
|
||||
require_relative "cyberarm_engine/pathfinding/graphs/grid"
|
||||
require_relative "cyberarm_engine/pathfinding/graph_search"
|
||||
require_relative "cyberarm_engine/pathfinding/graph_searches/breadth_first_search"
|
||||
require_relative "cyberarm_engine/pathfinding/graph_searches/dijkstra_search"
|
||||
require_relative "cyberarm_engine/pathfinding/graph_searches/astar_search"
|
||||
|
||||
require_relative "cyberarm_engine/ui/dsl"
|
||||
|
||||
require_relative "cyberarm_engine/ui/theme"
|
||||
@@ -58,6 +66,7 @@ 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/widget"
|
||||
require_relative "cyberarm_engine/ui/elements/check_box"
|
||||
require_relative "cyberarm_engine/ui/elements/radio"
|
||||
require_relative "cyberarm_engine/ui/elements/progress"
|
||||
@@ -67,6 +76,8 @@ require_relative "cyberarm_engine/ui/elements/menu_item"
|
||||
|
||||
require_relative "cyberarm_engine/game_state"
|
||||
require_relative "cyberarm_engine/ui/gui_state"
|
||||
require_relative "cyberarm_engine/ui/dialog"
|
||||
require_relative "cyberarm_engine/ui/page"
|
||||
|
||||
require_relative "cyberarm_engine/builtin/intro_state"
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
module CyberarmEngine
|
||||
class BackgroundNineSlice
|
||||
include CyberarmEngine::Common
|
||||
attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color
|
||||
attr_reader :image
|
||||
|
||||
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE)
|
||||
CACHE = {}
|
||||
|
||||
attr_accessor :x, :y, :z, :width, :height, :mode, :color, :render_mode
|
||||
attr_reader :image, :left, :top, :right, :bottom
|
||||
|
||||
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE, render_mode: :default)
|
||||
@image = get_image(image_path) if image_path
|
||||
|
||||
@x = x
|
||||
@@ -22,6 +25,7 @@ module CyberarmEngine
|
||||
@bottom = bottom
|
||||
|
||||
@color = color
|
||||
@render_mode = render_mode
|
||||
|
||||
nine_slice if @image
|
||||
end
|
||||
@@ -32,8 +36,43 @@ module CyberarmEngine
|
||||
nine_slice if @image && old_image != @image
|
||||
end
|
||||
|
||||
def set_edges(left: @left, right: @right, top: @top, bottom: @bottom)
|
||||
changed = [left == @left, right == @right, top == @top, bottom == @bottom].any?(false)
|
||||
|
||||
@left = left
|
||||
@top = top
|
||||
@right = right
|
||||
@bottom = bottom
|
||||
|
||||
nine_slice if changed
|
||||
end
|
||||
|
||||
def left=(n)
|
||||
nine_slice if n != @left
|
||||
@left = n
|
||||
n
|
||||
end
|
||||
|
||||
def right=(n)
|
||||
nine_slice if n != @right
|
||||
@right = n
|
||||
n
|
||||
end
|
||||
|
||||
def top=(n)
|
||||
nine_slice if n != @top
|
||||
@top = n
|
||||
n
|
||||
end
|
||||
|
||||
def bottom=(n)
|
||||
nine_slice if n != @bottom
|
||||
@bottom = n
|
||||
n
|
||||
end
|
||||
|
||||
def nine_slice
|
||||
# pp [@left, @top, @right, @bottom, @image.width]
|
||||
# pp [@left, @top, @right, @bottom, @image.width, @image.height]
|
||||
|
||||
@segment_top_left = @image.subimage(0, 0, @left, @top)
|
||||
@segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)
|
||||
@@ -83,57 +122,58 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def draw_stretched
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color)
|
||||
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color) # SCALE X
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color, @render_mode)
|
||||
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale, 1, @color, @render_mode) # SCALE X
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color, @render_mode)
|
||||
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color) # SCALE Y
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
|
||||
@segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top, @z, 1, height_scale, @color, @render_mode) # SCALE Y
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color, @render_mode)
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color, @render_mode) # SCALE X
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color, @render_mode)
|
||||
@segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color, @render_mode) # SCALE Y
|
||||
|
||||
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color, @render_mode) # SCALE X and SCALE Y
|
||||
end
|
||||
|
||||
def draw_tiled
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color)
|
||||
@segment_top_left.draw(@x, @y, @z, 1, 1, @color, @render_mode)
|
||||
|
||||
# p [width_scale, height_scale]
|
||||
|
||||
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, 1, 1, @color) # SCALE X
|
||||
@segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z, 1, 1, @color, @render_mode) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color)
|
||||
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z, 1, 1, @color, @render_mode)
|
||||
|
||||
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, 1, 1, @color) # SCALE Y
|
||||
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z, 1, 1, @color, @render_mode) # SCALE Y
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color)
|
||||
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z, 1, 1, @color, @render_mode)
|
||||
|
||||
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, 1, 1, @color) # SCALE X
|
||||
@segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z, 1, 1, @color, @render_mode) # SCALE X
|
||||
end
|
||||
end
|
||||
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
|
||||
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color, @render_mode)
|
||||
|
||||
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, 1, 1, @color) # SCALE Y
|
||||
@segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z, 1, 1, @color, @render_mode) # 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, 1, 1, @color) # SCALE X and SCALE Y
|
||||
@segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z, 1, 1, @color, @render_mode) # SCALE X and SCALE Y
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
module CyberarmEngine
|
||||
GUI_DEBUG = false
|
||||
|
||||
module Common
|
||||
ImageBlob = Data.define(:to_blob, :columns, :rows)
|
||||
|
||||
|
||||
39
lib/cyberarm_engine/event_bus.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module CyberarmEngine
|
||||
class EventBus
|
||||
Subscriber = Data.define(:event, :listener, :method, :callback)
|
||||
@subscribers = []
|
||||
|
||||
def self.subscribe(event, listener = nil, method = nil, &block)
|
||||
raise "Subscriber cannot have both a method AND a callback block" if method && block_given?
|
||||
|
||||
subscriber = Subscriber.new(event, listener, method, block)
|
||||
@subscribers << subscriber
|
||||
|
||||
subscriber
|
||||
end
|
||||
|
||||
def self.unsubscribe(event, listener_or_subscriber)
|
||||
@subscribers.delete_if { |s| s.event == event && (s == listener_or_subscriber || s.listener == listener_or_subscriber) }
|
||||
end
|
||||
|
||||
def self.unsubscribe!(event)
|
||||
@subscribers.delete_if { |s| s.event == event }
|
||||
end
|
||||
|
||||
def self.announce(event, *payload)
|
||||
subscribers = @subscribers.select { |s| s.event == event }
|
||||
|
||||
subscribers.each do |subscriber|
|
||||
subscriber&.listener&.send(subscriber.method, *payload) if subscriber&.method
|
||||
subscriber.callback&.call(*payload)
|
||||
rescue StandardError => e
|
||||
# TODO
|
||||
warn "Failed to deliver event (#{event}) to listener (#{subscriber.inspect}): #{e}"
|
||||
end
|
||||
end
|
||||
|
||||
def self.publish(*args)
|
||||
announce(*args)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -33,6 +33,9 @@ module CyberarmEngine
|
||||
@timers.delete_if(&:dead?)
|
||||
end
|
||||
|
||||
def fixed_update(dt)
|
||||
end
|
||||
|
||||
def needs_redraw?
|
||||
true
|
||||
end
|
||||
|
||||
64
lib/cyberarm_engine/pathfinding/graph.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class Graph
|
||||
class Node
|
||||
KEY_FORMAT = "%s:%s:%s".freeze
|
||||
|
||||
attr_reader :x, :y, :z, :cost, :key, :edge_nodes
|
||||
|
||||
def initialize(x:, y:, z: 0, cost: 0)
|
||||
@x = x
|
||||
@y = y
|
||||
@z = z
|
||||
@cost = cost
|
||||
|
||||
@key = format(KEY_FORMAT, @x, @y, @z).freeze
|
||||
|
||||
@edge_nodes = []
|
||||
end
|
||||
|
||||
def add_edge_node(node)
|
||||
index = @edge_nodes.find_index { |n| n.key == node.key }
|
||||
|
||||
return if index
|
||||
|
||||
@edge_nodes.push(node)
|
||||
end
|
||||
|
||||
def remove_edge_node(node)
|
||||
index = @edge_nodes.find_index { |n| n.key == node.key }
|
||||
|
||||
return unless index
|
||||
|
||||
@edge_nodes.delete(index)
|
||||
end
|
||||
|
||||
def inspect
|
||||
format "#<%<class>s:%<id>#018x @key=%<key>s @cost=%<cost>s @edge_nodes=%<edge_nodes>s>",
|
||||
class: self.class,
|
||||
id: object_id << 1,
|
||||
key: key.inspect,
|
||||
cost: cost.inspect,
|
||||
edge_nodes: edge_nodes.map { |n| n&.key }
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(nodes: {})
|
||||
@nodes = nodes
|
||||
end
|
||||
|
||||
def node(key)
|
||||
@nodes[key]
|
||||
end
|
||||
|
||||
def node_at(x, y, z = 0)
|
||||
key = format(Node::KEY_FORMAT, x, y, z)
|
||||
@nodes[key]
|
||||
end
|
||||
|
||||
def neighbors(node)
|
||||
node.edge_nodes
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/cyberarm_engine/pathfinding/graph_search.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class GraphSearch
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class AStarSearch < GraphSearch
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class BreadthFirstSearch < GraphSearch
|
||||
def self.search(graph:, start:, goal: nil)
|
||||
frontier = []
|
||||
came_from = {}
|
||||
|
||||
frontier.push(start)
|
||||
came_from[start.key] = start
|
||||
|
||||
while(current = frontier.shift)
|
||||
# early exit if we reach our goal
|
||||
break if current == goal
|
||||
|
||||
graph.neighbors(current).each do |neighbor|
|
||||
next if came_from[neighbor.key]
|
||||
|
||||
frontier.push(neighbor)
|
||||
came_from[neighbor.key] = current
|
||||
end
|
||||
end
|
||||
|
||||
came_from
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class DijkstraSearch < GraphSearch
|
||||
end
|
||||
end
|
||||
end
|
||||
52
lib/cyberarm_engine/pathfinding/graphs/grid.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
module CyberarmEngine
|
||||
module Pathfinding
|
||||
class Grid < Graph
|
||||
def add_node(x:, y:, z: 0, cost: 0, replace: false)
|
||||
key = format(Node::KEY_FORMAT, x, y, z)
|
||||
node = @nodes[key]
|
||||
|
||||
raise "Node at: #{key} already exists! (#{node})" if node && !replace
|
||||
|
||||
node = Node.new(x: x, y: y, z: z, cost: cost)
|
||||
@nodes[key] = node
|
||||
|
||||
update_node_edge_nodes(node)
|
||||
node
|
||||
end
|
||||
|
||||
def remove_node(node)
|
||||
update_node_edge_nodes(node, remove_node: true)
|
||||
|
||||
@nodes.delete(node.key)
|
||||
end
|
||||
|
||||
def update_node_edge_nodes(node, remove_node: false)
|
||||
x = node.x
|
||||
y = node.y
|
||||
z = node.z
|
||||
|
||||
# add node to neighbors
|
||||
# 2D
|
||||
left = @nodes[format(Node::KEY_FORMAT, x - 1, y, z)]
|
||||
right = @nodes[format(Node::KEY_FORMAT, x + 1, y, z)]
|
||||
top = @nodes[format(Node::KEY_FORMAT, x, y - 1, z)]
|
||||
bottom = @nodes[format(Node::KEY_FORMAT, x, y + 1, z)]
|
||||
# 3D
|
||||
front = @nodes[format(Node::KEY_FORMAT, x, y, z - 1)]
|
||||
back = @nodes[format(Node::KEY_FORMAT, x, y, z + 1)]
|
||||
|
||||
possible_edge_nodes = [left, right, top, bottom, front, back]
|
||||
|
||||
if remove_node
|
||||
possible_edge_nodes.each { |n| n&.remove_edge_node(node) }
|
||||
else
|
||||
possible_edge_nodes.each { |n| n&.add_edge_node(node) }
|
||||
end
|
||||
end
|
||||
|
||||
def update_edge_nodes
|
||||
@nodes.each { |k, n| update_node_edge_nodes(n) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,7 +9,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def okay?
|
||||
!@error
|
||||
!error?
|
||||
end
|
||||
|
||||
def error?
|
||||
@@ -17,4 +17,3 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ module CyberarmEngine
|
||||
@color = Gosu::Color::WHITE
|
||||
end
|
||||
@mode = options[:mode] || :default
|
||||
@alignment = options[:alignment] || nil
|
||||
@alignment = options[:alignment] || :left
|
||||
|
||||
@border = options[:border]
|
||||
@border = true if options[:border].nil?
|
||||
@@ -39,17 +39,6 @@ module CyberarmEngine
|
||||
@static = options[:static] || (options[:static].nil? || options[:static] == false ? false : true)
|
||||
|
||||
@textobject = check_cache(@size, @font)
|
||||
|
||||
if @alignment
|
||||
case @alignment
|
||||
when :left
|
||||
@x = 0 + BUTTON_PADDING
|
||||
when :center
|
||||
@x = (CyberarmEngine::Window.instance.width / 2) - (@textobject.text_width(@text) / 2)
|
||||
when :right
|
||||
@x = CyberarmEngine::Window.instance.width - BUTTON_PADDING - @textobject.text_width(@text)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def check_cache(size, font_name)
|
||||
@@ -113,6 +102,38 @@ module CyberarmEngine
|
||||
invalidate_cache! if old_color != color
|
||||
end
|
||||
|
||||
def alignment=(value)
|
||||
invalidate_cache! if @alignment != value
|
||||
@alignment = value
|
||||
end
|
||||
|
||||
def shadow=(value)
|
||||
invalidate_cache! if @shadow != value
|
||||
@shadow = value
|
||||
end
|
||||
|
||||
def shadow_color=(color)
|
||||
old_color = @shadow_color
|
||||
|
||||
if color
|
||||
@shadow_color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color)
|
||||
else
|
||||
raise "color cannot be nil"
|
||||
end
|
||||
|
||||
invalidate_cache! if old_color != color
|
||||
end
|
||||
|
||||
def shadow_alpha=(value)
|
||||
invalidate_cache! if @shadow_alpha != value
|
||||
@shadow_alpha = value
|
||||
end
|
||||
|
||||
def shadow_size=(value)
|
||||
invalidate_cache! if @shadow_size != value
|
||||
@shadow_size = value
|
||||
end
|
||||
|
||||
def border=(boolean)
|
||||
invalidate_cache! if @border != boolean
|
||||
@border = boolean
|
||||
@@ -128,9 +149,16 @@ module CyberarmEngine
|
||||
@border_alpha = n
|
||||
end
|
||||
|
||||
def border_color=(n)
|
||||
invalidate_cache! if @border_color != n
|
||||
@border_color = n
|
||||
def border_color=(color)
|
||||
old_color = @border_color
|
||||
|
||||
if color
|
||||
@border_color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color)
|
||||
else
|
||||
raise "color cannot be nil"
|
||||
end
|
||||
|
||||
invalidate_cache! if old_color != color
|
||||
end
|
||||
|
||||
def width(text = @text)
|
||||
@@ -180,23 +208,23 @@ module CyberarmEngine
|
||||
img = Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
|
||||
|
||||
@cached_text_border_image = Gosu.render((_width + (@border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do
|
||||
img.draw(-_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(-_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(-_x, 0, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
img.draw(-_x, -_y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
|
||||
img.draw(0, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(0, -_y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
img.draw(_x, -_y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
|
||||
img.draw(_x, 0, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(_x, 0, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
img.draw(_x, _y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
|
||||
img.draw(0, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(-_x, _y, @z, @factor_x, @factor_y, @border_color, @mode)
|
||||
img.draw(0, _y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
img.draw(-_x, _y, @z, @factor_x, @factor_y, Gosu::Color::WHITE, @mode)
|
||||
end
|
||||
end
|
||||
|
||||
@cached_text_shadow_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) if @shadow
|
||||
@cached_text_shadow_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font, align: @alignment) if @shadow
|
||||
|
||||
@gosu_cached_text_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font)
|
||||
@gosu_cached_text_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font, align: @alignment)
|
||||
|
||||
@cached_text_border_image.draw(@x, @y, @z, @factor_x, @factor_y, @border_color, @mode) if @border
|
||||
|
||||
@@ -228,7 +256,7 @@ module CyberarmEngine
|
||||
@textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode)
|
||||
end
|
||||
|
||||
@cached_text_border_image.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color)
|
||||
@cached_text_border_image.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color, @mode)
|
||||
end
|
||||
|
||||
if @shadow
|
||||
|
||||
@@ -62,32 +62,32 @@ module CyberarmEngine
|
||||
|
||||
def update
|
||||
# TOP
|
||||
@top.x = @element.x + @element.styled(:border_thickness_left)
|
||||
@top.y = @element.y
|
||||
@top.x = @element.x + @element.styled(:margin_left) + @element.styled(:border_thickness_left)
|
||||
@top.y = @element.y + @element.styled(:margin_top)
|
||||
@top.z = @element.z
|
||||
|
||||
@top.width = @element.width - @element.styled(:border_thickness_left)
|
||||
@top.height = @element.styled(:border_thickness_top)
|
||||
|
||||
# RIGHT
|
||||
@right.x = @element.x + @element.width
|
||||
@right.y = @element.y + @element.styled(:border_thickness_top)
|
||||
@right.x = @element.x + @element.styled(:margin_left) + @element.width
|
||||
@right.y = @element.y + @element.styled(:margin_top) + @element.styled(:border_thickness_top)
|
||||
@right.z = @element.z
|
||||
|
||||
@right.width = -@element.styled(:border_thickness_right)
|
||||
@right.height = @element.height - @element.styled(:border_thickness_top)
|
||||
|
||||
# BOTTOM
|
||||
@bottom.x = @element.x
|
||||
@bottom.y = @element.y + @element.height
|
||||
@bottom.x = @element.x + @element.styled(:margin_left)
|
||||
@bottom.y = @element.y + @element.styled(:margin_top) + @element.height
|
||||
@bottom.z = @element.z
|
||||
|
||||
@bottom.width = @element.width - @element.styled(:border_thickness_right)
|
||||
@bottom.height = -@element.styled(:border_thickness_bottom)
|
||||
|
||||
# LEFT
|
||||
@left.x = @element.x
|
||||
@left.y = @element.y
|
||||
@left.x = @element.x + @element.styled(:margin_left)
|
||||
@left.y = @element.y + @element.styled(:margin_top)
|
||||
@left.z = @element.z
|
||||
|
||||
@left.width = @element.styled(:border_thickness_left)
|
||||
|
||||
24
lib/cyberarm_engine/ui/dialog.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module CyberarmEngine
|
||||
class Dialog < CyberarmEngine::GuiState
|
||||
def draw
|
||||
previous_state&.draw
|
||||
|
||||
Gosu.flush
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
def update
|
||||
super
|
||||
|
||||
return unless window.current_state == self
|
||||
|
||||
window.states.reverse.each do |state|
|
||||
# Don't update ourselves, forever
|
||||
next if state == self && state.is_a?(CyberarmEngine::GuiState)
|
||||
|
||||
state.update
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,13 +1,13 @@
|
||||
module CyberarmEngine
|
||||
module DSL
|
||||
def every(milliseconds, &block)
|
||||
element_parent.gui_state.add_timer(
|
||||
element_parent.root.gui_state.add_timer(
|
||||
CyberarmEngine::Timer.new(milliseconds, true, &block)
|
||||
)
|
||||
end
|
||||
|
||||
def after(milliseconds, &block)
|
||||
element_parent.gui_state.add_timer(
|
||||
element_parent.root.gui_state.add_timer(
|
||||
CyberarmEngine::Timer.new(milliseconds, false, &block)
|
||||
)
|
||||
end
|
||||
@@ -20,6 +20,10 @@ module CyberarmEngine
|
||||
container(CyberarmEngine::Element::Stack, options, &block)
|
||||
end
|
||||
|
||||
def widget(options = {}, &block)
|
||||
container(CyberarmEngine::Element::Widget, options, &block)
|
||||
end
|
||||
|
||||
def menu(options = {}, &block)
|
||||
container(CyberarmEngine::Element::Menu, options, &block)
|
||||
end
|
||||
@@ -112,6 +116,18 @@ module CyberarmEngine
|
||||
add_element(Element::Slider.new(options, block))
|
||||
end
|
||||
|
||||
def page(klass, options = {})
|
||||
element_parent.root.gui_state.page(klass, options)
|
||||
end
|
||||
|
||||
def current_page
|
||||
element_parent.root.gui_state.current_page
|
||||
end
|
||||
|
||||
def dialog(klass, options = {})
|
||||
element_parent.root.gui_state.window.push_state(klass, options)
|
||||
end
|
||||
|
||||
def background(color = Gosu::Color::NONE)
|
||||
element_parent.style.background = color
|
||||
end
|
||||
|
||||
@@ -18,6 +18,7 @@ module CyberarmEngine
|
||||
@visible = !@options.key?(:visible) ? true : @options[:visible]
|
||||
@tip = @options[:tip] || ""
|
||||
|
||||
@debug = @options[:debug].nil? ? nil : @options[:debug]
|
||||
@debug_color = @options[:debug_color].nil? ? Gosu::Color::RED : @options[:debug_color]
|
||||
|
||||
@style = Style.new(options)
|
||||
@@ -35,7 +36,7 @@ module CyberarmEngine
|
||||
@width = 0
|
||||
@height = 0
|
||||
|
||||
@style.width = default(:width) || nil
|
||||
@style.width = default(:width) || nil
|
||||
@style.height = default(:height) || nil
|
||||
|
||||
@background_canvas = nil # Background.new
|
||||
@@ -43,7 +44,7 @@ module CyberarmEngine
|
||||
@background_image_canvas = nil # BackgroundImage.new
|
||||
@border_canvas = nil # BorderCanvas.new(element: self)
|
||||
|
||||
@style_event = :default
|
||||
@style_event = enabled? ? :default : :disabled
|
||||
|
||||
stylize
|
||||
|
||||
@@ -52,7 +53,15 @@ module CyberarmEngine
|
||||
root.gui_state.request_focus(self) if @options[:autofocus]
|
||||
end
|
||||
|
||||
def resolution_scaled(n)
|
||||
return n unless @style.design_width
|
||||
|
||||
((Gosu.screen_width(window).to_f / @style.design_width) * n).floor
|
||||
end
|
||||
|
||||
def stylize
|
||||
@style_event = :disabled unless enabled?
|
||||
|
||||
set_static_position
|
||||
|
||||
set_color
|
||||
@@ -72,11 +81,11 @@ module CyberarmEngine
|
||||
when :border_color_bottom, :border_color_left, :border_color_right, :border_color_top
|
||||
safe_style_fetch(key, :border_color)
|
||||
when :border_thickness_bottom, :border_thickness_left, :border_thickness_right, :border_thickness_top
|
||||
safe_style_fetch(key, :border_thickness)
|
||||
visible? ? safe_style_fetch(key, :border_thickness) : 0
|
||||
when :margin_bottom, :margin_left, :margin_right, :margin_top
|
||||
safe_style_fetch(key, :margin)
|
||||
visible? ? safe_style_fetch(key, :margin) : 0
|
||||
when :padding_bottom, :padding_left, :padding_right, :padding_top
|
||||
safe_style_fetch(key, :padding)
|
||||
visible? ? safe_style_fetch(key, :padding) : 0
|
||||
else
|
||||
safe_style_fetch(key)
|
||||
end
|
||||
@@ -107,7 +116,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def set_font
|
||||
@text&.swap_font(safe_style_fetch(:text_size), safe_style_fetch(:font))
|
||||
@text&.swap_font(resolution_scaled(safe_style_fetch(:text_size)), safe_style_fetch(:font))
|
||||
end
|
||||
|
||||
def set_background
|
||||
@@ -124,8 +133,8 @@ module CyberarmEngine
|
||||
|
||||
@background_nine_slice_canvas.image = img
|
||||
|
||||
@background_nine_slice_canvas.x = @x
|
||||
@background_nine_slice_canvas.y = @y
|
||||
@background_nine_slice_canvas.x = @x + styled(:margin_left)
|
||||
@background_nine_slice_canvas.y = @y + styled(:margin_top)
|
||||
@background_nine_slice_canvas.z = @z
|
||||
@background_nine_slice_canvas.width = width
|
||||
@background_nine_slice_canvas.height = height
|
||||
@@ -134,10 +143,12 @@ module CyberarmEngine
|
||||
|
||||
@background_nine_slice_canvas.color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
|
||||
|
||||
@background_nine_slice_canvas.left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.bottom = safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.set_edges(
|
||||
left: safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge) || 1,
|
||||
top: safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge) || 1,
|
||||
right: safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge) || 1,
|
||||
bottom: safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge) || 1
|
||||
)
|
||||
end
|
||||
|
||||
def set_background_image
|
||||
@@ -204,6 +215,7 @@ module CyberarmEngine
|
||||
event(:blur)
|
||||
|
||||
event(:changed)
|
||||
event(:size_changed)
|
||||
end
|
||||
|
||||
def enter(_sender)
|
||||
@@ -211,7 +223,7 @@ module CyberarmEngine
|
||||
|
||||
if !@enabled
|
||||
update_styles(:disabled)
|
||||
elsif @focus
|
||||
elsif @focus && !@style.active.empty?
|
||||
update_styles(:active)
|
||||
else
|
||||
update_styles(:hover)
|
||||
@@ -226,7 +238,7 @@ module CyberarmEngine
|
||||
unless @enabled
|
||||
update_styles(:disabled)
|
||||
else
|
||||
update_styles(:active)
|
||||
update_styles(:active) unless @style.active.empty?
|
||||
end
|
||||
|
||||
window.current_state.focus = self
|
||||
@@ -272,6 +284,7 @@ module CyberarmEngine
|
||||
root.gui_state.request_repaint if @enabled != boolean
|
||||
|
||||
@enabled = boolean
|
||||
@style_event = :default if boolean && @style_event == :disabled
|
||||
|
||||
recalculate
|
||||
|
||||
@@ -327,6 +340,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
return if @debug == false || !CyberarmEngine::GUI_DEBUG # allow elements to opt out of debug drawing, makes debugging some things easier.
|
||||
return if CyberarmEngine.const_defined?("GUI_DEBUG_ONLY_ELEMENT") && self.class == GUI_DEBUG_ONLY_ELEMENT
|
||||
|
||||
Gosu.draw_line(
|
||||
@@ -374,13 +388,13 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def hit?(x, y)
|
||||
x.between?(@x, @x + width) &&
|
||||
y.between?(@y, @y + height)
|
||||
x.between?(@x + styled(:margin_left), @x + styled(:margin_left) + width) &&
|
||||
y.between?(@y + styled(:margin_top), @y + styled(:margin_top) + height)
|
||||
end
|
||||
|
||||
def width
|
||||
if visible?
|
||||
inner_width + @width
|
||||
inner_width + content_width
|
||||
else
|
||||
0
|
||||
end
|
||||
@@ -404,7 +418,7 @@ module CyberarmEngine
|
||||
|
||||
def height
|
||||
if visible?
|
||||
inner_height + @height
|
||||
inner_height + content_height
|
||||
else
|
||||
0
|
||||
end
|
||||
@@ -455,9 +469,9 @@ module CyberarmEngine
|
||||
|
||||
pairs_ << a_ unless pairs_.last == a_
|
||||
|
||||
@cached_scroll_height = pairs_.sum { |pair| + styled(:padding_top) + styled(:border_thickness_top) + pair.map(&:outer_height).max } + styled(:padding_bottom) + styled(:border_thickness_bottom)
|
||||
@cached_scroll_height = pairs_.sum { |pair| pair.map(&:outer_height).max } + noncontent_height
|
||||
else
|
||||
@cached_scroll_height = styled(:padding_top) + styled(:border_thickness_top) + @children.sum(&:outer_height) + styled(:padding_bottom) + styled(:border_thickness_bottom)
|
||||
@cached_scroll_height = noncontent_height + @children.sum(&:outer_height)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -474,6 +488,8 @@ module CyberarmEngine
|
||||
|
||||
new_size = if size.is_a?(Float) && size.between?(0.0, 1.0)
|
||||
(@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
|
||||
elsif size.is_a?(Numeric)
|
||||
(size - send(:"noncontent_#{dimension}")).floor
|
||||
else
|
||||
size
|
||||
end
|
||||
@@ -502,7 +518,7 @@ module CyberarmEngine
|
||||
|
||||
def space_available_height
|
||||
# TODO: This may get expensive if there are a lot of children, probably should cache it somehow
|
||||
fill_siblings = @parent.children.select { |c| c.styled(:fill) }.count.to_f # include self since we're dividing
|
||||
fill_siblings = @parent.children.select { |c| c.styled(:fill) && c.visible? }.count.to_f # include self since we're dividing
|
||||
|
||||
available_space = ((@parent.content_height - (@parent.children.reject { |c| c.styled(:fill) }).map(&:outer_height).sum) / fill_siblings)
|
||||
(available_space.nan? || available_space.infinite?) ? 0 : available_space.floor # The parent element might not have its dimensions, yet.
|
||||
@@ -516,8 +532,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def update_background
|
||||
@background_canvas&.x = @x
|
||||
@background_canvas&.y = @y
|
||||
@background_canvas&.x = @x + styled(:margin_left)
|
||||
@background_canvas&.y = @y + styled(:margin_top)
|
||||
@background_canvas&.z = @z
|
||||
@background_canvas&.width = width
|
||||
@background_canvas&.height = height
|
||||
@@ -545,8 +561,8 @@ module CyberarmEngine
|
||||
def update_background_image
|
||||
return unless @background_image_canvas
|
||||
|
||||
@background_image_canvas.x = @x
|
||||
@background_image_canvas.y = @y
|
||||
@background_image_canvas.x = @x + styled(:margin_left)
|
||||
@background_image_canvas.y = @y + styled(:margin_top)
|
||||
@background_image_canvas.z = @z
|
||||
@background_image_canvas.width = width
|
||||
@background_image_canvas.height = height
|
||||
@@ -563,6 +579,8 @@ module CyberarmEngine
|
||||
|
||||
@old_width = width
|
||||
@old_height = height
|
||||
|
||||
publish(:size_changed)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -618,7 +636,7 @@ module CyberarmEngine
|
||||
root.gui_state.request_recalculate if @parent && !is_a?(ToolTip) && (width != old_width || height != old_height)
|
||||
root.gui_state.request_repaint if width != old_width || height != old_height
|
||||
|
||||
root.gui_state.menu.recalculate if root.gui_state.menu && root.gui_state.menu.parent == self
|
||||
root.gui_state.active_menu.recalculate if root.gui_state.active_menu && root.gui_state.active_menu.parent == self
|
||||
end
|
||||
|
||||
def layout
|
||||
|
||||
@@ -23,8 +23,8 @@ module CyberarmEngine
|
||||
|
||||
def draw_image
|
||||
@image.draw(
|
||||
styled(:border_thickness_left) + styled(:padding_left) + @x,
|
||||
styled(:border_thickness_top) + styled(:padding_top) + @y,
|
||||
styled(:border_thickness_left) + styled(:margin_left) + styled(:padding_left) + @x,
|
||||
styled(:border_thickness_top) + styled(:margin_top) + styled(:padding_top) + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y, @text.color
|
||||
)
|
||||
@@ -35,13 +35,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def layout
|
||||
unless @enabled
|
||||
@background_canvas.background = @style.disabled.background
|
||||
@text.color = @style.disabled.color
|
||||
else
|
||||
@background_canvas.background = styled(:background)
|
||||
@text.color = styled(:color)
|
||||
end
|
||||
@background_canvas.background = styled(:background)
|
||||
@text.color = styled(:color)
|
||||
|
||||
if @image
|
||||
@width = 0
|
||||
@@ -78,6 +73,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(value)
|
||||
old_value = self.value
|
||||
|
||||
if value.is_a?(Gosu::Image)
|
||||
@image = value
|
||||
else
|
||||
@@ -93,7 +90,9 @@ module CyberarmEngine
|
||||
recalculate
|
||||
end
|
||||
|
||||
publish(:changed, self.value)
|
||||
publish(:changed, self.value) if value != old_value
|
||||
|
||||
self.value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -46,8 +46,12 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(bool)
|
||||
old_value = self.value
|
||||
|
||||
@toggle_button.value = bool
|
||||
publish(:changed, @toggle_button.value)
|
||||
publish(:changed, bool) if bool != old_value
|
||||
|
||||
bool
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,21 +80,28 @@ module CyberarmEngine
|
||||
|
||||
def render
|
||||
Gosu.clip_to(
|
||||
@x + styled(:border_thickness_left) + styled(:padding_left),
|
||||
@y + styled(:border_thickness_top) + styled(:padding_top),
|
||||
@x - 1 + styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left),
|
||||
@y - 1 + styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top),
|
||||
content_width + 1,
|
||||
content_height + 1
|
||||
) do
|
||||
|
||||
Gosu.translate(@scroll_position.x, @scroll_position.y) do
|
||||
@children.each(&:draw)
|
||||
|
||||
@children.each do |child|
|
||||
next unless child.visible?
|
||||
|
||||
child.debug_draw
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
debug_draw
|
||||
end
|
||||
|
||||
def debug_draw
|
||||
super
|
||||
|
||||
@children.each(&:debug_draw)
|
||||
end
|
||||
|
||||
def update
|
||||
@@ -102,9 +109,12 @@ module CyberarmEngine
|
||||
@children.each(&:update)
|
||||
end
|
||||
|
||||
def hit_element?(x, y)
|
||||
# return nil if element was not hit, or array of hit elements if hit, includes self.
|
||||
def hit_element?(x, y, elements = [])
|
||||
return unless hit?(x, y)
|
||||
|
||||
elements << self
|
||||
|
||||
# Offset child hit point by scroll position/offset
|
||||
child_x = x - @scroll_position.x
|
||||
child_y = y - @scroll_position.y
|
||||
@@ -114,20 +124,23 @@ module CyberarmEngine
|
||||
|
||||
case child
|
||||
when Container
|
||||
if (element = child.hit_element?(child_x, child_y))
|
||||
return element
|
||||
if (child.hit_element?(child_x, child_y, elements))
|
||||
return elements
|
||||
end
|
||||
else
|
||||
return child if child.hit?(child_x, child_y)
|
||||
if child.hit?(child_x, child_y)
|
||||
elements << child
|
||||
return elements
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self if hit?(x, y)
|
||||
elements
|
||||
end
|
||||
|
||||
def update_child_element_visibity(child)
|
||||
child.element_visible = child.x >= (@x - @scroll_position.x) - child.width && child.x <= (@x - @scroll_position.x) + width &&
|
||||
child.y >= (@y - @scroll_position.y) - child.height && child.y <= (@y - @scroll_position.y) + height
|
||||
child.element_visible = child.x >= ((styled(:margin_left) + @x) - @scroll_position.x) - child.width && child.x <= ((styled(:margin_left) + @x) - @scroll_position.x) + width &&
|
||||
child.y >= ((styled(:margin_top) + @y) - @scroll_position.y) - child.height && child.y <= ((styled(:margin_top) + @y) - @scroll_position.y) + height
|
||||
end
|
||||
|
||||
def update_scroll
|
||||
@@ -182,8 +195,8 @@ module CyberarmEngine
|
||||
_width = dimensional_size(@style.width, :width)
|
||||
_height = dimensional_size(@style.height, :height)
|
||||
|
||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
|
||||
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
|
||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max.to_f - (styled(:margin_left) + styled(:padding_left))).floor
|
||||
@height = _height || (@children.map { |c| c.y + c.outer_height }.max.to_f - (styled(:margin_top) + styled(:padding_top))).floor
|
||||
end
|
||||
|
||||
# FIXME: Correctly handle alignment when element has siblings
|
||||
@@ -193,9 +206,9 @@ module CyberarmEngine
|
||||
|
||||
case styled(:v_align)
|
||||
when :center
|
||||
@y = parent.height / 2 - height / 2
|
||||
@y = parent.y + parent.outer_height / 2 - outer_height / 2
|
||||
when :bottom
|
||||
@y = parent.height - height
|
||||
@y = parent.y + parent.outer_height - outer_height
|
||||
end
|
||||
end
|
||||
|
||||
@@ -204,17 +217,17 @@ module CyberarmEngine
|
||||
|
||||
case styled(:h_align)
|
||||
when :center
|
||||
@x = parent.width / 2 - width / 2
|
||||
@x = parent.x + parent.outer_width / 2 - outer_width / 2
|
||||
when :right
|
||||
@x = parent.width - width
|
||||
@x = parent.x + parent.outer_width - outer_width
|
||||
end
|
||||
end
|
||||
|
||||
# t = Gosu.milliseconds
|
||||
# Move children to parent after positioning
|
||||
@children.each do |child|
|
||||
child.x += (@x + styled(:border_thickness_left)) - styled(:margin_left)
|
||||
child.y += (@y + styled(:border_thickness_top)) - styled(:margin_top)
|
||||
child.x += (@x + styled(:border_thickness_left))
|
||||
child.y += (@y + styled(:border_thickness_top))
|
||||
|
||||
child.stylize
|
||||
child.recalculate
|
||||
@@ -258,8 +271,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def position_on_current_line(element) # Flow
|
||||
element.x = element.styled(:margin_left) + @current_position.x
|
||||
element.y = element.styled(:margin_top) + @current_position.y
|
||||
element.x = @current_position.x
|
||||
element.y = @current_position.y
|
||||
|
||||
@current_position.x += element.outer_width
|
||||
end
|
||||
@@ -278,15 +291,15 @@ module CyberarmEngine
|
||||
@current_position.x = styled(:margin_left) + styled(:padding_left)
|
||||
@current_position.y += tallest_neighbor(element, @current_position.y).outer_height
|
||||
|
||||
element.x = element.styled(:margin_left) + @current_position.x
|
||||
element.y = element.styled(:margin_top) + @current_position.y
|
||||
element.x = @current_position.x
|
||||
element.y = @current_position.y
|
||||
|
||||
@current_position.x += element.outer_width
|
||||
end
|
||||
|
||||
def move_to_next_line(element) # Stack
|
||||
element.x = element.styled(:margin_left) + @current_position.x
|
||||
element.y = element.styled(:margin_top) + @current_position.y
|
||||
element.x = @current_position.x
|
||||
element.y = @current_position.y
|
||||
|
||||
@current_position.y += element.outer_height
|
||||
end
|
||||
|
||||
@@ -45,19 +45,23 @@ module CyberarmEngine
|
||||
|
||||
def draw_selection
|
||||
selection_width = caret_position - selection_start_position
|
||||
selection_start_line = calculate_line(@text_input.caret_pos)
|
||||
selection_end_line = calculate_line(@text_input.selection_start)
|
||||
|
||||
Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.textobject.height,
|
||||
# pp [selection_width, selection_start_line, selection_end_line]
|
||||
|
||||
Gosu.draw_rect(selection_start_position, @text.y + selection_start_line * @text.textobject.height, 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
|
||||
def text_input_position_for(method)
|
||||
line = @text_input.text[0...@text_input.send(method)].lines.last
|
||||
_x = @text.x + @offset_x
|
||||
|
||||
if @type == :password
|
||||
_x + @text.width(default(:password_character) * line.length)
|
||||
_x + @text.width(default(:password_character) * line.length) - @style.border_thickness_left
|
||||
else
|
||||
_x + @text.width(line)
|
||||
_x + @text.width(line) - @style.border_thickness_left
|
||||
end
|
||||
end
|
||||
|
||||
@@ -66,9 +70,12 @@ module CyberarmEngine
|
||||
@text_input.selection_start = @text_input.caret_pos = int
|
||||
end
|
||||
|
||||
def calculate_line(caret_pos = @text_input.caret_pos)
|
||||
sub_text = @text_input.text[0...caret_pos].lines.size - 1
|
||||
end
|
||||
|
||||
def calculate_active_line
|
||||
sub_text = @text_input.text[0...@text_input.caret_pos]
|
||||
@active_line = sub_text.lines.size - 1
|
||||
@active_line = calculate_line(@text_input.caret_pos)
|
||||
end
|
||||
|
||||
def caret_stay_left_of_last_newline
|
||||
@@ -92,7 +99,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
active_line = row_at(mouse_y - y_scroll_offset)
|
||||
right_offset = column_at(mouse_x, mouse_y)
|
||||
right_offset = column_at(mouse_x, mouse_y - y_scroll_offset)
|
||||
|
||||
buffer = @text_input.text.lines[0..active_line].join if active_line != 0
|
||||
buffer = @text_input.text.lines.first if active_line == 0
|
||||
@@ -176,6 +183,8 @@ module CyberarmEngine
|
||||
pos = text.length
|
||||
end
|
||||
|
||||
pp [direction, pos, @active_line]
|
||||
|
||||
set_position(pos)
|
||||
end
|
||||
|
||||
|
||||
@@ -291,6 +291,8 @@ module CyberarmEngine
|
||||
|
||||
def value=(string)
|
||||
@text_input.text = string
|
||||
|
||||
string
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,8 +14,8 @@ module CyberarmEngine
|
||||
|
||||
def render
|
||||
@image.draw(
|
||||
styled(:border_thickness_left) + styled(:padding_left) + @x,
|
||||
styled(:border_thickness_top) + styled(:padding_top) + @y,
|
||||
styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left) + @x,
|
||||
styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top) + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y, styled(:color)
|
||||
)
|
||||
@@ -56,12 +56,18 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(path_or_image, retro: false, tileable: false)
|
||||
old_value = self.value
|
||||
|
||||
@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
|
||||
|
||||
publish(:changed, @image) if old_value != self.value
|
||||
|
||||
self.value
|
||||
end
|
||||
|
||||
def path
|
||||
|
||||
@@ -56,7 +56,7 @@ module CyberarmEngine
|
||||
def show_menu
|
||||
@menu.clear do
|
||||
|
||||
@menu.style.width = width
|
||||
@menu.style.width = width - (@menu.styled(:border_thickness_left) + @menu.styled(:border_thickness_right))
|
||||
|
||||
@items.each do |item|
|
||||
# prevent already selected item from appearing in list
|
||||
|
||||
@@ -11,10 +11,10 @@ module CyberarmEngine
|
||||
# FIXME: properly find scrollable parent, if any.
|
||||
parent_scroll_top = parent&.parent ? parent.parent.scroll_top : 0
|
||||
|
||||
@x = @parent.x
|
||||
@y = parent_scroll_top + @parent.y + @parent.height
|
||||
@x = @parent.x + @parent.styled(:border_thickness_left)
|
||||
@y = parent_scroll_top + @parent.y + @parent.height + @parent.styled(:border_thickness_top)
|
||||
|
||||
@y = (parent_scroll_top + @parent.y) - height if @y + height > window.height
|
||||
@y = (parent_scroll_top + @parent.y + @parent.styled(:border_thickness_top)) - height if @y + height > window.height
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
@@ -83,9 +83,11 @@ module CyberarmEngine
|
||||
@fraction = decimal.clamp(0.0, 1.0)
|
||||
update_background
|
||||
|
||||
root.gui_state.request_repaint if @fraction != old_value
|
||||
if @fraction != old_value
|
||||
root.gui_state.request_repaint
|
||||
publish(:changed, @fraction)
|
||||
end
|
||||
|
||||
publish(:changed, @fraction)
|
||||
@fraction
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,10 +62,10 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def position_handle
|
||||
@handle.x = @x + @handle.styled(:margin_left) + styled(:padding_left) + styled(:border_thickness_left) +
|
||||
@handle.x = styled(:margin_left) + @x + @handle.styled(:margin_left) + styled(:padding_left) + styled(:border_thickness_left) +
|
||||
((content_width - @handle.outer_width) * (@value - @range.min) / (@range.max - @range.min).to_f)
|
||||
|
||||
@handle.y = @y + @handle.styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top)
|
||||
@handle.y = styled(:margin_top) + @y + @handle.styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top)
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -94,13 +94,17 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(n)
|
||||
old_value = self.value
|
||||
|
||||
@value = n
|
||||
position_handle
|
||||
@handle.recalculate
|
||||
|
||||
root.gui_state.request_repaint
|
||||
|
||||
publish(:changed, @value)
|
||||
publish(:changed, @value) if old_value != n
|
||||
|
||||
n
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ module CyberarmEngine
|
||||
text, font: @options[:font], z: @z, color: @options[:color],
|
||||
size: @options[:text_size], shadow: @options[:text_shadow],
|
||||
static: @options[:text_static],
|
||||
alignment: @options[:text_align],
|
||||
shadow_size: @options[:text_shadow_size],
|
||||
shadow_color: @options[:text_shadow_color],
|
||||
border: @options[:text_border],
|
||||
@@ -41,11 +42,18 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def layout
|
||||
unless @enabled
|
||||
@text.color = @style.disabled.color
|
||||
else
|
||||
@text.color = styled(:color)
|
||||
end
|
||||
@text.color = styled(:color)
|
||||
@text.alignment = styled(:text_align)
|
||||
|
||||
@text.shadow = styled(:text_shadow)
|
||||
@text.shadow_color = styled(:text_shadow_color)
|
||||
@text.shadow_alpha = styled(:text_shadow_alpha)
|
||||
@text.shadow_size = styled(:text_shadow_size)
|
||||
|
||||
@text.border = styled(:text_border)
|
||||
@text.border_color = styled(:text_border_color)
|
||||
@text.border_alpha = styled(:text_border_alpha)
|
||||
@text.border_size = styled(:text_border_size)
|
||||
|
||||
@width = 0
|
||||
@height = 0
|
||||
@@ -62,21 +70,21 @@ module CyberarmEngine
|
||||
@width = _width || @text_width.floor
|
||||
@height = _height || @text_height.floor
|
||||
|
||||
@text.y = styled(:border_thickness_top) + styled(:padding_top) + @y
|
||||
@text.y = styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top) + @y
|
||||
@text.z = @z + 3
|
||||
|
||||
if (text_alignment = @options[:text_align] || @options[:text_h_align])
|
||||
case text_alignment
|
||||
when :left
|
||||
@text.x = styled(:border_thickness_left) + styled(:padding_left) + @x
|
||||
@text.x = styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left) + @x
|
||||
when :center
|
||||
@text.x = if @text_width <= width
|
||||
@x + width / 2 - @text_width / 2
|
||||
@x + styled(:margin_left) + width / 2 - @text_width / 2
|
||||
else # Act as left aligned
|
||||
styled(:border_thickness_left) + styled(:padding_left) + @x
|
||||
styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left) + @x
|
||||
end
|
||||
when :right
|
||||
@text.x = @x + outer_width - (@text_width + styled(:border_thickness_right) + styled(:padding_right))
|
||||
@text.x = @x + styled(:margin_left) + outer_width - (@text_width + styled(:border_thickness_right) + styled(:padding_right))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -84,12 +92,12 @@ module CyberarmEngine
|
||||
case vertical_alignment
|
||||
when :center
|
||||
@text.y = if @text_height <= height
|
||||
@y + height / 2 - @text_height / 2
|
||||
@y + styled(:margin_top) + height / 2 - @text_height / 2
|
||||
else
|
||||
styled(:border_thickness_top) + styled(:padding_top) + @y
|
||||
styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top) + @y
|
||||
end
|
||||
when :bottom
|
||||
@text.y = @y + outer_height - (@text_height + styled(:border_thickness_bottom) + styled(:padding_bottom))
|
||||
@text.y = @y + styled(:margin_top) + outer_height - (@text_height + styled(:border_thickness_bottom) + styled(:padding_bottom))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -186,9 +194,12 @@ module CyberarmEngine
|
||||
recalculate
|
||||
end
|
||||
|
||||
root.gui_state.request_repaint if old_value != @raw_text
|
||||
if old_value != @raw_text
|
||||
root.gui_state.request_repaint
|
||||
publish(:changed, @raw_text)
|
||||
end
|
||||
|
||||
publish(:changed, self.value)
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
@@ -214,6 +225,9 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
class ToolTip < TextBlock
|
||||
def needs_repaint?
|
||||
@needs_repaint || !value.to_s.empty?
|
||||
end
|
||||
end
|
||||
|
||||
class Link < TextBlock
|
||||
|
||||
@@ -50,6 +50,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
def value=(boolean)
|
||||
old_value = self.value
|
||||
|
||||
@value = boolean
|
||||
|
||||
if boolean
|
||||
@@ -60,7 +62,9 @@ module CyberarmEngine
|
||||
|
||||
recalculate
|
||||
|
||||
publish(:changed, @value)
|
||||
publish(:changed, @value) if old_value != boolean
|
||||
|
||||
boolean
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
45
lib/cyberarm_engine/ui/elements/widget.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module CyberarmEngine
|
||||
class Element
|
||||
# Special container that has a layout like a Flow
|
||||
# and makes all its children mirror its styling (i.e. hover, active, disabled...)
|
||||
class Widget < Flow
|
||||
def update_styles(style = :default)
|
||||
super
|
||||
|
||||
@children.each do |child|
|
||||
recursive_styles(style, self)
|
||||
end
|
||||
end
|
||||
|
||||
# Make child elements mirror the widgets styling
|
||||
# disabled elements will not have their styling overridden
|
||||
def recursive_styles(style, container)
|
||||
container.children.each do |child|
|
||||
child.update_styles(style)
|
||||
|
||||
recursive_styles(style, child) if child.is_a?(Container)
|
||||
end
|
||||
end
|
||||
|
||||
# Enable child elements to display their tooltips
|
||||
# but fall back to the Widget if no hit element has a tip
|
||||
def tip
|
||||
parent_scroll_position = CyberarmEngine::Vector.new
|
||||
|
||||
element = self
|
||||
while (parent_element = element.parent)
|
||||
parent_scroll_position += parent_element.scroll_position
|
||||
|
||||
element = parent_element
|
||||
end
|
||||
|
||||
elements = hit_element?(window.mouse_x - parent_scroll_position.x, window.mouse_y - parent_scroll_position.y)
|
||||
|
||||
return @tip unless elements
|
||||
|
||||
elements.delete(self) # prevent infinite recursive loop (Widget#tip)
|
||||
elements.reverse.find { |e| !e.tip.empty? }&.tip || @tip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -13,7 +13,7 @@ module CyberarmEngine
|
||||
def publish(event, *args)
|
||||
raise ArgumentError, "#{self.class} does not handle #{event.inspect}" unless @event_handler.include?(event)
|
||||
|
||||
return unless enabled?
|
||||
return if !enabled? && event != :changed
|
||||
|
||||
was_handled = false
|
||||
|
||||
|
||||
@@ -18,6 +18,10 @@ module CyberarmEngine
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
|
||||
@pages = {}
|
||||
@page_host = nil
|
||||
@page = nil
|
||||
|
||||
@menu = nil
|
||||
@focus = nil
|
||||
@mouse_over = nil
|
||||
@@ -27,6 +31,7 @@ module CyberarmEngine
|
||||
@dragging_element = nil
|
||||
@pending_recalculate_request = false
|
||||
@pending_element_recalculate_requests = []
|
||||
@hit_elements = []
|
||||
|
||||
@needs_repaint = false
|
||||
|
||||
@@ -39,14 +44,45 @@ module CyberarmEngine
|
||||
@tip = Element::ToolTip.new("", parent: @root_container, z: Float::INFINITY, theme: current_theme)
|
||||
end
|
||||
|
||||
def menu
|
||||
def active_menu
|
||||
@menu
|
||||
end
|
||||
|
||||
def page(klass, options = {})
|
||||
@page&.blur
|
||||
|
||||
@pages[klass] = klass.new(parent: options[:parent] || self) unless @pages[klass]
|
||||
@page = @pages[klass]
|
||||
|
||||
@page.options = options
|
||||
page_host.clear do
|
||||
@page.setup
|
||||
end
|
||||
@page.focus
|
||||
end
|
||||
|
||||
def current_page
|
||||
@page
|
||||
end
|
||||
|
||||
def page_host
|
||||
return @page.parent if @page.parent.is_a?(CyberarmEngine::Element::Container)
|
||||
return @page_host if @page_host && @page_host.is_a?(CyberarmEngine::Element::Container)
|
||||
|
||||
@root_container
|
||||
end
|
||||
|
||||
def page_host=(container)
|
||||
raise "page host must be a CyberarmEngine::Element::Container" unless container.is_a?(Container)
|
||||
|
||||
@page_host = container
|
||||
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 && element && @focus != element
|
||||
@hit_elements.delete(@focus)
|
||||
@focus = element
|
||||
end
|
||||
|
||||
@@ -62,7 +98,15 @@ module CyberarmEngine
|
||||
Stats.frame.start_timing(:gui_element_recalculate_requests)
|
||||
|
||||
# puts "PENDING REQUESTS: #{@pending_element_recalculate_requests.size}" if @pending_element_recalculate_requests.size.positive?
|
||||
@pending_element_recalculate_requests.shift(&:recalculate)
|
||||
while (e = @pending_element_recalculate_requests.shift)
|
||||
# its pointless to recalculate individual elements when we've been asked to recalculate the whole gui
|
||||
if @pending_recalculate_request
|
||||
@pending_element_recalculate_requests.clear
|
||||
break
|
||||
end
|
||||
|
||||
e.recalculate
|
||||
end
|
||||
|
||||
Stats.frame.end_timing(:gui_element_recalculate_requests)
|
||||
|
||||
@@ -98,6 +142,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
@needs_repaint = false
|
||||
|
||||
@page&.draw
|
||||
end
|
||||
|
||||
def needs_repaint?
|
||||
@@ -127,22 +173,37 @@ module CyberarmEngine
|
||||
return unless window.has_focus?
|
||||
return unless window.current_state == self
|
||||
|
||||
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)
|
||||
# list of containers decending down to hit element
|
||||
new_hit_elements = (@menu || @root_container).hit_element?(window.mouse_x, window.mouse_y)
|
||||
# element the mouse is over, if any.
|
||||
new_mouse_over = new_hit_elements&.find { |c| c.is_a?(CyberarmEngine::Element::Widget) } || new_hit_elements&.last
|
||||
|
||||
if new_mouse_over
|
||||
new_mouse_over.publish(:enter) if new_mouse_over != @mouse_over
|
||||
new_mouse_over.publish(:hover)
|
||||
# puts "#{new_mouse_over.class}[#{new_mouse_over.value}]: #{new_mouse_over.x}:#{new_mouse_over.y} #{new_mouse_over.width}:#{new_mouse_over.height}" if new_mouse_over != @mouse_over
|
||||
# is the currently hit element the same as last hit element?
|
||||
same_element = @mouse_over && new_mouse_over == @mouse_over
|
||||
|
||||
unless @hit_elements == new_hit_elements
|
||||
added_hit_elements = (new_hit_elements || []) - @hit_elements
|
||||
removed_hit_elements = @hit_elements - (new_hit_elements || [])
|
||||
|
||||
added_hit_elements.each do |e|
|
||||
e.publish(:enter)
|
||||
e.publish(:hover)
|
||||
end
|
||||
|
||||
removed_hit_elements.each do |e|
|
||||
e.publish(:leave)
|
||||
end
|
||||
end
|
||||
@mouse_over.publish(:leave) if @mouse_over && new_mouse_over != @mouse_over
|
||||
|
||||
@mouse_over = new_mouse_over
|
||||
@hit_elements = new_hit_elements || []
|
||||
|
||||
redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MS_LEFT)
|
||||
redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MS_MIDDLE)
|
||||
redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MS_RIGHT)
|
||||
|
||||
if Vector.new(window.mouse_x, window.mouse_y) == @last_mouse_pos
|
||||
# handle tooltip
|
||||
if same_element
|
||||
if @mouse_over && (Gosu.milliseconds - @mouse_moved_at) > tool_tip_delay
|
||||
@tip.value = @mouse_over.tip if @mouse_over
|
||||
@tip.x = window.mouse_x
|
||||
@@ -162,6 +223,8 @@ module CyberarmEngine
|
||||
|
||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||
@mouse_pos = @last_mouse_pos.clone
|
||||
|
||||
@page&.update
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
@@ -176,9 +239,14 @@ module CyberarmEngine
|
||||
redirect_mouse_button(:right)
|
||||
when Gosu::KB_F5
|
||||
request_recalculate
|
||||
when Gosu::KB_F12
|
||||
CyberarmEngine.const_set(:GUI_DEBUG, !CyberarmEngine::GUI_DEBUG)
|
||||
request_repaint
|
||||
end
|
||||
|
||||
@focus.button_down(id) if @focus.respond_to?(:button_down)
|
||||
|
||||
@page&.button_down(id)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
@@ -209,6 +277,8 @@ module CyberarmEngine
|
||||
|
||||
# Prevents menu from popping back up if the listbox is clicked to hide it.
|
||||
@hid_menu_for = nil
|
||||
|
||||
@page&.button_up(id)
|
||||
end
|
||||
|
||||
def tool_tip_delay
|
||||
@@ -219,6 +289,7 @@ module CyberarmEngine
|
||||
hide_menu unless @menu && (@menu == @mouse_over) || (@mouse_over&.parent == @menu)
|
||||
|
||||
if @focus && @mouse_over != @focus
|
||||
@hit_elements.delete(@focus)
|
||||
@focus.publish(:blur)
|
||||
@focus = nil
|
||||
end
|
||||
|
||||
39
lib/cyberarm_engine/ui/page.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module CyberarmEngine
|
||||
class Page
|
||||
include CyberarmEngine::DSL
|
||||
include CyberarmEngine::Common
|
||||
|
||||
attr_reader :parent
|
||||
|
||||
def initialize(parent:)
|
||||
@parent = parent
|
||||
|
||||
@options = {}
|
||||
end
|
||||
|
||||
def options=(options)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def setup
|
||||
end
|
||||
|
||||
def focus
|
||||
end
|
||||
|
||||
def blur
|
||||
end
|
||||
|
||||
def draw
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -23,7 +23,7 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
%i[
|
||||
x y z width height min_width min_height max_width max_height color background
|
||||
x y z design_width width height min_width min_height max_width max_height color background
|
||||
background_image background_image_mode background_image_color
|
||||
background_nine_slice background_nine_slice_mode background_nine_slice_color background_nine_slice_from_edge
|
||||
background_nine_slice_left background_nine_slice_top background_nine_slice_right background_nine_slice_bottom
|
||||
@@ -31,8 +31,12 @@ module CyberarmEngine
|
||||
border_thickness border_thickness_left border_thickness_right border_thickness_top border_thickness_bottom
|
||||
padding padding_left padding_right padding_top padding_bottom
|
||||
margin margin_left margin_right margin_top margin_bottom
|
||||
aspect_ratio
|
||||
|
||||
fraction_background scroll fill text_wrap v_align h_align delay tag font text_size
|
||||
fraction_background scroll fill text_wrap v_align h_align delay tag font
|
||||
text_align text_static text_size
|
||||
text_shadow text_shadow_size text_shadow_alpha text_shadow_color
|
||||
text_border text_border_size text_border_alpha text_border_color
|
||||
image_width image_height
|
||||
].each do |item|
|
||||
define_method(item) do
|
||||
@@ -57,6 +61,10 @@ module CyberarmEngine
|
||||
def mark_clean!
|
||||
@dirty = false
|
||||
end
|
||||
|
||||
def empty?
|
||||
@hash.empty?
|
||||
end
|
||||
end
|
||||
|
||||
class Style < StyleData
|
||||
|
||||
@@ -30,6 +30,11 @@ module CyberarmEngine
|
||||
end
|
||||
end
|
||||
|
||||
# css-like style classes, however First In, First Out- that is,
|
||||
options[:style_class]&.each do |klass|
|
||||
hash = deep_merge(hash, _theme[klass]) if _theme[klass]
|
||||
end
|
||||
|
||||
deep_merge(hash, options)
|
||||
end
|
||||
|
||||
@@ -53,6 +58,7 @@ module CyberarmEngine
|
||||
y: 0,
|
||||
z: 30,
|
||||
|
||||
design_width: nil,
|
||||
width: nil,
|
||||
height: nil,
|
||||
color: Gosu::Color::WHITE,
|
||||
@@ -68,7 +74,7 @@ module CyberarmEngine
|
||||
debug_color: Gosu::Color::YELLOW
|
||||
},
|
||||
|
||||
Button: { # < Label
|
||||
Button: { # < TextBlock
|
||||
margin: 1,
|
||||
padding: 4,
|
||||
border_thickness: 1,
|
||||
@@ -121,7 +127,13 @@ module CyberarmEngine
|
||||
text_size: 28,
|
||||
text_wrap: :word_wrap, # :word_wrap, :break_word, :none
|
||||
text_shadow: false,
|
||||
text_shadow_color: 0,
|
||||
text_shadow_alpha: 30,
|
||||
text_shadow_size: 2,
|
||||
text_border: false,
|
||||
text_border_color: 0,
|
||||
text_border_alpha: 30,
|
||||
text_border_size: 2,
|
||||
text_align: :left,
|
||||
font: "Arial",
|
||||
margin: 0,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module CyberarmEngine
|
||||
NAME = "InDev".freeze
|
||||
VERSION = "0.25.1".freeze
|
||||
VERSION = "0.30.0".freeze
|
||||
end
|
||||
|
||||
@@ -6,6 +6,9 @@ module CyberarmEngine
|
||||
SAMPLES = {}
|
||||
SONGS = {}
|
||||
|
||||
FixedUpdateProperties = Data.define(:fixed_timestep, :fixed_timestep_int_ms, :max_timestep)
|
||||
FixedUpdate = Struct.new(:milliseconds, :accumulator, :interpolation, :alpha)
|
||||
|
||||
attr_accessor :show_cursor, :show_stats_plotter
|
||||
attr_writer :exit_on_opengl_error
|
||||
attr_reader :last_frame_time, :delta_time, :states
|
||||
@@ -28,7 +31,7 @@ module CyberarmEngine
|
||||
@@instance
|
||||
end
|
||||
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false)
|
||||
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0 / 60, resizable: false, borderless: false, fixed_update_timestep: 1.0 / 125, fixed_update_timestep_int_ms: 8, fixed_max_timestep: 1.0 / 60)
|
||||
@show_cursor = false
|
||||
@has_focus = false
|
||||
@show_stats_plotter = false
|
||||
@@ -46,6 +49,9 @@ module CyberarmEngine
|
||||
preload_default_shaders if respond_to?(:preload_default_shaders)
|
||||
@stats_plotter = Stats::StatsPlotter.new(2, 28) # FIXME: Make positioning easy
|
||||
|
||||
@fixed_timestep_data = FixedUpdateProperties.new(1.0 / 125, 8, 1.0 / 60)
|
||||
@fixed_update = FixedUpdate.new(0, 0.0, 0.0, 1.0)
|
||||
|
||||
setup
|
||||
end
|
||||
|
||||
@@ -79,11 +85,44 @@ module CyberarmEngine
|
||||
|
||||
Stats.frame.start_timing(:update)
|
||||
current_state&.update
|
||||
Stats.frame.start_timing(:fixed_update)
|
||||
fixed_update
|
||||
Stats.frame.end_timing(:fixed_update)
|
||||
Stats.frame.end_timing(:update)
|
||||
|
||||
Stats.frame.start_timing(:interframe_sleep) unless needs_redraw?
|
||||
end
|
||||
|
||||
def fixed_update
|
||||
@delta_time = @fixed_timestep_data.max_timestep if @delta_time > @fixed_timestep_data.max_timestep
|
||||
@fixed_update.accumulator += @delta_time
|
||||
|
||||
while @fixed_update.accumulator >= @fixed_timestep_data.fixed_timestep
|
||||
@fixed_update.accumulator -= @fixed_timestep_data.fixed_timestep
|
||||
|
||||
@fixed_update.alpha = @fixed_update.accumulator / @fixed_timestep_data.fixed_timestep
|
||||
|
||||
@fixed_update.milliseconds += @fixed_timestep_data.fixed_timestep_int_ms
|
||||
current_state&.fixed_update(@fixed_timestep_data.fixed_timestep)
|
||||
end
|
||||
end
|
||||
|
||||
def fixed_update_milliseconds
|
||||
@fixed_update.milliseconds
|
||||
end
|
||||
|
||||
def fixed_update_accumulator
|
||||
@fixed_update.accumulator
|
||||
end
|
||||
|
||||
def fixed_update_interpolation
|
||||
@fixed_update.interpolation
|
||||
end
|
||||
|
||||
def fixed_update_alpha
|
||||
@fixed_update.alpha
|
||||
end
|
||||
|
||||
def needs_cursor?
|
||||
@show_cursor
|
||||
end
|
||||
|
||||