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

0001 #include "updater/vmapitem/sightupdater.h"
0002 
0003 #include "controller/item_controllers/sightcontroller.h"
0004 #include "network/networkmessagereader.h"
0005 
0006 #include <QDataStream>
0007 
0008 SightUpdater::SightUpdater(QObject* parent) : VMapItemControllerUpdater{parent} {}
0009 
0010 void SightUpdater::addItemController(vmap::VisualItemController* ctrl, bool sendOff)
0011 {
0012     if(nullptr == ctrl)
0013         return;
0014 
0015     Q_UNUSED(sendOff)
0016 
0017     auto sightCtrl= dynamic_cast<vmap::SightController*>(ctrl);
0018 
0019     if(nullptr == sightCtrl)
0020         return;
0021 
0022     VMapItemControllerUpdater::addItemController(sightCtrl);
0023 
0024     // connect(sightCtrl, &vmap::SightController::characterSightChanged, this,
0025     //         [this, sightCtrl]() { sendOffVMapChanges<bool>(sightCtrl, QStringLiteral("characterSight")); });
0026     connect(sightCtrl, &vmap::SightController::fowPathChanged, this,
0027             [this, sightCtrl]() { sendOffVMapChanges<QPainterPath>(sightCtrl, QStringLiteral("fowPath")); });
0028     connect(sightCtrl, &vmap::SightController::characterCountChanged, this,
0029             [this, sightCtrl]() { sendOffVMapChanges<int>(sightCtrl, QStringLiteral("characterCount")); });
0030 
0031     /*if(!ctrl->remote() && sendOff)
0032         connect(sightCtrl, &vmap::SightController::initializedChanged, this,
0033                 [sightCtrl]() { MessageHelper::sendOffLine(sightCtrl, sightCtrl->mapUuid()); });*/
0034 }
0035 
0036 bool SightUpdater::updateItemProperty(NetworkMessageReader* msg, vmap::VisualItemController* ctrl)
0037 {
0038     if(nullptr == msg || nullptr == ctrl)
0039         return false;
0040 
0041     // TODO implement save/restore
0042     auto datapos= msg->pos();
0043 
0044     if(VMapItemControllerUpdater::updateItemProperty(msg, ctrl))
0045         return true;
0046 
0047     msg->resetToPos(datapos);
0048 
0049     updatingCtrl= ctrl;
0050 
0051     auto property= msg->string16();
0052 
0053     QVariant var;
0054 
0055     if(property == QStringLiteral("characterSight"))
0056     {
0057         var= static_cast<bool>(msg->uint8());
0058     }
0059     else if(property == QStringLiteral("fowPath"))
0060     {
0061         auto data= msg->byteArray32();
0062         {
0063             QDataStream read(&data, QIODevice::ReadOnly);
0064             QPainterPath path;
0065             read >> path;
0066             var= QVariant::fromValue(path);
0067         }
0068     }
0069     else if(property == QStringLiteral("characterCount"))
0070     {
0071         var= QVariant::fromValue(msg->int64());
0072     }
0073     else
0074     {
0075         return false;
0076     }
0077 
0078     m_updatingFromNetwork= true;
0079     auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var);
0080     m_updatingFromNetwork= false;
0081     updatingCtrl= nullptr;
0082 
0083     return feedback;
0084 }