File indexing completed on 2022-11-29 18:38:54

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kbbgraphicsitemballrepository.h"
0010 
0011 
0012 
0013 #include <QGraphicsScene>
0014 
0015 
0016 #include "kbbgraphicsitemonbox.h"
0017 #include "kbbgraphicsitemset.h"
0018 #include "kbbscalablegraphicwidget.h"
0019 
0020 
0021 
0022 //
0023 // Constructor / Destructor
0024 //
0025 
0026 KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::informationBackground, parent->scene(), themeManager)
0027 {
0028     m_parent = parent;
0029     m_themeManager = themeManager;
0030     m_width = 1;
0031     m_height = 1;
0032     m_ballToPlace = 0;
0033 
0034     m_ballsOutside = new KBBGraphicsItemSet(parent->scene());
0035 }
0036 
0037 
0038 KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository()
0039 {
0040     delete m_ballsOutside;
0041 }
0042 
0043 
0044 
0045 //
0046 // Public
0047 //
0048 
0049 int KBBGraphicsItemBallRepository::ballToTake() const
0050 {
0051     return m_ballsOutside->anyItemPosition();
0052 }
0053 
0054 
0055 void KBBGraphicsItemBallRepository::fillBallsOutside(int placed)
0056 {
0057     int i = m_columns*m_rows;
0058     while ((m_ballsOutside->count()+placed)<m_ballToPlace) {
0059         if (!(m_ballsOutside->containsVisible(i))) {
0060             KBBGraphicsItemOnBox* b = new KBBGraphicsItemOnBox(KBBScalableGraphicWidget::playerBall, m_parent, m_themeManager, i, m_columns, m_rows);
0061             m_ballsOutside->insert(b);
0062             b->setPos(x() + ((i - m_columns*m_rows) / m_height)*KBBScalableGraphicWidget::RATIO, y() + ((i - m_columns*m_rows) % m_height)*KBBScalableGraphicWidget::RATIO);
0063         }
0064         i++;
0065     }
0066 }
0067 
0068 
0069 void KBBGraphicsItemBallRepository::newGame(int columns, int rows, int balls)
0070 {
0071     m_columns = columns;
0072     m_rows = rows;
0073 
0074     m_ballsOutside->clear();
0075 
0076     m_ballToPlace = balls;
0077     setTransform(QTransform::fromScale(1./m_width, 1./m_height), true);
0078 
0079     m_height = (rows/2);
0080     if (rows % 2 > 0)
0081         m_height++;
0082     m_width = balls/m_height;
0083     if (balls % m_height > 0)
0084         m_width++;
0085 
0086     setTransform(QTransform::fromScale(m_width/1., m_height/1.), true);
0087 
0088     setPos((-(m_width+1)*KBBScalableGraphicWidget::RATIO), KBBScalableGraphicWidget::RATIO);
0089 
0090     fillBallsOutside(0);
0091 }
0092 
0093 
0094 void KBBGraphicsItemBallRepository::removeBall(int outsidePosition)
0095 {
0096     m_ballsOutside->remove(outsidePosition);
0097 }
0098 
0099