• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kblackbox

kbbgraphicsitemballrepository.cpp

Go to the documentation of this file.
00001 //
00002 // KBlackBox
00003 //
00004 // A simple game inspired by an emacs module
00005 //
00006 /***************************************************************************
00007  *   Copyright (c) 2007, Nicolas Roffet                                    *
00008  *   nicolas-kde@roffet.com                                                *
00009  *                                                                         *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
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 // Constructor / Destructor
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 // Public
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"

kblackbox

Skip menu "kblackbox"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal