File indexing completed on 2022-11-23 10:43:24

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kbbgraphicsitemcursor.h"
0010 
0011 
0012 
0013 //
0014 // Constructor / Destructor
0015 //
0016 
0017 KBBGraphicsItemCursor::KBBGraphicsItemCursor(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::cursor, parent->scene(), themeManager)
0018 {
0019     setBoardSize(1,1);
0020 }
0021 
0022 
0023 
0024 //
0025 // Public
0026 //
0027 
0028 int KBBGraphicsItemCursor::borderPosition()
0029 {
0030     return m_borderPosition;
0031 }
0032 
0033 
0034 int KBBGraphicsItemCursor::boxPosition()
0035 {
0036     return m_boxPosition;
0037 }
0038 
0039 
0040 void KBBGraphicsItemCursor::moveDown()
0041 {
0042     if (m_borderPosition!=NO_POSITION) {
0043         if (m_borderPosition<m_columns) {
0044             m_boxPosition = m_borderPosition;
0045             m_borderPosition = NO_POSITION;
0046         } else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows-1))
0047             m_borderPosition++;
0048         else if ((m_borderPosition>=2*m_columns+m_rows+1) && (m_borderPosition<2*m_columns+2*m_rows))
0049             m_borderPosition--;
0050     } else if (m_boxPosition!=NO_POSITION) {
0051         if ((m_boxPosition+m_columns)<(m_columns*m_rows))
0052             m_boxPosition += m_columns;
0053         else {
0054             m_borderPosition = 2*m_columns+m_rows-(m_boxPosition+m_columns-m_columns*m_rows)-1;
0055             m_boxPosition = NO_POSITION;
0056         }
0057     }
0058     
0059     updatePositions();
0060 }
0061 
0062 
0063 void KBBGraphicsItemCursor::moveLeft()
0064 {
0065     if (m_borderPosition!=NO_POSITION) {
0066         if ((m_borderPosition>0) && (m_borderPosition<m_columns))
0067             m_borderPosition--;
0068         else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows)) {
0069             m_boxPosition = m_columns*(m_borderPosition-m_columns+1)-1;
0070             m_borderPosition = NO_POSITION;
0071         } else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows-1))
0072             m_borderPosition++;
0073     } else if (m_boxPosition!=NO_POSITION) {
0074         if ((m_boxPosition % m_columns)!=0)
0075             m_boxPosition--;
0076         else {
0077             m_borderPosition = 2*m_columns + 2*m_rows - m_boxPosition/m_columns -1;
0078             m_boxPosition = NO_POSITION;
0079         }
0080     }
0081     
0082     updatePositions();
0083 }
0084 
0085 
0086 void KBBGraphicsItemCursor::moveRight()
0087 {
0088     if (m_borderPosition!=NO_POSITION) {
0089         if (m_borderPosition<m_columns-1)
0090             m_borderPosition++;
0091         else if ((m_borderPosition>=m_columns+m_rows+1) && (m_borderPosition<2*m_columns+m_rows))
0092             m_borderPosition--;
0093         else if (m_borderPosition>=2*m_columns+m_rows) {
0094             m_boxPosition = m_columns*(2*m_columns+2*m_rows-m_borderPosition-1);
0095             m_borderPosition = NO_POSITION;
0096         }
0097     } else if (m_boxPosition!=NO_POSITION) {
0098         if (((m_boxPosition+1) % m_columns)!=0)
0099             m_boxPosition++;
0100         else {
0101             m_borderPosition = m_columns + m_boxPosition/m_columns;
0102             m_boxPosition = NO_POSITION;
0103         }
0104     }
0105     
0106     updatePositions();
0107 }
0108 
0109 
0110 void KBBGraphicsItemCursor::moveUp()
0111 {
0112     if (m_borderPosition!=NO_POSITION) {
0113         if ((m_borderPosition>=m_columns+1) && (m_borderPosition<m_columns+m_rows))
0114             m_borderPosition--;
0115         else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows)) {
0116             m_boxPosition = m_columns*m_rows-(m_borderPosition-m_columns-m_rows)-1;
0117             m_borderPosition = NO_POSITION;
0118         } else if ((m_borderPosition>=2*m_columns+m_rows) && (m_borderPosition<2*m_columns+2*m_rows-1))
0119             m_borderPosition++;
0120     } else if (m_boxPosition!=NO_POSITION) {
0121         if ((m_boxPosition-m_columns)>=0)
0122             m_boxPosition -= m_columns;
0123         else {
0124             m_borderPosition = m_boxPosition;
0125             m_boxPosition = NO_POSITION;
0126         }
0127     }
0128     
0129     updatePositions();
0130 }
0131 
0132 
0133 void KBBGraphicsItemCursor::setBoardSize(const int columns, const int rows)
0134 {
0135     m_columns = columns;
0136     m_rows = rows;
0137     
0138     m_boxPosition = 0;
0139     m_borderPosition = NO_POSITION;
0140     
0141     updatePositions();
0142     hide();
0143 }
0144 
0145 
0146 void KBBGraphicsItemCursor::setBorderPosition(const int borderPosition)
0147 {
0148     if (borderPosition!=NO_POSITION) {
0149         m_borderPosition = borderPosition;
0150         m_boxPosition = NO_POSITION;
0151         
0152         const int b = KBBScalableGraphicWidget::BORDER_SIZE;
0153         const int r = KBBScalableGraphicWidget::RATIO;
0154         const int offset = 0;
0155         int x;
0156         int y;
0157         if (m_borderPosition<m_columns) {
0158             x = borderPosition*r + b;
0159             y = offset;
0160         } else if (borderPosition<m_columns + m_rows) {
0161             x = m_columns*r + b + b/2 - offset;
0162             y = (borderPosition - m_columns)*r + b;
0163         } else if (borderPosition<2*m_columns + m_rows) {
0164             x = (2*m_columns + m_rows - borderPosition)*r + b/2;
0165             y = m_rows*r + 3*b/2 - offset;
0166         } else {
0167             x = offset;
0168             y = (2*m_columns + 2*m_rows - borderPosition)*r + b/2;
0169         }
0170         setPos(x, y);
0171     }
0172 }
0173 
0174 
0175 void KBBGraphicsItemCursor::setBoxPosition(const int boxPosition)
0176 {
0177     if (boxPosition!=NO_POSITION) {
0178         m_borderPosition = NO_POSITION;
0179         m_boxPosition = boxPosition;
0180         
0181         setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition % m_columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition / m_columns));
0182     }
0183 }
0184 
0185 
0186 //
0187 // Private
0188 //
0189 
0190 void KBBGraphicsItemCursor::updatePositions()
0191 {
0192     setBoxPosition(m_boxPosition);
0193     setBorderPosition(m_borderPosition);
0194     
0195     Q_EMIT cursorAtNewPosition(m_borderPosition);
0196 }
0197 
0198