Skip to content

Commit bf60879

Browse files
softmothmyk002
authored andcommitted
[tweak/stable-cursor] Keep stable cursor when viewport is near enough
Allow the viewport to move a bit and still keep the cursor location.
1 parent 0f464b1 commit bf60879

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

docs/Plugins.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ Subcommands that persist until disabled or DF quits:
442442
:nestbox-color: Fixes the color of built nestboxes
443443
:reaction-gloves: Fixes reactions to produce gloves in sets with correct handedness (:bug:`6273`)
444444
:shift-8-scroll: Gives Shift-8 (or :kbd:`*`) priority when scrolling menus, instead of scrolling the map
445-
:stable-cursor: Saves the exact cursor position between t/q/k/d/b/etc menus of fortress mode.
445+
:stable-cursor: Saves the exact cursor position between t/q/k/d/b/etc menus of fortress mode, if the
446+
map view is near enough to its previous position.
446447
:stone-status-all: Adds an option to toggle the economic status of all stones
447448
:title-start-rename: Adds a safe rename option to the title screen "Start Playing" menu
448449
:tradereq-pet-gender: Displays pet genders on the trade request screen

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
5656
- `autochop`: only designate the amount of trees required to reach ``max_logs``
5757
- `autochop`: preferably designate larger trees over smaller ones
5858
- `blueprint`: ``track`` phase renamed to ``carve``. carved fortifications and (optionally) engravings are now captured in blueprints
59+
- `tweak` stable-cursor: Keep the cursor stable even when the viewport moves a small amount
5960

6061
## Documentation
6162
- Add more examples to the plugin skeleton files so they are more informative for a newbie

plugins/tweak/tweaks/stable-cursor.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ struct stable_cursor_hook : df::viewscreen_dwarfmodest
3232
}
3333
}
3434

35+
bool check_viewport_near_enough()
36+
{
37+
df::coord view = Gui::getViewportPos();
38+
auto dims = Gui::getDwarfmodeViewDims();
39+
int view_size = min(dims.map_x2 - dims.map_x1, dims.map_y2 - dims.map_y1);
40+
// Heuristic to keep the cursor position if the view is still in the
41+
// part of the map that was already visible
42+
int near_enough_distance = min(30, view_size / 2);
43+
44+
return
45+
// Is the viewport near enough to the old viewport?
46+
abs(view.x - last_view.x) <= near_enough_distance &&
47+
abs(view.y - last_view.y) <= near_enough_distance &&
48+
view.z == last_view.z &&
49+
// And is the last cursor visible in the current viewport?
50+
last_cursor.x >= view.x && last_cursor.y >= view.y &&
51+
last_cursor.x <= view.x + dims.map_x2 - dims.map_x1 &&
52+
last_cursor.y <= view.y + dims.map_y2 - dims.map_y1;
53+
}
54+
3555
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
3656
{
3757
bool was_default = check_default();
@@ -47,9 +67,8 @@ struct stable_cursor_hook : df::viewscreen_dwarfmodest
4767
{
4868
last_view = view; last_cursor = cursor;
4969
}
50-
else if (!is_default && was_default &&
51-
Gui::getViewportPos() == last_view &&
52-
last_cursor.isValid() && cur_cursor.isValid())
70+
else if (!is_default && was_default && cur_cursor.isValid() &&
71+
last_cursor.isValid() && check_viewport_near_enough())
5372
{
5473
Gui::setCursorCoords(last_cursor.x, last_cursor.y, last_cursor.z);
5574
Gui::refreshSidebar();

0 commit comments

Comments
 (0)