Refactor so apclientpp.h doesn't have to be included a second time

This commit is contained in:
Jerom Venneker
2025-05-29 00:00:13 +02:00
parent 422b7d1950
commit 7d49cb1281
4 changed files with 70 additions and 57 deletions

View File

@@ -134,7 +134,57 @@ bool ArchipelagoClient::StartClient() {
return;
}
ArchipelagoConsole_PrintJson(arg.data);
std::vector<ColoredTextNode> coloredNodes;
for(const APClient::TextNode& node : arg.data) {
APClient* client = apClient.get();
std::string color;
std::string text;
if(node.type == "player_id") {
int id = std::stoi(node.text);
if (color.empty() && id == client->get_player_number()) color = "magenta";
else if(color.empty()) color = "yellow";
text = client->get_player_alias(id);
} else if (node.type == "item_id") {
int64_t id = std::stoll(node.text);
if(color.empty()) {
if (node.flags & APClient::ItemFlags::FLAG_ADVANCEMENT) color = "plum";
else if (node.flags & APClient::ItemFlags::FLAG_NEVER_EXCLUDE) color = "slateblue";
else if (node.flags & APClient::ItemFlags::FLAG_TRAP) color = "salmon";
else color = "cyan";
}
text = client->get_item_name(id, client->get_player_game(node.player));
} else if (node.type == "location_id") {
int64_t id = std::stoll(node.text);
if (color.empty()) color = "blue";
text = client->get_location_name(id, client->get_player_game(node.player));
} else if (node.type == "hint_status") {
text = node.text;
if (node.hintStatus == APClient::HINT_FOUND) color = "green";
else if (node.hintStatus == APClient::HINT_UNSPECIFIED) color = "grey";
else if (node.hintStatus == APClient::HINT_NO_PRIORITY) color = "slateblue";
else if (node.hintStatus == APClient::HINT_AVOID) color = "salmon";
else if (node.hintStatus == APClient::HINT_PRIORITY) color = "plum";
else color = "red"; // unknown status -> red
} else if (node.type == "ERROR") {
color = "ERROR";
text = node.text;
} else if (node.type == "LOG") {
color = "LOG";
text = node.text;
} else {
color = "white";
text = node.text;
}
ColoredTextNode Colornode;
Colornode.color = color;
Colornode.text = text;
coloredNodes.push_back(Colornode);
}
ArchipelagoConsole_PrintJson(coloredNodes);
});
return true;

View File

@@ -27,6 +27,11 @@ class ArchipelagoClient{
uint64_t index;
};
struct ColoredTextNode {
std::string text;
std::string color;
};
static ArchipelagoClient& GetInstance();
bool StartClient();

View File

@@ -4,7 +4,7 @@
#include "soh/SohGui/SohGui.hpp"
#include "soh/OTRGlobals.h"
std::vector<std::list<APClient::TextNode>> Items;
std::vector<std::vector<ArchipelagoClient::ColoredTextNode>> Items;
bool autoScroll = true;
using namespace UIWidgets;
@@ -19,21 +19,20 @@ void ArchipelagoConsole_SendMessage(const char* fmt, bool debugMessage, ...) {
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args);
APClient::TextNode node;
ArchipelagoClient::ColoredTextNode node;
node.text = std::string(buf);
node.color = "white";
if (strstr(buf, "[ERROR]")) {
node.type = "ERROR";
node.color = "ERROR";
} else if (strstr(buf, "[LOG]")) {
node.type = "LOG";
node.color = "LOG";
}
node.text = std::string(buf);
std::list<APClient::TextNode> line;
std::vector<ArchipelagoClient::ColoredTextNode> line;
line.push_back(node);
Items.push_back(line);
}
void ArchipelagoConsole_PrintJson(const std::list<APClient::TextNode> nodes) {
void ArchipelagoConsole_PrintJson(const std::vector<ArchipelagoClient::ColoredTextNode> nodes) {
Items.push_back(nodes);
}
@@ -43,56 +42,15 @@ void ArchipelagoConsoleWindow::DrawElement() {
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.15f, 0.15f, 0.15f, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 8.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(15.0f, 12.0f));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 2.0f));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 1.0f));
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 400), ImGuiChildFlags_AlwaysUseWindowPadding,
ImGuiWindowFlags_HorizontalScrollbar)) {
for (const std::list<APClient::TextNode>& line : Items) {
for(const APClient::TextNode& node : line) {
APClient* client = ArchipelagoClient::GetInstance().apClient.get();
std::string color;
std::string text;
if(node.type == "player_id") {
int id = std::stoi(node.text);
if (color.empty() && id == client->get_player_number()) color = "magenta";
else if(color.empty()) color = "yellow";
text = client->get_player_alias(id);
} else if (node.type == "item_id") {
int64_t id = std::stoll(node.text);
if(color.empty()) {
if (node.flags & APClient::ItemFlags::FLAG_ADVANCEMENT) color = "plum";
else if (node.flags & APClient::ItemFlags::FLAG_NEVER_EXCLUDE) color = "slateblue";
else if (node.flags & APClient::ItemFlags::FLAG_TRAP) color = "salmon";
else color = "cyan";
}
text = client->get_item_name(id, client->get_player_game(node.player));
} else if (node.type == "location_id") {
int64_t id = std::stoll(node.text);
if (color.empty()) color = "blue";
text = client->get_location_name(id, client->get_player_game(node.player));
} else if (node.type == "hint_status") {
text = node.text;
if (node.hintStatus == APClient::HINT_FOUND) color = "green";
else if (node.hintStatus == APClient::HINT_UNSPECIFIED) color = "grey";
else if (node.hintStatus == APClient::HINT_NO_PRIORITY) color = "slateblue";
else if (node.hintStatus == APClient::HINT_AVOID) color = "salmon";
else if (node.hintStatus == APClient::HINT_PRIORITY) color = "plum";
else color = "red"; // unknown status -> red
} else if (node.type == "ERROR") {
color = "ERROR";
text = node.text;
} else if (node.type == "LOG") {
color = "LOG";
text = node.text;
} else {
color = "white";
text = node.text;
}
ImGui::PushStyleColor(ImGuiCol_Text, getColorVal(color));
ImGui::TextUnformatted(text.c_str());
for(const std::vector<ArchipelagoClient::ColoredTextNode>& line : Items) {
for(const ArchipelagoClient::ColoredTextNode& node : line) {
ImGui::PushStyleColor(ImGuiCol_Text, getColorVal(node.color));
ImGui::TextUnformatted(node.text.c_str());
ImGui::SameLine();
ImGui::PopStyleColor();
}
@@ -110,7 +68,7 @@ void ArchipelagoConsoleWindow::DrawElement() {
ImGui::PopStyleVar(3);
};
ImVec4 getColorVal(const std::string& color) {
ImVec4 getColorVal(const std::string& color) { // TODO change color strings to an enum
if (color == "ERROR") {
return ImVec4(1.0f, 0.4f, 0.4f, 1.0f);
} else if(color =="LOG") {

View File

@@ -3,7 +3,7 @@
#define ARCHIPELAGO_CONSOLE_WINDOW_H
#include <libultraship/libultraship.h>
#include <apclient.hpp>
#include "Archipelago.h"
#include <vector>
#include <list>
@@ -19,7 +19,7 @@ class ArchipelagoConsoleWindow final : public Ship::GuiWindow {
};
void ArchipelagoConsole_SendMessage(const char* fmt, bool debugMessage = false, ...);
void ArchipelagoConsole_PrintJson(const std::list<APClient::TextNode> nodes);
void ArchipelagoConsole_PrintJson(const std::vector<ArchipelagoClient::ColoredTextNode> nodes);
ImVec4 getColorVal(const std::string& color);
#endif // ARCHIPELAGO_CONSOLE_WINDOW_H