Skip to content

Commit

Permalink
Added functions to add/remove custom lines from outside of the minima…
Browse files Browse the repository at this point in the history
…p widget

V2c3f to gamepos
  • Loading branch information
3vcloud committed Dec 5, 2023
1 parent b5b2f6e commit a5c15c9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Dependencies/GWCA
Submodule GWCA updated from 1798fa to e33b61
55 changes: 39 additions & 16 deletions GWToolboxdll/Widgets/Minimap/CustomRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(inifile.GetDoubleValue(section, "x1", 0.0));
lines.back().p1.y = static_cast<float>(inifile.GetDoubleValue(section, "y1", 0.0));
lines.back().p2.x = static_cast<float>(inifile.GetDoubleValue(section, "x2", 0.0));
lines.back().p2.y = static_cast<float>(inifile.GetDoubleValue(section, "y2", 0.0));
lines.back().map = static_cast<GW::Constants::MapID>(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<float>(inifile.GetDoubleValue(section, "x1", 0.0));
line->p1.y = static_cast<float>(inifile.GetDoubleValue(section, "y1", 0.0));
line->p2.x = static_cast<float>(inifile.GetDoubleValue(section, "x2", 0.0));
line->p2.y = static_cast<float>(inifile.GetDoubleValue(section, "y2", 0.0));
line->map = static_cast<GW::Constants::MapID>(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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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<int>(i));
markers_changed |= ImGui::Checkbox("##visible", &line.visible);
if (ImGui::IsItemHovered()) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions GWToolboxdll/Widgets/Minimap/CustomRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GW::Constants::MapID>(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,
Expand Down Expand Up @@ -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<GW::Constants::MapID>(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();
Expand All @@ -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<CustomLine>& GetLines() const { return lines; }
[[nodiscard]] const std::vector<CustomLine*>& GetLines() const { return lines; }
[[nodiscard]] const std::vector<CustomPolygon>& GetPolys() const { return polygons; }
[[nodiscard]] const std::vector<CustomMarker>& 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);
Expand All @@ -127,7 +133,7 @@ class CustomRenderer : public VBuffer {
int show_polygon_details = -1;
bool markers_changed = false;
bool marker_file_dirty = true;
std::vector<CustomLine> lines{};
std::vector<CustomLine*> lines{};
std::vector<CustomMarker> markers{};
std::vector<CustomPolygon> polygons{};
};
8 changes: 4 additions & 4 deletions GWToolboxdll/Widgets/Minimap/GameWorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenericPolyRenderable>(device, line.map, points, line.color, false));
std::vector points = {line->p1, line->p2};
renderables.push_back(std::make_unique<GenericPolyRenderable>(device, line->map, points, line->color, false));
}
}

Expand Down
2 changes: 1 addition & 1 deletion GWToolboxdll/Widgets/Minimap/Minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down

0 comments on commit a5c15c9

Please sign in to comment.