satdump/src-core/handlers/handler.cpp

189 lines
7.2 KiB
C++
Raw Permalink Normal View History

2024-12-31 17:01:12 +01:00
#include "handler.h"
2025-05-13 09:15:20 +02:00
#include "core/style.h"
2024-12-31 17:01:12 +01:00
#include "imgui/imgui.h"
2025-04-19 12:01:12 +02:00
#include "logger.h"
#include <utility>
2024-12-31 17:01:12 +01:00
namespace satdump
{
namespace handlers
2024-12-31 17:01:12 +01:00
{
2025-01-02 17:04:32 +01:00
inline ImGuiTreeNodeFlags nodeFlags(std::shared_ptr<Handler> &h, bool sec)
{
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_None;
2025-02-07 22:57:27 +01:00
// if (!h->hasSubhandlers())
2025-06-08 18:49:41 +02:00
flags |= ImGuiTreeNodeFlags_Leaf;
2025-01-02 17:04:32 +01:00
if (sec)
flags |= ImGuiTreeNodeFlags_Selected;
return flags;
}
bool Handler::isParent(const std::shared_ptr<Handler> &dragged, const std::shared_ptr<Handler> &potential_child)
{
for (auto &s : dragged->subhandlers)
{
if (s == potential_child)
return true;
2025-03-14 14:51:14 +01:00
if (isParent(s, potential_child))
return true;
}
return false;
}
bool Handler::drawTreeMenu(std::shared_ptr<Handler> &h)
2024-12-31 17:01:12 +01:00
{
bool handler_contained = false;
2024-12-31 17:01:12 +01:00
struct DragDropWip
{
std::shared_ptr<Handler> drag;
2025-05-05 21:03:57 +02:00
Handler *hdel;
2024-12-31 17:01:12 +01:00
};
tree_local.start();
for (int i = 0; i < subhandlers.size(); i++)
{
auto &handler = subhandlers[i];
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
2025-05-13 09:15:20 +02:00
bool tree_extended = ImGui::TreeNodeEx(handler->getTreeID().c_str(), nodeFlags(handler, h == handler));
handler_contained |= h == handler;
2025-05-12 06:35:58 +02:00
tree_local.node(handler->handler_tree_icon);
2025-12-10 20:30:34 +01:00
// Node-specific menu
if (handler->handler_can_be_dragged && ImGui::BeginPopupContextItem())
{
2025-12-11 14:28:36 +01:00
handler->drawContextMenu();
2025-12-10 20:30:34 +01:00
if (ImGui::MenuItem("Delete"))
subhandlers_marked_for_del.push_back(handler);
ImGui::EndPopup();
}
2025-05-13 09:15:20 +02:00
if (tree_extended)
2024-12-31 17:01:12 +01:00
{
2025-08-02 15:43:34 +02:00
if (handler_can_be_selected && ImGui::IsItemClicked())
{
2024-12-31 17:01:12 +01:00
h = handler;
handler_contained = true;
}
2024-12-31 17:01:12 +01:00
2025-04-06 10:27:54 +02:00
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", handler->getName().c_str());
2025-05-05 21:03:57 +02:00
if (handler_can_subhandlers_be_dragged && handler->handler_can_be_dragged && ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
2024-12-31 17:01:12 +01:00
{
2025-05-05 21:03:57 +02:00
DragDropWip d({handler, this});
2025-06-22 17:18:53 +02:00
ImGui::SetDragDropPayload("EXPLORER_HANDLER", &d, sizeof(DragDropWip));
2024-12-31 17:01:12 +01:00
ImGui::Text("%s", handler->getName().c_str());
ImGui::EndDragDropSource();
}
if (handler->handler_can_be_dragged_to && ImGui::BeginDragDropTarget())
2024-12-31 17:01:12 +01:00
{
2025-06-22 17:18:53 +02:00
if (const ImGuiPayload *payload = ImGui::AcceptDragDropPayload("EXPLORER_HANDLER"))
2024-12-31 17:01:12 +01:00
{
IM_ASSERT(payload->DataSize == sizeof(DragDropWip));
const DragDropWip *payload_n = (const DragDropWip *)payload->Data;
2025-05-05 21:03:57 +02:00
logger->info("Handler " + handler->getName() + " got drag of " + payload_n->drag->getName());
if (!isParent(payload_n->drag, handler))
{
handler->addSubHandler(payload_n->drag);
2025-05-05 21:03:57 +02:00
payload_n->hdel->delSubHandler(payload_n->drag);
}
else
{
logger->error("Can't drag parent onto child handler!");
}
2024-12-31 17:01:12 +01:00
}
ImGui::EndDragDropTarget();
}
2025-05-13 09:15:20 +02:00
if (handler_can_be_reorg)
2025-05-12 06:35:58 +02:00
{
2025-05-18 20:37:09 +02:00
float xpos = ImGui::GetWindowWidth();
2025-08-09 12:41:58 +02:00
// float ypos = ImGui::GetCursorPosY();
2025-06-11 20:09:10 +02:00
ImVec4 c = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
c.w = 1;
2025-05-18 20:37:09 +02:00
2025-05-13 09:15:20 +02:00
if (i > 0)
{
2025-08-09 12:41:58 +02:00
// ImGui::GetWindowDrawList()->AddRectFilled({xpos - 32 * ui_scale, ypos + 17 * ui_scale}, //
// {xpos - 10 * ui_scale, ypos + 32 * ui_scale}, //
// (ImU32)((ImColor)c));
2025-06-11 20:09:10 +02:00
2025-05-13 09:15:20 +02:00
ImGui::SameLine();
2025-05-18 20:37:09 +02:00
ImGui::SetCursorPosX(xpos - 35 * ui_scale);
ImGui::Text(u8"\ueaf4");
2025-05-13 09:15:20 +02:00
if (ImGui::IsItemClicked(ImGuiMouseButton_Left))
std::swap(subhandlers[i], subhandlers[i - 1]);
}
2025-05-13 09:15:20 +02:00
if (i != (int)subhandlers.size() - 1)
{
2025-08-09 12:41:58 +02:00
// ImGui::GetWindowDrawList()->AddRectFilled({xpos - 52 * ui_scale, ypos + 17 * ui_scale}, //
// {xpos - 30 * ui_scale, ypos + 32 * ui_scale}, //
// (ImU32)((ImColor)c));
2025-06-11 20:09:10 +02:00
2025-05-13 09:15:20 +02:00
ImGui::SameLine();
2025-05-18 20:37:09 +02:00
ImGui::SetCursorPosX(xpos - 55 * ui_scale);
ImGui::Text(u8"\ueaf3");
2025-05-13 09:15:20 +02:00
if (ImGui::IsItemClicked(ImGuiMouseButton_Left))
std::swap(subhandlers[i], subhandlers[i + 1]);
}
2025-05-12 06:35:58 +02:00
}
2025-08-02 22:13:07 +02:00
handler_contained |= handler->drawTreeMenu(h);
2025-05-13 09:15:20 +02:00
ImGui::TreePop();
2025-05-12 06:35:58 +02:00
}
2024-12-31 17:01:12 +01:00
}
tree_local.end();
// Try handle deletion
2025-02-04 19:49:31 +01:00
delSubHandlersNow();
return handler_contained;
2024-12-31 17:01:12 +01:00
}
bool Handler::hasSubhandlers()
{
subhandlers_mtx.lock();
bool has = subhandlers.size();
subhandlers_mtx.unlock();
return has;
}
void Handler::addSubHandler(std::shared_ptr<Handler> handler, bool ontop)
2024-12-31 17:01:12 +01:00
{
subhandlers_mtx.lock();
if (ontop)
subhandlers.insert(subhandlers.begin(), &handler, &handler + 1);
else
subhandlers.push_back(handler);
2024-12-31 17:01:12 +01:00
subhandlers_mtx.unlock();
}
2025-02-04 19:49:31 +01:00
void Handler::delSubHandler(std::shared_ptr<Handler> handler, bool now)
2024-12-31 17:01:12 +01:00
{
subhandlers_mtx.lock();
subhandlers_marked_for_del.push_back(handler);
subhandlers_mtx.unlock();
2025-02-04 19:49:31 +01:00
if (now)
delSubHandlersNow();
2024-12-31 17:01:12 +01:00
}
2025-07-13 14:40:31 +02:00
std::vector<std::shared_ptr<Handler>> Handler::getAllSubHandlers()
{
std::vector<std::shared_ptr<Handler>> sh;
subhandlers_mtx.lock();
sh = subhandlers;
subhandlers_mtx.unlock();
return sh;
}
2025-04-14 19:03:42 +01:00
nlohmann::json Handler::getConfig() { return {}; }
} // namespace handlers
2025-04-14 19:03:42 +01:00
} // namespace satdump