• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

ksquares

  • sources
  • kde-4.14
  • kdegames
  • ksquares
  • src
gameboardscene.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Matthew Williams <matt@milliams.com> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  ***************************************************************************/
9 
10 #include "gameboardscene.h"
11 
12 #include "highlightanimation.h"
13 
14 #include <math.h>
15 
16 #include <QGraphicsSceneMouseEvent>
17 #include <QGraphicsEllipseItem>
18 
19 #include <kdebug.h>
20 
21 GameBoardScene::GameBoardScene(int newWidth, int newHeight, QObject *parent) : QGraphicsScene(parent), width(newWidth), height(newHeight), acceptEvents(true)
22 {
23  //kDebug() << "GameBoardScene::GameBoardScene()";
24 
25  for(int i = 0; i < (2*width*height + width + height); i++)
26  {
27  lineList.append(false); //simply fill array with 'false's
28  }
29 
30  spacing = 40; //this hard coding doesn't matter since auto-resizing exists :)
31  for(int iWidth = 0; iWidth <= width; iWidth++)
32  {
33  for(int iHeight = 0; iHeight <= height; iHeight++)
34  {
35  int x = iWidth*spacing;
36  int y = iHeight*spacing;
37  QGraphicsEllipseItem *dot = new QGraphicsEllipseItem(QRectF(-2,-2,4,4));
38  dot->moveBy(x,y);
39  dot->setBrush(Qt::SolidPattern);
40  dot->setZValue(20); // set the elevation, the dot's are on top
41  addItem(dot);
42  }
43  }
44  QPen feintPen(Qt::DotLine); //for the guidelines between dots
45  feintPen.setWidth(1);
46  feintPen.setColor(Qt::lightGray);
47  for(int iWidth = 0; iWidth <= width; iWidth++)
48  {
49  addLine(QLineF(spacing*iWidth, 0, spacing*iWidth, spacing*height), feintPen);
50  }
51  for(int iHeight = 0; iHeight <= height; iHeight++)
52  {
53  addLine(QLineF(0, spacing*iHeight, spacing*width, spacing*iHeight), feintPen);
54  }
55 
56  setBackgroundBrush(QBrush(Qt::white));
57 
58  indicatorLine = new QGraphicsLineItem(1,1,1,1);
59  indicatorLine->setZValue(10);
60  indicatorLine->setPen(QPen(Qt::yellow, 2.5));
61  indicatorLine->hide();
62  addItem(indicatorLine);
63 
64  QGraphicsEllipseItem tempItem;
65  QGraphicsEllipseItemType = tempItem.type();
66 
67  qreal border = 10;
68  QRectF rect = sceneRect();
69  rect.setLeft(rect.left() - border);
70  rect.setRight(rect.right() + border);
71  rect.setTop(rect.top() - border);
72  rect.setBottom(rect.bottom() + border);
73  setSceneRect(rect);
74 }
75 
76 GameBoardScene::~GameBoardScene()
77 {
78  kDebug() << "GameBoardScene::~GameBoardScene()";
79  delete indicatorLine;
80 }
81 
82 void GameBoardScene::drawLine(int index, const QColor &colour)
83 {
84  QGraphicsLineItem* line = new QGraphicsLineItem(lineFromIndex(index));
85  line->setZValue(10);
86  line->setPen(QPen(QBrush(colour), 2.5));
87  addItem(line); //draw new line
88  lineList[index] = true; //keep this table in sync
89  indicatorLine->hide();
90  update(line->boundingRect());
91 }
92 
93 void GameBoardScene::highlightLine(int index)
94 {
95  HighlightAnimation* anim = new HighlightAnimation(lineFromIndex(index));
96  anim->setZValue(9);
97  addItem(anim);
98 }
99 
100 void GameBoardScene::drawSquare(int index, const QColor &colour)
101 {
102  QBrush brush(colour, Qt::SolidPattern);
103 
104  addRect(QRectF(qreal((index%width)*spacing), qreal((index/width)*spacing), qreal(spacing), qreal(spacing)), QPen(), brush)->setZValue(-1);
105 }
106 
107 int GameBoardScene::indexFromPointPair(const QList<QGraphicsEllipseItem*> &pointPair) const
108 {
109 
110  if (pointPair.size() != 2)
111  return -1; // if it isn't a pair
112 
113  qreal pointOneX = pointPair.at(0)->scenePos().x()/spacing;
114  qreal pointOneY = pointPair.at(0)->scenePos().y()/spacing;
115  qreal pointTwoX = pointPair.at(1)->scenePos().x()/spacing;
116  qreal pointTwoY = pointPair.at(1)->scenePos().y()/spacing;
117 
118  //this int conversion could go bad but SHOULD be safe
119  qreal refX; // these two will be the grid-coord of the
120  qreal refY; // to and left most point of the two
121 
122  int index = -1;
123  if (pointOneX == pointTwoX)
124  {
125  //dir = VERTICAL;
126  refX = pointOneX;
127  if (pointTwoY < pointOneY) //want the topmost point as reference
128  refY = pointTwoY;
129  else
130  refY = pointOneY;
131  index = static_cast<int>(refY * ((2*width)+1) + refX + width);
132  }
133 
134  else if (pointOneY == pointTwoY)
135  {
136  //dir = HORIZONTAL;
137  refY = pointOneY;
138  if (pointOneX < pointTwoX) //want the leftmost point as reference
139  refX = pointOneX;
140  else
141  refX = pointTwoX;
142  index = static_cast<int>(refY * ((2*width)+1) + refX);
143  }
144  return index;
145 }
146 
147 QLineF GameBoardScene::lineFromIndex(int index) const
148 {
149  int index2 = index % ((2*width) + 1);
150  enum{HORIZONTAL, VERTICAL} dir;
151  if(index2 < width)
152  dir = HORIZONTAL;
153  else
154  dir = VERTICAL;
155 
156  int yCoordStart = (index / ((2*width) + 1)) * spacing;
157  int xCoordStart = 0;
158  int yCoordEnd = 0;
159  int xCoordEnd = 0;
160  switch(dir)
161  {
162  case HORIZONTAL:
163  xCoordStart = index2 * spacing;
164  yCoordEnd = yCoordStart;
165  xCoordEnd = xCoordStart + spacing;
166  break;
167  case VERTICAL:
168  xCoordStart = (index2 - width) * spacing;
169  yCoordEnd = yCoordStart + spacing;
170  xCoordEnd = xCoordStart;
171  break;
172  }
173  return QLineF(xCoordStart, yCoordStart, xCoordEnd, yCoordEnd);
174 }
175 
176 bool GameBoardScene::isLineAlready(const QList<QGraphicsEllipseItem*> &pointPair) const //TODO does this work?
177 {
178  int index = indexFromPointPair(pointPair);
179  if (index == -1)
180  return true;
181 
182  return lineList.at(index);
183 }
184 
185 void GameBoardScene::addLineToIndex(const QList<QGraphicsEllipseItem*> &pointPair)
186 {
187  int index = indexFromPointPair(pointPair);
188  if (index == -1) //not a valid line since no two unique ends
189  return;
190 
191  emit lineDrawn(index); //addLineToIndex(index);
192 }
193 
194 QList<QGraphicsEllipseItem*> GameBoardScene::getTwoNearestPoints(const QPointF &pos) const
195 {
196  QList<QGraphicsItem*> itemList = items();
197  QList<QGraphicsEllipseItem*> connectList;
198  for (int i = 0; i < itemList.size(); ++i)
199  {
200  if (itemList.at(i)->type() == QGraphicsEllipseItemType)
201  {
202  //cout << "itemList.at(i)->scenePos():" << qgraphicsitem_cast<QGraphicsEllipseItem*>(itemList.at(i))->scenePos().x() << "," << qgraphicsitem_cast<QGraphicsEllipseItem*>(itemList.at(i))->scenePos().y() << endl;
203  QPointF dist(pos - itemList.at(i)->scenePos());
204  qreal distMod = sqrt(dist.x()*dist.x() + dist.y()*dist.y());
205  //if (distMod < (spacing*0.7071)) //there will only ever be either 1 or 2 items that fulfil this [0.7071 ~ 2^(-0.5)]
206  if (distMod < spacing-5)
207  {
208  connectList << qgraphicsitem_cast<QGraphicsEllipseItem*>(itemList.at(i));
209  }
210  }
211  }
212  return connectList;
213 }
214 
215 const QSize GameBoardScene::minimumSizeHint() const
216 {
217  return QSize((width*spacing)+10, (height*spacing)+10); // the +10 is to provide padding and to avoid scrollbars
218 }
219 
220 /*void GameBoardScene::mousePressEvent (QGraphicsSceneMouseEvent* mouseEvent)
221 {
222  if (!acceptEvents) return;
223  QGraphicsScene::mousePressEvent(mouseEvent);
224 }*/
225 
226 void GameBoardScene::mouseReleaseEvent (QGraphicsSceneMouseEvent* mouseEvent)
227 {
228  if (!acceptEvents)
229  {
230  QGraphicsScene::mouseReleaseEvent(mouseEvent);
231  return;
232  }
233 
234  //cout << "GameBoardScene::mouseReleaseEvent" << endl;
235  if (mouseEvent->button() == Qt::LeftButton)
236  {
237  QList<QGraphicsEllipseItem*> connectList = getTwoNearestPoints(mouseEvent->scenePos());
238  if (connectList.size() == 2)
239  {
240  addLineToIndex(connectList);
241  }
242  }
243 
244  QGraphicsScene::mouseReleaseEvent(mouseEvent);
245 }
246 
247 void GameBoardScene::acknowledgeMove(int x1, int y1, int x2, int y2)
248 {
249  QPoint calculatedpos(((x1 + x2) / 2.0) * spacing, ((y1 + y2) / 2.0) * spacing);
250  QList<QGraphicsEllipseItem*> connectList = getTwoNearestPoints(calculatedpos);
251  addLineToIndex(connectList);
252 }
253 
254 void GameBoardScene::mouseMoveEvent (QGraphicsSceneMouseEvent* mouseEvent)
255 {
256  if (!acceptEvents) return;
257  //indicatorLine = 0;
258 
259  //kDebug() << "GameBoardScene::mouseMoveEvent";
260  //kDebug() << "mouseEvent->scenePos():" << mouseEvent->scenePos().x() << "," << mouseEvent->scenePos().y();
261 
262  QList<QGraphicsEllipseItem*> connectList = getTwoNearestPoints(mouseEvent->scenePos());
263 
264  if (connectList.size() == 2 && !isLineAlready(connectList))
265  { //if there are two nearest points and there isn't already a line
266  indicatorLine->setLine(QLineF(connectList.at(0)->scenePos(), connectList.at(1)->scenePos())); //where
267  indicatorLine->show();
268  }
269  else
270  {
271  indicatorLine->hide();
272  }
273 
274  QGraphicsScene::mouseMoveEvent(mouseEvent);
275 }
276 
277 #include "gameboardscene.moc"
GameBoardScene::acknowledgeMove
void acknowledgeMove(int x1, int y1, int x2, int y2)
Definition: gameboardscene.cpp:247
GameBoardScene::spacing
int spacing
Pixel spacing for standard zoom.
Definition: gameboardscene.h:119
QGraphicsEllipseItem::type
virtual int type() const
QGraphicsLineItem::boundingRect
virtual QRectF boundingRect() const
QGraphicsScene
HighlightAnimation
Highlight animation when new lines appear.
Definition: highlightanimation.h:23
highlightanimation.h
QGraphicsItem::moveBy
void moveBy(qreal dx, qreal dy)
QGraphicsScene::items
QList< QGraphicsItem * > items() const
QRectF::setRight
void setRight(qreal x)
GameBoardScene::lineDrawn
void lineDrawn(int)
Emits the index of the closet (undrawn) line when a click is detected.
GameBoardScene::highlightLine
void highlightLine(int index)
Draw a temporary halo behind a line.
Definition: gameboardscene.cpp:93
QGraphicsScene::addRect
QGraphicsRectItem * addRect(const QRectF &rect, const QPen &pen, const QBrush &brush)
QList::at
const T & at(int i) const
QGraphicsScene::setBackgroundBrush
void setBackgroundBrush(const QBrush &brush)
QGraphicsScene::sceneRect
QRectF sceneRect() const
QRectF::top
qreal top() const
KSquares::VERTICAL
Definition: aicontroller.h:15
QBrush
QGraphicsSceneMouseEvent::scenePos
QPointF scenePos() const
QGraphicsItem::hide
void hide()
QPoint
GameBoardScene::QGraphicsEllipseItemType
int QGraphicsEllipseItemType
QGraphicsEllipseItem::type()
Definition: gameboardscene.h:113
GameBoardScene::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: gameboardscene.cpp:254
QGraphicsScene::height
qreal height() const
QRectF::left
qreal left() const
QRectF::setLeft
void setLeft(qreal x)
QList::size
int size() const
QPointF
GameBoardScene::indicatorLine
QGraphicsLineItem * indicatorLine
Moves to show where the next line will be drawn.
Definition: gameboardscene.h:107
QGraphicsScene::mouseReleaseEvent
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
KSquares::HORIZONTAL
Definition: aicontroller.h:15
QRectF::bottom
qreal bottom() const
QList::append
void append(const T &value)
GameBoardScene::lineList
QList< bool > lineList
A local list of lines (non-canon)
Definition: gameboardscene.h:110
GameBoardScene::minimumSizeHint
const QSize minimumSizeHint() const
The smallest the view can be when 'auto-zoom' is off.
Definition: gameboardscene.cpp:215
QObject
QGraphicsLineItem
QGraphicsSceneMouseEvent
QGraphicsScene::mouseMoveEvent
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
QGraphicsLineItem::setLine
void setLine(const QLineF &line)
QGraphicsSceneMouseEvent::button
Qt::MouseButton button() const
QList
QColor
QPen::setColor
void setColor(const QColor &color)
QLineF
GameBoardScene::drawLine
void drawLine(int index, const QColor &colour)
Add the line to the scene so it shows up in the view.
Definition: gameboardscene.cpp:82
QGraphicsLineItem::setPen
void setPen(const QPen &pen)
QRectF::right
qreal right() const
QSize
GameBoardScene::indexFromPointPair
int indexFromPointPair(const QList< QGraphicsEllipseItem * > &pointPair) const
Takes a pair of QGraphicsEllipseItems and finds the index that relates to the line that's between the...
Definition: gameboardscene.cpp:107
GameBoardScene::addLineToIndex
void addLineToIndex(const QList< QGraphicsEllipseItem * > &pointPair)
Adds the line to the index for a specified pair of points.
Definition: gameboardscene.cpp:185
QGraphicsScene::addLine
QGraphicsLineItem * addLine(const QLineF &line, const QPen &pen)
GameBoardScene::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: gameboardscene.cpp:226
QPen::setWidth
void setWidth(int width)
QRectF::setBottom
void setBottom(qreal y)
QRectF
QRectF::setTop
void setTop(qreal y)
GameBoardScene::GameBoardScene
GameBoardScene(int newWidth, int newHeight, QObject *parent=0)
Create a new gameboard scene with the appropriate size.
Definition: gameboardscene.cpp:21
GameBoardScene::getTwoNearestPoints
QList< QGraphicsEllipseItem * > getTwoNearestPoints(const QPointF &pos) const
Given a single location in the scene, gives the two nearest QGraphicsEllipseItem. ...
Definition: gameboardscene.cpp:194
QGraphicsScene::update
void update(qreal x, qreal y, qreal w, qreal h)
GameBoardScene::~GameBoardScene
~GameBoardScene()
Destructor.
Definition: gameboardscene.cpp:76
QPen
GameBoardScene::lineFromIndex
QLineF lineFromIndex(int index) const
Takes a line-index and returns a QLineF located at that position.
Definition: gameboardscene.cpp:147
QAbstractGraphicsShapeItem::setBrush
void setBrush(const QBrush &brush)
QGraphicsItem::show
void show()
GameBoardScene::isLineAlready
bool isLineAlready(const QList< QGraphicsEllipseItem * > &pointPair) const
Given a pair of points, returns whether there is already a line there.
Definition: gameboardscene.cpp:176
GameBoardScene::drawSquare
void drawSquare(int index, const QColor &colour)
Fill a box to show it is owned be a particular player.
Definition: gameboardscene.cpp:100
QGraphicsScene::addItem
void addItem(QGraphicsItem *item)
QGraphicsEllipseItem
QGraphicsItem::setZValue
void setZValue(qreal z)
GameBoardScene::acceptEvents
bool acceptEvents
This property holds whether mouse events are enabled for this widget.
Definition: gameboardscene.h:121
gameboardscene.h
QGraphicsScene::width
qreal width() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ksquares

Skip menu "ksquares"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal