File indexing completed on 2024-04-21 04:02:09

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Mauricio Piacentini <mauricio@tabuleiro.com>       *
0004 *                      Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0005 *                                                                         *
0006 *   SPDX-License-Identifier: GPL-2.0-or-later
0007 ***************************************************************************/
0008 #include "KBlocksSvgItem.h"
0009 
0010 
0011 KBlocksSvgItem::KBlocksSvgItem(KBlocksLayout *p, int type, int posX, int posY)
0012 {
0013     mpGameLayout = p;
0014 
0015     mPosX = posX;
0016     mPosY = posY;
0017 
0018     mType = type;
0019     mColor = -1;
0020 
0021     setCacheMode(QGraphicsItem::DeviceCoordinateCache);
0022 }
0023 
0024 KBlocksSvgItem::~KBlocksSvgItem()
0025 {
0026 }
0027 
0028 void KBlocksSvgItem::setLayoutPos(int posX, int posY)
0029 {
0030     mPosX = posX;
0031     mPosY = posY;
0032 }
0033 
0034 bool KBlocksSvgItem::updateSelf()
0035 {
0036     int tmpColor;
0037 
0038     if (mType == KBlocksSvgItem_FieldArea) {
0039         tmpColor = mpGameLayout->getFieldColor(mPosX, mPosY);
0040     } else if (mType == KBlocksSvgItem_PrepareArea) {
0041         tmpColor = mpGameLayout->getPrepareColor(mPosX, mPosY);
0042     } else {
0043         return false;
0044     }
0045 
0046     if (mColor != tmpColor) {
0047         mColor = tmpColor;
0048         if (mColor == -1) {
0049             setVisible(false);
0050         } else {
0051             setElementId(QStringLiteral("BLOCK_%1").arg(mColor));
0052             setVisible(true);
0053         }
0054     }
0055 
0056     return true;
0057 }
0058 
0059 void KBlocksSvgItem::clearCache()
0060 {
0061     setCacheMode(NoCache);
0062     setCacheMode(DeviceCoordinateCache);
0063 }
0064 
0065 
0066 void KBlocksSvgItem::startOpAnim()
0067 {
0068     setElementId(QStringLiteral("BLOCK_OUT_%1").arg(mColor));
0069 }
0070 
0071 void KBlocksSvgItem::stopOpAnim()
0072 {
0073     setElementId(QStringLiteral("BLOCK_%1").arg(mColor));
0074 }
0075 
0076 void KBlocksSvgItem::startPosAnim(QPointF target)
0077 {
0078     mOriginPos = pos();
0079     mTargetPos = pos() + target;
0080 }
0081 
0082 void KBlocksSvgItem::execPosAnim(qreal step)
0083 {
0084     QPointF delta = mTargetPos - mOriginPos;
0085     delta = delta * step;
0086     setPos(mOriginPos + delta);
0087 }
0088 
0089 void KBlocksSvgItem::stopPosAnim()
0090 {
0091     setPos(mOriginPos);
0092     mTargetPos = mOriginPos;
0093 }
0094 
0095 #include "moc_KBlocksSvgItem.cpp"