SVN_SILENT made messages (.desktop file)
[kdegames.git] / kblackbox / kbbgraphicsitemballrepository.cpp
blob1c8ac35b92410e806918bdd1c0c2acbe534aa200
1 //
2 // KBlackBox
3 //
4 // A simple game inspired by an emacs module
5 //
6 /***************************************************************************
7 * Copyright (c) 2007, Nicolas Roffet *
8 * nicolas-kde@roffet.com *
9 * *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA *
25 ***************************************************************************/
27 #include "kbbgraphicsitemballrepository.h"
31 #include <QGraphicsScene>
34 #include "kbbgraphicsitemonbox.h"
35 #include "kbbgraphicsitemset.h"
36 #include "kbbscalablegraphicwidget.h"
41 // Constructor / Destructor
44 KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::informationBackground, parent->scene(), themeManager)
46 m_parent = parent;
47 m_themeManager = themeManager;
48 m_width = 1;
49 m_height = 1;
50 m_ballToPlace = 0;
52 m_random.setSeed(0);
54 m_ballsOutside = new KBBGraphicsItemSet(parent->scene());
58 KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository()
60 delete m_ballsOutside;
66 // Public
69 int KBBGraphicsItemBallRepository::ballToTake() const
71 return m_ballsOutside->anyItemPosition();
75 void KBBGraphicsItemBallRepository::fillBallsOutside(int placed)
77 int i = m_columns*m_rows;
78 while ((m_ballsOutside->count()+placed)<m_ballToPlace) {
79 if (!(m_ballsOutside->containsVisible(i))) {
80 KBBGraphicsItemOnBox* b = new KBBGraphicsItemOnBox(KBBScalableGraphicWidget::playerBall, m_parent, m_themeManager, i, m_columns, m_rows);
81 m_ballsOutside->insert(b);
82 b->setPos(x() + ((i - m_columns*m_rows) / m_height)*KBBScalableGraphicWidget::RATIO, y() + ((i - m_columns*m_rows) % m_height)*KBBScalableGraphicWidget::RATIO);
84 i++;
89 void KBBGraphicsItemBallRepository::newGame(int columns, int rows, int balls)
91 m_columns = columns;
92 m_rows = rows;
94 m_ballsOutside->clear();
96 m_ballToPlace = balls;
97 scale(1./m_width, 1./m_height);
100 m_height = (rows/2);
101 if (rows % 2 > 0)
102 m_height++;
103 m_width = balls/m_height;
104 if (balls % m_height > 0)
105 m_width++;
107 scale(m_width/1., m_height/1.);
109 setPos((-(m_width+1)*KBBScalableGraphicWidget::RATIO), KBBScalableGraphicWidget::RATIO);
111 fillBallsOutside(0);
115 void KBBGraphicsItemBallRepository::removeBall(int outsidePosition)
117 m_ballsOutside->remove(outsidePosition);
120 #include "kbbgraphicsitemballrepository.moc"