// // Created by koncord on 20.07.16. // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "GUIController.hpp" #include "Main.hpp" #include "PlayerMarkerCollection.hpp" mwmp::GUIController::GUIController(): mInputBox(0) { mChat = nullptr; keySay = SDLK_y; keyChatMode = SDLK_F2; calledMessageBox = false; } mwmp::GUIController::~GUIController() { } void mwmp::GUIController::cleanup() { mPlayerMarkers.clear(); if (mChat != nullptr) delete mChat; mChat = nullptr; } void mwmp::GUIController::setupChat(const Settings::Manager &mgr) { assert(mChat == nullptr); float chatDelay = mgr.getFloat("delay", "Chat"); int chatY = mgr.getInt("y", "Chat"); int chatX = mgr.getInt("x", "Chat"); int chatW = mgr.getInt("w", "Chat"); int chatH = mgr.getInt("h", "Chat"); keySay = SDL_GetKeyFromName(mgr.getString("keySay", "Chat").c_str()); keyChatMode = SDL_GetKeyFromName(mgr.getString("keyChatMode", "Chat").c_str()); mChat = new GUIChat(chatX, chatY, chatW, chatH); mChat->SetDelay(chatDelay); } void mwmp::GUIController::PrintChatMessage(std::string &msg) { if (mChat != nullptr) mChat->print(msg); } void mwmp::GUIController::setChatVisible(bool chatVisible) { mChat->setVisible(chatVisible); } void mwmp::GUIController::ShowMessageBox(const BasePlayer::GUIMessageBox &guiMessageBox) { MWBase::WindowManager *windowManager = MWBase::Environment::get().getWindowManager(); std::vector buttons; buttons.push_back("Ok"); windowManager->interactiveMessageBox(guiMessageBox.label, buttons); calledMessageBox = true; } std::vector splitString(const std::string &str, char delim = ';') { std::istringstream ss(str); std::vector result; std::string token; while (std::getline(ss, token, delim)) result.push_back(token); return result; } void mwmp::GUIController::ShowCustomMessageBox(const BasePlayer::GUIMessageBox &guiMessageBox) { MWBase::WindowManager *windowManager = MWBase::Environment::get().getWindowManager(); std::vector buttons = splitString(guiMessageBox.buttons); windowManager->interactiveMessageBox(guiMessageBox.label, buttons); calledMessageBox = true; } void mwmp::GUIController::ShowInputBox(const BasePlayer::GUIMessageBox &guiMessageBox) { MWBase::WindowManager *windowManager = MWBase::Environment::get().getWindowManager(); windowManager->removeDialog(mInputBox); windowManager->pushGuiMode(MWGui::GM_TES3MPPipe); mInputBox = 0; mInputBox = new MWGui::TextInputDialog(); mInputBox->setTextLabel(guiMessageBox.label); mInputBox->eventDone += MyGUI::newDelegate(this, &GUIController::OnInputBoxDone); } void mwmp::GUIController::OnInputBoxDone(MWGui::WindowBase *parWindow) { //MWBase::WindowManager *windowManager = MWBase::Environment::get().getWindowManager(); printf("GUIController::OnInputBoxDone: %s.\n",mInputBox->getTextInput().c_str()); Main::get().getLocalPlayer()->guiMessageBox.data = mInputBox->getTextInput(); Main::get().getNetworking()->GetPacket(ID_GUI_MESSAGEBOX)->Send(Main::get().getLocalPlayer()); MWBase::Environment::get().getWindowManager()->removeDialog(mInputBox); mInputBox = 0; MWBase::Environment::get().getWindowManager()->popGuiMode(); } bool mwmp::GUIController::pressedKey(int key) { MWBase::WindowManager *windowManager = MWBase::Environment::get().getWindowManager(); if (mChat == nullptr || windowManager->getMode() != MWGui::GM_None) return false; if (key == keyChatMode) { mChat->PressedChatMode(); return true; } else if (key == keySay) { //MyGUI::Widget *oldFocus = MyGUI::InputManager::getInstance().getKeyFocusWidget(); mChat->PressedSay(); /*MyGUI::Widget *newFocus = MyGUI::InputManager::getInstance().getKeyFocusWidget(); printf("mwmp::GUIController::pressedKey. oldFocus: %s.\n", oldFocus ? oldFocus->getName().c_str() : "nil"); printf("mwmp::GUIController::pressedKey.newFocus: %s.\n", newFocus ? newFocus->getName().c_str() : "nil");*/ return true; } else if(key == SDLK_RETURN) { static bool test = true; if(test) { test = false; SetMapVisibility(0, true); } } return false; } bool mwmp::GUIController::HaveFocusedElement() { return false; } void mwmp::GUIController::update(float dt) { if (mChat != nullptr) mChat->Update(dt); int pressedButton = MWBase::Environment::get().getWindowManager()->readPressedButton(); if (pressedButton != -1 && calledMessageBox) { printf("Pressed: %d\n", pressedButton); calledMessageBox = false; Main::get().getLocalPlayer()->guiMessageBox.data = MyGUI::utility::toString(pressedButton); Main::get().getNetworking()->GetPacket(ID_GUI_MESSAGEBOX)->Send(Main::get().getLocalPlayer()); } } void mwmp::GUIController::WM_UpdateVisible(MWGui::GuiMode mode) { switch(mode) { case MWGui::GM_TES3MPPipe: { if (mInputBox != 0) mInputBox->setVisible(true); break; } default: break; } } class MarkerWidget: public MyGUI::Widget { MYGUI_RTTI_DERIVED(MarkerWidget) public: void setNormalColour(const MyGUI::Colour& colour) { mNormalColour = colour; setColour(colour); } void setHoverColour(const MyGUI::Colour& colour) { mHoverColour = colour; } private: MyGUI::Colour mNormalColour; MyGUI::Colour mHoverColour; void onMouseLostFocus(MyGUI::Widget* _new) { setColour(mNormalColour); } void onMouseSetFocus(MyGUI::Widget* _old) { setColour(mHoverColour); } }; void mwmp::GUIController::SetMapVisibility(int pid, bool state) { ESM::CustomMarker mEditingMarker; mEditingMarker.mNote = "TEST"; MWBase::World *world = MWBase::Environment::get().getWorld(); const ESM::Cell *ptrCell = world->getPlayerPtr().getCell()->getCell(); mEditingMarker.mCell = ptrCell->mCellId; mEditingMarker.mWorldX = world->getPlayerPtr().getRefData().getPosition().pos[0]; mEditingMarker.mWorldY = world->getPlayerPtr().getRefData().getPosition().pos[1]; mEditingMarker.mCell.mWorldspace = ESM::CellId::sDefaultWorldspace; mPlayerMarkers.addMarker(mEditingMarker, true); } void mwmp::GUIController::updatePlayersMarkers(MWGui::LocalMapBase *localMapBase) { printf("updatePlayersMarkers!!!\n"); for (std::vector::iterator it = mPlayerMarkerWidgets.begin(); it != mPlayerMarkerWidgets.end(); ++it) MyGUI::Gui::getInstance().destroyWidget(*it); mPlayerMarkerWidgets.clear(); for (int dX = -localMapBase->mCellDistance; dX <= localMapBase->mCellDistance; ++dX) { for (int dY =-localMapBase->mCellDistance; dY <= localMapBase->mCellDistance; ++dY) { ESM::CellId cellId; cellId.mPaged = !localMapBase->mInterior; cellId.mWorldspace = (localMapBase->mInterior ? localMapBase->mPrefix : ESM::CellId::sDefaultWorldspace); cellId.mIndex.mX = localMapBase->mCurX+dX; cellId.mIndex.mY = localMapBase->mCurY+dY; PlayerMarkerCollection::RangeType markers = mPlayerMarkers.getMarkers(cellId); for (PlayerMarkerCollection::ContainerType::const_iterator it = markers.first; it != markers.second; ++it) { const ESM::CustomMarker &marker = it->second; MWGui::LocalMapBase::MarkerUserData markerPos (localMapBase->mLocalMapRender); MyGUI::IntPoint widgetPos = localMapBase->getMarkerPosition(marker.mWorldX, marker.mWorldY, markerPos); MyGUI::IntCoord widgetCoord(widgetPos.left - 8, widgetPos.top - 8, 16, 16); MarkerWidget* markerWidget = localMapBase->mLocalMap->createWidget("CustomMarkerButton", widgetCoord, MyGUI::Align::Default); markerWidget->setDepth(0); // Local_MarkerAboveFogLayer markerWidget->setUserString("ToolTipType", "Layout"); markerWidget->setUserString("ToolTipLayout", "TextToolTipOneLine"); markerWidget->setUserString("Caption_TextOneLine", MyGUI::TextIterator::toTagsString(marker.mNote)); markerWidget->setNormalColour(MyGUI::Colour(0.6f, 0.6f, 0.6f)); markerWidget->setHoverColour(MyGUI::Colour(1.0f, 1.0f, 1.0f)); markerWidget->setUserData(marker); markerWidget->setNeedMouseFocus(true); //localMapBase->customMarkerCreated(markerWidget); mPlayerMarkerWidgets.push_back(markerWidget); } } } localMapBase->redraw(); } void mwmp::GUIController::setGlobalMapMarkerTooltip(MWGui::MapWindow *mapWindow, MyGUI::Widget *markerWidget, int x, int y) { ESM::CellId cellId; cellId.mIndex.mX = x; cellId.mIndex.mY = y; cellId.mWorldspace = ESM::CellId::sDefaultWorldspace; cellId.mPaged = true; PlayerMarkerCollection::RangeType markers = mPlayerMarkers.getMarkers(cellId); std::vector destNotes; for (PlayerMarkerCollection::ContainerType::const_iterator it = markers.first; it != markers.second; ++it) destNotes.push_back(it->second.mNote); if (!destNotes.empty()) { MWGui::LocalMapBase::MarkerUserData data (NULL); data.notes = destNotes; data.caption = markerWidget->getUserString("Caption_TextOneLine"); markerWidget->setUserData(data); markerWidget->setUserString("ToolTipType", "MapMarker"); } else markerWidget->setUserString("ToolTipType", "Layout"); } void mwmp::GUIController::updateGlobalMapMarkerTooltips(MWGui::MapWindow *mapWindow) { std::map, MyGUI::Widget*>::iterator widgetIt = mapWindow->mGlobalMapMarkers.begin(); for (; widgetIt != mapWindow->mGlobalMapMarkers.end(); ++widgetIt) { int x = widgetIt->first.first; int y = widgetIt->first.second; MyGUI::Widget* markerWidget = widgetIt->second; setGlobalMapMarkerTooltip(mapWindow, markerWidget, x, y); } }