forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmumeclockwidget.cpp
More file actions
260 lines (239 loc) · 8.98 KB
/
Copy pathmumeclockwidget.cpp
File metadata and controls
260 lines (239 loc) · 8.98 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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
// Author: Nils Schimmelmann <nschimme@gmail.com> (Jahara)
#include "mumeclockwidget.h"
#include "../configuration/configuration.h"
#include "mumeclock.h"
#include "mumemoment.h"
#include <cassert>
#include <QDateTime>
#include <QLabel>
#include <QMouseEvent>
#include <QString>
MumeClockWidget::MumeClockWidget(GameObserver &observer, MumeClock &clock, QWidget *const parent)
: QWidget(parent)
, m_clock(clock)
{
setupUi(this);
moonPhaseLabel->setText("");
seasonLabel->setText("");
setAttribute(Qt::WA_DeleteOnClose);
assert(testAttribute(Qt::WA_DeleteOnClose));
setAttribute(Qt::WA_Hover, true);
assert(testAttribute(Qt::WA_Hover));
observer.sig2_timeOfDayChanged.connect(m_lifetime,
[this](const MumeTimeEnum time) { updateTime(time); });
observer.sig2_moonPhaseChanged.connect(m_lifetime, [this](MumeMoonPhaseEnum phase) {
updateMoonPhase(phase);
});
observer.sig2_moonVisibilityChanged.connect(m_lifetime,
[this](const MumeMoonVisibilityEnum visibility) {
updateMoonVisibility(visibility);
});
observer.sig2_seasonChanged.connect(m_lifetime, [this](const MumeSeasonEnum season) {
updateSeason(season);
});
observer.sig2_weatherChanged.connect(m_lifetime, [this](const PromptWeatherEnum weather) {
updateWeather(weather);
});
observer.sig2_fogChanged.connect(m_lifetime,
[this](const PromptFogEnum fog) { updateFog(fog); });
observer.sig2_tick.connect(m_lifetime,
[this](const MumeMoment &moment) { updateCountdown(moment); });
auto moment = m_clock.getMumeMoment();
updateTime(moment.toTimeOfDay());
updateMoonPhase(moment.moonPhase());
updateMoonVisibility(moment.moonVisibility());
updateSeason(moment.toSeason());
updateCountdown(moment);
updateWeather(PromptWeatherEnum::NICE);
updateFog(PromptFogEnum::NO_FOG);
}
MumeClockWidget::~MumeClockWidget() = default;
void MumeClockWidget::mousePressEvent(QMouseEvent * /*event*/)
{
// Force precision to minute and reset last sync to current timestamp
m_clock.setPrecision(MumeClockPrecisionEnum::MINUTE);
m_clock.setLastSyncEpoch(QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
auto moment = m_clock.getMumeMoment();
updateTime(moment.toTimeOfDay());
updateCountdown(moment);
updateStatusTips(moment);
}
bool MumeClockWidget::event(QEvent *event)
{
if (event->type() == QEvent::HoverEnter) {
updateStatusTips(m_clock.getMumeMoment());
}
return QWidget::event(event);
}
void MumeClockWidget::updateTime(MumeTimeEnum time)
{
// The current time is 12:15 am.
QString styleSheet = "";
if (m_clock.getPrecision() <= MumeClockPrecisionEnum::UNSET) {
styleSheet = "padding-left:1px;padding-right:1px;color:white;background:grey";
} else if (time == MumeTimeEnum::DAWN) {
styleSheet = "padding-left:1px;padding-right:1px;color:white;background:red";
} else if (time >= MumeTimeEnum::DUSK) {
styleSheet = "padding-left:1px;padding-right:1px;color:white;background:blue";
} else {
styleSheet = "padding-left:1px;padding-right:1px;color:black;background:yellow";
}
timeLabel->setStyleSheet(styleSheet);
}
void MumeClockWidget::updateMoonPhase(MumeMoonPhaseEnum phase)
{
switch (phase) {
case MumeMoonPhaseEnum::WAXING_CRESCENT:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x92"));
break;
case MumeMoonPhaseEnum::FIRST_QUARTER:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x93"));
break;
case MumeMoonPhaseEnum::WAXING_GIBBOUS:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x94"));
break;
case MumeMoonPhaseEnum::FULL_MOON:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x95"));
break;
case MumeMoonPhaseEnum::WANING_GIBBOUS:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x96"));
break;
case MumeMoonPhaseEnum::THIRD_QUARTER:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x97"));
break;
case MumeMoonPhaseEnum::WANING_CRESCENT:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x98"));
break;
case MumeMoonPhaseEnum::NEW_MOON:
moonPhaseLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\x91"));
break;
case MumeMoonPhaseEnum::UNKNOWN:
moonPhaseLabel->setText("");
break;
}
}
void MumeClockWidget::updateMoonVisibility(MumeMoonVisibilityEnum visibility)
{
const QString moonStyleSheet = (visibility == MumeMoonVisibilityEnum::INVISIBLE
|| visibility == MumeMoonVisibilityEnum::UNKNOWN)
? "color:black;background:grey"
: (visibility == MumeMoonVisibilityEnum::BRIGHT)
? "color:black;background:yellow"
: "color:black;background:white";
moonPhaseLabel->setStyleSheet(moonStyleSheet);
}
void MumeClockWidget::updateSeason(MumeSeasonEnum season)
{
QString styleSheet = "color:black";
QString text = "";
switch (season) {
case MumeSeasonEnum::WINTER:
styleSheet = "color:black;background:white";
text = "Winter";
break;
case MumeSeasonEnum::SPRING:
styleSheet = "color:white;background:teal";
text = "Spring";
break;
case MumeSeasonEnum::SUMMER:
styleSheet = "color:white;background:green";
text = "Summer";
break;
case MumeSeasonEnum::AUTUMN:
styleSheet = "color:black;background:orange";
text = "Autumn";
break;
case MumeSeasonEnum::UNKNOWN:
default:
break;
}
seasonLabel->setStyleSheet(styleSheet);
seasonLabel->setText(text);
}
void MumeClockWidget::updateWeather(PromptWeatherEnum weather)
{
switch (weather) {
case PromptWeatherEnum::CLOUDS:
weatherLabel->setText(QString::fromUtf8("\xE2\x98\x81"));
weatherLabel->setStatusTip("Cloudy");
weatherLabel->setVisible(true);
break;
case PromptWeatherEnum::RAIN:
weatherLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\xA7"));
weatherLabel->setStatusTip("Rainy");
weatherLabel->setVisible(true);
break;
case PromptWeatherEnum::HEAVY_RAIN:
weatherLabel->setText(QString::fromUtf8("\xE2\x9B\x88"));
weatherLabel->setStatusTip("Heavy Rain");
weatherLabel->setVisible(true);
break;
case PromptWeatherEnum::SNOW:
weatherLabel->setText(QString::fromUtf8("\xE2\x9D\x84"));
weatherLabel->setStatusTip("Snowy");
weatherLabel->setVisible(true);
break;
case PromptWeatherEnum::NICE:
default:
weatherLabel->setText("");
weatherLabel->setStatusTip("");
weatherLabel->setVisible(false);
break;
}
}
void MumeClockWidget::updateFog(PromptFogEnum fog)
{
switch (fog) {
case PromptFogEnum::LIGHT_FOG:
fogLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\xAB"));
fogLabel->setStatusTip("Light Fog");
fogLabel->setVisible(true);
break;
case PromptFogEnum::HEAVY_FOG:
fogLabel->setText(QString::fromUtf8("\xF0\x9F\x8C\xAB\xF0\x9F\x8C\xAB"));
fogLabel->setStatusTip("Heavy Fog");
fogLabel->setVisible(true);
break;
case PromptFogEnum::NO_FOG:
default:
fogLabel->setText("");
fogLabel->setStatusTip("");
fogLabel->setVisible(false);
break;
}
}
void MumeClockWidget::updateCountdown(const MumeMoment &moment)
{
// FIXME: Use ChangeMonitor
setVisible(getConfig().mumeClock.display);
if (!getConfig().mumeClock.display) {
return;
}
const MumeClockPrecisionEnum precision = m_clock.getPrecision();
if (precision <= MumeClockPrecisionEnum::HOUR) {
timeLabel->setText(QString::fromUtf8("\xE2\x9A\xA0").append(m_clock.toCountdown(moment)));
} else {
timeLabel->setText(m_clock.toCountdown(moment));
}
}
void MumeClockWidget::updateStatusTips(const MumeMoment &moment)
{
moonPhaseLabel->setStatusTip(moment.toMumeMoonTime());
seasonLabel->setStatusTip(m_clock.toMumeTime(moment));
QString statusTip = "";
const MumeClockPrecisionEnum precision = m_clock.getPrecision();
const auto time = moment.toTimeOfDay();
if (time == MumeTimeEnum::DAWN) {
statusTip = "Ticks left until day";
} else if (time >= MumeTimeEnum::DUSK) {
statusTip = "Ticks left until day";
} else {
statusTip = "Ticks left until night";
}
if (precision != MumeClockPrecisionEnum::MINUTE) {
statusTip = "The clock has not synced with MUME! Click to override at your own risk.";
}
timeLabel->setStatusTip(statusTip);
}