File indexing completed on 2024-03-24 04:04:42
0001 /* 0002 KBlackBox - A simple game inspired by an emacs module 0003 0004 SPDX-FileCopyrightText: 1999-2000 Robert Cimrman <cimrman3@students.zcu.cz> 0005 SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "kbbgraphicsitemball.h" 0011 0012 0013 0014 #include <QGraphicsSceneHoverEvent> 0015 #include <QTimer> 0016 0017 0018 #include "kbbgraphicsiteminteractioninfo.h" 0019 #include "kbbscalablegraphicwidget.h" 0020 #include "kbbthememanager.h" 0021 0022 0023 0024 // 0025 // Constructor / Destructor 0026 // 0027 0028 KBBGraphicsItemBall::KBBGraphicsItemBall(KBBScalableGraphicWidget::itemType itemType, KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager, int boxPosition, int columns, int rows) : KBBGraphicsItemOnBox( itemType, parent, themeManager, boxPosition, columns, rows) 0029 { 0030 m_timer = nullptr; 0031 m_ballType = itemType; 0032 m_themeManager = themeManager; 0033 0034 setAcceptHoverEvents(true); 0035 0036 for (int i=0; i<8;i++) 0037 m_interactionInfos[i] = nullptr; 0038 } 0039 0040 0041 KBBGraphicsItemBall::~KBBGraphicsItemBall() 0042 { 0043 removeInteractionInfos(); 0044 } 0045 0046 0047 0048 // 0049 // Private slots 0050 // 0051 0052 void KBBGraphicsItemBall::showInteractions() 0053 { 0054 delete m_timer; 0055 m_timer = nullptr; 0056 0057 const int offsetX[8] = {0, 1, 2, 2, 2, 1, 0, 0}; 0058 const int offsetY[8] = {0, 0, 0, 1, 2, 2, 2, 1}; 0059 qreal posX; 0060 qreal posY; 0061 0062 // General interactions for every balls: 0063 for (int i=0; i<8;i++) { 0064 posX = x() - KBBScalableGraphicWidget::RATIO/2 + offsetX[i]*KBBScalableGraphicWidget::RATIO; 0065 posY = y() - KBBScalableGraphicWidget::RATIO/2 + offsetY[i]*KBBScalableGraphicWidget::RATIO; 0066 KBBScalableGraphicWidget::itemType type = KBBScalableGraphicWidget::interactionInfoDeflection; 0067 if (i%2 == 1) 0068 type = KBBScalableGraphicWidget::interactionInfoHit; 0069 m_interactionInfos[i] = new KBBGraphicsItemInteractionInfo(m_widget, m_themeManager, type, posX, posY, i*45 ); 0070 } 0071 0072 // If the ball is on a border: 0073 const KBBScalableGraphicWidget::itemType r = KBBScalableGraphicWidget::interactionInfoReflection; 0074 const KBBScalableGraphicWidget::itemType rS = KBBScalableGraphicWidget::interactionInfoReflectionSym; 0075 if (position()<m_columns) { 0076 m_interactionInfos[0]->setType(r); 0077 m_interactionInfos[2]->setType(rS); 0078 } 0079 if (position()>=m_columns*(m_rows-1)) { 0080 m_interactionInfos[4]->setType(r); 0081 m_interactionInfos[6]->setType(rS); 0082 } 0083 if (position()%m_columns == 0) { 0084 m_interactionInfos[6]->setType(r); 0085 m_interactionInfos[0]->setType(rS); 0086 } 0087 if (position()%m_columns == (m_columns-1)) { 0088 m_interactionInfos[2]->setType(r); 0089 m_interactionInfos[4]->setType(rS); 0090 } 0091 0092 // If the ball is on a corner: 0093 const KBBScalableGraphicWidget::itemType n = KBBScalableGraphicWidget::interactionInfoNothing; 0094 if (position()==0) 0095 m_interactionInfos[0]->setType(n); 0096 if (position()==m_columns-1) 0097 m_interactionInfos[2]->setType(n); 0098 if (position()==m_rows*m_columns-1) 0099 m_interactionInfos[4]->setType(n); 0100 if (position()==(m_rows-1)*m_columns) 0101 m_interactionInfos[6]->setType(n); 0102 } 0103 0104 0105 0106 // 0107 // Private 0108 // 0109 0110 void KBBGraphicsItemBall::hoverEnterEvent (QGraphicsSceneHoverEvent*) 0111 { 0112 if (m_timer==nullptr) { 0113 m_timer = new QTimer(this); 0114 connect(m_timer, &QTimer::timeout, this, &KBBGraphicsItemBall::showInteractions); 0115 m_timer->start(TIME_TO_WAIT_BEFORE_SHOWING_INTERACTIONS); 0116 } 0117 0118 m_widget->hoverMovePosition(position()); 0119 } 0120 0121 0122 void KBBGraphicsItemBall::hoverLeaveEvent (QGraphicsSceneHoverEvent*) 0123 { 0124 delete m_timer; 0125 m_timer = nullptr; 0126 removeInteractionInfos(); 0127 } 0128 0129 0130 void KBBGraphicsItemBall::removeInteractionInfos() 0131 { 0132 for (int i=0; i<8;i++) { 0133 delete m_interactionInfos[i]; 0134 m_interactionInfos[i] = nullptr; 0135 } 0136 } 0137 0138 #include "moc_kbbgraphicsitemball.cpp"