First stab at a class that abstracts out automatic scaling for sprite-based displays.
(I haven't even been able to test compilation or get IntelliSense for this.)
This commit is contained in:
parent
3a2e9c4d66
commit
2179653494
11
Mixins/ScaledDisplay/ScaledDisplay.projitems
Normal file
11
Mixins/ScaledDisplay/ScaledDisplay.projitems
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||||
|
<HasSharedItems>true</HasSharedItems>
|
||||||
|
<SharedGUID>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</SharedGUID>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="$(MSBuildThisFileDirectory)ScaledGridDisplay.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
105
Mixins/ScaledDisplay/ScaledGridDisplay.cs
Normal file
105
Mixins/ScaledDisplay/ScaledGridDisplay.cs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
public ScaledGridDisplay(IMyTextSurface surface, int rows, int columns)
|
||||||
|
{
|
||||||
|
// 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.3;
|
||||||
|
_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.Scale = _scale * 1.2f;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
sprite.Scale = _scale * 1.1f;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sprite.Scale = _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 _staticSprites.Values) 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.Y = 0f;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
offset.Y = _rowHeight * 1.2f;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
offset.Y = _topPadding + (row * _rowHeight);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return _viewport.Position + offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
Mixins/ScaledDisplay/ScaledGridDisplay.shproj
Normal file
19
Mixins/ScaledDisplay/ScaledGridDisplay.shproj
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<ProjectGuid>8a3cdcc5-4b55-4d87-a415-698a0e1ff06f</ProjectGuid>
|
||||||
|
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
|
||||||
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props"/>
|
||||||
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props"/>
|
||||||
|
<PropertyGroup/>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="ScaledDisplay.projitems" Label="Shared"/>
|
||||||
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets"/>
|
||||||
|
</Project>
|
|
@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InfoDisplays", "InfoDisplay
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockLoader", "DockLoader\DockLoader.csproj", "{A4D9F936-93B2-423A-BD76-A97A084BD605}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockLoader", "DockLoader\DockLoader.csproj", "{A4D9F936-93B2-423A-BD76-A97A084BD605}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{8A3CDCC5-4B55-4D87-A415-698A0E1FF06F}") = "ScaledGridDisplay", "Mixins\ScaledDisplay\ScaledGridDisplay.shproj", "{1C1031E3-B1FE-43AE-91F1-BA74D854B69D}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
Loading…
Reference in New Issue
Block a user