diff options
Diffstat (limited to 'src/gui/viewport.cpp')
-rw-r--r-- | src/gui/viewport.cpp | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index 763d0c43..1451b935 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -53,10 +53,7 @@ Viewport::Viewport(): mMouseY(0), mPixelViewX(0.0f), mPixelViewY(0.0f), - mTileViewX(0), - mTileViewY(0), mShowDebugPath(false), - mVisibleNames(false), mPlayerFollowMouse(false), mLocalWalkTime(-1) { @@ -67,21 +64,20 @@ Viewport::Viewport(): mScrollRadius = (int) config.getValue("ScrollRadius", 0); mScrollCenterOffsetX = (int) config.getValue("ScrollCenterOffsetX", 0); mScrollCenterOffsetY = (int) config.getValue("ScrollCenterOffsetY", 0); - mVisibleNames = config.getValue("visiblenames", 1); config.addListener("ScrollLaziness", this); config.addListener("ScrollRadius", this); - config.addListener("visiblenames", this); mPopupMenu = new PopupMenu; mBeingPopup = new BeingPopup; + + setFocusable(true); } Viewport::~Viewport() { delete mPopupMenu; - - config.removeListener("visiblenames", this); + delete mBeingPopup; } void Viewport::setMap(Map *map) @@ -109,10 +105,6 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) Graphics *graphics = static_cast<Graphics*>(gcnGraphics); - // Ensure the client doesn't freak out if a feature localplayer uses - // is dependent on a map. - player_node->setMapInitialized(true); - // Avoid freaking out when tick_time overflows if (tick_time < lastTick) { @@ -184,9 +176,6 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) mPixelViewY = viewYmax; } - mTileViewX = (int) (mPixelViewX + 16) / 32; - mTileViewY = (int) (mPixelViewY + 16) / 32; - // Draw tiles and sprites if (mMap) { @@ -312,25 +301,18 @@ void Viewport::mousePressed(gcn::MouseEvent &event) const int pixelX = event.getX() + (int) mPixelViewX; const int pixelY = event.getY() + (int) mPixelViewY; - const int tileX = pixelX / mMap->getTileWidth(); - const int tileY = pixelY / mMap->getTileHeight(); // Right click might open a popup if (event.getButton() == gcn::MouseEvent::RIGHT) { - Being *being; - FloorItem *floorItem; - - if ((being = beingManager->findBeingByPixel(pixelX, pixelY)) && - being != player_node) + if (mHoverBeing && mHoverBeing != player_node) { - mPopupMenu->showPopup(event.getX(), event.getY(), being); + mPopupMenu->showPopup(event.getX(), event.getY(), mHoverBeing); return; } - else if ((floorItem = floorItemManager->findByCoordinates(tileX, - tileY))) + else if (mHoverItem) { - mPopupMenu->showPopup(event.getX(), event.getY(), floorItem); + mPopupMenu->showPopup(event.getX(), event.getY(), mHoverItem); return; } } @@ -345,35 +327,32 @@ void Viewport::mousePressed(gcn::MouseEvent &event) // Left click can cause different actions if (event.getButton() == gcn::MouseEvent::LEFT) { - FloorItem *item; - Being *being; - // Interact with some being - if ((being = beingManager->findBeingByPixel(pixelX, pixelY))) + if (mHoverBeing) { - switch (being->getType()) + switch (mHoverBeing->getType()) { // Talk to NPCs case Being::NPC: - dynamic_cast<NPC*>(being)->talk(); + static_cast<NPC*>(mHoverBeing)->talk(); break; // Attack or walk to monsters or players case Being::MONSTER: case Being::PLAYER: // Ignore it if its dead - if (!being->isAlive()) + if (!mHoverBeing->isAlive()) break; - if (player_node->withinAttackRange(being) || + if (player_node->withinAttackRange(mHoverBeing) || keyboard.isKeyActive(keyboard.KEY_ATTACK)) { - player_node->attack(being, + player_node->attack(mHoverBeing, !keyboard.isKeyActive(keyboard.KEY_TARGET)); } else { - player_node->setGotoTarget(being); + player_node->setGotoTarget(mHoverBeing); } break; default: @@ -381,9 +360,9 @@ void Viewport::mousePressed(gcn::MouseEvent &event) } // Picks up a item if we clicked on one } - else if ((item = floorItemManager->findByCoordinates(tileX, tileY))) + else if (mHoverItem) { - player_node->pickUp(item); + player_node->pickUp(mHoverItem); } else if (player_node->getCurrentAction() == Being::SIT) { @@ -433,8 +412,10 @@ void Viewport::mouseDragged(gcn::MouseEvent &event) if (mLocalWalkTime != player_node->getWalkTime()) { mLocalWalkTime = player_node->getWalkTime(); - int destX = event.getX() / 32 + mTileViewX; - int destY = event.getY() / 32 + mTileViewY; + int destX = (event.getX() + mPixelViewX + 16) / + mMap->getTileWidth(); + int destY = (event.getY() + mPixelViewY + 16) / + mMap->getTileHeight(); player_node->setDestination(destX, destY); } } @@ -464,9 +445,6 @@ void Viewport::optionChanged(const std::string &name) { mScrollLaziness = (int) config.getValue("ScrollLaziness", 32); mScrollRadius = (int) config.getValue("ScrollRadius", 32); - - if (name == "visiblenames") - mVisibleNames = config.getValue("visiblenames", 1); } void Viewport::mouseMoved(gcn::MouseEvent &event) @@ -478,11 +456,42 @@ void Viewport::mouseMoved(gcn::MouseEvent &event) const int x = (event.getX() + (int) mPixelViewX); const int y = (event.getY() + (int) mPixelViewY); - mSelectedBeing = beingManager->findBeingByPixel(x, y); - if (Player *p = dynamic_cast<Player*>(mSelectedBeing)) + mHoverBeing = beingManager->findBeingByPixel(x, y); + if (Player *p = dynamic_cast<Player*>(mHoverBeing)) mBeingPopup->show(getMouseX(), getMouseY(), p); else mBeingPopup->setVisible(false); + + mHoverItem = floorItemManager->findByCoordinates(x / mMap->getTileWidth(), + y / mMap->getTileHeight()); + + if (mHoverBeing) + { + switch (mHoverBeing->getType()) + { + // NPCs + case Being::NPC: + gui->setCursorType(Gui::CURSOR_TALK); + break; + + // Monsters + case Being::MONSTER: + gui->setCursorType(Gui::CURSOR_FIGHT); + break; + default: + gui->setCursorType(Gui::CURSOR_POINTER); + break; + } + // Item mouseover + } + else if (mHoverItem) + { + gui->setCursorType(Gui::CURSOR_PICKUP); + } + else + { + gui->setCursorType(Gui::CURSOR_POINTER); + } } void Viewport::toggleDebugPath() @@ -496,3 +505,7 @@ void Viewport::toggleDebugPath() } } +void Viewport::hideBeingPopup() +{ + mBeingPopup->setVisible(false); +} |