Skip to content

Commit 2e4d875

Browse files
committed
Implement sidebar preview for voxel units
1 parent 1a204c6 commit 2e4d875

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/TSMapEditor/UI/Sidebar/AircraftListPanel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Microsoft.Xna.Framework.Graphics;
23
using Rampastring.XNAUI;
34
using TSMapEditor.Models;
45
using TSMapEditor.Rendering;
@@ -26,9 +27,19 @@ private void AircraftPlacementAction_ActionExited(object sender, EventArgs e)
2627

2728
private readonly AircraftPlacementAction aircraftPlacementAction;
2829

30+
private RenderTarget2D renderTarget;
31+
32+
protected override (Texture2D regular, Texture2D remap) GetObjectTextures<T>(T objectType, ShapeImage[] textures)
33+
{
34+
const byte facingSouthEast = 64;
35+
return GetTextureForVoxel(objectType, TheaterGraphics.AircraftModels, renderTarget, facingSouthEast);
36+
}
37+
2938
protected override void InitObjects()
3039
{
40+
renderTarget = new RenderTarget2D(GraphicsDevice, ObjectTreeView.Width, ObjectTreeView.LineHeight, false, SurfaceFormat.Color, DepthFormat.None);
3141
InitObjectsBase(Map.Rules.AircraftTypes, null);
42+
renderTarget.Dispose();
3243
}
3344

3445
protected override void ObjectSelected()

src/TSMapEditor/UI/Sidebar/ObjectListPanel.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using TSMapEditor.CCEngine;
89
using TSMapEditor.Models;
910
using TSMapEditor.Models.ArtConfig;
1011
using TSMapEditor.Rendering;
11-
using TSMapEditor.Rendering.ObjectRenderers;
1212

1313
namespace TSMapEditor.UI.Sidebar
1414
{
@@ -143,6 +143,28 @@ private void SearchBox_TextChanged(object sender, EventArgs e)
143143

144144
protected abstract void InitObjects();
145145

146+
protected (Texture2D regular, Texture2D remap) GetTextureForVoxel<T>(T objectType, VoxelModel[] typeGraphicsArray, RenderTarget2D renderTarget, byte facing) where T : TechnoType, IArtConfigContainer
147+
{
148+
Renderer.BeginDraw();
149+
150+
var frame = typeGraphicsArray[objectType.Index].GetFrame(facing, RampType.None, false);
151+
if (frame == null || frame.Texture == null)
152+
return (null, null);
153+
154+
var remapFrame = typeGraphicsArray[objectType.Index].GetRemapFrame(facing, RampType.None, false);
155+
156+
Renderer.EndDraw();
157+
158+
// Render them as smaller textures to be independent of the voxel cache
159+
Texture2D regularTexture = Helpers.RenderTextureAsSmaller(frame.Texture, renderTarget, GraphicsDevice);
160+
Texture2D remapTexture = null;
161+
162+
if (remapFrame != null && remapFrame.Texture != null)
163+
remapTexture = Helpers.RenderTextureAsSmaller(remapFrame.Texture, renderTarget, GraphicsDevice);
164+
165+
return (regularTexture, remapTexture);
166+
}
167+
146168
protected virtual (Texture2D regular, Texture2D remap) GetObjectTextures<T>(T objectType, ShapeImage[] textures) where T : TechnoType, IArtConfigContainer
147169
{
148170
Texture2D texture = null;
@@ -218,7 +240,20 @@ protected void InitObjectsBase<T>(List<T> objectTypeList, ShapeImage[] textures,
218240

219241
House ownerHouse = Map.StandardHouses.Find(h => h.ININame == ownerName);
220242
if (ownerHouse != null)
243+
{
221244
remapColor = ownerHouse.XNAColor;
245+
}
246+
else
247+
{
248+
// As as last resort, check if EditorRules has a remap color specified for the side
249+
string colorOverrideName = Map.EditorConfig.EditorRulesIni.GetStringValue("ObjectOwnerColors", ownerName, null);
250+
if (!string.IsNullOrWhiteSpace(colorOverrideName))
251+
{
252+
var rulesColor = Map.Rules.Colors.Find(c => c.Name == colorOverrideName);
253+
if (rulesColor != null)
254+
remapColor = rulesColor.XNAColor;
255+
}
256+
}
222257

223258
// Prevent duplicates that can occur due to category overrides
224259
// (For example, if objects owned by "Soviet1" are overridden to be listed under

src/TSMapEditor/UI/Sidebar/UnitListPanel.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Rampastring.XNAUI;
1+
using Microsoft.Xna.Framework.Graphics;
2+
using Rampastring.XNAUI;
23
using System;
34
using TSMapEditor.Models;
45
using TSMapEditor.Rendering;
@@ -27,6 +28,19 @@ private void UnitPlacementAction_ActionExited(object sender, System.EventArgs e)
2728
private readonly bool isNaval;
2829
private readonly UnitPlacementAction unitPlacementAction;
2930

31+
private RenderTarget2D renderTarget;
32+
33+
protected override (Texture2D regular, Texture2D remap) GetObjectTextures<T>(T objectType, ShapeImage[] textures)
34+
{
35+
const byte facingSouthEast = 64;
36+
37+
var unitType = objectType as UnitType;
38+
if (unitType.ArtConfig.Voxel)
39+
return GetTextureForVoxel(objectType, TheaterGraphics.UnitModels, renderTarget, facingSouthEast);
40+
41+
return base.GetObjectTextures(objectType, textures);
42+
}
43+
3044
protected override void InitObjects()
3145
{
3246
Func<UnitType, bool> filterFunction = null;
@@ -40,7 +54,9 @@ protected override void InitObjects()
4054
filterFunction = u => u.SpeedType != "Float" && u.MovementZone != "Water";
4155
}
4256

57+
renderTarget = new RenderTarget2D(GraphicsDevice, ObjectTreeView.Width, ObjectTreeView.LineHeight, false, SurfaceFormat.Color, DepthFormat.None);
4358
InitObjectsBase(Map.Rules.UnitTypes, TheaterGraphics.UnitTextures, filterFunction);
59+
renderTarget.Dispose();
4460
}
4561

4662
protected override void ObjectSelected()

0 commit comments

Comments
 (0)