-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMapCanvasData.h
More file actions
281 lines (237 loc) · 7.71 KB
/
MapCanvasData.h
File metadata and controls
281 lines (237 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#pragma once
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
// Author: Nils Schimmelmann <nschimme@gmail.com> (Jahara)
#include "../global/EnumIndexedArray.h"
#include "../map/ExitDirection.h"
#include "../map/mmapper2room.h"
#include "../mapdata/mapdata.h"
#include "../mapdata/roomselection.h"
#include "../opengl/OpenGL.h"
#include "CanvasMouseModeEnum.h"
#include "RoadIndex.h"
#include "connectionselection.h"
#include "prespammedpath.h"
#include <map>
#include <memory>
#include <optional>
#include <unordered_map>
#include <QOpenGLTexture>
#include <QWidget>
#include <QWindow>
#include <QtGui/QMatrix4x4>
#include <QtGui/QMouseEvent>
#include <QtGui/qopengl.h>
#include <QtGui>
class ConnectionSelection;
class MapData;
class PrespammedPath;
class InfomarkSelection;
enum class NODISCARD RoomTintEnum { DARK, NO_SUNDEATH };
static const size_t NUM_ROOM_TINTS = 2;
NODISCARD extern const MMapper::Array<RoomTintEnum, NUM_ROOM_TINTS> &getAllRoomTints();
#define ALL_ROOM_TINTS getAllRoomTints()
struct NODISCARD ScaleFactor final
{
public:
// value chosen so the inverse hits 1/25th after 20 steps, just as before.
static constexpr const float ZOOM_STEP = 1.175f;
static constexpr int MIN_VALUE_HUNDREDTHS = 4; // 1/25th
static constexpr int MAX_VALUE_INT = 5;
static constexpr float MIN_VALUE = static_cast<float>(MIN_VALUE_HUNDREDTHS) * 0.01f;
static constexpr float MAX_VALUE = static_cast<float>(MAX_VALUE_INT);
private:
float m_scaleFactor = 1.f;
// pinch gesture
float m_pinchFactor = 1.f;
private:
NODISCARD static float clamp(float x)
{
assert(std::isfinite(x)); // note: also checks for NaN
return std::clamp(x, MIN_VALUE, MAX_VALUE);
}
public:
NODISCARD static bool isClamped(float x) { return ::isClamped(x, MIN_VALUE, MAX_VALUE); }
public:
NODISCARD float getRaw() const { return clamp(m_scaleFactor); }
NODISCARD float getTotal() const { return clamp(m_scaleFactor * m_pinchFactor); }
public:
void set(const float scale) { m_scaleFactor = clamp(scale); }
void setPinch(const float pinch)
{
// Don't bother to clamp this, since the total is clamped.
m_pinchFactor = pinch;
}
void endPinch()
{
const float total = getTotal();
m_scaleFactor = total;
m_pinchFactor = 1.f;
}
void reset() { *this = ScaleFactor(); }
public:
ScaleFactor &operator*=(const float ratio)
{
assert(std::isfinite(ratio) && ratio > 0.f);
set(m_scaleFactor * ratio);
return *this;
}
void logStep(const int numSteps)
{
if (numSteps == 0) {
return;
}
auto &self = *this;
self *= std::pow(ZOOM_STEP, static_cast<float>(numSteps));
}
};
// Abstraction to get size from either QWidget or QWindow
struct NODISCARD SizeSource final
{
private:
QWidget *m_widget = nullptr;
QWindow *m_window = nullptr;
public:
explicit SizeSource(QWidget &widget)
: m_widget{&widget}
{}
explicit SizeSource(QWindow &window)
: m_window{&window}
{}
NODISCARD int width() const
{
if (m_widget)
return m_widget->width();
if (m_window)
return m_window->width();
return 0;
}
NODISCARD int height() const
{
if (m_widget)
return m_widget->height();
if (m_window)
return m_window->height();
return 0;
}
NODISCARD QRect rect() const
{
if (m_widget)
return m_widget->rect();
if (m_window)
return QRect(0, 0, m_window->width(), m_window->height());
return QRect();
}
};
struct NODISCARD MapCanvasViewport
{
private:
SizeSource m_sizeSource;
public:
glm::mat4 m_viewProj{1.f};
glm::vec2 m_scroll{0.f};
ScaleFactor m_scaleFactor;
int m_currentLayer = 0;
public:
explicit MapCanvasViewport(QWidget &sizeSource)
: m_sizeSource{sizeSource}
{}
#ifdef __EMSCRIPTEN__
explicit MapCanvasViewport(QWindow &sizeSource)
: m_sizeSource{sizeSource}
{}
#endif
public:
NODISCARD auto width() const { return m_sizeSource.width(); }
NODISCARD auto height() const { return m_sizeSource.height(); }
NODISCARD Viewport getViewport() const
{
const auto &r = m_sizeSource.rect();
return Viewport{glm::ivec2{r.x(), r.y()}, glm::ivec2{r.width(), r.height()}};
}
NODISCARD float getTotalScaleFactor() const { return m_scaleFactor.getTotal(); }
public:
NODISCARD std::optional<glm::vec3> project(const glm::vec3 &) const;
NODISCARD glm::vec3 unproject_raw(const glm::vec3 &) const;
NODISCARD glm::vec3 unproject_clamped(const glm::vec2 &) const;
NODISCARD std::optional<glm::vec3> unproject(const QInputEvent *event) const;
NODISCARD std::optional<MouseSel> getUnprojectedMouseSel(const QInputEvent *event) const;
NODISCARD glm::vec2 getMouseCoords(const QInputEvent *event) const;
};
class NODISCARD MapScreen final
{
public:
static constexpr const float DEFAULT_MARGIN_PIXELS = 24.f;
private:
const MapCanvasViewport &m_viewport;
enum class NODISCARD VisiblityResultEnum {
INSIDE_MARGIN,
ON_MARGIN,
OUTSIDE_MARGIN,
OFF_SCREEN
};
public:
explicit MapScreen(const MapCanvasViewport &);
~MapScreen();
DELETE_CTORS_AND_ASSIGN_OPS(MapScreen);
public:
NODISCARD glm::vec3 getCenter() const;
NODISCARD bool isRoomVisible(const Coordinate &c, float margin) const;
NODISCARD glm::vec3 getProxyLocation(const glm::vec3 &pos, float margin) const;
private:
NODISCARD VisiblityResultEnum testVisibility(const glm::vec3 &input_pos, float margin) const;
};
struct NODISCARD MapCanvasInputState
{
CanvasMouseModeEnum m_canvasMouseMode = CanvasMouseModeEnum::MOVE;
bool m_mouseRightPressed = false;
bool m_mouseLeftPressed = false;
bool m_altPressed = false;
bool m_ctrlPressed = false;
// mouse selection
std::optional<MouseSel> m_sel1;
std::optional<MouseSel> m_sel2;
// scroll origin of the current mouse movement
std::optional<MouseSel> m_moveBackup;
bool m_selectedArea = false; // no area selected at start time
SharedRoomSelection m_roomSelection;
struct NODISCARD RoomSelMove final
{
Coordinate2i pos;
bool wrongPlace = false;
};
std::optional<RoomSelMove> m_roomSelectionMove;
NODISCARD bool hasRoomSelectionMove() { return m_roomSelectionMove.has_value(); }
std::shared_ptr<InfomarkSelection> m_infoMarkSelection;
struct NODISCARD InfomarkSelectionMove final
{
Coordinate2f pos;
};
std::optional<InfomarkSelectionMove> m_infoMarkSelectionMove;
NODISCARD bool hasInfomarkSelectionMove() const { return m_infoMarkSelectionMove.has_value(); }
std::shared_ptr<ConnectionSelection> m_connectionSelection;
PrespammedPath &m_prespammedPath;
public:
explicit MapCanvasInputState(PrespammedPath &prespammedPath);
~MapCanvasInputState();
public:
NODISCARD static MouseSel getMouseSel(const std::optional<MouseSel> &x)
{
if (x.has_value()) {
return x.value();
}
assert(false);
return {};
}
public:
NODISCARD bool hasSel1() const { return m_sel1.has_value(); }
NODISCARD bool hasSel2() const { return m_sel2.has_value(); }
NODISCARD bool hasBackup() const { return m_moveBackup.has_value(); }
public:
NODISCARD MouseSel getSel1() const { return getMouseSel(m_sel1); }
NODISCARD MouseSel getSel2() const { return getMouseSel(m_sel2); }
NODISCARD MouseSel getBackup() const { return getMouseSel(m_moveBackup); }
public:
void startMoving(const MouseSel &startPos) { m_moveBackup = startPos; }
void stopMoving() { m_moveBackup.reset(); }
};