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
|
/*
Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
Libkmahjongg 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.
*/
// own
#include "kmahjonggtileset.h"
// STL
#include <cstdlib>
// Qt
#include <QFile>
#include <QImage>
#include <QMap>
#include <QPainter>
#include <QPixmapCache>
#include <QStandardPaths>
#include <QSvgRenderer>
// KDE
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
// LibKMahjongg
#include "libkmahjongg_debug.h"
class KMahjonggTilesetMetricsData
{
public:
short lvloffx; // used for 3D indentation, x value
short lvloffy; // used for 3D indentation, y value
short w; // tile width ( +border +shadow)
short h; // tile height ( +border +shadow)
short fw; // face width
short fh; // face height
KMahjonggTilesetMetricsData()
: lvloffx(0)
, lvloffy(0)
, w(0)
, h(0)
, fw(0)
, fh(0)
{
}
};
class KMahjonggTilesetPrivate
{
public:
KMahjonggTilesetPrivate()
: isSVG(false)
, graphicsLoaded(false)
{
}
QList<QString> elementIdTable;
QMap<QString, QString> authorproperties;
KMahjonggTilesetMetricsData originaldata;
KMahjonggTilesetMetricsData scaleddata;
QString filename; // cache the last file loaded to save reloading it
QString graphicspath;
QSvgRenderer svg;
bool isSVG;
bool graphicsLoaded;
};
// ---------------------------------------------------------
KMahjonggTileset::KMahjonggTileset()
: d(new KMahjonggTilesetPrivate)
{
buildElementIdTable();
static bool _inited = false;
if (_inited) {
return;
}
_inited = true;
}
// ---------------------------------------------------------
KMahjonggTileset::~KMahjonggTileset()
{
delete d;
}
void KMahjonggTileset::updateScaleInfo(short tilew, short tileh)
{
d->scaleddata.w = tilew;
d->scaleddata.h = tileh;
double ratio = (static_cast<qreal>(d->scaleddata.w)) / (static_cast<qreal>(d->originaldata.w));
d->scaleddata.lvloffx = static_cast<short>(d->originaldata.lvloffx * ratio);
d->scaleddata.lvloffy = static_cast<short>(d->originaldata.lvloffy * ratio);
d->scaleddata.fw = static_cast<short>(d->originaldata.fw * ratio);
d->scaleddata.fh = static_cast<short>(d->originaldata.fh * ratio);
}
QSize KMahjonggTileset::preferredTileSize(const QSize & boardsize, int horizontalCells, int verticalCells)
{
//calculate our best tile size to fit the boardsize passed to us
qreal newtilew, newtileh, aspectratio;
qreal bw = boardsize.width();
qreal bh = boardsize.height();
//use tileface for calculation, with one complete tile in the sum for extra margin
qreal fullh = (d->originaldata.fh * verticalCells) + d->originaldata.h;
qreal fullw = (d->originaldata.fw * horizontalCells) + d->originaldata.w;
qreal floatw = d->originaldata.w;
qreal floath = d->originaldata.h;
if ((fullw / fullh) > (bw / bh)) {
//space will be left on height, use width as limit
aspectratio = bw / fullw;
} else {
aspectratio = bh / fullh;
}
newtilew = aspectratio * floatw;
newtileh = aspectratio * floath;
return QSize(static_cast<short>(newtilew), static_cast<short>(newtileh));
}
bool KMahjonggTileset::loadDefault()
{
QString idx = QLatin1String("default.desktop");
QString tilesetPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kmahjongglib/tilesets/" + idx);
qCDebug(LIBKMAHJONGG_LOG) << "Inside LoadDefault(), located path at" << tilesetPath;
if (tilesetPath.isEmpty()) {
return false;
}
return loadTileset(tilesetPath);
}
QString KMahjonggTileset::authorProperty(const QString & key) const
{
return d->authorproperties[key];
}
short KMahjonggTileset::width() const
{
return d->scaleddata.w;
}
short KMahjonggTileset::height() const
{
return d->scaleddata.h;
}
short KMahjonggTileset::levelOffsetX() const
{
return d->scaleddata.lvloffx;
}
short KMahjonggTileset::levelOffsetY() const
{
return d->scaleddata.lvloffy;
}
short KMahjonggTileset::qWidth() const
{
return static_cast<short>(d->scaleddata.fw / 2.0);
}
short KMahjonggTileset::qHeight() const
{
return static_cast<short>(d->scaleddata.fh / 2.0);
}
QString KMahjonggTileset::path() const
{
return d->filename;
}
#define kTilesetVersionFormat 1
// ---------------------------------------------------------
bool KMahjonggTileset::loadTileset(const QString & tilesetPath)
{
//qCDebug(LIBKMAHJONGG_LOG) << "Attempting to load .desktop at" << tilesetPath;
//clear our properties map
d->authorproperties.clear();
// verify if it is a valid file first and if we can open it
QFile tilesetfile(tilesetPath);
if (!tilesetfile.open(QIODevice::ReadOnly)) {
return false;
}
tilesetfile.close();
KConfig tileconfig(tilesetPath, KConfig::SimpleConfig);
KConfigGroup group = tileconfig.group("KMahjonggTileset");
d->authorproperties.insert(QLatin1String("Name"), group.readEntry("Name")); // Returns translated data
d->authorproperties.insert(QLatin1String("Author"), group.readEntry("Author"));
d->authorproperties.insert(QLatin1String("Description"), group.readEntry("Description"));
d->authorproperties.insert(QLatin1String("AuthorEmail"), group.readEntry("AuthorEmail"));
//Version control
int tileversion = group.readEntry("VersionFormat", 0);
//Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
if (tileversion > kTilesetVersionFormat) {
return false;
}
QString graphName = group.readEntry("FileName");
d->graphicspath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kmahjongglib/tilesets/" + graphName);
//qCDebug(LIBKMAHJONGG_LOG) << "Using tileset at" << d->graphicspath;
//only SVG for now
d->isSVG = true;
if (d->graphicspath.isEmpty()) {
return false;
}
d->originaldata.w = group.readEntry("TileWidth", 30);
d->originaldata.h = group.readEntry("TileHeight", 50);
d->originaldata.fw = group.readEntry("TileFaceWidth", 30);
d->originaldata.fh = group.readEntry("TileFaceHeight", 50);
d->originaldata.lvloffx = group.readEntry("LevelOffsetX", 10);
d->originaldata.lvloffy = group.readEntry("LevelOffsetY", 10);
//client application needs to call loadGraphics()
d->graphicsLoaded = false;
d->filename = tilesetPath;
return true;
}
// ---------------------------------------------------------
bool KMahjonggTileset::loadGraphics()
{
if (d->graphicsLoaded) {
return true;
}
if (d->isSVG) {
//really?
d->svg.load(d->graphicspath);
if (d->svg.isValid()) {
//invalidate our global cache
QPixmapCache::clear();
d->graphicsLoaded = true;
reloadTileset(QSize(d->originaldata.w, d->originaldata.h));
} else {
return false;
}
} else {
//TODO add support for png??
return false;
}
return true;
}
// ---------------------------------------------------------
bool KMahjonggTileset::reloadTileset(const QSize & newTilesize)
{
if (QSize(d->scaleddata.w, d->scaleddata.h) == newTilesize) {
return false;
}
if (d->isSVG) {
if (d->svg.isValid()) {
updateScaleInfo(newTilesize.width(), newTilesize.height());
//rendering will be done when needed, automatically using the global cache
} else {
return false;
}
} else {
//TODO add support for png???
return false;
}
return true;
}
void KMahjonggTileset::buildElementIdTable()
{
//Build a list for faster lookup of element ids, mapped to the enumeration used by GameData and BoardWidget
//Unselected tiles
for (short idx = 1; idx <= 4; idx++) {
d->elementIdTable.append(QString::fromLatin1("TILE_%1").arg(idx));
}
//Selected tiles
for (short idx = 1; idx <= 4; idx++) {
d->elementIdTable.append(QString::fromLatin1("TILE_%1_SEL").arg(idx));
}
//now faces
for (short idx = 1; idx <= 9; idx++) {
d->elementIdTable.append(QString::fromLatin1("CHARACTER_%1").arg(idx));
}
for (short idx = 1; idx <= 9; idx++) {
d->elementIdTable.append(QString::fromLatin1("BAMBOO_%1").arg(idx));
}
for (short idx = 1; idx <= 9; idx++) {
d->elementIdTable.append(QString::fromLatin1("ROD_%1").arg(idx));
}
for (short idx = 1; idx <= 4; idx++) {
d->elementIdTable.append(QString::fromLatin1("SEASON_%1").arg(idx));
}
for (short idx = 1; idx <= 4; idx++) {
d->elementIdTable.append(QString::fromLatin1("WIND_%1").arg(idx));
}
for (short idx = 1; idx <= 3; idx++) {
d->elementIdTable.append(QString::fromLatin1("DRAGON_%1").arg(idx));
}
for (short idx = 1; idx <= 4; idx++) {
d->elementIdTable.append(QString::fromLatin1("FLOWER_%1").arg(idx));
}
}
QString KMahjonggTileset::pixmapCacheNameFromElementId(const QString & elementid)
{
return authorProperty(QLatin1String("Name")) + elementid + QString::fromLatin1("W%1H%2").arg(d->scaleddata.w).arg(d->scaleddata.h);
}
QPixmap KMahjonggTileset::renderElement(short width, short height, const QString & elementid)
{
//qCDebug(LIBKMAHJONGG_LOG) << "render element" << elementid << width << height;
QImage qiRend(QSize(width, height), QImage::Format_ARGB32_Premultiplied);
qiRend.fill(0);
if (d->svg.isValid()) {
QPainter p(&qiRend);
d->svg.render(&p, elementid);
}
return QPixmap::fromImage(qiRend);
}
QPixmap KMahjonggTileset::selectedTile(int num)
{
QPixmap pm;
QString elemId = d->elementIdTable.at(num + 4); //selected offset in our idtable;
if (!QPixmapCache::find(pixmapCacheNameFromElementId(elemId), &pm)) {
//use tile size
pm = renderElement(d->scaleddata.w, d->scaleddata.h, elemId);
QPixmapCache::insert(pixmapCacheNameFromElementId(elemId), pm);
}
return pm;
}
QPixmap KMahjonggTileset::unselectedTile(int num)
{
QPixmap pm;
QString elemId = d->elementIdTable.at(num);
if (!QPixmapCache::find(pixmapCacheNameFromElementId(elemId), &pm)) {
//use tile size
pm = renderElement(d->scaleddata.w, d->scaleddata.h, elemId);
QPixmapCache::insert(pixmapCacheNameFromElementId(elemId), pm);
}
return pm;
}
QPixmap KMahjonggTileset::tileface(int num)
{
QPixmap pm;
if ((num + 8) >= d->elementIdTable.count()) {
//qCDebug(LIBKMAHJONGG_LOG) << "Client asked for invalid tileface id";
return pm;
}
QString elemId = d->elementIdTable.at(num + 8); //tileface offset in our idtable;
if (!QPixmapCache::find(pixmapCacheNameFromElementId(elemId), &pm)) {
//use face size
pm = renderElement(d->scaleddata.fw, d->scaleddata.fh, elemId);
QPixmapCache::insert(pixmapCacheNameFromElementId(elemId), pm);
}
return pm;
}
|