File: tileedit.cpp

package info (click to toggle)
slashem 0.0.7E7F3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 24,312 kB
  • ctags: 23,514
  • sloc: ansic: 263,201; cpp: 7,180; yacc: 2,154; sh: 739; lex: 440; awk: 97; makefile: 61; sed: 11
file content (413 lines) | stat: -rw-r--r-- 9,488 bytes parent folder | download | duplicates (18)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*	SCCS Id: @(#)tileedit.cpp	3.4	1999/11/19	*/
/* Copyright (c) Warwick Allison, 1999. */
/* NetHack may be freely redistributed.  See license for details. */
/*
Build this little utility program if you want to use it to edit the tile
files.  Move tileedit.cpp and tileedit.h to ../../util, add the
3 lines below to the Makefile there and "make tileedit".

tileedit: tileedit.cpp $(TEXT_IO)
	moc -o tileedit.moc tileedit.h
	$(CC) -o tileedit -I../include -I$(QTDIR)/include -L$(QTDIR)/lib tileedit.cpp $(TEXT_IO) -lqt
*/


#include "tileedit.h"
#include <qapplication.h>
#include <qmainwindow.h>
#include <qkeycode.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qpainter.h>
#include <qstatusbar.h>
#include <qhbox.h>
#include <qlabel.h>

extern "C" {
#include "config.h"
#include "tile.h"
extern const char *FDECL(tilename, (int, int));
}

#define TILES_ACROSS 20

TilePickerTab::TilePickerTab(const char* basename, int i, QWidget* parent) :
    QWidget(parent)
{
    id = i;
    filename = basename;
    filename += ".txt";
    num = 0;
    int index = 0;
    for (int real=0; real<2; real++) {
	if ( real ) {
	    image.create( TILES_ACROSS*TILE_X,
		((num+TILES_ACROSS-1)/TILES_ACROSS)*TILE_Y, 32 );
	}
	if ( !fopen_text_file(filename.latin1(), RDTMODE) ) {
	    // XXX handle better
	    exit(1);
	}
	pixel p[TILE_Y][TILE_X];
	while ( read_text_tile(p) ) {
	    if ( real ) {
		int ox = (index%TILES_ACROSS)*TILE_X;
		int oy = (index/TILES_ACROSS)*TILE_Y;
		for ( int y=0; y<TILE_Y; y++ ) {
		    QRgb* rgb = ((QRgb*)image.scanLine(oy+y)) + ox;
		    for ( int x=0; x<TILE_X; x++ ) {
			*rgb++ = qRgb(p[y][x].r, p[y][x].g, p[y][x].b);
		    }
		}
		index++;
	    } else {
		// Just count...
		num++;
	    }
	}
	fclose_text_file();
    }
    image = image.convertDepth( 8, AvoidDither );
    pixmap.convertFromImage( image );
}

bool TilePickerTab::save()
{
    if ( !fopen_text_file(filename.latin1(), WRTMODE) ) {
	// XXX handle better
	exit(1);
    }
    pixel p[TILE_Y][TILE_X];
    for ( int index=0; index < num; index++ ) {
	int ox = (index%TILES_ACROSS)*TILE_X;
	int oy = (index/TILES_ACROSS)*TILE_Y;
	for ( int y=0; y<TILE_Y; y++ ) {
	    uchar* c = image.scanLine(oy+y) + ox;
	    for ( int x=0; x<TILE_X; x++ ) {
		QRgb rgb = image.color(*c++);
		p[y][x].r = qRed(rgb);
		p[y][x].g = qGreen(rgb);
		p[y][x].b = qBlue(rgb);
	    }
	}
	write_text_tile(p);
    }
    fclose_text_file();
}

void TilePickerTab::mousePressEvent(QMouseEvent* e)
{
    int ox = e->x()-e->x()%TILE_X;
    int oy = e->y()-e->y()%TILE_Y;
    QImage subimage = image.copy(ox,oy,TILE_X,TILE_Y);
    if ( e->button() == RightButton ) {
	setCurrent(subimage);
    } else {
	last_pick = ox/TILE_X + oy/TILE_Y*TILES_ACROSS;
    }
    emit pick(subimage);
    emit pickName(tilename(id, last_pick));
}

void TilePickerTab::setCurrent(const QImage& i)
{
    int ox = last_pick%TILES_ACROSS * TILE_X;
    int oy = last_pick/TILES_ACROSS * TILE_Y;
    bitBlt( &image, ox, oy, &i );
    bitBlt( &pixmap, ox, oy, &i );
    repaint( ox, oy, TILE_X, TILE_Y, FALSE );
}

QSize TilePickerTab::sizeHint() const
{
    return pixmap.size();
}

void TilePickerTab::paintEvent( QPaintEvent* )
{
    QPainter p(this);
    p.drawPixmap(0,0,pixmap);
}

static struct {
    const char* name;
    TilePickerTab* tab;
} tileset[] = {
    { "monsters", 0 },
    { "objects", 0 },
    { "other", 0 },
    { 0 }
};

TilePicker::TilePicker(QWidget* parent) :
    QTabWidget(parent)
{
    for (int i=0; tileset[i].name; i++) {
	QString tabname = tileset[i].name;
	tabname[0] = tabname[0].upper();
	tileset[i].tab = new TilePickerTab(tileset[i].name,i+1,this);
	addTab( tileset[i].tab, tabname );
	connect( tileset[i].tab, SIGNAL(pick(const QImage&)),
		 this, SIGNAL(pick(const QImage&)) );
	connect( tileset[i].tab, SIGNAL(pickName(const QString&)),
		 this, SIGNAL(pickName(const QString&)) );
    }
}

void TilePicker::setCurrent(const QImage& i)
{
    ((TilePickerTab*)currentPage())->setCurrent(i);
}

void TilePicker::save()
{
    for (int i=0; tileset[i].tab; i++) {
	tileset[i].tab->save();
    }
}

TrivialTileEditor::TrivialTileEditor( QWidget* parent ) :
    QWidget(parent)
{
}

const QImage& TrivialTileEditor::image() const
{
    return img;
}

void TrivialTileEditor::setColor( QRgb rgb )
{
    pen = rgb;
    for (penpixel = 0;
	    penpixel<img.numColors()-1 && (img.color(penpixel)&0xffffff)!=(pen.rgb()&0xffffff);
	    penpixel++)
	continue;
}

void TrivialTileEditor::setImage( const QImage& i )
{
    img = i;
    setColor(pen.rgb()); // update penpixel
    repaint(FALSE);
}

void TrivialTileEditor::paintEvent( QPaintEvent* e )
{
    QRect r = e->rect();
    QPoint tl = imagePoint(r.topLeft());
    QPoint br = imagePoint(r.bottomRight());
    r = QRect(tl,br).intersect(img.rect());
    QPainter painter(this);
    for (int y=r.top(); y<=r.bottom(); y++) {
	for (int x=r.left(); x<=r.right(); x++) {
	    paintPoint(painter,QPoint(x,y));
	}
    }
}

void TrivialTileEditor::paintPoint(QPainter& painter, QPoint p)
{
    QPoint p1 = screenPoint(p);
    QPoint p2 = screenPoint(p+QPoint(1,1));
    QColor c = img.color(img.scanLine(p.y())[p.x()]);
    painter.fillRect(QRect(p1,p2-QPoint(1,1)), c);
}

void TrivialTileEditor::mousePressEvent(QMouseEvent* e)
{
    QPoint p = imagePoint(e->pos());
    if ( !img.rect().contains(p) )
	return;
    uchar& pixel = img.scanLine(p.y())[p.x()];
    if ( e->button() == LeftButton ) {
	pixel = penpixel;
	QPainter painter(this);
	paintPoint(painter,p);
    } else if ( e->button() == RightButton ) {
	emit pick( img.color(pixel) );
    } else if ( e->button() == MidButton ) {
	QPainter painter(this);
	if ( pixel != penpixel )
	    fill(painter,p,pixel);
    }
}

void TrivialTileEditor::fill(QPainter& painter, QPoint p, uchar from)
{
    if ( img.rect().contains(p) ) {
	uchar& pixel = img.scanLine(p.y())[p.x()];
	if ( pixel == from ) {
	    pixel = penpixel;
	    paintPoint(painter,p);
	    fill(painter, p+QPoint(-1,0), from);
	    fill(painter, p+QPoint(+1,0), from);
	    fill(painter, p+QPoint(0,-1), from);
	    fill(painter, p+QPoint(0,+1), from);
	}
    }
}

void TrivialTileEditor::mouseReleaseEvent(QMouseEvent* e)
{
    emit edited(image());
}

void TrivialTileEditor::mouseMoveEvent(QMouseEvent* e)
{
    QPoint p = imagePoint(e->pos());
    if ( !img.rect().contains(p) )
	return;
    uchar& pixel = img.scanLine(p.y())[p.x()];
    pixel = penpixel;
    QPainter painter(this);
    paintPoint(painter,p);
}

QPoint TrivialTileEditor::imagePoint(QPoint p) const
{
    return QPoint(p.x()*TILE_X/width(), p.y()*TILE_Y/height());
}

QPoint TrivialTileEditor::screenPoint(QPoint p) const
{
    return QPoint(p.x()*width()/TILE_X, p.y()*height()/TILE_Y);
}

QSizePolicy TrivialTileEditor::sizePolicy() const
{
    return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding, TRUE );
}

QSize TrivialTileEditor::sizeHint() const
{
    return sizeForWidth(-1);
}

QSize TrivialTileEditor::sizeForWidth(int w) const
{
    if ( w < 0 )
	return QSize(TILE_X*32,TILE_Y*32);
    else
	return QSize(w,w*TILE_Y/TILE_X);
}


TilePalette::TilePalette( QWidget* parent ) :
    QWidget(parent)
{
    num = 0;
    rgb = 0;
}

TilePalette::~TilePalette()
{
    delete rgb;
}

void TilePalette::setFromImage( const QImage& i )
{
    num = i.numColors();
    rgb = new QRgb[num];
    memcpy(rgb, i.colorTable(), num*sizeof(QRgb));
    repaint(FALSE);
}

void TilePalette::setColor(QRgb c)
{
    for (int i=0; i<num; i++)
	if ( c == rgb[i] ) {
	    emit pick(c);
	    return;
	}
}

QSizePolicy TilePalette::sizePolicy() const
{
    return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum, FALSE );
}

QSize TilePalette::sizeHint() const
{
    return QSize(num*16,16);
}

void TilePalette::paintEvent( QPaintEvent* )
{
    QPainter p(this);
    for (int i=0; i<num; i++) {
	int x1 = width()*i/num;
	int x2 = width()*(i+1)/num;
	p.fillRect(x1,0,x2-x1,height(),QColor(rgb[i]));
    }
}

void TilePalette::mousePressEvent(QMouseEvent* e)
{
    int c = e->x()*num/width();
    emit pick(rgb[c]);
}

TileEditor::TileEditor(QWidget* parent) :
    QVBox(parent),
    editor(this),
    palette(this)
{
    connect( &palette, SIGNAL(pick(QRgb)),
	     &editor, SLOT(setColor(QRgb)) );
    connect( &editor, SIGNAL(pick(QRgb)),
	     &palette, SLOT(setColor(QRgb)) );
    connect( &editor, SIGNAL(edited(const QImage&)),
	     this, SIGNAL(edited(const QImage&)) );
}

void TileEditor::edit(const QImage& i)
{
    editor.setImage(i);
    palette.setFromImage(i);
}

const QImage& TileEditor::image() const
{
    return editor.image();
}

class Main : public QMainWindow {
public:
    Main() :
	central(this),
	editor(&central),
	picker(&central)
    {
	QPopupMenu* file = new QPopupMenu(menuBar());
	file->insertItem("&Save", &picker, SLOT(save()), CTRL+Key_S);
	file->insertSeparator();
	file->insertItem("&Exit", qApp, SLOT(quit()), CTRL+Key_Q);
	menuBar()->insertItem("&File", file);

	connect( &picker, SIGNAL(pick(const QImage&)),
		 &editor, SLOT(edit(const QImage&)) );
	connect( &picker, SIGNAL(pickName(const QString&)),
		 statusBar(), SLOT(message(const QString&)) );
	connect( &editor, SIGNAL(edited(const QImage&)),
		 &picker, SLOT(setCurrent(const QImage&)) );

	setCentralWidget(&central);
    }

private:
    QHBox central; 
    TileEditor editor;
    TilePicker picker;
};

main(int argc, char** argv)
{
    QApplication app(argc,argv);
    Main m;
    app.setMainWidget(&m);
    m.show();
    return app.exec();
}

#include "tileedit.moc"