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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
#include <tqbitmap.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqimage.h>
#include <tqpixmapcache.h>
#include <tqwhatsthis.h>
#include <tdeapplication.h>
#include <kcombobox.h>
#include <tdeconfig.h>
#include <knuminput.h>
#include <kpixmapeffect.h>
#include <kstandarddirs.h>
#include "slope.h"
Slope::Slope(TQRect rect, TQCanvas *canvas)
: TQCanvasRectangle(rect, canvas), type(KImageEffect::VerticalGradient), grade(4), reversed(false), color(TQColor("#327501"))
{
stuckOnGround = false;
showingInfo = false;
gradientKeys[KImageEffect::VerticalGradient] = "Vertical";
gradientKeys[KImageEffect::HorizontalGradient] = "Horizontal";
gradientKeys[KImageEffect::DiagonalGradient] = "Diagonal";
gradientKeys[KImageEffect::CrossDiagonalGradient] = "Opposite Diagonal";
gradientKeys[KImageEffect::EllipticGradient] = "Elliptic";
gradientI18nKeys[KImageEffect::VerticalGradient] = i18n("Vertical");
gradientI18nKeys[KImageEffect::HorizontalGradient] = i18n("Horizontal");
gradientI18nKeys[KImageEffect::DiagonalGradient] = i18n("Diagonal");
gradientI18nKeys[KImageEffect::CrossDiagonalGradient] = i18n("Opposite Diagonal");
gradientI18nKeys[KImageEffect::EllipticGradient] = i18n("Circular");
setZ(-50);
if (!TQPixmapCache::find("grass", grass))
{
grass.load(locate("appdata", "pics/grass.png"));
TQPixmapCache::insert("grass", grass);
}
point = new RectPoint(color.light(), this, canvas);
TQFont font(kapp->font());
font.setPixelSize(18);
text = new TQCanvasText(canvas);
text->setZ(99999.99);
text->setFont(font);
text->setColor(white);
editModeChanged(false);
hideInfo();
// this does updatePixmap
setGradient("Vertical");
}
bool Slope::terrainCollisions() const
{
// we are a terrain collision
return true;
}
void Slope::showInfo()
{
showingInfo = true;
Arrow *arrow = 0;
for (arrow = arrows.first(); arrow; arrow = arrows.next())
{
arrow->setZ(z() + .01);
arrow->setVisible(true);
}
text->setVisible(true);
}
void Slope::hideInfo()
{
showingInfo = false;
Arrow *arrow = 0;
for (arrow = arrows.first(); arrow; arrow = arrows.next())
arrow->setVisible(false);
text->setVisible(false);
}
void Slope::aboutToDie()
{
delete point;
clearArrows();
delete text;
}
void Slope::clearArrows()
{
Arrow *arrow = 0;
for (arrow = arrows.first(); arrow; arrow = arrows.next())
{
arrow->setVisible(false);
arrow->aboutToDie();
}
arrows.setAutoDelete(true);
arrows.clear();
arrows.setAutoDelete(false);
}
TQPtrList<TQCanvasItem> Slope::moveableItems() const
{
TQPtrList<TQCanvasItem> ret;
ret.append(point);
return ret;
}
void Slope::setGrade(double newGrade)
{
if (newGrade >= 0 && newGrade < 11)
{
grade = newGrade;
updatePixmap();
}
}
void Slope::setSize(int width, int height)
{
newSize(width, height);
}
void Slope::newSize(int width, int height)
{
if (type == KImageEffect::EllipticGradient)
{
TQCanvasRectangle::setSize(width, width);
// move point back to good spot
moveBy(0, 0);
if (game && game->isEditing())
game->updateHighlighter();
}
else
TQCanvasRectangle::setSize(width, height);
updatePixmap();
updateZ();
}
void Slope::moveBy(double dx, double dy)
{
TQCanvasRectangle::moveBy(dx, dy);
point->dontMove();
point->move(x() + width(), y() + height());
moveArrow();
updateZ();
}
void Slope::moveArrow()
{
int xavg = 0, yavg = 0;
TQPointArray r = areaPoints();
for (unsigned int i = 0; i < r.size(); ++i)
{
xavg += r[i].x();
yavg += r[i].y();
}
xavg /= r.size();
yavg /= r.size();
Arrow *arrow = 0;
for (arrow = arrows.first(); arrow; arrow = arrows.next())
arrow->move((double)xavg, (double)yavg);
if (showingInfo)
showInfo();
else
hideInfo();
text->move((double)xavg - text->boundingRect().width() / 2, (double)yavg - text->boundingRect().height() / 2);
}
void Slope::editModeChanged(bool changed)
{
point->setVisible(changed);
moveBy(0, 0);
}
void Slope::updateZ(TQCanvasRectangle *vStrut)
{
const int area = (height() * width());
const int defaultz = -50;
double newZ = 0;
TQCanvasRectangle *rect = 0;
if (!stuckOnGround)
rect = vStrut? vStrut : onVStrut();
if (rect)
{
if (area > (rect->width() * rect->height()))
newZ = defaultz;
else
newZ = rect->z();
}
else
newZ = defaultz;
setZ(((double)1 / (area == 0? 1 : area)) + newZ);
}
void Slope::load(TDEConfig *cfg)
{
stuckOnGround = cfg->readBoolEntry("stuckOnGround", stuckOnGround);
grade = cfg->readDoubleNumEntry("grade", grade);
reversed = cfg->readBoolEntry("reversed", reversed);
// bypass updatePixmap which newSize normally does
TQCanvasRectangle::setSize(cfg->readNumEntry("width", width()), cfg->readNumEntry("height", height()));
updateZ();
TQString gradientType = cfg->readEntry("gradient", gradientKeys[type]);
setGradient(gradientType);
}
void Slope::save(TDEConfig *cfg)
{
cfg->writeEntry("reversed", reversed);
cfg->writeEntry("width", width());
cfg->writeEntry("height", height());
cfg->writeEntry("gradient", gradientKeys[type]);
cfg->writeEntry("grade", grade);
cfg->writeEntry("stuckOnGround", stuckOnGround);
}
void Slope::draw(TQPainter &painter)
{
painter.drawPixmap(x(), y(), pixmap);
}
TQPointArray Slope::areaPoints() const
{
switch (type)
{
case KImageEffect::CrossDiagonalGradient:
{
TQPointArray ret(3);
ret[0] = TQPoint((int)x(), (int)y());
ret[1] = TQPoint((int)x() + width(), (int)y() + height());
ret[2] = reversed? TQPoint((int)x() + width(), y()) : TQPoint((int)x(), (int)y() + height());
return ret;
}
case KImageEffect::DiagonalGradient:
{
TQPointArray ret(3);
ret[0] = TQPoint((int)x() + width(), (int)y());
ret[1] = TQPoint((int)x(), (int)y() + height());
ret[2] = !reversed? TQPoint((int)x() + width(), y() + height()) : TQPoint((int)x(), (int)y());
return ret;
}
case KImageEffect::EllipticGradient:
{
TQPointArray ret;
ret.makeEllipse((int)x(), (int)y(), width(), height());
return ret;
}
default:
return TQCanvasRectangle::areaPoints();
}
}
bool Slope::collision(Ball *ball, long int /*id*/)
{
if (grade <= 0)
return false;
double vx = ball->xVelocity();
double vy = ball->yVelocity();
double addto = 0.013 * grade;
const bool diag = type == KImageEffect::DiagonalGradient || type == KImageEffect::CrossDiagonalGradient;
const bool circle = type == KImageEffect::EllipticGradient;
double slopeAngle = 0;
if (diag)
slopeAngle = atan((double)width() / (double)height());
else if (circle)
{
const TQPoint start(x() + (int)width() / 2.0, y() + (int)height() / 2.0);
const TQPoint end((int)ball->x(), (int)ball->y());
Vector betweenVector(start, end);
const double factor = betweenVector.magnitude() / ((double)width() / 2.0);
slopeAngle = betweenVector.direction();
// this little bit by Daniel
addto *= factor * M_PI / 2;
addto = sin(addto);
}
switch (type)
{
case KImageEffect::HorizontalGradient:
reversed? vx += addto : vx -= addto;
break;
case KImageEffect::VerticalGradient:
reversed? vy += addto : vy -= addto;
break;
case KImageEffect::DiagonalGradient:
case KImageEffect::EllipticGradient:
reversed? vx += cos(slopeAngle) * addto : vx -= cos(slopeAngle) * addto;
reversed? vy += sin(slopeAngle) * addto : vy -= sin(slopeAngle) * addto;
break;
case KImageEffect::CrossDiagonalGradient:
reversed? vx -= cos(slopeAngle) * addto : vx += cos(slopeAngle) * addto;
reversed? vy += sin(slopeAngle) * addto : vy -= sin(slopeAngle) * addto;
break;
default:
break;
}
ball->setVelocity(vx, vy);
// check if the ball is at the center of a pit or mound
// or has otherwise stopped.
if (vx == 0 && vy ==0)
ball->setState(Stopped);
else
ball->setState(Rolling);
// do NOT do terrain collisions
return false;
}
void Slope::setGradient(TQString text)
{
for (TQMap<KImageEffect::GradientType, TQString>::Iterator it = gradientKeys.begin(); it != gradientKeys.end(); ++it)
{
if (it.data() == text)
{
setType(it.key());
return;
}
}
// extra forgiveness ;-) (note it's i18n keys)
for (TQMap<KImageEffect::GradientType, TQString>::Iterator it = gradientI18nKeys.begin(); it != gradientI18nKeys.end(); ++it)
{
if (it.data() == text)
{
setType(it.key());
return;
}
}
}
void Slope::setType(KImageEffect::GradientType type)
{
this->type = type;
if (type == KImageEffect::EllipticGradient)
{
// calls updatePixmap
newSize(width(), height());
}
else
updatePixmap();
}
void Slope::updatePixmap()
{
// make a gradient, make grass that's bright or dim
// merge into this->pixmap. This is drawn in draw()
// we update the arrows in this function
clearArrows();
const bool diag = type == KImageEffect::DiagonalGradient || type == KImageEffect::CrossDiagonalGradient;
const bool circle = type == KImageEffect::EllipticGradient;
const TQColor darkColor = color.dark(100 + grade * (circle? 20 : 10));
const TQColor lightColor = diag || circle? color.light(110 + (diag? 5 : .5) * grade) : color;
// hack only for circles
const bool _reversed = circle? !reversed : reversed;
TQImage gradientImage = KImageEffect::gradient(TQSize(width(), height()), _reversed? darkColor : lightColor, _reversed? lightColor : darkColor, type);
TQPixmap qpixmap(width(), height());
TQPainter p(&qpixmap);
p.drawTiledPixmap(TQRect(0, 0, width(), height()), grass);
p.end();
const double length = sqrt(width() * width() + height() * height()) / 4;
if (circle)
{
const TQColor otherLightColor = color.light(110 + 15 * grade);
const TQColor otherDarkColor = darkColor.dark(110 + 20 * grade);
TQImage otherGradientImage = KImageEffect::gradient(TQSize(width(), height()), reversed? otherDarkColor : otherLightColor, reversed? otherLightColor : otherDarkColor, KImageEffect::DiagonalGradient);
TQImage grassImage(qpixmap.convertToImage());
TQImage finalGradientImage = KImageEffect::blend(otherGradientImage, gradientImage, .60);
pixmap.convertFromImage(KImageEffect::blend(grassImage, finalGradientImage, .40));
// make arrows
double angle = 0;
for (int i = 0; i < 4; ++i)
{
angle += M_PI / 2;
Arrow *arrow = new Arrow(canvas());
arrow->setLength(length);
arrow->setAngle(angle);
arrow->setReversed(reversed);
arrow->updateSelf();
arrows.append(arrow);
}
}
else
{
Arrow *arrow = new Arrow(canvas());
float ratio = 0;
float factor = 1;
double angle = 0;
switch (type)
{
case KImageEffect::HorizontalGradient:
angle = 0;
factor = .32;
break;
case KImageEffect::VerticalGradient:
angle = M_PI / 2;
factor = .32;
break;
case KImageEffect::DiagonalGradient:
angle = atan((double)width() / (double)height());
factor = .45;
break;
case KImageEffect::CrossDiagonalGradient:
angle = atan((double)width() / (double)height());
angle = M_PI - angle;
factor = .05;
break;
default:
break;
}
float factorPart = factor * 2;
// gradePart is out of 1
float gradePart = grade / 8.0;
ratio = factorPart * gradePart;
// reverse the reversed ones
if (reversed)
ratio *= -1;
else
angle += M_PI;
KPixmap kpixmap = qpixmap;
(void) KPixmapEffect::intensity(kpixmap, ratio);
TQImage grassImage(kpixmap.convertToImage());
// okay, now we have a grass image that's
// appropriately lit, and a gradient;
// lets blend..
pixmap.convertFromImage(KImageEffect::blend(gradientImage, grassImage, .42));
arrow->setAngle(angle);
arrow->setLength(length);
arrow->updateSelf();
arrows.append(arrow);
}
text->setText(TQString::number(grade));
if (diag || circle)
{
// make cleared bitmap
TQBitmap bitmap(pixmap.width(), pixmap.height(), true);
TQPainter bpainter(&bitmap);
bpainter.setBrush(color1);
TQPointArray r = areaPoints();
// shift all the points
for (unsigned int i = 0; i < r.count(); ++i)
{
TQPoint &p = r[i];
p.setX(p.x() - x());
p.setY(p.y() - y());
}
bpainter.drawPolygon(r);
// mask is drawn
pixmap.setMask(bitmap);
}
moveArrow();
update();
}
/////////////////////////
SlopeConfig::SlopeConfig(Slope *slope, TQWidget *parent)
: Config(parent)
{
this->slope = slope;
TQVBoxLayout *layout = new TQVBoxLayout(this, marginHint(), spacingHint());
KComboBox *gradient = new KComboBox(this);
TQStringList items;
TQString curText;
for (TQMap<KImageEffect::GradientType, TQString>::Iterator it = slope->gradientI18nKeys.begin(); it != slope->gradientI18nKeys.end(); ++it)
{
if (it.key() == slope->curType())
curText = it.data();
items.append(it.data());
}
gradient->insertStringList(items);
gradient->setCurrentText(curText);
layout->addWidget(gradient);
connect(gradient, TQT_SIGNAL(activated(const TQString &)), this, TQT_SLOT(setGradient(const TQString &)));
layout->addStretch();
TQCheckBox *reversed = new TQCheckBox(i18n("Reverse direction"), this);
reversed->setChecked(slope->isReversed());
layout->addWidget(reversed);
connect(reversed, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setReversed(bool)));
TQHBoxLayout *hlayout = new TQHBoxLayout(layout, spacingHint());
hlayout->addWidget(new TQLabel(i18n("Grade:"), this));
KDoubleNumInput *grade = new KDoubleNumInput(this);
grade->setRange(0, 8, 1, true);
grade->setValue(slope->curGrade());
hlayout->addWidget(grade);
connect(grade, TQT_SIGNAL(valueChanged(double)), this, TQT_SLOT(gradeChanged(double)));
TQCheckBox *stuck = new TQCheckBox(i18n("Unmovable"), this);
TQWhatsThis::add(stuck, i18n("Whether or not this slope can be moved by other objects, like floaters."));
stuck->setChecked(slope->isStuckOnGround());
layout->addWidget(stuck);
connect(stuck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setStuckOnGround(bool)));
}
void SlopeConfig::setGradient(const TQString &text)
{
slope->setGradient(text);
changed();
}
void SlopeConfig::setReversed(bool yes)
{
slope->setReversed(yes);
changed();
}
void SlopeConfig::setStuckOnGround(bool yes)
{
slope->setStuckOnGround(yes);
changed();
}
void SlopeConfig::gradeChanged(double newgrade)
{
slope->setGrade(newgrade);
changed();
}
#include "slope.moc"
|