space_engineers/Mixins/ScaledGridDisplay/ScaledGridDisplay.cs

115 lines
4.0 KiB
C#

using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using VRage.Game.GUI.TextPanel;
using VRageMath;
namespace IngameScript
{
partial class Program
{
public class ScaledGridDisplay
{
private IMyTextSurface _surface;
private List<MySprite> _sprites = new List<MySprite>();
private RectangleF _viewport;
private float _scale;
private float _topPadding;
private int _rows;
private float _rowHeight;
private const float LeftPadding = 2f;
private int _columns;
private float _columnWidth;
private IConsole _console;
public ScaledGridDisplay(IMyTextSurface surface, int rows, int columns, IConsole console)
{
_console = new PrefixedConsole(console, "ScaledGridDisplay");
// Setup draw surface
_surface = surface;
_surface.ScriptBackgroundColor = new Color(0, 0, 0, 255);
_surface.ContentType = ContentType.SCRIPT;
_surface.Script = "";
// configure viewport / scaling
_viewport = new RectangleF(
(_surface.TextureSize - _surface.SurfaceSize) / 2f,
_surface.SurfaceSize
);
_scale = _viewport.Size.Y / 400;
// Calculate grid geometry
_rows = rows;
_columns = columns;
_topPadding = _viewport.Size.Y / rows * 0.3f;
_rowHeight = (_viewport.Size.Y - _topPadding) / rows;
_columnWidth = (_viewport.Size.X - LeftPadding) / columns;
}
public void AddSprite(MySprite sprite, int row, int column, TextAlignment alignment)
{
// TODO: figure out how coordinates should work...
// Shift the sprite into the viewable portion of the surface
sprite.Position = getPosition(row, column, alignment);
switch (sprite.Type)
{
case SpriteType.TEXT:
switch (row)
{
case 0:
sprite.RotationOrScale = _scale * 1.2f;
break;
case 1:
sprite.RotationOrScale = _scale * 1.1f;
break;
default:
sprite.RotationOrScale = _scale;
break;
}
break;
case SpriteType.TEXTURE:
sprite.Size *= _scale;
break;
default:
_console.Print("Unknown sprite type. How did you manage that?");
break;
}
_sprites.Add(sprite);
}
public void Draw()
{
MySpriteDrawFrame frame = _surface.DrawFrame();
foreach (MySprite sprite in _sprites) frame.Add(sprite);
frame.Dispose();
}
private Vector2 getPosition(int row, int column, TextAlignment alignment)
{
Vector2 offset = new Vector2();
offset.X = LeftPadding + (column * _columnWidth);
switch (row)
{
case 0:
offset.X = _viewport.Size.X / 2f;
offset.Y = 0f;
break;
case 1:
offset.Y = _rowHeight * 1.2f;
break;
default:
offset.Y = _topPadding + (row * _rowHeight);
break;
}
return _viewport.Position + offset;
}
}
}
}