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

kblackbox

kbbgraphicsitemball.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) 1999-2000, Robert Cimrman                               *
00008  *   cimrman3@students.zcu.cz                                              *
00009  *                                                                         *
00010  *   Copyright (c) 2007, Nicolas Roffet                                    *
00011  *   nicolas-kde@roffet.com                                                *
00012  *                                                                         *
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  *   This program is distributed in the hope that it will be useful,       *
00020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00022  *   GNU General Public License for more details.                          *
00023  *                                                                         *
00024  *   You should have received a copy of the GNU General Public License     *
00025  *   along with this program; if not, write to the                         *
00026  *   Free Software Foundation, Inc.,                                       *
00027  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
00028  ***************************************************************************/
00029 
00030 #include "kbbgraphicsitemball.h"
00031 
00032 
00033 
00034 #include <QGraphicsSceneHoverEvent>
00035 #include <QTimer>
00036 
00037 
00038 #include "kbbgraphicsiteminteractioninfo.h"
00039 #include "kbbgraphicsitemonbox.h"
00040 #include "kbbscalablegraphicwidget.h"
00041 #include "kbbthememanager.h"
00042 
00043 
00044 
00045 //
00046 // Constructor / Destructor
00047 //
00048 
00049 KBBGraphicsItemBall::KBBGraphicsItemBall(KBBScalableGraphicWidget::itemType itemType, KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager, int boxPosition, int columns, int rows) : KBBGraphicsItemOnBox( itemType, parent, themeManager, boxPosition, columns, rows)
00050 {
00051     m_timer = NULL;
00052     m_ballType = itemType;
00053     m_themeManager = themeManager;
00054     
00055     setAcceptsHoverEvents(true);
00056     
00057     for (int i=0; i<8;i++)
00058         m_interactionInfos[i] = NULL;
00059 }
00060 
00061 
00062 KBBGraphicsItemBall::~KBBGraphicsItemBall()
00063 {
00064     removeInteractionInfos();
00065 }
00066 
00067 
00068 
00069 //
00070 // Private slots
00071 //
00072 
00073 void KBBGraphicsItemBall::showInteractions()
00074 {
00075     delete m_timer;
00076     m_timer = NULL;
00077     
00078     const int offsetX[8] = {0, 1, 2, 2, 2, 1, 0, 0};
00079     const int offsetY[8] = {0, 0, 0, 1, 2, 2, 2, 1};
00080     qreal posX;
00081     qreal posY;
00082     
00083     // General interactions for every balls:
00084     for (int i=0; i<8;i++) {
00085         posX = x() - KBBScalableGraphicWidget::RATIO/2 + offsetX[i]*KBBScalableGraphicWidget::RATIO;
00086         posY = y() - KBBScalableGraphicWidget::RATIO/2 + offsetY[i]*KBBScalableGraphicWidget::RATIO;
00087         KBBScalableGraphicWidget::itemType type = KBBScalableGraphicWidget::interactionInfoDeflection;
00088         if (i%2 == 1)
00089             type = KBBScalableGraphicWidget::interactionInfoHit;
00090         m_interactionInfos[i] = new KBBGraphicsItemInteractionInfo(m_widget, m_themeManager, type, posX, posY, i*45 );
00091     }
00092     
00093     // If the ball is on a border:
00094     const KBBScalableGraphicWidget::itemType r = KBBScalableGraphicWidget::interactionInfoReflection;
00095     const KBBScalableGraphicWidget::itemType rS = KBBScalableGraphicWidget::interactionInfoReflectionSym;
00096     if (position()<m_columns) {
00097         m_interactionInfos[0]->setType(r);
00098         m_interactionInfos[2]->setType(rS);
00099     }
00100     if (position()>=m_columns*(m_rows-1)) {
00101         m_interactionInfos[4]->setType(r);
00102         m_interactionInfos[6]->setType(rS);
00103     }
00104     if (position()%m_columns == 0) {
00105         m_interactionInfos[6]->setType(r);
00106         m_interactionInfos[0]->setType(rS);
00107     }
00108     if (position()%m_columns == (m_columns-1)) {
00109         m_interactionInfos[2]->setType(r);
00110         m_interactionInfos[4]->setType(rS);
00111     }
00112     
00113     // If the ball is on a corner:
00114     const KBBScalableGraphicWidget::itemType n = KBBScalableGraphicWidget::interactionInfoNothing;
00115     if (position()==0)
00116         m_interactionInfos[0]->setType(n);
00117     if (position()==m_columns-1)
00118         m_interactionInfos[2]->setType(n);
00119     if (position()==m_rows*m_columns-1)
00120         m_interactionInfos[4]->setType(n);
00121     if (position()==(m_rows-1)*m_columns)
00122         m_interactionInfos[6]->setType(n);
00123 }
00124 
00125 
00126 
00127 //
00128 // Private
00129 //
00130 
00131 void KBBGraphicsItemBall::hoverEnterEvent (QGraphicsSceneHoverEvent*)
00132 {
00133     if (m_timer==NULL) {
00134         m_timer = new QTimer(this);
00135         connect(m_timer, SIGNAL(timeout()), this, SLOT(showInteractions()));
00136         m_timer->start(TIME_TO_WAIT_BEFORE_SHOWING_INTERACTIONS);
00137     }
00138 }
00139 
00140 
00141 void KBBGraphicsItemBall::hoverLeaveEvent (QGraphicsSceneHoverEvent*)
00142 {
00143     delete m_timer;
00144     m_timer = NULL;
00145     removeInteractionInfos();
00146 }
00147 
00148 
00149 void KBBGraphicsItemBall::removeInteractionInfos()
00150 {
00151     for (int i=0; i<8;i++) {
00152         delete m_interactionInfos[i];
00153         m_interactionInfos[i] = NULL;
00154     }
00155 }
00156 
00157 #include "kbbgraphicsitemball.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