develop
John Cosker 2023-05-03 19:37:44 -04:00
parent 611e6d3a12
commit 58e11b01cb
2 changed files with 157 additions and 157 deletions

@ -41,6 +41,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Misc Improvements ## Misc Improvements
- Terminal console no longer appears in front of the game window on startup - Terminal console no longer appears in front of the game window on startup
- `gui/design`: Improved performance for drawing shapes
## Documentation ## Documentation

@ -17,7 +17,6 @@
#include "modules/World.h" #include "modules/World.h"
DFHACK_PLUGIN("design"); DFHACK_PLUGIN("design");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
using DFHack::color_value; using DFHack::color_value;
REQUIRE_GLOBAL(window_x); REQUIRE_GLOBAL(window_x);
@ -27,36 +26,37 @@ REQUIRE_GLOBAL(plotinfo);
using namespace DFHack; using namespace DFHack;
using namespace df::enums; using namespace df::enums;
enum ConfigValues {
CONFIG_IS_ENABLED = 0,
};
namespace DFHack { namespace DFHack {
DBG_DECLARE(pathable, log, DebugCategory::LINFO); DBG_DECLARE(design, log, DebugCategory::LINFO);
} }
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out,
std::vector<PluginCommand> &commands) {
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_shutdown(color_ostream &out) { std::map<uint32_t, Screen::Pen> PENS;
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) {
DEBUG(log,out).print("clearing PENS\n");
PENS.clear();
}
return CR_OK; return CR_OK;
} }
std::map<uint32_t, Screen::Pen> PENS;
struct DrawingPoint { struct DrawingPoint {
uint32_t penKey = 0; uint32_t penKey = 0;
std::pair<int, int> cursor_coords; std::pair<int, int> cursor_coords;
DrawingPoint() : penKey(0), cursor_coords({-1, -1}) {} DrawingPoint() : penKey(0), cursor_coords({-1, -1}) {}
}; };
typedef std::map<int, std::map<int, DrawingPoint>> ShapeMap; typedef std::map<int, std::map<int, DrawingPoint>> ShapeMap;
ShapeMap arr; ShapeMap arr;
bool has_point(int x, int y) { bool has_point(int x, int y) {
return arr.count(x) != 0 && arr.at(x).count(y) != 0; return arr.count(x) != 0 && arr.at(x).count(y) != 0;
}; };
// Key tuple is N, W, E, S // Key tuple is N, W, E, S
@ -81,188 +81,187 @@ std::map<DirectionKey, std::pair<int, int>> CURSORS_MAP = {
}; };
enum PenMask { enum PenMask {
North = 0, North = 0,
South, South,
East, East,
West, West,
DragPoint, DragPoint,
MouseOver, MouseOver,
InShape, InShape,
ExtraPoint, ExtraPoint,
NumFlags NumFlags
}; };
uint32_t gen_pen_key(bool n, bool s, bool e, bool w, bool is_drag_point, uint32_t gen_pen_key(bool n, bool s, bool e, bool w, bool is_drag_point,
bool is_mouse_over, bool inshape, bool extra_point) { bool is_mouse_over, bool inshape, bool extra_point) {
std::bitset<static_cast<size_t>(PenMask::NumFlags)> ret; std::bitset<static_cast<size_t>(PenMask::NumFlags)> ret;
ret[PenMask::North] = n; ret[PenMask::North] = n;
ret[PenMask::South] = s; ret[PenMask::South] = s;
ret[PenMask::East] = e; ret[PenMask::East] = e;
ret[PenMask::West] = w; ret[PenMask::West] = w;
ret[PenMask::DragPoint] = is_drag_point; ret[PenMask::DragPoint] = is_drag_point;
ret[PenMask::MouseOver] = is_mouse_over; ret[PenMask::MouseOver] = is_mouse_over;
ret[PenMask::InShape] = inshape; ret[PenMask::InShape] = inshape;
ret[PenMask::ExtraPoint] = extra_point; ret[PenMask::ExtraPoint] = extra_point;
return ret.to_ulong(); return ret.to_ulong();
} }
Screen::Pen make_pen(const std::pair<int, int> &direction, bool is_drag_point, Screen::Pen make_pen(const std::pair<int, int> &direction, bool is_drag_point,
bool is_mouse_over, bool inshape, bool extra_point) { bool is_mouse_over, bool inshape, bool extra_point) {
color_value color = COLOR_GREEN; color_value color = COLOR_GREEN;
int ycursor_mod = 0; int ycursor_mod = 0;
if (!extra_point) { if (!extra_point) {
if (is_drag_point) { if (is_drag_point) {
color = COLOR_CYAN; color = COLOR_CYAN;
ycursor_mod += 6; ycursor_mod += 6;
if (is_mouse_over) { if (is_mouse_over) {
color = COLOR_MAGENTA; color = COLOR_MAGENTA;
ycursor_mod += 3; ycursor_mod += 3;
} }
} }
} else { } else {
ycursor_mod += 15; ycursor_mod += 15;
color = COLOR_LIGHTRED; color = COLOR_LIGHTRED;
if (is_mouse_over) { if (is_mouse_over) {
color = COLOR_RED; color = COLOR_RED;
ycursor_mod += 3; ycursor_mod += 3;
}
} }
}
Screen::Pen pen;
Screen::Pen pen; pen.ch = inshape ? 'X' : 'o';
pen.ch = inshape ? 'X' : 'o'; pen.fg = color;
pen.fg = color; int selected_tile_texpos = 0;
int selected_tile_texpos = 0; Screen::findGraphicsTile("CURSORS", direction.first,
Screen::findGraphicsTile("CURSORS", direction.first, direction.second + ycursor_mod,
direction.second + ycursor_mod, &selected_tile_texpos);
&selected_tile_texpos); pen.tile = selected_tile_texpos;
pen.tile = selected_tile_texpos;
return pen;
return pen;
} }
Screen::Pen get_pen(int x, int y, ShapeMap &arr, const std::string &type = "") { Screen::Pen get_pen(int x, int y, ShapeMap &arr, const std::string &type = "") {
bool n = false, w = false, e = false, s = false; bool n = false, w = false, e = false, s = false;
if (has_point(x, y)) { if (has_point(x, y)) {
if (y == 0 || !has_point(x, y - 1)) n = true; if (y == 0 || !has_point(x, y - 1)) n = true;
if (x == 0 || !has_point(x - 1, y)) w = true; if (x == 0 || !has_point(x - 1, y)) w = true;
if (!has_point(x + 1, y)) e = true; // TODO check map size if (!has_point(x + 1, y)) e = true; // TODO check map size
if (!has_point(x, y + 1)) s = true; // TODO check map size if (!has_point(x, y + 1)) s = true; // TODO check map size
} }
bool is_drag_point = type == "drag_point"; bool is_drag_point = type == "drag_point";
bool is_extra = type == "extra_point"; bool is_extra = type == "extra_point";
bool is_in_shape = has_point(x, y); bool is_in_shape = has_point(x, y);
auto mouse_pos = Gui::getMousePos(); auto mouse_pos = Gui::getMousePos();
bool mouse_over = mouse_pos.x == x && mouse_pos.y == y; bool mouse_over = mouse_pos.x == x && mouse_pos.y == y;
uint32_t pen_key = uint32_t pen_key = gen_pen_key(n, s, e, w, is_drag_point, mouse_over,
gen_pen_key(n, s, e, w, is_drag_point, mouse_over, is_in_shape, is_extra); is_in_shape, is_extra);
if (CURSORS_MAP.count({n, w, e, s}) > 0 && has_point(x,y)) { if (CURSORS_MAP.count({n, w, e, s}) > 0 && has_point(x, y)) {
arr[x][y].cursor_coords = CURSORS_MAP.at({n, w, e, s}); arr[x][y].cursor_coords = CURSORS_MAP.at({n, w, e, s});
}
if (PENS.find(pen_key) == PENS.end()) {
std::pair<int, int> cursor{-1, -1};
if (type != "") {
return make_pen(CURSORS_MAP.at({n, w, e, s}), is_drag_point, mouse_over,
is_in_shape, is_extra);
} }
if (CURSORS_MAP.count({n, w, e, s}) > 0) { if (PENS.find(pen_key) == PENS.end()) {
PENS.emplace(pen_key, std::pair<int, int> cursor{-1, -1};
make_pen(CURSORS_MAP.at({n, w, e, s}), is_drag_point,
mouse_over, is_in_shape, is_extra)); if (type != "") {
if (type == "" && has_point(x,y)) { return make_pen(CURSORS_MAP.at({n, w, e, s}), is_drag_point,
arr[x][y].penKey = pen_key; mouse_over, is_in_shape, is_extra);
} }
if (CURSORS_MAP.count({n, w, e, s}) > 0) {
PENS.emplace(pen_key,
make_pen(CURSORS_MAP.at({n, w, e, s}), is_drag_point,
mouse_over, is_in_shape, is_extra));
if (type == "" && has_point(x, y)) {
arr[x][y].penKey = pen_key;
}
}
} }
}
// DEBUG(status).print("not cached lmao\n"); return PENS.at(pen_key);
return PENS.at(pen_key);
} }
static int design_load_shape(lua_State *L) { static int design_load_shape(lua_State *L) {
if (lua_istable(L, -1)) { if (lua_istable(L, -1)) {
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
int x = lua_tointeger(L, -2);
if (lua_istable(L, -1)) {
lua_pushnil(L); lua_pushnil(L);
while (lua_next(L, -2) != 0) { while (lua_next(L, -2) != 0) {
int y = lua_tointeger(L, -2); int x = lua_tointeger(L, -2);
bool value = lua_toboolean(L, -1);
if (lua_istable(L, -1)) {
if (value) { lua_pushnil(L);
arr[x][y] = DrawingPoint(); while (lua_next(L, -2) != 0) {
} int y = lua_tointeger(L, -2);
lua_pop(L, 1); bool value = lua_toboolean(L, -1);
if (value) {
arr[x][y] = DrawingPoint();
}
lua_pop(L, 1);
}
}
lua_pop(L, 1);
} }
}
lua_pop(L, 1);
} }
}
return 0; return 0;
} }
static int design_clear_shape(lua_State *L) { static int design_clear_shape(lua_State *L) {
arr.clear(); arr.clear();
return 0; return 0;
} }
static int design_draw_shape(lua_State *L) { static int design_draw_shape(lua_State *L) {
if (arr.size() == 0) { if (arr.size() == 0) {
design_load_shape(L); design_load_shape(L);
} }
for (auto x : arr) { for (auto x : arr) {
for (auto y : x.second) { for (auto y : x.second) {
Screen::Pen cur_tile = Screen::readTile(x.first, y.first, true); Screen::Pen cur_tile = Screen::readTile(x.first, y.first, true);
Screen::Pen pen = get_pen(x.first, y.first, arr); Screen::Pen pen = get_pen(x.first, y.first, arr);
cur_tile.tile = pen.tile; cur_tile.tile = pen.tile;
Screen::paintTile(cur_tile, x.first - *window_x, y.first - *window_y, Screen::paintTile(cur_tile, x.first - *window_x,
true); y.first - *window_y, true);
}
} }
}
return 0; return 0;
} }
static int design_draw_points(lua_State *L) { static int design_draw_points(lua_State *L) {
if (lua_istable(L, -1)) { if (lua_istable(L, -1)) {
const char *str; const char *str;
lua_rawgeti(L, -1, 2); lua_rawgeti(L, -1, 2);
str = lua_tostring(L, -1); str = lua_tostring(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_rawgeti(L, -1, 1); lua_rawgeti(L, -1, 1);
int n = luaL_len(L, -1); int n = luaL_len(L, -1);
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
lua_rawgeti(L, -1, i); lua_rawgeti(L, -1, i);
int x, y; int x, y;
lua_getfield(L, -1, "y"); lua_getfield(L, -1, "y");
y = lua_tointeger(L, -1); y = lua_tointeger(L, -1);
lua_getfield(L, -2, "x"); lua_getfield(L, -2, "x");
x = lua_tointeger(L, -1); x = lua_tointeger(L, -1);
lua_pop(L, 3); lua_pop(L, 3);
Screen::Pen cur_tile = Screen::readTile(x, y, true); Screen::Pen cur_tile = Screen::readTile(x, y, true);
Screen::Pen pen = get_pen(x, y, arr, str); Screen::Pen pen = get_pen(x, y, arr, str);
cur_tile.tile = pen.tile; cur_tile.tile = pen.tile;
Screen::paintTile(cur_tile, x - *window_x, y - *window_y, true); Screen::paintTile(cur_tile, x - *window_x, y - *window_y, true);
}
lua_pop(L, 1);
} }
lua_pop(L, 1);
}
return 0; return 0;
} }
DFHACK_PLUGIN_LUA_COMMANDS{DFHACK_LUA_COMMAND(design_draw_shape), DFHACK_PLUGIN_LUA_COMMANDS{DFHACK_LUA_COMMAND(design_draw_shape),