tibia-client/src/client/uimap.cpp

225 lines
6.1 KiB
C++
Raw Normal View History

2011-08-29 05:04:23 +02:00
/*
2014-04-01 07:36:42 +02:00
* Copyright (c) 2010-2014 OTClient <https://github.com/edubart/otclient>
2011-08-29 05:04:23 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "uimap.h"
#include "game.h"
#include "map.h"
#include "mapview.h"
#include <framework/otml/otml.h>
#include <framework/graphics/graphics.h>
#include "localplayer.h"
2011-08-29 05:04:23 +02:00
2012-01-20 03:33:11 +01:00
UIMap::UIMap()
{
2012-08-07 01:43:14 +02:00
m_draggable = true;
2012-01-30 01:00:12 +01:00
m_mapView = MapViewPtr(new MapView);
m_zoom = m_mapView->getVisibleDimension().height();
2013-01-24 20:15:07 +01:00
m_keepAspectRatio = true;
m_limitVisibleRange = false;
m_aspectRatio = m_mapView->getVisibleDimension().ratio();
m_maxZoomIn = 3;
2013-01-24 20:15:07 +01:00
m_maxZoomOut = 513;
m_mapRect.resize(1,1);
2012-01-30 01:00:12 +01:00
g_map.addMapView(m_mapView);
}
UIMap::~UIMap()
{
g_map.removeMapView(m_mapView);
2012-01-20 03:33:11 +01:00
}
2012-06-08 18:58:08 +02:00
void UIMap::drawSelf(Fw::DrawPane drawPane)
2011-08-29 05:04:23 +02:00
{
2012-06-08 18:58:08 +02:00
UIWidget::drawSelf(drawPane);
2011-08-29 05:04:23 +02:00
2012-06-08 18:58:08 +02:00
if(drawPane & Fw::ForegroundPane) {
// draw map border
g_painter->setColor(Color::black);
g_painter->drawBoundingRect(m_mapRect.expanded(1));
2012-01-30 01:00:12 +01:00
2012-06-08 18:58:08 +02:00
if(drawPane != Fw::BothPanes) {
glDisable(GL_BLEND);
2012-06-08 18:58:08 +02:00
g_painter->setColor(Color::alpha);
g_painter->drawFilledRect(m_mapRect);
glEnable(GL_BLEND);
2012-06-08 18:58:08 +02:00
}
}
if(drawPane & Fw::BackgroundPane) {
g_painter->setColor(Color::white);
m_mapView->draw(m_mapRect);
}
2011-08-29 05:04:23 +02:00
}
void UIMap::movePixels(int x, int y)
{
m_mapView->move(x, y);
}
bool UIMap::setZoom(int zoom)
2012-01-30 01:00:12 +01:00
{
2013-12-01 04:12:06 +01:00
m_zoom = stdext::clamp<int>(zoom, m_maxZoomIn, m_maxZoomOut);
updateVisibleDimension();
return false;
2012-01-30 01:00:12 +01:00
}
bool UIMap::zoomIn()
2012-01-30 01:00:12 +01:00
{
int delta = 2;
2013-01-24 20:15:07 +01:00
if(m_zoom - delta < m_maxZoomIn)
delta--;
if(m_zoom - delta < m_maxZoomIn)
return false;
2012-01-31 05:12:54 +01:00
m_zoom -= delta;
updateVisibleDimension();
return true;
}
2012-01-30 01:00:12 +01:00
bool UIMap::zoomOut()
{
int delta = 2;
2013-01-24 20:15:07 +01:00
if(m_zoom + delta > m_maxZoomOut)
delta--;
if(m_zoom + delta > m_maxZoomOut)
return false;
2012-01-30 01:00:12 +01:00
m_zoom += 2;
updateVisibleDimension();
return true;
2012-01-30 01:00:12 +01:00
}
void UIMap::setVisibleDimension(const Size& visibleDimension)
{
m_mapView->setVisibleDimension(visibleDimension);
2013-01-24 20:15:07 +01:00
m_aspectRatio = visibleDimension.ratio();
2013-01-24 20:15:07 +01:00
if(m_keepAspectRatio)
2012-06-22 07:26:22 +02:00
updateMapSize();
}
void UIMap::setKeepAspectRatio(bool enable)
2012-01-31 21:50:35 +01:00
{
2013-01-24 20:15:07 +01:00
m_keepAspectRatio = enable;
if(enable)
m_aspectRatio = getVisibleDimension().ratio();
2012-06-22 07:26:22 +02:00
updateMapSize();
2012-01-31 21:50:35 +01:00
}
2012-08-21 03:03:30 +02:00
Position UIMap::getPosition(const Point& mousePos)
2011-08-29 05:04:23 +02:00
{
2012-01-02 20:01:48 +01:00
if(!m_mapRect.contains(mousePos))
2012-08-21 03:03:30 +02:00
return Position();
Point relativeMousePos = mousePos - m_mapRect.topLeft();
2013-01-25 20:26:51 +01:00
return m_mapView->getPosition(relativeMousePos, m_mapRect.size());
2012-08-21 03:03:30 +02:00
}
TilePtr UIMap::getTile(const Point& mousePos)
{
Position tilePos = getPosition(mousePos);
if(!tilePos.isValid())
return nullptr;
// we must check every floor, from top to bottom to check for a clickable tile
TilePtr tile;
tilePos.coveredUp(tilePos.z - m_mapView->getCachedFirstVisibleFloor());
for(int i = m_mapView->getCachedFirstVisibleFloor(); i <= m_mapView->getCachedLastVisibleFloor(); i++) {
tile = g_map.getTile(tilePos);
if(tile && tile->isClickable())
break;
tilePos.coveredDown();
}
if(!tile || !tile->isClickable())
return nullptr;
2012-01-02 20:01:48 +01:00
2012-08-21 03:03:30 +02:00
return tile;
2011-08-29 05:04:23 +02:00
}
2011-08-30 17:12:57 +02:00
void UIMap::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleName, styleNode);
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "multifloor")
setMultifloor(node->value<bool>());
else if(node->tag() == "auto-view-mode")
setAutoViewMode(node->value<bool>());
else if(node->tag() == "draw-texts")
setDrawTexts(node->value<bool>());
else if(node->tag() == "draw-lights")
setDrawLights(node->value<bool>());
else if(node->tag() == "animated")
setAnimated(node->value<bool>());
}
}
2012-01-05 03:42:17 +01:00
void UIMap::onGeometryChange(const Rect& oldRect, const Rect& newRect)
2011-08-30 17:12:57 +02:00
{
UIWidget::onGeometryChange(oldRect, newRect);
updateMapSize();
}
void UIMap::updateVisibleDimension()
{
int dimensionHeight = m_zoom;
2013-01-24 20:15:07 +01:00
float ratio = m_aspectRatio;
if(!m_limitVisibleRange && !m_mapRect.isEmpty() && !m_keepAspectRatio)
ratio = m_mapRect.size().ratio();
if(dimensionHeight % 2 == 0)
dimensionHeight += 1;
int dimensionWidth = m_zoom * ratio;
if(dimensionWidth % 2 == 0)
dimensionWidth += 1;
m_mapView->setVisibleDimension(Size(dimensionWidth, dimensionHeight));
2013-01-24 20:15:07 +01:00
if(m_keepAspectRatio)
updateMapSize();
}
void UIMap::updateMapSize()
{
2012-06-02 02:38:26 +02:00
Rect clippingRect = getPaddingRect();
Size mapSize;
2013-01-24 20:15:07 +01:00
if(m_keepAspectRatio) {
Rect mapRect = clippingRect.expanded(-1);
mapSize = Size(m_aspectRatio*m_zoom, m_zoom);
mapSize.scale(mapRect.size(), Fw::KeepAspectRatio);
} else {
mapSize = clippingRect.expanded(-1).size();
}
2011-12-07 01:31:55 +01:00
m_mapRect.resize(mapSize);
m_mapRect.moveCenter(clippingRect.center());
m_mapView->optimizeForSize(mapSize);
2013-01-24 20:15:07 +01:00
if(!m_keepAspectRatio)
updateVisibleDimension();
2011-08-30 17:12:57 +02:00
}
/* vim: set ts=4 sw=4 et: */