Skip to content

Commit bfee603

Browse files
authored
Merge branch 'develop' into refactor/hotkey_module
2 parents c080015 + d24e8f1 commit bfee603

File tree

12 files changed

+446
-15
lines changed

12 files changed

+446
-15
lines changed

docs/changelog.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ Template for new versions:
5555
# Future
5656

5757
## New Tools
58+
- ``edgescroll``: Introduced plugin to pan the view automatically when the mouse reaches the screen border.
5859

5960
## New Features
61+
- `sort`: Places search widget can search "Siege engines" subtab by name, loaded status, and operator status
6062

6163
## Fixes
64+
- `sort`: Using the squad unit selector will no longer cause Dwarf Fortress to crash on exit
65+
- `sort`: Places search widget moved to account for DF's new "Siege engines" subtab
6266

6367
## Misc Improvements
6468
- `createitem`: created items can now be placed onto/into tables, nests, bookcases, display cases, and altars
@@ -71,6 +75,7 @@ Template for new versions:
7175
- ``Hotkey``: New module for hotkey functionality
7276

7377
## Lua
78+
- The ``Lua interactive interpreter`` banner now documents keywords such as ``unit`` and ``item`` which reference the currently-selected object in the DF UI.
7479
- ``dfhack.hotkey.addKeybind``: Creates new keybindings
7580
- ``dfhack.hotkey.removeKeybind``: Removes existing keybindings
7681
- ``dfhack.hotkey.listActiveKeybinds``: Lists all keybinds for the current context

docs/plugins/edgescroll.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
edgescroll
2+
==========
3+
4+
.. dfhack-tool::
5+
:summary: Scroll the game world and region maps when the mouse reaches the window border.
6+
:tags: interface
7+
8+
Usage
9+
-----
10+
11+
::
12+
13+
enable edgescroll

library/Core.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,10 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) {
20122012
return ret;
20132013
}
20142014

2015+
void Core::DFH_SDL_Loop() {
2016+
DFHack::runRenderThreadCallbacks();
2017+
}
2018+
20152019
bool Core::doSdlInputEvent(SDL_Event* ev)
20162020
{
20172021
// this should only ever be called from the render thread

library/Hooks.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ DFhackCExport void dfhooks_sdl_loop() {
6868
if (disabled)
6969
return;
7070
// TODO: wire this up to the new SDL-based console once it is merged
71+
DFHack::Core::getInstance().DFH_SDL_Loop();
7172
}
7273

7374
// called from the main thread for each utf-8 char read from the ncurses input

library/include/Core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ namespace DFHack
154154
friend void ::dfhooks_update();
155155
friend void ::dfhooks_prerender();
156156
friend bool ::dfhooks_sdl_event(SDL_Event* event);
157+
friend void ::dfhooks_sdl_loop();
157158
friend bool ::dfhooks_ncurses_key(int key);
158159
public:
159160
/// Get the single Core instance or make one.
@@ -261,6 +262,7 @@ namespace DFHack
261262
int Update (void);
262263
int Shutdown (void);
263264
bool DFH_SDL_Event(SDL_Event* event);
265+
void DFH_SDL_Loop();
264266
bool ncurses_wgetch(int in, int & out);
265267
bool DFH_ncurses_key(int key);
266268

library/include/modules/DFSDL.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include "Export.h"
44
#include "ColorText.h"
55

6+
#include <cstdint>
7+
#include <functional>
68
#include <vector>
79
#include <cstdint>
810

911
struct SDL_Surface;
1012
struct SDL_Rect;
13+
struct SDL_Renderer;
1114
struct SDL_PixelFormat;
1215
struct SDL_Window;
1316
union SDL_Event;
@@ -57,6 +60,10 @@ namespace DFHack::DFSDL
5760
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format);
5861
DFHACK_EXPORT int DFSDL_ShowSimpleMessageBox(uint32_t flags, const char* title, const char* message, SDL_Window* window);
5962

63+
DFHACK_EXPORT uint32_t DFSDL_GetMouseState(int* x, int* y);
64+
DFHACK_EXPORT void DFSDL_RenderWindowToLogical(SDL_Renderer* renderer, int windowX, int windowY, float* logicalX, float* logicalY);
65+
DFHACK_EXPORT void DFSDL_RenderLogicalToWindow(SDL_Renderer* renderer, float logicalX, float logicalY, int* windowX, int* windowY);
66+
6067
// submitted and returned text is UTF-8
6168
// see wrapper functions below for cp-437 variants
6269
DFHACK_EXPORT char* DFSDL_GetClipboardText();
@@ -80,4 +87,7 @@ namespace DFHack
8087
DFHACK_EXPORT bool getClipboardTextCp437Multiline(std::vector<std::string> * lines);
8188
DFHACK_EXPORT bool setClipboardTextCp437Multiline(std::string text);
8289

90+
// Queue a cb to be run on the render thread
91+
DFHACK_EXPORT void runOnRenderThread(std::function<void()> cb);
92+
DFHACK_EXPORT void runRenderThreadCallbacks();
8393
}

library/lua/dfhack.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -845,16 +845,26 @@ function dfhack.interpreter(prompt,hfile,env)
845845
return nil, 'not interactive'
846846
end
847847

848-
print("Type quit to exit interactive lua interpreter.")
848+
local function print_keyword(pre, keyword, post)
849+
dfhack.color(COLOR_RESET)
850+
if pre ~= nil then dfhack.print(pre) end
851+
dfhack.color(COLOR_YELLOW)
852+
dfhack.print(keyword)
853+
dfhack.color(COLOR_RESET)
854+
if post ~= nil then print(post) end
855+
end
856+
print_keyword("Type ", "quit", " to exit interactive lua interpreter.")
849857

850858
if print_banner then
851-
print("Shortcuts:\n"..
852-
" '= foo' => '_1,_2,... = foo'\n"..
853-
" '! foo' => 'print(foo)'\n"..
854-
" '~ foo' => 'printall(foo)'\n"..
855-
" '^ foo' => 'printall_recurse(foo)'\n"..
856-
" '@ foo' => 'printall_ipairs(foo)'\n"..
857-
"All of these save the first result as '_'.")
859+
print("Shortcuts:")
860+
print_keyword(" '", "= foo", "' => '_1,_2,... = foo'")
861+
print_keyword(" '", "! foo", "' => 'print(foo)'")
862+
print_keyword(" '", "~ foo", "' => 'printall(foo)'")
863+
print_keyword(" '", "^ foo", "' => 'printall_recurse(foo)'")
864+
print_keyword(" '", "@ foo", "' => 'printall_ipairs(foo)'")
865+
print_keyword("All of these save the first result as '", "_", "'.")
866+
print("These keywords refer to the currently-selected object in the game:")
867+
print_keyword(" ", "unit item plant building bld job workshop_job wsjob screen scr", "")
858868
print_banner = false
859869
end
860870

library/modules/DFSDL.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <SDL_stdinc.h>
1010
#include <SDL_keycode.h>
1111

12+
#include <vector>
13+
1214
#ifdef WIN32
1315
# include <regex>
1416
#endif
@@ -62,6 +64,9 @@ SDL_Surface* (*g_SDL_CreateRGBSurfaceWithFormat)(uint32_t flags, int width, int
6264
int (*g_SDL_ShowSimpleMessageBox)(uint32_t flags, const char *title, const char *message, SDL_Window *window) = nullptr;
6365
char* (*g_SDL_GetPrefPath)(const char* org, const char* app) = nullptr;
6466
char* (*g_SDL_GetBasePath)() = nullptr;
67+
uint32_t (*g_SDL_GetMouseState)(int* x, int* y) = nullptr;
68+
void (*g_SDL_RenderWindowToLogical)(SDL_Renderer* renderer, int windowX, int windowY, float* logicalX, float* logicalY);
69+
void (*g_SDL_RenderLogicalToWindow)(SDL_Renderer* renderer, float logicalX, float logicalY, int* windowX, int* windowY);
6570

6671
SDL_Keycode (*g_SDL_GetKeyFromName)(const char* name) = nullptr;
6772
const char* (*g_SDL_GetKeyName)(SDL_Keycode key) = nullptr;
@@ -112,6 +117,9 @@ bool DFSDL::init(color_ostream &out) {
112117
bind(g_sdl_handle, SDL_GetBasePath);
113118
bind(g_sdl_handle, SDL_GetKeyFromName);
114119
bind(g_sdl_handle, SDL_GetKeyName);
120+
bind(g_sdl_handle, SDL_GetMouseState);
121+
bind(g_sdl_handle, SDL_RenderWindowToLogical);
122+
bind(g_sdl_handle, SDL_RenderLogicalToWindow);
115123
#undef bind
116124

117125
DEBUG(dfsdl,out).print("sdl successfully loaded\n");
@@ -196,6 +204,18 @@ char* DFSDL::DFSDL_GetBasePath()
196204
return g_SDL_GetBasePath();
197205
}
198206

207+
uint32_t DFSDL::DFSDL_GetMouseState(int* x, int* y) {
208+
return g_SDL_GetMouseState(x, y);
209+
}
210+
211+
void DFSDL::DFSDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY, float *logicalX, float *logicalY) {
212+
g_SDL_RenderWindowToLogical(renderer, windowX, windowY, logicalX, logicalY);
213+
}
214+
215+
void DFSDL::DFSDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, int *windowX, int *windowY) {
216+
g_SDL_RenderLogicalToWindow(renderer, logicalX, logicalY, windowX, windowY);
217+
}
218+
199219
int DFSDL::DFSDL_ShowSimpleMessageBox(uint32_t flags, const char *title, const char *message, SDL_Window *window) {
200220
if (!g_SDL_ShowSimpleMessageBox)
201221
return -1;
@@ -284,3 +304,25 @@ DFHACK_EXPORT bool DFHack::setClipboardTextCp437Multiline(string text) {
284304
}
285305
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(str.str().c_str());
286306
}
307+
308+
// Queue to run callbacks on the render thread.
309+
// Semantics loosely based on SDL3's SDL_RunOnMainThread
310+
static std::recursive_mutex render_cb_lock;
311+
static std::vector<std::function<void()>> render_cb_queue;
312+
313+
DFHACK_EXPORT void DFHack::runOnRenderThread(std::function<void()> cb) {
314+
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
315+
render_cb_queue.push_back(std::move(cb));
316+
}
317+
318+
DFHACK_EXPORT void DFHack::runRenderThreadCallbacks() {
319+
static decltype(render_cb_queue) local_queue;
320+
{
321+
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
322+
std::swap(local_queue, render_cb_queue);
323+
}
324+
for (auto& cb : local_queue) {
325+
cb();
326+
}
327+
local_queue.clear();
328+
}

plugins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if(BUILD_SUPPORTED)
7575
dfhack_plugin(dwarfvet dwarfvet.cpp LINK_LIBRARIES lua)
7676
#dfhack_plugin(dwarfmonitor dwarfmonitor.cpp LINK_LIBRARIES lua)
7777
#add_subdirectory(embark-assistant)
78+
dfhack_plugin(edgescroll edgescroll.cpp)
7879
dfhack_plugin(eventful eventful.cpp LINK_LIBRARIES lua)
7980
dfhack_plugin(fastdwarf fastdwarf.cpp)
8081
dfhack_plugin(filltraffic filltraffic.cpp)

0 commit comments

Comments
 (0)