summaryrefslogtreecommitdiffstats
path: root/kjumpingcube/kcubewidget.cpp
blob: f3db282823ce2920fb4fa36011325de9cdcf1559 (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
346
347
348
/* ****************************************************************************
  This file is part of the game 'KJumpingCube'

  Copyright (C) 1998-2000 by Matthias Kiefer
                            <[email protected]>

  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 "kcubewidget.h"

#include <tqpainter.h>
#include <tqtimer.h>

#include <kapplication.h>
#include <kdebug.h>

/* ****************************************************** **
**                 static elements                        **
** ****************************************************** */
bool KCubeWidget::_clicksAllowed=true;
TQPalette KCubeWidget::color1;
TQPalette KCubeWidget::color2;

 
void KCubeWidget::enableClicks(bool flag)
{
   _clicksAllowed=flag;
}


void KCubeWidget::setColor(Owner forWhom, TQPalette newPalette)
{
   if(forWhom==One)
   {   
      color1=newPalette;
   }
   else if(forWhom==Two)
   {
      color2=newPalette;
   }
} 

TQPalette KCubeWidget::color(Owner forWhom)
{
   TQPalette color;
   if(forWhom==One)
   {
      color=color1;
   }
   else if(forWhom==Two)
   {
      color=color2;
   }
   
   return color;
}


/* ****************************************************** **
**                 public functions                       **
** ****************************************************** */

KCubeWidget::KCubeWidget(TQWidget* parent,const char* name
                  ,Owner owner,int value,int max)
              : TQFrame(parent,name),
                Cube(owner,value,max)
{
  setFrameStyle(Panel|Raised);
  //setLineWidth(2);
  setMinimumSize(20,20); 

  setCoordinates(0,0);
  
  //initialize hintTimer
  // will be automatically destroyed by the parent
  hintTimer = new TQTimer(this);
  hintCounter=0;
  connect(hintTimer,TQT_SIGNAL(timeout()),TQT_SLOT(hint()));
  
  setPalette(kapp->palette());

  // show values
  repaint(false);
}

KCubeWidget::~KCubeWidget()
{
}


KCubeWidget& KCubeWidget::operator=(const Cube& cube)
{
   if(this!=&cube)
   {
      setOwner(cube.owner());
      setValue(cube.value());
      setMax(cube.max());
   }
   
   return *this;
}

KCubeWidget& KCubeWidget::operator=(const KCubeWidget& cube)
{
   if(this!=&cube)
   {
      setOwner(cube.owner());
      setValue(cube.value());
      setMax(cube.max());
   }

   return *this;
}
KCubeWidget::Owner KCubeWidget::setOwner(Owner newOwner)
{
   Owner old=Cube::setOwner(newOwner);
   
   updateColors();

   return old;
}

void KCubeWidget::setValue(int newValue)
{
   Cube::setValue(newValue);
   update();
}


void KCubeWidget::showHint(int interval,int number)
{
   if(hintTimer->isActive())
      return;
   
   hintCounter=2*number;
   hintTimer->start(interval);
}


void KCubeWidget::animate(bool )
{
}


void KCubeWidget::setCoordinates(int row,int column)
{
   _row=row;
   _column=column;
}

int KCubeWidget::row() const
{
   return _row;
}

int KCubeWidget::column() const
{
   return _column;
}




/* ****************************************************** **
**                   public slots                         **
** ****************************************************** */

void KCubeWidget::reset()
{
  setValue(1);
  setOwner(Nobody);
}


void KCubeWidget::updateColors()
{
  if(owner()==One)
    setPalette(color1);
  else if(owner()==Two)
    setPalette(color2);
  else if(owner()==Nobody)
     setPalette(kapp->palette());
}

void KCubeWidget::stopHint()
{
   if(hintTimer->isActive())
   {
      hintTimer->stop();
      setBackgroundMode(PaletteBackground);
   }

}



/* ****************************************************** **
**                   protected slots                      **
** ****************************************************** */

void KCubeWidget::hint()
{   
   hintCounter--;
   if(hintCounter%2==1)
   {
      setBackgroundMode(PaletteLight);
   }
   else
   {
      setBackgroundMode(PaletteBackground);
   }
   if(hintCounter==0)
   {
      stopHint();
   }
}



/* ****************************************************** **
**                   Event handler                        **
** ****************************************************** */

void KCubeWidget::mouseReleaseEvent(TQMouseEvent *e)
{
  // only accept click if it was inside this cube
  if(e->x()< 0 || e->x() > width() || e->y() < 0 || e->y() > height())
    return;

  if(e->button() == Qt::LeftButton && _clicksAllowed)
  {
    stopHint();
    emit clicked(row(),column(),true);
  }
}



void KCubeWidget::drawContents(TQPainter *painter)
{
  TQRect contents=contentsRect();
  TQPixmap buffer(contents.size());
  buffer.fill(this,contents.topLeft());
  TQPainter *p=new TQPainter;
  p->begin(&buffer);
  int  h=contents.height();
  int  w=contents.width();
  int circleSize=(h<w?h:w)/7;
  int points=value();
  TQBrush brush("black");
  TQPen pen("black");
  p->setBrush(brush);
  p->setPen(pen);
  switch(points)
    {
    case 1:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
      break;

    case 3:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
    case 2:
      p->drawEllipse(w/4-circleSize/2,h/4-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      break;
      
    case 5:
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,circleSize,circleSize);
    case 4:
      p->drawEllipse(w/4-circleSize/2,h/4-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/4-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/4-circleSize/2,
		     circleSize,circleSize);
      break;

    case 8:
      p->drawEllipse(w/2-circleSize/2,2*h/3-circleSize/2,
		     circleSize,circleSize);
    case 7:
      p->drawEllipse(w/2-circleSize/2,h/3-circleSize/2,
		     circleSize,circleSize);
    case 6:
      p->drawEllipse(w/4-circleSize/2,h/6-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      break;
     
   
    case 9:
      p->drawEllipse(w/4-circleSize/2,h/6-circleSize/2,circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,3*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(3*w/4-circleSize/2,5*h/6-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,2*h/7-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,5*h/7-circleSize/2,
		     circleSize,circleSize);
      p->drawEllipse(w/2-circleSize/2,h/2-circleSize/2,
		     circleSize,circleSize);
      break; 

    default:
      kdDebug() << "cube had value " << points << endl;
      TQString s;
      s.sprintf("%d",points);
      p->drawText(w/2,h/2,s);
      break;
    }
   p->end();
   delete p;	
   
   painter->drawPixmap(contents.topLeft(),buffer);

}

#include "kcubewidget.moc"