-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShijimaWidget.cc
371 lines (341 loc) · 11 KB
/
ShijimaWidget.cc
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//
// Shijima-Qt - Cross-platform shimeji simulation app for desktop
// Copyright (C) 2025 pixelomer
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#include "ShijimaWidget.hpp"
#include <QWidget>
#include <QPainter>
#include <QFile>
#include <QDir>
#include <QScreen>
#include <QMouseEvent>
#include <QMenu>
#include <QWindow>
#include <QDebug>
#include <QGuiApplication>
#include <QTextStream>
#include <shijima/shijima.hpp>
#include "Platform/Platform.hpp"
#include "ShimejiInspectorDialog.hpp"
#include "AssetLoader.hpp"
#include "ShijimaContextMenu.hpp"
#include "ShijimaManager.hpp"
#include <shimejifinder/utils.hpp>
using namespace shijima;
ShijimaWidget::ShijimaWidget(MascotData *mascotData,
std::unique_ptr<shijima::mascot::manager> mascot,
int mascotId, bool windowedMode, QWidget *parent):
#if defined(__APPLE__)
PlatformWidget(nullptr, PlatformWidget::ShowOnAllDesktops),
#else
PlatformWidget(parent, PlatformWidget::ShowOnAllDesktops),
#endif
m_windowedMode(windowedMode), m_data(mascotData),
m_inspector(nullptr), m_mascotId(mascotId)
{
m_windowHeight = 128;
m_windowWidth = 128;
m_mascot = std::move(mascot);
QDir dir { m_data->imgRoot() };
if (dir.exists() && dir.cdUp() && dir.cd("sound")) {
m_sounds.searchPaths.push_back(dir.path());
}
if (!m_windowedMode) {
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_ShowWithoutActivating);
setAttribute(Qt::WA_MacShowFocusRect, false);
Qt::WindowFlags flags = Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint
| Qt::WindowDoesNotAcceptFocus | Qt::NoDropShadowWindowHint
| Qt::WindowOverridesSystemGestures;
#if defined(__APPLE__)
flags |= Qt::Window;
#else
flags |= Qt::Tool;
#endif
setWindowFlags(flags);
}
setFixedSize(m_windowWidth, m_windowHeight);
}
ShijimaWidget::ShijimaWidget(ShijimaWidget &old, bool windowedMode,
QWidget *parent) : ShijimaWidget(old.mascotData(),
std::move(old.m_mascot), old.m_mascotId,
windowedMode, parent) {}
void ShijimaWidget::showInspector() {
if (m_inspector == nullptr) {
m_inspector = new ShimejiInspectorDialog { this };
}
m_inspector->show();
}
bool ShijimaWidget::inspectorVisible() {
return m_inspector != nullptr && m_inspector->isVisible();
}
Asset const& ShijimaWidget::getActiveAsset() {
auto &name = m_mascot->state->active_frame.get_name(m_mascot->state->looking_right);
auto lowerName = shimejifinder::to_lower(name);
auto imagePath = QDir::cleanPath(m_data->imgRoot()
+ QDir::separator() + QString::fromStdString(lowerName));
return AssetLoader::defaultLoader()->loadAsset(imagePath);
}
bool ShijimaWidget::isMirroredRender() const {
return m_mascot->state->active_frame.right_name.empty() &&
m_mascot->state->looking_right;
}
void ShijimaWidget::paintEvent(QPaintEvent *event) {
if (!m_visible) {
return;
}
auto &asset = getActiveAsset();
auto &image = asset.image(isMirroredRender());
auto scaledSize = image.size() / m_drawScale;
QPainter painter(this);
painter.drawImage(QRect { m_drawOrigin, scaledSize }, image);
#ifdef __linux__
if (Platform::useWindowMasks()) {
m_windowMask = QBitmap::fromPixmap(asset.mask(isMirroredRender())
.scaled(scaledSize));
m_windowMask.translate(m_drawOrigin);
auto bounding = m_windowMask.boundingRect();
bounding.setTop(0);
bounding.setLeft(0);
if (bounding.width() > 0 && bounding.height() > 0) {
setMask(m_windowMask);
}
else {
setMask(QRect { m_windowWidth - 2, m_windowHeight - 2, 1, 1 });
}
}
#endif
}
bool ShijimaWidget::updateOffsets() {
bool needsRepaint = false;
auto &frame = m_mascot->state->active_frame;
auto &asset = getActiveAsset();
// Does the image go outside of the minimum boundary? If so,
// extend the window boundary
int originalWidth = asset.originalSize().width();
int originalHeight = asset.originalSize().height();
double scale = m_mascot->state->env->get_scale();
int screenWidth = (int)(m_mascot->state->env->screen.width()
/ scale);
int screenHeight = (int)(m_mascot->state->env->screen.height()
/ scale);
int windowWidth = (int)(originalWidth / scale);
int windowHeight = (int)(originalHeight / scale);
if (windowWidth != m_windowWidth) {
m_windowWidth = windowWidth;
setFixedWidth(m_windowWidth);
needsRepaint = true;
}
if (windowHeight != m_windowHeight) {
m_windowHeight = windowHeight;
setFixedHeight(m_windowHeight);
needsRepaint = true;
}
// Determine the frame anchor within the window
if (isMirroredRender()) {
m_anchorInWindow = {
(int)((originalWidth - frame.anchor.x) / scale),
(int)(frame.anchor.y / scale) };
}
else {
m_anchorInWindow = { (int)(frame.anchor.x / scale),
(int)(frame.anchor.y / scale) };
}
// Detemine draw offsets and window positions
QPoint drawOffset;
m_visible = true;
int winX = (int)m_mascot->state->anchor.x - m_anchorInWindow.x()
- (int)env()->screen.left;
int winY = (int)m_mascot->state->anchor.y - m_anchorInWindow.y()
- (int)env()->screen.top;
if (winX < 0) {
drawOffset.setX(winX);
winX = 0;
}
else if (winX + windowWidth > screenWidth) {
drawOffset.setX(winX - screenWidth + windowWidth);
winX = screenWidth - windowWidth;
}
if (winY < 0) {
drawOffset.setY(winY);
winY = 0;
}
else if (winY + windowHeight > screenHeight) {
drawOffset.setY(winY - screenHeight + windowHeight);
winY = screenHeight - windowHeight;
}
winX += (int)env()->screen.left;
winY += (int)env()->screen.top;
if (isMirroredRender()) {
drawOffset += QPoint {
(int)((originalWidth - asset.offset().topRight().x()) / scale),
(int)(asset.offset().topLeft().y() / scale) };
}
else {
drawOffset += asset.offset().topLeft() / scale;
}
if (drawOffset != m_drawOrigin) {
needsRepaint = true;
m_drawOrigin = drawOffset;
}
if (scale != m_drawScale) {
needsRepaint = true;
m_drawScale = scale;
}
move(winX, winY);
return needsRepaint;
}
bool ShijimaWidget::pointInside(QPoint const& point) {
if (!m_visible) {
return false;
}
auto &asset = getActiveAsset();
auto image = asset.image(isMirroredRender());
int drawnWidth = (int)(image.width() / m_drawScale);
int drawnHeight = (int)(image.height() / m_drawScale);
auto imagePos = point - m_drawOrigin;
if (imagePos.x() < 0 || imagePos.y() < 0 ||
imagePos.x() > drawnWidth || imagePos.y() > drawnHeight)
{
return false;
}
//FIXME: is this position correct?
auto color = image.pixelColor(imagePos * m_drawScale);
if (color.alpha() == 0) {
return false;
}
return true;
}
void ShijimaWidget::tick() {
if (m_markedForDeletion) {
close();
return;
}
if (paused()) {
return;
}
// Tick
auto prev_frame = m_mascot->state->active_frame;
m_mascot->tick();
auto &new_frame = m_mascot->state->active_frame;
auto &new_sound = m_mascot->state->active_sound;
bool forceRepaint = prev_frame.name != new_frame.name;
bool offsetsChanged = updateOffsets();
if (m_mascot->state->dead) {
forceRepaint = true;
new_frame.name = "";
new_sound = "";
m_mascot->state->active_sound_changed = true;
markForDeletion();
}
if (offsetsChanged || forceRepaint) {
repaint();
update();
}
if (m_mascot->state->active_sound_changed) {
m_sounds.stop();
if (!new_sound.empty()) {
m_sounds.play(QString::fromStdString(new_sound));
}
}
else if (!m_sounds.playing()) {
m_mascot->state->active_sound.clear();
}
// Update inspector
if (m_inspector != nullptr && m_inspector->isVisible()) {
m_inspector->tick();
}
}
void ShijimaWidget::contextMenuClosed(QCloseEvent *event) {
m_contextMenuVisible = false;
}
void ShijimaWidget::showContextMenu(QPoint const& pos) {
m_contextMenuVisible = true;
ShijimaContextMenu *menu = new ShijimaContextMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->popup(pos);
}
ShijimaWidget::~ShijimaWidget() {
if (m_dragTargetPt != nullptr) {
*m_dragTargetPt = nullptr;
m_dragTargetPt = nullptr;
}
if (m_inspector != nullptr) {
m_inspector->close();
delete m_inspector;
}
setDragTarget(nullptr);
}
void ShijimaWidget::setDragTarget(ShijimaWidget *target) {
if (m_dragTarget != nullptr) {
m_dragTarget->m_dragTargetPt = nullptr;
}
if (target != nullptr) {
if (target->m_dragTargetPt != nullptr) {
throw std::runtime_error("target widget being dragged by multiple widgets");
}
m_dragTarget = target;
m_dragTarget->m_dragTargetPt = &m_dragTarget;
}
else {
m_dragTarget = nullptr;
}
}
void ShijimaWidget::mousePressEvent(QMouseEvent *event) {
auto pos = event->pos();
if (m_dragTarget != nullptr) {
m_dragTarget->m_mascot->state->dragging = false;
}
if (pointInside(pos)) {
setDragTarget(this);
}
else {
QPoint envPos;
if (m_windowedMode) {
envPos = mapToParent(pos);
}
else {
envPos = mapToGlobal(pos);
}
ShijimaWidget *target = ShijimaManager::defaultManager()->hitTest(envPos);
setDragTarget(target);
if (target == nullptr) {
event->ignore();
return;
}
}
if (event->button() == Qt::MouseButton::LeftButton) {
m_dragTarget->m_mascot->state->dragging = true;
}
else if (event->button() == Qt::MouseButton::RightButton) {
auto screenPos = mapToGlobal(pos);
m_dragTarget->showContextMenu(screenPos);
setDragTarget(nullptr);
}
}
void ShijimaWidget::closeAction() {
close();
}
void ShijimaWidget::mouseReleaseEvent(QMouseEvent *event) {
if (m_dragTarget == nullptr) {
return;
}
if (event->button() == Qt::MouseButton::LeftButton) {
m_dragTarget->m_mascot->state->dragging = false;
setDragTarget(nullptr);
}
}