File indexing completed on 2022-11-29 18:38:54

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 
0011 #include "kbbgraphicsitemray.h"
0012 
0013 #include <QGraphicsScene>
0014 #include <QList>
0015 #include <QPainterPath>
0016 #include <QPen>
0017 
0018 
0019 #include "kbbballsonboard.h"
0020 #include "kbbgamedoc.h"
0021 #include "kbbthememanager.h"
0022 
0023 
0024 
0025 //
0026 // Constructor / Destructor
0027 //
0028 
0029 KBBGraphicsItemRay::KBBGraphicsItemRay(KBBScalableGraphicWidget::itemType itemType, QGraphicsScene* scene, KBBThemeManager* themeManager) : KBBGraphicsItemBorder(0, 1, 1, KBBScalableGraphicWidget::BORDER_SIZE/2), QGraphicsPathItem (nullptr)
0030 {
0031     scene->addItem(this);
0032     QPen pen;
0033 
0034     pen.setColor(themeManager->color(itemType));
0035     pen.setStyle(themeManager->style(itemType));
0036     pen.setWidthF(themeManager->width(itemType));
0037     setZValue(themeManager->zValue(itemType));
0038 
0039     pen.setJoinStyle(Qt::RoundJoin);
0040     pen.setCapStyle(Qt::RoundCap);
0041 
0042     setPen(pen);
0043 }
0044 
0045 
0046 
0047 //
0048 // Public
0049 //
0050 
0051 void KBBGraphicsItemRay::draw(KBBBallsOnBoard* ballsOnBoard, const int borderPosition)
0052 {
0053     const int columns = ballsOnBoard->columns();
0054     const int rows = ballsOnBoard->rows();
0055     
0056     QList<int> points;
0057     const int oppositeBorderPosition = ballsOnBoard->oppositeBorderPositionWithPoints(borderPosition, points);
0058     
0059     QPainterPath path;
0060     setSize(borderPosition, columns, rows);
0061     path.moveTo(m_centerX, m_centerY);
0062 
0063     const float b = (float) KBBScalableGraphicWidget::BORDER_SIZE;
0064     const float r = (float) KBBScalableGraphicWidget::RATIO;
0065     float x;
0066     float y;
0067     for (int i=0; i<points.count(); i++) {
0068         x = b - r/2 + r*((points[i] % columns) + 1);
0069         y = b - r/2 + r*((points[i] / columns) + 1);
0070         path.lineTo(x,y);
0071     }
0072     
0073     if (oppositeBorderPosition!=KBBGameDoc::HIT_POSITION) {
0074         float x1;
0075         float y1;
0076         centerCoordinate(oppositeBorderPosition, x1, y1, b/2.);
0077         path.lineTo(x1,y1);
0078     }
0079     
0080     setPath(path);
0081 }
0082 
0083 
0084 void KBBGraphicsItemRay::hide()
0085 {
0086     setPath(QPainterPath());
0087 }