Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. #include "puzzlepiece.h"
  2. #include<QGraphicsScene>
  3. #include<QGraphicsItem>
  4. #include<QVector>
  5. #include<QMessageBox>
  6.  
  7. PuzzlePiece::PuzzlePiece(ConnectorPosition north, ConnectorPosition east, ConnectorPosition south, ConnectorPosition west)
  8. {
  9.  
  10.  
  11. connector_position[North] = north;
  12. connector_position[South] = south;
  13. connector_position[West] = west;
  14. connector_position[East] = east;
  15.  
  16.  
  17. m_neighbours[0] = nullptr;
  18. m_neighbours[1] = nullptr;
  19. m_neighbours[2] = nullptr;
  20. m_neighbours[3] = nullptr;
  21. QPainterPath a;
  22. constructShape(a);
  23. setPath(a);
  24.  
  25.  
  26. }
  27.  
  28.  
  29. void PuzzlePiece::checkNeighbours(QVector<QPoint> &checked)
  30. {
  31. if(checked.contains(Coordinates()))
  32. return;
  33. checked.insert(checked.begin(),Coordinates());
  34. find_neighbour(West);
  35. find_neighbour(East);
  36. find_neighbour(North);
  37. find_neighbour(South);
  38.  
  39.  
  40. if (m_neighbours[0])
  41. m_neighbours[0]->checkNeighbours(checked);
  42. if (m_neighbours[1])
  43. m_neighbours[1]->checkNeighbours(checked);
  44. if (m_neighbours[2])
  45. m_neighbours[2]->checkNeighbours(checked);
  46. if (m_neighbours[3])
  47. m_neighbours[3]->checkNeighbours(checked);
  48. }
  49.  
  50.  
  51. void PuzzlePiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  52. {
  53. QVector<QPoint> check;
  54. checkNeighbours(check);
  55. if(check.count()==all)
  56. {
  57. QMessageBox::information(nullptr,"ВЫ ВЫИГРАЛИ","УРААА!!!");
  58. }
  59.  
  60. }
  61.  
  62. void PuzzlePiece::setPixmap(QPixmap new_piec)
  63. {
  64. piece=new_piec;
  65. update();
  66. }
  67.  
  68. void PuzzlePiece::setCoordinates(QPoint point)
  69. {
  70. coordinates.setX(point.x());
  71. coordinates.setY(point.y());
  72. }
  73.  
  74. QVariant PuzzlePiece::itemChange(QGraphicsItem::GraphicsItemChange temp, const QVariant &value)
  75. {
  76. if (temp==ItemPositionHasChanged)
  77. {
  78. QPoint newpoint=value.toPoint();
  79. if (m_neighbours[0])
  80. m_neighbours[0]->setPos(newpoint.x(),newpoint.y()-50);
  81. if (m_neighbours[1])
  82. m_neighbours[1]->setPos(newpoint.x(),newpoint.y()+50);
  83. if (m_neighbours[2])
  84. m_neighbours[2]->setPos(newpoint.x()-50,newpoint.y());
  85. if (m_neighbours[3])
  86. m_neighbours[3]->setPos(newpoint.x()+50,newpoint.y());
  87. }
  88. return QGraphicsItem::itemChange(temp, value);
  89. }
  90.  
  91. void PuzzlePiece::setNumber(QSize picture)
  92. {
  93. all=picture.width()*picture.height();
  94. }
  95.  
  96.  
  97. void PuzzlePiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  98. {
  99. painter->setClipPath(path());
  100. QRect rect=this->boundingRect().toRect();
  101. painter->drawPixmap(rect.x(),rect.y(),piece);
  102. painter->setPen(Qt::black);
  103. painter->drawPath(path());
  104.  
  105. }
  106.  
  107.  
  108.  
  109. void PuzzlePiece::link(PuzzlePiece *pointer, PuzzlePiece::Direction direction)
  110. {
  111.  
  112. switch(direction)
  113. {
  114. case South:
  115. pointer->m_neighbours[0]=this;
  116. this->m_neighbours[1]=pointer;
  117. break;
  118.  
  119. case North:
  120. pointer->m_neighbours[1]=this;
  121. this->m_neighbours[0]=pointer;
  122. break;
  123.  
  124. case East:
  125. pointer->m_neighbours[2]=this;
  126. this->m_neighbours[3]=pointer;
  127. break;
  128.  
  129. case West:
  130. pointer->m_neighbours[3]=this;
  131. this->m_neighbours[2]=pointer;
  132. break;
  133. }
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. QPoint PuzzlePiece::Coordinates()
  144. {
  145. return coordinates;
  146. }
  147.  
  148. QPixmap PuzzlePiece::getPiece()
  149. {
  150. return piece;
  151. }
  152.  
  153.  
  154.  
  155.  
  156. void PuzzlePiece::find_neighbour(PuzzlePiece::Direction direct)
  157. {
  158. PuzzlePiece* temp;
  159. if (direct==North && m_neighbours[0]) return;
  160. if (direct==South && m_neighbours[1]) return;
  161. if (direct==East && m_neighbours[2]) return;
  162. if (direct==West && m_neighbours[3]) return;
  163.  
  164. switch(direct)
  165. {
  166. case North:
  167. {
  168. temp=(PuzzlePiece*)(scene()->itemAt(pos().x(),pos().y()-50,QTransform()));
  169. if(temp&&((temp->coordinates.x()==coordinates.x())&&(temp->coordinates.y()==coordinates.y()-1)))
  170. {
  171. link(temp,direct);
  172. temp->setPos(pos().x(),pos().y()-50);
  173. }
  174. break;
  175. }
  176. case South:
  177. {
  178. temp=(PuzzlePiece*)(scene()->itemAt(pos().x(),pos().y()+50,QTransform()));
  179. if(temp&&((temp->coordinates.x()==coordinates.x())&&(temp->coordinates.y()==coordinates.y()+1)))
  180. {
  181. link(temp,direct);
  182. temp->setPos(pos().x(),pos().y()+50);
  183. }
  184. break;
  185. }
  186. case East:
  187. {
  188. temp=(PuzzlePiece*)scene()->itemAt(pos().x()+50,pos().y(),QTransform());
  189. if(temp&&((temp->coordinates.x()==coordinates.x()+1)&&(temp->coordinates.y()==coordinates.y())))
  190. {
  191. link(temp,direct);
  192. temp->setPos(pos().x()+50,pos().y());
  193. }
  194. break;
  195. }
  196. case West:
  197. {
  198. temp=(PuzzlePiece*)scene()->itemAt(pos().x()-50,pos().y(),QTransform());
  199. if(temp&&((temp->coordinates.x()==coordinates.x()-1)&&(temp->coordinates.y()==coordinates.y())))
  200. {
  201. link(temp,direct);
  202. temp->setPos(pos().x()-50,pos().y());
  203. }
  204. break;
  205. }
  206.  
  207. }
  208.  
  209. }
  210.  
  211. void PuzzlePiece::constructShape(QPainterPath &shape)
  212. {
  213.  
  214. shape.moveTo(size/2., size/2.);
  215. shape.lineTo(size/4.,size/2);
  216.  
  217. switch(connector_position[South])
  218. {
  219. case Out:
  220. shape.cubicTo(size/4.,3*size/4., -size/4., 3*size/4.,-size/4.,size/2.);
  221. break;
  222. case In:
  223. shape.cubicTo(size/4.,size/4., -size/4., size/4.,-size/4.,size/2.);
  224. break;
  225. case None:
  226. shape.lineTo(-size/4.,size/2);
  227. break;
  228.  
  229. }
  230.  
  231. shape.lineTo(-size/2.,size/2.);
  232. shape.lineTo(-size/2.,size/4.);
  233. switch(connector_position[West])
  234. {
  235. case Out:
  236. shape.cubicTo(-3*size/4.,size/4., -3*size/4., -size/4.,-size/2.,-size/4.);
  237. break;
  238. case In:
  239. shape.cubicTo(-size/4.,size/4.,-size/4., -size/4.,-size/2.,-size/4.);
  240. break;
  241. case None:
  242. shape.lineTo(-size/2.,-size/4.);
  243. break;
  244.  
  245. }
  246.  
  247. shape.lineTo(-size/2.,-size/2.);
  248. shape.lineTo(-size/4.,-size/2.);
  249. switch(connector_position[North])
  250. {
  251. case Out:
  252. shape.cubicTo(-size/4.,-3*size/4., size/4., -3*size/4.,size/4.,-size/2.);break;
  253. case In:
  254. shape.cubicTo(-size/4.,-size/4., size/4., -size/4.,size/4.,-size/2.);break;
  255. case None:
  256. shape.lineTo(size/4.,-size/2.);break;
  257.  
  258.  
  259. }
  260.  
  261. shape.lineTo(size/2,-size/2);
  262. shape.lineTo(size/2,-size/4);
  263. switch(connector_position[East])
  264. {
  265. case Out:
  266. shape.cubicTo(3*size/4.,-size/4., 3*size/4., size/4.,size/2.,size/4.);break;
  267. case In:
  268. shape.cubicTo(size/4.,-size/4., size/4., size/4.,size/2.,size/4.);break;
  269. case None:
  270. shape.lineTo(size/2.,size/4.);break;
  271.  
  272. }
  273.  
  274.  
  275. shape.lineTo(size/2.,size/2.);
  276. shape.closeSubpath();
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement