skin loading and fullscreen toggle in options

This commit is contained in:
Michael 2017-07-26 09:59:48 -04:00
parent c9b183a0e0
commit 74d0790ab3
7 changed files with 176 additions and 35 deletions

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;
using ShiftOS.Objects.ShiftFS;
namespace ShiftOS.Frontend.Apps
{
[FileHandler("Skin Loader", ".skn", "")]
public class SkinLoader : IFileHandler
{
public void OpenFile(string file)
{
string skn = Utils.ReadAllText(file);
Utils.WriteAllText(Paths.GetPath("skin.json"), skn);
SkinEngine.LoadSkin();
}
}
}

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Frontend.GraphicsSubsystem;
using ShiftOS.Engine;
using static ShiftOS.Engine.SkinEngine;
namespace ShiftOS.Frontend.GUI
{
public class CheckBox : GUI.Control
{
public CheckBox()
{
Width = 24;
Height = 24;
Click += () =>
{
Checked = !_checked;
};
}
private bool _checked = false;
public bool Checked
{
get
{
return _checked;
}
set
{
if (value == _checked)
return;
_checked = value;
CheckedChanged?.Invoke();
Invalidate();
}
}
protected override void OnPaint(GraphicsContext gfx)
{
gfx.DrawRectangle(0, 0, Width, Height, UIManager.SkinTextures["ControlTextColor"]);
if (_checked)
{
gfx.DrawRectangle(1, 1, Width - 2, Height - 2, UIManager.SkinTextures["ControlTextColor"]);
}
else
{
gfx.DrawRectangle(1, 1, Width - 2, Height - 2, UIManager.SkinTextures["ControlColor"]);
}
}
public event Action CheckedChanged;
}
}

View file

@ -18,6 +18,28 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
private static List<GUI.Control> topLevels = new List<GUI.Control>();
public static System.Drawing.Size Viewport { get; set; }
public static GUI.Control FocusedControl = null;
private static ShiftOS _game = null;
public static void Init(ShiftOS sentience)
{
_game = sentience;
}
public static bool Fullscreen
{
get
{
return _game.graphicsDevice.IsFullScreen;
}
set
{
var uconf = Objects.UserConfig.Get();
uconf.Fullscreen = value;
System.IO.File.WriteAllText("config.json", Newtonsoft.Json.JsonConvert.SerializeObject(uconf, Newtonsoft.Json.Formatting.Indented));
_game.graphicsDevice.IsFullScreen = value;
_game.graphicsDevice.ApplyChanges();
}
}
public static void BringToFront(GUI.Control ctrl)
{

View file

@ -22,6 +22,7 @@ namespace ShiftOS.Frontend
private Button _resUp = new Button();
private TextControl _resDisplay = new TextControl();
private Button _optionsSave = new Button();
private CheckBox _fullscreen = new CheckBox();
public MainMenu()
{
@ -39,6 +40,7 @@ namespace ShiftOS.Frontend
AddControl(_resDown);
AddControl(_resDisplay);
AddControl(_optionsSave);
AddControl(_fullscreen);
_optionsSave.Text = "Save sentience settings";
_optionsSave.Width = (Width / 4) - 60;
@ -63,6 +65,10 @@ namespace ShiftOS.Frontend
_sandbox.Font = _campaign.Font;
_sandbox.X = 30;
_fullscreen.X = 30;
_fullscreen.Y = _campaign.Y;
_options.Text = "Options";
_options.Width = 200;
_options.Height = 6 + _campaign.Font.Height;
@ -138,22 +144,35 @@ namespace ShiftOS.Frontend
}
};
_fullscreen.CheckedChanged += () =>
{
UIManager.Fullscreen = _fullscreen.Checked;
};
_optionsSave.Click += () =>
{
var uconf = Objects.UserConfig.Get();
Engine.Infobox.PromptYesNo("Confirm sentience edit", "Performing this operation requires your sentience to be re-established which may cause you to go unconscious. Do you wish to continue?", (sleep) =>
if (UIManager.Viewport != _screenResolutions[_resIndex])
{
if (sleep == true)
Engine.Infobox.PromptYesNo("Confirm sentience edit", "Performing this operation requires your sentience to be re-established which may cause you to go unconscious. Do you wish to continue?", (sleep) =>
{
var res = _screenResolutions[_resIndex];
uconf.ScreenWidth = res.Width;
uconf.ScreenHeight = res.Height;
System.IO.File.WriteAllText("config.json", Newtonsoft.Json.JsonConvert.SerializeObject(uconf, Newtonsoft.Json.Formatting.Indented));
System.Diagnostics.Process.Start("ShiftOS.Frontend.exe");
Environment.Exit(-1);
}
});
if (sleep == true)
{
SaveOptions();
System.Diagnostics.Process.Start("ShiftOS.Frontend.exe");
Environment.Exit(-1);
}
});
}
else
{
SaveOptions();
_menuTitle.Text = "Main Menu";
_campaign.Visible = true;
_sandbox.Visible = true;
_options.Visible = true;
}
};
foreach(var mode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes.OrderBy(x=>x.Width * x.Height))
@ -162,6 +181,17 @@ namespace ShiftOS.Frontend
if (UIManager.Viewport == _screenResolutions.Last())
_resIndex = _screenResolutions.Count - 1;
}
_fullscreen.Y = _sandbox.Y;
}
public void SaveOptions()
{
var uconf = Objects.UserConfig.Get();
var res = _screenResolutions[_resIndex];
uconf.ScreenWidth = res.Width;
uconf.ScreenHeight = res.Height;
System.IO.File.WriteAllText("config.json", Newtonsoft.Json.JsonConvert.SerializeObject(uconf, Newtonsoft.Json.Formatting.Indented));
}
public void ShowOptions()
@ -272,6 +302,11 @@ namespace ShiftOS.Frontend
_resUp.Visible = (_menuTitle.Text == "Options" && _resIndex < _screenResolutions.Count - 1);
_resDisplay.Visible = _menuTitle.Text == "Options";
_optionsSave.Visible = _resDisplay.Visible;
_fullscreen.Visible = _optionsSave.Visible;
if (_fullscreen.Visible)
{
_fullscreen.Checked = UIManager.Fullscreen;
}
Invalidate();
}

View file

@ -45,6 +45,7 @@
<Compile Include="Apps\CodeShop.cs" />
<Compile Include="Apps\FileSkimmer.cs" />
<Compile Include="Apps\Pong.cs" />
<Compile Include="Apps\SkinLoader.cs" />
<Compile Include="Apps\SystemStatus.cs" />
<Compile Include="Apps\Terminal.cs" />
<Compile Include="Apps\TextPad.cs" />
@ -54,6 +55,7 @@
<Compile Include="GraphicsSubsystem\GraphicsContext.cs" />
<Compile Include="GraphicsSubsystem\UIManager.cs" />
<Compile Include="GUI\Button.cs" />
<Compile Include="GUI\CheckBox.cs" />
<Compile Include="GUI\Control.cs" />
<Compile Include="GUI\ItemGroup.cs" />
<Compile Include="GUI\ListBox.cs" />

View file

@ -16,25 +16,25 @@ namespace ShiftOS.Frontend
/// </summary>
public class ShiftOS : Game
{
GraphicsDeviceManager GraphicsDevice;
internal GraphicsDeviceManager graphicsDevice;
SpriteBatch spriteBatch;
private bool DisplayDebugInfo = false;
public ShiftOS()
{
GraphicsDevice = new GraphicsDeviceManager(this);
graphicsDevice = new GraphicsDeviceManager(this);
var uconf = Objects.UserConfig.Get();
GraphicsDevice.PreferredBackBufferHeight = uconf.ScreenHeight;
GraphicsDevice.PreferredBackBufferWidth = uconf.ScreenWidth;
graphicsDevice.PreferredBackBufferHeight = uconf.ScreenHeight;
graphicsDevice.PreferredBackBufferWidth = uconf.ScreenWidth;
SkinEngine.SkinLoaded += () =>
{
UIManager.ResetSkinTextures(GraphicsDevice.GraphicsDevice);
UIManager.ResetSkinTextures(GraphicsDevice);
UIManager.InvalidateAll();
};
UIManager.Viewport = new System.Drawing.Size(
GraphicsDevice.PreferredBackBufferWidth,
GraphicsDevice.PreferredBackBufferHeight
graphicsDevice.PreferredBackBufferWidth,
graphicsDevice.PreferredBackBufferHeight
);
Content.RootDirectory = "Content";
@ -49,8 +49,8 @@ namespace ShiftOS.Frontend
//Fullscreen
GraphicsDevice.IsFullScreen = false;
graphicsDevice.IsFullScreen = uconf.Fullscreen;
UIManager.Init(this);
}
private Keys lastKey = Keys.None;
@ -67,7 +67,7 @@ namespace ShiftOS.Frontend
OutOfBoxExperience.Init(new MonoGameOOBE());
//Before we do ANYTHING, we've got to initiate the ShiftOS engine.
UIManager.GraphicsDevice = GraphicsDevice.GraphicsDevice;
UIManager.GraphicsDevice = GraphicsDevice;
//Let's get localization going.
Localization.RegisterProvider(new MonoGameLanguageProvider());
@ -119,7 +119,7 @@ namespace ShiftOS.Frontend
// Create a new SpriteBatch, which can be used to draw textures.
this.spriteBatch = new SpriteBatch(base.GraphicsDevice);
UIManager.ResetSkinTextures(GraphicsDevice.GraphicsDevice);
UIManager.ResetSkinTextures(GraphicsDevice);
// TODO: use this.Content to load your game content here
@ -128,7 +128,7 @@ namespace ShiftOS.Frontend
byte[] rgb = new byte[Math.Abs(_lock.Stride) * _lock.Height];
Marshal.Copy(_lock.Scan0, rgb, 0, rgb.Length);
bmp.UnlockBits(_lock);
MouseTexture = new Texture2D(GraphicsDevice.GraphicsDevice, bmp.Width, bmp.Height);
MouseTexture = new Texture2D(GraphicsDevice, bmp.Width, bmp.Height);
MouseTexture.SetData<byte>(rgb);
rgb = null;
}
@ -200,8 +200,7 @@ namespace ShiftOS.Frontend
{
if (lastKey == Keys.F11)
{
GraphicsDevice.IsFullScreen = !GraphicsDevice.IsFullScreen;
GraphicsDevice.ApplyChanges();
UIManager.Fullscreen = !UIManager.Fullscreen;
}
else
{
@ -269,7 +268,7 @@ namespace ShiftOS.Frontend
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
UIManager.DrawControlsToTargets(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0);
UIManager.DrawControlsToTargets(GraphicsDevice, spriteBatch, 0, 0);
var rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
@ -279,7 +278,7 @@ namespace ShiftOS.Frontend
SamplerState.LinearClamp, DepthStencilState.Default,
rasterizerState);
//Draw the desktop BG.
UIManager.DrawBackgroundLayer(GraphicsDevice.GraphicsDevice, spriteBatch, 640, 480);
UIManager.DrawBackgroundLayer(GraphicsDevice, spriteBatch, 640, 480);
//The desktop is drawn, now we can draw the UI.
UIManager.DrawTArgets(spriteBatch);
@ -297,7 +296,7 @@ namespace ShiftOS.Frontend
if (Hacking.CurrentHackable.DoConnectionTimeout)
{
string str = $"Connection TImeout in {(Hacking.CurrentHackable.MillisecondsCountdown / 1000).ToString("#.##")} seconds.";
var gfx = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0, UIManager.Viewport.Width, UIManager.Viewport.Height);
var gfx = new GraphicsContext(GraphicsDevice, spriteBatch, 0, 0, UIManager.Viewport.Width, UIManager.Viewport.Height);
var measure = gfx.MeasureString(str, SkinEngine.LoadedSkin.HeaderFont);
gfx.DrawString(str, 5, (gfx.Height - ((int)measure.Y) - 5), Color.Red, SkinEngine.LoadedSkin.HeaderFont);
}
@ -305,7 +304,7 @@ namespace ShiftOS.Frontend
if (DisplayDebugInfo)
{
var gfxContext = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0, GraphicsDevice.PreferredBackBufferWidth, GraphicsDevice.PreferredBackBufferHeight);
var gfxContext = new GraphicsContext(GraphicsDevice, spriteBatch, 0, 0, graphicsDevice.PreferredBackBufferWidth, graphicsDevice.PreferredBackBufferHeight);
var color = Color.White;
double fps = Math.Round(1 / gameTime.ElapsedGameTime.TotalSeconds);
if (fps <= 20)
@ -333,8 +332,8 @@ Skin texture caches: {UIManager.SkinTextures.Count}
Open windows (excluding dialog boxes): {AppearanceManager.OpenForms.Count}
Experimental effects enabled: {UIManager.ExperimentalEffects}
Fullscreen: {GraphicsDevice.IsFullScreen}
Game resolution: {GraphicsDevice.PreferredBackBufferWidth}x{GraphicsDevice.PreferredBackBufferHeight}
Fullscreen: {UIManager.Fullscreen}
Game resolution: {graphicsDevice.PreferredBackBufferWidth}x{graphicsDevice.PreferredBackBufferHeight}
Mouse state:
X: {LastMouseState.X}

View file

@ -15,12 +15,16 @@ namespace ShiftOS.Objects
public int DigitalSocietyPort { get; set; }
public int ScreenWidth = 1920;
public int ScreenHeight = 1080;
public bool Fullscreen = true;
private static UserConfig def = new UserConfig
{
Language = "english",
DigitalSocietyAddress = "michaeltheshifter.me",
DigitalSocietyPort = 13370
{
Language = "english",
DigitalSocietyAddress = "michaeltheshifter.me",
DigitalSocietyPort = 13370,
Fullscreen = true,
ScreenWidth = 1920,
ScreenHeight = 1080,
};
public static UserConfig current = null;