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

0001 /***************************************************************************
0002  *  Copyright (C) 2020 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 "updater/vmapitem/vmapitemcontrollerupdater.h"
0021 
0022 #include <QFont>
0023 #include <QSet>
0024 
0025 #include "data/charactervision.h"
0026 #include "network/networkmessagereader.h"
0027 
0028 VMapItemControllerUpdater::VMapItemControllerUpdater(QObject* parent)
0029 {
0030     // Force template generation
0031     /*sendOffVMapChanges<QRectF>(nullptr, "");
0032     sendOffVMapChanges<quint16>(nullptr, "");
0033     sendOffVMapChanges<QByteArray>(nullptr, "");
0034     sendOffVMapChanges<QFont>(nullptr, "");
0035     sendOffVMapChanges<QString>(nullptr, "");
0036     sendOffVMapChanges<std::vector<QPointF>>(nullptr, "");
0037     sendOffVMapChanges<CharacterVision::SHAPE>(nullptr, "");*/
0038 }
0039 
0040 void VMapItemControllerUpdater::addItemController(vmap::VisualItemController* ctrl, bool sendOff)
0041 {
0042     if(nullptr == ctrl)
0043         return;
0044 
0045     Q_UNUSED(sendOff)
0046 
0047     connect(ctrl, &vmap::VisualItemController::visibleChanged, this,
0048             [this, ctrl]() { sendOffVMapChanges<bool>(ctrl, QStringLiteral("visible")); });
0049     connect(ctrl, &vmap::VisualItemController::opacityChanged, this,
0050             [this, ctrl]() { sendOffVMapChanges<qreal>(ctrl, QStringLiteral("opacity")); });
0051     connect(ctrl, &vmap::VisualItemController::rotationEditFinished, this,
0052             [this, ctrl]() { sendOffVMapChanges<qreal>(ctrl, QStringLiteral("rotation")); });
0053     connect(ctrl, &vmap::VisualItemController::layerChanged, this,
0054             [this, ctrl]() { sendOffVMapChanges<Core::Layer>(ctrl, QStringLiteral("layer")); });
0055     connect(ctrl, &vmap::VisualItemController::posEditFinished, this,
0056             [this, ctrl]() { sendOffVMapChanges<QPointF>(ctrl, QStringLiteral("pos")); });
0057     connect(ctrl, &vmap::VisualItemController::colorChanged, this,
0058             [this, ctrl]() { sendOffVMapChanges<QColor>(ctrl, QStringLiteral("color")); });
0059     connect(ctrl, &vmap::VisualItemController::lockedChanged, this,
0060             [this, ctrl]() { sendOffVMapChanges<bool>(ctrl, QStringLiteral("locked")); });
0061     connect(ctrl, &vmap::VisualItemController::parentUuidChanged, this,
0062             [this, ctrl]() { sendOffVMapChanges<QString>(ctrl, QStringLiteral("parentUuid")); });
0063 }
0064 
0065 bool VMapItemControllerUpdater::updateItemProperty(NetworkMessageReader* msg, vmap::VisualItemController* ctrl)
0066 {
0067     if(nullptr == msg || nullptr == ctrl)
0068         return false;
0069 
0070     updatingCtrl= ctrl;
0071 
0072     auto property= msg->string16();
0073 
0074     QVariant var;
0075     QSet<QString> boolProperties({QString("visible"), QString("locked")});
0076     QSet<QString> colorProperties({QString("color")});
0077     QSet<QString> uint8Properties({QString("layer")});
0078     QSet<QString> pointProperties({QString("pos")});
0079     QSet<QString> realProperties({QString("opacity"), QString("rotation")});
0080 
0081     if(boolProperties.contains(property))
0082     {
0083         var= QVariant::fromValue(static_cast<bool>(msg->uint8()));
0084     }
0085     else if(colorProperties.contains(property))
0086     {
0087         var= QVariant::fromValue(QColor(msg->rgb()));
0088     }
0089     else if(pointProperties.contains(property))
0090     {
0091         auto x= msg->real();
0092         auto y= msg->real();
0093         var= QVariant::fromValue(QPointF(x, y));
0094     }
0095     else if(realProperties.contains(property))
0096     {
0097         var= QVariant::fromValue(msg->real());
0098     }
0099     else if(uint8Properties.contains(property))
0100     {
0101         auto value= msg->uint8();
0102         if(property == QStringLiteral("layer"))
0103         {
0104             var= QVariant::fromValue(static_cast<Core::Layer>(value));
0105         }
0106     }
0107     else
0108     {
0109         auto val= msg->string32();
0110         var= QVariant::fromValue(val);
0111     }
0112 
0113     m_updatingFromNetwork= true;
0114     auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var);
0115     m_updatingFromNetwork= false;
0116     updatingCtrl= nullptr;
0117 
0118     return feedback;
0119 }
0120 
0121 bool VMapItemControllerUpdater::synchronized() const
0122 {
0123     return m_synchronized;
0124 }
0125 
0126 void VMapItemControllerUpdater::setSynchronized(bool b)
0127 {
0128     if(b == m_synchronized)
0129         return;
0130     m_synchronized= b;
0131     emit synchronizedChanged(m_synchronized);
0132 }