[Server] Get rid handwritten ids in GUI API

sol2-server-rewrite
Koncord 7 years ago
parent 4bde7d80f5
commit 54945b537d

@ -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<sol::function>(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<sol::function>(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<sol::function>(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<sol::function>(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<sol::function>(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");

@ -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<int, std::shared_ptr<Window>> windows;
std::unordered_map<int, std::shared_ptr<sol::function>> callbacks;
int lastWindowId;
int lastGuiId; // for message boxes
};
class QuickKey

@ -21,8 +21,7 @@ namespace mwmp
{
DEBUG_PRINTF(strPacketID.c_str());
Networking::get().getState().getEventCtrl().Call<CoreEvent::ON_GUI_ACTION>(player.get(), player->guiMessageBox.id,
player->guiMessageBox.data);
player->getGUI().onGUIAction();
}
};
}

Loading…
Cancel
Save