From 1dc0bc3de293dfd6f04cac950799840dff6f0f5d Mon Sep 17 00:00:00 2001 From: DavideRei <118212274+DavideRei@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:43:22 +0200 Subject: [PATCH 1/3] Added grid mode view to tiles window --- CentrED/UI/UIManager.cs | 4 +- CentrED/UI/Windows/TilesWindow.cs | 139 ++++++++++++++++++++++++++++-- 2 files changed, 135 insertions(+), 8 deletions(-) diff --git a/CentrED/UI/UIManager.cs b/CentrED/UI/UIManager.cs index cf7bbea..58c8f95 100644 --- a/CentrED/UI/UIManager.cs +++ b/CentrED/UI/UIManager.cs @@ -467,7 +467,7 @@ public static void Tooltip(string text) } } - public static bool TwoWaySwitch(string leftLabel, string rightLabel, ref bool value) + public static bool TwoWaySwitch(string leftLabel, string rightLabel, string buttonLabel, ref bool value) { ImGui.Text(leftLabel); ImGui.SameLine(); @@ -475,7 +475,7 @@ public static bool TwoWaySwitch(string leftLabel, string rightLabel, ref bool va var wpos = ImGui.GetCursorScreenPos(); if (value) wpos.X += 40; - var result = ImGui.Button(" ", new Vector2(80, 18)); //Just empty label makes button non functional + var result = ImGui.Button(buttonLabel, new Vector2(80, 18)); //Just empty label makes button non functional if (result) { value = !value; diff --git a/CentrED/UI/Windows/TilesWindow.cs b/CentrED/UI/Windows/TilesWindow.cs index 8528fbd..d995eeb 100644 --- a/CentrED/UI/Windows/TilesWindow.cs +++ b/CentrED/UI/Windows/TilesWindow.cs @@ -12,9 +12,9 @@ namespace CentrED.UI.Windows; public class TilesWindow : Window { - record struct TileInfo(int RealIndex, Texture2D Texture, Rectangle Bounds, string Name) + record struct TileInfo(int RealIndex, Texture2D Texture, Rectangle Bounds, string Name, string flags) { - public static TileInfo INVALID = new(-1, null, default, ""); + public static TileInfo INVALID = new(-1, null, default, "", ""); }; private static readonly Random _random = new(); @@ -40,6 +40,7 @@ public TilesWindow() private static readonly float TotalRowHeight = TilesDimensions.Y + ImGui.GetStyle().ItemSpacing.Y; public const string Static_DragDrop_Target_Type = "StaticDragDrop"; public const string Land_DragDrop_Target_Type = "LandDragDrop"; + private bool gridMode = false; private int[] _matchedLandIds; private int[] _matchedStaticIds; @@ -105,14 +106,28 @@ protected override void InternalDraw() { FilterTiles(); } - if (UIManager.TwoWaySwitch("Land", "Statics", ref staticMode)) + if (UIManager.TwoWaySwitch("Land", "Statics", " ", ref staticMode)) { UpdateScroll = true; _tileSetIndex = 0; ActiveTileSetValues = Empty; _tileSetSelectedId = 0; } - DrawTiles(); + ImGui.SameLine(); + ImGui.Spacing(); + ImGui.SameLine(); + if (UIManager.TwoWaySwitch("List", "Grid", " ", ref gridMode)) + { + UpdateScroll = true; + } + if (gridMode) + { + DrawTilesGridRow(); + } + else + { + DrawTiles(); + } DrawTileSets(); } @@ -197,6 +212,112 @@ private void DrawTiles() ImGui.EndChild(); } + private void DrawTilesGridRow() + { + ImGui.BeginChild("Tiles", new Vector2(), ImGuiChildFlags.Border | ImGuiChildFlags.ResizeY); + _tableWidth = ImGui.GetContentRegionAvail().X; + int columnsNumber = (int)(_tableWidth / (TilesDimensions.X + ImGui.GetStyle().ItemSpacing.X)); + if (columnsNumber < 4) + { + columnsNumber = 4; + } + if (ImGui.BeginTable("TilesTable", columnsNumber) && CEDClient.Initialized) + { + unsafe + { + ImGuiListClipperPtr clipper = new ImGuiListClipperPtr(ImGuiNative.ImGuiListClipper_ImGuiListClipper()); + for (int i = 0; i < columnsNumber; i++) + { + ImGui.TableSetupColumn("Graphic", ImGuiTableColumnFlags.WidthFixed, TilesDimensions.X); + } + _tableWidth = ImGui.GetContentRegionAvail().X; + var ids = LandMode ? _matchedLandIds : _matchedStaticIds; + clipper.Begin(ids.Length / columnsNumber, TotalRowHeight); + while (clipper.Step()) + { + for (int rowIndex = clipper.DisplayStart; rowIndex < clipper.DisplayEnd; rowIndex++) + { + ImGui.TableNextRow(ImGuiTableRowFlags.None, TilesDimensions.Y); + for (int columnIndex = 0; columnIndex < columnsNumber; columnIndex++) + { + if (columnIndex + (columnsNumber * rowIndex) > ids.Length - 1) + { + continue; + } + int tileIndex = ids[columnIndex + (columnsNumber * rowIndex)]; + var tileInfo = LandMode ? LandInfo(tileIndex) : StaticInfo(tileIndex); + if (ImGui.TableNextColumn()) + { + if (tileInfo == TileInfo.INVALID) + { + ImGui.GetWindowDrawList().AddRect(ImGui.GetCursorPos(), TilesDimensions, ImGui.GetColorU32(UIManager.Pink)); + } + else + { + CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions); + } + } + ImGui.SetCursorPosY(ImGui.GetCursorPosY() - TilesDimensions.Y - ImGui.GetStyle().ItemSpacing.Y); + ImGui.SetItemTooltip($"0x{tileIndex:X4}" + "\n" + tileInfo.Name + "\n" + tileInfo.flags); + if (ImGui.Selectable + ( + $"##tile{tileInfo.RealIndex}", + LandMode ? SelectedLandId == tileIndex : SelectedStaticId == tileIndex, + ImGuiSelectableFlags.None, + new Vector2(TilesDimensions.X, TilesDimensions.Y) + )) + { + if (LandMode) + SelectedLandId = tileIndex; + else + SelectedStaticId = tileIndex; + _tileSetSelectedId = 0; + } + if (ImGui.BeginPopupContextItem()) + { + if (_tileSetIndex != 0 && ImGui.Button("Add to set")) + { + AddToTileSet((ushort)tileIndex); + ImGui.CloseCurrentPopup(); + } + if (StaticMode) + { + if (ImGui.Button("Filter")) + { + CEDGame.MapManager.StaticFilterIds.Add(tileIndex); + ImGui.CloseCurrentPopup(); + } + } + ImGui.EndPopup(); + } + if (ImGui.BeginDragDropSource()) + { + ImGui.SetDragDropPayload + ( + LandMode ? Land_DragDrop_Target_Type : Static_DragDrop_Target_Type, + (IntPtr)(&tileIndex), + sizeof(int) + ); + ImGui.Text(tileInfo.Name); + CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions); + ImGui.EndDragDropSource(); + } + } + } + } + clipper.End(); + if (UpdateScroll) + { + float itemPosY = clipper.StartPosY + TotalRowHeight * (Array.IndexOf(ids, LandMode ? SelectedLandId : SelectedStaticId) / columnsNumber); + ImGui.SetScrollFromPosY(itemPosY - ImGui.GetWindowPos().Y); + UpdateScroll = false; + } + } + ImGui.EndTable(); + } + ImGui.EndChild(); + } + private int _tileSetIndex; private string _tileSetName; private ushort _tileSetSelectedId; @@ -385,7 +506,9 @@ private TileInfo LandInfo(int index) } var spriteInfo = CEDGame.MapManager.Arts.GetLand((uint)index); var name = TileDataLoader.Instance.LandData[index].Name; - return new(index, spriteInfo.Texture, spriteInfo.UV, name); + var flags = TileDataLoader.Instance.LandData[index].Flags.ToString(); + + return new(index, spriteInfo.Texture, spriteInfo.UV, name, flags); } private TileInfo StaticInfo(int index) @@ -400,18 +523,22 @@ private TileInfo StaticInfo(int index) var spriteInfo = CEDGame.MapManager.Arts.GetArt((uint)(index + indexEntry.AnimOffset)); var realBounds = CEDGame.MapManager.Arts.GetRealArtBounds((uint)index); var name = TileDataLoader.Instance.StaticData[index].Name; + var flags = TileDataLoader.Instance.StaticData[index].Flags.ToString(); + return new ( realIndex, spriteInfo.Texture, new Rectangle(spriteInfo.UV.X + realBounds.X, spriteInfo.UV.Y + realBounds.Y, realBounds.Width, realBounds.Height), - name + name, + flags ); } private void DrawTileRow(int index, TileInfo tileInfo) { ImGui.TableNextRow(ImGuiTableRowFlags.None, TilesDimensions.Y); + ImGui.SetItemTooltip("Flags: " + tileInfo.flags); if (ImGui.TableNextColumn()) { ImGui.SetCursorPosY From 8381b41cafcdbd4b1ba2c4f2cfe3cfa78df2e4be Mon Sep 17 00:00:00 2001 From: DavideRei <118212274+DavideRei@users.noreply.github.com> Date: Tue, 1 Oct 2024 20:41:22 +0200 Subject: [PATCH 2/3] grid view fix --- CentrED/UI/Windows/TilesWindow.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CentrED/UI/Windows/TilesWindow.cs b/CentrED/UI/Windows/TilesWindow.cs index d995eeb..dabbdc9 100644 --- a/CentrED/UI/Windows/TilesWindow.cs +++ b/CentrED/UI/Windows/TilesWindow.cs @@ -232,7 +232,8 @@ private void DrawTilesGridRow() } _tableWidth = ImGui.GetContentRegionAvail().X; var ids = LandMode ? _matchedLandIds : _matchedStaticIds; - clipper.Begin(ids.Length / columnsNumber, TotalRowHeight); + int rowsNumber = (ids.Length / columnsNumber) + 1; + clipper.Begin(rowsNumber, TotalRowHeight); while (clipper.Step()) { for (int rowIndex = clipper.DisplayStart; rowIndex < clipper.DisplayEnd; rowIndex++) From 38c725d1c036c872ddcbbbd36d228272917cef00 Mon Sep 17 00:00:00 2001 From: DavideRei <118212274+DavideRei@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:08:49 +0200 Subject: [PATCH 3/3] got rid of buttonLabel in TwoWaySwitch --- CentrED/UI/UIManager.cs | 4 ++-- CentrED/UI/Windows/TilesWindow.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CentrED/UI/UIManager.cs b/CentrED/UI/UIManager.cs index 58c8f95..6c9fd4b 100644 --- a/CentrED/UI/UIManager.cs +++ b/CentrED/UI/UIManager.cs @@ -467,7 +467,7 @@ public static void Tooltip(string text) } } - public static bool TwoWaySwitch(string leftLabel, string rightLabel, string buttonLabel, ref bool value) + public static bool TwoWaySwitch(string leftLabel, string rightLabel, ref bool value) { ImGui.Text(leftLabel); ImGui.SameLine(); @@ -475,7 +475,7 @@ public static bool TwoWaySwitch(string leftLabel, string rightLabel, string butt var wpos = ImGui.GetCursorScreenPos(); if (value) wpos.X += 40; - var result = ImGui.Button(buttonLabel, new Vector2(80, 18)); //Just empty label makes button non functional + var result = ImGui.Button($" ##{leftLabel}{rightLabel}", new Vector2(80, 18)); //Just empty label makes button non functional if (result) { value = !value; diff --git a/CentrED/UI/Windows/TilesWindow.cs b/CentrED/UI/Windows/TilesWindow.cs index dabbdc9..9af272f 100644 --- a/CentrED/UI/Windows/TilesWindow.cs +++ b/CentrED/UI/Windows/TilesWindow.cs @@ -106,7 +106,7 @@ protected override void InternalDraw() { FilterTiles(); } - if (UIManager.TwoWaySwitch("Land", "Statics", " ", ref staticMode)) + if (UIManager.TwoWaySwitch("Land", "Statics", ref staticMode)) { UpdateScroll = true; _tileSetIndex = 0; @@ -116,7 +116,7 @@ protected override void InternalDraw() ImGui.SameLine(); ImGui.Spacing(); ImGui.SameLine(); - if (UIManager.TwoWaySwitch("List", "Grid", " ", ref gridMode)) + if (UIManager.TwoWaySwitch("List", "Grid", ref gridMode)) { UpdateScroll = true; }