1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 1999-2000, Robert Cimrman * |
8 | * cimrman3@students.zcu.cz * |
9 | * * |
10 | * Copyright (c) 2007, Nicolas Roffet * |
11 | * nicolas-kde@roffet.com * |
12 | * * |
13 | * * |
14 | * This program is free software; you can redistribute it and/or modify * |
15 | * it under the terms of the GNU General Public License as published by * |
16 | * the Free Software Foundation; either version 2 of the License, or * |
17 | * (at your option) any later version. * |
18 | * * |
19 | * This program is distributed in the hope that it will be useful, * |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 | * GNU General Public License for more details. * |
23 | * * |
24 | * You should have received a copy of the GNU General Public License * |
25 | * along with this program; if not, write to the * |
26 | * Free Software Foundation, Inc., * |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
28 | ***************************************************************************/ |
29 | |
30 | |
31 | #include "kbbgraphicsitemray.h" |
32 | |
33 | #include <QGraphicsScene> |
34 | #include <QList> |
35 | #include <QPainterPath> |
36 | #include <QPen> |
37 | |
38 | |
39 | #include "kbbballsonboard.h" |
40 | #include "kbbgamedoc.h" |
41 | #include "kbbgraphicsitemborder.h" |
42 | #include "kbbscalablegraphicwidget.h" |
43 | #include "kbbthememanager.h" |
44 | |
45 | |
46 | |
47 | // |
48 | // Constructor / Destructor |
49 | // |
50 | |
51 | KBBGraphicsItemRay::KBBGraphicsItemRay(KBBScalableGraphicWidget::itemType itemType, QGraphicsScene* scene, KBBThemeManager* themeManager) : KBBGraphicsItemBorder(0, 1, 1, KBBScalableGraphicWidget::BORDER_SIZE/2), QGraphicsPathItem (0, scene) |
52 | { |
53 | QPen pen; |
54 | |
55 | pen.setColor(themeManager->color(itemType)); |
56 | pen.setStyle(themeManager->style(itemType)); |
57 | pen.setWidthF(themeManager->width(itemType)); |
58 | setZValue(themeManager->zValue(itemType)); |
59 | |
60 | pen.setJoinStyle(Qt::RoundJoin); |
61 | pen.setCapStyle(Qt::RoundCap); |
62 | |
63 | setPen(pen); |
64 | } |
65 | |
66 | |
67 | |
68 | // |
69 | // Public |
70 | // |
71 | |
72 | void KBBGraphicsItemRay::draw(KBBBallsOnBoard* ballsOnBoard, const int borderPosition) |
73 | { |
74 | const int columns = ballsOnBoard->columns(); |
75 | const int rows = ballsOnBoard->rows(); |
76 | |
77 | QList<int> points; |
78 | const int oppositeBorderPosition = ballsOnBoard->oppositeBorderPositionWithPoints(borderPosition, points); |
79 | |
80 | QPainterPath path; |
81 | setSize(borderPosition, columns, rows); |
82 | path.moveTo(m_centerX, m_centerY); |
83 | |
84 | const float b = (float) KBBScalableGraphicWidget::BORDER_SIZE; |
85 | const float r = (float) KBBScalableGraphicWidget::RATIO; |
86 | float x; |
87 | float y; |
88 | for (int i=0; i<points.count(); i++) { |
89 | x = b - r/2 + r*((points[i] % columns) + 1); |
90 | y = b - r/2 + r*((points[i] / columns) + 1); |
91 | path.lineTo(x,y); |
92 | } |
93 | |
94 | if (oppositeBorderPosition!=KBBGameDoc::HIT_POSITION) { |
95 | float x1; |
96 | float y1; |
97 | centerCoordinate(oppositeBorderPosition, x1, y1, b/2.); |
98 | path.lineTo(x1,y1); |
99 | } |
100 | |
101 | setPath(path); |
102 | } |
103 | |
104 | |
105 | void KBBGraphicsItemRay::hide() |
106 | { |
107 | setPath(QPainterPath()); |
108 | } |
109 | |