Skip to content

Commit

Permalink
Merge pull request DFHack#1051 from myk002/myk_block_borders
Browse files Browse the repository at this point in the history
update devel/block-borders
  • Loading branch information
myk002 authored Mar 19, 2024
2 parents 45d72d6 + eae81a0 commit d310650
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 72 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Template for new versions:
- `instruments`: provides information on how to craft the instruments used by the player civilization
- `modtools/item-trigger`: (reinstated) modder's resource for triggering scripted content when specific items are used
- `modtools/if-entity`: (reinstated) modder's resource for triggering scripted content depending on the race of the loaded fort
- `devel/block-borders`: (reinstated) highlights boundaries of map blocks or embark tile blocks

## New Features
- `gui/settings-manager`: add import, export, and autoload for work details
Expand Down
124 changes: 53 additions & 71 deletions devel/block-borders.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
-- overlay that displays map block borders

--[====[
devel/block-borders
===================
An overlay that draws borders of map blocks. See :doc:`/docs/api/Maps` for
details on map blocks.
]====]

local gui = require "gui"
local gui = require('gui')
local guidm = require "gui.dwarfmode"

local ui = df.global.plotinfo
local widgets = require('gui.widgets')

local DRAW_CHARS = {
ns = string.char(179),
Expand All @@ -23,64 +12,39 @@ local DRAW_CHARS = {
se = string.char(218),
sw = string.char(191),
}
-- persist across script runs
color = color or COLOR_LIGHTCYAN

BlockBordersOverlay = defclass(BlockBordersOverlay, guidm.MenuOverlay)
BlockBordersOverlay.ATTRS{
block_size = 16,
draw_borders = true,
BlockBorders = defclass(BlockBorders, widgets.Window)
BlockBorders.ATTRS {
frame_title='Block Borders',
frame={t=20, r=3, w=29, h=7},
autoarrange_subviews=true,
autoarrange_gap=1,
}

function BlockBordersOverlay:onInput(keys)
if keys.LEAVESCREEN then
self:dismiss()
elseif keys.D_PAUSE then
self.draw_borders = not self.draw_borders
elseif keys.CUSTOM_B then
self.block_size = self.block_size == 16 and 48 or 16
elseif keys.CUSTOM_C then
color = color + 1
if color > 15 then
color = 1
end
elseif keys.CUSTOM_SHIFT_C then
color = color - 1
if color < 1 then
color = 15
end
elseif keys.D_LOOK then
self:sendInputToParent(ui.main.mode == df.ui_sidebar_mode.LookAround and 'LEAVESCREEN' or 'D_LOOK')
else
self:propagateMoveKeys(keys)
end
end

function BlockBordersOverlay:onRenderBody(dc)
dc = dc:viewport(1, 1, dc.width - 2, dc.height - 2)
dc:key_string('D_PAUSE', 'Toggle borders')
:newline()
dc:key_string('CUSTOM_B', self.block_size == 16 and '1 block (16 tiles)' or '3 blocks (48 tiles)')
:newline()
dc:key('CUSTOM_C')
:string(', ')
:key_string('CUSTOM_SHIFT_C', 'Color: ')
:string('Example', color)
:newline()
dc:key_string('D_LOOK', 'Toggle cursor')
:newline()

self:renderOverlay()
function BlockBorders:init()
self:addviews{
widgets.ToggleHotkeyLabel{
view_id='draw',
key='CUSTOM_CTRL_D',
label='Draw borders:',
initial_option=true,
},
widgets.CycleHotkeyLabel{
view_id='size',
key='CUSTOM_CTRL_B',
label=' Block size:',
options={16, 48},
},
}
end

function BlockBordersOverlay:renderOverlay()
if not self.draw_borders then return end

local block_end = self.block_size - 1
self:renderMapOverlay(function(pos, is_cursor)
function BlockBorders:render_overlay()
local block_size = self.subviews.size:getOptionValue()
local block_end = block_size - 1
guidm.renderMapOverlay(function(pos, is_cursor)
if is_cursor then return end
local block_x = pos.x % self.block_size
local block_y = pos.y % self.block_size
local block_x = pos.x % block_size
local block_y = pos.y % block_size
local key
if block_x == 0 and block_y == 0 then
key = 'se'
Expand All @@ -95,16 +59,34 @@ function BlockBordersOverlay:renderOverlay()
elseif block_y == 0 or block_y == block_end then
key = 'ew'
end
return DRAW_CHARS[key], color or COLOR_LIGHTCYAN
if not key then return nil end
return COLOR_LIGHTCYAN, DRAW_CHARS[key]
end)
end

function BlockBorders:onRenderFrame(dc, rect)
if self.subviews.draw:getOptionValue() then
self:render_overlay()
end
BlockBorders.super.onRenderFrame(self, dc, rect)
end

BlockBordersScreen = defclass(BlockBordersScreen, gui.ZScreen)
BlockBordersScreen.ATTRS {
focus_path='block-borders',
pass_movement_keys=true,
}

function BlockBordersScreen:init()
self:addviews{BlockBorders{}}
end

function BlockBordersScreen:onDismiss()
view = nil
end

if not dfhack.isMapLoaded() then
qerror('This script requires a fortress map to be loaded')
end

-- we can work both with a cursor and without one. start in a mode that mirrors
-- the current game state
local is_cursor = not not guidm.getCursorPos()
local sidebar_mode = df.ui_sidebar_mode[is_cursor and 'LookAround' or 'Default']
BlockBordersOverlay{sidebar_mode=sidebar_mode}:show()
view = view and view:raise() or BlockBordersScreen{}:show()
2 changes: 1 addition & 1 deletion docs/devel/block-borders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ devel/block-borders

.. dfhack-tool::
:summary: Outline map blocks on the map screen.
:tags: unavailable
:tags: dev map

This tool displays an overlay that highlights the borders of map blocks. See
:doc:`/docs/api/Maps` for details on map blocks.
Expand Down

0 comments on commit d310650

Please sign in to comment.