Add a function to find viewscreens (or parents) of a given type

This makes it possible for some plugins to detect the game state more
reliably - for example, prospector would previously fail when
embarking if the viewscreen_choose_start_sitest instance had any
children.
develop
lethosor 2015-10-02 21:50:55 -04:00
parent 60bc2619a1
commit d343dfd8a0
4 changed files with 28 additions and 1 deletions

@ -114,6 +114,7 @@ DFHack Future
plug:
- lists all plugins
- shows state and number of commands in plugins
prospect: works from within command-prompt
quicksave: Restricted to fortress mode
remotefortressreader: Exposes more information
search:

@ -156,6 +156,15 @@ namespace DFHack
/// Get the current top-level view-screen
DFHACK_EXPORT df::viewscreen *getCurViewscreen(bool skip_dismissed = false);
DFHACK_EXPORT df::viewscreen *getViewscreenByIdentity(virtual_identity id, int n = 1);
/// Get the top-most viewscreen of the given type from the top `n` viewscreens (or all viewscreens if n < 1)
/// returns NULL if none match
template <typename T>
inline T *getViewscreenByType (int n = 1) {
return strict_virtual_cast<T>(getViewscreenByIdentity(T::_identity, n));
}
inline std::string getCurFocus(bool skip_dismissed = false) {
return getFocusString(getCurViewscreen(skip_dismissed));
}

@ -1326,6 +1326,21 @@ df::viewscreen *Gui::getCurViewscreen(bool skip_dismissed)
return ws;
}
df::viewscreen *Gui::getViewscreenByIdentity (virtual_identity id, int n)
{
bool limit = (n > 0);
df::viewscreen *screen = Gui::getCurViewscreen();
while (screen)
{
if (limit && n-- <= 0)
break;
if (id.is_instance(screen))
return screen;
screen = screen->parent;
}
return NULL;
}
df::coord Gui::getViewportPos()
{
if (!df::global::window_x || !df::global::window_y || !df::global::window_z)

@ -18,6 +18,7 @@ using namespace std;
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/Gui.h"
#include "modules/MapCache.h"
#include "MiscUtils.h"
@ -575,7 +576,8 @@ command_result prospector (color_ostream &con, vector <string> & parameters)
CoreSuspender suspend;
// Embark screen active: estimate using world geology data
if (VIRTUAL_CAST_VAR(screen, df::viewscreen_choose_start_sitest, Core::getTopViewscreen()))
auto screen = Gui::getViewscreenByType<df::viewscreen_choose_start_sitest>(0);
if (screen)
return embark_prospector(con, screen, showHidden, showValue);
if (!Maps::IsValid())