From 40f3a3333e0d5131d749910129e4d0c9732c67f7 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 1 Dec 2022 00:08:56 -0500 Subject: [PATCH 1/2] Make Cursor widget prefix and coordinate types configurable e.g. the old behavior can be restored with "coords_type": "mouse_ui" and "coords_short": true --- plugins/lua/dwarfmonitor.lua | 52 +++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/plugins/lua/dwarfmonitor.lua b/plugins/lua/dwarfmonitor.lua index ff98d12ca..41f6accbe 100644 --- a/plugins/lua/dwarfmonitor.lua +++ b/plugins/lua/dwarfmonitor.lua @@ -3,6 +3,7 @@ local _ENV = mkmodule('plugins.dwarfmonitor') local json = require('json') local guidm = require('gui.dwarfmode') local overlay = require('plugins.overlay') +local utils = require('utils') local DWARFMONITOR_CONFIG_FILE = 'dfhack-config/dwarfmonitor.json' @@ -150,23 +151,50 @@ CursorWidget = defclass(CursorWidget, overlay.OverlayWidget) CursorWidget.ATTRS{ default_pos={x=2,y=-4}, viewscreens={'dungeonmode', 'dwarfmode'}, + type='all', + short=false, } -function CursorWidget:onRenderBody(dc) - local screenx, screeny = dfhack.screen.getMousePos() - local mouse_map = dfhack.gui.getMousePos() - local keyboard_map = guidm.getCursorPos() +CursorWidget.COORD_TYPES = utils.OrderedTable() +CursorWidget.COORD_TYPES.mouse_ui = { + description = 'mouse UI grid', + get_coords = function() + return xyz2pos(dfhack.screen.getMousePos()) + end, +} +CursorWidget.COORD_TYPES.mouse_map = { + description = 'mouse map coord', + get_coords = dfhack.gui.getMousePos, +} +CursorWidget.COORD_TYPES.keyboard_map = { + description = 'kbd cursor coord', + get_coords = guidm.getCursorPos, +} - local text = {} - table.insert(text, ('mouse UI grid (%d,%d)'):format(screenx, screeny)) - if mouse_map then - table.insert(text, ('mouse map coord (%d,%d,%d)') - :format(mouse_map.x, mouse_map.y, mouse_map.z)) +function CursorWidget:init() + local ok, config = pcall(json.decode_file, DWARFMONITOR_CONFIG_FILE) + if ok then + self.type = tostring(config.coords_type or self.type) + self.short = config.coords_short + else + dfhack.printerr(('Failed to load %s: %s'):format(DWARFMONITOR_CONFIG_FILE, config)) end - if keyboard_map then - table.insert(text, ('kbd cursor coord (%d,%d,%d)') - :format(keyboard_map.x, keyboard_map.y, keyboard_map.z)) +end + +function CursorWidget:onRenderBody(dc) + local text = {} + for k, v in pairs(CursorWidget.COORD_TYPES) do + -- fall back to 'all' behavior if type isn't recognized + if k == self.type or not CursorWidget.COORD_TYPES[self.type] then + local coords = v.get_coords() + if coords then + local prefix = self.short and '' or (v.description .. ' ') + local coord_str = table.concat({coords.x, coords.y, coords.z}, ',') + table.insert(text, ('%s(%s)'):format(prefix, coord_str)) + end + end end + local width = 0 for i,line in ipairs(text) do dc:seek(0, i-1):string(line) From e0e8b2f983b4694175883e77625076312a0bf413 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 1 Dec 2022 00:19:16 -0500 Subject: [PATCH 2/2] Document dwarfmonitor.cursor config, add example --- docs/plugins/dwarfmonitor.rst | 41 ++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/plugins/dwarfmonitor.rst b/docs/plugins/dwarfmonitor.rst index 75e261171..c1eb27aef 100644 --- a/docs/plugins/dwarfmonitor.rst +++ b/docs/plugins/dwarfmonitor.rst @@ -35,15 +35,40 @@ screen with the `overlay` framework: They can be enabled or disable via the `overlay` command. -The :file:`dfhack-config/dwarfmonitor.json` file can be edited to specify the -format for the ``dwarfmonitor.date`` widget: +The :file:`dfhack-config/dwarfmonitor.json` file can be edited to change widget +configuration with any of the following fields: -* ``Y`` or ``y``: The current year -* ``M``: The current month, zero-padded if necessary -* ``m``: The current month, *not* zero-padded -* ``D``: The current day, zero-padded if necessary -* ``d``: The current day, *not* zero-padded +* ``date_format`` (string): format for the ``dwarfmonitor.date`` widget: -The default date format is ``Y-M-D``, per the ISO8601_ standard. + * ``Y`` or ``y``: The current year + * ``M``: The current month, zero-padded if necessary + * ``m``: The current month, *not* zero-padded + * ``D``: The current day, zero-padded if necessary + * ``d``: The current day, *not* zero-padded + + The default date format is ``Y-M-D``, per the ISO8601_ standard. + +* ``coords_type`` (string): the coordinate type to show in the ``dwarfmonitor.cursor`` widget: + + * ``all`` (the default): show all of the coordinate types listed here + * ``mouse_ui``: the X/Y UI coordinates of the tile the mouse is positioned over + * ``mouse_map``: the X/Y/Z map coordinates of the tile the mouse is + positioned over (only if over the map) + * ``keyboard_map``: the X/Y/Z map coordinates of the tile selected by the + keyboard-controlled ``X`` cursor in DF (if active) + +* ``coords_short`` (boolean, default: ``false``): if ``true``, hides explanatory + text from the ``dwarfmonitor.cursor`` widget, and only shows coordinates as + ``(X,Y,Z)`` + +Example configuration file: + +.. code-block:: json + + { + "date_format": "m/d/y", + "coords_type": "mouse_map", + "coords_short": false + } .. _ISO8601: https://en.wikipedia.org/wiki/ISO_8601