Skip to content

Commit

Permalink
BaseTool, aremode for huetool, some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Dec 31, 2023
1 parent af9abc0 commit 72959d4
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 145 deletions.
19 changes: 11 additions & 8 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public Tool ActiveTool
StencilEnable = false
};

public int minZ = -127;
public int maxZ = 127;
public int MinZ = -127;
public int MaxZ = 127;

public bool StaticFilterEnabled;
public bool StaticFilterInclusive = true;
Expand Down Expand Up @@ -276,10 +276,10 @@ public static Vector2 ScreenToMapCoordinates(float x, float y)
public Dictionary<int, TileObject> AllTiles = new();
public LandObject?[,] LandTiles;
public int LandTilesCount;
public List<LandObject> GhostLandTiles = new();
public Dictionary<LandObject, LandObject> GhostLandTiles = new();
public List<StaticObject>?[,] StaticTiles;
public int StaticTilesCount;
public List<StaticObject> GhostStaticTiles = new();
public Dictionary<TileObject, StaticObject> GhostStaticTiles = new();
public VirtualLayerObject VirtualLayer = VirtualLayerObject.Instance; //Used for drawing
public VirtualLayerTile? VirtualLayerTile; //Used for selection

Expand Down Expand Up @@ -374,8 +374,9 @@ public void RemoveTiles(ushort x, ushort y)
}
}

public IEnumerable<TileObject> GetTopTiles(TileObject? t1, TileObject? t2, bool landOnly = false)
public IEnumerable<TileObject> GetTopTiles(TileObject? t1, TileObject? t2)
{
var landOnly = ActiveTool.Name == "Draw" && CEDGame.UIManager.TilesWindow.LandMode;
if (t1 == null || t2 == null)
yield break;
var mx = t1.Tile.X < t2.Tile.X ? (t1.Tile.X, t2.Tile.X) : (t2.Tile.X, t1.Tile.X);
Expand Down Expand Up @@ -554,6 +555,8 @@ public void Reset()
{
LandTiles = new LandObject[Client.Width * 8, Client.Height * 8];
StaticTiles = new List<StaticObject>[Client.Width * 8, Client.Height * 8];
GhostLandTiles.Clear();
GhostStaticTiles.Clear();
AllTiles.Clear();
ViewRange = Rectangle.Empty;
Client.ResizeCache(0);
Expand Down Expand Up @@ -671,7 +674,7 @@ private bool CanDrawStatic(ushort id)

private bool WithinZRange(short z)
{
return z >= minZ && z <= maxZ;
return z >= MinZ && z <= MaxZ;
}

private void DrawStatic(StaticObject so, Vector3 hueOverride = default)
Expand Down Expand Up @@ -813,7 +816,7 @@ private void DrawLand()
DrawLand(tile);
}
}
foreach (var tile in GhostLandTiles)
foreach (var tile in GhostLandTiles.Values)
{
DrawLand(tile);
}
Expand Down Expand Up @@ -850,7 +853,7 @@ private void DrawStatics()
}
}
}
foreach (var tile in GhostStaticTiles)
foreach (var tile in GhostStaticTiles.Values)
{
DrawStatic(tile);
}
Expand Down
8 changes: 6 additions & 2 deletions CentrED/Map/StaticObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ public void UpdateHue(ushort newHue)
Vertices[i].HueVec = hueVec;
}
}

private int _ghostHue;

public ushort Hue
public int GhostHue
{
get => _ghostHue;
set
{
_ghostHue = value;
for (var index = 0; index < Vertices.Length; index++)
{
Vertices[index].HueVec = HuesManager.Instance.GetHueVector(Tile.Id, value, Vertices[index].HueVec.Z);
Vertices[index].HueVec = HuesManager.Instance.GetHueVector(Tile.Id, _ghostHue == -1 ? StaticTile.Hue : (ushort)_ghostHue, Vertices[index].HueVec.Z);
}
}
}
Expand Down
103 changes: 103 additions & 0 deletions CentrED/Tools/BaseTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using CentrED.Map;
using Microsoft.Xna.Framework.Input;
using static CentrED.Application;

namespace CentrED.Tools;

public abstract class BaseTool : Tool
{
protected abstract void GhostApply(TileObject? o);
protected abstract void GhostClear(TileObject? o);
protected abstract void Apply(TileObject? o);

protected bool _pressed;
protected bool _areaMode;
private TileObject? _areaStartTile;

public sealed override void OnKeyPressed(Keys key)
{
if (key == Keys.LeftControl && !_pressed)
{
_areaMode = true;
}
}

public sealed override void OnKeyReleased(Keys key)
{
if (key == Keys.LeftControl && !_pressed)
{
_areaMode = false;
}
}

public sealed override void OnMousePressed(TileObject? o)
{
_pressed = true;
if (_areaMode && _areaStartTile == null && o != null)
{
_areaStartTile = o;
}
}

public sealed override void OnMouseReleased(TileObject? o)
{
if(_pressed)
{
if (_areaMode)
{
foreach (var to in CEDGame.MapManager.GetTopTiles(_areaStartTile, o))
{
Apply(to);
GhostClear(to);

}
}
else
{
Apply(o);
GhostClear(o);
}
}

_pressed = false;
_areaStartTile = null;
}


public sealed override void OnMouseEnter(TileObject? o)
{
if (o == null)
return;

if (_areaMode && _pressed)
{
foreach (var to in CEDGame.MapManager.GetTopTiles(_areaStartTile, o))
{
GhostApply(to);
}
}
else
{
GhostApply(o);
}
}

public sealed override void OnMouseLeave(TileObject? o)
{
if (_pressed && !_areaMode)
{
Apply(o);
}
if (_pressed && _areaMode)
{
foreach (var to in CEDGame.MapManager.GetTopTiles(_areaStartTile, o))
{
GhostClear(to);
}
}
else
{
GhostClear(o);
}
}
}
145 changes: 40 additions & 105 deletions CentrED/Tools/DrawTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@

namespace CentrED.Tools;

public class DrawTool : Tool
public class DrawTool : BaseTool
{
private static readonly Random Random = new();
public override string Name => "Draw";
public override Keys Shortcut => Keys.F2;

private bool _pressed;
private bool _rectangular;
private TileObject? _startTile;

[Flags]
enum DrawMode
{
Expand Down Expand Up @@ -69,133 +65,72 @@ public override void OnDeactivated(TileObject? o)
CEDGame.MapManager.ShowVirtualLayer = false;
CEDGame.MapManager.UseVirtualLayer = false;
}

public override void OnKeyPressed(Keys key)
{
if (key == Keys.LeftControl && !_pressed)
{
_rectangular = true;
}
}

public override void OnKeyReleased(Keys key)
private sbyte CalculateNewZ(TileObject o)
{
if (key == Keys.LeftControl && !_pressed)
{
_rectangular = false;
}
var height = o is StaticObject ? TileDataLoader.Instance.StaticData[o.Tile.Id].Height : 0;
return (sbyte)(o.Tile.Z + (_drawMode == (int)DrawMode.ON_TOP ? height : 0));
}

public override void OnMousePressed(TileObject? o)
protected override void GhostApply(TileObject? o)
{
_pressed = true;
if (_rectangular && _startTile == null && o != null)
{
_startTile = o;
}
}

public override void OnMouseReleased(TileObject? o)
{
if(_pressed)
{
Apply(o);
}
_pressed = false;
_startTile = null;
}

public override void OnMouseEnter(TileObject? o)
{
if (o == null)
return;

if (_rectangular && _pressed)
if (o == null || Random.Next(100) > _drawChance) return;
var tilesWindow = CEDGame.UIManager.TilesWindow;
if (tilesWindow.StaticMode)
{
foreach (var to in CEDGame.MapManager.GetTopTiles(_startTile, o, CEDGame.UIManager.TilesWindow.LandMode))
if (o is StaticObject && (DrawMode)_drawMode == DrawMode.REPLACE)
{
AddGhostTile(to);
o.Alpha = 0.3f;
}

var newTile = new StaticTile
(
tilesWindow.ActiveId,
o.Tile.X,
o.Tile.Y,
CalculateNewZ(o),
(ushort)(_withHue ? CEDGame.UIManager.HuesWindow.ActiveId : 0)
);
CEDGame.MapManager.GhostStaticTiles.Add(o, new StaticObject(newTile));
}
else
{
AddGhostTile(o);
}
}

public override void OnMouseLeave(TileObject? o)
{
if (_pressed && !_rectangular)
{
Apply(o);
}
CEDGame.MapManager.GhostStaticTiles.Clear();
var ghostLandTiles = CEDGame.MapManager.GhostLandTiles;
foreach (var ghostLandTile in ghostLandTiles)
else if(o is LandObject lo)
{
var landTile = CEDGame.MapManager.LandTiles[ghostLandTile.Tile.X, ghostLandTile.Tile.Y];
if (landTile != null)
{
landTile.Visible = true;
}
o.Visible = false;
var newTile = new LandTile(tilesWindow.ActiveId, o.Tile.X, o.Tile.Y, o.Tile.Z);
CEDGame.MapManager.GhostLandTiles.Add(lo, new LandObject(newTile));
}
CEDGame.MapManager.GhostLandTiles.Clear();
}

private void Apply(TileObject? o)
protected override void GhostClear(TileObject? o)
{
var mapManager = CEDGame.MapManager;
foreach (var ghostLandTile in mapManager.GhostLandTiles)
if (o != null)
{
var landTile = mapManager.LandTiles[ghostLandTile.Tile.X, ghostLandTile.Tile.Y];
if(landTile == null)
continue;
landTile.Tile.Id = ghostLandTile.Tile.Id;
}
foreach (var ghostStaticTile in mapManager.GhostStaticTiles)
{
var staticTiles = mapManager.StaticTiles[ghostStaticTile.Tile.X, ghostStaticTile.Tile.Y];
if ((DrawMode)_drawMode == DrawMode.REPLACE || staticTiles?.Count == 0)
{
var topTile = staticTiles[^1];
topTile.Tile.Id = ghostStaticTile.Tile.Id;
}
else
o.Alpha = 1f;
o.Visible = true;
CEDGame.MapManager.GhostStaticTiles.Remove(o);
if (o is LandObject lo)
{
CEDClient.Add(ghostStaticTile.StaticTile);
CEDGame.MapManager.GhostLandTiles.Remove(lo);
}
}
}

private void AddGhostTile(TileObject? o)
protected override void Apply(TileObject? o)
{
if (o == null || Random.Next(100) > _drawChance) return;
var tilesWindow = CEDGame.UIManager.TilesWindow;
if (tilesWindow.StaticMode)
if (tilesWindow.StaticMode && o != null)
{
var height = o is StaticObject ? TileDataLoader.Instance.StaticData[o.Tile.Id].Height : 0;
var newZ = o.Tile.Z + (_drawMode == (int)DrawMode.ON_TOP ? height : 0);

if (o is StaticObject && (DrawMode)_drawMode == DrawMode.REPLACE)
var newTile = CEDGame.MapManager.GhostStaticTiles[o];
if ((DrawMode)_drawMode == DrawMode.REPLACE && o is StaticObject so)
{
o.Alpha = 0.3f;
CEDClient.Remove(so.StaticTile);
}

var newTile = new StaticTile
(
tilesWindow.ActiveId,
o.Tile.X,
o.Tile.Y,
(sbyte)newZ,
(ushort)(_withHue ? CEDGame.UIManager.HuesWindow.ActiveId : 0)
);
CEDGame.MapManager.GhostStaticTiles.Add(new StaticObject(newTile));
CEDClient.Add(newTile.StaticTile);
}
else if(o is LandObject)
else if(o is LandObject lo)
{
o.Visible = false;
var newTile = new LandTile(tilesWindow.ActiveId, o.Tile.X, o.Tile.Y, o.Tile.Z);
CEDGame.MapManager.GhostLandTiles.Add(new LandObject(newTile));
var ghostTile = CEDGame.MapManager.GhostLandTiles[lo];
o.Tile.Id = ghostTile.Tile.Id;
}
}
}
Loading

0 comments on commit 72959d4

Please sign in to comment.