2011-08-29 05:04:23 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 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"
|
2011-12-30 19:14:50 +01:00
|
|
|
#include <otclient/core/game.h>
|
2011-08-29 05:04:23 +02:00
|
|
|
#include <otclient/core/map.h>
|
2012-01-30 01:00:12 +01:00
|
|
|
#include <otclient/core/mapview.h>
|
2011-11-03 10:59:11 +01:00
|
|
|
#include <framework/otml/otml.h>
|
|
|
|
#include <framework/graphics/graphics.h>
|
2012-01-07 23:24:29 +01:00
|
|
|
#include <otclient/core/localplayer.h>
|
2011-08-29 05:04:23 +02:00
|
|
|
|
2012-01-20 03:33:11 +01:00
|
|
|
UIMap::UIMap()
|
|
|
|
{
|
|
|
|
m_dragable = true;
|
2012-01-30 01:00:12 +01:00
|
|
|
m_mapView = MapViewPtr(new MapView);
|
2012-04-07 01:12:46 +02:00
|
|
|
m_zoom = m_mapView->getVisibleDimension().height();
|
|
|
|
m_aspectRatio = 0.0f;
|
|
|
|
m_maxZoomIn = 3;
|
|
|
|
m_maxZoomOut = 512;
|
2012-04-27 08:30:54 +02:00
|
|
|
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-03-24 16:22:40 +01:00
|
|
|
void UIMap::drawSelf()
|
2011-08-29 05:04:23 +02:00
|
|
|
{
|
2012-03-24 16:22:40 +01:00
|
|
|
UIWidget::drawSelf();
|
2011-08-29 05:04:23 +02:00
|
|
|
|
2012-02-03 05:18:54 +01:00
|
|
|
// draw map border
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::black);
|
|
|
|
g_painter->drawBoundingRect(m_mapRect.expanded(1));
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::white);
|
2012-01-30 01:00:12 +01:00
|
|
|
m_mapView->draw(m_mapRect);
|
2011-08-29 05:04:23 +02:00
|
|
|
}
|
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
bool UIMap::setZoom(int zoom)
|
2012-01-30 01:00:12 +01:00
|
|
|
{
|
2012-04-27 08:30:54 +02:00
|
|
|
m_zoom = std::min(std::max(zoom, m_maxZoomIn), m_maxZoomOut);
|
|
|
|
updateVisibleDimension();
|
2012-04-07 01:12:46 +02:00
|
|
|
return false;
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
bool UIMap::zoomIn()
|
2012-01-30 01:00:12 +01:00
|
|
|
{
|
2012-04-07 01:12:46 +02:00
|
|
|
int delta = 2;
|
|
|
|
if(m_zoom - delta <= m_maxZoomIn)
|
|
|
|
return false;
|
2012-01-31 05:12:54 +01:00
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
m_zoom -= delta;
|
|
|
|
updateVisibleDimension();
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
bool UIMap::zoomOut()
|
|
|
|
{
|
|
|
|
int delta = 2;
|
|
|
|
if(m_zoom + delta >= m_maxZoomOut)
|
|
|
|
return false;
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
m_zoom += 2;
|
|
|
|
updateVisibleDimension();
|
|
|
|
return true;
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
void UIMap::setVisibleDimension(const Size& visibleDimension)
|
2012-03-18 14:34:39 +01:00
|
|
|
{
|
2012-04-07 01:12:46 +02:00
|
|
|
m_mapView->setVisibleDimension(visibleDimension);
|
|
|
|
|
|
|
|
if(m_aspectRatio != 0.0f)
|
|
|
|
m_aspectRatio = visibleDimension.ratio();
|
2012-03-18 14:34:39 +01:00
|
|
|
}
|
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
void UIMap::setKeepAspectRatio(bool enable)
|
2012-01-31 21:50:35 +01:00
|
|
|
{
|
2012-04-07 01:12:46 +02:00
|
|
|
if(enable)
|
|
|
|
m_aspectRatio = getVisibleDimension().ratio();
|
|
|
|
else
|
|
|
|
m_aspectRatio = 0.0f;
|
2012-01-31 21:50:35 +01:00
|
|
|
}
|
|
|
|
|
2012-01-21 02:01:11 +01:00
|
|
|
TilePtr UIMap::getTile(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-01-21 02:01:11 +01:00
|
|
|
return nullptr;
|
2012-01-02 20:01:48 +01:00
|
|
|
|
2012-02-07 02:35:46 +01:00
|
|
|
//TODO: move MapView code to UIMap and rework this shit
|
|
|
|
return m_mapView->getTile(mousePos, m_mapRect);
|
2011-08-29 05:04:23 +02:00
|
|
|
}
|
2011-08-30 17:12:57 +02:00
|
|
|
|
2012-04-07 01:12:46 +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-minimap-colors")
|
|
|
|
setDrawMinimapColors(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
|
|
|
{
|
2012-03-23 02:52:31 +01:00
|
|
|
UIWidget::onGeometryChange(oldRect, newRect);
|
2012-04-07 01:12:46 +02:00
|
|
|
updateMapSize();
|
|
|
|
}
|
2012-03-23 02:52:31 +01:00
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
void UIMap::updateVisibleDimension()
|
|
|
|
{
|
|
|
|
int dimensionHeight = m_zoom;
|
|
|
|
if(dimensionHeight % 2 == 0)
|
|
|
|
dimensionHeight += 1;
|
|
|
|
int dimensionWidth = m_zoom * m_mapRect.size().ratio();
|
|
|
|
if(dimensionWidth % 2 == 0)
|
|
|
|
dimensionWidth += 1;
|
|
|
|
|
|
|
|
m_mapView->setVisibleDimension(Size(dimensionWidth, dimensionHeight));
|
|
|
|
|
|
|
|
if(m_aspectRatio != 0.0f)
|
|
|
|
updateMapSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIMap::updateMapSize()
|
|
|
|
{
|
|
|
|
Rect clippingRect = getClippingRect();
|
|
|
|
Size mapSize;
|
|
|
|
if(m_aspectRatio != 0.0f) {
|
|
|
|
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-11-23 05:27:58 +01:00
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
m_mapRect.resize(mapSize);
|
2012-04-07 01:12:46 +02:00
|
|
|
m_mapRect.moveCenter(clippingRect.center());
|
|
|
|
m_mapView->optimizeForSize(mapSize);
|
|
|
|
|
|
|
|
if(m_aspectRatio == 0.0f)
|
|
|
|
updateVisibleDimension();
|
2011-08-30 17:12:57 +02:00
|
|
|
}
|
2012-04-07 01:12:46 +02:00
|
|
|
|