summaryrefslogtreecommitdiffstats
path: root/kpat/cardmaps.cpp
blob: 9b50c356d83dfdc3033bf7205bf9419a1149cef5 (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
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
/***********************-*-C++-*-********

  cardmaps.cpp  defines pixmaps for playing cards

     Copyright (C) 1995  Paul Olav Tvete

 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.

****************************************/

#include <stdio.h>
#include <unistd.h>

#include <tqpainter.h>

#include <kconfig.h>

#include <config.h>
#include "cardmaps.h"
#include <stdlib.h>

#include <config.h>
#include <kdebug.h>
#include <kapplication.h>
#include <klocale.h>
#include "version.h"
#include <kstaticdeleter.h>
#include <tqimage.h>
#include <kimageeffect.h>
#include <kcarddialog.h>
#include <kglobalsettings.h>
#include <assert.h>

cardMap *cardMap::_self = 0;
static KStaticDeleter<cardMap> cms;

cardMap::cardMap(const TQColor &dim) : dimcolor(dim)
{
    assert(!_self);

    card_width = 0;
    card_height = 0;

    kdDebug(11111) << "cardMap\n";
    KConfig *config = kapp->config();
    KConfigGroupSaver cs(config, settings_group );

    TQString bg = config->readEntry( "Back", KCardDialog::getDefaultDeck());
    setBackSide( bg, false);

    TQString dir = config->readEntry("Cards",  KCardDialog::getDefaultCardDir());
    setCardDir( dir );

    cms.setObject(_self, this);
//    kdDebug(11111) << "card " << CARDX << " " << CARDY << endl;
}

bool cardMap::setCardDir( const TQString &dir)
{
    KConfig *config = kapp->config();
    KConfigGroupSaver cs(config, settings_group );

    // create an animation window while loading pixmaps (this
    // may take a while (approx. 3 seconds on my AMD K6PR200)
    bool animate = config->readBoolEntry( "Animation", true);

    TQWidget* w = 0;
    TQPainter p;
    TQTime t1, t2;

    TQString imgname = KCardDialog::getCardPath(dir, 11);

    TQImage image;
    image.load(imgname);
    if( image.isNull()) {
        kdDebug(11111) << "cannot load card pixmap \"" << imgname << "\" in " << dir << "\n";
        p.end();
        delete w;
        return false;
    }

    int old_card_width = card_width;
    int old_card_height = card_height;

    card_width = image.width();
    card_height = image.height();

    const int diff_x_between_cards = TQMAX(card_width / 9, 1);
    TQString wait_message = i18n("please wait, loading cards...");
    TQString greeting = i18n("KPatience - a Solitaire game");

    const int greeting_width = 20 + diff_x_between_cards * 52 + card_width;

    if( animate ) {
        t1 = TQTime::currentTime();
        w = new TQWidget( 0, "", TQt::WStyle_Customize | TQt::WStyle_NoBorder | TQt::WStyle_Tool );
        TQRect dg = TDEGlobalSettings::splashScreenDesktopGeometry();
        w->setBackgroundColor( TQt::darkGreen );
        w->setGeometry( dg.left() + ( dg.width() - greeting_width ) / 2, dg.top() + ( dg.height() - 180 ) / 2, greeting_width, 180);
        w->show();
        tqApp->processEvents();

        p.begin( w );
        p.drawText(0, 150, greeting_width, 20, TQt::AlignCenter,
                   wait_message );

        p.setFont(TQFont("Times", 24));
        p.drawText(0, 0, greeting_width, 40, TQt::AlignCenter,
                   greeting);

        p.setPen(TQPen(TQColor(0, 0, 0), 4));
        p.setBrush(TQt::NoBrush);
        p.drawRect(0, 0, greeting_width, 180);
        p.flush();
    }

    setBackSide(back, true);

    for(int idx = 1; idx < 53; idx++)
    {
        // translate index to suit/rank
        // this is necessary since kpoker uses another
        // mapping in the pictures
        int rank = (idx - 1) / 4;
        if(rank != 0)
            rank = 13 - rank;
        int suit = 0;
        switch((idx - 1) % 4) {
            case 0:
                suit = 0;
                break;
            case 1:
                suit = 3;
                break;
            case 2:
                suit = 2;
                break;
            case 3:
                suit = 1;
                break;
        }

        imgname = KCardDialog::getCardPath(dir, idx);
        image.load(imgname);

        if( image.isNull() || image.width() != card_width || image.height() != card_height ) {
            kdDebug(11111) << "cannot load card pixmap \"" << imgname << "\" in (" << idx << ") " << dir << "\n";
            p.end();
            delete w;
            card_width = old_card_width;
            card_height = old_card_height;

            setBackSide(back, true);
            return false;
        }

        img[rank][suit].normal.convertFromImage(image);
        KImageEffect::fade(image, 0.4, dimcolor);
        img[rank][suit].inverted.convertFromImage(image);

        if( animate )
        {
            if( idx > 1 )
                p.drawPixmap( 10 + ( idx - 1 ) * diff_x_between_cards, 45, back );
            p.drawPixmap( 10 + idx * diff_x_between_cards, 45, img[ rank ][ suit ].normal );
            p.flush();
        }
    }

    if( animate )
    {
        const int time_to_see = 900;
        p.end();
        t2 = TQTime::currentTime();
        if(t1.msecsTo(t2) < time_to_see)
          usleep((time_to_see-t1.msecsTo(t2))*1000);
        delete w;
    }

    return true;
}

bool cardMap::setBackSide( const TQPixmap &pm, bool scale )
{
    if (pm.isNull())
        return false;

    back = pm;

    if(scale && (back.width() != card_width ||
                 back.height() != card_height))
    {
        kdDebug(11111) << "scaling back!!\n";
        // scale to fit size
        TQWMatrix wm;
        wm.scale(((float)(card_width))/back.width(),
                 ((float)(card_height))/back.height());
        back = back.xForm(wm);
    }

    return true;
}

int cardMap::CARDX() {
    return self()->card_width; // 72;
}

int cardMap::CARDY() {
    return self()->card_height; // 96;
}

TQPixmap cardMap::backSide() const
{
    return back;
}

TQPixmap cardMap::image( Card::Rank _rank, Card::Suit _suit, bool inverted) const
{
    if( 1 <= _rank && _rank <= 13 
	&& 1 <= _suit && _suit <= 4 )
    {
        if (inverted)
            return img[ _rank - 1 ][ _suit - 1 ].inverted;
        else
            return img[ _rank - 1 ][ _suit - 1 ].normal;
    }
    else
    {
        kdError() << "access to invalid card " << int(_rank) << ", " << int(_suit) << endl;
    }
    return 0;
}

cardMap *cardMap::self() {
    assert(_self);
    return _self;
}