Mod UI kit: make your window look like Kiln's pages (since 0.2.0-dev.32.9)
Kiln's own menu pages (Keys, Server, Conflicts, Tools, Commands) are drawn with one small kit: a list "book" beside a
framed inspector, a head with the screen title and a counter, captions with the diamond-tipped rule, key and value
pairs, wrapped and cut text in the game's type, the game's icons, badges, a thin scroll rail, rows drawn by hand with
the game's hover and focus sounds, and an empty state. The public UiKit class (mod API 0.2.37) supplies that kit.
Kiln's pages draw through the same class, so a window made with it looks like them and keeps looking like them when
Kiln's look changes.
This page shows how to put a window together with it. The pieces it builds on are in
game-look-ui.md: the window frame, UI units and the scale, the tokens, the text styles, the game's
art and sounds, and the widget set (ActionButton, SearchField, ListRow, UiScrollList, ...).
Everything of the game's (its font, UI textures, sounds, controller glyphs) is read from the player's own game files while the game runs. Kiln never ships them, and neither should your mod. Where a piece cannot be read (an older game build, the first frames while it loads, a build without the data), the kit draws Kiln's flat fallback: plain colours, a diamond emblem in place of a missing icon, the system font, no sound. Your window stays usable.
Opting in
UiKit is part of the experimental game-look surface: [Experimental("KILNX033")], like the widget set. Opt in once
in your project (analyzers.md):
<PropertyGroup>
<NoWarn>$(NoWarn);KILNX033</NoWarn>
</PropertyGroup>
Which Kiln you need
A mod built with the 0.2.0-dev.32.9 dev kit declares "apiVersion": "0.2.37", so an older Kiln, which has no
UiKit, refuses to load it instead of failing at the first call. Add "kiln": ">=0.2.0-dev.32.9" to the manifest's
dependencies too, so players see the floor in plain words (manifest.md).
The window
A Ui.Screen (or Ui.Window) of a mod made for mod API 0.2.35 or later already wears the game look: the solid reading
surface, the game's ornamented gold frame, the title row with a close button, the world dimmed and blurred behind it,
and the player's UI scale. Leave Style at UiStyle.Player and don't define a theme of your own for it: a custom
UiThemeSpec takes the window back to plain ImGui colours. For the few plain ImGui calls you keep, ui.TokenLook()
gives them the design's colours, square corners and the game's font.
Keep one UiKit per window, and start it at the top of every frame:
private readonly UiKit _kit = new();
void Draw(UiDraw ui)
{
var k = _kit.Begin(ui); // the window's scale, the tokens now, controller navigation
var (book, info) = k.Split(k.ContentArea);
// ... the book on the left, the inspector on the right
}
Begin(ui) takes the window's game-look scale (k.Scale, pixels per UI unit) and the tokens in effect (k.Tokens),
and keeps k.NavVisible: whether keyboard or controller navigation shows, by the rule Kiln's menu uses (a controller
button or stick or a navigation key shows it; moving or clicking the mouse hides it). In your own section of Kiln's menu
(Ui.AddMenuSection) start it with Begin(ui, page) instead: it takes the shell's scale and navigation state, so your
page matches the pages around it.
Sizes in the kit are UI units: pixels at 1920x1080, times k.Scale on screen (k.U(units) converts). Positions are
screen pixels. k.ContentArea is the space left in the window at the cursor: lay your page out in it rather than in fixed
numbers, because the room changes. Since 0.2.0-dev.32.9 Kiln keeps a screen's Width and Height inside the display, so
a screen sized for 1080p (1560 x 860, say) still fits at the player's largest Kiln size (150 %), where the display is only
1280 x 720 units; the page then gets that room.
A page: the book and the inspector
| Kiln's page | The kit |
|---|---|
| The work area split into the list book and the framed inspector (1.46 : 0.87, a 22-unit gap) | k.Split(area) → (Book, Inspector) |
| Each half on its panel | ui.BeginGamePanel(id, UiPanelStyle.Book or Framed, rect.Width / k.Scale, rect.Height / k.Scale) after k.At(rect.X, rect.Y) |
| The book's head: the screen title in capitals and a counter | k.BookHead(book, title, counter, counterTone) |
| The search line, the list, the foot | k.BookLayout(book) → (Search, List, Foot) |
| The foot's hairline and range counter ("12–19 of 46") | k.BookFoot(foot, k.RangeText(list)) (answers the foot's middle, for a button) |
| The inspector's motif, caption and heading | k.InspectorTop(info, iconPath, caption, heading) (answers where the body starts) |
| ... with an item's icon or your mod's in the motif | k.InspectorTop(info, picture, caption, heading): any UiImage (Ui.ItemImage(item), ModIcons.Image(icon)); Kiln's emblem for null |
| A paragraph | k.Paragraph(x, y, width, text, GameTextStyle.UiBody, 24, UiToken.Sub, maxLines, 1.36f) |
| A caption with the diamond-tipped rule | k.Divider(x0, x1, y, caption, trailing) (at the cursor: ui.SectionDivider) |
| A key and value over a hairline | k.Pair(x0, x1, y, key, value) (at the cursor: ui.ValueRow) |
| The inspector's foot buttons | k.InspectorFootBottom(info) (UiKit.InspectorFootUnits is its distance from the inspector's bottom edge, in units), then ui.PlaceNextWidget and ui.ActionButton |
| Nothing to list | k.EmptyState(list, title, hint, action) |
The inspector's text starts 35 units in from its sides: info.X + k.U(35) to info.X + info.Width - k.U(35). Leave
UiKit.SectionGapUnits under a paragraph before the next divider.
The list
Rows are ui.ListRow in a UiScrollList: the game's gold scroll thumb, fades at a cut edge, smooth wheel and right
stick, the arrows and the d-pad moving the selection, the cream focus fill, the hover and focus sounds. Put the list in a
child region of List's size so it scrolls alone:
var (search, list, foot) = k.BookLayout(book);
ui.PlaceNextWidget(search.X, search.Y, search.X + search.Width, search.Y + search.Height);
if (ui.SearchField("##ledger.search", ref _term, "Search your stock")) _rows.ScrollToTop();
k.At(list.X, list.Y);
if (ui.BeginChild("##ledger.rows", list.Width, list.Height, UiChildFlags.NavFlattened,
UiWindowFlags.NoBackground | UiWindowFlags.NoScrollbar | UiWindowFlags.NoScrollWithMouse))
{
if (shown.Count == 0)
{
if (k.EmptyState(list, "Nothing matches.", "Try another word.", "Clear search##ledger.clear")) _term = "";
}
else if (_rows.Begin(ui, "ledger.rows", shown.Count, k.Tokens.Metric(UiMetric.LibraryRow)))
{
foreach (var i in _rows.Visible)
{
var s = shown[i];
var row = new UiRowContent { Title = Items.DisplayName(s.Item), Trailing = s.Count.ToString("N0"), Icon = Ui.ItemImage(s.Item) };
if (ui.ListRow("##ledger.row" + i, row, new UiWidgetOptions { Selected = i == _rows.Selected })) _rows.Selected = i;
}
_rows.End();
}
}
ui.EndChild();
k.BookFoot(foot, k.RangeText(_rows));
A row's icon is any UiImage: an item's (Ui.ItemImage(item)), your mod's (ModIcons.Image(icon), with the
ModIcon from ModIcons.Of(Context.Id) kept in a field: looking it up reads your mod's folder), a picture of the game's
UI art (UiGameLook.ArtAt("icons.crafting.resources")?.Image) or your own PNG (Ui.LoadImage). The images Kiln gives
you are Kiln's: ask for them again each frame (an image Kiln let go draws nothing), and their Dispose does nothing.
For content of your own over a row (a column, a second badge), draw it after the row over ui.LastWidget's rectangle and
take its colours from k.RowInk(ui.LastWidget, UiToken.Sub): on the cream focus fill the text turns dark
(UiToken.FocusInk, UiToken.FocusSub), elsewhere it keeps the token you give. k.FillShown(state) says whether the
fill shows. Kiln's Tools page draws its log rows this way.
Rows and tiles drawn by hand
When a row is not a list row (a boxed record, a tile with a picture), make it clickable with k.Hit, draw it, and give it
the focus outline:
var (clicked, item) = k.Hit("##chest" + i, x0, y, x1, y + h);
var fill = k.Tile(x0, y, x1, y + h, i == _chosen, item); // ground, hover, selection bar, cream fill, focus outline
k.Text(x0 + k.U(20), y + k.U(14), chest.Name, GameTextStyle.UiSubsection, 28, fill ? UiToken.FocusInk : UiToken.Ink);
if (clicked) _chosen = i;
Hit takes the mouse, the keyboard and the controller like any widget, and plays the game's hover sound when the pointer
arrives and its focus sound when navigation lands on it (once per arrival). Tile is Kiln's Conflicts and Server
records. k.Focus(x0, y0, x1, y1) draws the outline alone, only while navigation shows.
Item slots you handle yourself
ui.ItemSlotTile is the game's item slot as a widget: it takes its own clicks. A page with drag and drop (a backpack
beside a storage list, a crafting grid) handles the slots' input itself, so it draws them with k.ItemSlot instead: the
same look from the same code (the slot's shape and outline, a rarity's tint, the icon, the hover glow, the pressed look, a
chosen slot's gold outline, the focus outline while navigation shows), drawn only. Give it the state of your own area,
and a count for its corner:
for (var i = 0; i < slots.Count; i++)
{
var x = x0 + i * (side + k.U(10));
var (clicked, item) = k.Hit("##bag" + i, x, y, x + side, y + side);
if (clicked) _chosen = i;
if (_dragging && PointerOver(x, y, side)) item = item with { Hovered = true }; // the slot a drag is over looks hovered
var corner = slots[i].Count > 1 ? UiKit.CountText(slots[i].Count) : null;
k.ItemSlot(x, y, x + side, y + side, Ui.ItemImage(slots[i].Item), i == _chosen, item, corner: corner);
}
k.ItemSlot plays no sound (Hit does) and lays nothing out. The corner is a count or a short word ("Split") on a dark
plate in the slot's bottom right corner, at the player's text scale while the slot holds it and smaller where it would
not (never cut: a cut count reads as another number); UiKit.CountText(count) makes a count short enough for it (9,999,
then 12.4k, 3.1M), in the player's culture. It is a new string each call: keep it while the count stays the same.
rarity: tints the slot, disabled: true dims it.
The window's foot
Kiln's pages end in a hint bar of the device the player uses. When your foot also holds a message, measure the bar first
(ui.MeasureHintBar(hints): its width with this frame's key caps or controller glyphs), give the message what is left
and place the bar at the right:
var (barW, _) = ui.MeasureHintBar(Foot);
float right = foot.X + foot.Width, cy = foot.Y + foot.Height / 2, x = foot.X + k.U(10);
var barX = Math.Max(foot.X, right - barW);
var badgeW = k.BadgeWidth("Synced");
k.Badge(x + badgeW, cy, "Synced", UiTone.Ok);
x += badgeW + k.U(12);
var shown = k.Fit(_message, GameTextStyle.UiControl, 21, Math.Max(0, barX - k.U(24) - x));
k.Text(x, cy - k.Measure(shown, GameTextStyle.UiControl, 21).Height / 2, shown, GameTextStyle.UiControl, 21, UiToken.Sub);
ui.PlaceNextWidget(barX, foot.Y, right, foot.Y + foot.Height);
ui.HintBar(Foot);
Measure it every frame: the player switching between the keyboard and a controller changes its width. A bar placed in a
rectangle narrower than its measure drops whole hints from its start (the end, Choose and Back, stays). k.BadgeWidth(text)
is the width k.Badge gives a badge, for laying out what goes before it. Kiln's own menu cuts its footer line the same
way.
Text
k.Text(x, y, text, style, size, ink) draws one of the game's type styles (GameTextStyle.UiScreenTitle,
UiWindowTitle, UiSubsection, UiListName, UiBody, UiControl, UiSecondary, UiCaption, ...; sizes in units,
0 for the style's) in any colour token. k.Measure sizes it, k.LineHeight(style, size) is the line a stack of rows
takes (1.2 em, so rows keep Kiln's rhythm with any font), k.Fit(text, style, size, width) cuts it with "…", and
k.Tip(x, y, text, right) shows the whole of a cut name while the row is hovered or focused. k.Paragraph wraps (and
cuts at maxLines); k.LinesIn(height, ...) says how many lines fit above a foot. The player's text scale is in every
size already; k.TextGrow is how much to grow the height of rows that hold text (Kiln's settings rows do).
k.TokenColor(token, alpha) is a colour token's colour (its alpha times alpha), for your own ui.* calls.
The game's art, your icon
k.Icon(path, x, y, size, tint)draws a white-mask icon of the game's UI configuration by its field path, tinted;k.IconOr(...)draws Kiln's diamond emblem where the build has no such icon or it is still loading. Paths Kiln's pages use:icons.genericIcons.settings,icons.genericIcons.edit,icons.genericIcons.protectionShield,icons.genericIcons.dedicatedServer,icons.genericIcons.textEnabled,icons.baseInfo.iconFlameAltar,icons.crafting.*(resources, weapon, armor, ammo, farming, magical, production, ...). Any texture field works (UiGameLook.ArtAt);UiArtnames the frames, buttons, rules and shapes.UiGameArt.Imageis a piece of the game's art as aUiImage, for every place that takes one (a list row's or card's icon, a HUD line's).k.Diamond,k.Rule,k.Rect: the design's small diamond, a hairline in a token's colour, a square-cornered rectangle.k.ModPicture(Context.Id, x, y, size)draws your mod's icon as Kiln's Mods and Settings pages show it (yourmod-icon.png, else the Kiln emblem: mod-icons.md; the kit looks it up once);ModIcons.Image(icon)is the same picture as aUiImage.k.InspectorTop(info, picture, caption, heading)puts a picture of yours in the inspector's motif instead of a game icon: an item's icon (Ui.ItemImage(item)), your mod's (ModIcons.Image(icon)), anyUiImage, in full colour, its art fitted in the motif; Kiln's emblem for null.k.Badge(right, cy, text, tone)places a state badge by its right edge, sized to its words at the player's text scale (k.BadgeWidth(text)is its width;maxWidth:cuts the words with "…" past that many pixels);k.ScrollRailis the thin rail for a region you scroll yourself.
Names, pickers and the controller
Kiln's pages follow a few rules; follow them and your window reads like theirs.
- Names, never ids, in rows. A row shows what the player knows: an item's, creature's or ability's
DisplayName(Items.DisplayName(item), display-names.md), nevergame:wood_logsor0x1a2b3c4dand never an id line under the name. Show an id only on hover (ui.InfoTip) or in a raw view the player opens on purpose. - Pick, don't type. A choice is a
Dropdown, aToggle, aValueSlider, item slots (ItemSlotTile,k.ItemSlot) or a list to pick from, not a text field the player types an id, an enum or a time into. Text fields are for words that are the player's own (a name, a search). - Words with colour. A state is a badge with its words (
StateBadge,ListRow'sBadgeandTone), never a colour alone. - The controller works everywhere. Every action is a widget or a
k.Hitarea, so the d-pad reaches it; give the safe choiceDefaultFocus; draw the hints of the device the player uses withui.HintBarat the window's foot (ui.PlaceNextWidgetover a stripk.Tokens.Metric(UiMetric.FooterHeight) * 0.7funits tall; with a message beside it, see the window's foot), and focus visuals only whilek.NavVisible. - Kiln's sizes. Use the tokens' metrics (
UiMetric.ButtonHeight,ControlHeight,LibraryRow,PanelPadding) and colours (UiToken.*) instead of numbers and colours of your own, so the player's size and text settings reach your window.
Seeing it without the game
The repository's UI preview draws a sample mod screen made only of the kit and the widget set (the stock view of a ledger-like mod with backpack slots drawn by hand and a message beside the hint bar, its empty state with the mod's icon in the motif, a controller, the largest size):
tools/ui-preview/preview.sh --shell-scene ui-kit # also ui-kit-empty, ui-kit-controller, ui-kit-max, or all
The pictures land in $KILN_OUT/preview/shell/. The sample's code is tools/ui-preview/Kiln.Ui.Preview/KitSample.cs:
copy it as the start of your own window.