Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Template for new versions:
# Future

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

## New Features

Expand Down
13 changes: 13 additions & 0 deletions docs/plugins/edgescroll.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
edgescroll
==========

.. dfhack-tool::
:summary: Scroll the game world and region maps when the mouse reaches the window border.
:tags: interface

Usage
-----

::

enable edgescroll
4 changes: 4 additions & 0 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,10 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) {
return ret;
}

void Core::DFH_SDL_Loop() {
DFHack::runRenderThreadCallbacks();
}

bool Core::doSdlInputEvent(SDL_Event* ev)
{
// this should only ever be called from the render thread
Expand Down
1 change: 1 addition & 0 deletions library/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ DFhackCExport void dfhooks_sdl_loop() {
if (disabled)
return;
// TODO: wire this up to the new SDL-based console once it is merged
DFHack::Core::getInstance().DFH_SDL_Loop();
}

// called from the main thread for each utf-8 char read from the ncurses input
Expand Down
2 changes: 2 additions & 0 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ namespace DFHack
friend void ::dfhooks_update();
friend void ::dfhooks_prerender();
friend bool ::dfhooks_sdl_event(SDL_Event* event);
friend void ::dfhooks_sdl_loop();
friend bool ::dfhooks_ncurses_key(int key);
public:
/// Get the single Core instance or make one.
Expand Down Expand Up @@ -238,6 +239,7 @@ namespace DFHack
int Update (void);
int Shutdown (void);
bool DFH_SDL_Event(SDL_Event* event);
void DFH_SDL_Loop();
bool ncurses_wgetch(int in, int & out);
bool DFH_ncurses_key(int key);

Expand Down
10 changes: 10 additions & 0 deletions library/include/modules/DFSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include "Export.h"
#include "ColorText.h"

#include <cstdint>
#include <functional>
#include <vector>

struct SDL_Surface;
struct SDL_Rect;
struct SDL_Renderer;
struct SDL_PixelFormat;
struct SDL_Window;
union SDL_Event;
Expand Down Expand Up @@ -55,6 +58,10 @@ namespace DFHack::DFSDL
DFHACK_EXPORT SDL_Surface* DFSDL_CreateRGBSurfaceWithFormat(uint32_t flags, int width, int height, int depth, uint32_t format);
DFHACK_EXPORT int DFSDL_ShowSimpleMessageBox(uint32_t flags, const char* title, const char* message, SDL_Window* window);

DFHACK_EXPORT uint32_t DFSDL_GetMouseState(int* x, int* y);
DFHACK_EXPORT void DFSDL_RenderWindowToLogical(SDL_Renderer* renderer, int windowX, int windowY, float* logicalX, float* logicalY);
DFHACK_EXPORT void DFSDL_RenderLogicalToWindow(SDL_Renderer* renderer, float logicalX, float logicalY, int* windowX, int* windowY);

// submitted and returned text is UTF-8
// see wrapper functions below for cp-437 variants
DFHACK_EXPORT char* DFSDL_GetClipboardText();
Expand All @@ -76,4 +83,7 @@ namespace DFHack
DFHACK_EXPORT bool getClipboardTextCp437Multiline(std::vector<std::string> * lines);
DFHACK_EXPORT bool setClipboardTextCp437Multiline(std::string text);

// Queue a cb to be run on the render thread
DFHACK_EXPORT void runOnRenderThread(std::function<void()> cb);
DFHACK_EXPORT void runRenderThreadCallbacks();
}
42 changes: 42 additions & 0 deletions library/modules/DFSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <SDL_stdinc.h>

#include <vector>

#ifdef WIN32
# include <regex>
#endif
Expand Down Expand Up @@ -61,6 +63,9 @@ SDL_Surface* (*g_SDL_CreateRGBSurfaceWithFormat)(uint32_t flags, int width, int
int (*g_SDL_ShowSimpleMessageBox)(uint32_t flags, const char *title, const char *message, SDL_Window *window) = nullptr;
char* (*g_SDL_GetPrefPath)(const char* org, const char* app) = nullptr;
char* (*g_SDL_GetBasePath)() = nullptr;
uint32_t (*g_SDL_GetMouseState)(int* x, int* y) = nullptr;
void (*g_SDL_RenderWindowToLogical)(SDL_Renderer* renderer, int windowX, int windowY, float* logicalX, float* logicalY);
void (*g_SDL_RenderLogicalToWindow)(SDL_Renderer* renderer, float logicalX, float logicalY, int* windowX, int* windowY);

bool DFSDL::init(color_ostream &out) {
for (auto &lib_str : SDL_LIBS) {
Expand Down Expand Up @@ -106,6 +111,9 @@ bool DFSDL::init(color_ostream &out) {
bind(g_sdl_handle, SDL_ShowSimpleMessageBox);
bind(g_sdl_handle, SDL_GetPrefPath);
bind(g_sdl_handle, SDL_GetBasePath);
bind(g_sdl_handle, SDL_GetMouseState);
bind(g_sdl_handle, SDL_RenderWindowToLogical);
bind(g_sdl_handle, SDL_RenderLogicalToWindow);
#undef bind

DEBUG(dfsdl,out).print("sdl successfully loaded\n");
Expand Down Expand Up @@ -190,6 +198,18 @@ char* DFSDL::DFSDL_GetBasePath()
return g_SDL_GetBasePath();
}

uint32_t DFSDL::DFSDL_GetMouseState(int* x, int* y) {
return g_SDL_GetMouseState(x, y);
}

void DFSDL::DFSDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY, float *logicalX, float *logicalY) {
g_SDL_RenderWindowToLogical(renderer, windowX, windowY, logicalX, logicalY);
}

void DFSDL::DFSDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, int *windowX, int *windowY) {
g_SDL_RenderLogicalToWindow(renderer, logicalX, logicalY, windowX, windowY);
}

int DFSDL::DFSDL_ShowSimpleMessageBox(uint32_t flags, const char *title, const char *message, SDL_Window *window) {
if (!g_SDL_ShowSimpleMessageBox)
return -1;
Expand Down Expand Up @@ -266,3 +286,25 @@ DFHACK_EXPORT bool DFHack::setClipboardTextCp437Multiline(string text) {
}
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(str.str().c_str());
}

// Queue to run callbacks on the render thread.
// Semantics loosely based on SDL3's SDL_RunOnMainThread
static std::recursive_mutex render_cb_lock;
static std::vector<std::function<void()>> render_cb_queue;

DFHACK_EXPORT void DFHack::runOnRenderThread(std::function<void()> cb) {
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
render_cb_queue.push_back(std::move(cb));
}

DFHACK_EXPORT void DFHack::runRenderThreadCallbacks() {
static decltype(render_cb_queue) local_queue;
{
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
std::swap(local_queue, render_cb_queue);
}
for (auto& cb : local_queue) {
cb();
}
local_queue.clear();
}
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ if(BUILD_SUPPORTED)
dfhack_plugin(dwarfvet dwarfvet.cpp LINK_LIBRARIES lua)
#dfhack_plugin(dwarfmonitor dwarfmonitor.cpp LINK_LIBRARIES lua)
#add_subdirectory(embark-assistant)
dfhack_plugin(edgescroll edgescroll.cpp)
dfhack_plugin(eventful eventful.cpp LINK_LIBRARIES lua)
dfhack_plugin(fastdwarf fastdwarf.cpp)
dfhack_plugin(filltraffic filltraffic.cpp)
Expand Down
Loading