Crossfire Server, Trunk
CRESmoothFaceMaker.cpp
Go to the documentation of this file.
1 #include "CRESmoothFaceMaker.h"
2 #include "CREPixmap.h"
3 #include <QtWidgets>
4 #include "FaceComboBox.h"
5 
6 extern "C" {
7 #include "global.h"
8 #include "image.h"
9 }
10 
11 #include "assets.h"
12 #include "AssetsManager.h"
13 
15 {
16  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
17 
18  myAutoClose = false;
19  myOverwrite = false;
20 
21  QGridLayout* layout = new QGridLayout(this);
22  int line = 0;
23 
24  layout->addWidget(new QLabel(tr("Face to use:"), this), line, 0);
25  myFace = new FaceComboBox(this, false);
26  layout->addWidget(myFace, line++, 1, 1, 2);
27 
28  layout->addWidget(new QLabel(tr("Path of the picture to create:"), this), line, 0);
29  myDestination = new QLineEdit();
30  layout->addWidget(myDestination, line, 1);
31  connect(myDestination, SIGNAL(textEdited(const QString &)), this, SLOT(destinationEdited(const QString&)));
32 
33  QPushButton* browse = new QPushButton(tr("Browse"), this);
34  layout->addWidget(browse, line++, 2);
35  connect(browse, SIGNAL(clicked(bool)), this, SLOT(browse(bool)));
36 
37  QDialogButtonBox* box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal, this);
38  layout->addWidget(box, line++, 1, 1, 3);
39  connect(box, SIGNAL(rejected()), this, SLOT(reject()));
40  connect(box, SIGNAL(accepted()), this, SLOT(makeSmooth()));
41 
42  setWindowTitle(tr("Smooth face base generator"));
43 
44  QApplication::restoreOverrideCursor();
45 }
46 
48 {
49 }
50 
52 {
53  myFace->setFace(face);
54 }
55 
57 {
58  return myDestination->text();
59 }
60 
62 {
63  myAutoClose = autoClose;
64 }
65 
67 {
68  if (destination().isEmpty())
69  {
70  QMessageBox::warning(this, tr("Oops"), tr("You must select a destination!"));
71  return;
72  }
73 
74  if (!myOverwrite && QFile::exists(destination()))
75  {
76  if (QMessageBox::question(this, tr("Confirm file overwrite"), tr("File %1 already exists. Overwrite it?").arg(destination()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
77  return;
78  }
79 
80  QPixmap pic(32 * 16, 32 * 2);
81  pic.fill(QColor(0, 0, 0, 0));
82  QImage icon = CREPixmap::getIcon(myFace->face()->number).pixmap(32, 32).toImage();
83  QPainter device(&pic);
84  device.setBackground(QColor(0, 0, 0, 0));
85 
86  long spots[] = {
87  0x0, /* 0b000000000 */
88  0x49, /* 0b001001001 */
89  0x7, /* 0b000000111 */
90  0x4f, /* 0b001001111 */
91  0x124, /* 0b100100100 */
92  0x16d, /* 0b101101101 */
93  0x127, /* 0b100100111 */
94  0x16f, /* 0b101101111 */
95  0x1c0, /* 0b111000000 */
96  0x1c9, /* 0b111001001 */
97  0x1c7, /* 0b111000111 */
98  0x1cf, /* 0b111001111 */
99  0x1e4, /* 0b111100100 */
100  0x1ed, /* 0b111101101 */
101  0x1e7, /* 0b111100111 */
102  0x1ef, /* 0b111101111 */
103  0x0, /* 0b000000000 */
104  0x1, /* 0b000000001 */
105  0x4, /* 0b000000100 */
106  0x5, /* 0b000000101 */
107  0x100, /* 0b100000000 */
108  0x101, /* 0b100000001 */
109  0x104, /* 0b100000100 */
110  0x105, /* 0b100000101 */
111  0x40, /* 0b001000000 */
112  0x41, /* 0b001000001 */
113  0x44, /* 0b001000100 */
114  0x45, /* 0b001000101 */
115  0x140, /* 0b101000000 */
116  0x141, /* 0b101000001 */
117  0x144, /* 0b101000100 */
118  0x145, /* 0b101000101 */
119  };
120 
121 
122  int sx, sy, sw, sh, dx, dy, spot;
123  for (int x = 0; x < 16; x++) {
124  for (int y = 0; y < 2; y++) {
125  spot = y * 16 + x;
126 
127  for (int bit = 0; bit < 9; bit++) {
128  if ((spots[spot] & (1 << bit)) == 0)
129  continue;
130 
131  dx = x * 32;
132  dy = y * 32;
133  sw = 8;
134  sh = 8;
135 
136  switch (bit / 3)
137  {
138  case 0:
139  sy = 0;
140  break;
141  case 1:
142  sy = 8;
143  dy += 8;
144  sh = 16;
145  break;
146  case 2:
147  sy = 24;
148  dy += 24;
149  break;
150  default:
151  Q_ASSERT(false);
152  }
153 
154  switch (bit % 3)
155  {
156  case 0:
157  sx = 0;
158  break;
159  case 1:
160  sx = 8;
161  dx += 8;
162  sw = 16;
163  break;
164  case 2:
165  sx = 24;
166  dx += 24;
167  break;
168  default:
169  Q_ASSERT(false);
170  }
171 
172  device.drawImage(dx, dy, icon, sx, sy, sw, sh);
173  }
174 
175  }
176  }
177 
178  if (!pic.save(destination(), "PNG"))
179  {
180  QMessageBox::critical(this, tr("Error"), tr("Error while saving the picture as %1!").arg(destination()));
181  return;
182  }
183 
184  QMessageBox::information(this, tr("Smooth base saved"), tr("The smooth base was correctly saved as %1").arg(destination()));
185 
186  if (myAutoClose)
187  accept();
188 
189  myOverwrite = false;
190 }
191 
193 {
194  QString dest = QFileDialog::getSaveFileName(this, tr("Select destination file"), "", tr("PNG file (*.png);;All files (*.*)"));
195  if (dest.isEmpty())
196  return;
197 
198  myDestination->setText(dest);
199  myOverwrite = true;
200 }
201 
203 {
204  myOverwrite = false;
205 }
Face
Definition: face.h:14
global.h
layout
Definition: main.c:85
FaceComboBox
Definition: FaceComboBox.h:11
diamondslots.x
x
Definition: diamondslots.py:15
AssetsManager.h
CREPixmap::getIcon
static QIcon getIcon(uint16_t faceNumber)
Definition: CREPixmap.cpp:55
CRESmoothFaceMaker::myFace
FaceComboBox * myFace
Definition: CRESmoothFaceMaker.h:33
CRESmoothFaceMaker::destinationEdited
void destinationEdited(const QString &)
Definition: CRESmoothFaceMaker.cpp:202
CRESmoothFaceMaker::CRESmoothFaceMaker
CRESmoothFaceMaker()
Definition: CRESmoothFaceMaker.cpp:14
is_valid_types_gen.line
line
Definition: is_valid_types_gen.py:34
Face::number
uint16_t number
Definition: face.h:15
FaceComboBox::setFace
void setFace(const Face *face)
Definition: FaceComboBox.cpp:33
CRESmoothFaceMaker::myDestination
QLineEdit * myDestination
Definition: CRESmoothFaceMaker.h:32
say.box
box
Definition: say.py:152
image.h
CRESmoothFaceMaker::myAutoClose
bool myAutoClose
Definition: CRESmoothFaceMaker.h:30
CRESmoothFaceMaker::destination
QString destination() const
Definition: CRESmoothFaceMaker.cpp:56
CRESmoothFaceMaker::browse
void browse(bool)
Definition: CRESmoothFaceMaker.cpp:192
CRESmoothFaceMaker::makeSmooth
void makeSmooth()
Definition: CRESmoothFaceMaker.cpp:66
convert.dest
dest
Definition: convert.py:25
diamondslots.y
y
Definition: diamondslots.py:16
assets.h
CRESmoothFaceMaker::setAutoClose
void setAutoClose(bool autoClose=true)
Definition: CRESmoothFaceMaker.cpp:61
FaceComboBox::face
const Face face
Definition: FaceComboBox.h:15
FaceComboBox.h
connect
Definition: connect.py:1
CRESmoothFaceMaker.h
altar_valkyrie.accept
def accept(description)
Definition: altar_valkyrie.py:22
CRESmoothFaceMaker::myOverwrite
bool myOverwrite
Definition: CRESmoothFaceMaker.h:31
CREPixmap.h
CRESmoothFaceMaker::setSelectedFace
void setSelectedFace(const Face *face)
Definition: CRESmoothFaceMaker.cpp:51
CRESmoothFaceMaker::~CRESmoothFaceMaker
virtual ~CRESmoothFaceMaker()
Definition: CRESmoothFaceMaker.cpp:47