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

View File

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