1 | /*************************************************************************** |
---|---|
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Mauricio Piacentini <mauricio@tabuleiro.com> * |
4 | * Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | ***************************************************************************/ |
11 | #include "KBlocksSvgItem.h" |
12 | |
13 | #include <QPainter> |
14 | |
15 | KBlocksSvgItem::KBlocksSvgItem(KBlocksLayout * p, int type, int posX, int posY) |
16 | { |
17 | mpGameLayout = p; |
18 | |
19 | mPosX = posX; |
20 | mPosY = posY; |
21 | |
22 | mType = type; |
23 | mColor = -1; |
24 | |
25 | setCacheMode(QGraphicsItem::DeviceCoordinateCache); |
26 | } |
27 | |
28 | KBlocksSvgItem::~KBlocksSvgItem() |
29 | { |
30 | } |
31 | |
32 | void KBlocksSvgItem::setLayoutPos(int posX, int posY) |
33 | { |
34 | mPosX = posX; |
35 | mPosY = posY; |
36 | } |
37 | |
38 | bool KBlocksSvgItem::updateSelf() |
39 | { |
40 | int tmpColor; |
41 | |
42 | if (mType == KBlocksSvgItem_FieldArea) |
43 | { |
44 | tmpColor = mpGameLayout->getFieldColor(mPosX, mPosY); |
45 | } |
46 | else if (mType == KBlocksSvgItem_PrepareArea) |
47 | { |
48 | tmpColor = mpGameLayout->getPrepareColor(mPosX, mPosY); |
49 | } |
50 | else |
51 | { |
52 | return false; |
53 | } |
54 | |
55 | if (mColor != tmpColor) |
56 | { |
57 | mColor = tmpColor; |
58 | if (mColor == -1) |
59 | { |
60 | setVisible(false); |
61 | } |
62 | else |
63 | { |
64 | setElementId(QString("BLOCK_%1").arg(mColor)); |
65 | setVisible(true); |
66 | } |
67 | } |
68 | |
69 | return true; |
70 | } |
71 | |
72 | void KBlocksSvgItem::startOpAnim() |
73 | { |
74 | setElementId(QString("BLOCK_OUT_%1").arg(mColor)); |
75 | } |
76 | |
77 | void KBlocksSvgItem::stopOpAnim() |
78 | { |
79 | setElementId(QString("BLOCK_%1").arg(mColor)); |
80 | } |
81 | |
82 | void KBlocksSvgItem::startPosAnim(QPointF target) |
83 | { |
84 | mOriginPos = pos(); |
85 | mTargetPos = pos()+ target; |
86 | } |
87 | |
88 | void KBlocksSvgItem::execPosAnim(qreal step) |
89 | { |
90 | QPointF delta = mTargetPos - mOriginPos; |
91 | delta = delta * step; |
92 | setPos(mOriginPos + delta); |
93 | } |
94 | |
95 | void KBlocksSvgItem::stopPosAnim() |
96 | { |
97 | setPos(mOriginPos); |
98 | mTargetPos = mOriginPos; |
99 | } |
100 |