1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00

Merge branch 'cpp-map-editor' into Actions

This commit is contained in:
Nordsoft91 2022-09-05 21:04:38 +04:00 committed by GitHub
commit 4793ed9679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 409 additions and 75 deletions

View File

@ -46,7 +46,7 @@ void TownPlacer::init()
void TownPlacer::placeTowns(ObjectManager & manager)
{
if((zone.getType() == ETemplateZoneType::CPU_START) || (zone.getType() == ETemplateZoneType::PLAYER_START))
if(zone.getOwner() && ((zone.getType() == ETemplateZoneType::CPU_START) || (zone.getType() == ETemplateZoneType::PLAYER_START)))
{
//set zone types to player faction, generate main town
logGlobal->info("Preparing playing zone");

View File

@ -120,8 +120,9 @@ MainWindow::MainWindow(QWidget *parent) :
ui->mapView->setController(&controller);
connect(ui->mapView, &MapView::openObjectProperties, this, &MainWindow::loadInspector);
sceneMini = new QGraphicsScene(this);
ui->minimapView->setScene(sceneMini);
ui->minimapView->setScene(controller.miniScene(0));
ui->minimapView->setController(&controller);
connect(ui->minimapView, &MinimapView::cameraPositionChanged, ui->mapView, &MapView::cameraChanged);
scenePreview = new QGraphicsScene(this);
ui->objectPreview->setScene(scenePreview);
@ -176,6 +177,8 @@ void MainWindow::initializeMap(bool isNew)
mapLevel = 0;
ui->mapView->setScene(controller.scene(mapLevel));
ui->minimapView->setScene(controller.miniScene(mapLevel));
ui->minimapView->dimensions();
setStatusMessage(QString("Scene objects: %1").arg(ui->mapView->scene()->items().size()));
@ -227,7 +230,7 @@ void MainWindow::saveMap()
CMapService mapService;
try
{
mapService.saveMap(std::unique_ptr<CMap>(controller.map()), filename.toStdString());
mapService.saveMap(controller.getMapUniquePtr(), filename.toStdString());
}
catch(const std::exception & e)
{
@ -551,6 +554,7 @@ void MainWindow::on_actionLevel_triggered()
{
mapLevel = mapLevel ? 0 : 1;
ui->mapView->setScene(controller.scene(mapLevel));
ui->minimapView->setScene(controller.miniScene(mapLevel));
}
}
@ -780,15 +784,17 @@ void MainWindow::on_inspectorWidget_itemChanged(QTableWidgetItem *item)
void MainWindow::on_actionMapSettings_triggered()
{
auto mapSettingsDialog = new MapSettings(controller, this);
mapSettingsDialog->setModal(true);
auto settingsDialog = new MapSettings(controller, this);
settingsDialog->setWindowModality(Qt::WindowModal);
settingsDialog->setModal(true);
}
void MainWindow::on_actionPlayers_settings_triggered()
{
auto mapSettingsDialog = new PlayerSettings(*controller.map(), this);
mapSettingsDialog->setModal(true);
auto settingsDialog = new PlayerSettings(*controller.map(), this);
settingsDialog->setWindowModality(Qt::WindowModal);
settingsDialog->setModal(true);
}
void MainWindow::enableUndo(bool enable)
@ -799,4 +805,5 @@ void MainWindow::enableUndo(bool enable)
void MainWindow::enableRedo(bool enable)
{
ui->actionRedo->setEnabled(enable);
}
}

View File

@ -101,9 +101,7 @@ private:
private:
Ui::MainWindow *ui;
ObjectBrowser * objectBrowser = nullptr;
QGraphicsScene * sceneMini;
QGraphicsScene * scenePreview;
QPixmap minimap;
QString filename;
bool unsaved = false;

View File

@ -77,9 +77,16 @@
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
</widget>
<widget class="QMenu" name="menuPlayer">
<property name="title">
<string>Player</string>
</property>
<addaction name="actionNeutral"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuMap"/>
<addaction name="menuPlayer"/>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
@ -148,7 +155,7 @@
<number>0</number>
</property>
<item>
<widget class="QGraphicsView" name="minimapView">
<widget class="MinimapView" name="minimapView">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -862,6 +869,14 @@
<bool>true</bool>
</property>
</action>
<action name="actionNeutral">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Neutral</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@ -869,6 +884,11 @@
<extends>QGraphicsView</extends>
<header>mapview.h</header>
</customwidget>
<customwidget>
<class>MinimapView</class>
<extends>QGraphicsView</extends>
<header>mapview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

View File

@ -18,12 +18,19 @@ MapController::MapController(MainWindow * m): main(m)
{
_scenes[0].reset(new MapScene(0));
_scenes[1].reset(new MapScene(1));
_miniscenes[0].reset(new MinimapScene(0));
_miniscenes[1].reset(new MinimapScene(1));
}
MapController::~MapController()
{
}
const std::unique_ptr<CMap> & MapController::getMapUniquePtr() const
{
return _map;
}
CMap * MapController::map()
{
return _map.get();
@ -39,11 +46,18 @@ MapScene * MapController::scene(int level)
return _scenes[level].get();
}
MinimapScene * MapController::miniScene(int level)
{
return _miniscenes[level].get();
}
void MapController::setMap(std::unique_ptr<CMap> cmap)
{
_map = std::move(cmap);
_scenes[0].reset(new MapScene(0));
_scenes[1].reset(new MapScene(1));
_miniscenes[0].reset(new MinimapScene(0));
_miniscenes[1].reset(new MinimapScene(1));
resetMapHandler();
sceneForceUpdate();
@ -58,13 +72,18 @@ void MapController::setMap(std::unique_ptr<CMap> cmap)
void MapController::sceneForceUpdate()
{
_scenes[0]->updateViews();
_miniscenes[0]->updateViews();
if(_map->twoLevel)
{
_scenes[1]->updateViews();
_miniscenes[1]->updateViews();
}
}
void MapController::sceneForceUpdate(int level)
{
_scenes[level]->updateViews();
_miniscenes[level]->updateViews();
}
void MapController::resetMapHandler()
@ -72,6 +91,8 @@ void MapController::resetMapHandler()
_mapHandler.reset(new MapHandler(_map.get()));
_scenes[0]->initialize(*this);
_scenes[1]->initialize(*this);
_miniscenes[0]->initialize(*this);
_miniscenes[1]->initialize(*this);
}
void MapController::commitTerrainChange(int level, const Terrain & terrain)
@ -91,6 +112,7 @@ void MapController::commitTerrainChange(int level, const Terrain & terrain)
_scenes[level]->terrainView.setDirty(t);
_scenes[level]->terrainView.draw();
_miniscenes[level]->updateViews();
main->mapChanged();
}
@ -105,6 +127,7 @@ void MapController::commitObjectErase(int level)
resetMapHandler();
_scenes[level]->updateViews();
_miniscenes[level]->updateViews();
main->mapChanged();
}
@ -156,6 +179,7 @@ void MapController::commitObstacleFill(int level)
resetMapHandler();
_scenes[level]->updateViews();
_miniscenes[level]->updateViews();
main->mapChanged();
}
@ -165,6 +189,7 @@ void MapController::commitObjectChange(int level)
_scenes[level]->objectsView.draw();
_scenes[level]->selectionObjectsView.draw();
_miniscenes[level]->updateViews();
main->mapChanged();
}
@ -205,6 +230,7 @@ void MapController::commitObjectShiftOrCreate(int level)
resetMapHandler();
_scenes[level]->updateViews();
_miniscenes[level]->updateViews();
main->mapChanged();
}

View File

@ -17,9 +17,11 @@ public:
void setMap(std::unique_ptr<CMap>);
const std::unique_ptr<CMap> & getMapUniquePtr() const; //to be used for map saving
CMap * map();
MapHandler * mapHandler();
MapScene * scene(int level);
MinimapScene * miniScene(int level);
void resetMapHandler();
@ -46,6 +48,7 @@ private:
std::unique_ptr<MapHandler> _mapHandler;
MainWindow * main;
mutable std::array<std::unique_ptr<MapScene>, 2> _scenes;
mutable std::array<std::unique_ptr<MinimapScene>, 2> _miniscenes;
};
#endif // MAPCONTROLLER_H

View File

@ -502,3 +502,39 @@ void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj,
}
}
}
QRgb MapHandler::getTileColor(int x, int y, int z)
{
// if object at tile is owned - it will be colored as its owner
for(auto & object : getObjects(x, y, z))
{
//heroes will be blitted later
switch (object.obj->ID)
{
case Obj::HERO:
case Obj::PRISON:
continue;
}
PlayerColor player = object.obj->getOwner();
if(player == PlayerColor::NEUTRAL)
return graphics->neutralColor;
else
if (player < PlayerColor::PLAYER_LIMIT)
return graphics->playerColors[player.getNum()];
}
// else - use terrain color (blocked version or normal)
auto & tile = map->getTile(int3(x, y, z));
auto color = Terrain::Manager::getInfo(tile.terType).minimapUnblocked;
if (tile.blocked && (!tile.visitable))
color = Terrain::Manager::getInfo(tile.terType).minimapBlocked;
return qRgb(color[0], color[1], color[2]);
}
void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
{
painter.setPen(getTileColor(x, y, z));
painter.drawPoint(x, y);
}

View File

@ -98,7 +98,9 @@ public:
std::vector<TerrainTileObject> & getObjects(int x, int y, int z);
//void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, bool moving) const;
//void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const;
QRgb getTileColor(int x, int y, int z);
void drawMinimapTile(QPainter & painter, int x, int y, int z);
mutable std::map<const CGObjectInstance*, ui8> animationPhase;
MapHandler(const CMap * Map);

View File

@ -4,12 +4,62 @@
#include <QGraphicsSceneMouseEvent>
#include "mapcontroller.h"
MinimapView::MinimapView(QWidget * parent):
QGraphicsView(parent)
{
// Disable scrollbars
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void MinimapView::dimensions()
{
fitInView(0, 0, controller->map()->width, controller->map()->height, Qt::KeepAspectRatio);
}
void MinimapView::setController(MapController * ctrl)
{
controller = ctrl;
}
void MinimapView::mouseMoveEvent(QMouseEvent *mouseEvent)
{
this->update();
auto * sc = static_cast<MinimapScene*>(scene());
if(!sc)
return;
int w = sc->viewport.viewportWidth();
int h = sc->viewport.viewportHeight();
auto pos = mapToScene(mouseEvent->pos());
pos.setX(pos.x() - w / 2);
pos.setY(pos.y() - h / 2);
QPointF point = pos * 32;
emit cameraPositionChanged(point);
}
void MinimapView::mousePressEvent(QMouseEvent* event)
{
mouseMoveEvent(event);
}
MapView::MapView(QWidget * parent):
QGraphicsView(parent),
selectionTool(MapView::SelectionTool::None)
{
}
void MapView::cameraChanged(const QPointF & pos)
{
//ui->mapView->translate(pos.x(), pos.y());
horizontalScrollBar()->setValue(pos.x());
verticalScrollBar()->setValue(pos.y());
}
void MapView::setController(MapController * ctrl)
{
controller = ctrl;
@ -273,15 +323,43 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
}
}
MapScene::MapScene(int lev):
bool MapView::viewportEvent(QEvent *event)
{
if(auto * sc = static_cast<MapScene*>(scene()))
{
//auto rect = sceneRect();
auto rect = mapToScene(viewport()->geometry()).boundingRect();
controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
}
return QGraphicsView::viewportEvent(event);
}
MapSceneBase::MapSceneBase(int lvl):
QGraphicsScene(nullptr),
level(lvl)
{
}
void MapSceneBase::initialize(MapController & controller)
{
for(auto * layer : getAbstractLayers())
layer->initialize(controller);
}
void MapSceneBase::updateViews()
{
for(auto * layer : getAbstractLayers())
layer->update();
}
MapScene::MapScene(int lvl):
MapSceneBase(lvl),
gridView(this),
passabilityView(this),
selectionTerrainView(this),
terrainView(this),
objectsView(this),
selectionObjectsView(this),
level(lev)
selectionObjectsView(this)
{
}
@ -298,19 +376,36 @@ std::list<AbstractLayer *> MapScene::getAbstractLayers()
};
}
void MapScene::initialize(MapController & controller)
{
for(auto * layer : getAbstractLayers())
layer->initialize(controller);
}
void MapScene::updateViews()
{
for(auto * layer : getAbstractLayers())
layer->update();
MapSceneBase::updateViews();
terrainView.show(true);
objectsView.show(true);
selectionTerrainView.show(true);
selectionObjectsView.show(true);
}
MinimapScene::MinimapScene(int lvl):
MapSceneBase(lvl),
minimapView(this),
viewport(this)
{
}
std::list<AbstractLayer *> MinimapScene::getAbstractLayers()
{
//sequence is important because it defines rendering order
return {
&minimapView,
&viewport
};
}
void MinimapScene::updateViews()
{
MapSceneBase::updateViews();
minimapView.show(true);
viewport.show(true);
}

View File

@ -11,13 +11,40 @@ class CGObjectInstance;
class MainWindow;
class MapController;
class MapScene : public QGraphicsScene
class MapSceneBase : public QGraphicsScene
{
public:
MapScene(int lev);
MapSceneBase(int lvl);
void updateViews();
void initialize(MapController &);
const int level;
virtual void updateViews();
virtual void initialize(MapController &);
protected:
virtual std::list<AbstractLayer *> getAbstractLayers() = 0;
};
class MinimapScene : public MapSceneBase
{
public:
MinimapScene(int lvl);
void updateViews() override;
MinimapLayer minimapView;
MinimapViewLayer viewport;
protected:
virtual std::list<AbstractLayer *> getAbstractLayers();
};
class MapScene : public MapSceneBase
{
public:
MapScene(int lvl);
void updateViews() override;
GridLayer gridView;
PassabilityLayer passabilityView;
@ -26,10 +53,8 @@ public:
ObjectsLayer objectsView;
SelectionObjectsLayer selectionObjectsView;
const int level;
private:
std::list<AbstractLayer *> getAbstractLayers();
protected:
std::list<AbstractLayer *> getAbstractLayers() override;
};
class MapView : public QGraphicsView
@ -52,9 +77,15 @@ public slots:
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void cameraChanged(const QPointF & pos);
signals:
void openObjectProperties(CGObjectInstance *);
//void viewportChanged(const QRectF & rect);
protected:
bool viewportEvent(QEvent *event) override;
private:
MapController * controller = nullptr;
QPointF mouseStart;
@ -63,4 +94,27 @@ private:
bool pressedOnSelected;
};
class MinimapView : public QGraphicsView
{
Q_OBJECT
public:
MinimapView(QWidget * parent);
void setController(MapController *);
void dimensions();
public slots:
void mouseMoveEvent(QMouseEvent * mouseEvent) override;
void mousePressEvent(QMouseEvent* event) override;
signals:
void cameraPositionChanged(const QPointF & newPosition);
private:
MapController * controller = nullptr;
int displayWidth = 192;
int displayHeight = 192;
};
#endif // MAPVIEW_H

View File

@ -2,6 +2,9 @@
<ui version="4.0">
<class>PlayerSettings</class>
<widget class="QDialog" name="PlayerSettings">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
@ -10,8 +13,14 @@
<height>283</height>
</rect>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Player settings</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="8">

View File

@ -6,7 +6,7 @@
#include "mapview.h"
#include "mapcontroller.h"
AbstractLayer::AbstractLayer(MapScene * s): scene(s)
AbstractLayer::AbstractLayer(MapSceneBase * s): scene(s)
{
}
@ -21,29 +21,9 @@ void AbstractLayer::show(bool show)
if(isShown == show)
return;
if(show)
{
if(pixmap)
{
if(item)
item->setPixmap(*pixmap);
else
item.reset(scene->addPixmap(*pixmap));
}
else
{
if(item)
item->setPixmap(emptyPixmap);
else
item.reset(scene->addPixmap(emptyPixmap));
}
}
else
{
item->setPixmap(emptyPixmap);
}
isShown = show;
redraw();
}
void AbstractLayer::redraw()
@ -64,7 +44,7 @@ void AbstractLayer::redraw()
}
}
GridLayer::GridLayer(MapScene * s): AbstractLayer(s)
GridLayer::GridLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -90,7 +70,7 @@ void GridLayer::update()
redraw();
}
PassabilityLayer::PassabilityLayer(MapScene * s): AbstractLayer(s)
PassabilityLayer::PassabilityLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -121,7 +101,7 @@ void PassabilityLayer::update()
redraw();
}
SelectionTerrainLayer::SelectionTerrainLayer(MapScene * s): AbstractLayer(s)
SelectionTerrainLayer::SelectionTerrainLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -200,7 +180,7 @@ const std::set<int3> & SelectionTerrainLayer::selection() const
return area;
}
TerrainLayer::TerrainLayer(MapScene * s): AbstractLayer(s)
TerrainLayer::TerrainLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -275,7 +255,7 @@ void TerrainLayer::draw(bool onlyDirty)
redraw();
}
ObjectsLayer::ObjectsLayer(MapScene * s): AbstractLayer(s)
ObjectsLayer::ObjectsLayer(MapSceneBase * s): AbstractLayer(s)
{
}
@ -342,7 +322,7 @@ void ObjectsLayer::setDirty(const CGObjectInstance * object)
dirty.insert(object);
}
SelectionObjectsLayer::SelectionObjectsLayer(MapScene * s): AbstractLayer(s), newObject(nullptr)
SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)
{
}
@ -491,3 +471,71 @@ void SelectionObjectsLayer::clear()
shift.setX(0);
shift.setY(0);
}
MinimapLayer::MinimapLayer(MapSceneBase * s): AbstractLayer(s)
{
}
void MinimapLayer::update()
{
if(!map)
return;
pixmap.reset(new QPixmap(map->width, map->height));
QPainter painter(pixmap.get());
//coordinate transfomation
for(int j = 0; j < map->height; ++j)
{
for(int i = 0; i < map->width; ++i)
{
handler->drawMinimapTile(painter, i, j, scene->level);
}
}
redraw();
}
MinimapViewLayer::MinimapViewLayer(MapSceneBase * s): AbstractLayer(s)
{
}
void MinimapViewLayer::update()
{
if(!map)
return;
pixmap.reset(new QPixmap(map->width, map->height));
pixmap->fill(QColor(0, 0, 0, 0));
QPainter painter(pixmap.get());
painter.setPen(QColor(255, 255, 255));
painter.drawRect(x, y, w, h);
redraw();
}
void MinimapViewLayer::draw()
{
if(!map)
return;
pixmap->fill(QColor(0, 0, 0, 0));
//maybe not optimal but ok
QPainter painter(pixmap.get());
painter.setPen(QColor(255, 255, 255));
painter.drawRect(x, y, w, h);
redraw();
}
void MinimapViewLayer::setViewport(int _x, int _y, int _w, int _h)
{
x = _x;
y = _y;
w = _w;
h = _h;
draw();
}

View File

@ -3,7 +3,7 @@
#include "../lib/int3.h"
class MapScene;
class MapSceneBase;
class CGObjectInstance;
class MapController;
class CMap;
@ -12,7 +12,7 @@ class MapHandler;
class AbstractLayer
{
public:
AbstractLayer(MapScene * s);
AbstractLayer(MapSceneBase * s);
virtual void update() = 0;
@ -21,7 +21,7 @@ public:
void initialize(MapController & controller);
protected:
MapScene * scene;
MapSceneBase * scene;
CMap * map = nullptr;
MapHandler * handler = nullptr;
bool isShown = false;
@ -37,7 +37,7 @@ private:
class GridLayer: public AbstractLayer
{
public:
GridLayer(MapScene * s);
GridLayer(MapSceneBase * s);
void update() override;
};
@ -45,7 +45,7 @@ public:
class PassabilityLayer: public AbstractLayer
{
public:
PassabilityLayer(MapScene * s);
PassabilityLayer(MapSceneBase * s);
void update() override;
};
@ -54,7 +54,7 @@ public:
class SelectionTerrainLayer: public AbstractLayer
{
public:
SelectionTerrainLayer(MapScene * s);
SelectionTerrainLayer(MapSceneBase * s);
void update() override;
@ -73,7 +73,7 @@ private:
class TerrainLayer: public AbstractLayer
{
public:
TerrainLayer(MapScene * s);
TerrainLayer(MapSceneBase * s);
void update() override;
@ -88,7 +88,7 @@ private:
class ObjectsLayer: public AbstractLayer
{
public:
ObjectsLayer(MapScene * s);
ObjectsLayer(MapSceneBase * s);
void update() override;
@ -105,7 +105,7 @@ private:
class SelectionObjectsLayer: public AbstractLayer
{
public:
SelectionObjectsLayer(MapScene * s);
SelectionObjectsLayer(MapSceneBase * s);
void update() override;
@ -127,4 +127,32 @@ private:
std::set<CGObjectInstance *> selectedObjects;
};
class MinimapLayer: public AbstractLayer
{
public:
MinimapLayer(MapSceneBase * s);
void update() override;
};
class MinimapViewLayer: public AbstractLayer
{
public:
MinimapViewLayer(MapSceneBase * s);
void setViewport(int x, int y, int w, int h);
void draw();
void update() override;
int viewportX() const {return x;}
int viewportY() const {return y;}
int viewportWidth() const {return w;}
int viewportHeight() const {return h;}
private:
int x = 0, y = 0, w = 1, h = 1;
};
#endif // SCENELAYER_H

View File

@ -250,15 +250,23 @@ void WindowNewMap::on_templateCombo_activated(int index)
void WindowNewMap::on_widthTxt_textChanged(const QString &arg1)
{
mapGenOptions.setWidth(arg1.toInt());
updateTemplateList();
int sz = arg1.toInt();
if(sz > 1)
{
mapGenOptions.setWidth(arg1.toInt());
updateTemplateList();
}
}
void WindowNewMap::on_heightTxt_textChanged(const QString &arg1)
{
mapGenOptions.setHeight(arg1.toInt());
updateTemplateList();
int sz = arg1.toInt();
if(sz > 1)
{
mapGenOptions.setHeight(arg1.toInt());
updateTemplateList();
}
}
void WindowNewMap::updateTemplateList()