File indexing completed on 2024-05-19 05:40:35

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "controller/item_controllers/sightcontroller.h"
0021 
0022 #include <QDebug>
0023 #include <QPolygonF>
0024 
0025 //#include "controller/item_controllers/characteritemcontrollermanager.h"
0026 #include "controller/view_controller/vectorialmapcontroller.h"
0027 
0028 namespace vmap
0029 {
0030 /////////////////////////////
0031 SightController::SightController(VectorialMapController* ctrl, QObject* parent)
0032     : VisualItemController(VisualItemController::SIGHT, std::map<QString, QVariant>(), ctrl, parent)
0033 {
0034     // constructor
0035     setUuid("sightController");
0036 
0037     connect(m_ctrl, &VectorialMapController::visibilityChanged, this,
0038             [this]() { setVisible(m_ctrl->visibility() == Core::VisibilityMode::FOGOFWAR); });
0039     connect(m_ctrl, &VectorialMapController::characterVisionChanged, this, &SightController::setCharacterSight);
0040 
0041     connect(m_ctrl, &VectorialMapController::visualRectChanged, this, &vmap::SightController::rectChanged);
0042     setVisible(m_ctrl->visibility() == Core::VisibilityMode::FOGOFWAR);
0043 
0044     connect(this, &SightController::characterSightChanged, this, [this] { setModified(); });
0045     connect(this, &SightController::fowPathChanged, this, [this] { setModified(); });
0046     connect(this, &SightController::rectChanged, this, [this] { setModified(); });
0047     connect(this, &SightController::characterCountChanged, this, [this] { setModified(); });
0048     setEditable(false);
0049     setInitialized(true);
0050 }
0051 
0052 void SightController::aboutToBeRemoved()
0053 {
0054     emit removeItem();
0055 }
0056 
0057 const QList<QPointer<CharacterVision>>& SightController::visionData() const
0058 {
0059     return m_visions;
0060 }
0061 
0062 void SightController::setCorner(const QPointF& move, int corner, Core::TransformType tt) {}
0063 
0064 void SightController::endGeometryChange() {}
0065 
0066 int SightController::fowItemCount() const
0067 {
0068     return static_cast<int>(m_fogSingularityList.size());
0069 }
0070 
0071 int SightController::characterCount() const
0072 {
0073     return m_visions.count();
0074 }
0075 
0076 bool SightController::characterSight() const
0077 {
0078     return m_characterSight;
0079 }
0080 
0081 QRectF SightController::rect() const
0082 {
0083     auto rect= m_ctrl->visualRect();
0084     rect= rect.united(m_rect);
0085     return rect;
0086 }
0087 
0088 void SightController::setRect(const QRectF& rect)
0089 {
0090     if(m_rect == rect)
0091         return;
0092 
0093     m_rect= rect;
0094     emit rectChanged(m_rect);
0095 }
0096 
0097 void SightController::setCharacterSight(bool b)
0098 {
0099     if(b == m_characterSight)
0100         return;
0101     m_characterSight= b;
0102     emit characterSightChanged();
0103 }
0104 
0105 void SightController::setFowPath(const QPainterPath &path)
0106 {
0107     if(m_remoteFowPath == path)
0108         return;
0109     m_remoteFowPath = path;
0110     emit fowPathChanged();
0111 }
0112 
0113 QPainterPath SightController::fowPath() const
0114 {
0115     QPainterPath path;
0116     if(remote())
0117     {
0118         path.addRect(rect());
0119         path = path.intersected(m_remoteFowPath);
0120         auto bRect = m_remoteFowPath.boundingRect();
0121         auto cRect = rect();
0122         if(cRect.height() > bRect.height())
0123         {
0124             QPainterPath subPoly;
0125             subPoly.addRect(QRectF(0,bRect.height(),cRect.width(),cRect.height()-bRect.height()));
0126             path = path.united(subPoly);
0127         }
0128 
0129         if(cRect.width() > bRect.width())
0130         {
0131             QPainterPath subPoly;
0132             subPoly.addRect(QRectF(bRect.width(),0,cRect.width()-bRect.width(),cRect.height()));
0133             path = path.united(subPoly);
0134         }
0135     }
0136     else
0137     {
0138         path.addRect(rect());
0139         for(auto& fogs : m_fogSingularityList)
0140         {
0141             QPainterPath subPoly;
0142             subPoly.addPolygon(fogs.first);
0143             path= fogs.second ? path.united(subPoly) : path.subtracted(subPoly);
0144         }
0145     }
0146     return path;
0147 }
0148 
0149 void SightController::addPolygon(const QPolygonF& poly, bool mask)
0150 {
0151     m_fogSingularityList.push_back(std::make_pair(poly, mask));
0152     emit fowPathChanged();
0153 }
0154 
0155 void SightController::addCharacterVision(CharacterVision* vision)
0156 {
0157     connect(vision, &CharacterVision::angleChanged, this, &SightController::requiredUpdate);
0158     connect(vision, &CharacterVision::radiusChanged, this, &SightController::requiredUpdate);
0159     connect(vision, &CharacterVision::positionChanged, this, &SightController::requiredUpdate);
0160     connect(vision, &CharacterVision::shapeChanged, this, &SightController::requiredUpdate);
0161     connect(vision, &CharacterVision::rotationChanged, this, &SightController::requiredUpdate);
0162     connect(vision, &CharacterVision::sideChanged, this, &SightController::requiredUpdate);
0163     connect(vision, &CharacterVision::removedChanged, this, &SightController::requiredUpdate);
0164     m_visions.push_back(vision);
0165     emit characterCountChanged();
0166 }
0167 
0168 void SightController::removeCharacterVision(CharacterVision* vision)
0169 {
0170     connect(vision, 0, this, 0);
0171     if(m_visions.removeOne(vision))
0172         emit characterCountChanged();
0173 }
0174 
0175 const std::vector<std::pair<QPolygonF, bool>>& SightController::singularityList() const
0176 {
0177     return m_fogSingularityList;
0178 }
0179 } // namespace vmap