diff --git a/apps/openmw-mp/GUI.cpp b/apps/openmw-mp/GUI.cpp index 08b6be7a3..647a42b31 100644 --- a/apps/openmw-mp/GUI.cpp +++ b/apps/openmw-mp/GUI.cpp @@ -35,8 +35,33 @@ void GUI::processUpdate() packet->Send(false); } -void GUI::messageBox(int id, const char *label) +int GUI::generateGuiId() { + int id = 0; + + for (const auto &item: callbacks) + { + if (item.second == nullptr) + { + id = item.first; + break; + } + } + + if (id == 0) + id = lastGuiId++; + + return id; +} + +void GUI::messageBox(sol::function fn, const char *label, sol::this_environment te) +{ + if(!fn.valid()) + return; + + int id = generateGuiId(); + callbacks[id] = std::make_shared(fn); + player->guiMessageBox.id = id; player->guiMessageBox.label = label; player->guiMessageBox.type = Player::GUIMessageBox::Type::MessageBox; @@ -44,8 +69,14 @@ void GUI::messageBox(int id, const char *label) setChanged(); } -void GUI::customMessageBox(int id, const char *label, const char *buttons) +void GUI::customMessageBox(sol::function fn, const char *label, const char *buttons, sol::this_environment te) { + if(!fn.valid()) + return; + + int id = generateGuiId(); + callbacks[id] = std::make_shared(fn); + player->guiMessageBox.id = id; player->guiMessageBox.label = label; player->guiMessageBox.buttons = buttons; @@ -54,8 +85,14 @@ void GUI::customMessageBox(int id, const char *label, const char *buttons) setChanged(); } -void GUI::inputDialog(int id, const char *label) +void GUI::inputDialog(sol::function fn, const char *label, sol::this_environment te) { + if(!fn.valid()) + return; + + int id = generateGuiId(); + callbacks[id] = std::make_shared(fn); + player->guiMessageBox.id = id; player->guiMessageBox.label = label; player->guiMessageBox.type = Player::GUIMessageBox::Type::InputDialog; @@ -63,8 +100,14 @@ void GUI::inputDialog(int id, const char *label) setChanged(); } -void GUI::passwordDialog(int id, const char *label, const char *note) +void GUI::passwordDialog(sol::function fn, const char *label, const char *note, sol::this_environment te) { + if(!fn.valid()) + return; + + int id = generateGuiId(); + callbacks[id] = std::make_shared(fn); + player->guiMessageBox.id = id; player->guiMessageBox.label = label; player->guiMessageBox.note = note; @@ -73,8 +116,14 @@ void GUI::passwordDialog(int id, const char *label, const char *note) setChanged(); } -void GUI::listBox(int id, const char *label, const char *items) +void GUI::listBox(sol::function fn, const char *label, const char *items, sol::this_environment te) { + if(!fn.valid()) + return; + + int id = generateGuiId(); + callbacks[id] = std::make_shared(fn); + player->guiMessageBox.id = id; player->guiMessageBox.label = label; player->guiMessageBox.data = items; @@ -83,6 +132,16 @@ void GUI::listBox(int id, const char *label, const char *items) setChanged(); } +void GUI::onGUIAction() +{ + auto it = callbacks.find(player->guiMessageBox.id); + if (it != callbacks.end() && it->second != nullptr) + { + it->second->call(player, player->guiMessageBox.data); + it->second = nullptr; + } +} + void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state) { LOG_MESSAGE(Log::LOG_WARN, "stub"); diff --git a/apps/openmw-mp/GUI.hpp b/apps/openmw-mp/GUI.hpp index 36eb61709..7f48f6bc0 100644 --- a/apps/openmw-mp/GUI.hpp +++ b/apps/openmw-mp/GUI.hpp @@ -17,13 +17,15 @@ public: public: explicit GUI(Player *player); - void messageBox(int id, const char *label); + void messageBox(sol::function fn, const char *label, sol::this_environment te); - void customMessageBox(int id, const char *label, const char *buttons); - void inputDialog(int id, const char *label); - void passwordDialog(int id, const char *label, const char *note); + void customMessageBox(sol::function fn, const char *label, const char *buttons, sol::this_environment te); + void inputDialog(sol::function fn, const char *label, sol::this_environment te); + void passwordDialog(sol::function fn, const char *label, const char *note, sol::this_environment te); - void listBox(int id, const char *label, const char *items); + void listBox(sol::function fn, const char *label, const char *items, sol::this_environment te); + + void onGUIAction(); //state 0 - disallow, 1 - allow void setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state); @@ -38,9 +40,12 @@ public: unsigned int getChanges() const; private: + int generateGuiId(); void processUpdate() final; std::unordered_map> windows; + std::unordered_map> callbacks; int lastWindowId; + int lastGuiId; // for message boxes }; class QuickKey diff --git a/apps/openmw-mp/processors/player/ProcessorGUIMessageBox.hpp b/apps/openmw-mp/processors/player/ProcessorGUIMessageBox.hpp index de7e6bc1e..0f8c3ac19 100644 --- a/apps/openmw-mp/processors/player/ProcessorGUIMessageBox.hpp +++ b/apps/openmw-mp/processors/player/ProcessorGUIMessageBox.hpp @@ -21,8 +21,7 @@ namespace mwmp { DEBUG_PRINTF(strPacketID.c_str()); - Networking::get().getState().getEventCtrl().Call(player.get(), player->guiMessageBox.id, - player->guiMessageBox.data); + player->getGUI().onGUIAction(); } }; }