1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
#
#  MyDocument.rb
#  LEGS
#
#  Created by Scott Russell on 16/07/07.
#  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
#

require 'osx/cocoa'
require 'yaml'

FOO!

class MyDocument < OSX::NSDocument
        ib_outlets :map_controller, :airports_controller, :planes_controller, :view
        attr_accessor :airports, :planes, :pax, :sim, :time, :running, :something_happened, :map, :status, :logtext

        def initialize
                @version=2.0
                @running = false
                @sim=SimModel.new(self)
                @map=MapModel.new
                puts "MAP: #{@map_controller.inspect}"
        end
        
        def allowed_trips
                @sim.allowed_trips if @sim
        end

        def allowed_trips=(val)
                @sim.allowed_trips = val.to_i if @sim
        end
        
        def dispatch(plane)
        end

        def real_distance
        end

        def reset
                @sim.addLog("SIMRESET: Simulation Started")
                @sim.reset
                @view.tick 
        end

        def runToggle
                if @running
                        @sim.addLog("SIMSTOP: Simulation Stopped")
                        @running = false
                        @thinkTimer.invalidate
                        @thinkTimer = nil
                else
                        @sim.addLog("SIMSTART: Simulation Started")
                        @thinkTimer = OSX::NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0/960.0,self,:tick,nil,true)
                        @running = true
                end
                setValue_forKey run_title, "run_title"
        end
        
        def update
                @status = @sim.time.strftime("%a, %b %d - %H:%M")
                setValue_forKey @status, "status"
                @logtext = @sim.logText
                setValue_forKey @logtext, "logtext"
        end
        
        def tick
                @sim.tick
                @view.tick 
                self.update
                @planes_controller.update
                @airports_controller.update
        end

        def run_title
                @running ? "Stop" : "Start"
        end

        def run_title=(val)
        end

  def windowNibName
    return "MyDocument"
  end

  def windowControllerDidLoadNib(aController)
    super_windowControllerDidLoadNib(aController)
        self.update
        @planes_controller.update
        @airports_controller.update
  end

  def dataRepresentationOfType(type)
        @sim.world = nil
        rep = {:version=>@version, :sim=>@sim, :map=>@map}
        rep_str = rep.to_yaml
        @sim.world = self
        str = OSX::NSString.alloc.initWithString rep_str 
        str.dataUsingEncoding(OSX::NSASCIIStringEncoding)
  end
  
  def loadDataRepresentation_ofType(data, type)
        loaded_str_rep = OSX::NSString.alloc.initWithData_encoding(data, OSX::NSASCIIStringEncoding).to_s
        rep = YAML.load(loaded_str_rep)
    @version = rep[:version]
    @time=rep[:time]
        @map=rep[:map] || MapModel.new
        @sim=rep[:sim]
        @sim.sanity_check self, @version
        @sim.reset
        return true
  end
end