From a5c15c96f90b7fad1c7c5f0bea513144f033f794 Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 5 Dec 2023 14:12:58 +0000 Subject: [PATCH] Added functions to add/remove custom lines from outside of the minimap widget V2c3f to gamepos --- Dependencies/GWCA | 2 +- .../Widgets/Minimap/CustomRenderer.cpp | 55 +++++++++++++------ GWToolboxdll/Widgets/Minimap/CustomRenderer.h | 34 +++++++----- .../Widgets/Minimap/GameWorldRenderer.cpp | 8 +-- GWToolboxdll/Widgets/Minimap/Minimap.cpp | 2 +- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/Dependencies/GWCA b/Dependencies/GWCA index 1798fae1b..e33b61fe0 160000 --- a/Dependencies/GWCA +++ b/Dependencies/GWCA @@ -1 +1 @@ -Subproject commit 1798fae1b1e50e0476d4149766dfdce4330c9730 +Subproject commit e33b61fe0884685f79adb71ea425d0de959852d6 diff --git a/GWToolboxdll/Widgets/Minimap/CustomRenderer.cpp b/GWToolboxdll/Widgets/Minimap/CustomRenderer.cpp index 9f95df06a..70f522e94 100644 --- a/GWToolboxdll/Widgets/Minimap/CustomRenderer.cpp +++ b/GWToolboxdll/Widgets/Minimap/CustomRenderer.cpp @@ -95,15 +95,16 @@ void CustomRenderer::LoadMarkers() continue; } if (strncmp(section, "customline", "customline"s.length()) == 0) { - lines.push_back(CustomLine(inifile.GetValue(section, "name", "line"))); - lines.back().p1.x = static_cast(inifile.GetDoubleValue(section, "x1", 0.0)); - lines.back().p1.y = static_cast(inifile.GetDoubleValue(section, "y1", 0.0)); - lines.back().p2.x = static_cast(inifile.GetDoubleValue(section, "x2", 0.0)); - lines.back().p2.y = static_cast(inifile.GetDoubleValue(section, "y2", 0.0)); - lines.back().map = static_cast(inifile.GetLongValue(section, "map", 0)); - lines.back().color = Colors::Load(&inifile, section, "color", lines.back().color); - lines.back().visible = inifile.GetBoolValue(section, "visible", true); - lines.back().draw_on_terrain = inifile.GetBoolValue(section, "draw_on_terrain", false); + auto line = new CustomLine(inifile.GetValue(section, "name", "line")); + line->p1.x = static_cast(inifile.GetDoubleValue(section, "x1", 0.0)); + line->p1.y = static_cast(inifile.GetDoubleValue(section, "y1", 0.0)); + line->p2.x = static_cast(inifile.GetDoubleValue(section, "x2", 0.0)); + line->p2.y = static_cast(inifile.GetDoubleValue(section, "y2", 0.0)); + line->map = static_cast(inifile.GetLongValue(section, "map", 0)); + line->color = Colors::Load(&inifile, section, "color", line->color); + line->visible = inifile.GetBoolValue(section, "visible", true); + line->draw_on_terrain = inifile.GetBoolValue(section, "draw_on_terrain", false); + lines.push_back(line); inifile.Delete(section, nullptr); } else if (strncmp(section, "custommarker", "custommarker"s.length()) == 0) { @@ -178,7 +179,7 @@ void CustomRenderer::SaveMarkers() // then save for (auto i = 0u; i < lines.size(); i++) { - const CustomLine& line = lines[i]; + const CustomLine& line = *lines[i]; char section[32]; snprintf(section, 32, "customline%03d", i); inifile.SetValue(section, "name", line.name); @@ -262,6 +263,20 @@ void CustomRenderer::SetTooltipMapID(const GW::Constants::MapID& map_id) ImGui::SetTooltip(map_id_tooltip.tooltip_str); } +bool CustomRenderer::RemoveCustomLine(CustomRenderer::CustomLine* line) { + const auto found = std::ranges::find(lines, line); + if (found != lines.end()) { + lines.erase(found); + return true; + } + return false; +} +CustomRenderer::CustomLine* CustomRenderer::AddCustomLine(const GW::GamePos& from,const GW::GamePos& to) { + const auto line = new CustomRenderer::CustomLine(from.x,from.y,to.x,to.y,GW::Map::GetMapID()); + lines.push_back(line); + return line; +} + void CustomRenderer::DrawLineSettings() { if (Colors::DrawSettingHueWheel("Color", &color)) { @@ -270,7 +285,7 @@ void CustomRenderer::DrawLineSettings() const float spacing = ImGui::GetStyle().ItemInnerSpacing.x; ImGui::PushID("lines"); for (size_t i = 0; i < lines.size(); i++) { - CustomLine& line = lines[i]; + CustomLine& line = *lines[i]; ImGui::PushID(static_cast(i)); markers_changed |= ImGui::Checkbox("##visible", &line.visible); if (ImGui::IsItemHovered()) { @@ -344,7 +359,7 @@ void CustomRenderer::DrawLineSettings() if (ImGui::Button("Add Line")) { char buf[32]; snprintf(buf, 32, "line%zu", lines.size()); - lines.push_back(CustomLine(buf)); + lines.push_back(new CustomLine(buf)); markers_changed = true; } } @@ -636,6 +651,14 @@ void CustomRenderer::Initialize(IDirect3DDevice9* device) printf("Error setting up CustomRenderer vertex buffer: HRESULT: 0x%lX\n", hr); } } +void CustomRenderer::Terminate() +{ + VBuffer::Terminate(); + for (auto l : lines) { + delete l; + } + lines.clear(); +} void CustomRenderer::CustomPolygon::Initialize(IDirect3DDevice9* device) { @@ -879,10 +902,10 @@ void CustomRenderer::DrawCustomMarkers(IDirect3DDevice9* device) void CustomRenderer::DrawCustomLines(const IDirect3DDevice9*) { if (GW::Map::GetInstanceType() == GW::Constants::InstanceType::Explorable) { - for (const CustomLine& line : lines) { - if (line.visible && (line.map == GW::Constants::MapID::None || line.map == GW::Map::GetMapID())) { - EnqueueVertex(line.p1.x, line.p1.y, line.color); - EnqueueVertex(line.p2.x, line.p2.y, line.color); + for (const auto line : lines) { + if (line->visible && (line->map == GW::Constants::MapID::None || line->map == GW::Map::GetMapID())) { + EnqueueVertex(line->p1.x, line->p1.y, line->color); + EnqueueVertex(line->p2.x, line->p2.y, line->color); } } } diff --git a/GWToolboxdll/Widgets/Minimap/CustomRenderer.h b/GWToolboxdll/Widgets/Minimap/CustomRenderer.h index 5be033a50..9b93c8547 100644 --- a/GWToolboxdll/Widgets/Minimap/CustomRenderer.h +++ b/GWToolboxdll/Widgets/Minimap/CustomRenderer.h @@ -25,19 +25,7 @@ namespace mapbox::util { class CustomRenderer : public VBuffer { friend class AgentRenderer; - struct CustomLine { - CustomLine(float x1, float y1, float x2, float y2, GW::Constants::MapID m, const char* n); - explicit CustomLine(const char* n) - : CustomLine(0, 0, 0, 0, static_cast(0), n) { } - GW::Vec2f p1{}; - GW::Vec2f p2{}; - GW::Constants::MapID map{}; - Color color{0xFFFFFFFF}; - bool visible = true; - bool draw_on_terrain = false; - char name[128]{}; - }; enum class Shape { LineCircle, @@ -86,8 +74,23 @@ class CustomRenderer : public VBuffer { }; public: + struct CustomLine { + CustomLine(float x1, float y1, float x2, float y2, GW::Constants::MapID m, const char* n = nullptr); + + explicit CustomLine(const char* n) + : CustomLine(0, 0, 0, 0, static_cast(0), n) { } + GW::Vec2f p1{}; + GW::Vec2f p2{}; + GW::Constants::MapID map{}; + Color color{0xFFFFFFFF}; + bool visible = true; + bool draw_on_terrain = false; + char name[128]{}; + }; + void Render(IDirect3DDevice9* device) override; void Invalidate() override; + void Terminate() override; void DrawSettings(); void DrawLineSettings(); void DrawMarkerSettings(); @@ -96,13 +99,16 @@ class CustomRenderer : public VBuffer { void SaveSettings(ToolboxIni* ini, const char* section); void LoadMarkers(); void SaveMarkers(); + CustomLine* AddCustomLine(const GW::GamePos& from, const GW::GamePos& to); + bool RemoveCustomLine(CustomLine* line); - [[nodiscard]] const std::vector& GetLines() const { return lines; } + [[nodiscard]] const std::vector& GetLines() const { return lines; } [[nodiscard]] const std::vector& GetPolys() const { return polygons; } [[nodiscard]] const std::vector& GetMarkers() const { return markers; } private: void Initialize(IDirect3DDevice9* device) override; + void DrawCustomMarkers(IDirect3DDevice9* device); void DrawCustomLines(const IDirect3DDevice9* device); void EnqueueVertex(float x, float y, Color color); @@ -127,7 +133,7 @@ class CustomRenderer : public VBuffer { int show_polygon_details = -1; bool markers_changed = false; bool marker_file_dirty = true; - std::vector lines{}; + std::vector lines{}; std::vector markers{}; std::vector polygons{}; }; diff --git a/GWToolboxdll/Widgets/Minimap/GameWorldRenderer.cpp b/GWToolboxdll/Widgets/Minimap/GameWorldRenderer.cpp index 967a113fb..155eb6c3d 100644 --- a/GWToolboxdll/Widgets/Minimap/GameWorldRenderer.cpp +++ b/GWToolboxdll/Widgets/Minimap/GameWorldRenderer.cpp @@ -369,12 +369,12 @@ void GameWorldRenderer::SyncLines(IDirect3DDevice9* device) // sync lines with CustomRenderer const auto& lines = Minimap::Instance().custom_renderer.GetLines(); // for each line, add as a renderable if appropriate - for (const auto& line : lines) { - if (!line.draw_on_terrain || !line.visible) { + for (const auto line : lines) { + if (!line->draw_on_terrain || !line->visible) { continue; } - std::vector points = {line.p1, line.p2}; - renderables.push_back(std::make_unique(device, line.map, points, line.color, false)); + std::vector points = {line->p1, line->p2}; + renderables.push_back(std::make_unique(device, line->map, points, line->color, false)); } } diff --git a/GWToolboxdll/Widgets/Minimap/Minimap.cpp b/GWToolboxdll/Widgets/Minimap/Minimap.cpp index e50e1f82e..0c982dfdc 100644 --- a/GWToolboxdll/Widgets/Minimap/Minimap.cpp +++ b/GWToolboxdll/Widgets/Minimap/Minimap.cpp @@ -205,7 +205,7 @@ namespace { if (const auto quest = GW::QuestMgr::GetActiveQuest()) { struct QuestUIMsg { GW::Constants::QuestID quest_id{}; - GW::Vec3f marker{}; + GW::GamePos marker{}; uint32_t h0024{}; GW::Constants::MapID map_to{}; uint32_t log_state{};