-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEditAnonymousNPC.cpp
200 lines (150 loc) · 5.83 KB
/
EditAnonymousNPC.cpp
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
#include <QFileDialog>
#include <QMessageBox>
#include <QDoubleValidator>
#include <QMenu>
#include "EditAnonymousNPC.h"
#include "ImageDimensions.h"
#include "Level.h"
#include "LevelNPC.h"
#include "LevelCommands.h"
#include "DarkTitleBar.h"
#include "gs1/GS1Converter.h"
namespace TilesEditor
{
void EditAnonymousNPC::accept()
{
if (m_modified)
{
m_npc->setX(ui.xText->text().toDouble() * m_npc->getUnitWidth());
m_npc->setY(ui.yText->text().toDouble() * m_npc->getUnitHeight());
m_npc->setImageName(ui.imageText->text());
m_npc->setCode(ui.plainTextEdit->toPlainText());
m_world->setModified(m_npc->getLevel());
m_world->updateMovedEntity(m_npc);
}
QDialog::accept();
}
void EditAnonymousNPC::reject()
{
if (m_modified)
{
if (QMessageBox::question(nullptr, "Changes made", "You have made changes to the NPC. Are you sure you want to close?") == QMessageBox::Yes)
{
QDialog::reject();
}
} else QDialog::reject();
}
void EditAnonymousNPC::imageBrowsePressed()
{
auto fileName = m_world->getResourceManager().getFileSystem()->getOpenFileName("Select Image", m_world->getResourceManager().getRootDir(), "Image Files (*.png *.gif)");
if (!fileName.isEmpty())
{
QFileInfo fi(fileName);
auto directory = fi.absolutePath() + "/";
m_world->getResourceManager().addSearchDir(directory);
ui.imageText->setText(fi.fileName());
int width, height;
if (ImageDimensions::getImageFileDimensions(fileName, &width, &height))
{
ui.widthText->setText(QString::number(width));
ui.heightText->setText(QString::number(height));
}
m_modified = true;
}
}
void EditAnonymousNPC::positionChanged()
{
m_modified = true;
auto posX = std::floor((ui.xText->text().toDouble() * m_npc->getUnitWidth()) * 100.0) / 100.0;
auto posY = std::floor((ui.yText->text().toDouble() * m_npc->getUnitHeight()) * 100.0) / 100.0;
ui.localXText->setText(QString::number((posX - m_npc->getLevel()->getX()) / m_npc->getUnitWidth(), 'f', 2));
ui.localYText->setText(QString::number((posY - m_npc->getLevel()->getY()) / m_npc->getUnitHeight(), 'f', 2));
}
void EditAnonymousNPC::deletePressed()
{
if (QMessageBox::question(nullptr, "Warning", "Are you sure you want to delete this NPC?") == QMessageBox::Yes)
{
m_modified = false;
m_world->deleteEntity(m_npc);
QDialog::reject();
}
}
void EditAnonymousNPC::convertPressed()
{
auto originalCode = ui.plainTextEdit->toPlainText();
//Clear
auto doc = ui.plainTextEdit->document();
QTextCursor curs(doc);
curs.select(QTextCursor::Document);
curs.removeSelectedText();
curs.insertText(QString::fromStdString(GS1Converter::convert3(originalCode.toStdString())));
m_modified = true;
}
void EditAnonymousNPC::revertPressed()
{
auto originalCode = ui.plainTextEdit->toPlainText();
//Clear
auto doc = ui.plainTextEdit->document();
QTextCursor curs(doc);
curs.select(QTextCursor::Document);
curs.removeSelectedText();
curs.insertText(QString::fromStdString(GS1Converter::revert(originalCode.toStdString())));
m_modified = true;
}
void EditAnonymousNPC::textEdited(const QString& text)
{
m_modified = true;
}
QByteArray EditAnonymousNPC::savedGeometry;
EditAnonymousNPC::EditAnonymousNPC(LevelNPC* npc, IWorld* world, QWidget* parent)
: QDialog(parent)
{
DarkTitleBar::ApplyStyle(this->winId());
using namespace kgl;
m_modified = false;
m_npc = npc;
m_world = world;
ui.setupUi(this);
this->setWindowFlag(Qt::Window);
this->setWindowFlag(Qt::WindowMaximizeButtonHint);
// ## MainWindow::MainWindow
static QCodeEditorDesign design;
// modify design ...
static QList<QSyntaxRule> rules =
QSyntaxRules::loadFromFile(":/MainWindow/rule_cpp.xml", design);
ui.plainTextEdit->setRules(rules);
ui.plainTextEdit->setFrameShape(QFrame::StyledPanel);
ui.plainTextEdit->setPlainText(m_npc->getCode());
ui.imageText->setText(m_npc->getImageName());
ui.xText->setValidator(new QDoubleValidator(-1000000000, 1000000000, 2, this));
ui.yText->setValidator(new QDoubleValidator(-1000000000, 1000000000, 2, this));
ui.xText->setText(QString::number(std::floor((npc->getX() / npc->getUnitWidth()) * 100.0) / 100.0));
ui.yText->setText(QString::number(std::floor((npc->getY() / npc->getUnitHeight()) * 100.0) / 100.0));
ui.localXText->setText(QString::number((npc->getX() - npc->getLevel()->getX()) / npc->getUnitWidth(), 'f', 2));
ui.localYText->setText(QString::number((npc->getY() - npc->getLevel()->getY()) / npc->getUnitHeight(), 'f', 2));
ui.widthText->setText(QString::number(npc->getWidth()));
ui.heightText->setText(QString::number(npc->getHeight()));
ui.plainTextEdit->setWordWrapMode(QTextOption::NoWrap);
connect(ui.plainTextEdit, &QCodeEditor::lineChanged, this, &EditAnonymousNPC::editorLineChanged);
connect(ui.imageBrowse, &QPushButton::pressed, this, &EditAnonymousNPC::imageBrowsePressed);
connect(ui.xText, &QLineEdit::textEdited, this, &EditAnonymousNPC::textEdited);
connect(ui.yText, &QLineEdit::textEdited, this, &EditAnonymousNPC::textEdited);
connect(ui.imageText, &QLineEdit::textEdited, this, &EditAnonymousNPC::textEdited);
connect(ui.deleteButton, &QPushButton::pressed, this, &EditAnonymousNPC::deletePressed);
connect(ui.convertButton, &QPushButton::pressed, this, &EditAnonymousNPC::convertPressed);
connect(ui.revertButton, &QPushButton::pressed, this, &EditAnonymousNPC::revertPressed);
//connect(ui.convertButton, &QToolButton::triggered, this, &EditAnonymousNPC::convertPressed);
if (!savedGeometry.isNull())
restoreGeometry(savedGeometry);
}
EditAnonymousNPC::~EditAnonymousNPC()
{
//Dno why but have to do this (this control still sends signals after this class is destroyed)
ui.plainTextEdit->blockSignals(true);
savedGeometry = saveGeometry();
}
void EditAnonymousNPC::editorLineChanged(QTextBlock block)
{
m_modified = true;
}
};