Otclient  14/8/2020
uimap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "uimap.h"
24 #include "game.h"
25 #include "map.h"
26 #include "mapview.h"
27 #include <framework/otml/otml.h>
29 #include "localplayer.h"
30 
32 {
33  m_draggable = true;
34  m_mapView = MapViewPtr(new MapView);
35  m_zoom = m_mapView->getVisibleDimension().height();
36  m_keepAspectRatio = true;
37  m_limitVisibleRange = false;
38  m_aspectRatio = m_mapView->getVisibleDimension().ratio();
39  m_maxZoomIn = 3;
40  m_maxZoomOut = 513;
41  m_mapRect.resize(1,1);
42  g_map.addMapView(m_mapView);
43 }
44 
46 {
47  g_map.removeMapView(m_mapView);
48 }
49 
51 {
52  UIWidget::drawSelf(drawPane);
53 
54  if(drawPane & Fw::ForegroundPane) {
55  // draw map border
57  g_painter->drawBoundingRect(m_mapRect.expanded(1));
58 
59  if(drawPane != Fw::BothPanes) {
60  glDisable(GL_BLEND);
62  g_painter->drawFilledRect(m_mapRect);
63  glEnable(GL_BLEND);
64  }
65  }
66 
67  if(drawPane & Fw::BackgroundPane) {
69  m_mapView->draw(m_mapRect);
70  }
71 }
72 
73 void UIMap::movePixels(int x, int y)
74 {
75  m_mapView->move(x, y);
76 }
77 
78 bool UIMap::setZoom(int zoom)
79 {
80  m_zoom = stdext::clamp<int>(zoom, m_maxZoomIn, m_maxZoomOut);
81  updateVisibleDimension();
82  return false;
83 }
84 
86 {
87  int delta = 2;
88  if(m_zoom - delta < m_maxZoomIn)
89  delta--;
90 
91  if(m_zoom - delta < m_maxZoomIn)
92  return false;
93 
94  m_zoom -= delta;
95  updateVisibleDimension();
96  return true;
97 }
98 
100 {
101  int delta = 2;
102  if(m_zoom + delta > m_maxZoomOut)
103  delta--;
104 
105  if(m_zoom + delta > m_maxZoomOut)
106  return false;
107 
108  m_zoom += 2;
109  updateVisibleDimension();
110  return true;
111 }
112 
113 void UIMap::setVisibleDimension(const Size& visibleDimension)
114 {
115  m_mapView->setVisibleDimension(visibleDimension);
116  m_aspectRatio = visibleDimension.ratio();
117 
118  if(m_keepAspectRatio)
119  updateMapSize();
120 }
121 
122 void UIMap::setKeepAspectRatio(bool enable)
123 {
124  m_keepAspectRatio = enable;
125  if(enable)
126  m_aspectRatio = getVisibleDimension().ratio();
127  updateMapSize();
128 }
129 
131 {
132  if(!m_mapRect.contains(mousePos))
133  return Position();
134 
135  Point relativeMousePos = mousePos - m_mapRect.topLeft();
136  return m_mapView->getPosition(relativeMousePos, m_mapRect.size());
137 }
138 
139 TilePtr UIMap::getTile(const Point& mousePos)
140 {
141  Position tilePos = getPosition(mousePos);
142  if(!tilePos.isValid())
143  return nullptr;
144 
145  // we must check every floor, from top to bottom to check for a clickable tile
146  TilePtr tile;
147  tilePos.coveredUp(tilePos.z - m_mapView->getCachedFirstVisibleFloor());
148  for(int i = m_mapView->getCachedFirstVisibleFloor(); i <= m_mapView->getCachedLastVisibleFloor(); i++) {
149  tile = g_map.getTile(tilePos);
150  if(tile && tile->isClickable())
151  break;
152  tilePos.coveredDown();
153  }
154 
155  if(!tile || !tile->isClickable())
156  return nullptr;
157 
158  return tile;
159 }
160 
161 void UIMap::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
162 {
163  UIWidget::onStyleApply(styleName, styleNode);
164  for(const OTMLNodePtr& node : styleNode->children()) {
165  if(node->tag() == "multifloor")
166  setMultifloor(node->value<bool>());
167  else if(node->tag() == "auto-view-mode")
168  setAutoViewMode(node->value<bool>());
169  else if(node->tag() == "draw-texts")
170  setDrawTexts(node->value<bool>());
171  else if(node->tag() == "draw-lights")
172  setDrawLights(node->value<bool>());
173  else if(node->tag() == "animated")
174  setAnimated(node->value<bool>());
175  }
176 }
177 
178 void UIMap::onGeometryChange(const Rect& oldRect, const Rect& newRect)
179 {
180  UIWidget::onGeometryChange(oldRect, newRect);
181  updateMapSize();
182 }
183 
184 void UIMap::updateVisibleDimension()
185 {
186  int dimensionHeight = m_zoom;
187 
188  float ratio = m_aspectRatio;
189  if(!m_limitVisibleRange && !m_mapRect.isEmpty() && !m_keepAspectRatio)
190  ratio = m_mapRect.size().ratio();
191 
192  if(dimensionHeight % 2 == 0)
193  dimensionHeight += 1;
194  int dimensionWidth = m_zoom * ratio;
195  if(dimensionWidth % 2 == 0)
196  dimensionWidth += 1;
197 
198  m_mapView->setVisibleDimension(Size(dimensionWidth, dimensionHeight));
199 
200  if(m_keepAspectRatio)
201  updateMapSize();
202 }
203 
204 void UIMap::updateMapSize()
205 {
206  Rect clippingRect = getPaddingRect();
207  Size mapSize;
208  if(m_keepAspectRatio) {
209  Rect mapRect = clippingRect.expanded(-1);
210  mapSize = Size(m_aspectRatio*m_zoom, m_zoom);
211  mapSize.scale(mapRect.size(), Fw::KeepAspectRatio);
212  } else {
213  mapSize = clippingRect.expanded(-1).size();
214  }
215 
216  m_mapRect.resize(mapSize);
217  m_mapRect.moveCenter(clippingRect.center());
218  m_mapView->optimizeForSize(mapSize);
219 
220  if(!m_keepAspectRatio)
221  updateVisibleDimension();
222 }
223 
224 /* vim: set ts=4 sw=4 et: */
TRect::contains
bool contains(const TPoint< T > &p, bool insideOnly=false) const
Definition: rect.h:141
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
TRect::resize
void resize(const TSize< T > &size)
Definition: rect.h:100
graphics.h
g_map
Map g_map
Definition: map.cpp:36
UIMap::zoomOut
bool zoomOut()
Definition: uimap.cpp:99
Tile::isClickable
bool isClickable()
Definition: tile.cpp:535
UIMap::setKeepAspectRatio
void setKeepAspectRatio(bool enable)
Definition: uimap.cpp:122
UIMap::zoomIn
bool zoomIn()
Definition: uimap.cpp:85
MapView::getCachedFirstVisibleFloor
int getCachedFirstVisibleFloor()
Definition: mapview.h:73
otml.h
TRect< int >
TSize::scale
void scale(const TSize< T > &s, Fw::AspectRatioMode mode)
Definition: size.h:76
UIMap::setAutoViewMode
void setAutoViewMode(bool enable)
Definition: uimap.h:54
UIWidget::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiwidget.cpp:1461
Position::isValid
bool isValid() const
Definition: position.h:182
Fw::KeepAspectRatio
@ KeepAspectRatio
Definition: const.h:188
TRect::isEmpty
bool isEmpty() const
Definition: rect.h:49
UIWidget::drawSelf
virtual void drawSelf(Fw::DrawPane drawPane)
Definition: uiwidget.cpp:88
Map::getTile
const TilePtr & getTile(const Position &pos)
Definition: map.cpp:310
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
MapView::getPosition
Position getPosition(const Point &point, const Size &mapSize)
Definition: mapview.cpp:554
Fw::BackgroundPane
@ BackgroundPane
Definition: const.h:287
Map::removeMapView
void removeMapView(const MapViewPtr &mapView)
Definition: map.cpp:55
MapViewPtr
stdext::shared_object_ptr< MapView > MapViewPtr
Definition: declarations.h:55
Map::addMapView
void addMapView(const MapViewPtr &mapView)
Definition: map.cpp:50
UIWidget::onGeometryChange
virtual void onGeometryChange(const Rect &oldRect, const Rect &newRect)
Definition: uiwidget.cpp:1477
Position::z
short z
Definition: position.h:245
UIMap::movePixels
void movePixels(int x, int y)
Definition: uimap.cpp:73
UIWidget::getPosition
Point getPosition()
Definition: uiwidget.h:354
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
localplayer.h
mapview.h
Fw::BothPanes
@ BothPanes
Definition: const.h:288
MapView::setVisibleDimension
void setVisibleDimension(const Size &visibleDimension)
Definition: mapview.cpp:504
Position
Definition: position.h:33
Color::white
static const Color white
Definition: color.h:101
UIMap::setMultifloor
void setMultifloor(bool enable)
Definition: uimap.h:49
TRect::expanded
TRect< T > expanded(T add) const
Definition: rect.h:120
Painter::drawBoundingRect
virtual void drawBoundingRect(const Rect &dest, int innerLineWidth=1)=0
UIMap::getTile
TilePtr getTile(const Point &mousePos)
Definition: uimap.cpp:139
Size
TSize< int > Size
Definition: size.h:107
map.h
TRect::moveCenter
void moveCenter(const TPoint< T > &p)
Definition: rect.h:122
UIWidget::m_draggable
stdext::boolean< false > m_draggable
Definition: uiwidget.h:69
uimap.h
UIMap::setZoom
bool setZoom(int zoom)
Definition: uimap.cpp:78
Color::black
static const Color black
Definition: color.h:102
UIMap::setDrawLights
void setDrawLights(bool enable)
Definition: uimap.h:59
Position::coveredDown
bool coveredDown(int n=1)
Definition: position.h:234
Color::alpha
static const Color alpha
Definition: color.h:100
UIMap::setAnimated
void setAnimated(bool enable)
Definition: uimap.h:61
TSize::height
int height() const
Definition: size.h:44
UIMap::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uimap.cpp:161
Fw::DrawPane
DrawPane
Definition: const.h:285
stdext::shared_object_ptr< Tile >
TSize::ratio
float ratio() const
Definition: size.h:100
MapView
Definition: mapview.h:34
Fw::ForegroundPane
@ ForegroundPane
Definition: const.h:286
g_painter
Painter * g_painter
Definition: painter.cpp:28
UIMap::onGeometryChange
virtual void onGeometryChange(const Rect &oldRect, const Rect &newRect)
Definition: uimap.cpp:178
UIMap::getVisibleDimension
Size getVisibleDimension()
Definition: uimap.h:79
game.h
MapView::draw
void draw(const Rect &rect)
Definition: mapview.cpp:78
UIMap::drawSelf
void drawSelf(Fw::DrawPane drawPane)
Definition: uimap.cpp:50
UIWidget::enable
void enable()
Definition: uiwidget.h:223
MapView::optimizeForSize
void optimizeForSize(const Size &visibleSize)
Definition: mapview.cpp:535
UIMap::UIMap
UIMap()
Definition: uimap.cpp:31
TPoint< int >
TRect::size
TSize< T > size() const
Definition: rect.h:71
UIMap::setVisibleDimension
void setVisibleDimension(const Size &visibleDimension)
Definition: uimap.cpp:113
Painter::drawFilledRect
virtual void drawFilledRect(const Rect &dest)=0
MapView::move
void move(int x, int y)
Definition: mapview.cpp:581
UIMap::~UIMap
~UIMap()
Definition: uimap.cpp:45
TRect::center
TPoint< T > center() const
Definition: rect.h:68
TSize< int >
MapView::getVisibleDimension
Size getVisibleDimension()
Definition: mapview.h:70
UIMap::setDrawTexts
void setDrawTexts(bool enable)
Definition: uimap.h:56
Position::coveredUp
bool coveredUp(int n=1)
Definition: position.h:225