summaryrefslogtreecommitdiffstats
path: root/ksame/StoneWidget.cpp
blob: dc02a1bcf0086b1f71320574abd52e726b49614b (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
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
/*
 *   ksame 0.4 - simple Game
 *   Copyright (C) 1997,1998  Marcus Kreutzberger
 *
 *   This program 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 <config.h>
#include <stdio.h>
#include <stdlib.h>

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqbitmap.h>
#include <tqimage.h>
#include <tqcursor.h>

#include <kapplication.h>
#include <kconfig.h>
#include <kiconloader.h>

#include <time.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include <assert.h>

#include "StoneWidget.moc"


struct StoneSlice {
    TQPixmap stone;
};


StoneWidget::StoneWidget( TQWidget *parent, int x, int y )
    : TQWidget(parent,"StoneWidget"), stonefield(x,y)
{
    setBackgroundPixmap(TQPixmap(locate("wallpaper", "Time-For-Lunch-2.jpg")));
    TQPixmap stonemap(locate("appdata", "stones.png"));

    assert(!stonemap.isNull());

    slice=0;
    maxslices=30;
    maxcolors=4;

    sizex=x;
    sizey=y;

    stone_width=stonemap.width()/(maxslices+1);
    stone_height=stonemap.height()/maxcolors;

    map = new StoneSlice*[maxcolors];
    TQBitmap mask;
    for (int c = 0; c < maxcolors; c++) {
        map[c] = new StoneSlice[maxslices];

        for (int s = 0; s < maxslices; s++) {
            map[c][s].stone.resize(stone_width, stone_height);
            assert(!map[c][s].stone.isNull());
            bitBlt(TQT_TQPAINTDEVICE(&map[c][s].stone), 0, 0,
                   TQT_TQPAINTDEVICE(&stonemap), stone_width * s,
                   c*stone_height,
                   stone_width,stone_height,CopyROP,false);
            TQImage im = map[c][s].stone.convertToImage();
            mask.convertFromImage(im.createHeuristicMask());
            map[c][s].stone.setMask(mask);
        }
    }

    field_height=stone_height*sizey;
    field_width=stone_width*sizex;

    setMouseTracking(true);

    // TQColor c(115,115,115);
    // setBackgroundColor(c);

    // emit s_sizechanged();
    startTimer( 100 );
    history.setAutoDelete(true);
}

StoneWidget::~StoneWidget() {
    for (int c = 0; c < maxcolors; c++) {
        delete [] map[c];
    }
    delete [] map;

    setMouseTracking(false);
    TQT_TQOBJECT(this)->killTimers();
}

unsigned int
StoneWidget::board() {
    return stonefield.getBoard();
}

int
StoneWidget::score() {
    return stonefield.getScore();
}

int
StoneWidget::count(int color) {
  return stonefield.count(color);
}

int
StoneWidget::marked() {
	return stonefield.getMarked();
}

TQSize
StoneWidget::size() {
    return TQSize(sizex,sizey);
}

int
StoneWidget::colors() {
    return stonefield.getColors();
}

TQSize
StoneWidget::sizeHint () const {
    return TQSize(field_width,field_height);
}

void
StoneWidget::newGame(unsigned int board,int colors) {
    stonefield.newGame(board,colors);
    history.clear();
    modified= false;
    emit s_newgame();
    emit s_colors(stonefield.getColors());
    emit s_board(stonefield.getBoard());
}

void
StoneWidget::reset() {
    stonefield.reset();
    history.clear();
    emit s_newgame();
}

void
StoneWidget::unmark() {
    stonefield.unmark();
    emit s_marked(0);
}

bool StoneWidget::undoPossible() const {
    if (stonefield.isGameover()) return false;
    return stonefield.undoPossible();
}

int
StoneWidget::undo(int count) {
    if (stonefield.isGameover()) return 0;

    int ret_val=stonefield.undo(count);

    TQPoint p=mapFromGlobal(cursor().pos());
    int x=p.x();
    int y=p.y();
    if (x<0||y<0||x>=field_width||y>=field_height) {
        emit s_score(stonefield.getMarked());
        return ret_val;
    }

    int marked=stonefield.mark(x/stone_width,y/stone_height);
    emit s_marked(marked);
    slice=0;
    emit s_score(stonefield.getScore());
    modified= (stonefield.getScore()>0);
    return ret_val;
}

bool StoneWidget::isGameover() {
    return stonefield.isGameover();
}

bool StoneWidget::hasBonus() {
	return stonefield.gotBonus(); // don't ask me why the names differ... ;-| [hlm]
}

bool StoneWidget::isOriginalBoard() {
	return !modified;
}

void StoneWidget::readProperties(KConfig *conf) {
    Q_ASSERT(conf);

    history.clear();

    if (!conf->hasKey("Board")||
        !conf->hasKey("Colors")||
        !conf->hasKey("Stones")) {
        return;
    }
    newGame(conf->readNumEntry("Board"),conf->readNumEntry("Colors"));

    TQStrList list;
    conf->readListEntry("Stones",list);

    for (const char *item=list.first();item;item=list.next()) {
        int x=-1,y=-1;
        if (sscanf(item,"%02X%02X",&x,&y)!=2) break;
        history.append(new TQPoint(x,y));
        stonefield.remove(x,y);
    }
}


void
StoneWidget::saveProperties(KConfig *conf) {
    Q_ASSERT(conf);

    TQStrList list(true);
    TQString tmp;

    for (TQPoint *item=history.first();item;item=history.next()) {
        tmp.sprintf("%02X%02X",item->x(),item->y());
        list.append(tmp.ascii());
    }

    conf->writeEntry("Stones",list);
    conf->writeEntry("Board",stonefield.getBoard());
    conf->writeEntry("Colors",stonefield.getColors());
}

void
StoneWidget::timerEvent( TQTimerEvent * ) {
    TQPoint p=mapFromGlobal(cursor().pos());
    int x=p.x();
    int y=p.y();
    if (x<0||y<0||x>=field_width||y>=field_height)
        stonefield.unmark();
    slice=(slice+1)%maxslices;
    paintEvent(0);
}

void
StoneWidget::paintEvent( TQPaintEvent *e ) {

    Stone *stone=stonefield.getField();

    for (int y=0;y<sizey;y++) {
        int cy = y * stone_height;

        for (int x=0;x<sizex;x++) {
            int cx = stone_width * x;

            bool redraw=stone->marked||stone->changed;

            if (!redraw&&e) {
                TQRect r(cx,cy,stone_width,stone_height);
                redraw=r.intersects(e->rect());
            }
            if (redraw) {
                stone->changed=false;
                if (stone->color) {

                    int tslice = stone->marked?slice:0;
                    bitBlt(TQT_TQPAINTDEVICE(this),cx,cy,
                           TQT_TQPAINTDEVICE(&map[stone->color-1][tslice].stone),
                           0, 0,
                           stone_width,stone_height,CopyROP,FALSE);

                } else {
                    erase(cx, cy, stone_width, stone_height);
                }
            }
            stone++;  // naechster Stein.
        }
    }
}

void
StoneWidget::mousePressEvent ( TQMouseEvent *e) {

    if (stonefield.isGameover()) return;

    int x=e->pos().x();
    int y=e->pos().y();
    if (x<0||y<0||x>=field_width||y>=field_height) return;

    int sx=x/stone_width;
    int sy=y/stone_height;

    if (stonefield.remove(sx, sy)) {
        history.append(new TQPoint(sx, sy));

        emit s_remove(sx, sy);

        stonefield.mark(sx,sy);
        emit s_marked(stonefield.getMarked());
		modified= true;

        emit s_score(stonefield.getScore());
        if (stonefield.isGameover()) emit s_gameover();
    }
}

void
StoneWidget::mouseMoveEvent ( TQMouseEvent *e)
{
    if (stonefield.isGameover()) {
        stonefield.unmark();
        emit s_marked(0);
        return;
    }

    int x=e->pos().x();
    int y=e->pos().y();
    if (x<0||y<0||x>=field_width||y>=field_height) return;

    int marked=stonefield.mark(x/stone_width,y/stone_height);
    if (marked>=0) {
        emit s_marked(marked);
        slice=0;
    }
}