Permalink
Browse files

Added code-review page.

  • Loading branch information...
1 parent 74fac9a commit ab0c27a48cee7d11abf7ba4fd31ed650d129fe9b @Spooner committed Feb 27, 2012
Showing with 86 additions and 4 deletions.
  1. +1 −0 lib/ruby_armor.rb
  2. +12 −4 lib/ruby_armor/states/play.rb
  3. +73 −0 lib/ruby_armor/states/review_code.rb
View
1 lib/ruby_armor.rb
@@ -22,6 +22,7 @@
require "ruby_armor/sprite_sheet"
require "ruby_armor/states/choose_profile"
require "ruby_armor/states/play"
+require "ruby_armor/states/review_code"
require "ruby_armor/window"
require "ruby_armor/warrior_config"
View
16 lib/ruby_armor/states/play.rb
@@ -106,7 +106,7 @@ def setup
end
def create_ui_bar
- vertical padding: 0, height: 260, width: 100 do
+ vertical padding: 0, height: 260, width: 100, spacing: 6 do
# Labels at top-right.
@tower_label = label "", tip: "Each tower has a different difficulty level"
@level_label = label "Level:", tip: "Each tower contains 9 levels"
@@ -144,21 +144,27 @@ def create_ui_bar
end
horizontal padding: 0, spacing: 21 do
- button_options = { padding: 4, border_thickness: 0, shortcut: :auto, shortcut_color: SHORTCUT_COLOR }
- @turn_slower_button = button "-", button_options.merge(tip: "Make turns run slower") do
+ options = { padding: 4, border_thickness: 0, shortcut: :auto, shortcut_color: SHORTCUT_COLOR }
+ @turn_slower_button = button "-", options.merge(tip: "Make turns run slower") do
@config.turn_delay = [@config.turn_delay + TURN_DELAY_STEP, MAX_TURN_DELAY].min if @config.turn_delay < MAX_TURN_DELAY
update_turn_delay
end
@turn_duration_label = label "", align: :center
- @turn_faster_button = button "+", button_options.merge(tip: "Make turns run faster") do
+ @turn_faster_button = button "+", options.merge(tip: "Make turns run faster") do
@config.turn_delay = [@config.turn_delay - TURN_DELAY_STEP, MIN_TURN_DELAY].max if @config.turn_delay > MIN_TURN_DELAY
update_turn_delay
end
update_turn_delay
end
+
+ # Review old level code.
+ @review_button = button "Review", button_options.merge(tip: "Review code used for each level",
+ enabled: false, border_thickness: 0) do
+ ReviewCode.new(profile).show
+ end
end
end
@@ -291,6 +297,8 @@ def prepare_level
@units_record = Array.new
@health = [level.warrior.health]
+ @review_button.enabled = ReviewCode.saved_levels? profile # Can't review code unless some has been saved.
+
self.turn = 0
@file_contents["README"].text = replace_syntax File.read(File.join(level.player_path, "README"))
View
73 lib/ruby_armor/states/review_code.rb
@@ -0,0 +1,73 @@
+module RubyArmor
+ class ReviewCode < Fidgit::DialogState
+ LEVELS = 1..9
+
+ class << self
+ def path_for_level(profile, level)
+ File.join(profile.player_path, "ruby_armor/player_#{level.to_s.rjust(2, '0')}.rb")
+ end
+
+ # Check if there are levels saved that can be recalled.
+ def saved_levels?(profile)
+ LEVELS.any? {|level| File.exists? path_for_level(profile, level) }
+ end
+ end
+
+ def initialize(profile)
+ super(shadow_full: true)
+
+ @profile = profile
+
+ vertical spacing: 10, align: :center, background_color: Color::BLACK do
+ label "Reviewing code that completed levels in #{profile.tower.name} tower", font_height: 20
+
+ @tab_group = group do
+ @tab_buttons = horizontal padding: 0, spacing: 4 do
+ LEVELS.each do |level|
+ if File.exists?(path_for_level(level))
+ radio_button level.to_s, level, border_thickness: 0,
+ tip: "View code used to complete level #{level}"
+ else
+ button level.to_s, border_thickness: 0, enabled: false,
+ tip: "No code saved for level #{level}"
+ end
+ end
+
+ horizontal padding_left: 50, padding: 0 do
+ button "copy", tip: "Copy displayed code to clipboard", font_height: 12, border_thickness: 0, padding: 4 do
+ Clipboard.copy @code.stripped_text
+ end
+ end
+ end
+
+ subscribe :changed do |_, value|
+ buttons = @tab_buttons.each.grep Fidgit::RadioButton
+ current = buttons.find {|b| b.value == value }
+ buttons.each {|b| b.enabled = (b != current) }
+ current.color, current.background_color = current.background_color, current.color
+
+ @code.text = File.read path_for_level(value)
+ end
+ end
+
+ # Contents of those tabs.
+ vertical padding: 0 do
+ scroll_window width: 700, height: 430 do
+ @code = text_area width: 680
+ end
+ end
+
+ button "Close", shortcut: :escape, align_h: :center, border_thickness: 0 do
+ hide
+ end
+
+ # Pick the last level we have completed (and saved the code).
+ @tab_group.value = LEVELS.to_a.reverse.find {|level| File.exists? path_for_level(level) }
+ end
+ end
+
+ def path_for_level(level)
+ self.class.path_for_level @profile, level
+ end
+ end
+end

0 comments on commit ab0c27a

Please sign in to comment.