File indexing completed on 2024-04-14 03:59:52

0001 /*
0002     SPDX-FileCopyrightText: 2012 Christian Krippendorf <Coding@Christian-Krippendorf.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // own
0008 #include "selectionanimation.h"
0009 
0010 // Qt
0011 #include <QList>
0012 
0013 // KMahjongg
0014 #include "gameitem.h"
0015 
0016 SelectionAnimation::SelectionAnimation(QObject * parent)
0017     : QTimer(parent)
0018     , m_animationSpeed(0)
0019     , m_repetitions(0)
0020     , m_finishedRepetitions(0)
0021     , m_itemsSelected(false)
0022     , m_gameItems(new QList<GameItem *>())
0023 {
0024     connect(this, &SelectionAnimation::timeout, this, &SelectionAnimation::timeoutOccurred);
0025 }
0026 
0027 SelectionAnimation::~SelectionAnimation()
0028 {
0029     delete m_gameItems;
0030 }
0031 
0032 void SelectionAnimation::setRepetitions(int repetitions)
0033 {
0034     m_repetitions = repetitions;
0035 }
0036 
0037 void SelectionAnimation::setAnimationSpeed(int animationSpeed)
0038 {
0039     m_animationSpeed = animationSpeed;
0040 }
0041 
0042 int SelectionAnimation::getAnimationSpeed() const
0043 {
0044     return m_animationSpeed;
0045 }
0046 
0047 int SelectionAnimation::getRepetitions() const
0048 {
0049     return m_repetitions;
0050 }
0051 
0052 void SelectionAnimation::addGameItem(GameItem * gameItem)
0053 {
0054     m_gameItems->append(gameItem);
0055 }
0056 
0057 QList<GameItem *> SelectionAnimation::getGameItems() const
0058 {
0059     return *m_gameItems;
0060 }
0061 
0062 void SelectionAnimation::start()
0063 {
0064     QTimer::start(m_animationSpeed);
0065 }
0066 
0067 void SelectionAnimation::stop()
0068 {
0069     m_gameItems->clear();
0070     m_finishedRepetitions = 8;
0071     QTimer::stop();
0072     m_finishedRepetitions = 0;
0073     setSelectedGameItems(false);
0074     m_itemsSelected = false;
0075 }
0076 
0077 void SelectionAnimation::timeoutOccurred()
0078 {
0079     if (m_itemsSelected) {
0080         // Items are selected, so deselect them.
0081         setSelectedGameItems(false);
0082         m_itemsSelected = false;
0083     } else {
0084         // Any animation repetitions left?
0085         if (m_repetitions > m_finishedRepetitions) {
0086             setSelectedGameItems(true);
0087             m_itemsSelected = true;
0088             ++m_finishedRepetitions;
0089             start();
0090         } else {
0091             stop();
0092         }
0093     }
0094 }
0095 
0096 void SelectionAnimation::setSelectedGameItems(bool selected)
0097 {
0098     for (int i = 0; i < m_gameItems->size(); ++i) {
0099         m_gameItems->at(i)->setSelected(selected);
0100     }
0101 }
0102 
0103 #include "moc_selectionanimation.cpp"