2012-08-18 23:31:09 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Internal.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2012-10-02 03:49:31 -06:00
|
|
|
#include <set>
|
2012-08-18 23:31:09 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2022-09-18 09:07:08 -06:00
|
|
|
#include "modules/Renderer.h"
|
2012-08-18 23:31:09 -06:00
|
|
|
#include "modules/Screen.h"
|
2015-11-13 18:10:29 -07:00
|
|
|
#include "modules/GuiHooks.h"
|
2022-12-20 21:10:14 -07:00
|
|
|
#include "Debug.h"
|
2012-08-18 23:31:09 -06:00
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "Types.h"
|
|
|
|
#include "Error.h"
|
|
|
|
#include "ModuleFactory.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "PluginManager.h"
|
2012-08-19 04:27:44 -06:00
|
|
|
#include "LuaTools.h"
|
|
|
|
|
2012-08-18 23:31:09 -06:00
|
|
|
#include "MiscUtils.h"
|
2012-08-19 04:27:44 -06:00
|
|
|
|
2012-08-18 23:31:09 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/init.h"
|
2022-12-22 22:35:58 -07:00
|
|
|
#include "df/texture_handlerst.h"
|
|
|
|
#include "df/tile_pagest.h"
|
2012-08-18 23:31:09 -06:00
|
|
|
#include "df/interfacest.h"
|
2012-08-19 04:27:44 -06:00
|
|
|
#include "df/enabler.h"
|
2022-12-27 15:39:03 -07:00
|
|
|
#include "df/graphic_viewportst.h"
|
2012-09-20 01:11:20 -06:00
|
|
|
#include "df/unit.h"
|
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/building.h"
|
2012-10-30 00:38:32 -06:00
|
|
|
#include "df/renderer.h"
|
2017-05-05 12:45:46 -06:00
|
|
|
#include "df/plant.h"
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
using namespace df::enums;
|
|
|
|
using df::global::init;
|
|
|
|
using df::global::gps;
|
|
|
|
using df::global::texture;
|
|
|
|
using df::global::gview;
|
2012-08-19 04:27:44 -06:00
|
|
|
using df::global::enabler;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
using Screen::Pen;
|
2015-03-27 20:56:20 -06:00
|
|
|
using Screen::PenArray;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
2012-10-02 03:49:31 -06:00
|
|
|
using std::string;
|
|
|
|
|
2022-12-20 21:10:14 -07:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_DECLARE(core, screen, DebugCategory::LINFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
/*
|
|
|
|
* Screen painting API.
|
|
|
|
*/
|
|
|
|
|
2022-12-20 12:00:49 -07:00
|
|
|
// returns ui grid coordinates, even if the game map is scaled differently
|
2012-08-18 23:31:09 -06:00
|
|
|
df::coord2d Screen::getMousePos()
|
|
|
|
{
|
2022-12-20 12:00:49 -07:00
|
|
|
if (!gps)
|
2012-08-19 10:00:10 -06:00
|
|
|
return df::coord2d(-1, -1);
|
2022-12-21 15:07:36 -07:00
|
|
|
return df::coord2d(gps->mouse_x, gps->mouse_y);
|
2022-12-16 12:53:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the screen pixel coordinates
|
|
|
|
df::coord2d Screen::getMousePixels()
|
|
|
|
{
|
2022-12-20 12:00:49 -07:00
|
|
|
if (!gps)
|
2022-12-16 12:53:44 -07:00
|
|
|
return df::coord2d(-1, -1);
|
2022-12-21 15:07:36 -07:00
|
|
|
return df::coord2d(gps->precise_mouse_x, gps->precise_mouse_y);
|
2012-08-18 23:31:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
df::coord2d Screen::getWindowSize()
|
|
|
|
{
|
2012-08-19 10:00:10 -06:00
|
|
|
if (!gps) return df::coord2d(80, 25);
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
return df::coord2d(gps->dimx, gps->dimy);
|
|
|
|
}
|
|
|
|
|
2018-06-30 12:12:29 -06:00
|
|
|
void Screen::zoom(df::zoom_commands cmd) {
|
|
|
|
enabler->zoom_display(cmd);
|
|
|
|
}
|
|
|
|
|
2012-08-19 10:00:10 -06:00
|
|
|
bool Screen::inGraphicsMode()
|
|
|
|
{
|
|
|
|
return init && init->display.flag.is_set(init_display_flags::USE_GRAPHICS);
|
|
|
|
}
|
|
|
|
|
2022-12-27 18:00:50 -07:00
|
|
|
static bool doSetTile_map(const Pen &pen, int x, int y) {
|
2023-01-10 20:40:13 -07:00
|
|
|
auto &vp = gps->main_viewport;
|
2022-12-31 22:00:44 -07:00
|
|
|
|
2023-01-10 20:40:13 -07:00
|
|
|
if (x < 0 || x >= vp->dim_x || y < 0 || y >= vp->dim_y)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t max_index = vp->dim_x * vp->dim_y - 1;
|
|
|
|
size_t index = (x * vp->dim_y) + y;
|
|
|
|
|
|
|
|
if (index > max_index)
|
2022-12-31 22:00:44 -07:00
|
|
|
return false;
|
|
|
|
|
2022-12-27 18:00:50 -07:00
|
|
|
long texpos = pen.tile;
|
|
|
|
if (texpos == 0) {
|
|
|
|
texpos = init->font.large_font_texpos[(uint8_t)pen.ch];
|
|
|
|
}
|
2023-01-10 20:40:13 -07:00
|
|
|
vp->screentexpos_interface[index] = texpos;
|
2022-12-27 18:00:50 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:40:14 -06:00
|
|
|
static bool doSetTile_default(const Pen &pen, int x, int y, bool map)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
2022-12-27 18:00:50 -07:00
|
|
|
bool use_graphics = Screen::inGraphicsMode();
|
|
|
|
|
|
|
|
if (map && use_graphics)
|
|
|
|
return doSetTile_map(pen, x, y);
|
|
|
|
|
2023-01-10 20:40:13 -07:00
|
|
|
if (x < 0 || x >= gps->dimx || y < 0 || y >= gps->dimy)
|
|
|
|
return false;
|
|
|
|
|
2022-12-27 16:05:13 -07:00
|
|
|
size_t index = (x * gps->dimy) + y;
|
|
|
|
uint8_t *screen = &gps->screen[index * 8];
|
|
|
|
|
|
|
|
if (screen > gps->screen_limit)
|
2022-12-27 15:39:03 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
long *texpos = &gps->screentexpos[index];
|
2022-12-27 16:05:13 -07:00
|
|
|
long *texpos_lower = &gps->screentexpos_lower[index];
|
2022-12-27 15:39:03 -07:00
|
|
|
uint32_t *flag = &gps->screentexpos_flag[index];
|
|
|
|
|
|
|
|
*screen = 0;
|
|
|
|
*texpos = 0;
|
2022-12-27 16:05:13 -07:00
|
|
|
*texpos_lower = 0;
|
2023-01-01 01:45:14 -07:00
|
|
|
gps->screentexpos_anchored[index] = 0;
|
2022-12-30 01:53:44 -07:00
|
|
|
// keep SCREENTEXPOS_FLAG_ANCHOR_SUBORDINATE so occluded anchored textures
|
|
|
|
// don't appear corrupted
|
|
|
|
*flag &= 4;
|
2022-12-27 15:39:03 -07:00
|
|
|
|
|
|
|
if (gps->top_in_use) {
|
|
|
|
screen = &gps->screen_top[index * 8];
|
|
|
|
texpos = &gps->screentexpos_top[index];
|
2022-12-27 16:05:13 -07:00
|
|
|
texpos_lower = &gps->screentexpos_top_lower[index];
|
2022-12-27 15:39:03 -07:00
|
|
|
flag = &gps->screentexpos_top_flag[index];
|
|
|
|
|
|
|
|
*screen = 0;
|
|
|
|
*texpos = 0;
|
2022-12-27 16:05:13 -07:00
|
|
|
*texpos_lower = 0;
|
2023-01-01 01:45:14 -07:00
|
|
|
gps->screentexpos_top_anchored[index] = 0;
|
2022-12-30 01:53:44 -07:00
|
|
|
*flag &= 4; // keep SCREENTEXPOS_FLAG_ANCHOR_SUBORDINATE
|
2022-12-20 17:33:11 -07:00
|
|
|
}
|
2022-12-27 15:39:03 -07:00
|
|
|
|
2022-12-30 06:35:04 -07:00
|
|
|
uint8_t fg = pen.fg | (pen.bold << 3);
|
|
|
|
uint8_t bg = pen.bg;
|
|
|
|
|
2022-12-27 15:39:03 -07:00
|
|
|
if (pen.tile_mode == Screen::Pen::CharColor)
|
|
|
|
*flag |= 2; // SCREENTEXPOS_FLAG_ADDCOLOR
|
2022-12-30 06:35:04 -07:00
|
|
|
else if (pen.tile_mode == Screen::Pen::TileColor) {
|
2022-12-27 15:39:03 -07:00
|
|
|
*flag |= 1; // SCREENTEXPOS_FLAG_GRAYSCALE
|
2022-12-30 06:35:04 -07:00
|
|
|
if (pen.tile_fg)
|
|
|
|
fg = pen.tile_fg;
|
|
|
|
if (pen.tile_bg)
|
|
|
|
bg = pen.tile_bg;
|
|
|
|
}
|
2022-12-27 15:39:03 -07:00
|
|
|
|
2022-12-27 18:00:50 -07:00
|
|
|
if (pen.tile && use_graphics) {
|
2022-12-27 15:39:03 -07:00
|
|
|
*texpos = pen.tile;
|
|
|
|
} else {
|
|
|
|
screen[0] = uint8_t(pen.ch);
|
2022-12-27 16:05:13 -07:00
|
|
|
*texpos_lower = 909;
|
2022-12-27 15:39:03 -07:00
|
|
|
}
|
|
|
|
|
2022-12-30 06:35:04 -07:00
|
|
|
auto rgb_fg = &gps->uccolor[fg][0];
|
|
|
|
auto rgb_bg = &gps->uccolor[bg][0];
|
|
|
|
screen[1] = rgb_fg[0];
|
|
|
|
screen[2] = rgb_fg[1];
|
|
|
|
screen[3] = rgb_fg[2];
|
|
|
|
screen[4] = rgb_bg[0];
|
|
|
|
screen[5] = rgb_bg[1];
|
|
|
|
screen[6] = rgb_bg[2];
|
2022-12-27 15:39:03 -07:00
|
|
|
|
2017-06-01 22:40:14 -06:00
|
|
|
return true;
|
2012-08-18 23:31:09 -06:00
|
|
|
}
|
|
|
|
|
2015-11-14 12:14:32 -07:00
|
|
|
GUI_HOOK_DEFINE(Screen::Hooks::set_tile, doSetTile_default);
|
2017-06-01 22:40:14 -06:00
|
|
|
static bool doSetTile(const Pen &pen, int x, int y, bool map)
|
2015-11-13 18:10:29 -07:00
|
|
|
{
|
2017-06-02 19:22:19 -06:00
|
|
|
return GUI_HOOK_TOP(Screen::Hooks::set_tile)(pen, x, y, map);
|
2015-11-13 18:10:29 -07:00
|
|
|
}
|
|
|
|
|
2015-12-22 09:42:51 -07:00
|
|
|
bool Screen::paintTile(const Pen &pen, int x, int y, bool map)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
2023-01-10 20:40:13 -07:00
|
|
|
if (!gps || !pen.valid()) return false;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
2015-12-22 09:42:51 -07:00
|
|
|
doSetTile(pen, x, y, map);
|
2012-08-18 23:31:09 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-31 22:00:44 -07:00
|
|
|
static Pen doGetTile_map(int x, int y) {
|
2023-01-10 20:40:13 -07:00
|
|
|
auto &vp = gps->main_viewport;
|
|
|
|
|
|
|
|
if (x < 0 || x >= vp->dim_x || y < 0 || y >= vp->dim_y)
|
|
|
|
return Pen(0, 0, 0, -1);
|
|
|
|
|
|
|
|
size_t max_index = vp->dim_x * vp->dim_y - 1;
|
|
|
|
size_t index = (x * vp->dim_y) + y;
|
2022-12-31 22:00:44 -07:00
|
|
|
|
|
|
|
if (index < 0 || index > max_index)
|
|
|
|
return Pen(0, 0, 0, -1);
|
|
|
|
|
2023-01-10 20:40:13 -07:00
|
|
|
int tile = vp->screentexpos[index];
|
2022-12-31 22:00:44 -07:00
|
|
|
if (tile == 0)
|
2023-01-10 20:40:13 -07:00
|
|
|
tile = vp->screentexpos_item[index];
|
2022-12-31 22:00:44 -07:00
|
|
|
if (tile == 0)
|
2023-01-10 20:40:13 -07:00
|
|
|
tile = vp->screentexpos_building_one[index];
|
2022-12-31 22:00:44 -07:00
|
|
|
if (tile == 0)
|
2023-01-10 20:40:13 -07:00
|
|
|
tile = vp->screentexpos_background_two[index];
|
2022-12-31 22:00:44 -07:00
|
|
|
if (tile == 0)
|
2023-01-10 20:40:13 -07:00
|
|
|
tile = vp->screentexpos_background[index];
|
2022-12-31 22:00:44 -07:00
|
|
|
|
|
|
|
char ch = 0;
|
|
|
|
uint8_t fg = 0;
|
|
|
|
uint8_t bg = 0;
|
|
|
|
return Pen(ch, fg, bg, tile, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t to_16_bit_color(uint8_t *rgb) {
|
|
|
|
for (uint8_t c = 0; c < 16; ++c) {
|
|
|
|
if (rgb[0] == gps->uccolor[c][0] &&
|
|
|
|
rgb[1] == gps->uccolor[c][1] &&
|
|
|
|
rgb[2] == gps->uccolor[c][2]) {
|
|
|
|
return c;
|
2012-09-07 01:36:45 -06:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 22:00:44 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pen doGetTile_default(int x, int y, bool map) {
|
|
|
|
bool use_graphics = Screen::inGraphicsMode();
|
|
|
|
|
|
|
|
if (map && use_graphics)
|
|
|
|
return doGetTile_map(x, y);
|
|
|
|
|
2023-01-10 20:40:13 -07:00
|
|
|
if (x < 0 || x >= gps->dimx || y < 0 || y >= gps->dimy)
|
|
|
|
return Pen(0, 0, 0, -1);
|
|
|
|
|
2022-12-31 22:00:44 -07:00
|
|
|
size_t index = (x * gps->dimy) + y;
|
|
|
|
uint8_t *screen = &gps->screen[index * 8];
|
|
|
|
|
|
|
|
if (screen > gps->screen_limit)
|
|
|
|
return Pen(0, 0, 0, -1);
|
|
|
|
|
|
|
|
long *texpos = &gps->screentexpos[index];
|
|
|
|
uint32_t *flag = &gps->screentexpos_flag[index];
|
|
|
|
|
|
|
|
if (gps->top_in_use &&
|
|
|
|
(gps->screen_top[index * 8] ||
|
|
|
|
(use_graphics && gps->screentexpos_top[index]))) {
|
|
|
|
screen = &gps->screen_top[index * 8];
|
|
|
|
texpos = &gps->screentexpos_top[index];
|
|
|
|
flag = &gps->screentexpos_top_flag[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
char ch = *screen;
|
|
|
|
uint8_t fg = to_16_bit_color(&screen[1]);
|
|
|
|
uint8_t bg = to_16_bit_color(&screen[4]);
|
|
|
|
int tile = 0;
|
|
|
|
if (use_graphics)
|
|
|
|
tile = *texpos;
|
|
|
|
|
|
|
|
if (*flag & 1) {
|
|
|
|
// TileColor
|
|
|
|
return Pen(ch, fg&7, bg, !!(fg&8), tile, fg, bg);
|
|
|
|
} else if (*flag & 2) {
|
|
|
|
// CharColor
|
|
|
|
return Pen(ch, fg, bg, tile, true);
|
|
|
|
}
|
2012-09-07 01:36:45 -06:00
|
|
|
|
2022-12-31 22:00:44 -07:00
|
|
|
// AsIs
|
|
|
|
return Pen(ch, fg, bg, tile, false);
|
2012-09-07 01:36:45 -06:00
|
|
|
}
|
|
|
|
|
2017-05-31 10:12:15 -06:00
|
|
|
GUI_HOOK_DEFINE(Screen::Hooks::get_tile, doGetTile_default);
|
|
|
|
static Pen doGetTile(int x, int y, bool map)
|
|
|
|
{
|
|
|
|
return GUI_HOOK_TOP(Screen::Hooks::get_tile)(x, y, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
Pen Screen::readTile(int x, int y, bool map)
|
|
|
|
{
|
2023-01-10 20:40:13 -07:00
|
|
|
if (!gps) return Pen(0,0,0,-1);
|
2017-05-31 10:12:15 -06:00
|
|
|
|
|
|
|
return doGetTile(x, y, map);
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:42:51 -07:00
|
|
|
bool Screen::paintString(const Pen &pen, int x, int y, const std::string &text, bool map)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
2012-11-23 18:18:56 -07:00
|
|
|
auto dim = getWindowSize();
|
|
|
|
if (!gps || y < 0 || y >= dim.y) return false;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
Pen tmp(pen);
|
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
for (size_t i = -std::min(0,x); i < text.size(); i++)
|
|
|
|
{
|
2012-11-23 18:18:56 -07:00
|
|
|
if (x + i >= size_t(dim.x))
|
2012-08-18 23:31:09 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
tmp.ch = text[i];
|
|
|
|
tmp.tile = (pen.tile ? pen.tile + uint8_t(text[i]) : 0);
|
2015-12-22 09:42:51 -07:00
|
|
|
paintTile(tmp, x+i, y, map);
|
2012-08-18 23:31:09 -06:00
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:42:51 -07:00
|
|
|
bool Screen::fillRect(const Pen &pen, int x1, int y1, int x2, int y2, bool map)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
2012-11-23 18:18:56 -07:00
|
|
|
auto dim = getWindowSize();
|
2012-09-07 01:36:45 -06:00
|
|
|
if (!gps || !pen.valid()) return false;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
|
|
|
if (x1 < 0) x1 = 0;
|
|
|
|
if (y1 < 0) y1 = 0;
|
2012-11-23 18:18:56 -07:00
|
|
|
if (x2 >= dim.x) x2 = dim.x-1;
|
|
|
|
if (y2 >= dim.y) y2 = dim.y-1;
|
2012-08-18 23:31:09 -06:00
|
|
|
if (x1 > x2 || y1 > y2) return false;
|
|
|
|
|
|
|
|
for (int x = x1; x <= x2; x++)
|
|
|
|
{
|
|
|
|
for (int y = y1; y <= y2; y++)
|
2015-12-22 09:42:51 -07:00
|
|
|
doSetTile(pen, x, y, map);
|
2012-08-18 23:31:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-21 14:28:11 -06:00
|
|
|
bool Screen::drawBorder(const std::string &title)
|
|
|
|
{
|
|
|
|
if (!gps) return false;
|
|
|
|
|
2012-11-23 18:18:56 -07:00
|
|
|
auto dim = getWindowSize();
|
2012-09-29 02:47:41 -06:00
|
|
|
Pen border('\xDB', 8);
|
2012-08-21 14:28:11 -06:00
|
|
|
Pen text(0, 0, 7);
|
2012-08-22 02:28:01 -06:00
|
|
|
Pen signature(0, 0, 8);
|
2012-08-21 14:28:11 -06:00
|
|
|
|
2012-11-23 18:18:56 -07:00
|
|
|
for (int x = 0; x < dim.x; x++)
|
2012-08-21 14:28:11 -06:00
|
|
|
{
|
2015-11-13 18:10:29 -07:00
|
|
|
doSetTile(border, x, 0, false);
|
|
|
|
doSetTile(border, x, dim.y - 1, false);
|
2012-08-21 14:28:11 -06:00
|
|
|
}
|
2012-11-23 18:18:56 -07:00
|
|
|
for (int y = 0; y < dim.y; y++)
|
2012-08-21 14:28:11 -06:00
|
|
|
{
|
2015-11-13 18:10:29 -07:00
|
|
|
doSetTile(border, 0, y, false);
|
2015-11-14 11:03:38 -07:00
|
|
|
doSetTile(border, dim.x - 1, y, false);
|
2012-08-21 14:28:11 -06:00
|
|
|
}
|
2012-08-22 02:28:01 -06:00
|
|
|
|
2012-11-23 18:18:56 -07:00
|
|
|
paintString(signature, dim.x-8, dim.y-1, "DFHack");
|
2012-08-22 02:28:01 -06:00
|
|
|
|
2012-11-23 18:18:56 -07:00
|
|
|
return paintString(text, (dim.x - title.length()) / 2, 0, title);
|
2012-08-21 14:28:11 -06:00
|
|
|
}
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
bool Screen::clear()
|
|
|
|
{
|
|
|
|
if (!gps) return false;
|
|
|
|
|
2012-11-23 18:18:56 -07:00
|
|
|
auto dim = getWindowSize();
|
|
|
|
return fillRect(Pen(' ',0,0,false), 0, 0, dim.x-1, dim.y-1);
|
2012-08-19 04:27:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Screen::invalidate()
|
|
|
|
{
|
|
|
|
if (!enabler) return false;
|
|
|
|
|
|
|
|
enabler->flag.bits.render = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-19 07:11:26 -07:00
|
|
|
const Pen Screen::Painter::default_pen(0,COLOR_GREY,0);
|
|
|
|
const Pen Screen::Painter::default_key_pen(0,COLOR_LIGHTGREEN,0);
|
|
|
|
|
2015-12-22 09:42:51 -07:00
|
|
|
void Screen::Painter::do_paint_string(const std::string &str, const Pen &pen, bool map)
|
2012-11-19 07:11:26 -07:00
|
|
|
{
|
|
|
|
if (gcursor.y < clip.first.y || gcursor.y > clip.second.y)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int dx = std::max(0, int(clip.first.x - gcursor.x));
|
|
|
|
int len = std::min((int)str.size(), int(clip.second.x - gcursor.x + 1));
|
|
|
|
|
|
|
|
if (len > dx)
|
2015-12-22 09:42:51 -07:00
|
|
|
paintString(pen, gcursor.x + dx, gcursor.y, str.substr(dx, len-dx), map);
|
2012-11-19 07:11:26 -07:00
|
|
|
}
|
|
|
|
|
2012-08-18 23:31:09 -06:00
|
|
|
bool Screen::findGraphicsTile(const std::string &pagename, int x, int y, int *ptile, int *pgs)
|
|
|
|
{
|
|
|
|
if (!gps || !texture || x < 0 || y < 0) return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < texture->page.size(); i++)
|
|
|
|
{
|
|
|
|
auto page = texture->page[i];
|
|
|
|
if (!page->loaded || page->token != pagename) continue;
|
|
|
|
|
|
|
|
if (x >= page->page_dim_x || y >= page->page_dim_y)
|
|
|
|
break;
|
|
|
|
int idx = y*page->page_dim_x + x;
|
|
|
|
if (size_t(idx) >= page->texpos.size())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ptile) *ptile = page->texpos[idx];
|
|
|
|
if (pgs) *pgs = page->texpos_gs[idx];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-01 09:15:29 -07:00
|
|
|
static std::map<df::viewscreen*, Plugin*> plugin_screens;
|
|
|
|
|
2018-06-14 08:12:06 -06:00
|
|
|
bool Screen::show(std::unique_ptr<df::viewscreen> screen, df::viewscreen *before, Plugin *plugin)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(screen);
|
|
|
|
CHECK_INVALID_ARGUMENT(!screen->parent && !screen->child);
|
|
|
|
|
|
|
|
if (!gps || !gview) return false;
|
|
|
|
|
|
|
|
df::viewscreen *parent = &gview->view;
|
|
|
|
while (parent && parent->child != before)
|
|
|
|
parent = parent->child;
|
|
|
|
|
|
|
|
if (!parent) return false;
|
|
|
|
|
|
|
|
gps->force_full_display_count += 2;
|
|
|
|
|
|
|
|
screen->child = parent->child;
|
|
|
|
screen->parent = parent;
|
2018-06-14 08:12:06 -06:00
|
|
|
df::viewscreen* s = screen.release();
|
|
|
|
parent->child = s;
|
|
|
|
if (s->child)
|
|
|
|
s->child->parent = s;
|
2012-08-18 23:31:09 -06:00
|
|
|
|
2018-06-14 08:12:06 -06:00
|
|
|
if (dfhack_viewscreen::is_instance(s))
|
|
|
|
static_cast<dfhack_viewscreen*>(s)->onShow();
|
2012-08-24 03:20:08 -06:00
|
|
|
|
2016-01-01 09:15:29 -07:00
|
|
|
if (plugin)
|
2018-06-14 08:12:06 -06:00
|
|
|
plugin_screens[s] = plugin;
|
2016-01-01 09:15:29 -07:00
|
|
|
|
2012-08-18 23:31:09 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-24 03:20:08 -06:00
|
|
|
void Screen::dismiss(df::viewscreen *screen, bool to_first)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(screen);
|
|
|
|
|
2016-01-01 09:15:29 -07:00
|
|
|
auto it = plugin_screens.find(screen);
|
|
|
|
if (it != plugin_screens.end())
|
|
|
|
plugin_screens.erase(it);
|
|
|
|
|
2012-08-24 03:20:08 -06:00
|
|
|
if (screen->breakdown_level != interface_breakdown_types::NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (to_first)
|
|
|
|
screen->breakdown_level = interface_breakdown_types::TOFIRST;
|
|
|
|
else
|
|
|
|
screen->breakdown_level = interface_breakdown_types::STOPSCREEN;
|
|
|
|
|
|
|
|
if (dfhack_viewscreen::is_instance(screen))
|
|
|
|
static_cast<dfhack_viewscreen*>(screen)->onDismiss();
|
2012-08-18 23:31:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Screen::isDismissed(df::viewscreen *screen)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(screen);
|
|
|
|
|
|
|
|
return screen->breakdown_level != interface_breakdown_types::NONE;
|
|
|
|
}
|
|
|
|
|
2016-01-01 09:15:29 -07:00
|
|
|
bool Screen::hasActiveScreens(Plugin *plugin)
|
|
|
|
{
|
|
|
|
if (plugin_screens.empty())
|
|
|
|
return false;
|
|
|
|
df::viewscreen *screen = &gview->view;
|
|
|
|
while (screen)
|
|
|
|
{
|
|
|
|
auto it = plugin_screens.find(screen);
|
|
|
|
if (it != plugin_screens.end() && it->second == plugin)
|
|
|
|
return true;
|
|
|
|
screen = screen->child;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:36:46 -07:00
|
|
|
void Screen::raise(df::viewscreen *screen) {
|
|
|
|
Hide swapper(screen, Screen::Hide::RESTORE_AT_TOP);
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:08:35 -06:00
|
|
|
namespace DFHack { namespace Screen {
|
|
|
|
|
2021-03-21 18:42:50 -06:00
|
|
|
Hide::Hide(df::viewscreen* screen, int flags) :
|
|
|
|
screen{screen}, flags{flags}
|
2018-06-20 11:08:35 -06:00
|
|
|
{
|
2021-03-21 18:42:50 -06:00
|
|
|
extract(screen);
|
2018-06-20 11:08:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Hide::~Hide()
|
|
|
|
{
|
2021-03-21 18:42:50 -06:00
|
|
|
if (screen)
|
|
|
|
merge(screen);
|
2018-06-20 11:08:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hide::extract(df::viewscreen* a)
|
|
|
|
{
|
|
|
|
df::viewscreen* ap = a->parent;
|
|
|
|
df::viewscreen* ac = a->child;
|
|
|
|
|
|
|
|
ap->child = ac;
|
|
|
|
if (ac) ac->parent = ap;
|
|
|
|
else Core::getInstance().top_viewscreen = ap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hide::merge(df::viewscreen* a)
|
|
|
|
{
|
2021-03-21 18:42:50 -06:00
|
|
|
if (flags & RESTORE_AT_TOP) {
|
|
|
|
a->parent = NULL;
|
|
|
|
a->child = NULL;
|
|
|
|
Screen::show(std::unique_ptr<df::viewscreen>(a));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:08:35 -06:00
|
|
|
df::viewscreen* ap = a->parent;
|
|
|
|
df::viewscreen* ac = a->parent->child;
|
|
|
|
|
|
|
|
ap->child = a;
|
|
|
|
a->child = ac;
|
|
|
|
if (ac) ac->parent = a;
|
|
|
|
else Core::getInstance().top_viewscreen = a;
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
|
2012-10-02 03:49:31 -06:00
|
|
|
string Screen::getKeyDisplay(df::interface_key key)
|
|
|
|
{
|
|
|
|
if (enabler)
|
|
|
|
return enabler->GetKeyDisplay(key);
|
|
|
|
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
2014-08-11 04:23:19 -06:00
|
|
|
int Screen::keyToChar(df::interface_key key)
|
|
|
|
{
|
|
|
|
if (key < interface_key::STRING_A000 ||
|
|
|
|
key > interface_key::STRING_A255)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (key < interface_key::STRING_A128)
|
|
|
|
return key - interface_key::STRING_A000;
|
|
|
|
|
|
|
|
return key - interface_key::STRING_A128 + 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
df::interface_key Screen::charToKey(char code)
|
|
|
|
{
|
|
|
|
int val = (unsigned char)code;
|
|
|
|
if (val < 127)
|
|
|
|
return df::interface_key(interface_key::STRING_A000 + val);
|
|
|
|
else if (val == 127)
|
|
|
|
return interface_key::NONE;
|
|
|
|
else
|
|
|
|
return df::interface_key(interface_key::STRING_A128 + (val-128));
|
|
|
|
}
|
|
|
|
|
2015-03-27 20:56:20 -06:00
|
|
|
/*
|
|
|
|
* Pen array
|
|
|
|
*/
|
|
|
|
|
|
|
|
PenArray::PenArray(unsigned int bufwidth, unsigned int bufheight)
|
2015-03-28 09:27:47 -06:00
|
|
|
:dimx(bufwidth), dimy(bufheight), static_alloc(false)
|
2015-03-27 20:56:20 -06:00
|
|
|
{
|
|
|
|
buffer = new Pen[bufwidth * bufheight];
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2015-03-28 09:27:47 -06:00
|
|
|
PenArray::PenArray(unsigned int bufwidth, unsigned int bufheight, void *buf)
|
|
|
|
:dimx(bufwidth), dimy(bufheight), static_alloc(true)
|
|
|
|
{
|
2015-03-28 16:35:07 -06:00
|
|
|
buffer = (Pen*)((PenArray*)buf + 1);
|
2015-03-28 09:27:47 -06:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2015-03-27 20:56:20 -06:00
|
|
|
PenArray::~PenArray()
|
|
|
|
{
|
2015-03-28 09:27:47 -06:00
|
|
|
if (!static_alloc)
|
|
|
|
delete[] buffer;
|
2015-03-27 20:56:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void PenArray::clear()
|
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < dimx; x++)
|
|
|
|
{
|
|
|
|
for (unsigned int y = 0; y < dimy; y++)
|
|
|
|
{
|
|
|
|
set_tile(x, y, Screen::Pen(0, 0, 0, 0, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 16:35:07 -06:00
|
|
|
Pen PenArray::get_tile(unsigned int x, unsigned int y)
|
|
|
|
{
|
|
|
|
if (x < dimx && y < dimy)
|
|
|
|
return buffer[(y * dimx) + x];
|
|
|
|
return Pen(0, 0, 0, 0, false);
|
|
|
|
}
|
|
|
|
|
2015-03-27 20:56:20 -06:00
|
|
|
void PenArray::set_tile(unsigned int x, unsigned int y, Screen::Pen pen)
|
|
|
|
{
|
|
|
|
if (x < dimx && y < dimy)
|
|
|
|
buffer[(y * dimx) + x] = pen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PenArray::draw(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
|
|
|
|
unsigned int bufx, unsigned int bufy)
|
|
|
|
{
|
|
|
|
if (!gps)
|
|
|
|
return;
|
|
|
|
for (unsigned int gridx = x; gridx < x + width; gridx++)
|
|
|
|
{
|
|
|
|
for (unsigned int gridy = y; gridy < y + height; gridy++)
|
|
|
|
{
|
2018-04-05 15:47:47 -06:00
|
|
|
if (gridx >= unsigned(gps->dimx) ||
|
|
|
|
gridy >= unsigned(gps->dimy) ||
|
2015-03-27 20:56:20 -06:00
|
|
|
gridx - x + bufx >= dimx ||
|
|
|
|
gridy - y + bufy >= dimy)
|
|
|
|
continue;
|
|
|
|
Screen::paintTile(buffer[((gridy - y + bufy) * dimx) + (gridx - x + bufx)], gridx, gridy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
/*
|
|
|
|
* Base DFHack viewscreen.
|
|
|
|
*/
|
|
|
|
|
2012-08-18 23:31:09 -06:00
|
|
|
static std::set<df::viewscreen*> dfhack_screens;
|
|
|
|
|
2012-08-22 08:18:19 -06:00
|
|
|
dfhack_viewscreen::dfhack_viewscreen() : text_input_mode(false)
|
2012-08-18 23:31:09 -06:00
|
|
|
{
|
|
|
|
dfhack_screens.insert(this);
|
2012-08-24 03:20:08 -06:00
|
|
|
|
|
|
|
last_size = Screen::getWindowSize();
|
2012-08-18 23:31:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dfhack_viewscreen::~dfhack_viewscreen()
|
|
|
|
{
|
|
|
|
dfhack_screens.erase(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dfhack_viewscreen::is_instance(df::viewscreen *screen)
|
|
|
|
{
|
|
|
|
return dfhack_screens.count(screen) != 0;
|
|
|
|
}
|
2012-08-19 04:27:44 -06:00
|
|
|
|
2012-09-20 01:11:20 -06:00
|
|
|
dfhack_viewscreen *dfhack_viewscreen::try_cast(df::viewscreen *screen)
|
|
|
|
{
|
|
|
|
return is_instance(screen) ? static_cast<dfhack_viewscreen*>(screen) : NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:18:19 -06:00
|
|
|
void dfhack_viewscreen::check_resize()
|
|
|
|
{
|
|
|
|
auto size = Screen::getWindowSize();
|
|
|
|
|
|
|
|
if (size != last_size)
|
|
|
|
{
|
|
|
|
last_size = size;
|
|
|
|
resize(size.x, size.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_viewscreen::logic()
|
|
|
|
{
|
|
|
|
check_resize();
|
|
|
|
|
|
|
|
// Various stuff works poorly unless always repainting
|
|
|
|
Screen::invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_viewscreen::render()
|
|
|
|
{
|
|
|
|
check_resize();
|
|
|
|
}
|
|
|
|
|
2012-08-22 02:23:56 -06:00
|
|
|
bool dfhack_viewscreen::key_conflict(df::interface_key key)
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
if (key == interface_key::OPTIONS)
|
|
|
|
return true;
|
|
|
|
|
2022-12-20 21:10:14 -07:00
|
|
|
/* TODO: understand how this changes for v50
|
2012-08-22 08:18:19 -06:00
|
|
|
if (text_input_mode)
|
|
|
|
{
|
|
|
|
if (key == interface_key::HELP || key == interface_key::MOVIES)
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-20 21:10:14 -07:00
|
|
|
*/
|
2012-08-22 08:18:19 -06:00
|
|
|
|
|
|
|
return false;
|
2012-08-22 02:23:56 -06:00
|
|
|
}
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
/*
|
|
|
|
* Lua-backed viewscreen.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int DFHACK_LUA_VS_TOKEN = 0;
|
|
|
|
|
|
|
|
df::viewscreen *dfhack_lua_viewscreen::get_pointer(lua_State *L, int idx, bool make)
|
|
|
|
{
|
|
|
|
df::viewscreen *screen;
|
|
|
|
|
|
|
|
if (lua_istable(L, idx))
|
|
|
|
{
|
|
|
|
if (!Lua::IsCoreContext(L))
|
|
|
|
luaL_error(L, "only the core context can create lua screens");
|
|
|
|
|
|
|
|
lua_rawgetp(L, idx, &DFHACK_LUA_VS_TOKEN);
|
|
|
|
|
|
|
|
if (!lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
if (make)
|
|
|
|
luaL_error(L, "this screen is already on display");
|
|
|
|
|
|
|
|
screen = (df::viewscreen*)lua_touserdata(L, -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!make)
|
|
|
|
luaL_error(L, "this screen is not on display");
|
|
|
|
|
|
|
|
screen = new dfhack_lua_viewscreen(L, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
screen = Lua::CheckDFObject<df::viewscreen>(L, idx);
|
|
|
|
|
|
|
|
return screen;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dfhack_lua_viewscreen::safe_call_lua(int (*pf)(lua_State *), int args, int rvs)
|
|
|
|
{
|
|
|
|
CoreSuspendClaimer suspend;
|
|
|
|
color_ostream_proxy out(Core::getInstance().getConsole());
|
|
|
|
|
|
|
|
auto L = Lua::Core::State;
|
|
|
|
lua_pushcfunction(L, pf);
|
|
|
|
if (args > 0) lua_insert(L, -args-1);
|
|
|
|
lua_pushlightuserdata(L, this);
|
|
|
|
if (args > 0) lua_insert(L, -args-1);
|
|
|
|
|
|
|
|
return Lua::Core::SafeCall(out, args+1, rvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
dfhack_lua_viewscreen *dfhack_lua_viewscreen::get_self(lua_State *L)
|
|
|
|
{
|
|
|
|
auto self = (dfhack_lua_viewscreen*)lua_touserdata(L, 1);
|
|
|
|
lua_rawgetp(L, LUA_REGISTRYINDEX, self);
|
|
|
|
if (!lua_istable(L, -1)) return NULL;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfhack_lua_viewscreen::do_destroy(lua_State *L)
|
|
|
|
{
|
|
|
|
auto self = get_self(L);
|
|
|
|
if (!self) return 0;
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_rawsetp(L, LUA_REGISTRYINDEX, self);
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_rawsetp(L, -2, &DFHACK_LUA_VS_TOKEN);
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_setfield(L, -2, "_native");
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "onDestroy");
|
2012-08-19 07:53:25 -06:00
|
|
|
if (lua_isnil(L, -1))
|
|
|
|
return 0;
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::update_focus(lua_State *L, int idx)
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
lua_getfield(L, idx, "text_input_mode");
|
|
|
|
text_input_mode = lua_toboolean(L, -1);
|
|
|
|
lua_pop(L, 1);
|
2015-06-05 16:45:52 -06:00
|
|
|
lua_getfield(L, idx, "allow_options");
|
|
|
|
allow_options = lua_toboolean(L, -1);
|
|
|
|
lua_pop(L, 1);
|
2012-08-22 08:18:19 -06:00
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
lua_getfield(L, idx, "focus_path");
|
|
|
|
auto str = lua_tostring(L, -1);
|
|
|
|
if (!str) str = "";
|
|
|
|
focus = str;
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
if (focus.empty())
|
|
|
|
focus = "lua";
|
|
|
|
else
|
|
|
|
focus = "lua/"+focus;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfhack_lua_viewscreen::do_render(lua_State *L)
|
|
|
|
{
|
|
|
|
auto self = get_self(L);
|
|
|
|
if (!self) return 0;
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "onRender");
|
|
|
|
|
|
|
|
if (lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
Screen::clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfhack_lua_viewscreen::do_notify(lua_State *L)
|
|
|
|
{
|
|
|
|
int args = lua_gettop(L);
|
|
|
|
|
|
|
|
auto self = get_self(L);
|
|
|
|
if (!self) return 0;
|
|
|
|
|
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
if (lua_isnil(L, -1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// self field args table fn -> table fn table args
|
|
|
|
lua_replace(L, 1);
|
|
|
|
lua_copy(L, -1, 2);
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, args-1, 1);
|
|
|
|
|
|
|
|
self->update_focus(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfhack_lua_viewscreen::do_input(lua_State *L)
|
|
|
|
{
|
|
|
|
auto self = get_self(L);
|
|
|
|
if (!self) return 0;
|
|
|
|
|
|
|
|
auto keys = (std::set<df::interface_key>*)lua_touserdata(L, 2);
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "onInput");
|
|
|
|
|
|
|
|
if (lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
if (keys->count(interface_key::LEAVESCREEN))
|
|
|
|
Screen::dismiss(self);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushvalue(L, -2);
|
2022-10-31 12:43:37 -06:00
|
|
|
Lua::PushInterfaceKeys(L, *keys);
|
2012-08-19 04:27:44 -06:00
|
|
|
|
|
|
|
lua_call(L, 2, 0);
|
|
|
|
self->update_focus(L, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfhack_lua_viewscreen::dfhack_lua_viewscreen(lua_State *L, int table_idx)
|
|
|
|
{
|
|
|
|
assert(Lua::IsCoreContext(L));
|
|
|
|
|
|
|
|
Lua::PushDFObject(L, (df::viewscreen*)this);
|
|
|
|
lua_setfield(L, table_idx, "_native");
|
|
|
|
lua_pushlightuserdata(L, this);
|
|
|
|
lua_rawsetp(L, table_idx, &DFHACK_LUA_VS_TOKEN);
|
|
|
|
|
|
|
|
lua_pushvalue(L, table_idx);
|
|
|
|
lua_rawsetp(L, LUA_REGISTRYINDEX, this);
|
|
|
|
|
|
|
|
update_focus(L, table_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
dfhack_lua_viewscreen::~dfhack_lua_viewscreen()
|
|
|
|
{
|
|
|
|
safe_call_lua(do_destroy, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::render()
|
|
|
|
{
|
2012-10-25 02:09:39 -06:00
|
|
|
if (Screen::isDismissed(this))
|
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
parent->render();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 08:18:19 -06:00
|
|
|
|
|
|
|
dfhack_viewscreen::render();
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
safe_call_lua(do_render, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::logic()
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
if (Screen::isDismissed(this)) return;
|
|
|
|
|
|
|
|
dfhack_viewscreen::logic();
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
lua_pushstring(Lua::Core::State, "onIdle");
|
|
|
|
safe_call_lua(do_notify, 1, 0);
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:45:52 -06:00
|
|
|
bool dfhack_lua_viewscreen::key_conflict(df::interface_key key)
|
|
|
|
{
|
|
|
|
if (key == df::interface_key::OPTIONS)
|
|
|
|
return !allow_options;
|
|
|
|
return dfhack_viewscreen::key_conflict(key);
|
|
|
|
}
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
void dfhack_lua_viewscreen::help()
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
if (Screen::isDismissed(this)) return;
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
lua_pushstring(Lua::Core::State, "onHelp");
|
|
|
|
safe_call_lua(do_notify, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::resize(int w, int h)
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
if (Screen::isDismissed(this)) return;
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
auto L = Lua::Core::State;
|
|
|
|
lua_pushstring(L, "onResize");
|
|
|
|
lua_pushinteger(L, w);
|
|
|
|
lua_pushinteger(L, h);
|
|
|
|
safe_call_lua(do_notify, 3, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::feed(std::set<df::interface_key> *keys)
|
|
|
|
{
|
2012-08-22 08:18:19 -06:00
|
|
|
if (Screen::isDismissed(this)) return;
|
|
|
|
|
2012-08-19 04:27:44 -06:00
|
|
|
lua_pushlightuserdata(Lua::Core::State, keys);
|
|
|
|
safe_call_lua(do_input, 1, 0);
|
|
|
|
}
|
2012-08-24 03:20:08 -06:00
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::onShow()
|
|
|
|
{
|
|
|
|
lua_pushstring(Lua::Core::State, "onShow");
|
|
|
|
safe_call_lua(do_notify, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfhack_lua_viewscreen::onDismiss()
|
|
|
|
{
|
|
|
|
lua_pushstring(Lua::Core::State, "onDismiss");
|
|
|
|
safe_call_lua(do_notify, 1, 0);
|
|
|
|
}
|
2012-09-20 01:11:20 -06:00
|
|
|
|
|
|
|
df::unit *dfhack_lua_viewscreen::getSelectedUnit()
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder frame(Lua::Core::State);
|
|
|
|
lua_pushstring(Lua::Core::State, "onGetSelectedUnit");
|
|
|
|
safe_call_lua(do_notify, 1, 1);
|
|
|
|
return Lua::GetDFObject<df::unit>(Lua::Core::State, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
df::item *dfhack_lua_viewscreen::getSelectedItem()
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder frame(Lua::Core::State);
|
|
|
|
lua_pushstring(Lua::Core::State, "onGetSelectedItem");
|
|
|
|
safe_call_lua(do_notify, 1, 1);
|
|
|
|
return Lua::GetDFObject<df::item>(Lua::Core::State, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
df::job *dfhack_lua_viewscreen::getSelectedJob()
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder frame(Lua::Core::State);
|
|
|
|
lua_pushstring(Lua::Core::State, "onGetSelectedJob");
|
|
|
|
safe_call_lua(do_notify, 1, 1);
|
|
|
|
return Lua::GetDFObject<df::job>(Lua::Core::State, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
df::building *dfhack_lua_viewscreen::getSelectedBuilding()
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder frame(Lua::Core::State);
|
|
|
|
lua_pushstring(Lua::Core::State, "onGetSelectedBuilding");
|
|
|
|
safe_call_lua(do_notify, 1, 1);
|
|
|
|
return Lua::GetDFObject<df::building>(Lua::Core::State, -1);
|
|
|
|
}
|
2017-05-05 12:45:46 -06:00
|
|
|
|
|
|
|
df::plant *dfhack_lua_viewscreen::getSelectedPlant()
|
|
|
|
{
|
|
|
|
Lua::StackUnwinder frame(Lua::Core::State);
|
|
|
|
lua_pushstring(Lua::Core::State, "onGetSelectedPlant");
|
|
|
|
safe_call_lua(do_notify, 1, 1);
|
|
|
|
return Lua::GetDFObject<df::plant>(Lua::Core::State, -1);
|
|
|
|
}
|
2018-07-22 16:27:22 -06:00
|
|
|
|
|
|
|
#define STATIC_FIELDS_GROUP
|
|
|
|
#include "../DataStaticsFields.cpp"
|
|
|
|
|
|
|
|
using df::identity_traits;
|
|
|
|
|
|
|
|
#define CUR_STRUCT dfhack_viewscreen
|
|
|
|
static const struct_field_info dfhack_viewscreen_fields[] = {
|
|
|
|
{ METHOD(OBJ_METHOD, is_lua_screen), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getFocusString), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, onShow), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, onDismiss), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getSelectedUnit), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getSelectedItem), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getSelectedJob), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getSelectedBuilding), 0, 0 },
|
|
|
|
{ METHOD(OBJ_METHOD, getSelectedPlant), 0, 0 },
|
|
|
|
{ FLD_END }
|
|
|
|
};
|
|
|
|
#undef CUR_STRUCT
|
|
|
|
virtual_identity dfhack_viewscreen::_identity(sizeof(dfhack_viewscreen), nullptr, "dfhack_viewscreen", nullptr, &df::viewscreen::_identity, dfhack_viewscreen_fields);
|
|
|
|
|
|
|
|
#define CUR_STRUCT dfhack_lua_viewscreen
|
|
|
|
static const struct_field_info dfhack_lua_viewscreen_fields[] = {
|
|
|
|
{ FLD_END }
|
|
|
|
};
|
|
|
|
#undef CUR_STRUCT
|
|
|
|
virtual_identity dfhack_lua_viewscreen::_identity(sizeof(dfhack_lua_viewscreen), nullptr, "dfhack_lua_viewscreen", nullptr, &dfhack_viewscreen::_identity, dfhack_lua_viewscreen_fields);
|