Class: Shuttlecraft::ShuttlecraftApp

Inherits:
Object
  • Object
show all
Defined in:
lib/shuttlecraft/shuttlecraft_app.rb

Class Method Summary collapse

Class Method Details

.runObject



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/shuttlecraft/shuttlecraft_app.rb', line 36

def self.run
  @my_app = Shoes.app width: 360, height: 360, resizeable: false, title: 'Shuttlecraft' do

    @shuttlecraft = nil

    def display_screen
      clear do
        stack :margin => 20 do
          title "Shuttlecraft #{@shuttlecraft.name}"

          stack do @status = para end

          @registered = nil
          @updating_area = stack
          @msg_stack = stack
        end

        animate(5) {
          if @shuttlecraft

            detect_registration_change

            if @registered
              @registrations.replace registrations_text

              @msg_stack.clear do
                for msg in @shuttlecraft.msg_log
                  para msg
                end
              end
            end
          end
        }
      end
    end

    def detect_registration_change
      if @registered != @shuttlecraft.registered?
        @registered = @shuttlecraft.registered?
        @status.replace "#{"Not " unless @registered}Registered"
        @updating_area.clear do
          if @registered
            button("Unregister") { unregister }

            el = edit_line

            button("Send") {
              @shuttlecraft.broadcast(el.text)
              el.text = ''
            }
            stack do
              para 'Registered Services:'
              @registrations = para
            end
          else
            button("Register")    { register }
          end
        end
      end
    end

    def launch_screen
      clear do
        background black
        title "Build Shuttlecraft", stroke: white
        edit_line text: 'Name' do |s|
          @name = s.text
        end
        button('launch') {
          @shuttlecraft = MyShuttlecraft.new(name: @name)
          initiate_comms_screen
        }
      end
    end

    def initiate_comms_screen
      clear do
        background black
        title "Initiate Comms", stroke: white

        stack do
          motherships = @shuttlecraft.find_all_motherships

          if motherships.empty?
            subtitle "No Motherships within range", stroke: white
          else
            subtitle "Select Mothership", stroke: white
          end
          for mothership in motherships
            button(mothership[:name]) {|b|
              begin
                @shuttlecraft.initiate_communication_with_mothership(b.text)
              rescue
                initiate_comms_screen
              end
              display_screen
            }
          end

          button('launch mothership') {
            load File.dirname(__FILE__) + '/mothership_app.rb'
          }
          button('rescan') {
            initiate_comms_screen
          }
        end
      end
    end

    def register
      @shuttlecraft.register if @shuttlecraft
    end

    def unregister
      @shuttlecraft.unregister if @shuttlecraft
    end

    def registrations_text
      if @shuttlecraft
        @shuttlecraft.registered_services.join(', ')
      end
    end

    launch_screen
  end
ensure
  @my_app.unregister if @my_app
end