File indexing completed on 2024-04-28 05:38:14

0001 /***************************************************************************
0002  *  Copyright (C) 2009 by Renaud Guezennec                                 *
0003  *   https://rolisteam.org/contact                    *
0004  *                                                                          *
0005  *   rolisteam 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 
0021 #include <QHBoxLayout>
0022 #include <QScrollArea>
0023 #include <QScrollBar>
0024 #include <QUuid>
0025 #include <QVBoxLayout>
0026 
0027 #include "vmapframe.h"
0028 
0029 #include "controller/view_controller/vectorialmapcontroller.h"
0030 
0031 QString visibilityText(Core::VisibilityMode vis)
0032 {
0033     QStringList visibilityData;
0034     visibilityData << QObject::tr("Hidden") << QObject::tr("Fog Of War") << QObject::tr("All visible");
0035 
0036     if(vis < visibilityData.size())
0037     {
0038         return visibilityData.at(vis);
0039     }
0040     return {};
0041 }
0042 
0043 QString permissionModeText(Core::PermissionMode mode)
0044 {
0045     QStringList permissionData;
0046     permissionData << QObject::tr("No Right") << QObject::tr("His character") << QObject::tr("All Permissions");
0047     return permissionData.at(mode);
0048 }
0049 
0050 QString layerText(Core::Layer mode)
0051 {
0052     QHash<Core::Layer, QString> data{{Core::Layer::GROUND, QObject::tr("Ground")},
0053                                      {Core::Layer::OBJECT, QObject::tr("Object")},
0054                                      {Core::Layer::CHARACTER_LAYER, QObject::tr("Character")},
0055                                      {Core::Layer::GAMEMASTER_LAYER, QObject::tr("GameMaster")},
0056                                      {Core::Layer::FOG, QObject::tr("Fog Layer")},
0057                                      {Core::Layer::GRIDLAYER, QObject::tr("Grid Layer")},
0058                                      {Core::Layer::NONE, QObject::tr("No Layer")}};
0059 
0060     auto res= data[mode];
0061     if(res.isEmpty())
0062         res= QObject::tr("No Layer");
0063 
0064     return res;
0065 }
0066 
0067 VMapFrame::VMapFrame(VectorialMapController* ctrl, QWidget* parent)
0068     : MediaContainer(ctrl, MediaContainer::ContainerType::VMapContainer, parent)
0069     , m_ctrl(ctrl)
0070     , m_vmap(new VMap(ctrl))
0071     , m_graphicView(new RGraphicsView(ctrl))
0072     , m_toolbox(new ToolBox(ctrl))
0073     , m_topBar(new VmapTopBar(ctrl))
0074 {
0075     setupUi();
0076 
0077     setObjectName("VMapFrame");
0078     setWindowIcon(QIcon::fromTheme("vmap"));
0079     m_graphicView->setScene(m_vmap.get());
0080 
0081     auto func= [this]()
0082     {
0083         setWindowTitle(tr("%1 - visibility: %2 - permission: %3 - layer: %4")
0084                            .arg(m_ctrl->name(), visibilityText(m_ctrl->visibility()),
0085                                 permissionModeText(m_ctrl->permission()), layerText(m_ctrl->layer())));
0086     };
0087 
0088     connect(m_ctrl, &VectorialMapController::nameChanged, this, func);
0089     connect(m_ctrl, &VectorialMapController::visibilityChanged, this, func);
0090     connect(m_ctrl, &VectorialMapController::layerChanged, this, func);
0091     connect(m_ctrl, &VectorialMapController::permissionChanged, this, func);
0092     if(m_ctrl)
0093         func();
0094 
0095     auto updateSmallImage= [this]()
0096     {
0097         if(!m_ctrl)
0098             return;
0099         auto w= std::min(m_toolbox->width() - 10, 100);
0100         auto sw= m_vmap->width() * m_ctrl->zoomLevel();
0101         auto sh= m_vmap->height() * m_ctrl->zoomLevel();
0102         auto h= static_cast<int>(w * sh / sw);
0103 
0104         QPixmap map{QSize{w, h}};
0105         auto visible= m_graphicView->viewport()->rect();
0106 
0107         visible.translate({static_cast<int>(m_graphicView->horizontalScrollBar()->value() - m_vmap->sceneRect().x()),
0108                            static_cast<int>(m_graphicView->verticalScrollBar()->value() - m_vmap->sceneRect().y())});
0109 
0110         auto ratioX= w / sw;
0111         auto ratioY= h / sh;
0112 
0113         {
0114             QPainter painter(&map);
0115             m_vmap->render(&painter); //, m_vmap->sceneRect(), map.rect()
0116 
0117             painter.setPen(Qt::blue);
0118             painter.drawRect(QRectF{visible.x() * ratioX, visible.y() * ratioY, visible.width() * ratioX,
0119                                     visible.height() * ratioY});
0120         }
0121 
0122         m_toolbox->setImage(map);
0123     };
0124 
0125     connect(m_vmap.get(), &VMap::changed, this, updateSmallImage);
0126 
0127     auto updateFrame= [this]()
0128     {
0129         auto rect= m_vmap->sceneRect();
0130         auto frame= m_graphicView->frameRect();
0131         auto poly= m_graphicView->mapToScene(frame).boundingRect();
0132 
0133         m_ctrl->setVisualRect(rect.united(poly));
0134     };
0135     connect(m_vmap.get(), &VMap::sceneRectChanged, m_ctrl, updateFrame);
0136     connect(m_graphicView.get(), &RGraphicsView::updateVisualZone, m_ctrl, updateFrame);
0137 
0138     connect(m_graphicView->horizontalScrollBar(), &QScrollBar::valueChanged, this, updateSmallImage);
0139     connect(m_graphicView->verticalScrollBar(), &QScrollBar::valueChanged, this, updateSmallImage);
0140     connect(m_ctrl, &VectorialMapController::zoomLevelChanged, this, updateSmallImage);
0141 }
0142 
0143 VMapFrame::~VMapFrame()= default;
0144 
0145 void VMapFrame::setupUi()
0146 {
0147     auto wid= new QWidget(this);
0148     auto layout= new QVBoxLayout(wid);
0149     layout->addWidget(m_topBar.get());
0150 
0151     auto horizontal= new QSplitter(Qt::Horizontal, this);
0152     layout->addWidget(horizontal);
0153 
0154     horizontal->addWidget(m_toolbox.get());
0155     horizontal->addWidget(m_graphicView.get());
0156     horizontal->setStretchFactor(0, 0);
0157     horizontal->setStretchFactor(1, 1);
0158 
0159     setWidget(wid);
0160 }
0161 
0162 bool VMapFrame::openFile(const QString& filepath)
0163 {
0164     if(filepath.isEmpty())
0165         return false;
0166 
0167     QFile input(filepath);
0168     if(!input.open(QIODevice::ReadOnly))
0169         return false;
0170     QDataStream in(&input);
0171     in.setVersion(QDataStream::Qt_5_7);
0172     return true;
0173 }
0174 
0175 void VMapFrame::keyPressEvent(QKeyEvent* event)
0176 {
0177     switch(event->key())
0178     {
0179     case Qt::Key_Escape:
0180         m_ctrl->setTool(Core::HANDLER);
0181         break;
0182     default:
0183         MediaContainer::keyPressEvent(event);
0184     }
0185 }
0186 
0187 VmapTopBar* VMapFrame::topBar() const
0188 {
0189     return m_topBar.get();
0190 }
0191 
0192 ToolBox* VMapFrame::toolBox() const
0193 {
0194     return m_toolbox.get();
0195 }
0196 
0197 VMap* VMapFrame::map() const
0198 {
0199     return m_vmap.get();
0200 }
0201 
0202 QPointF VMapFrame::mapFromScene(const QPointF& point)
0203 {
0204     return m_graphicView->mapToGlobal(point);
0205 }