Skip to content

Commit

Permalink
feat: improve action form logics
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 5, 2024
1 parent 2b1cbea commit 89058cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
14 changes: 1 addition & 13 deletions include/endstone/form/action_form.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,7 @@ class ActionForm : public Form<ActionForm> {

using OnSubmitCallback = std::function<void(Player *, int)>;

explicit ActionForm()
{
on_submit_ = [this](Player *player, int selection) {
if (selection < 0 || selection >= buttons_.size()) {
return;
}
auto &button = buttons_[selection];
auto on_click = button.getOnClick();
if (on_click) {
on_click(player);
}
};
}
explicit ActionForm() = default;

/**
* @brief Get the content of the form.
Expand Down
19 changes: 12 additions & 7 deletions src/endstone_core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,20 +591,25 @@ void EndstonePlayer::onFormResponse(int form_id, const nlohmann::json &json)
try {
std::visit(entt::overloaded{
[&](const MessageForm &form) {
auto callback = form.getOnSubmit();
if (callback) {
if (auto callback = form.getOnSubmit()) {
callback(this, json.get<bool>() ? 0 : 1);
}
},
[&](const ActionForm &form) {
auto callback = form.getOnSubmit();
if (callback) {
callback(this, json.get<int>());
int selection = json.get<int>();
if (auto callback = form.getOnSubmit()) {
callback(this, selection);
}
const auto &buttons = form.getButtons();
if (selection >= 0 && selection < buttons.size()) {
const auto &button = buttons[selection];
if (auto on_click = button.getOnClick()) {
on_click(this);
}
}
},
[&](const ModalForm &form) {
auto callback = form.getOnSubmit();
if (callback) {
if (auto callback = form.getOnSubmit()) {
callback(this, json.dump());
}
},
Expand Down

0 comments on commit 89058cd

Please sign in to comment.