HEAPS of desktop stuff

This commit is contained in:
Michael 2017-07-05 10:34:09 -04:00
parent 4f41f51267
commit 181f94e701
4 changed files with 195 additions and 27 deletions

View file

@ -6,11 +6,22 @@ using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;
using ShiftOS.Frontend.GraphicsSubsystem;
using static ShiftOS.Engine.SkinEngine;
namespace ShiftOS.Frontend.Desktop
{
public class Desktop : GUI.Control, IDesktop
{
public Desktop()
{
SaveSystem.GameReady += () =>
{
Show();
SetupDesktop();
};
}
public string DesktopName
{
get
@ -21,12 +32,12 @@ namespace ShiftOS.Frontend.Desktop
public void Close()
{
throw new NotImplementedException();
UIManager.StopHandling(this);
}
public Size GetSize()
{
return new Size(Width, Height);
return UIManager.Viewport;
}
public void HideAppLauncher()
@ -57,10 +68,22 @@ namespace ShiftOS.Frontend.Desktop
public void PopulateAppLauncher(LauncherItem[] items)
{
Invalidate();
}
public void PopulatePanelButtons()
{
PanelButtons.Clear();
foreach(var win in AppearanceManager.OpenForms)
{
var border = win as WindowBorder;
var pbtn = new PanelButtonData();
pbtn.Title = border.Text;
pbtn.Window = border;
PanelButtons.Add(pbtn);
}
Invalidate();
}
public void PushNotification(string app, string title, string message)
@ -73,14 +96,136 @@ namespace ShiftOS.Frontend.Desktop
public void SetupDesktop()
{
Invalidate();
}
public void Show()
{
UIManager.AddTopLevel(this);
Visible = true;
Invalidate();
}
public void ShowWindow(IWindowBorder border)
{
}
protected override void OnLayout()
{
SendToBack();
X = 0;
Y = 0;
Width = GetSize().Width;
Height = GetSize().Height;
Invalidate();
}
private List<PanelButtonData> PanelButtons = new List<PanelButtonData>();
protected override void OnPaint(GraphicsContext gfx)
{
//Let's get data for the desktop panel.
//We need the width and the height and the position.
int dp_height = LoadedSkin.DesktopPanelHeight;
int dp_position = (LoadedSkin.DesktopPanelPosition == 0) ? 0 : Height - dp_height;
int dp_width = Width;
//Alright, now we need to know if we should draw using a texture or a color
if (UIManager.SkinTextures.ContainsKey("desktoppanel"))
{
//Draw with the texture
gfx.DrawRectangle(0, dp_position, dp_width, dp_height, UIManager.SkinTextures["desktoppanel"]);
}
else
{
//draw with a color
var color = LoadedSkin.DesktopPanelColor.ToMonoColor();
gfx.DrawRectangle(0, dp_position, dp_width, dp_height, color);
}
//Alright, now App Launcher.
var al_left = LoadedSkin.AppLauncherFromLeft;
var holderSize = LoadedSkin.AppLauncherHolderSize;
if (UIManager.SkinTextures.ContainsKey("applauncher"))
{
gfx.DrawRectangle(al_left.X, dp_position + al_left.Y, holderSize.Width, holderSize.Height, UIManager.SkinTextures["applauncher"]);
}
//Panel clock.
var panelClockRight = LoadedSkin.DesktopPanelClockFromRight;
var panelClockTextColor = LoadedSkin.DesktopPanelClockColor.ToMonoColor();
var dateTimeString = DateTime.Now.TimeOfDay.ToString();
var measure = gfx.MeasureString(dateTimeString, LoadedSkin.DesktopPanelClockFont);
int panelclockleft = Width - (int)measure.X;
int panelclockwidth = Width - panelclockleft;
if (UIManager.SkinTextures.ContainsKey("panelclockbg"))
{
//draw the background using panelclock texture
gfx.DrawRectangle(panelclockleft, dp_position, panelclockwidth, dp_height, UIManager.SkinTextures["panelclockbg"]);
}
else
{
//draw using the bg color
var pcBGColor = LoadedSkin.DesktopPanelClockBackgroundColor.ToMonoColor();
gfx.DrawRectangle(panelclockleft, dp_position, panelclockwidth, dp_height, pcBGColor);
}
int text_left = (panelclockwidth - (int)measure.X) / 2;
int text_top = (dp_height - (int)measure.Y) / 2;
//draw string
gfx.DrawString(dateTimeString, panelclockleft + text_left, dp_position + text_top, panelClockTextColor, LoadedSkin.DesktopPanelClockFont);
int initialGap = LoadedSkin.PanelButtonHolderFromLeft;
int offset = initialGap;
foreach(var pbtn in PanelButtons)
{
offset += LoadedSkin.PanelButtonFromLeft.X;
int pbtnfromtop = LoadedSkin.PanelButtonFromTop;
int pbtnwidth = LoadedSkin.PanelButtonSize.Width;
int pbtnheight = LoadedSkin.PanelButtonSize.Height;
//Draw panel button background...
if (UIManager.SkinTextures.ContainsKey("panelbutton"))
{
gfx.DrawRectangle(offset, dp_position + pbtnfromtop, pbtnwidth, pbtnheight, UIManager.SkinTextures["panelbutton"]);
}
else
{
gfx.DrawRectangle(offset, dp_position + pbtnfromtop, pbtnwidth, pbtnheight, LoadedSkin.PanelButtonColor.ToMonoColor());
}
//now we draw the text
gfx.DrawString(pbtn.Title, offset + 2, dp_position + pbtnfromtop + 2, LoadedSkin.PanelButtonTextColor.ToMonoColor(), LoadedSkin.PanelButtonFont);
offset += LoadedSkin.PanelButtonSize.Width;
}
}
}
public class PanelButtonData
{
public string Title { get; set; }
public WindowBorder Window { get; set; }
public bool IsActive
{
get
{
return Window.IsFocusedControl || Window.ContainsFocusedControl;
}
}
}
}

View file

@ -367,6 +367,19 @@ namespace ShiftOS.Frontend.GUI
gfx.Clear(Engine.SkinEngine.LoadedSkin.ControlColor.ToMonoColor());
}
public void SendToBack()
{
if(_parent != null)
{
_parent._children.Remove(this);
_parent._children.Insert(0, this);
}
else
{
UIManager.SendToBack(this);
}
}
public void InvalidateTopLevel()
{
var parent = this;

View file

@ -49,12 +49,23 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
if (ctrl.Visible == true)
{
int hc = ctrl.GetHashCode();
if (!TextureCaches.ContainsKey(hc))
{
ctrl.Invalidate();
continue;
}
var _target = TextureCaches[hc];
batch.Draw(_target, new Rectangle(ctrl.X, ctrl.Y, ctrl.Width, ctrl.Height), Color.White);
}
}
}
public static void SendToBack(Control ctrl)
{
topLevels.Remove(ctrl);
topLevels.Insert(0, ctrl);
}
public static void DrawControlsToTargets(GraphicsDevice graphics, SpriteBatch batch, int width, int height)
{
foreach (var ctrl in topLevels.ToArray().Where(x=>x.Visible==true))
@ -80,9 +91,10 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
{
graphics.SetRenderTarget(_target);
graphics.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
batch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
batch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied,
SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullNone);
graphics.Clear(Color.Transparent);
var gfxContext = new GraphicsContext(graphics, batch, 0, 0, _target.Width, _target.Height);
ctrl.Paint(gfxContext);
@ -169,24 +181,9 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
if (SkinEngine.LoadedSkin == null)
SkinEngine.Init();
graphics.Clear(SkinEngine.LoadedSkin.DesktopColor.ToMonoColor());
var desktopbg = SkinEngine.GetImage("desktopbackground");
if(desktopbg != null)
if (SkinTextures.ContainsKey("desktopbackground"))
{
var bmp = (System.Drawing.Bitmap)SkinEngine.GetImage("desktopbackground");
GUI.Control.ResizeImage(bmp, width, height);
var _lock = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var rgb = new byte[Math.Abs(_lock.Stride) * _lock.Height];
Marshal.Copy(_lock.Scan0, rgb, 0, rgb.Length);
bmp.UnlockBits(_lock);
bmp.Dispose();
if(DesktopBackground == null)
{
DesktopBackground = new Texture2D(graphics, width, height);
}
DesktopBackground.SetData<byte>(rgb);
batch.Draw(DesktopBackground, new Rectangle(0, 0, width, height), Color.White);
batch.Draw(SkinTextures["desktopbackground"], new Rectangle(0, 0, Viewport.Width, Viewport.Height), Color.White);
}
}

View file

@ -19,6 +19,8 @@ namespace ShiftOS.Frontend
GraphicsDeviceManager GraphicsDevice;
SpriteBatch spriteBatch;
private bool DisplayDebugInfo = false;
public ShiftOS()
{
GraphicsDevice = new GraphicsDeviceManager(this);
@ -186,8 +188,16 @@ namespace ShiftOS.Frontend
var alt = keystate.IsKeyDown(Keys.LeftAlt) || keystate.IsKeyDown(Keys.RightAlt);
var control = keystate.IsKeyDown(Keys.LeftControl) || keystate.IsKeyDown(Keys.RightControl);
var e = new KeyEvent(control, alt, shift, lastKey);
UIManager.ProcessKeyEvent(e);
if (control && lastKey == Keys.D)
{
DisplayDebugInfo = !DisplayDebugInfo;
}
else
{
var e = new KeyEvent(control, alt, shift, lastKey);
UIManager.ProcessKeyEvent(e);
}
}
}
kb_elapsedms += gameTime.ElapsedGameTime.TotalMilliseconds;
@ -213,7 +223,9 @@ namespace ShiftOS.Frontend
{
UIManager.DrawControlsToTargets(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0);
this.spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied,
SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullNone);
//Draw the desktop BG.
UIManager.DrawBackgroundLayer(GraphicsDevice.GraphicsDevice, spriteBatch, 640, 480);
@ -226,11 +238,12 @@ namespace ShiftOS.Frontend
var mousepos = Mouse.GetState(this.Window).Position;
spriteBatch.Draw(MouseTexture, new Rectangle(mousepos.X, mousepos.Y, MouseTexture.Width, MouseTexture.Height), Color.White);
if (DisplayDebugInfo)
{
var gfxContext = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0, 0, GraphicsDevice.PreferredBackBufferWidth, GraphicsDevice.PreferredBackBufferHeight);
var gfxContext = new GraphicsContext(GraphicsDevice.GraphicsDevice, spriteBatch, 0,0, GraphicsDevice.PreferredBackBufferWidth, GraphicsDevice.PreferredBackBufferHeight);
gfxContext.DrawString("ShiftOS 1.0 Beta 4\r\nCopyright (c) 2017 Michael VanOverbeek, Rylan Arbour, RogueAI\r\nThis is an unstable build.\r\nFPS: " + (1 / gameTime.ElapsedGameTime.TotalSeconds).ToString(), 0, 0, Color.White, new System.Drawing.Font("Lucida Console", 9F, System.Drawing.FontStyle.Bold));
gfxContext.DrawString("ShiftOS 1.0 Beta 4\r\nCopyright (c) 2017 Michael VanOverbeek, Rylan Arbour, RogueAI\r\nThis is an unstable build.\r\nFPS: " + (1 / gameTime.ElapsedGameTime.TotalSeconds).ToString(), 0, 0, Color.White, new System.Drawing.Font("Lucida Console", 9F, System.Drawing.FontStyle.Bold));
}
spriteBatch.End();
base.Draw(gameTime);