Skip to content

Commit

Permalink
Static tiles filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Nov 14, 2023
1 parent 39ab200 commit 2572a95
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 25 deletions.
9 changes: 9 additions & 0 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class MapManager
public int minZ = -127;
public int maxZ = 127;

public bool StaticFilterEnabled;
public bool StaticFilterInclusive = true;
public SortedSet<int> StaticFilterIds = new();

public int[] ValidLandIds { get; private set; }
public int[] ValidStaticIds { get; private set; }

Expand Down Expand Up @@ -640,6 +644,11 @@ private bool CanDrawStatic(ushort id)
case 0x21A3:
case 0x21A4: return false;
}

if(StaticFilterEnabled)
{
return !(StaticFilterInclusive ^ StaticFilterIds.Contains(id));
}

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions CentrED/Tools/DrawTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void OnMouseEnter(MapObject? o)
_ => 0
};

var newId = CEDGame.UIManager.TilesWindow._selectedId;
var newId = CEDGame.UIManager.TilesWindow.SelectedId;
if (TilesWindow.IsLandTile(newId))
{
if (o is LandObject lo)
Expand Down Expand Up @@ -85,7 +85,7 @@ public override void OnMouseEnter(MapObject? o)

public override void OnMouseLeave(MapObject? o)
{
if (TilesWindow.IsLandTile(CEDGame.UIManager.TilesWindow._selectedId))
if (TilesWindow.IsLandTile(CEDGame.UIManager.TilesWindow.SelectedId))
{
if (o is LandObject lo)
{
Expand Down Expand Up @@ -113,10 +113,10 @@ public override void OnMouseReleased(MapObject? o)
{
if (_pressed && o == _focusObject)
{
var newId = CEDGame.UIManager.TilesWindow._selectedId;
var newId = CEDGame.UIManager.TilesWindow.SelectedId;
if (TilesWindow.IsLandTile(newId) && o is LandObject lo)
{
lo.LandTile.Id = (ushort)CEDGame.UIManager.TilesWindow._selectedId;
lo.LandTile.Id = (ushort)CEDGame.UIManager.TilesWindow.SelectedId;
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions CentrED/UI/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ private void DrawContextMenu()
HuesWindow.UpdateSelectedHue(so.StaticTile.Hue);
ImGui.CloseCurrentPopup();
}
if (ImGui.Button("Filter TileId"))
{
if(CEDGame.MapManager.StaticFilterIds.Contains(so.Tile.Id))
CEDGame.MapManager.StaticFilterIds.Remove(so.Tile.Id);
else
CEDGame.MapManager.StaticFilterIds.Add(so.Tile.Id);
ImGui.CloseCurrentPopup();
}
}
}
else
Expand Down
124 changes: 122 additions & 2 deletions CentrED/UI/Windows/FilterWindow.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using ImGuiNET;
using ClassicUO.Assets;
using ImGuiNET;
using Microsoft.Xna.Framework;
using static CentrED.Application;
using Vector2 = System.Numerics.Vector2;

namespace CentrED.UI.Windows;

public class FilterWindow : Window
{
public override string Name => "Filter";
private static readonly Vector2 StaticDimensions = new(44, 44);
private float _tableWidth;
internal int SelectedId;

private SortedSet<int> StaticFilterIds => CEDGame.MapManager.StaticFilterIds;

public override void Draw()
{
Expand All @@ -19,7 +27,119 @@ public override void Draw()
ImGui.SameLine();
ImGui.Checkbox("Statics", ref CEDGame.MapManager.IsDrawStatics);
ImGui.SameLine();
ImGui.Checkbox("Shadows", ref CEDGame.MapManager.IsDrawShadows);
ImGui.Checkbox("Shadows", ref CEDGame.MapManager.IsDrawShadows);
ImGui.BeginChild("Filters");
if(ImGui.BeginTabBar("FiltersTabs")){
if (ImGui.BeginTabItem("Statics"))
{
ImGui.Checkbox("Enabled", ref CEDGame.MapManager.StaticFilterEnabled);
ImGui.SameLine();
ImGui.Checkbox("Inclusive", ref CEDGame.MapManager.StaticFilterInclusive);
ImGui.SameLine();
if (ImGui.Button("Clear"))
{
StaticFilterIds.Clear();
}
if (ImGui.BeginTable("TilesTable", 3) && CEDClient.Initialized)
{
unsafe
{
ImGuiListClipperPtr clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper());
ImGui.TableSetupColumn("Id", ImGuiTableColumnFlags.WidthFixed, ImGui.CalcTextSize("0x0000").X);
ImGui.TableSetupColumn("Graphic", ImGuiTableColumnFlags.WidthFixed, StaticDimensions.X);
_tableWidth = ImGui.GetContentRegionAvail().X;
clipper.Begin(StaticFilterIds.Count, StaticDimensions.Y);
while (clipper.Step())
{
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; row++)
{
if(row < StaticFilterIds.Count)
DrawStatic(StaticFilterIds.ElementAt(row));
}
}
clipper.End();
}
ImGui.EndTable();
}
ImGui.EndTabItem();
}
if (ImGui.BeginTabItem("Hues"))
{
ImGui.Text("Not implemented :)");
ImGui.Text("Let me know if you want it to be!");
ImGui.EndTabItem();
}
ImGui.EndTabBar();
}
ImGui.EndChild();
if (ImGui.BeginDragDropTarget())
{
var payloadPtr = ImGui.AcceptDragDropPayload(TilesWindow.Statics_DragDrop_Target_Type);
unsafe
{
if (payloadPtr.NativePtr != null)
{
var dataPtr = (int*)payloadPtr.Data;
int id = dataPtr[0];
StaticFilterIds.Add(id - TilesWindow.MaxLandIndex);
}
}
ImGui.EndDragDropTarget();
}
ImGui.End();
}

private void DrawStatic(int index)
{
var realIndex = index + TilesWindow.MaxLandIndex;
var texture = ArtLoader.Instance.GetStaticTexture((uint)index, out var fakeBounds);
var realBounds = ArtLoader.Instance.GetRealArtBounds(index);
var bounds = new Rectangle
(fakeBounds.X + realBounds.X, fakeBounds.Y + realBounds.Y, realBounds.Width, realBounds.Height);
var name = TileDataLoader.Instance.StaticData[index].Name;
ImGui.TableNextRow(ImGuiTableRowFlags.None, StaticDimensions.Y);
if (ImGui.TableNextColumn())
{
var startPos = ImGui.GetCursorPos();
var selectableSize = new Vector2(_tableWidth, StaticDimensions.Y);
if (ImGui.Selectable
(
$"##tile{realIndex}",
SelectedId == realIndex,
ImGuiSelectableFlags.SpanAllColumns,
selectableSize
))
{
SelectedId = realIndex;
}
if (ImGui.BeginPopupContextItem())
{
if (ImGui.Button("Remove"))
{
StaticFilterIds.Remove(index);
ImGui.CloseCurrentPopup();
}
ImGui.EndPopup();
}
ImGui.SetCursorPos
(
startPos with
{
Y = startPos.Y + (StaticDimensions.Y - ImGui.GetFontSize()) / 2
}
);
ImGui.Text($"0x{index:X4}");
}

if (ImGui.TableNextColumn())
{
CEDGame.UIManager.DrawImage(texture, bounds, StaticDimensions);
}

if (ImGui.TableNextColumn())
{
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + (StaticDimensions.Y - ImGui.GetFontSize()) / 2);
ImGui.TextUnformatted(name);
}
}
}
60 changes: 41 additions & 19 deletions CentrED/UI/Windows/TilesWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public TilesWindow()
public override string Name => "Tiles";

private string _filter = "";
internal int _selectedId;
internal int SelectedId;
private bool _updateScroll;
private bool _landVisible = true;
private bool _staticVisible = true;
private float _tableWidth;
public const int MaxLandIndex = ArtLoader.MAX_LAND_DATA_INDEX_COUNT;
private static readonly Vector2 _tilesDimensions = new(44, 44);
private static readonly Vector2 TilesDimensions = new(44, 44);
public const string Statics_DragDrop_Target_Type = "StaticsDragDrop";

private int[] _matchedLandIds;
private int[] _matchedStaticIds;
Expand Down Expand Up @@ -108,11 +109,11 @@ public override void Draw()
{
ImGuiListClipperPtr clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper());
ImGui.TableSetupColumn("Id", ImGuiTableColumnFlags.WidthFixed, ImGui.CalcTextSize("0x0000").X);
ImGui.TableSetupColumn("Graphic", ImGuiTableColumnFlags.WidthFixed, _tilesDimensions.X);
ImGui.TableSetupColumn("Graphic", ImGuiTableColumnFlags.WidthFixed, TilesDimensions.X);
_tableWidth = ImGui.GetContentRegionAvail().X;
if (_landVisible)
{
clipper.Begin(_matchedLandIds.Length, _tilesDimensions.Y);
clipper.Begin(_matchedLandIds.Length, TilesDimensions.Y);
while (clipper.Step())
{
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; row++)
Expand All @@ -123,16 +124,16 @@ public override void Draw()
clipper.End();
}

if (IsLandTile(_selectedId) && _updateScroll)
if (IsLandTile(SelectedId) && _updateScroll)
{
float itemPosY = clipper.StartPosY + _tilesDimensions.Y * Array.IndexOf
(_matchedLandIds, _selectedId);
float itemPosY = clipper.StartPosY + TilesDimensions.Y * Array.IndexOf
(_matchedLandIds, SelectedId);
ImGui.SetScrollFromPosY(itemPosY - tilesPosY);
_updateScroll = false;
}
if (_staticVisible)
{
clipper.Begin(_matchedStaticIds.Length, _tilesDimensions.Y);
clipper.Begin(_matchedStaticIds.Length, TilesDimensions.Y);
while (clipper.Step())
{
for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; row++)
Expand All @@ -144,8 +145,8 @@ public override void Draw()
}
if (_updateScroll)
{
float itemPosY = clipper.StartPosY + _tilesDimensions.Y * Array.IndexOf
(_matchedStaticIds, _selectedId - MaxLandIndex);
float itemPosY = clipper.StartPosY + TilesDimensions.Y * Array.IndexOf
(_matchedStaticIds, SelectedId - MaxLandIndex);
ImGui.SetScrollFromPosY(itemPosY - tilesPosY);
_updateScroll = false;
}
Expand Down Expand Up @@ -182,37 +183,58 @@ private void TilesDrawStatic(int index)

private void TilesDrawRow(int index, int realIndex, Texture2D texture, Rectangle bounds, string name)
{
ImGui.TableNextRow(ImGuiTableRowFlags.None, _tilesDimensions.Y);
ImGui.TableNextRow(ImGuiTableRowFlags.None, TilesDimensions.Y);
if (ImGui.TableNextColumn())
{
var startPos = ImGui.GetCursorPos();
var selectableSize = new Vector2(_tableWidth, _tilesDimensions.Y);
var selectableSize = new Vector2(_tableWidth, TilesDimensions.Y);
if (ImGui.Selectable
(
$"##tile{realIndex}",
_selectedId == realIndex,
SelectedId == realIndex,
ImGuiSelectableFlags.SpanAllColumns,
selectableSize
))
_selectedId = realIndex;
{
SelectedId = realIndex;
}
if(realIndex > MaxLandIndex && ImGui.BeginPopupContextItem())
{
if (ImGui.Button("Filter"))
{
CEDGame.MapManager.StaticFilterIds.Add(index);
ImGui.CloseCurrentPopup();
}
ImGui.EndPopup();
}
if (realIndex > MaxLandIndex && ImGui.BeginDragDropSource())
{
unsafe
{
ImGui.SetDragDropPayload(Statics_DragDrop_Target_Type, (IntPtr)(&realIndex), sizeof(int));
}
ImGui.Text(name);
CEDGame.UIManager.DrawImage(texture, bounds, TilesDimensions);
ImGui.EndDragDropSource();
}
ImGui.SetCursorPos
(
startPos with
{
Y = startPos.Y + (_tilesDimensions.Y - ImGui.GetFontSize()) / 2
Y = startPos.Y + (TilesDimensions.Y - ImGui.GetFontSize()) / 2
}
);
ImGui.Text($"0x{index:X4}");
}

if (ImGui.TableNextColumn())
{
CEDGame.UIManager.DrawImage(texture, bounds, _tilesDimensions);
CEDGame.UIManager.DrawImage(texture, bounds, TilesDimensions);
}

if (ImGui.TableNextColumn())
{
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + (_tilesDimensions.Y - ImGui.GetFontSize()) / 2);
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + (TilesDimensions.Y - ImGui.GetFontSize()) / 2);
ImGui.TextUnformatted(name);
}
}
Expand All @@ -221,9 +243,9 @@ startPos with

public void UpdateSelectedId(MapObject mapObject)
{
_selectedId = mapObject.Tile.Id;
SelectedId = mapObject.Tile.Id;
if (mapObject is StaticObject)
_selectedId += MaxLandIndex;
SelectedId += MaxLandIndex;
_updateScroll = true;
}
}

0 comments on commit 2572a95

Please sign in to comment.