Class: RubyArmor::ReviewCode

Inherits:
Fidgit::DialogState
  • Object
show all
Defined in:
lib/ruby_armor/states/review_code.rb

Constant Summary collapse

LEVELS =
(1..9).to_a + [:EPIC]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ ReviewCode

Returns a new instance of ReviewCode.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_armor/states/review_code.rb', line 16

def initialize(profile)
  super(shadow_full: true)

  on_input :escape, :hide

  @profile = profile

  vertical spacing: 10, align: :center, border_thickness: 4, 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: :auto, shortcut_color: Play::SHORTCUT_COLOR, 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

Class Method Details

.path_for_level(profile, level) ⇒ Object



6
7
8
# File 'lib/ruby_armor/states/review_code.rb', line 6

def path_for_level(profile, level)
  File.join(profile.player_path, "ruby_armor/player_#{level.is_a?(Symbol) ? level : level.to_s.rjust(3, '0')}.rb")
end

.saved_levels?(profile) ⇒ Boolean

Check if there are levels saved that can be recalled.

Returns:

  • (Boolean)


11
12
13
# File 'lib/ruby_armor/states/review_code.rb', line 11

def saved_levels?(profile)
  LEVELS.any? {|level| File.exists? path_for_level(profile, level) }
end

Instance Method Details

#path_for_level(level) ⇒ Object



71
72
73
# File 'lib/ruby_armor/states/review_code.rb', line 71

def path_for_level(level)
  self.class.path_for_level @profile, level
end