-
Notifications
You must be signed in to change notification settings - Fork 51
/
pngexport.cpp
157 lines (136 loc) · 5.51 KB
/
pngexport.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
#include "pngexport.h"
#include "ui_pngexport.h"
#include <QSettings>
#include <cmath>
PngExport::PngExport(QWidget *parent)
: QDialog(parent)
, ui(new Ui::PngExport)
, snapDistance(16)
{
ui->setupUi(this);
// remove "question mark" in title bar
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
// connect value change signals to check slots of "other" spinbox
connect(ui->spinBox_top, QOverload<int>::of(&QSpinBox::valueChanged),
this, &PngExport::checkTop);
connect(ui->spinBox_left, QOverload<int>::of(&QSpinBox::valueChanged),
this, &PngExport::checkLeft);
connect(ui->spinBox_bottom, QOverload<int>::of(&QSpinBox::valueChanged),
this, &PngExport::checkBottom);
connect(ui->spinBox_right, QOverload<int>::of(&QSpinBox::valueChanged),
this, &PngExport::checkRight);
// connect radio buttons to change single-step distance
connect(ui->radioButton_chunk, &QRadioButton::toggled,
this, &PngExport::setSingleStep);
connect(ui->radioButton_region, &QRadioButton::toggled,
this, &PngExport::setSingleStep);
// load the settings
QSettings settings;
if (settings.value("PNGExportSnapDistance", 16).toInt() == 512) {
snapDistance = 512;
ui->radioButton_region->setChecked(true);
}
}
PngExport::~PngExport()
{
delete ui;
}
void PngExport::setBoundsFromChunks(int top, int left, int bottom, int right)
{
// convert from Chunks to Blocks
setBoundsFromBlocks(16*top, 16*left, 16*bottom+15, 16*right+15);
}
void PngExport::setBoundsFromBlocks(int top, int left, int bottom, int right)
{
// restrict range of spin boxes to world dimension
// -> extend to next region boundary
int e_top = 512* std::floor(float(top)/512.0);
int e_left = 512* std::floor(float(left)/512.0);
int e_bottom = 512*(std::floor(float(bottom)/512.0) +1) -1;
int e_right = 512*(std::floor(float(right)/512.0) +1) -1;
ui->spinBox_top ->setRange(e_top, e_bottom-(snapDistance-1));
ui->spinBox_left ->setRange(e_left, e_right-(snapDistance-1));
ui->spinBox_bottom->setRange(e_top+(snapDistance-1), e_bottom);
ui->spinBox_right ->setRange(e_left+(snapDistance-1), e_right);
// initialize spin boxes to complete world dimension
// -> extended to next snapDistance
e_top = snapDistance* std::floor(float(top)/float(snapDistance));
e_left = snapDistance* std::floor(float(left)/float(snapDistance));
e_bottom = snapDistance*(std::floor(float(bottom)/float(snapDistance)) +1) -1;
e_right = snapDistance*(std::floor(float(right)/float(snapDistance)) +1) -1;
ui->spinBox_top ->setValue(e_top);
ui->spinBox_left ->setValue(e_left);
ui->spinBox_bottom->setValue(e_bottom);
ui->spinBox_right ->setValue(e_right);
}
QString PngExport::getLabelText(int value)
{
QString text;
if (snapDistance == 16) {
text = "C: " + QString::number(value >> 4);
}
if (snapDistance == 512) {
text = "R: " + QString::number(value >> 9);
}
return text;
}
void PngExport::checkTop(int value)
{
value = snapDistance * std::round(float(value)/snapDistance);
ui->spinBox_top->setValue(value);
if (value+(snapDistance-1) > ui->spinBox_bottom->value())
ui->spinBox_bottom->setValue(value+(snapDistance-1));
// update Chunk/Region label
ui->label_top->setText(getLabelText(value));
}
void PngExport::checkLeft(int value)
{
value = snapDistance * std::round(float(value)/snapDistance);
ui->spinBox_left->setValue(value);
if (value+(snapDistance-1) > ui->spinBox_right->value())
ui->spinBox_right->setValue(value+(snapDistance-1));
// update Chunk/Region label
ui->label_left->setText(getLabelText(value));
}
void PngExport::checkBottom(int value)
{
value = snapDistance * std::round(float(value)/snapDistance) -1;
ui->spinBox_bottom->setValue(value);
if (value-(snapDistance-1) < ui->spinBox_top->value())
ui->spinBox_top->setValue(value-(snapDistance-1));
// update Chunk/Region label
ui->label_bottom->setText(getLabelText(value));
}
void PngExport::checkRight(int value)
{
value = snapDistance * std::round(float(value)/snapDistance) -1;
ui->spinBox_right->setValue(value);
if (value-(snapDistance-1) < ui->spinBox_left->value())
ui->spinBox_left->setValue(value-(snapDistance-1));
// update Chunk/Region label
ui->label_right->setText(getLabelText(value));
}
void PngExport::setSingleStep()
{
if (ui->radioButton_chunk ->isChecked()) snapDistance = 16;
if (ui->radioButton_region->isChecked()) snapDistance = 512;
// change single step behavior
ui->spinBox_top ->setSingleStep(snapDistance);
ui->spinBox_left ->setSingleStep(snapDistance);
ui->spinBox_bottom->setSingleStep(snapDistance);
ui->spinBox_right ->setSingleStep(snapDistance);
// validate current values
checkTop (ui->spinBox_top ->value());
checkLeft (ui->spinBox_left ->value());
checkBottom(ui->spinBox_bottom->value());
checkRight (ui->spinBox_right ->value());
// store the settings
QSettings settings;
settings.setValue("PNGExportSnapDistance", snapDistance);
}
int PngExport::getTop() const { return ui->spinBox_top ->value(); }
int PngExport::getLeft() const { return ui->spinBox_left ->value(); }
int PngExport::getBottom() const { return ui->spinBox_bottom->value(); }
int PngExport::getRight() const { return ui->spinBox_right ->value(); }
bool PngExport::getChunkChecker() const { return ui->checkBox_chunk ->checkState(); }
bool PngExport::getRegionChecker() const { return ui->checkBox_region->checkState(); }