1
0

QtBiomeVisualiser: Zoom is now limited and aligned to steps.

This commit is contained in:
madmaxoft 2014-10-03 12:33:03 +02:00
parent 2f945475f2
commit f625b33d56
4 changed files with 169 additions and 59 deletions

View File

@ -80,6 +80,27 @@ void BiomeView::setChunkSource(std::shared_ptr<ChunkSource> a_ChunkSource)
void BiomeView::setPosition(int a_BlockX, int a_BlockZ)
{
m_X = a_BlockX;
m_Z = a_BlockZ;
redraw();
}
void BiomeView::setZoomLevel(double a_ZoomLevel)
{
m_Zoom = a_ZoomLevel;
redraw();
}
void BiomeView::redraw()
{
if (!hasData())
@ -307,12 +328,12 @@ void BiomeView::wheelEvent(QWheelEvent * a_Event)
m_MouseWheelDelta += a_Event->delta();
while (m_MouseWheelDelta >= DELTA_STEP)
{
increaseZoom();
emit wheelUp();
m_MouseWheelDelta -= DELTA_STEP;
}
while (m_MouseWheelDelta <= -DELTA_STEP)
{
decreaseZoom();
emit wheelDown();
m_MouseWheelDelta += DELTA_STEP;
}
}
@ -360,14 +381,14 @@ void BiomeView::keyPressEvent(QKeyEvent * a_Event)
case Qt::Key_PageUp:
case Qt::Key_Q:
{
increaseZoom();
emit increaseZoom();
break;
}
case Qt::Key_PageDown:
case Qt::Key_E:
{
decreaseZoom();
emit decreaseZoom();
break;
}
}
@ -376,52 +397,3 @@ void BiomeView::keyPressEvent(QKeyEvent * a_Event)
void BiomeView::decreaseZoom()
{
if (m_Zoom > 1.001)
{
m_Zoom--;
if (m_Zoom < 1.0)
{
// Just crossed the 100%, fixate the 100% threshold:
m_Zoom = 1.0;
}
}
else if (m_Zoom > 0.01)
{
m_Zoom = m_Zoom / 2;
}
redraw();
}
void BiomeView::increaseZoom()
{
if (m_Zoom > 0.99)
{
if (m_Zoom > 20.0)
{
// Zoom too large
return;
}
m_Zoom++;
}
else
{
m_Zoom = m_Zoom * 2;
if (m_Zoom > 1.0)
{
// Just crossed the 100%, fixate the 100% threshold:
m_Zoom = 1.0;
}
}
redraw();
}

View File

@ -25,7 +25,24 @@ public:
The entire view is then invalidated and regenerated. */
void setChunkSource(std::shared_ptr<ChunkSource> a_ChunkSource);
/** Sets the position of the central pixel of the map to the specified point and redraws the view. */
void setPosition(int a_BlockX, int a_BlockZ);
/** Sets the zoom level to the specified value and redraws the view. */
void setZoomLevel(double a_ZoomLevel);
signals:
/** Signalled when the user uses the wheel to scroll upwards. */
void wheelUp();
/** Signalled when the user uses the wheel to scroll downwards. */
void wheelDown();
/** Signalled when the user presses a key to increase zoom. */
void increaseZoom();
/** Signalled when the user presses a key to decrease zoom. */
void decreaseZoom();
public slots:
/** Redraw the entire widget area. */
@ -86,12 +103,6 @@ protected:
/** Called when the user presses a key. */
virtual void keyPressEvent(QKeyEvent * a_Event) override;
/** Decreases the zoom level and queues a redraw. */
void decreaseZoom();
/** Increases the zoom level and queues a redraw. */
void increaseZoom();
};

View File

@ -18,6 +18,15 @@
const double MainWindow::m_ViewZooms[] =
{
0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 24,
};
MainWindow::MainWindow(QWidget * parent) :
QMainWindow(parent),
m_GeneratorSetup(nullptr),
@ -26,6 +35,11 @@ MainWindow::MainWindow(QWidget * parent) :
initMinecraftPath();
m_BiomeView = new BiomeView();
connect(m_BiomeView, SIGNAL(increaseZoom()), this, SLOT(increaseZoom()));
connect(m_BiomeView, SIGNAL(decreaseZoom()), this, SLOT(decreaseZoom()));
connect(m_BiomeView, SIGNAL(wheelUp()), this, SLOT(increaseZoom()));
connect(m_BiomeView, SIGNAL(wheelDown()), this, SLOT(decreaseZoom()));
m_MainLayout = new QHBoxLayout();
m_MainLayout->addWidget(m_BiomeView, 1);
m_MainLayout->setMenuBar(menuBar());
@ -129,6 +143,68 @@ void MainWindow::openVanillaWorld()
void MainWindow::centerView()
{
m_BiomeView->setPosition(0, 0);
}
void MainWindow::setViewZoom()
{
// The zoom level is stored in the sender action's data, retrieve it:
QAction * action = qobject_cast<QAction *>(sender());
if (action == nullptr)
{
return;
}
double newZoom = m_ViewZooms[action->data().toInt()];
m_BiomeView->setZoomLevel(newZoom);
action->setChecked(true);
}
void MainWindow::increaseZoom()
{
// If already at max zoom, bail out:
if (m_CurrentZoomLevel >= ARRAYCOUNT(m_ViewZooms) - 1)
{
return;
}
// Increase the zoom level:
m_CurrentZoomLevel += 1;
m_actViewZoom[m_CurrentZoomLevel]->setChecked(true);
m_BiomeView->setZoomLevel(m_ViewZooms[m_CurrentZoomLevel]);
}
void MainWindow::decreaseZoom()
{
// If already at min zoom, bail out:
if (m_CurrentZoomLevel == 0)
{
return;
}
// Decrease the zoom level:
m_CurrentZoomLevel -= 1;
m_actViewZoom[m_CurrentZoomLevel]->setChecked(true);
m_BiomeView->setZoomLevel(m_ViewZooms[m_CurrentZoomLevel]);
}
void MainWindow::initMinecraftPath()
{
#ifdef Q_OS_MAC
@ -147,6 +223,7 @@ void MainWindow::initMinecraftPath()
void MainWindow::createActions()
{
// Map menu:
createWorldActions();
m_actNewGen = new QAction(tr("&New generator"), this);
@ -173,6 +250,26 @@ void MainWindow::createActions()
m_actExit->setShortcut(tr("Alt+X"));
m_actExit->setStatusTip(tr("Exit %1").arg(QApplication::instance()->applicationName()));
connect(m_actExit, SIGNAL(triggered()), this, SLOT(close()));
// View menu:
m_actViewCenter = new QAction(tr("&Reset to center"), this);
m_actViewCenter->setStatusTip(tr("Scrolls the view back to the map center"));
connect(m_actViewCenter, SIGNAL(triggered()), this, SLOT(centerView()));
QActionGroup * zoomGroup = new QActionGroup(this);
for (int i = 0; i < ARRAYCOUNT(m_ViewZooms); i++)
{
m_actViewZoom[i] = new QAction(tr("&Zoom %1%").arg(std::floor(m_ViewZooms[i] * 100)), this);
m_actViewZoom[i]->setCheckable(true);
if ((int)(m_ViewZooms[i] * 16) == 16)
{
m_actViewZoom[i]->setChecked(true);
m_CurrentZoomLevel = i;
}
m_actViewZoom[i]->setData(QVariant(i));
zoomGroup->addAction(m_actViewZoom[i]);
connect(m_actViewZoom[i], SIGNAL(triggered()), this, SLOT(setViewZoom()));
}
}
@ -220,6 +317,7 @@ void MainWindow::createWorldActions()
void MainWindow::createMenus()
{
// Map menu:
QMenu * file = menuBar()->addMenu(tr("&Map"));
file->addAction(m_actNewGen);
file->addAction(m_actOpenGen);
@ -235,6 +333,15 @@ void MainWindow::createMenus()
file->addAction(m_actReload);
file->addSeparator();
file->addAction(m_actExit);
// View menu:
QMenu * view = menuBar()->addMenu(tr("&View"));
view->addAction(m_actViewCenter);
view->addSeparator();
for (size_t i = 0; i < ARRAYCOUNT(m_actViewZoom); i++)
{
view->addAction(m_actViewZoom[i]);
}
}

View File

@ -39,13 +39,30 @@ private slots:
/** Opens a vanilla world that is specified by the calling action. */
void openVanillaWorld();
/** Moves the view to the map's center. */
void centerView();
/** Sets the zoom level specified in the triggering action. */
void setViewZoom();
/** Sets a zoom level one step larger than current, if allowed. */
void increaseZoom();
/** Sets a zoom level one step smaller than current, if allowed. */
void decreaseZoom();
protected:
/** The zoom levels */
static const double m_ViewZooms[10];
// Actions:
QAction * m_actNewGen;
QAction * m_actOpenGen;
QAction * m_actOpenWorld;
QAction * m_actReload;
QAction * m_actExit;
QAction * m_actViewCenter;
QAction * m_actViewZoom[ARRAYCOUNT(m_ViewZooms)];
/** List of actions that open the specific vanilla world. */
QList<QAction *> m_WorldActions;
@ -65,6 +82,9 @@ protected:
/** The separator line between biome view and generator setup. */
QWidget * m_LineSeparator;
/** Index into m_ViewZooms[] for the current zoom level. */
size_t m_CurrentZoomLevel;
/** Initializes the m_MinecraftPath based on the proper MC path */
void initMinecraftPath();