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

kblackbox

kbbgraphicsitemcursor.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 "kbbgraphicsitemcursor.h"
00028 
00029 
00030 
00031 //
00032 // Constructor / Destructor
00033 //
00034 
00035 KBBGraphicsItemCursor::KBBGraphicsItemCursor(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::cursor, parent->scene(), themeManager)
00036 {
00037     setBoardSize(1,1);
00038 }
00039 
00040 
00041 
00042 //
00043 // Public
00044 //
00045 
00046 int KBBGraphicsItemCursor::borderPosition()
00047 {
00048     return m_borderPosition;
00049 }
00050 
00051 
00052 int KBBGraphicsItemCursor::boxPosition()
00053 {
00054     return m_boxPosition;
00055 }
00056 
00057 
00058 void KBBGraphicsItemCursor::moveDown()
00059 {
00060     if (m_borderPosition!=NO_POSITION) {
00061         if (m_borderPosition<m_columns) {
00062             m_boxPosition = m_borderPosition;
00063             m_borderPosition = NO_POSITION;
00064         } else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows-1))
00065             m_borderPosition++;
00066         else if ((m_borderPosition>=2*m_columns+m_rows+1) && (m_borderPosition<2*m_columns+2*m_rows))
00067             m_borderPosition--;
00068     } else if (m_boxPosition!=NO_POSITION) {
00069         if ((m_boxPosition+m_columns)<(m_columns*m_rows))
00070             m_boxPosition += m_columns;
00071         else {
00072             m_borderPosition = 2*m_columns+m_rows-(m_boxPosition+m_columns-m_columns*m_rows)-1;
00073             m_boxPosition = NO_POSITION;
00074         }
00075     }
00076     
00077     updatePositions();
00078 }
00079 
00080 
00081 void KBBGraphicsItemCursor::moveLeft()
00082 {
00083     if (m_borderPosition!=NO_POSITION) {
00084         if ((m_borderPosition>0) && (m_borderPosition<m_columns))
00085             m_borderPosition--;
00086         else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows)) {
00087             m_boxPosition = m_columns*(m_borderPosition-m_columns+1)-1;
00088             m_borderPosition = NO_POSITION;
00089         } else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows-1))
00090             m_borderPosition++;
00091     } else if (m_boxPosition!=NO_POSITION) {
00092         if ((m_boxPosition % m_columns)!=0)
00093             m_boxPosition--;
00094         else {
00095             m_borderPosition = 2*m_columns + 2*m_rows - m_boxPosition/m_columns -1;
00096             m_boxPosition = NO_POSITION;
00097         }
00098     }
00099     
00100     updatePositions();
00101 }
00102 
00103 
00104 void KBBGraphicsItemCursor::moveRight()
00105 {
00106     if (m_borderPosition!=NO_POSITION) {
00107         if (m_borderPosition<m_columns-1)
00108             m_borderPosition++;
00109         else if ((m_borderPosition>=m_columns+m_rows+1) && (m_borderPosition<2*m_columns+m_rows))
00110             m_borderPosition--;
00111         else if (m_borderPosition>=2*m_columns+m_rows) {
00112             m_boxPosition = m_columns*(2*m_columns+2*m_rows-m_borderPosition-1);
00113             m_borderPosition = NO_POSITION;
00114         }
00115     } else if (m_boxPosition!=NO_POSITION) {
00116         if (((m_boxPosition+1) % m_columns)!=0)
00117             m_boxPosition++;
00118         else {
00119             m_borderPosition = m_columns + m_boxPosition/m_columns;
00120             m_boxPosition = NO_POSITION;
00121         }
00122     }
00123     
00124     updatePositions();
00125 }
00126 
00127 
00128 void KBBGraphicsItemCursor::moveUp()
00129 {
00130     if (m_borderPosition!=NO_POSITION) {
00131         if ((m_borderPosition>=m_columns+1) && (m_borderPosition<m_columns+m_rows))
00132             m_borderPosition--;
00133         else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows)) {
00134             m_boxPosition = m_columns*m_rows-(m_borderPosition-m_columns-m_rows)-1;
00135             m_borderPosition = NO_POSITION;
00136         } else if ((m_borderPosition>=2*m_columns+m_rows) && (m_borderPosition<2*m_columns+2*m_rows-1))
00137             m_borderPosition++;
00138     } else if (m_boxPosition!=NO_POSITION) {
00139         if ((m_boxPosition-m_columns)>=0)
00140             m_boxPosition -= m_columns;
00141         else {
00142             m_borderPosition = m_boxPosition;
00143             m_boxPosition = NO_POSITION;
00144         }
00145     }
00146     
00147     updatePositions();
00148 }
00149 
00150 
00151 void KBBGraphicsItemCursor::setBoardSize(const int columns, const int rows)
00152 {
00153     m_columns = columns;
00154     m_rows = rows;
00155     
00156     m_boxPosition = 0;
00157     m_borderPosition = NO_POSITION;
00158     
00159     updatePositions();
00160     hide();
00161 }
00162 
00163 
00164 void KBBGraphicsItemCursor::setBorderPosition(const int borderPosition)
00165 {
00166     if (borderPosition!=NO_POSITION) {
00167         m_borderPosition = borderPosition;
00168         m_boxPosition = NO_POSITION;
00169         
00170         const int b = KBBScalableGraphicWidget::BORDER_SIZE;
00171         const int r = KBBScalableGraphicWidget::RATIO;
00172         const int offset = 0;
00173         int x;
00174         int y;
00175         if (m_borderPosition<m_columns) {
00176             x = borderPosition*r + b;
00177             y = offset;
00178         } else if (borderPosition<m_columns + m_rows) {
00179             x = m_columns*r + b + b/2 - offset;
00180             y = (borderPosition - m_columns)*r + b;
00181         } else if (borderPosition<2*m_columns + m_rows) {
00182             x = (2*m_columns + m_rows - borderPosition)*r + b/2;
00183             y = m_rows*r + 3*b/2 - offset;
00184         } else {
00185             x = offset;
00186             y = (2*m_columns + 2*m_rows - borderPosition)*r + b/2;
00187         }
00188         setPos(x, y);
00189     }
00190 }
00191 
00192 
00193 void KBBGraphicsItemCursor::setBoxPosition(const int boxPosition)
00194 {
00195     if (boxPosition!=NO_POSITION) {
00196         m_borderPosition = NO_POSITION;
00197         m_boxPosition = boxPosition;
00198         
00199         setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition % m_columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition / m_columns));
00200     }
00201 }
00202 
00203 
00204 //
00205 // Private
00206 //
00207 
00208 void KBBGraphicsItemCursor::updatePositions()
00209 {
00210     setBoxPosition(m_boxPosition);
00211     setBorderPosition(m_borderPosition);
00212     
00213     emit cursorAtNewPosition(m_borderPosition);
00214 }
00215 
00216 #include "kbbgraphicsitemcursor.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