OSDN Git Service

shrink mine
[nethackexpress/trunk.git] / win / Qt / tileedit.cpp
1 /*      SCCS Id: @(#)tileedit.cpp       3.4     1999/11/19      */
2 /* Copyright (c) Warwick Allison, 1999. */
3 /* NetHack may be freely redistributed.  See license for details. */
4 /*
5 Build this little utility program if you want to use it to edit the tile
6 files.  Move tileedit.cpp and tileedit.h to ../../util, add the
7 3 lines below to the Makefile there and "make tileedit".
8
9 tileedit: tileedit.cpp $(TEXT_IO)
10         moc -o tileedit.moc tileedit.h
11         $(CC) -o tileedit -I../include -I$(QTDIR)/include -L$(QTDIR)/lib tileedit.cpp $(TEXT_IO) -lqt
12 */
13
14
15 #include "tileedit.h"
16 #include <qapplication.h>
17 #include <qmainwindow.h>
18 #include <qkeycode.h>
19 #include <qpopupmenu.h>
20 #include <qmenubar.h>
21 #include <qpainter.h>
22 #include <qstatusbar.h>
23 #include <qhbox.h>
24 #include <qlabel.h>
25
26 extern "C" {
27 #include "config.h"
28 #include "tile.h"
29 extern const char *FDECL(tilename, (int, int));
30 }
31
32 #define TILES_ACROSS 20
33
34 TilePickerTab::TilePickerTab(const char* basename, int i, QWidget* parent) :
35     QWidget(parent)
36 {
37     id = i;
38     filename = basename;
39     filename += ".txt";
40     num = 0;
41     int index = 0;
42     for (int real=0; real<2; real++) {
43         if ( real ) {
44             image.create( TILES_ACROSS*TILE_X,
45                 ((num+TILES_ACROSS-1)/TILES_ACROSS)*TILE_Y, 32 );
46         }
47         if ( !fopen_text_file(filename.latin1(), RDTMODE) ) {
48             // XXX handle better
49             exit(1);
50         }
51         pixel p[TILE_Y][TILE_X];
52         while ( read_text_tile(p) ) {
53             if ( real ) {
54                 int ox = (index%TILES_ACROSS)*TILE_X;
55                 int oy = (index/TILES_ACROSS)*TILE_Y;
56                 for ( int y=0; y<TILE_Y; y++ ) {
57                     QRgb* rgb = ((QRgb*)image.scanLine(oy+y)) + ox;
58                     for ( int x=0; x<TILE_X; x++ ) {
59                         *rgb++ = qRgb(p[y][x].r, p[y][x].g, p[y][x].b);
60                     }
61                 }
62                 index++;
63             } else {
64                 // Just count...
65                 num++;
66             }
67         }
68         fclose_text_file();
69     }
70     image = image.convertDepth( 8, AvoidDither );
71     pixmap.convertFromImage( image );
72 }
73
74 bool TilePickerTab::save()
75 {
76     if ( !fopen_text_file(filename.latin1(), WRTMODE) ) {
77         // XXX handle better
78         exit(1);
79     }
80     pixel p[TILE_Y][TILE_X];
81     for ( int index=0; index < num; index++ ) {
82         int ox = (index%TILES_ACROSS)*TILE_X;
83         int oy = (index/TILES_ACROSS)*TILE_Y;
84         for ( int y=0; y<TILE_Y; y++ ) {
85             uchar* c = image.scanLine(oy+y) + ox;
86             for ( int x=0; x<TILE_X; x++ ) {
87                 QRgb rgb = image.color(*c++);
88                 p[y][x].r = qRed(rgb);
89                 p[y][x].g = qGreen(rgb);
90                 p[y][x].b = qBlue(rgb);
91             }
92         }
93         write_text_tile(p);
94     }
95     fclose_text_file();
96 }
97
98 void TilePickerTab::mousePressEvent(QMouseEvent* e)
99 {
100     int ox = e->x()-e->x()%TILE_X;
101     int oy = e->y()-e->y()%TILE_Y;
102     QImage subimage = image.copy(ox,oy,TILE_X,TILE_Y);
103     if ( e->button() == RightButton ) {
104         setCurrent(subimage);
105     } else {
106         last_pick = ox/TILE_X + oy/TILE_Y*TILES_ACROSS;
107     }
108     emit pick(subimage);
109     emit pickName(tilename(id, last_pick));
110 }
111
112 void TilePickerTab::setCurrent(const QImage& i)
113 {
114     int ox = last_pick%TILES_ACROSS * TILE_X;
115     int oy = last_pick/TILES_ACROSS * TILE_Y;
116     bitBlt( &image, ox, oy, &i );
117     bitBlt( &pixmap, ox, oy, &i );
118     repaint( ox, oy, TILE_X, TILE_Y, FALSE );
119 }
120
121 QSize TilePickerTab::sizeHint() const
122 {
123     return pixmap.size();
124 }
125
126 void TilePickerTab::paintEvent( QPaintEvent* )
127 {
128     QPainter p(this);
129     p.drawPixmap(0,0,pixmap);
130 }
131
132 static struct {
133     const char* name;
134     TilePickerTab* tab;
135 } tileset[] = {
136     { "monsters", 0 },
137     { "objects", 0 },
138     { "other", 0 },
139     { 0 }
140 };
141
142 TilePicker::TilePicker(QWidget* parent) :
143     QTabWidget(parent)
144 {
145     for (int i=0; tileset[i].name; i++) {
146         QString tabname = tileset[i].name;
147         tabname[0] = tabname[0].upper();
148         tileset[i].tab = new TilePickerTab(tileset[i].name,i+1,this);
149         addTab( tileset[i].tab, tabname );
150         connect( tileset[i].tab, SIGNAL(pick(const QImage&)),
151                  this, SIGNAL(pick(const QImage&)) );
152         connect( tileset[i].tab, SIGNAL(pickName(const QString&)),
153                  this, SIGNAL(pickName(const QString&)) );
154     }
155 }
156
157 void TilePicker::setCurrent(const QImage& i)
158 {
159     ((TilePickerTab*)currentPage())->setCurrent(i);
160 }
161
162 void TilePicker::save()
163 {
164     for (int i=0; tileset[i].tab; i++) {
165         tileset[i].tab->save();
166     }
167 }
168
169 TrivialTileEditor::TrivialTileEditor( QWidget* parent ) :
170     QWidget(parent)
171 {
172 }
173
174 const QImage& TrivialTileEditor::image() const
175 {
176     return img;
177 }
178
179 void TrivialTileEditor::setColor( QRgb rgb )
180 {
181     pen = rgb;
182     for (penpixel = 0;
183             penpixel<img.numColors()-1 && (img.color(penpixel)&0xffffff)!=(pen.rgb()&0xffffff);
184             penpixel++)
185         continue;
186 }
187
188 void TrivialTileEditor::setImage( const QImage& i )
189 {
190     img = i;
191     setColor(pen.rgb()); // update penpixel
192     repaint(FALSE);
193 }
194
195 void TrivialTileEditor::paintEvent( QPaintEvent* e )
196 {
197     QRect r = e->rect();
198     QPoint tl = imagePoint(r.topLeft());
199     QPoint br = imagePoint(r.bottomRight());
200     r = QRect(tl,br).intersect(img.rect());
201     QPainter painter(this);
202     for (int y=r.top(); y<=r.bottom(); y++) {
203         for (int x=r.left(); x<=r.right(); x++) {
204             paintPoint(painter,QPoint(x,y));
205         }
206     }
207 }
208
209 void TrivialTileEditor::paintPoint(QPainter& painter, QPoint p)
210 {
211     QPoint p1 = screenPoint(p);
212     QPoint p2 = screenPoint(p+QPoint(1,1));
213     QColor c = img.color(img.scanLine(p.y())[p.x()]);
214     painter.fillRect(QRect(p1,p2-QPoint(1,1)), c);
215 }
216
217 void TrivialTileEditor::mousePressEvent(QMouseEvent* e)
218 {
219     QPoint p = imagePoint(e->pos());
220     if ( !img.rect().contains(p) )
221         return;
222     uchar& pixel = img.scanLine(p.y())[p.x()];
223     if ( e->button() == LeftButton ) {
224         pixel = penpixel;
225         QPainter painter(this);
226         paintPoint(painter,p);
227     } else if ( e->button() == RightButton ) {
228         emit pick( img.color(pixel) );
229     } else if ( e->button() == MidButton ) {
230         QPainter painter(this);
231         if ( pixel != penpixel )
232             fill(painter,p,pixel);
233     }
234 }
235
236 void TrivialTileEditor::fill(QPainter& painter, QPoint p, uchar from)
237 {
238     if ( img.rect().contains(p) ) {
239         uchar& pixel = img.scanLine(p.y())[p.x()];
240         if ( pixel == from ) {
241             pixel = penpixel;
242             paintPoint(painter,p);
243             fill(painter, p+QPoint(-1,0), from);
244             fill(painter, p+QPoint(+1,0), from);
245             fill(painter, p+QPoint(0,-1), from);
246             fill(painter, p+QPoint(0,+1), from);
247         }
248     }
249 }
250
251 void TrivialTileEditor::mouseReleaseEvent(QMouseEvent* e)
252 {
253     emit edited(image());
254 }
255
256 void TrivialTileEditor::mouseMoveEvent(QMouseEvent* e)
257 {
258     QPoint p = imagePoint(e->pos());
259     if ( !img.rect().contains(p) )
260         return;
261     uchar& pixel = img.scanLine(p.y())[p.x()];
262     pixel = penpixel;
263     QPainter painter(this);
264     paintPoint(painter,p);
265 }
266
267 QPoint TrivialTileEditor::imagePoint(QPoint p) const
268 {
269     return QPoint(p.x()*TILE_X/width(), p.y()*TILE_Y/height());
270 }
271
272 QPoint TrivialTileEditor::screenPoint(QPoint p) const
273 {
274     return QPoint(p.x()*width()/TILE_X, p.y()*height()/TILE_Y);
275 }
276
277 QSizePolicy TrivialTileEditor::sizePolicy() const
278 {
279     return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding, TRUE );
280 }
281
282 QSize TrivialTileEditor::sizeHint() const
283 {
284     return sizeForWidth(-1);
285 }
286
287 QSize TrivialTileEditor::sizeForWidth(int w) const
288 {
289     if ( w < 0 )
290         return QSize(TILE_X*32,TILE_Y*32);
291     else
292         return QSize(w,w*TILE_Y/TILE_X);
293 }
294
295
296 TilePalette::TilePalette( QWidget* parent ) :
297     QWidget(parent)
298 {
299     num = 0;
300     rgb = 0;
301 }
302
303 TilePalette::~TilePalette()
304 {
305     delete rgb;
306 }
307
308 void TilePalette::setFromImage( const QImage& i )
309 {
310     num = i.numColors();
311     rgb = new QRgb[num];
312     memcpy(rgb, i.colorTable(), num*sizeof(QRgb));
313     repaint(FALSE);
314 }
315
316 void TilePalette::setColor(QRgb c)
317 {
318     for (int i=0; i<num; i++)
319         if ( c == rgb[i] ) {
320             emit pick(c);
321             return;
322         }
323 }
324
325 QSizePolicy TilePalette::sizePolicy() const
326 {
327     return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum, FALSE );
328 }
329
330 QSize TilePalette::sizeHint() const
331 {
332     return QSize(num*16,16);
333 }
334
335 void TilePalette::paintEvent( QPaintEvent* )
336 {
337     QPainter p(this);
338     for (int i=0; i<num; i++) {
339         int x1 = width()*i/num;
340         int x2 = width()*(i+1)/num;
341         p.fillRect(x1,0,x2-x1,height(),QColor(rgb[i]));
342     }
343 }
344
345 void TilePalette::mousePressEvent(QMouseEvent* e)
346 {
347     int c = e->x()*num/width();
348     emit pick(rgb[c]);
349 }
350
351 TileEditor::TileEditor(QWidget* parent) :
352     QVBox(parent),
353     editor(this),
354     palette(this)
355 {
356     connect( &palette, SIGNAL(pick(QRgb)),
357              &editor, SLOT(setColor(QRgb)) );
358     connect( &editor, SIGNAL(pick(QRgb)),
359              &palette, SLOT(setColor(QRgb)) );
360     connect( &editor, SIGNAL(edited(const QImage&)),
361              this, SIGNAL(edited(const QImage&)) );
362 }
363
364 void TileEditor::edit(const QImage& i)
365 {
366     editor.setImage(i);
367     palette.setFromImage(i);
368 }
369
370 const QImage& TileEditor::image() const
371 {
372     return editor.image();
373 }
374
375 class Main : public QMainWindow {
376 public:
377     Main() :
378         central(this),
379         editor(&central),
380         picker(&central)
381     {
382         QPopupMenu* file = new QPopupMenu(menuBar());
383         file->insertItem("&Save", &picker, SLOT(save()), CTRL+Key_S);
384         file->insertSeparator();
385         file->insertItem("&Exit", qApp, SLOT(quit()), CTRL+Key_Q);
386         menuBar()->insertItem("&File", file);
387
388         connect( &picker, SIGNAL(pick(const QImage&)),
389                  &editor, SLOT(edit(const QImage&)) );
390         connect( &picker, SIGNAL(pickName(const QString&)),
391                  statusBar(), SLOT(message(const QString&)) );
392         connect( &editor, SIGNAL(edited(const QImage&)),
393                  &picker, SLOT(setCurrent(const QImage&)) );
394
395         setCentralWidget(&central);
396     }
397
398 private:
399     QHBox central; 
400     TileEditor editor;
401     TilePicker picker;
402 };
403
404 main(int argc, char** argv)
405 {
406     QApplication app(argc,argv);
407     Main m;
408     app.setMainWidget(&m);
409     m.show();
410     return app.exec();
411 }
412
413 #include "tileedit.moc"