Optimized the menu + bugfix

This commit is contained in:
Ryzerth 2021-07-10 18:33:11 +02:00
parent f86df07c36
commit 6cca4c654f
2 changed files with 18 additions and 18 deletions

View File

@ -27,12 +27,10 @@ void Menu::removeEntry(std::string name) {
bool Menu::draw(bool updateStates) { bool Menu::draw(bool updateStates) {
bool changed = false; bool changed = false;
headerTops.clear();
displayedNames.clear();
float menuWidth = ImGui::GetContentRegionAvailWidth(); float menuWidth = ImGui::GetContentRegionAvailWidth();
ImGuiWindow* window = ImGui::GetCurrentWindow(); ImGuiWindow* window = ImGui::GetCurrentWindow();
int id = 0; int displayedCount = 0;
int rawId = 0; int rawId = 0;
ImU32 textColor = ImGui::GetColorU32(ImGuiCol_Text); ImU32 textColor = ImGui::GetColorU32(ImGuiCol_Text);
@ -49,7 +47,7 @@ bool Menu::draw(bool updateStates) {
continue; continue;
} }
if (id == insertBefore && draggedMenuName != "") { if (displayedCount == insertBefore && !draggedMenuName.empty()) {
if (updateStates) { ImGui::SetNextItemOpen(false); } if (updateStates) { ImGui::SetNextItemOpen(false); }
ImVec2 posMin = ImGui::GetCursorScreenPos(); ImVec2 posMin = ImGui::GetCursorScreenPos();
ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight()); ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight());
@ -68,7 +66,7 @@ bool Menu::draw(bool updateStates) {
style::endDisabled(); style::endDisabled();
window->DrawList->AddRect(posMin, posMax, textColor); window->DrawList->AddRect(posMin, posMax, textColor);
} }
id++; displayedCount++;
MenuItem_t& item = items[opt.name]; MenuItem_t& item = items[opt.name];
@ -81,15 +79,15 @@ bool Menu::draw(bool updateStates) {
ImVec2 posMin = ImGui::GetCursorScreenPos(); ImVec2 posMin = ImGui::GetCursorScreenPos();
ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight()); ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight());
headerTops.push_back(posMin.y); headerTops[displayedCount-1] = posMin.y;
displayedNames.push_back(opt.name); optionIDs[displayedCount-1] = rawId-1;
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) && ImGui::IsMouseHoveringRect(posMin, posMax)) { if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) && ImGui::IsMouseHoveringRect(posMin, posMax)) {
menuClicked = true; menuClicked = true;
clickedMenuName = opt.name; clickedMenuName = opt.name;
} }
if (menuClicked && ImGui::IsMouseDragging(ImGuiMouseButton_Left) && draggedMenuName == "" && clickedMenuName == opt.name) { if (menuClicked && ImGui::IsMouseDragging(ImGuiMouseButton_Left) && draggedMenuName.empty() && clickedMenuName == opt.name) {
draggedMenuName = opt.name; draggedMenuName = opt.name;
draggedId = rawId-1; draggedId = rawId-1;
draggedOpt = opt; draggedOpt = opt;
@ -145,11 +143,11 @@ bool Menu::draw(bool updateStates) {
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left) && menuClicked) { if (!ImGui::IsMouseDown(ImGuiMouseButton_Left) && menuClicked) {
if (draggedMenuName != "") { if (!draggedMenuName.empty()) {
// Move menu // Move menu
order.erase(order.begin() + draggedId); order.erase(order.begin() + draggedId);
if (insertBefore == headerTops.size()) { if (insertBefore == displayedCount) {
order.push_back(draggedOpt); order.push_back(draggedOpt);
} }
else if (insertBeforeName != "") { else if (insertBeforeName != "") {
@ -172,7 +170,7 @@ bool Menu::draw(bool updateStates) {
} }
if (insertBefore == headerTops.size() && draggedMenuName != "") { if (insertBefore == displayedCount && !draggedMenuName.empty()) {
if (updateStates) { ImGui::SetNextItemOpen(false); } if (updateStates) { ImGui::SetNextItemOpen(false); }
ImVec2 posMin = ImGui::GetCursorScreenPos(); ImVec2 posMin = ImGui::GetCursorScreenPos();
ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight()); ImVec2 posMax = ImVec2(posMin.x + menuWidth, posMin.y + ImGui::GetFrameHeight());
@ -192,19 +190,18 @@ bool Menu::draw(bool updateStates) {
window->DrawList->AddRect(posMin, posMax, textColor); window->DrawList->AddRect(posMin, posMax, textColor);
} }
if (draggedMenuName != "") { if (!draggedMenuName.empty()) {
insertBefore = headerTops.size(); insertBefore = displayedCount;
ImVec2 mPos = ImGui::GetMousePos(); ImVec2 mPos = ImGui::GetMousePos();
for (int i = 0; i < headerTops.size(); i++) { for (int i = 0; i < displayedCount; i++) {
if (headerTops[i] > mPos.y) { if (headerTops[i] > mPos.y) {
insertBefore = i; insertBefore = i;
insertBeforeName = displayedNames[i]; insertBeforeName = order[optionIDs[i]].name;
break; break;
} }
} }
} }
return changed; return changed;
} }

View File

@ -4,6 +4,8 @@
#include <map> #include <map>
#include <module.h> #include <module.h>
#define MAX_MENU_COUNT 1024
class Menu { class Menu {
public: public:
Menu(); Menu();
@ -31,13 +33,14 @@ private:
bool menuClicked = false; bool menuClicked = false;
std::string clickedMenuName = ""; std::string clickedMenuName = "";
std::string draggedMenuName = ""; std::string draggedMenuName = "";
std::vector<float> headerTops;
int insertBefore = -1; int insertBefore = -1;
std::string insertBeforeName = ""; std::string insertBeforeName = "";
std::vector<std::string> displayedNames;
int draggedId = 0; int draggedId = 0;
MenuOption_t draggedOpt; MenuOption_t draggedOpt;
std::map<std::string, MenuItem_t> items; std::map<std::string, MenuItem_t> items;
float headerTops[MAX_MENU_COUNT];
int optionIDs[MAX_MENU_COUNT];
}; };