Skip to content

Commit

Permalink
Merge pull request #880 from myk002/myk_dims
Browse files Browse the repository at this point in the history
[gui/design] add floating label indicating selected dimensions
  • Loading branch information
myk002 authored Oct 31, 2023
2 parents cdeaee9 + 3393878 commit 5c8cdd7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Template for new versions:
- `sync-windmills`: synchronize or randomize movement of active windmills

## New Features
- `gui/design`: show selected dimensions next to the mouse cursor when designating with vanilla tools, for example when painting a burrow or designating digging

## Fixes
- `gui/unit-syndromes`: show the syndrome names properly in the UI
Expand Down
7 changes: 7 additions & 0 deletions docs/gui/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ Usage
::

gui/design

Overlay
-------

This script provides an overlay that shows the selected dimensions when
designating something with vanilla tools, for example when painting a burrow or
designating digging.
64 changes: 63 additions & 1 deletion gui/design.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- A GUI front-end for creating designs
--@ module = false
--@ module = true

-- TODOS ====================

Expand Down Expand Up @@ -38,6 +38,7 @@ local gui = require("gui")
local textures = require("gui.textures")
local guidm = require("gui.dwarfmode")
local widgets = require("gui.widgets")
local overlay = require('plugins.overlay')
local quickfort = reqscript("quickfort")
local shapes = reqscript("internal/design/shapes")
local util = reqscript("internal/design/util")
Expand Down Expand Up @@ -1771,6 +1772,67 @@ function DesignScreen:onDismiss()
view = nil
end

-- ----------------- --
-- DimensionsOverlay --
-- ----------------- --

local DIMENSION_LABEL_WIDTH = 15
local DIMENSION_LABEL_HEIGHT = 1

DimensionsOverlay = defclass(DimensionsOverlay, overlay.OverlayWidget)
DimensionsOverlay.ATTRS{
default_pos={x=1,y=1},
default_enabled=true,
viewscreens={
'dwarfmode/Designate',
'dwarfmode/Burrow/Paint',
'dwarfmode/Stockpile/Paint',
},
frame={w=DIMENSION_LABEL_WIDTH, h=DIMENSION_LABEL_HEIGHT},
}

local selection_rect = df.global.selection_rect

local function is_choosing_area()
return selection_rect.start_x >= 0 and dfhack.gui.getMousePos()
end

local function get_cur_area_dims()
local pos1 = dfhack.gui.getMousePos()
local pos2 = xyz2pos(selection_rect.start_x, selection_rect.start_y, selection_rect.start_z)
return math.abs(pos1.x - pos2.x) + 1,
math.abs(pos1.y - pos2.y) + 1,
math.abs(pos1.z - pos2.z) + 1
end

function DimensionsOverlay:init()
self:addviews{
widgets.Label{
view_id='label',
frame={t=0, l=0, h=DIMENSION_LABEL_HEIGHT},
text={
{text=function() return ('%dx%dx%d'):format(get_cur_area_dims()) end},
},
visible=is_choosing_area,
},
}
end

function DimensionsOverlay:onRenderFrame(dc, rect)
DimensionsOverlay.super.onRenderFrame(self, dc, rect)
local sw, sh = dfhack.screen.getWindowSize()
local x, y = dfhack.screen.getMousePos()
x = math.min(x + 3, sw - DIMENSION_LABEL_WIDTH)
y = math.min(y + 3, sh - DIMENSION_LABEL_HEIGHT)
self.frame.w = x + DIMENSION_LABEL_WIDTH
self.frame.h = y + DIMENSION_LABEL_HEIGHT
self.subviews.label.frame = {t=y, l=x}
end

OVERLAY_WIDGETS = {
dimensions=DimensionsOverlay,
}

if dfhack_flags.module then return end

if not dfhack.isMapLoaded() then
Expand Down

0 comments on commit 5c8cdd7

Please sign in to comment.