Repository navigation
Conversation
There was a problem hiding this comment.
Thanks for tackling this, comments inline. It's quite big and seems to combine unrelated changes, so wondering if this can be split. But 7 commits are OK for a PR
And you missed a fixup commit in the rebase. Can you fold that into the original commit too?
| for(int i = 0; i < myMap->width; i++) | ||
| { | ||
| temp = myMap->getVertex(i, j).z / 5 + 0x0A; //-V807 | ||
| temp = myMap->getVertex(i, j).h; //-V807 |
There was a problem hiding this comment.
Can you explain this a bit? What is the difference between z and h and why is this an issue now? I don't see the relation to zooming.
There was a problem hiding this comment.
h is the map value as stored in the map files. z is a derived value, representing how many pixels the vertex is lifted on the screen.
It is calculated in CMap.cpp:65 as z = triangleIncrease * (h - 0x0A). triangleIncrease is the number of pixels per height step. It is 5 at default zoom, but zooming changes it (range 1–10). 0x0A is the base level of a map.
I just found two instances where z is currently being used for comparison instead of h, making the logic more complicated. I will add them too, in the following commit.
| if(!mapObj || !mapObj->getMap()) | ||
| return false; | ||
| const int newInc = triangleIncrease + deltaInc; | ||
| if(newInc < 1 || newInc > 10) |
| if(y == 0) | ||
| break; | ||
|
|
||
| const Position deltaTri = (y > 0) ? Position(11, 5) : Position(-11, -5); |
There was a problem hiding this comment.
I'll change tryZoom to this, unifying the scroll wheel and the F5-F7-keys and moving the triangle-width and height calculation inside tryZoom(). I don't think it makes sense to calculate width and height (deltaTri) outside of the function:
enum class ZoomDirection
{
In,
Out
};
/// Zoom one step in the given direction, unless the zoom limit is reached
bool tryZoom(ZoomDirection zoomDirection, CMap* mapObj)Then your selected code will be removed and the calling code changes like this:
- if(!tryZoom(deltaTri, deltaInc, MapObj.get()))
+ if(!tryZoom((y > 0) ? ZoomDirection::In : ZoomDirection::Out, MapObj.get()))Which makes it more readable imo.
F6 calls resetZoom() anyway, and doesn't rely on tryZoom. Which should probably also be unified somehow. But I didn't want to fixup others code in this commit, just add minimal bug-fixes.
| } else | ||
| lastMousePos_ = mousePos; | ||
|
|
||
| // Columns of the nearest vertex in odd and even rows (odd rows are shifted by half a triangle to the right) |
There was a problem hiding this comment.
Likely preexisting but can you double-check this? In the init code even rows are shifted, not add rows. Looks like a bug or inconsistency.
See https://github.com/littleant/s25edit/blob/225b89e8521bdfe46b2ba16d96a181bee3d19a64/CMap.cpp#L55-L58
There was a problem hiding this comment.
I'll change it to:
// Columns of the nearest vertex in odd and even rows (even rows are shifted half a triangle to the left)
|
|
||
| // Keep the map point under the cursor stable after zoom | ||
| DisplayRectangle dr = MapObj->getDisplayRect(); | ||
| const PointF ratio(triangleWidth / oldTriSize.x, triangleHeight / oldTriSize.y); |
There was a problem hiding this comment.
Doesn't this need to be zoom-adjusted too or why is triangleHeight correct here?
There was a problem hiding this comment.
You are right. I caught the bug during testing just now, but didn't know the cause. Will be fixed.
| const PointF newOrigin = (PointF(dr.getOrigin()) + mousePos) * ratio - mousePos; | ||
| dr.left = static_cast<Sint32>(newOrigin.x); | ||
| dr.top = static_cast<Sint32>(newOrigin.y); |
There was a problem hiding this comment.
Is the truncation intended or should this be rounding via const Position newOriginInt(newOrigin); (or directly in newOrigin)?
The 4 calculations also seem odd: You only move the rect, don't you? IIRC we have a member function for setting the rect origin without changing the size
In general whenever you have 2 lines with foo.{x,y} there is likely a better way for the operation
There was a problem hiding this comment.
Yes, should be rounding.
The view will be moved with setOrigin instead of setting all 4 sides. Also will replace the per-component ratio with a point division.
| } | ||
| const int additionalRows = triangleIncrease * std::max(0, maxH - 0x0A) / triangleHeight; | ||
| // Vertices are moved up by raised and down by lowered terrain | ||
| const int raisedTolerance = triangleIncrease * std::max(0, maxH - 0x0A); |
There was a problem hiding this comment.
Pre-existing but used here a couple times: Can we have a named constant for 0x0A?
| // Odd rows are shifted right by half a triangle | ||
| const int shift = positiveModulo(y, height) % 2; |
There was a problem hiding this comment.
This is new at this point. And I don't really understand it: You add 0 or 1 to the X-coord. Nothing seems to relate to the "half a triangle" here. Can you explain and enhance the comment?
Likely the positive modulo for the height is not required, IIRC map heights are always multiples of 2
There was a problem hiding this comment.
Yes, we do not need the height. I'll adjust it to:
// The two vertices below vertex x are in columns x - 1 and x if this row is even, x and x + 1 if it is odd,
// because even rows are shifted half a triangle to the left
const int shift = positiveModulo(y, 2);Hope it's clearer that way?
The saved heights were calculated back from the zoomed vertex positions with the factor of the default zoom, so a map saved while zoomed in or out got wrong heights (e.g. 19 instead of 18 one zoom step in).
… the map - Draw the triangles around the view with vertex indices wrapped around the map instead of special-casing the view crossing a map border - Draw the texture borders at the nearest map copy, also if the triangle is more than one map size away - Draw objects of triangles just outside the view reaching into it - Wrap the view origin around the map borders also for large offsets (e.g. after zooming or jumping to a position) - Select the cursor vertex modulo the map size in pixels: The last column couldn't be selected in even rows - Select the column of the first row correctly when the cursor is below the last row (it used the column of an odd row) - Select the rows moved across the top or bottom map border by raised or lowered terrain: Rows behind the border were skipped - Select the cursor row by the height of the closest vertex in that row: It used the height of the neighboring vertex on one side, so next to raised or lowered vertices the cursor jumped to the wrong row - Draw the cursor at the map copy closest to the mouse - Move positiveModulo to defines.h and add floorDiv for CSurface.cpp
- Use the zoom constants instead of literals, with a ZoomDirection enum - Zoom around the cursor also with F5/F6/F7; anchor on the vertex under the cursor so terrain height doesn't make the view drift - Round the new view origin and move it with setOrigin - Add constants for the min, ground and max vertex heights - Clarify the comments on the row offsets
225b89e to
537cbd1
Compare
|
Yes, this PR is quite big, because I didn't want to touch the original code as you reviewed it already in #37. I snuck a fix into the fix-commit right now: when zooming with the F-keys, the cursor is also the "target" like when zooming with the scroll-wheel. |
|
I think I've addressed all change requests. Do you want me to do anything else here? |
This PR includes the commits of #37, rebased onto current master (pr37-rebased, see #37 (comment)), still authored by Morgan. If #37 is merged first, only my last two commits remain here. Otherwise this PR can be merged instead and #37 closed.
Fix wrong vertex heights saved while zoomed in or out
Heights were calculated back from the zoomed vertex positions using the default zoom factor, so they were wrong when saved while zoomed (e.g. 19 instead of 18). Now the stored height is saved directly.
Draw the map and the cursor wrapped around also for views larger than the map
When zoomed out, the view can be larger than the map, but it did not repeat correctly in every case. I fixed the following:
MouseBlit, which hasn't been read anywhere since 2019.Tested on Windows and Linux (Ubuntu).
This also covers changes proposed by #51 with the exception of a minimum window size, that I have not implemented and find unnecessary. So #51 can be closed if this PR merges.
Known Bug: In an Ubuntu VM with "mouse integration" enabled, scrolling does not work correctly. It never worked, but I just wanted to mention this here. I have a fix, but I'm not happy with it so I'm not including it in this PR.