Skip to content

Commit

Permalink
finezoom and zoomout activateable by setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Oct 6, 2019
1 parent e468324 commit 86aed53
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 41 deletions.
77 changes: 41 additions & 36 deletions mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
MapView::MapView(QWidget *parent)
: QWidget(parent)
, cache(ChunkCache::Instance())
, zoom_internal(1.0)
{
depth = 255;
scale = 1;
zoom = 1.0;
connect(&cache, SIGNAL(chunkLoaded(int, int)),
this, SLOT (chunkUpdated(int, int)));
connect(&cache, SIGNAL(structureFound(QSharedPointer<GeneratedStructure>)),
Expand Down Expand Up @@ -131,19 +131,24 @@ void MapView::clearCache() {
redraw();
}

double MapView::getZoom() const
void MapView::adjustZoom(double steps)
{
return zoom_internal*zoom_internal; // get a more "natural" zoom behaviour
}
const bool allowZoomOut = QSettings().value("zoomout", false).toBool();

void MapView::adjustZoom(double rate)
{
static const double zoomMin = sqrt(0.17);
static const double zoomMax = sqrt(20.0);
const double zoomMin = allowZoomOut ? 0.20 : 1.0;
const double zoomMax = 20.0;

const bool useFineZoomStrategy = QSettings().value("finezoom", false).toBool();

if (useFineZoomStrategy) {
zoom *= pow(1.3, steps);
}
else {
zoom = floor(zoom + steps);
}

zoom_internal *= (1.0 + (rate / 100.0));
if (zoom_internal < zoomMin) zoom_internal = zoomMin;
if (zoom_internal > zoomMax) zoom_internal = zoomMax;
if (zoom < zoomMin) zoom = zoomMin;
if (zoom > zoomMax) zoom = zoomMax;
}

static int lastMouseX = -1, lastMouseY = -1;
Expand All @@ -162,17 +167,17 @@ void MapView::mouseMoveEvent(QMouseEvent *event) {
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;

centerx -= (this->x - centerblockx) * getZoom();
centery -= (this->z - centerblockz) * getZoom();
centerx -= (this->x - centerblockx) * zoom;
centery -= (this->z - centerblockz) * zoom;

int mx = floor(centerblockx - (centerx - event->x()) / getZoom());
int mz = floor(centerblockz - (centery - event->y()) / getZoom());
int mx = floor(centerblockx - (centerx - event->x()) / zoom);
int mz = floor(centerblockz - (centery - event->y()) / zoom);

getToolTip(mx, mz);
return;
}
x += (lastMouseX-event->x()) / getZoom();
z += (lastMouseY-event->y()) / getZoom();
x += (lastMouseX-event->x()) / zoom;
z += (lastMouseY-event->y()) / zoom;
lastMouseX = event->x();
lastMouseY = event->y();

Expand All @@ -190,11 +195,11 @@ void MapView::mouseDoubleClickEvent(QMouseEvent *event) {
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;

centerx -= (this->x - centerblockx) * getZoom();
centery -= (this->z - centerblockz) * getZoom();
centerx -= (this->x - centerblockx) * zoom;
centery -= (this->z - centerblockz) * zoom;

int mx = floor(centerblockx - (centerx - event->x()) / getZoom());
int mz = floor(centerblockz - (centery - event->y()) / getZoom());
int mx = floor(centerblockx - (centerx - event->x()) / zoom);
int mz = floor(centerblockz - (centery - event->y()) / zoom);

// get the y coordinate
int my = getY(mx, mz);
Expand Down Expand Up @@ -239,22 +244,22 @@ void MapView::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Up:
case Qt::Key_W:
z -= stepSize / getZoom();
z -= stepSize / zoom;
redraw();
break;
case Qt::Key_Down:
case Qt::Key_S:
z += stepSize / getZoom();
z += stepSize / zoom;
redraw();
break;
case Qt::Key_Left:
case Qt::Key_A:
x -= stepSize / getZoom();
x -= stepSize / zoom;
redraw();
break;
case Qt::Key_Right:
case Qt::Key_D:
x += stepSize / getZoom();
x += stepSize / zoom;
redraw();
break;
case Qt::Key_PageUp:
Expand Down Expand Up @@ -301,7 +306,7 @@ void MapView::redraw() {
return;
}

double chunksize = 16 * getZoom();
double chunksize = 16 * zoom;

// first find the center block position
int centerchunkx = floor(x / 16);
Expand All @@ -310,8 +315,8 @@ void MapView::redraw() {
int centerx = imageChunks.width() / 2;
int centery = imageChunks.height() / 2;
// and align for panning
centerx -= (x - centerchunkx * 16) * getZoom();
centery -= (z - centerchunkz * 16) * getZoom();
centerx -= (x - centerchunkx * 16) * zoom;
centery -= (z - centerchunkz * 16) * zoom;
// now calculate the topleft block on the screen
int startx = centerchunkx - floor(centerx / chunksize) - 1;
int startz = centerchunkz - floor(centery / chunksize) - 1;
Expand All @@ -328,8 +333,8 @@ void MapView::redraw() {

// add on the entity layer
QPainter canvas(&imageOverlays);
double halfviewwidth = imageOverlays.width() / 2 / getZoom();
double halvviewheight = imageOverlays.height() / 2 / getZoom();
double halfviewwidth = imageOverlays.width() / 2 / zoom;
double halvviewheight = imageOverlays.height() / 2 / zoom;
double x1 = x - halfviewwidth;
double z1 = z - halvviewheight;
double x2 = x + halfviewwidth;
Expand All @@ -355,7 +360,7 @@ void MapView::redraw() {
int highY = chunk->depth[index];
if ( (entityY+10 >= highY) ||
(entityY+10 >= depth) )
(*it)->draw(x1, z1, getZoom(), &canvas);
(*it)->draw(x1, z1, zoom, &canvas);
}
}
}
Expand All @@ -369,7 +374,7 @@ void MapView::redraw() {
for (auto &item : overlayItems[type]) {
if (item->intersects(OverlayItem::Point(x1 - 1, 0, z1 - 1),
OverlayItem::Point(x2 + 1, depth, z2 + 1))) {
item->draw(x1, z1, getZoom(), &canvas);
item->draw(x1, z1, zoom, &canvas);
}
}
}
Expand Down Expand Up @@ -407,11 +412,11 @@ void MapView::drawChunk(int x, int z) {
double centerx = imageChunks.width() / 2;
double centery = imageChunks.height() / 2;
// which need to be shifted to account for panning inside that chunk
centerx -= (this->x - centerchunkx * 16) * getZoom();
centery -= (this->z - centerchunkz * 16) * getZoom();
centerx -= (this->x - centerchunkx * 16) * zoom;
centery -= (this->z - centerchunkz * 16) * zoom;
// centerx,y now points to the top left corner of the center chunk
// so now calculate our x,y in relation
double chunksize = 16 * getZoom();
double chunksize = 16 * zoom;
centerx += (x - centerchunkx) * chunksize;
centery += (z - centerchunkz) * chunksize;

Expand Down Expand Up @@ -544,7 +549,7 @@ QList<QSharedPointer<OverlayItem>> MapView::getItems(int x, int y, int z) {
QSharedPointer<Chunk> chunk(cache.fetch(cx, cz));

if (chunk) {
double invzoom = 10.0 / getZoom();
double invzoom = 10.0 / zoom;
for (auto &type : overlayItemTypes) {
// generated structures
for (auto &item : overlayItems[type]) {
Expand Down
5 changes: 2 additions & 3 deletions mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ class MapView : public QWidget {
void getToolTip(int x, int z);
int getY(int x, int z);
QList<QSharedPointer<OverlayItem>> getItems(int x, int y, int z);
double getZoom() const;
void adjustZoom(double rate);
void adjustZoom(double steps);

static const int CAVE_DEPTH = 16; // maximum depth caves are searched in cave mode
float caveshade[CAVE_DEPTH];

int depth;
double x, z;
int scale;
double zoom_internal;
double zoom;
int flags;
ChunkCache &cache;
QImage imageChunks;
Expand Down
20 changes: 20 additions & 0 deletions settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ Settings::Settings(QWidget *parent) : QDialog(parent) {
}
autoUpdate = info.value("autoupdate", true).toBool();
verticalDepth = info.value("verticaldepth", true).toBool();
fineZoom = info.value("finezoom", false).toBool();
zoomOut = info.value("zoomout", false).toBool();

// Set the UI to the current settings' values:
m_ui.checkBox_AutoUpdate->setChecked(autoUpdate);
m_ui.lineEdit_Location->setText(mcpath);
m_ui.lineEdit_Location->setDisabled(useDefault);
m_ui.checkBox_DefaultLocation->setChecked(useDefault);
m_ui.checkBox_VerticalDepth->setChecked(verticalDepth);
m_ui.checkBox_fine_zoom->setChecked(fineZoom);
m_ui.checkBox_zoom_out->setChecked(zoomOut);
}

QString Settings::getDefaultLocation()
Expand Down Expand Up @@ -104,3 +108,19 @@ void Settings::toggleVerticalDepth(bool value) {
info.setValue("verticaldepth", value);
emit settingsUpdated();
}

void Settings::on_checkBox_zoom_out_toggled(bool checked)
{
zoomOut = checked;
QSettings info;
info.setValue("zoomout", checked);
emit settingsUpdated();
}

void Settings::on_checkBox_fine_zoom_toggled(bool checked)
{
fineZoom = checked;
QSettings info;
info.setValue("finezoom", checked);
emit settingsUpdated();
}
8 changes: 7 additions & 1 deletion settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Settings : public QDialog {
bool autoUpdate;
bool verticalDepth;
QString mcpath;
bool fineZoom;
bool zoomOut;


/** Returns the default path to be used for Minecraft location. */
Expand All @@ -30,7 +32,11 @@ class Settings : public QDialog {
void pathChanged(const QString &path);
void toggleVerticalDepth(bool on);

private:
void on_checkBox_zoom_out_toggled(bool checked);

void on_checkBox_fine_zoom_toggled(bool checked);

private:
Ui::Settings m_ui;
};

Expand Down
25 changes: 24 additions & 1 deletion settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>242</height>
<height>370</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -85,6 +85,29 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_experimental">
<property name="title">
<string>Experimental or unstable features</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QCheckBox" name="checkBox_fine_zoom">
<property name="text">
<string>Fine zoom steps (recommended for zoom-out)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_zoom_out">
<property name="text">
<string>Zoom-out functionality (needs much RAM)</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand Down

0 comments on commit 86aed53

Please sign in to comment.