Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
terminal-cs committed Mar 25, 2023
1 parent 4cbc1fa commit b151080
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 5 deletions.
2 changes: 1 addition & 1 deletion PrismOS/Tests/GraphicsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static GraphicsTests()
Engine = new(800, 600, 75);
Button1 = new(50, 50, 64, 16, 4, "Button1", () => { });
Engine.Objects.Add(Mesh.GetCube(200, 200, 200));
Engine.Camera.Position.Z = 200;
Engine.Camera.Position.Z = -200;
MouseManager.ScreenHeight = Canvas.Height;
MouseManager.ScreenWidth = Canvas.Width;
}
Expand Down
9 changes: 8 additions & 1 deletion PrismUI/Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public override Graphics MainImage
{
get
{
// Get the offset values.
int OffsetX = Window == null ? 0 : Window.X;
int OffsetY = Window == null ? 0 : Window.Y;

// Check if the mouse is within the control.
if (MouseEx.IsMouseWithin(X, Y, Width, Height))
if (MouseEx.IsMouseWithin(OffsetX + X, OffsetY + Y, Width, Height))
{
if (MouseEx.IsClickPressed())
{
Expand Down Expand Up @@ -101,6 +105,9 @@ public override void Render()
ButtonHovering.DrawString(Width / 2, Height / 2, Text, default, Color.Black, true);
ButtonClicked.DrawString(Width / 2, Height / 2, Text, default, Color.Black, true);
ButtonIdle.DrawString(Width / 2, Height / 2, Text, default, Color.Black, true);

// Re-render the parent window.
Window?.Render();
}

public void Dispose()
Expand Down
63 changes: 60 additions & 3 deletions PrismUI/Controls/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,54 @@ public Control(int X, int Y, ushort Width, ushort Height, ushort Radius)
/// </summary>
public abstract Graphics MainImage { get; }

/// <summary>
/// The control's border radius.
/// </summary>
public ushort Radius
{
get
{
return _Radius;
}
set
{
_Radius = value;
Render();
}
}

/// <summary>
/// The control's pixel height.
/// </summary>
public ushort Height
{
get
{
return _Height;
}
set
{
_Height = value;
Render();
}
}

/// <summary>
/// The control's pixel width.
/// </summary>
public ushort Width
{
get
{
return _Width;
}
set
{
_Width = value;
Render();
}
}

#endregion

#region Methods
Expand All @@ -40,10 +88,19 @@ public Control(int X, int Y, ushort Width, ushort Height, ushort Radius)

#region Fields

public ushort Radius;
public ushort Height;
public ushort Width;
internal Window? Window;
private ushort _Radius;
private ushort _Height;
private ushort _Width;

/// <summary>
/// The control's X position.
/// </summary>
public int X;

/// <summary>
/// The control's Y position.
/// </summary>
public int Y;

#endregion
Expand Down
128 changes: 128 additions & 0 deletions PrismUI/Window.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using PrismUI.Controls;
using PrismGraphics;

namespace PrismUI
{
public class Window
{
public Window(int X, int Y, ushort Width, ushort Height)
{
// Initialize the control list.
ShelfControls = new();
Controls = new();

// Initialize the window's buffers.
MainImage = new(Width, Height);
TitleShelf = new(Width, 32);
WindowBody = new(Width, Height);

// Initialize the window fields.
this.Height = Height;
this.Width = Width;
this.X = X;
this.Y = Y;
}

#region Properties

public ushort Height
{
get
{
return _Height;
}
set
{
_Height = value;
Render();
}
}
public ushort Width
{
get
{
return _Width;
}
set
{
_Width = value;
Render();
}
}

#endregion

#region Methods

/// <summary>
/// Removes a control from the window.
/// </summary>
/// <param name="Control">The control to remove.</param>
public void RemoveControl(Control Control)
{
Control.Window = null;
Controls.Remove(Control);
Render();
}

/// <summary>
/// Adds a control to the window.
/// </summary>
/// <param name="Control">The control to add.</param>
public void AddControl(Control Control)
{
Control.Window = this;
Controls.Add(Control);
Render();
}

/// <summary>
/// Renders the window - Only use after changing something.
/// </summary>
public void Render()
{
// Resize if needed.
MainImage.Height = Height;
MainImage.Width = Width;
TitleShelf.Width = Width;
WindowBody.Height = Height;
WindowBody.Width = Width;

// Draw the window back panel.
TitleShelf.Clear(Color.DeepGray);
WindowBody.Clear(Color.White);

// Draw the cache of each control for the title shelf.
foreach (Control C in ShelfControls)
{
TitleShelf.DrawImage(C.X, C.Y, C.MainImage);
}

// Draw the cache of each control.
foreach (Control C in Controls)
{
WindowBody.DrawImage(C.X, C.Y, C.MainImage);
}

// Draw the window to the buffer.
MainImage.DrawImage(X, Y - 32, TitleShelf);
MainImage.DrawImage(X, Y, WindowBody);
}

#endregion

#region Fields

private readonly List<Control> ShelfControls;
private readonly List<Control> Controls;
private readonly Graphics TitleShelf;
private readonly Graphics WindowBody;
public readonly Graphics MainImage;
private ushort _Height;
private ushort _Width;
public int X;
public int Y;

#endregion
}
}

0 comments on commit b151080

Please sign in to comment.