summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--dragon/args.rb4
-rw-r--r--dragon/console.rb115
-rw-r--r--dragon/controller_config.rb24
-rw-r--r--dragon/directional_input_helper_methods.rb4
-rw-r--r--dragon/geometry.rb18
-rw-r--r--dragon/grid.rb45
-rw-r--r--dragon/inputs.rb94
7 files changed, 221 insertions, 83 deletions
diff --git a/dragon/args.rb b/dragon/args.rb
index e39e63e..e2e8a5c 100644
--- a/dragon/args.rb
+++ b/dragon/args.rb
@@ -46,7 +46,7 @@ module GTK
def serialize
{
- state: state.hash,
+ state: state.as_hash,
inputs: inputs.serialize,
passes: passes.serialize,
outputs: outputs.serialize,
@@ -65,7 +65,7 @@ module GTK
def render_target name
name = name.to_s
if !@render_targets[name]
- @render_targets[name] = Outputs.new name
+ @render_targets[name] = Outputs.new(target: name, background_color_override: [255, 255, 255, 0])
@passes << @render_targets[name]
end
@render_targets[name]
diff --git a/dragon/console.rb b/dragon/console.rb
index d58f507..5b1d93b 100644
--- a/dragon/console.rb
+++ b/dragon/console.rb
@@ -44,14 +44,12 @@ module GTK
end
def console_text_width
- @console_text_width ||= 1260.idiv($gtk.calcstringbox('W', self.size_enum, @font)[0])
+ @console_text_width ||= (GAME_WIDTH - 20).idiv($gtk.calcstringbox('W', self.size_enum, @font)[0])
@console_text_width
end
def save_history
- str = ''
- @command_history.reverse_each { |s| str << s ; str << "\n" }
- $gtk.ffi_file.storefile(@history_fname, str)
+ $gtk.ffi_file.storefile(@history_fname, @command_history.reverse.join("\n"))
end
def load_history
@@ -69,6 +67,8 @@ module GTK
break if @command_history.length >= @max_history
end
}
+
+ @command_history.uniq!
end
def disable
@@ -79,6 +79,32 @@ module GTK
@disabled = false
end
+ def addsprite obj
+ obj[:id] ||= "id_#{obj[:path]}_#{Time.now.to_i}".to_sym
+
+ if @last_line_log_index &&
+ @last_sprite_line.is_a?(Hash) &&
+ @last_sprite_line[:id] == obj[:id]
+
+ @log[@last_line_log_index] = obj
+ return
+ end
+
+ @log << obj
+ @last_line_log_index = @log.length - 1
+ @last_sprite_line = obj
+ nil
+ end
+
+ def add_primitive obj
+ if obj.is_a? Hash
+ addsprite obj
+ else
+ addtext obj
+ end
+ nil
+ end
+
def addtext obj
@last_log_lines_count ||= 1
@@ -110,6 +136,7 @@ module GTK
@last_log_lines_count = 1
@last_log_lines = log_lines
+ nil
end
def ready?
@@ -221,11 +248,19 @@ S
toast_extended id, nil, *messages
end
+ def console_toggle_keys
+ [
+ :backtick!,
+ :tilde!,
+ :superscript_two!,
+ :section_sign!,
+ :ordinal_indicator!,
+ :circumflex!,
+ ]
+ end
+
def console_toggle_key_down? args
- return args.inputs.keyboard.key_down.backtick! ||
- args.inputs.keyboard.key_down.superscript_two! ||
- args.inputs.keyboard.key_down.section_sign! ||
- args.inputs.keyboard.key_down.ordinal_indicator!
+ args.inputs.keyboard.key_down.any? console_toggle_keys
end
def eval_the_set_command
@@ -269,7 +304,7 @@ S
def scroll_up_full
fontwidth, fontheight = $gtk.calcstringbox 'W', self.size_enum, @font # we only need the height of a line of text here.
- lines_on_one_page = (720.0 / fontheight).to_i - 4
+ lines_on_one_page = (GAME_HEIGHT.fdiv(fontheight)).to_i - 4
@log_offset += lines_on_one_page
@log_offset = @log.size if @log_offset > @log.size
end
@@ -281,7 +316,7 @@ S
def scroll_up_half
fontwidth, fontheight = $gtk.calcstringbox 'W', self.size_enum, @font # we only need the height of a line of text here.
- lines_on_one_page = (720.0 / fontheight).to_i - 4
+ lines_on_one_page = (GAME_HEIGHT.fdiv(fontheight)).to_i - 4
@log_offset += lines_on_one_page.idiv(2)
@log_offset = @log.size if @log_offset > @log.size
end
@@ -294,7 +329,7 @@ S
def scroll_down_full
fontwidth, fontheight = $gtk.calcstringbox 'W', self.size_enum, @font # we only need the height of a line of text here.
- lines_on_one_page = (720.0 / fontheight).to_i - 4
+ lines_on_one_page = (GAME_HEIGHT.fdiv(fontheight)).to_i - 4
@log_offset -= lines_on_one_page
@log_offset = 0 if @log_offset < 0
end
@@ -311,7 +346,7 @@ S
def scroll_down_half
fontwidth, fontheight = $gtk.calcstringbox 'W', self.size_enum, @font # we only need the height of a line of text here.
- lines_on_one_page = (720.0 / fontheight).to_i - 4
+ lines_on_one_page = (GAME_HEIGHT.fdiv(fontheight)).to_i - 4
@log_offset -= lines_on_one_page.idiv(2)
@log_offset = 0 if @log_offset < 0
end
@@ -398,13 +433,24 @@ S
args.inputs.keyboard.key_held.clear
end
+ def write_primitive_and_return_offset args, left, y, str, errorinfo, headerinfo, txtinfo, line_height
+ if str.is_a?(Hash)
+ padding = 10
+ args.outputs.reserved << [left + 10, y - padding * 1.66, str[:w], str[:h], str[:path]].sprite
+ return str[:h] + padding
+ else
+ write_line args, left, y, str, errorinfo, headerinfo, txtinfo
+ return line_height
+ end
+ end
+
def write_line args, left, y, str, errorinfo, headerinfo, txtinfo
str ||= ''
if include_error_marker? str
args.outputs.reserved << [left + 10, y, str, self.size_enum, 0, *errorinfo].label
elsif include_subdued_markers? str
args.outputs.reserved << [left + 10, y, str, self.size_enum, 0, [txtinfo[0..2], txtinfo[3].half]].label
- elsif str.start_with?("====") || str.include?("app")
+ elsif (str.start_with?("====") || str.include?("app")) && !str.include?("apple")
args.outputs.reserved << [left + 10, y, str, self.size_enum, 0, *headerinfo].label
else
args.outputs.reserved << [left + 10, y, str, self.size_enum, 0, *txtinfo].label
@@ -427,8 +473,8 @@ S
top = $gtk.args.grid.top
left = $gtk.args.grid.left
- y = top - (720.0 * percent)
- args.outputs.reserved << [left, y, 1280, 720, @background_color[0], @background_color[1], @background_color[2], (@background_color[3].to_f * percent).to_i].solid
+ y = top - (GAME_HEIGHT * percent)
+ args.outputs.reserved << [left, y, GAME_WIDTH, GAME_HEIGHT, @background_color[0], @background_color[1], @background_color[2], (@background_color[3].to_f * percent).to_i].solid
logo_y = y
@@ -439,22 +485,22 @@ S
y += 2 # just give us a little padding at the bottom.
y += h # !!! FIXME: remove this when we fix coordinate origin on labels.
- args.outputs.reserved << [left + 1280 - 210, logo_y + 540, 200, 200, @logo, 0, (80.0 * percent).to_i].sprite
+ args.outputs.reserved << [left + GAME_WIDTH - 210, logo_y + (GAME_HEIGHT - 180), 200, 200, @logo, 0, (80.0 * percent).to_i].sprite
args.outputs.reserved << [left + 10, y, "#{@prompt}#{@current_input_str}", self.size_enum, 0, *txtinfo].label
args.outputs.reserved << [left + 8, y + 3, (" " * (prompt.length + @current_input_str.length)) + "|", self.size_enum, 0, *cursorinfo ].label
y += h.to_f / 2.0
- args.outputs.reserved << [left + 0, y, 1280, y, *txtinfo].line
+ args.outputs.reserved << [left + 0, y, GAME_WIDTH, y, *txtinfo].line
y += h.to_f / 2.0
y += h # !!! FIXME: remove this when we fix coordinate origin on labels.
((@log.size - @log_offset) - 1).downto(0) do |idx|
- write_line args, left, y, @log[idx], errorinfo, headerinfo, txtinfo
- y += h
+ offset_after_write = write_primitive_and_return_offset args, left, y, @log[idx], errorinfo, headerinfo, txtinfo, h
+ y += offset_after_write
break if y > top
end
# past log seperator
- args.outputs.reserved << [0, y - h.half, 1280, y - h.half, [txtinfo[0..2], txtinfo[3].idiv(4)]].line
+ args.outputs.reserved << [0, y - h.half, GAME_WIDTH, y - h.half, [txtinfo[0..2], txtinfo[3].idiv(4)]].line
y += h
@@ -464,8 +510,8 @@ S
headerinfo = [ @header_color[0], @header_color[1], @header_color[2], (@header_color[3].to_f * percent.half).to_i, @font ]
((@archived_log.size - @log_offset) - 1).downto(0) do |idx|
- write_line args, left, y, @archived_log[idx], errorinfo, headerinfo, txtinfo
- y += h
+ offset_after_write = write_primitive_and_return_offset args, left, y, @archived_log[idx], errorinfo, headerinfo, txtinfo, h
+ y += offset_after_write
break if y > top
end
@@ -475,7 +521,7 @@ S
def render_log_offset args
return if @log_offset <= 0
s = "[#{@log_offset}/#{@log.size}]"
- args.outputs.reserved << [1280 - 5, 720 - 5, s, 0, 2, 255, 255, 255].label
+ args.outputs.reserved << [GAME_WIDTH - 5, GAME_HEIGHT - 5, s, 0, 2, 255, 255, 255].label
end
def include_error_marker? text
@@ -529,14 +575,27 @@ S
end
end
- def set_command command, show_reason = nil
- @command_history << command
- if @command_set_at != Kernel.global_tick_count
- @current_input_str = command
- end
+ def set_command_with_history_silent command, histories, show_reason = nil
+ @command_history.concat histories
+ @command_history << command if @command_history[-1] != command
+ @current_input_str = command if @command_set_at != Kernel.global_tick_count
@command_set_at = Kernel.global_tick_count
@command_history_index = -1
+ save_history
+ end
+
+ def set_command_with_history command, histories, show_reason = nil
+ set_command_with_history_silent command, histories, show_reason
show show_reason
end
+
+ def set_command command, show_reason = nil
+ set_command_silent command, show_reason
+ show show_reason
+ end
+
+ def set_command_silent command, show_reason = nil
+ set_command_with_history_silent command, [], show_reason
+ end
end
end
diff --git a/dragon/controller_config.rb b/dragon/controller_config.rb
index 18bc92d..091fa1e 100644
--- a/dragon/controller_config.rb
+++ b/dragon/controller_config.rb
@@ -208,13 +208,13 @@ module GTK
if @fading == 0
return 255
elsif @fading > 0 # fading in
- percent = @toggled_at.ease_using_global_tick_count(@animation_duration, :flip, :quint, :flip)
+ percent = @toggled_at.global_ease(@animation_duration, :flip, :quint, :flip)
if percent >= 1.0
percent = 1.0
@fading = 0
end
else # fading out
- percent = @toggled_at.ease_using_global_tick_count(@animation_duration, :flip, :quint)
+ percent = @toggled_at.global_ease(@animation_duration, :flip, :quint)
if percent <= 0.0
percent = 0.0
@fading = 0
@@ -226,10 +226,10 @@ module GTK
def render_basics args, msg, fade=255
joystickname = @target[1][:name]
- args.outputs.primitives << [0, 0, 1280, 720, 255, 255, 255, fade].solid
- args.outputs.primitives << [0, 0, 1280, 720, 'dragonruby-controller.png', 0, fade, 255, 255, 255].sprite
- args.outputs.primitives << [1280 / 2, 700, joystickname, 2, 1, 0, 0, 0, fade].label
- args.outputs.primitives << [1280 / 2, 650, msg, 0, 1, 0, 0, 0, 255].label if !msg.empty?
+ args.outputs.primitives << [0, 0, GAME_WIDTH, GAME_HEIGHT, 255, 255, 255, fade].solid
+ args.outputs.primitives << [0, 0, GAME_WIDTH, GAME_HEIGHT, 'dragonruby-controller.png', 0, fade, 255, 255, 255].sprite
+ args.outputs.primitives << [GAME_WIDTH / 2, 700, joystickname, 2, 1, 0, 0, 0, fade].label
+ args.outputs.primitives << [GAME_WIDTH / 2, 650, msg, 0, 1, 0, 0, 0, 255].label if !msg.empty?
end
def render_part_highlight args, part, alpha=255
@@ -303,7 +303,7 @@ module GTK
end
render_basics args, 'Now play around with the controller, and make sure it feels right!'
- args.outputs.primitives << [1280 / 2, 90, '[ESCAPE]: Reconfigure, [SPACE]: Save this configuration', 0, 1, 0, 0, 0, 255].label
+ args.outputs.primitives << [GAME_WIDTH / 2, 90, '[ESCAPE]: Reconfigure, [SPACE]: Save this configuration', 0, 1, 0, 0, 0, 255].label
axes = @joystick_state[:axes]
if !axes.nil?
@@ -336,6 +336,12 @@ module GTK
return true
end
+ def should_tick?
+ return true if @play_around
+ return true if @target
+ return false
+ end
+
def tick args
return true if play_around args
return false if !choose_target
@@ -371,9 +377,9 @@ module GTK
return true if fade < 255 # all done for now
part = @parts[@current_part]
- args.outputs.primitives << [1280 / 2, 575, "Please press the #{part[2]}.", 0, 1, 0, 0, 0, 255].label
+ args.outputs.primitives << [GAME_WIDTH / 2, 575, "Please press the #{part[2]}.", 0, 1, 0, 0, 0, 255].label
render_part_highlight args, part, @part_alpha
- args.outputs.primitives << [1280 / 2, 90, '[ESCAPE]: Ignore controller, [BACKSPACE]: Go back one button, [SPACE]: Skip this button', 0, 1, 0, 0, 0, 255].label
+ args.outputs.primitives << [GAME_WIDTH / 2, 90, '[ESCAPE]: Ignore controller, [BACKSPACE]: Go back one button, [SPACE]: Skip this button', 0, 1, 0, 0, 0, 255].label
@part_alpha += @part_alpha_increment
if (@part_alpha_increment > 0) && (@part_alpha >= 255)
diff --git a/dragon/directional_input_helper_methods.rb b/dragon/directional_input_helper_methods.rb
index b64839b..8550e59 100644
--- a/dragon/directional_input_helper_methods.rb
+++ b/dragon/directional_input_helper_methods.rb
@@ -63,15 +63,13 @@ S
return send(m)
# see if the key is either held or down
- elsif self.key_down.respond_to? m
+ else
define_singleton_method(m) do
self.key_down.send(m) || self.key_held.send(m)
end
return send(m)
end
-
- super
end
end
end
diff --git a/dragon/geometry.rb b/dragon/geometry.rb
index 77e07a3..777adcd 100644
--- a/dragon/geometry.rb
+++ b/dragon/geometry.rb
@@ -5,6 +5,24 @@
module GTK
module Geometry
+ def self.cubic_bezier t, a, b, c, d
+ s = 1 - t
+ s0 = 1
+ s1 = s
+ s2 = s * s
+ s3 = s * s * s
+
+ t0 = 1
+ t1 = t
+ t2 = t * t
+ t3 = t * t * t
+
+ 1 * s3 * t0 * a +
+ 3 * s2 * t1 * b +
+ 3 * s1 * t2 * c +
+ 1 * s0 * t3 * d
+ end
+
def inside_rect? outer
Geometry.inside_rect? self, outer
end
diff --git a/dragon/grid.rb b/dragon/grid.rb
index ff6676a..b794a7d 100644
--- a/dragon/grid.rb
+++ b/dragon/grid.rb
@@ -7,10 +7,9 @@ module GTK
class Grid
include Serialize
SCREEN_Y_DIRECTION = -1.0
-
attr_accessor :bottom, :left, :right, :top,
:rect, :origin_x, :origin_y, :center_x, :center_y,
- :name, :ffi_draw
+ :name
def initialize ffi_draw
@ffi_draw = ffi_draw
@@ -33,18 +32,26 @@ module GTK
@origin_y + y * SCREEN_Y_DIRECTION
end
+ def ffi_draw
+ @ffi_draw
+ end
+
+ def ffi_draw= value
+ @ffi_draw = value
+ end
+
def origin_bottom_left!
return if @name == :bottom_left
@name = :bottom_left
@origin_x = 0.0
- @origin_y = 720.0
+ @origin_y = GAME_HEIGHT
@left = 0.0
- @right = 1280.0
- @top = 720.0
+ @right = GAME_WIDTH
+ @top = GAME_HEIGHT
@bottom = 0.0
- @center_x = 640.0
- @center_y = 360.0
- @rect = [@left, @bottom, 1280.0, 720.0].rect
+ @center_x = GAME_WIDTH.half
+ @center_y = GAME_HEIGHT.half
+ @rect = [@left, @bottom, GAME_WIDTH, GAME_HEIGHT].rect
@center = [@center_x, @center_y].point
@ffi_draw.set_gtk_grid @origin_x, @origin_y, SCREEN_Y_DIRECTION
end
@@ -52,33 +59,33 @@ module GTK
def origin_center!
return if @name == :center
@name = :center
- @origin_x = 640.0
- @origin_y = 360.0
- @left = -640.0
- @right = 640.0
- @top = 360.0
- @bottom = -360.0
+ @origin_x = GAME_WIDTH.half
+ @origin_y = GAME_HEIGHT.half
+ @left = -GAME_WIDTH.half
+ @right = GAME_WIDTH.half
+ @top = GAME_HEIGHT.half
+ @bottom = -GAME_HEIGHT.half
@center_x = 0.0
@center_y = 0.0
- @rect = [@left, @bottom, 1280.0, 720.0].rect
+ @rect = [@left, @bottom, GAME_WIDTH, GAME_HEIGHT].rect
@center = [@center_x, @center_y].point
@ffi_draw.set_gtk_grid @origin_x, @origin_y, SCREEN_Y_DIRECTION
end
def w
- 1280.0
+ GAME_WIDTH
end
def w_half
- 640.0
+ w.half
end
def h
- 720.0
+ GAME_HEIGHT
end
def h_half
- 360.0
+ h.half
end
def center
diff --git a/dragon/inputs.rb b/dragon/inputs.rb
index 07d1dc8..9b8720a 100644
--- a/dragon/inputs.rb
+++ b/dragon/inputs.rb
@@ -29,6 +29,7 @@ module GTK
:left, :right, :up, :down, :pageup, :pagedown,
:char, :plus, :at, :forward_slash, :back_slash, :asterisk,
:less_than, :greater_than, :carat, :ampersand, :superscript_two,
+ :circumflex,
:question_mark, :section_sign, :ordinal_indicator,
:raw_key
@@ -152,12 +153,12 @@ module GTK
"*" => [:asterisk],
"<" => [:less_than],
">" => [:greater_than],
- "^" => [:greater_than],
+ "^" => [:circumflex],
"&" => [:ampersand],
"²" => [:superscript_two],
"§" => [:section_sign],
"?" => [:question_mark],
- "%" => [:percent_sign],
+ '%' => [:percent_sign],
"º" => [:ordinal_indicator],
1073741903 => [:right],
1073741904 => [:left],
@@ -192,11 +193,11 @@ module GTK
def truthy_keys
get(all).find_all { |_, v| v }
- .map { |k, _| k.to_sym }
+ .map { |k, _| k.to_sym }
end
def all? keys
- values = keys_to_get(keys.map { |k| k.without_ending_bang })
+ values = get(keys.map { |k| k.without_ending_bang })
all_true = values.all? do |k, v|
v
end
@@ -211,7 +212,7 @@ module GTK
end
def any? keys
- values = keys_to_get(keys.map { |k| k.without_ending_bang })
+ values = get(keys.map { |k| k.without_ending_bang })
any_true = values.any? do |k, v|
v
end
@@ -232,8 +233,8 @@ module GTK
def all
@scrubbed_ivars ||= self.instance_variables
- .reject { |i| i == :@all || i == :@scrubbed_ivars }
- .map { |i| i.to_s.gsub("@", "") }
+ .reject { |i| i == :@all || i == :@scrubbed_ivars }
+ .map { |i| i.to_s.gsub("@", "") }
get(@scrubbed_ivars).map { |k, _| k }
end
@@ -273,25 +274,23 @@ S
end
def method_missing m, *args
- if m.to_s.length != 1 && m.end_with_bang? # creation of args.intputs.SOME_KEY! (where the key is queried and then immediately cleared)
- begin
- define_singleton_method(m) do
- r = self.instance_variable_get("@#{m.without_ending_bang}".to_sym)
- clear_key m
- return r
- end
-
- return self.send m
- rescue Exception => e
- log_important "#{e}}"
+ begin
+ define_singleton_method(m) do
+ r = self.instance_variable_get("@#{m.without_ending_bang}".to_sym)
+ clear_key m
+ return r
end
+
+ return self.send m
+ rescue Exception => e
+ log_important "#{e}}"
end
raise <<-S
* ERROR:
There is no member on the keyboard called #{m}. Here is a to_s representation of what's available:
-#{KeyboardKeys.char_to_method_hash}
+#{KeyboardKeys.char_to_method_hash.map { |k, v| "[#{k} => #{v.join(",")}]" }.join(" ")}
S
end
@@ -316,6 +315,10 @@ module GTK
@has_focus = false
end
+ def p
+ @key_down.p || @key_held.p
+ end
+
def left
@key_up.left || @key_held.left || a
end
@@ -355,6 +358,19 @@ module GTK
serialize.to_s
end
+ def keys
+ key
+ end
+
+ def key
+ {
+ down: @key_down.truthy_keys,
+ held: @key_held.truthy_keys,
+ down_or_held: (@key_down.truthy_keys + @key_held.truthy_keys).uniq,
+ up: @key_up.truthy_keys,
+ }
+ end
+
include DirectionalInputHelperMethods
end
end
@@ -505,6 +521,27 @@ module GTK
def created_at_elapsed
@created_at.elapsed_time
end
+
+ def to_hash
+ serialize
+ end
+
+ def serialize
+ {
+ x: @x,
+ y: @y,
+ created_at: @created_at,
+ global_created_at: @global_created_at
+ }
+ end
+
+ def inspect
+ serialize.to_s
+ end
+
+ def to_s
+ serialize.to_s
+ end
end
class Mouse
@@ -565,11 +602,11 @@ module GTK
result = {}
if @click
- result[:click] = @click.hash
- result[:down] = @click.hash
+ result[:click] = @click.to_hash
+ result[:down] = @click.to_hash
end
- result[:up] = @up.hash if @up
+ result[:up] = @up.to_hash if @up
result[:x] = @x
result[:y] = @y
result[:moved] = @moved
@@ -642,6 +679,19 @@ module GTK
@controllers.value(1)
end
+ def clear
+ @mouse.clear
+ @keyboard.key_down.clear
+ @keyboard.key_up.clear
+ @keyboard.key_held.clear
+ @controllers[0].key_down.clear
+ @controllers[0].key_up.clear
+ @controllers[0].key_held.clear
+ @controllers[1].key_down.clear
+ @controllers[1].key_up.clear
+ @controllers[1].key_held.clear
+ end
+
def serialize
{
controller_one: controller_one.serialize,