diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-02-11 22:43:23 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-02-11 22:43:23 +0300 |
commit | 121273a1f81b6476f12b32feb5c22e005623b211 (patch) | |
tree | c25cfc04b6b1bc7774fadac8d9748aa54b906e45 /src/gui/widgets | |
parent | 3887b0f4b9ddf987bcb2880ca47d6da0aeab3c54 (diff) | |
download | manaverse-121273a1f81b6476f12b32feb5c22e005623b211.tar.gz manaverse-121273a1f81b6476f12b32feb5c22e005623b211.tar.bz2 manaverse-121273a1f81b6476f12b32feb5c22e005623b211.tar.xz manaverse-121273a1f81b6476f12b32feb5c22e005623b211.zip |
Add support for add action commands inside links.
Diffstat (limited to 'src/gui/widgets')
-rw-r--r-- | src/gui/widgets/itemlinkhandler.cpp | 171 | ||||
-rw-r--r-- | src/gui/widgets/itemlinkhandler.h | 12 |
2 files changed, 119 insertions, 64 deletions
diff --git a/src/gui/widgets/itemlinkhandler.cpp b/src/gui/widgets/itemlinkhandler.cpp index ee2534546..b7ffe5e70 100644 --- a/src/gui/widgets/itemlinkhandler.cpp +++ b/src/gui/widgets/itemlinkhandler.cpp @@ -22,6 +22,8 @@ #include "gui/widgets/itemlinkhandler.h" +#include "logger.h" + #include "itemcolormanager.h" #include "gui/viewport.h" @@ -34,6 +36,8 @@ #include "gui/windows/confirmdialog.h" #include "gui/windows/helpwindow.h" +#include "input/inputmanager.h" + #include "utils/gettext.h" #include "utils/stringutils.h" @@ -57,84 +61,123 @@ ItemLinkHandler::~ItemLinkHandler() { } -void ItemLinkHandler::handleLink(const std::string &link, MouseEvent *event) +void ItemLinkHandler::handleCommandLink(const std::string &link) { - if (strStartWith(link, "http://") || strStartWith(link, "https://")) + std::string cmd; + std::string args; + + if (!parse2Str(link.substr(1), cmd, args)) + { + cmd = link.substr(1); + args.clear(); + } + inputManager.executeRemoteChatCommand(cmd, args, nullptr); +} + +void ItemLinkHandler::handleHelpLink(const std::string &link) +{ + if (helpWindow) + { + helpWindow->loadHelp(link.substr(7)); + helpWindow->requestMoveToTop(); + } +} + +void ItemLinkHandler::handleHttpLink(const std::string &link, + const MouseEvent *const event) +{ + if (!event) + return; + std::string url = link; + replaceAll(url, " ", ""); + listener.url = url; + const MouseButtonT button = event->getButton(); + if (button == MouseButton::LEFT) + { + ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog, + // TRANSLATORS: dialog message + _("Open url"), + url, + SOUND_REQUEST, + false, + Modal_true); + confirmDlg->addActionListener(&listener); + } + else if (button == MouseButton::RIGHT) + { + if (popupMenu) + popupMenu->showLinkPopup(url); + } +} + +void ItemLinkHandler::handleItemLink(const std::string &link) +{ + if (!itemPopup || link.empty()) + return; + + const char ch = link[0]; + if (ch < '0' || ch > '9') + return; + + std::vector<int> str; + splitToIntVector(str, link, ','); + if (str.empty()) + return; + + const int id = str[0]; + + if (id > 0) { - if (!event) - return; - std::string url = link; - replaceAll(url, " ", ""); - listener.url = url; - const MouseButtonT button = event->getButton(); - if (button == MouseButton::LEFT) + str.erase(str.begin()); + while (str.size() < maxCards) + str.push_back(0); + const ItemColor color = + ItemColorManager::getColorFromCards(&str[0]); + + const ItemInfo &itemInfo = ItemDB::get(id); + itemPopup->setItem(itemInfo, color, true, -1, &str[0]); + if (itemPopup->isPopupVisible()) { - ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog, - // TRANSLATORS: dialog message - _("Open url"), - url, - SOUND_REQUEST, - false, - Modal_true); - confirmDlg->addActionListener(&listener); + itemPopup->setVisible(Visible_false); } - else if (button == MouseButton::RIGHT) + else if (viewport) { - if (popupMenu) - popupMenu->showLinkPopup(url); + itemPopup->position(viewport->mMouseX, + viewport->mMouseY); } } +} + +void ItemLinkHandler::handleSearchLink(const std::string &link) +{ + if (helpWindow) + { + helpWindow->search(link.substr(1)); + helpWindow->requestMoveToTop(); + } +} + +void ItemLinkHandler::handleLink(const std::string &link, MouseEvent *event) +{ + logger->log("link: " + link); + if (strStartWith(link, "http://") || strStartWith(link, "https://")) + { + handleHttpLink(link, event); + } else if (!link.empty() && link[0] == '?') { - if (helpWindow) - { - helpWindow->search(link.substr(1)); - helpWindow->requestMoveToTop(); - } + handleSearchLink(link); } else if (strStartWith(link, "help://")) { - if (helpWindow) - { - helpWindow->loadHelp(link.substr(7)); - helpWindow->requestMoveToTop(); - } + handleHelpLink(link); + } + else if (strStartWith(link, "=")) + { + handleCommandLink(link); } else { - if (!itemPopup || link.empty()) - return; - - const char ch = link[0]; - if (ch < '0' || ch > '9') - return; - - std::vector<int> str; - splitToIntVector(str, link, ','); - if (str.empty()) - return; - - const int id = str[0]; - - if (id > 0) - { - str.erase(str.begin()); - while (str.size() < maxCards) - str.push_back(0); - const ItemColor color = - ItemColorManager::getColorFromCards(&str[0]); - - const ItemInfo &itemInfo = ItemDB::get(id); - itemPopup->setItem(itemInfo, color, true, -1, &str[0]); - if (itemPopup->isPopupVisible()) - { - itemPopup->setVisible(Visible_false); - } - else if (viewport) - { - itemPopup->position(viewport->mMouseX, - viewport->mMouseY); - } - } + handleItemLink(link); } } diff --git a/src/gui/widgets/itemlinkhandler.h b/src/gui/widgets/itemlinkhandler.h index 1729f3cf9..25d6dbe41 100644 --- a/src/gui/widgets/itemlinkhandler.h +++ b/src/gui/widgets/itemlinkhandler.h @@ -38,6 +38,18 @@ class ItemLinkHandler final : public LinkHandler void handleLink(const std::string &link, MouseEvent *event) override final; + + private: + static void handleCommandLink(const std::string &link); + + static void handleHelpLink(const std::string &link); + + static void handleHttpLink(const std::string &link, + const MouseEvent *const event); + + static void handleItemLink(const std::string &link); + + static void handleSearchLink(const std::string &link); }; #endif // GUI_WIDGETS_ITEMLINKHANDLER_H |