Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw Tool improvements: use random tile set flag to let you draw a si… #24

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public Tool ActiveTool
public bool ShowNoDraw = false;
public int VirtualLayerZ;
public bool UseVirtualLayer = false;
public bool UseRandomTileSet = false;
public bool WalkableSurfaces = false;
public bool FlatView = false;
public bool FlatShowHeight = false;
Expand Down Expand Up @@ -130,11 +131,14 @@ public MapManager(GraphicsDevice gd)
}

Client = CEDClient;
Client.LandTileReplaced += (tile, newId) =>
Client.LandTileReplaced += (tile, newId, newZ) =>
{
var landTile = LandTiles[tile.X, tile.Y];
landTile.UpdateCorners(newId);
landTile.UpdateId(newId);
if (landTile != null)
{
landTile.UpdateCorners(newId);
landTile.UpdateId(newId);
}
};
Client.LandTileElevated += (tile, newZ) =>
{
Expand All @@ -155,8 +159,11 @@ public MapManager(GraphicsDevice gd)
continue;

var landObject = LandTiles[newX, newY];
landObject.Vertices[i].Position.Z = newZ * TileObject.TILE_Z_SCALE;
landObject.UpdateId(landObject.LandTile.Id); //Just refresh ID to refresh if it's flat
if (landObject != null)
{
landObject.Vertices[i].Position.Z = newZ * TileObject.TILE_Z_SCALE;
landObject.UpdateId(landObject.LandTile.Id); //Just refresh ID to refresh if it's flat
}
}
};
Client.BlockLoaded += block =>
Expand Down
71 changes: 60 additions & 11 deletions CentrED/Tools/BaseTool.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CentrED.Map;
using CentrED.UI;
using CentrED.UI.Windows;
using Microsoft.Xna.Framework.Input;
using static CentrED.Application;

Expand Down Expand Up @@ -44,27 +45,45 @@ public sealed override void OnMousePressed(TileObject? o)
_pressed = true;
if (_areaMode && _areaStartTile == null && o != null)
{
_areaStartTile = o;
TileObject to = o;
var tilesWindow = UIManager.GetWindow<TilesWindow>();
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && o is VirtualLayerTile)
{
to = CEDGame.MapManager.LandTiles[o.Tile.X, o.Tile.Y];
}
_areaStartTile = to;
}
CEDClient.BeginUndoGroup();
}

public sealed override void OnMouseReleased(TileObject? o)
{
if(_pressed)
var tilesWindow = UIManager.GetWindow<TilesWindow>();

if (_pressed)
{
if (_areaMode)
{
foreach (var to in MapManager.GetTopTiles(_areaStartTile, o))
{
InternalApply(to);
GhostClear(to);
TileObject to2 = to;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
{
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
}
InternalApply(to2);
GhostClear(to2);
}
}
else
{
InternalApply(o);
GhostClear(o);
TileObject to = o;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
{
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
}
InternalApply(to);
GhostClear(to);
}
}
_pressed = false;
Expand All @@ -78,41 +97,71 @@ public sealed override void OnMouseEnter(TileObject? o)
if (o == null)
return;

var tilesWindow = UIManager.GetWindow<TilesWindow>();

if (_areaMode && _pressed)
{
foreach (var to in MapManager.GetTopTiles(_areaStartTile, o))
{
if (Random.Next(100) < _chance)
{
GhostApply(to);
TileObject to2 = to;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
{
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
}
GhostApply(to2);
}
}
}
else
{

if (Random.Next(100) < _chance)
{
GhostApply(o);
TileObject to = o;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
{
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
}
GhostApply(to);
}
}
}

public sealed override void OnMouseLeave(TileObject? o)
{
var tilesWindow = UIManager.GetWindow<TilesWindow>();

if (_pressed && !_areaMode)
{
InternalApply(o);
TileObject to = o;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
{
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
}
InternalApply(to);
}
if (_pressed && _areaMode)
{
foreach (var to in MapManager.GetTopTiles(_areaStartTile, o))
{
GhostClear(to);
TileObject to2 = to;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
{
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
}
GhostClear(to2);
}
}
else
{
GhostClear(o);
TileObject to = o;
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
{
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
}
GhostClear(to);
}
}

Expand Down
64 changes: 56 additions & 8 deletions CentrED/Tools/DrawTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ enum DrawMode
private bool _withHue;
private int _drawMode;
private bool _showVirtualLayer;
private int _randomZ = 0;
private bool _emptyTileOnly;

internal override void Draw()
{
base.Draw();
ImGui.Checkbox("Random Tile Set", ref MapManager.UseRandomTileSet);
ImGui.Checkbox("With Hue", ref _withHue);
ImGui.PushItemWidth(50);
ImGui.PopItemWidth();
Expand All @@ -40,13 +43,19 @@ internal override void Draw()
}
if (ImGui.RadioButton("Virtual Layer", ref _drawMode, (int)DrawMode.VIRTUAL_LAYER))
{
MapManager.UseVirtualLayer = true;
MapManager.UseVirtualLayer = true;
}
if (_drawMode == (int)DrawMode.VIRTUAL_LAYER)
{
ImGui.SameLine();
UIManager.DragInt("", ref MapManager.VirtualLayerZ, 1, -128, 127);
}
if (ImGui.Checkbox("Show VL", ref _showVirtualLayer))
{
MapManager.ShowVirtualLayer = _showVirtualLayer;
}
UIManager.DragInt("Z", ref MapManager.VirtualLayerZ, 1, -128, 127);
}
UIManager.DragInt("Add Random Z", ref _randomZ, 1, 0, 127);
ImGui.Checkbox("Empty tile only", ref _emptyTileOnly);
}

public override void OnActivated(TileObject? o)
Expand All @@ -66,8 +75,23 @@ public override void OnDeactivated(TileObject? o)

private sbyte CalculateNewZ(TileObject o)
{
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));
var height = o.Tile.Z;
if (_drawMode == (int)DrawMode.VIRTUAL_LAYER)
{
height = (sbyte)MapManager.VirtualLayerZ;
}
else if (o is StaticObject && _drawMode == (int)DrawMode.ON_TOP)
{
height += (sbyte)TileDataLoader.Instance.StaticData[o.Tile.Id].Height;
}

if (_randomZ > 0)
{
Random _random = new();
height += (sbyte)_random.Next(0, _randomZ);
}

return height;
}

protected override void GhostApply(TileObject? o)
Expand All @@ -76,6 +100,28 @@ protected override void GhostApply(TileObject? o)
var tilesWindow = UIManager.GetWindow<TilesWindow>();
if (tilesWindow.StaticMode)
{
if (_emptyTileOnly)
{
if (o is StaticObject)
{
return;
}
else if(o is VirtualLayerTile)
{
var staticObjects = MapManager.StaticTiles[o.Tile.X, o.Tile.Y];
if (staticObjects != null)
{
foreach (var so2 in staticObjects)
{
if (so2.StaticTile.Z == o.Tile.Z)
{
return;
}
}
}
}
}

if (o is StaticObject so && (DrawMode)_drawMode == DrawMode.REPLACE)
{
so.Alpha = 0.3f;
Expand All @@ -94,7 +140,7 @@ protected override void GhostApply(TileObject? o)
else if(o is LandObject lo)
{
o.Visible = false;
var newTile = new LandTile(tilesWindow.ActiveId, o.Tile.X, o.Tile.Y, o.Tile.Z);
var newTile = new LandTile(tilesWindow.ActiveId, o.Tile.X, o.Tile.Y, CalculateNewZ(o));
MapManager.GhostLandTiles[lo] = new LandObject(newTile);
}
}
Expand Down Expand Up @@ -130,8 +176,10 @@ protected override void InternalApply(TileObject? o)
{
if(MapManager.GhostLandTiles.TryGetValue(lo, out var ghostTile))
{
o.Tile.Id = ghostTile.Tile.Id;
}
//o.Tile.Id = ghostTile.Tile.Id;
lo.LandTile.ReplaceLand(ghostTile.Tile.Id, ghostTile.Tile.Z);
}

}
}
}
9 changes: 7 additions & 2 deletions CentrED/Tools/ElevateTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum ZMode

private int zMode;
private int value;
private int _randomZ = 0;

internal override void Draw()
{
Expand All @@ -32,12 +33,16 @@ internal override void Draw()
value = -value;
}
UIManager.DragInt("Z", ref value, 1, -128, 127);
if (zMode == (int)ZMode.ADD || zMode == (int)ZMode.SET)
{
UIManager.DragInt("Add Random Z", ref _randomZ, 1, 0, 127);
}
}

private sbyte NewZ(BaseTile tile) => (sbyte)((ZMode)zMode switch
{
ZMode.ADD => tile.Z + value,
ZMode.SET => value,
ZMode.ADD => tile.Z + value + Random.Next(0, _randomZ),
ZMode.SET => value + Random.Next(0, _randomZ),
ZMode.RANDOM => tile.Z + Random.Next(-Math.Abs(value), Math.Abs(value) + 1),
_ => throw new ArgumentOutOfRangeException()
});
Expand Down
40 changes: 38 additions & 2 deletions CentrED/Tools/LandBrushTool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using CentrED.IO;
using CentrED.IO.Models;
using CentrED.Map;
using CentrED.UI;
using CentrED.UI.Windows;
using ImGuiNET;
using Microsoft.Xna.Framework.Input;

namespace CentrED.Tools;
Expand All @@ -11,11 +13,44 @@ public class LandBrushTool : BaseTool
public override string Name => "LandBrush";
public override Keys Shortcut => Keys.F7;

private bool _fixedZ = false;
private int _fixedHeightZ = 0, _randomZ = 0;

internal override void Draw()
{
base.Draw();

ImGui.PopItemWidth();
ImGui.Checkbox("Fixed Z", ref _fixedZ);
if (_fixedZ)
{
ImGui.SameLine();
UIManager.DragInt("", ref _fixedHeightZ, 1, -128, 127);
}
UIManager.DragInt("Add Random Z", ref _randomZ, 1, 0, 127);
}

public override void OnActivated(TileObject? o)
{
UIManager.GetWindow<LandBrushWindow>().Show = true;
}

private sbyte CalculateNewZ(sbyte height)
{
if (_fixedZ)
{
height = (sbyte)_fixedHeightZ;
}

if (_randomZ > 0)
{
Random _random = new();
height += (sbyte)_random.Next(0, _randomZ);
}

return height;
}

protected override void GhostApply(TileObject? o)
{
var defaultTransitionDirection = Direction.Up;
Expand Down Expand Up @@ -181,7 +216,7 @@ private Direction AddTransistion(LandObject lo, Direction direction)
}
else
{
var newTile = new LandTile(newTileId, lo.Tile.X, lo.Tile.Y, lo.Tile.Z);
var newTile = new LandTile(newTileId, lo.Tile.X, lo.Tile.Y, CalculateNewZ(lo.Tile.Z));
MapManager.GhostLandTiles[lo] = new LandObject(newTile);
}
}
Expand Down Expand Up @@ -233,7 +268,8 @@ protected override void InternalApply(TileObject? o)
var tile = MapManager.LandTiles[newX, newY];
if (MapManager.GhostLandTiles.TryGetValue(tile, out var ghostTile))
{
tile.Tile.Id = ghostTile.Tile.Id;
//tile.Tile.Id = ghostTile.Tile.Id;
tile.LandTile.ReplaceLand(ghostTile.Tile.Id, ghostTile.Tile.Z);
}
}
}
Expand Down
Loading
Loading