| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D 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 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "targetnode.hpp" |
| 17 | #include "tracktile.hpp" |
| 18 | |
| 19 | #include <QGraphicsLineItem> |
| 20 | #include <QPainter> |
| 21 | #include <QPen> |
| 22 | |
| 23 | #include <cassert> |
| 24 | #include <memory> |
| 25 | |
| 26 | static const int LINE_WIDTH = 15; |
| 27 | static const QColor ROUTE_LINE_COLOR(0, 0, 255, 128); |
| 28 | static const QColor FIRST_NODE_COLOR(255, 255, 255, 128); |
| 29 | static const QColor NODE_COLOR(ROUTE_LINE_COLOR); |
| 30 | |
| 31 | TargetNode::TargetNode() |
| 32 | : m_routeLine(nullptr) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | TargetNode::TargetNode(const TargetNode & other) |
| 37 | : QGraphicsItem() |
| 38 | , TargetNodeBase() |
| 39 | , m_routeLine(nullptr) |
| 40 | { |
| 41 | setIndex(other.index()); |
| 42 | |
| 43 | setLocation(other.location()); |
| 44 | |
| 45 | setSize(other.size()); |
| 46 | } |
| 47 | |
| 48 | void TargetNode::setRouteLine(QGraphicsLineItem * routeLine) |
| 49 | { |
| 50 | m_routeLine = routeLine; |
| 51 | m_routeLine->setPen( |
| 52 | QPen(QBrush(ROUTE_LINE_COLOR), LINE_WIDTH, Qt::DashDotDotLine, Qt::RoundCap)); |
| 53 | } |
| 54 | |
| 55 | QGraphicsLineItem * TargetNode::routeLine() const |
| 56 | { |
| 57 | return m_routeLine; |
| 58 | } |
| 59 | |
| 60 | QRectF TargetNode::boundingRect() const |
| 61 | { |
| 62 | return QRectF( |
| 63 | -size().width() / 2 - LINE_WIDTH, -size().height() / 2 - LINE_WIDTH, |
| 64 | size().width() + LINE_WIDTH, size().height() + LINE_WIDTH); |
| 65 | } |
| 66 | |
| 67 | void TargetNode::paint(QPainter * painter, |
| 68 | const QStyleOptionGraphicsItem * option, QWidget * widget) |
| 69 | { |
| 70 | Q_UNUSED(widget); |
| 71 | Q_UNUSED(option); |
| 72 | |
| 73 | painter->save(); |
| 74 | |
| 75 | if (index() == 0) |
| 76 | { |
| 77 | painter->setPen(QPen(QBrush(FIRST_NODE_COLOR), LINE_WIDTH)); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | painter->setPen(QPen(QBrush(NODE_COLOR), LINE_WIDTH)); |
| 82 | } |
| 83 | |
| 84 | // Outline |
| 85 | painter->drawRect( |
| 86 | boundingRect().x() + LINE_WIDTH, boundingRect().y() + LINE_WIDTH, |
| 87 | boundingRect().width() - LINE_WIDTH * 2, boundingRect().height() - LINE_WIDTH * 2); |
| 88 | |
| 89 | // Circle in the middle |
| 90 | painter->drawEllipse( |
| 91 | QPointF( |
| 92 | boundingRect().x() + boundingRect().width() / 2 + LINE_WIDTH / 2, |
| 93 | boundingRect().y() + boundingRect().height() / 2 + LINE_WIDTH / 2), |
| 94 | LINE_WIDTH, |
| 95 | LINE_WIDTH); |
| 96 | |
| 97 | painter->restore(); |
| 98 | } |
| 99 | |
| 100 | void TargetNode::setLocation(QPointF newLocation) |
| 101 | { |
| 102 | TargetNodeBase::setLocation(newLocation); |
| 103 | setPos(newLocation); |
| 104 | |
| 105 | updateRouteLine(); |
| 106 | |
| 107 | if (auto prevPtr = std::dynamic_pointer_cast<TargetNode>(prev())) |
| 108 | { |
| 109 | prevPtr->updateRouteLine(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void TargetNode::updateRouteLine() |
| 114 | { |
| 115 | if (m_routeLine && next()) |
| 116 | { |
| 117 | m_routeLine->setLine( |
| 118 | location().x(), |
| 119 | location().y(), |
| 120 | next()->location().x(), |
| 121 | next()->location().y()); |
| 122 | } |
| 123 | } |