kblackbox
kbbgraphicsitemballrepository.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "kbbgraphicsitemballrepository.h"
00028
00029
00030
00031 #include <QGraphicsScene>
00032
00033
00034 #include "kbbgraphicsitemonbox.h"
00035 #include "kbbgraphicsitemset.h"
00036 #include "kbbscalablegraphicwidget.h"
00037
00038
00039
00040
00041
00042
00043
00044 KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::informationBackground, parent->scene(), themeManager)
00045 {
00046 m_parent = parent;
00047 m_themeManager = themeManager;
00048 m_width = 1;
00049 m_height = 1;
00050 m_ballToPlace = 0;
00051
00052 m_random.setSeed(0);
00053
00054 m_ballsOutside = new KBBGraphicsItemSet(parent->scene());
00055 }
00056
00057
00058 KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository()
00059 {
00060 delete m_ballsOutside;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 int KBBGraphicsItemBallRepository::ballToTake() const
00070 {
00071 return m_ballsOutside->anyItemPosition();
00072 }
00073
00074
00075 void KBBGraphicsItemBallRepository::fillBallsOutside(int placed)
00076 {
00077 int i = m_columns*m_rows;
00078 while ((m_ballsOutside->count()+placed)<m_ballToPlace) {
00079 if (!(m_ballsOutside->containsVisible(i))) {
00080 KBBGraphicsItemOnBox* b = new KBBGraphicsItemOnBox(KBBScalableGraphicWidget::playerBall, m_parent, m_themeManager, i, m_columns, m_rows);
00081 m_ballsOutside->insert(b);
00082 b->setPos(x() + ((i - m_columns*m_rows) / m_height)*KBBScalableGraphicWidget::RATIO, y() + ((i - m_columns*m_rows) % m_height)*KBBScalableGraphicWidget::RATIO);
00083 }
00084 i++;
00085 }
00086 }
00087
00088
00089 void KBBGraphicsItemBallRepository::newGame(int columns, int rows, int balls)
00090 {
00091 m_columns = columns;
00092 m_rows = rows;
00093
00094 m_ballsOutside->clear();
00095
00096 m_ballToPlace = balls;
00097 scale(1./m_width, 1./m_height);
00098
00099
00100 m_height = (rows/2);
00101 if (rows % 2 > 0)
00102 m_height++;
00103 m_width = balls/m_height;
00104 if (balls % m_height > 0)
00105 m_width++;
00106
00107 scale(m_width/1., m_height/1.);
00108
00109 setPos((-(m_width+1)*KBBScalableGraphicWidget::RATIO), KBBScalableGraphicWidget::RATIO);
00110
00111 fillBallsOutside(0);
00112 }
00113
00114
00115 void KBBGraphicsItemBallRepository::removeBall(int outsidePosition)
00116 {
00117 m_ballsOutside->remove(outsidePosition);
00118 }
00119
00120 #include "kbbgraphicsitemballrepository.moc"