Esempio n. 1
0
        /// <summary>
        ///   Constructor settings the initial window settings.
        /// </summary>
        /// <param name="settings">
        ///   Window settings, this is corrected on assignment.
        /// </param>
        public GameWindow(WindowSettings settings)
        {
            Running    = false;
            Settings   = settings?.AsValid() ?? new WindowSettings();
            ClearColor = Color.Black;
            Window     = null;
            Manager    = null;
            RunAgain   = false;

            LoadWindowSettings = false;
            SaveWindowSettings = true;
            LoadInputSettings  = true;
            SaveInputSettings  = true;
        }
Esempio n. 2
0
        /// <summary>
        ///   Changes the current window settings, optionally restarting the game for the changes to
        ///   take effect. Has no effect if <see cref="SaveWindowSettings"/> is false.
        /// </summary>
        /// <remarks>
        ///   Changes will not be loaded on restart if <see cref="LoadWindowSettings"/> is false.
        ///   Changes will not be saved if <see cref="SaveWindowSettings"/> is false as thus will
        ///   not be applied unless <see cref="LoadWindowSettings"/> is also false.
        /// </remarks>
        /// <param name="settings">
        ///   Window settings.
        /// </param>
        /// <param name="restart">
        ///   If game should close and restart to apply new changes.
        /// </param>
        /// <returns>
        ///   True if settings were valid and were assigned and written to file, otherwise false.
        /// </returns>
        public bool ChangeSettings(WindowSettings settings, bool restart = false)
        {
            if (settings is null || !settings.IsValid)
            {
                return(false);
            }

            Settings = settings;

            if (SaveWindowSettings && !XmlLoadable.ToFile(Settings, SettingsPath, true))
            {
                return(Logger.LogReturn("Unable to save newly set window settings to file.", false, LogType.Error));
            }

            if (restart)
            {
                RunAgain = true;
                Exit();
            }

            return(true);
        }
Esempio n. 3
0
        private bool Initialise()
        {
            // Load input settings.
            if (LoadInputSettings)
            {
                if (!Input.Manager.LoadFromFile())
                {
                    if (!MapDefaultActions())
                    {
                        return(Logger.LogReturn("Failed assigning default input actions.", false, LogType.Error));
                    }

                    if (!Input.Manager.SaveToFile())
                    {
                        Logger.Log("Failed saving default inputs. Any modifications to the assigned inputs will not be saved.", LogType.Warning);
                    }
                }
            }
            else if (!MapDefaultActions())
            {
                return(Logger.LogReturn("Failed assigning default input actions.", false, LogType.Error));
            }

            if (Settings is null)
            {
                Settings = new WindowSettings();
            }

            // Load/Save WindowSettings.
            if (LoadWindowSettings)
            {
                if (File.Exists(SettingsPath))
                {
                    Settings = XmlLoadable.FromFile <WindowSettings>(SettingsPath);

                    if (Settings is null)
                    {
                        ExitCode = ExitCode.InitFail;
                        return(Logger.LogReturn("Failed loading window settings from file although it exists.", false, LogType.Error));
                    }
                }
            }

            Settings.MakeValid();

            if (SaveWindowSettings)
            {
                if (!XmlLoadable.ToFile(Settings, SettingsPath, true))
                {
                    ExitCode = ExitCode.InitFail;
                    return(Logger.LogReturn("Failed saving window settings to file.", false, LogType.Error));
                }
            }

            // Set up window.
            if (Window is not null)
            {
                Window.Dispose();
            }

            Styles wm = Settings.WindowMode is WindowMode.Bordered   ? Styles.Titlebar :
                        (Settings.WindowMode is WindowMode.Fullscreen ? Styles.Fullscreen : Styles.None);

            if (wm is Styles.Titlebar)
            {
                if (Settings.Close)
                {
                    wm |= Styles.Close;
                }
                if (Settings.Resizable)
                {
                    wm |= Styles.Resize;
                }
            }

            Window = new RenderWindow(new VideoMode(Settings.Width, Settings.Height), GameTitle, wm);

            if (Window is null || !Window.IsOpen)
            {
                ExitCode = ExitCode.InitFail;
                return(Logger.LogReturn("Failed creating render window.", false, LogType.Error));
            }

            Window.Closed += OnClose;
            Manager        = new StateManager(this);

            if (!OnInit())
            {
                ExitCode = ExitCode.OnInitFail;
                return(false);
            }

            return(true);
        }