Skip to content

Commit

Permalink
Implement GUI capabilities for controls
Browse files Browse the repository at this point in the history
  • Loading branch information
baratgabor committed Nov 2, 2020
1 parent d4989be commit 07a629d
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 126 deletions.
6 changes: 3 additions & 3 deletions src/GridOS/Core/Common/WordWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public TextSurfaceWordWrapper(IDisplayConfig config)

public float MeasureStringWidthInPixels(string input)
{
return _surface.MeasureStringInPixels(_buffer.Clear().Append(input), _config.FontName, _config.FontSize).X;
return _surface.MeasureStringInPixels(_buffer.Clear().Append(input), _config.BaseFontName, _config.BaseFontSize).X;
}

public IEnumerable<StringSegment> WordWrap(string input, float maxWidthCorrection)
{
var fontSize = _config.FontSize;
var fontName = _config.FontName;
var fontSize = _config.BaseFontSize;
var fontName = _config.BaseFontName;
var lineWidth = _config.OutputWidth + maxWidthCorrection;
var spaceWidth = _surface.MeasureStringInPixels(_buffer.Clear().Append(' '), fontName, fontSize).X;

Expand Down
28 changes: 14 additions & 14 deletions src/GridOS/Core/Configuration/MainConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace IngameScript
class MainConfig : IMenuPresentationConfig, IBreadcrumbConfig, IDisplayConfig
{
public string PathSeparator { get; set; } = "›";
public string SeparatorLineTop { get; set; }
public string SeparatorLineBottom { get; set; }

public char PaddingChar { get; set; } = ' ';
public int PaddingLeft { get; set; } = 0;
Expand All @@ -35,25 +33,27 @@ class MainConfig : IMenuPresentationConfig, IBreadcrumbConfig, IDisplayConfig
Group = '»'
};

private int menuLines;
private string fontName = "Debug";
private float fontSize = StaticConfig.FontSizes[2];
private Color fontColor = StaticConfig.FontColors[0].Color;
private Color backgroundColor = StaticConfig.BackgroundColors[0].Color;
private string baseFontName = StaticConfig.FontTypes.Default;
private float baseFontSize = StaticConfig.FontSizes.Percent100;
private Color baseFontColor = StaticConfig.FontColors.White;
private Color baseBackgroundColor = StaticConfig.BackgroundColors.Cyan;
private float baseLineHeight;
private float baseLineSpacing;

private float outputWidth;
private int outputLineCapacity;
private float outputHeight;

public int MenuLines { get { return menuLines; } set { SetAndNotify(ref menuLines, nameof(MenuLines), value); } }
public string BaseFontName { get { return baseFontName; } set { SetAndNotify(ref baseFontName, nameof(BaseFontName), value); } }
public float BaseFontSize { get { return baseFontSize; } set { SetAndNotify(ref baseFontSize, nameof(BaseFontSize), value); } }
public Color BaseFontColor { get { return baseFontColor; } set { SetAndNotify(ref baseFontColor, nameof(BaseFontColor), value); } }
public Color BaseBackgroundColor { get { return baseBackgroundColor; } set { SetAndNotify(ref baseBackgroundColor, nameof(BaseBackgroundColor), value); } }
public float BaseLineHeight { get { return baseLineHeight; } set { SetAndNotify(ref baseLineHeight, nameof(BaseLineHeight), value); } }
public float BaseLineSpacing { get { return baseLineSpacing; } set { SetAndNotify(ref baseLineSpacing, nameof(BaseLineSpacing), value); } }

public string FontName { get { return fontName; } set { SetAndNotify(ref fontName, nameof(FontName), value); } }
public float FontSize { get { return fontSize; } set { SetAndNotify(ref fontSize, nameof(FontSize), value); } }
public Color FontColor { get { return fontColor; } set { SetAndNotify(ref fontColor, nameof(FontColor), value); } }
public Color BackgroundColor { get { return backgroundColor; } set { SetAndNotify(ref backgroundColor, nameof(BackgroundColor), value); } }
public IMyTextSurface OutputSurface { get; set; }
public float OutputWidth { get { return outputWidth; } set { SetAndNotify(ref outputWidth, nameof(OutputWidth), value); } }
public float OutputHeight { get { return outputHeight; } set { SetAndNotify(ref outputHeight, nameof(OutputHeight), value); } }
public int OutputLineCapacity { get { return outputLineCapacity; } set { SetAndNotify(ref outputLineCapacity, nameof(OutputLineCapacity), value); } }


public event Action<string> SettingChanged;
protected void SetAndNotify<T>(ref T field, string fieldName, T value)
Expand Down
15 changes: 7 additions & 8 deletions src/GridOS/Core/Configuration/_Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ namespace IngameScript
{
public interface IDisplayConfig
{
float FontSize { get; }
string FontName { get;}
float BaseFontSize { get; }
string BaseFontName { get;}

Color FontColor { get; }
Color BackgroundColor { get; }
Color BaseFontColor { get; }
Color BaseBackgroundColor { get; }

float BaseLineHeight { get; }
float BaseLineSpacing { get; }

IMyTextSurface OutputSurface { get; }
float OutputWidth { get; }
float OutputHeight { get; }
int OutputLineCapacity { get; }

event Action<string> SettingChanged;
}

public interface IMenuPresentationConfig
{
int MenuLines { get; }
int PaddingLeft { get; }
char PaddingChar { get; }
char SelectionMarker { get; }
Expand All @@ -35,8 +36,6 @@ public interface IMenuPresentationConfig
public interface IBreadcrumbConfig
{
string PathSeparator { get; }
string SeparatorLineTop { get; }
string SeparatorLineBottom { get; }
int PaddingLeft { get; }
char PaddingChar { get; }
}
Expand Down
28 changes: 11 additions & 17 deletions src/GridOS/Core/DisplaySystem/DisplayControls/Breadcrumb.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
using System.Collections.Generic;
using System.Text;
using System;

namespace IngameScript
{
/// <summary>
/// Creates a formatted string representation of position inside a hierarchical tree.
/// Should receive updated path data through <see cref="OnPathChanged(List{string})"/>.
/// </summary>
class Breadcrumb : IControl
class Breadcrumb : Control
{
public event Action<IControl> RedrawRequired;

protected StringBuilder _buffer = new StringBuilder();
protected string _padding = String.Empty;
protected IBreadcrumbConfig _config;
protected IEnumerable<string> _lastPath;

public Breadcrumb(IBreadcrumbConfig config)
{
_padding = new String(config.PaddingChar, config.PaddingLeft);
_config = config;

TextColor = new Color(255, 255, 235, 120);
BackgroundColor = new Color(0, 0, 0, 80);
FontSize = 0.7f;
PaddingUnit = SizeUnit.Em;
Padding = new Thickness(0.7f, 0.5f, 0, 0.5f);
Width = 100;
WidthUnit = SizeUnit.Percent;
}

public StringBuilder GetContent(bool FlushCache = false)
public override StringBuilder GetContent(bool FlushCache = false)
{
if (FlushCache)
BuildContent(_lastPath);
Expand All @@ -34,19 +37,14 @@ public StringBuilder GetContent(bool FlushCache = false)
public void OnPathChanged(IEnumerable<string> path)
{
BuildContent(path);
RedrawRequired?.Invoke(this);
OnRedrawRequired();
}

protected void BuildContent(IEnumerable<string> path)
{
_lastPath = path;
_buffer.Clear();

_buffer.Append(_config.SeparatorLineTop);
_buffer.AppendLine();

_buffer.Append(_padding);

foreach (var name in path)
{
_buffer.Append(name);
Expand All @@ -56,10 +54,6 @@ protected void BuildContent(IEnumerable<string> path)
}

_buffer.Length -= 3; // Trim trailing path separator.

_buffer.AppendLine();
_buffer.Append(_config.SeparatorLineBottom);
// TODO: Implement path string shortening if it exceeds a certain length (i.e. line length)
}
}
}
54 changes: 54 additions & 0 deletions src/GridOS/Core/DisplaySystem/DisplayControls/Control.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Text;

namespace IngameScript
{
/// <summary>
/// Represents a simple control that defines a rectangle with optional text content.
/// </summary>
abstract class Control : IControl
{
public event Action<IControl> RedrawRequired;

public bool Visible { get; set; } = true;

/// <summary>
/// Specifies how to apply the offset of the control.
/// </summary>
public Positioning Positioning { get; set; }

/// <summary>
/// Offsets the control from its position, depending on the positioning specified.
/// </summary>
public Vector2 Offset { get; set; }

public float Width { get; set; }
public SizeUnit WidthUnit { get; set; }
public float Height { get; set; }
public SizeUnit HeightUnit { get; set; }

public Thickness Margin { get; set; }
public SizeUnit MarginUnit { get; set; }
public Thickness Padding { get; set; }
public SizeUnit PaddingUnit { get; set; }

public SizeUnit BorderUnit { get; set; }
public Thickness Border { get; set; }
public Color BorderColor { get; set; } = Color.Transparent;

public string FontName { get; set; } = null;
public float FontSize { get; set; } = 1f;

public Color? TextColor { get; set; }
public Color? BackgroundColor { get; set; }

public TextAlignment TextAlignment { get; set; } = TextAlignment.LEFT;

public abstract StringBuilder GetContent(bool FlushCache = false);

protected virtual void OnRedrawRequired()
{
RedrawRequired?.Invoke(this);
}
}
}
21 changes: 13 additions & 8 deletions src/GridOS/Core/DisplaySystem/DisplayControls/DisplayHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@

namespace IngameScript
{
class DisplayHeader : IControl
class DisplayHeader : Control
{
protected readonly StringBuilder _buffer = new StringBuilder();
protected readonly ProgressIndicator2 _spinner = new ProgressIndicator2();
protected readonly IDiagnosticService _diagnostics;

public event Action<IControl> RedrawRequired;

public DisplayHeader(IBreadcrumbConfig config, IDiagnosticService diagnostics)
public DisplayHeader(IDiagnosticService diagnostics)
{
_diagnostics = diagnostics;

FontSize = 0.6f;
WidthUnit = SizeUnit.Percent;
Width = 100;
PaddingUnit = SizeUnit.Em;
Padding = new Thickness(1, 0.2f, 0, 0.2f);

TextColor = Color.Black;
BackgroundColor = new Color(255, 255, 255, 90);
}

public StringBuilder GetContent(bool FlushCache = false)
public override StringBuilder GetContent(bool FlushCache = false)
{
_buffer.Clear();
_buffer.Append(" ::GridOS:: ");
_buffer.Append("GridOS ");
_buffer.Append(_spinner.Get());
_buffer.Append(" LRT: ");
_buffer.AppendFormat("{0:G3}ms", _diagnostics.LastRunTimeMs);
return _buffer;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace IngameScript
{
enum Positioning
{
/// <summary>
/// Positions an element relative to the preceding elements.
/// </summary>
Relative,

/// <summary>
/// Positions an element independently of other elements, from 0,0.
/// </summary>
Absolute
}
}
25 changes: 25 additions & 0 deletions src/GridOS/Core/DisplaySystem/DisplayControls/Helpers/SizeUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace IngameScript
{
public enum SizeUnit
{
/// <summary>
/// Relative to the font size of the control, where 1 is 100% of the font size.
/// </summary>
Em,

/// <summary>
/// Literal pixels, where 1 is 1 pixel on the screen.
/// </summary>
Px,

/// <summary>
/// Virtual, density-independent pixels, where values are scaled to the density of the display.
/// </summary>
Dip,

/// <summary>
/// Relative to the window size, where e.g. 50 is half of the size.
/// </summary>
Percent
}
}
79 changes: 79 additions & 0 deletions src/GridOS/Core/DisplaySystem/DisplayControls/Helpers/Thickness.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace IngameScript
{
public struct Thickness
{
public float Left { get; set; }
public float Top { get; set; }
public float Right { get; set; }
public float Bottom { get; set; }

public Thickness(float uniformThickness)
{
Left = uniformThickness;
Top = uniformThickness;
Right = uniformThickness;
Bottom = uniformThickness;
}

public Thickness(float left, float top, float right, float bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}

public static Thickness operator *(Thickness a, float b)
{
return new Thickness()
{
Left = a.Left * b,
Top = a.Top * b,
Right = a.Right * b,
Bottom = a.Bottom * b
};
}

public static Thickness operator /(Thickness a, float b)
{
return new Thickness()
{
Left = a.Left / b,
Top = a.Top / b,
Right = a.Right / b,
Bottom = a.Bottom / b
};
}

public static Thickness operator +(Thickness a, float b)
{
return new Thickness()
{
Left = a.Left + b,
Top = a.Top + b,
Right = a.Right + b,
Bottom = a.Bottom + b
};
}

public static Thickness operator -(Thickness a, float b)
{
return new Thickness()
{
Left = a.Left - b,
Top = a.Top - b,
Right = a.Right - b,
Bottom = a.Bottom - b
};
}

public static Vector2 operator +(Thickness a, Vector2 b)
{
return new Vector2()
{
X = b.X + a.Left + a.Right,
Y = b.Y + a.Top + a.Bottom
};
}
}
}
Loading

0 comments on commit 07a629d

Please sign in to comment.