summaryrefslogtreecommitdiffstats
path: root/kshisen/tileset.cpp
blob: 492cde79789dc7230ad326f29a912863b168e47e (plain)
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
/**
 * tileset.cpp
 *
 * Copyright (c) 2002 Jason Lane <jglane@btopenworld.com>
 *           (c) 2002 Dave Corrie <kde@davecorrie.com>
 *
 *  This file is part of KShisen.
 *
 *  KMail is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <kapplication.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>

#include <tqimage.h>

#include <algorithm>

#include "tileset.h"

TileSet::TileSet() : scaledTiles(nTiles)
{
	//loadTiles
	TQImage tileset(KGlobal::dirs()->findResource("appdata", "tileset.png"));
	if(tileset.isNull())
	{
		KMessageBox::sorry(0, i18n("Cannot load tiles pixmap!"));
		KApplication::exit(1);
	}

	// split into individual tiles
	const int TILES_X = 9;
	const int TILES_Y = 4;
	unscaledTiles.reserve(nTiles);

	int w = tileset.width() / TILES_X;
	int h = tileset.height() / TILES_Y;
	for(int row = 0; row < TILES_Y; row++)
	{
		for(int col = 0; col < TILES_X; col++)
			unscaledTiles.push_back(tileset.copy(col * w, row * h, w, h));
	}
}

TileSet::~TileSet()
{
}

void TileSet::resizeTiles(int maxWidth, int maxHeight)
{
	// calculate largest tile size that will fit in maxWidth/maxHeight
	// and maintain the tile's height-to-width ratio
	double ratio = static_cast<double>(unscaledTileHeight()) / unscaledTileWidth();
	if(maxWidth * ratio < maxHeight)
		maxHeight = qRound(maxWidth * ratio);
	else
		maxWidth = qRound(maxHeight / ratio);

	if(maxHeight == tileHeight() && maxWidth == tileWidth())
		return;

	//kdDebug() << "tile size: " << maxWidth << "x" << maxHeight << endl;

	TQImage img;
	for(int i = 0; i < nTiles; i++)
	{
		if(maxHeight == unscaledTileHeight())
			img = unscaledTiles[i].copy();//.convertDepth(32);
		else
			img = unscaledTiles[i].smoothScale(maxWidth, maxHeight);

		scaledTiles[i].convertFromImage(img);
	}
}

const TQPixmap &TileSet::tile(int n) const
{
	return scaledTiles[n];
}

TQPixmap TileSet::highlightedTile(int n) const
{
	const double LIGHTEN_FACTOR = 1.3;

	// lighten the image
	TQImage img = scaledTiles[n].convertToImage().convertDepth(32);

	for(int y = 0; y < img.height(); y++)
	{
		uchar* p = img.scanLine(y);
		for(int x = 0; x < img.width() * 4; x++)
		{
			*p = static_cast<uchar>(std::min(255, static_cast<int>(*p * LIGHTEN_FACTOR)));
			p++;
		}
	}

	TQPixmap highlightedTile;
	highlightedTile.convertFromImage(img);

	return highlightedTile;
}

int TileSet::lineWidth() const
{
	int width = qRound(tileHeight() / 10.0);
	if(width < 3)
		width = 3;

	return width;
}

int TileSet:: tileWidth() const
{
	return scaledTiles[0].width();
}

int TileSet:: tileHeight() const
{
	return scaledTiles[0].height();
}

int TileSet:: unscaledTileHeight() const
{
	return unscaledTiles[0].height();
}

int TileSet:: unscaledTileWidth() const
{
	return unscaledTiles[0].width();
}