uiminimap.cpp

main 155 lines · 4.9 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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 "game.h"
25#include "luavaluecasts_client.h"
26#include "minimap.h"
27#include "uimapanchorlayout.h"
28
29#include "uimapanchorlayout.h"
30#include <framework/graphics/painter.h>
31
32UIMinimap::UIMinimap() {
33 m_zoom = 0;
34 m_scale = 1.0f;
35 m_minZoom = -5;
36 m_maxZoom = 5;
37 m_layout =
38 UIMapAnchorLayoutPtr(new UIMapAnchorLayout(static_self_cast<UIWidget>()));
39}
40
41void UIMinimap::drawSelf(Fw::DrawPane drawPane) {
42 UIWidget::drawSelf(drawPane);
43
44 if (drawPane != Fw::ForegroundPane)
45 return;
46
47 g_minimap.draw(getPaddingRect(), getCameraPosition(), m_scale, m_color);
48}
49
50bool UIMinimap::setZoom(int zoom) {
51 if (zoom == m_zoom)
52 return true;
53
54 if (zoom < m_minZoom || zoom > m_maxZoom)
55 return false;
56
57 int oldZoom = m_zoom;
58 m_zoom = zoom;
59 if (m_zoom < 0)
60 m_scale = 1.0f / (1 << std::abs(zoom));
61 else if (m_zoom > 0)
62 m_scale = 1.0f * (1 << std::abs(zoom));
63 else
64 m_scale = 1;
65 m_layout->update();
66
67 onZoomChange(zoom, oldZoom);
68 return true;
69}
70
71void UIMinimap::setCameraPosition(const Position &pos) {
72 Position oldPos = m_cameraPosition;
73 m_cameraPosition = pos;
74 m_layout->update();
75
76 onCameraPositionChange(pos, oldPos);
77}
78
79bool UIMinimap::floorUp() {
80 Position pos = getCameraPosition();
81 if (!pos.up())
82 return false;
83 setCameraPosition(pos);
84 return true;
85}
86
87bool UIMinimap::floorDown() {
88 Position pos = getCameraPosition();
89 if (!pos.down())
90 return false;
91 setCameraPosition(pos);
92 return true;
93}
94
95Point UIMinimap::getTilePoint(const Position &pos) {
96 return g_minimap.getTilePoint(pos, getPaddingRect(), getCameraPosition(),
97 m_scale);
98}
99
100Rect UIMinimap::getTileRect(const Position &pos) {
101 return g_minimap.getTileRect(pos, getPaddingRect(), getCameraPosition(),
102 m_scale);
103}
104
105Position UIMinimap::getTilePosition(const Point &mousePos) {
106 return g_minimap.getTilePosition(mousePos, getPaddingRect(),
107 getCameraPosition(), m_scale);
108}
109
110void UIMinimap::anchorPosition(const UIWidgetPtr &anchoredWidget,
111 Fw::AnchorEdge anchoredEdge,
112 const Position &hookedPosition,
113 Fw::AnchorEdge hookedEdge) {
114 UIMapAnchorLayoutPtr layout = m_layout->static_self_cast<UIMapAnchorLayout>();
115 VALIDATE(layout);
116 layout->addPositionAnchor(anchoredWidget, anchoredEdge, hookedPosition,
117 hookedEdge);
118}
119
120void UIMinimap::fillPosition(const UIWidgetPtr &anchoredWidget,
121 const Position &hookedPosition) {
122 UIMapAnchorLayoutPtr layout = m_layout->static_self_cast<UIMapAnchorLayout>();
123 VALIDATE(layout);
124 layout->fillPosition(anchoredWidget, hookedPosition);
125}
126
127void UIMinimap::centerInPosition(const UIWidgetPtr &anchoredWidget,
128 const Position &hookedPosition) {
129 UIMapAnchorLayoutPtr layout = m_layout->static_self_cast<UIMapAnchorLayout>();
130 VALIDATE(layout);
131 layout->centerInPosition(anchoredWidget, hookedPosition);
132}
133
134void UIMinimap::onZoomChange(int zoom, int oldZoom) {
135 callLuaField("onZoomChange", zoom, oldZoom);
136}
137
138void UIMinimap::onCameraPositionChange(const Position &position,
139 const Position &oldPosition) {
140 callLuaField("onCameraPositionChange", position, oldPosition);
141}
142
143void UIMinimap::onStyleApply(const std::string &styleName,
144 const OTMLNodePtr &styleNode) {
145 UIWidget::onStyleApply(styleName, styleNode);
146 for (const OTMLNodePtr &node : styleNode->children()) {
147 if (node->tag() == "zoom")
148 setZoom(node->value<int>());
149 else if (node->tag() == "max-zoom")
150 setMaxZoom(node->value<int>());
151 else if (node->tag() == "min-zoom")
152 setMinZoom(node->value<int>());
153 }
154}
155
Top