예제 #1
0
파일: Addon.cs 프로젝트: zentarul/krpc
        void InitUI()
        {
            // Layout extensions
            GUILayoutExtensions.Init(gameObject);

            // Disconnect client dialog
            clientDisconnectDialog = gameObject.AddComponent <ClientDisconnectDialog> ();

            // Info window
            infoWindow          = gameObject.AddComponent <InfoWindow> ();
            infoWindow.Closable = true;
            infoWindow.Visible  = config.InfoWindowVisible;
            infoWindow.Position = config.InfoWindowPosition;

            // Main window
            mainWindow          = gameObject.AddComponent <MainWindow> ();
            mainWindow.Config   = config;
            mainWindow.Server   = server;
            mainWindow.Visible  = config.MainWindowVisible;
            mainWindow.Position = config.MainWindowPosition;
            mainWindow.ClientDisconnectDialog = clientDisconnectDialog;
            mainWindow.InfoWindow             = infoWindow;

            // New connection dialog
            clientConnectingDialog = gameObject.AddComponent <ClientConnectingDialog> ();

            // Set up events
            InitEvents();

            // Add button to the applauncher
            mainWindow.Closable = true;
            textureOnline       = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-online", false);
            textureOffline      = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-offline", false);
            GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
            GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIApplicationLauncherDestroyed);
            server.OnStarted += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOnline);
                }
            };
            server.OnStopped += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOffline);
                }
            };
        }
예제 #2
0
        /// <summary>
        /// Wake the addon. Creates the server instance and UI.
        /// </summary>
        public void Awake()
        {
            if (!ServicesChecker.OK)
            {
                return;
            }

            Init();

            KRPCServer.Context.SetGameScene(KSPAddonImproved.CurrentGameScene.ToGameScene());
            Logger.WriteLine("Game scene switched to " + KRPCServer.Context.GameScene);

            GUILayoutExtensions.Init(gameObject);

            server.GetUniversalTime = Planetarium.GetUniversalTime;

            // Disconnect client dialog
            clientDisconnectDialog = gameObject.AddComponent <ClientDisconnectDialog> ();

            // Create info window
            infoWindow          = gameObject.AddComponent <InfoWindow> ();
            infoWindow.Server   = server;
            infoWindow.Closable = true;
            infoWindow.Visible  = config.InfoWindowVisible;
            infoWindow.Position = config.InfoWindowPosition;

            // Create main window
            mainWindow          = gameObject.AddComponent <MainWindow> ();
            mainWindow.Config   = config;
            mainWindow.Server   = server;
            mainWindow.Visible  = config.MainWindowVisible;
            mainWindow.Position = config.MainWindowPosition;
            mainWindow.ClientDisconnectDialog = clientDisconnectDialog;
            mainWindow.InfoWindow             = infoWindow;

            // Create new connection dialog
            clientConnectingDialog = gameObject.AddComponent <ClientConnectingDialog> ();

            // Main window events
            mainWindow.OnStartServerPressed += (s, e) => StartServer();
            mainWindow.OnStopServerPressed  += (s, e) => {
                server.Stop();
                clientConnectingDialog.Close();
            };
            mainWindow.OnHide += (s, e) => {
                config.Load();
                config.MainWindowVisible = false;
                config.Save();
            };
            mainWindow.OnShow += (s, e) => {
                config.Load();
                config.MainWindowVisible = true;
                config.Save();
            };
            mainWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as MainWindow;
                config.MainWindowPosition = window.Position;
                config.Save();
            };

            // Info window events
            infoWindow.OnHide += (s, e) => {
                config.Load();
                config.InfoWindowVisible = false;
                config.Save();
            };
            infoWindow.OnShow += (s, e) => {
                config.Load();
                config.InfoWindowVisible = true;
                config.Save();
            };
            infoWindow.OnMoved += (s, e) => {
                config.Load();
                var window = s as InfoWindow;
                config.InfoWindowPosition = window.Position;
                config.Save();
            };

            // Server events
            server.OnClientRequestingConnection += (s, e) => {
                if (config.AutoAcceptConnections)
                {
                    e.Request.Allow();
                }
                else
                {
                    clientConnectingDialog.OnClientRequestingConnection(s, e);
                }
            };


            // Add a button to the applauncher
            mainWindow.Closable = true;
            textureOnline       = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-online", false);
            textureOffline      = GameDatabase.Instance.GetTexture("kRPC/icons/applauncher-offline", false);
            GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
            GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIApplicationLauncherDestroyed);
            server.OnStarted += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOnline);
                }
            };
            server.OnStopped += (s, e) => {
                if (applauncherButton != null)
                {
                    applauncherButton.SetTexture(textureOffline);
                }
            };
        }