Otclient  14/8/2020
uiminimap.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 "uiminimap.h"
24 #include "minimap.h"
25 #include "game.h"
26 #include "uimapanchorlayout.h"
27 #include "luavaluecasts.h"
28 
30 #include "uimapanchorlayout.h"
31 
33 {
34  m_zoom = 0;
35  m_scale = 1.0f;
36  m_minZoom = -5;
37  m_maxZoom = 5;
38  m_layout = UIMapAnchorLayoutPtr(new UIMapAnchorLayout(static_self_cast<UIWidget>()));
39 }
40 
42 {
43  UIWidget::drawSelf(drawPane);
44 
45  if((drawPane & Fw::ForegroundPane) == 0)
46  return;
47 
49 }
50 
51 bool UIMinimap::setZoom(int zoom)
52 {
53  if(zoom == m_zoom)
54  return true;
55 
56  if(zoom < m_minZoom || zoom > m_maxZoom)
57  return false;
58 
59  int oldZoom = m_zoom;
60  m_zoom = zoom;
61  if(m_zoom < 0)
62  m_scale = 1.0f / (1 << std::abs(zoom));
63  else if(m_zoom > 0)
64  m_scale = 1.0f * (1 << std::abs(zoom));
65  else
66  m_scale = 1;
67  m_layout->update();
68 
69  onZoomChange(zoom, oldZoom);
70  return true;
71 }
72 
74 {
75  Position oldPos = m_cameraPosition;
76  m_cameraPosition = pos;
77  m_layout->update();
78 
79  onCameraPositionChange(pos, oldPos);
80 }
81 
83 {
85  if(!pos.up())
86  return false;
87  setCameraPosition(pos);
88  return true;
89 }
90 
92 {
94  if(!pos.down())
95  return false;
96  setCameraPosition(pos);
97  return true;
98 }
99 
101 {
102  return g_minimap.getTilePoint(pos, getPaddingRect(), getCameraPosition(), m_scale);
103 }
104 
106 {
107  return g_minimap.getTileRect(pos, getPaddingRect(), getCameraPosition(), m_scale);
108 }
109 
111 {
112  return g_minimap.getTilePosition(mousePos, getPaddingRect(), getCameraPosition(), m_scale);
113 }
114 
115 void UIMinimap::anchorPosition(const UIWidgetPtr& anchoredWidget, Fw::AnchorEdge anchoredEdge, const Position& hookedPosition, Fw::AnchorEdge hookedEdge)
116 {
118  assert(layout);
119  layout->addPositionAnchor(anchoredWidget, anchoredEdge, hookedPosition, hookedEdge);
120 }
121 
122 void UIMinimap::fillPosition(const UIWidgetPtr& anchoredWidget, const Position& hookedPosition)
123 {
125  assert(layout);
126  layout->fillPosition(anchoredWidget, hookedPosition);
127 }
128 
129 void UIMinimap::centerInPosition(const UIWidgetPtr& anchoredWidget, const Position& hookedPosition)
130 {
132  assert(layout);
133  layout->centerInPosition(anchoredWidget, hookedPosition);
134 }
135 
136 void UIMinimap::onZoomChange(int zoom, int oldZoom)
137 {
138  callLuaField("onZoomChange", zoom, oldZoom);
139 }
140 
141 void UIMinimap::onCameraPositionChange(const Position& position, const Position& oldPosition)
142 {
143  callLuaField("onCameraPositionChange", position, oldPosition);
144 }
145 
146 void UIMinimap::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
147 {
148  UIWidget::onStyleApply(styleName, styleNode);
149  for(const OTMLNodePtr& node : styleNode->children()) {
150  if(node->tag() == "zoom")
151  setZoom(node->value<int>());
152  else if(node->tag() == "max-zoom")
153  setMaxZoom(node->value<int>());
154  else if(node->tag() == "min-zoom")
155  setMinZoom(node->value<int>());
156  }
157 }
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
UIWidget::m_layout
UILayoutPtr m_layout
Definition: uiwidget.h:72
UIMapAnchorLayout
Definition: uimapanchorlayout.h:42
UIMinimap::getTilePosition
Position getTilePosition(const Point &mousePos)
Definition: uiminimap.cpp:110
TRect< int >
UIMinimap::fillPosition
void fillPosition(const UIWidgetPtr &anchoredWidget, const Position &hookedPosition)
Definition: uiminimap.cpp:122
uimapanchorlayout.h
UIWidget::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiwidget.cpp:1461
UIMinimap::setZoom
bool setZoom(int zoom)
Definition: uiminimap.cpp:51
luavaluecasts.h
UIMinimap::getTileRect
Rect getTileRect(const Position &pos)
Definition: uiminimap.cpp:105
Fw::AnchorEdge
AnchorEdge
Definition: const.h:211
UIMinimap::setMaxZoom
void setMaxZoom(int maxZoom)
Definition: uiminimap.h:41
UIMinimap::floorDown
bool floorDown()
Definition: uiminimap.cpp:91
UIWidget::drawSelf
virtual void drawSelf(Fw::DrawPane drawPane)
Definition: uiwidget.cpp:88
UIMinimap::anchorPosition
void anchorPosition(const UIWidgetPtr &anchoredWidget, Fw::AnchorEdge anchoredEdge, const Position &hookedPosition, Fw::AnchorEdge hookedEdge)
Definition: uiminimap.cpp:115
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
UIMinimap::UIMinimap
UIMinimap()
Definition: uiminimap.cpp:32
UIMinimap::setMinZoom
void setMinZoom(int minZoom)
Definition: uiminimap.h:40
minimap.h
UIMinimap::getTilePoint
Point getTilePoint(const Position &pos)
Definition: uiminimap.cpp:100
UIMapAnchorLayoutPtr
stdext::shared_object_ptr< UIMapAnchorLayout > UIMapAnchorLayoutPtr
Definition: declarations.h:115
painter.h
LuaObject::callLuaField
R callLuaField(const std::string &field, const T &... args)
Definition: luaobject.h:172
UIMinimap::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiminimap.cpp:146
stdext::shared_object::static_self_cast
stdext::shared_object_ptr< T > static_self_cast()
Definition: shared_object.h:50
Position
Definition: position.h:33
UIMinimap::onZoomChange
virtual void onZoomChange(int zoom, int oldZoom)
Definition: uiminimap.cpp:136
UIMinimap::centerInPosition
void centerInPosition(const UIWidgetPtr &anchoredWidget, const Position &hookedPosition)
Definition: uiminimap.cpp:129
UILayout::update
void update()
Definition: uilayout.cpp:28
Minimap::getTileRect
Rect getTileRect(const Position &pos, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:174
Minimap::getTilePosition
Position getTilePosition(const Point &point, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:163
Minimap::getTilePoint
Point getTilePoint(const Position &pos, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:152
Position::down
bool down(int n=1)
Definition: position.h:216
Fw::DrawPane
DrawPane
Definition: const.h:285
stdext::shared_object_ptr< UIWidget >
UIMinimap::getCameraPosition
Position getCameraPosition()
Definition: uiminimap.h:50
Fw::ForegroundPane
@ ForegroundPane
Definition: const.h:286
g_minimap
Minimap g_minimap
Definition: minimap.cpp:36
game.h
UIMinimap::setCameraPosition
void setCameraPosition(const Position &pos)
Definition: uiminimap.cpp:73
uiminimap.h
TPoint< int >
UIMinimap::drawSelf
void drawSelf(Fw::DrawPane drawPane)
Definition: uiminimap.cpp:41
Position::up
bool up(int n=1)
Definition: position.h:207
UIMinimap::floorUp
bool floorUp()
Definition: uiminimap.cpp:82
UIMinimap::onCameraPositionChange
virtual void onCameraPositionChange(const Position &position, const Position &oldPosition)
Definition: uiminimap.cpp:141
Minimap::draw
void draw(const Rect &screenRect, const Position &mapCenter, float scale, const Color &color)
Definition: minimap.cpp:101
UIWidget::m_color
Color m_color
Definition: uiwidget.h:281