Expand description
Developer tools for the Nadir engine: an immediate-mode debug UI, a
stats overlay (Stats), a profiler with Chrome trace export
(Profiler), an inspector of the world’s entities (Inspector) and
a console of commands (Console). The debug drawing and the time
controls are the engine’s, in nadir_engine’s dev (roadmap M7.6).
They are for development builds only: a
game depends on this crate behind its dev feature, and
cargo xtask release-no-dev checks that a release does not.
§The debug UI
A DebugUi keeps what lasts from frame to frame. Each frame,
DebugUi::frame takes that frame’s events from a DebugInput, code
fills windows with Frame::window and their widgets through a
Ui, each widget answering what was done to it (a Response), and
Frame::finish pushes what they draw into the frame’s
RenderFrame::ui, after the game’s UI and in front of it.
- Input: the engine feeds a
DebugInputwith the window’s events (nadir_engine’sdev); a test feeds it its own. A frame takes at most one press and one release of the left button, so that a click is judged where it was pressed and where it was released. - Identity: a widget is the hash of its scope’s identity and its
text, or its explicit ID after
##:"Pause (3)##pause". - Hot and active: the widget under the pointer is hot; the one the button went down on is active until it goes up, and a click is a release over the widget that was pressed.
- Capture:
DebugUi::wants_mouseandDebugUi::wants_keyboardtell the game to leave the mouse or the keys alone, andDebugUi::is_overwhether a point is over a window. - Places: each window’s place is kept as data
(
DebugUi::places,DebugUi::set_place); the engine saves it. - Text: measured by
TextMetrics:FontMetricsfor aFont.
§The tools
Each fills a window it is given. Stats::show shows the last frame’s
numbers, which the engine copies in. A Profiler keeps the last
PROFILER_FRAMES frames, each with the scopes
nadir_core::profile::take_frame collected, and the GPU’s time over
its drawing once the engine reads it back (GpuTiming,
Profiler::set_gpu): Profiler::show draws their processor and GPU
times as graphs and the scopes of the frame clicked as a tree, and
Profiler::trace_json writes them as Chrome trace event JSON, with a
track of the GPU’s passes, which Perfetto UI opens. An Inspector
lists a nadir-ecs world’s entities and edits the components of the one
clicked, by the types of the world’s nadir-scene TypeRegistry,
through their reflection: Inspector::show fills its window, and
Inspector::prefab writes the entity as a prefab when its
InspectorResponse asks. A Console is a command line over a log:
commands registered by their usage, a line checked against it and handed
back with its Args to run (Console::show, Console::run_line),
with history and Tab completion, and the engine’s log records added by
the caller (Console::log_record).
use nadir_core::{GenIndex, Handle, Key, MouseButton, Rect, UVec2, Vec2};
use nadir_debug::{DebugInput, DebugUi, TextMetrics};
use nadir_render::UiList;
/// A font of 8 units a line, each character 6 wide.
struct Mono;
impl TextMetrics for Mono {
fn line_height(&self) -> f32 {
8.0
}
fn caret_x(&self, text: &str, byte: usize) -> f32 {
6.0 * text[..byte].chars().count() as f32
}
}
fn handle<T>(index: u32) -> Handle<T> {
Handle::new(GenIndex::new(index, std::num::NonZeroU32::MIN))
}
let mut ui = DebugUi::new(handle(0), Mono, handle(1));
let mut input = DebugInput::new();
let mut paused = false;
let mut name = String::from("hero");
let mut renamed = false;
let mut out = UiList::new();
let output = UVec2::new(640, 360);
for _ in 0..2 {
// A click on the checkbox, in the second frame, where the first drew it.
let mut frame = ui.frame(&mut input, output, output);
frame.window("Tools", Rect::new(Vec2::ZERO, Vec2::new(200.0, 120.0)), |ui| {
ui.checkbox("Paused", &mut paused);
// Enter in the field, once it has the keyboard.
renamed |= ui.text_input("Name", &mut name).submitted;
});
frame.finish(&mut out);
input.pointer_moved(Vec2::new(8.0, 18.0));
input.button(MouseButton::Left, true);
input.button(MouseButton::Left, false);
}
assert!(paused && !renamed);
assert!(!ui.wants_keyboard());Structs§
- Args
- A command’s arguments, the words typed after its name, checked against its usage: each word the usage names typed as it is, each choice one of the choices, as the usage spells it, each required argument there, and no more than the usage has.
- Console
- A console: a command line under a log, in a window of its own. A
command is registered with its usage, which names it and its arguments,
a line of help, and
command, the caller’s value for it (an enum of the caller’s commands, or a function), which the console hands back with the arguments when a line names it, for the caller to run. - Console
Out - Where a command prints: lines added to the console’s log, in the text’s color unless it says another.
- Debug
Input - The debug UI’s input: the window’s events, fed in their order, by the engine from its window (roadmap M7.10) or by a test.
- DebugUi
- An immediate-mode debug UI: each frame, code calls a window and its widgets, and each widget says what was done to it. It keeps what lasts from frame to frame: which widget is held, which text field has the keyboard, the windows’ places and which tree nodes are open.
- Font
Metrics - A
Font’s measures: the text laid out asFont::layoutlays it out, with no markup, so that a caret is where the glyphs are drawn. - Frame
- A frame of the debug UI, from
DebugUi::frame: its windows are filled, thenFrame::finishpushes what they draw. Dropped, it ends the frame: a frame not finished draws nothing, and its input is spent. - Frame
Timing - When a frame started, and how long it took.
- GpuTiming
- The GPU’s time over a frame’s drawing: each of the renderer’s passes,
as its timers measured them, read back frames later
(
Profiler::set_gpu). - Inspector
- The inspector’s window: a list of the world’s entities, a page at a
time, filtered by their numbers and the names of their components, and
the components of the one clicked, which
Inspector::showedits through theirReflectimpls. It finds the component types in the world’sTypeRegistry, a resource. - Inspector
Response - What the inspector asks of its caller, this frame.
- Plot
- How
Ui::plotdraws its values. - Plot
Response - What a plot did this frame.
- Profiled
Frame - A frame the profiler holds.
- Profiler
- The last
PROFILER_FRAMESframes’ timings and scopes, whichProfiler::showdraws as graphs of each frame’s processor time and GPU time and, for the frame clicked, its scopes as a tree, andProfiler::trace_jsonwrites as a Chrome trace. - Profiler
Response - What the profiler’s window did this frame.
- Response
- What a widget did this frame.
- Stats
- What the stats overlay shows: plain numbers, which the engine copies in each frame from its own resources.
- Style
- The debug UI’s look. Sizes are in UI units: a UI unit is a pixel of the font.
- Ui
- A window’s content, being filled: each widget takes a row, from the
top, or a place in a row of
Ui::horizontal. - Window
Place - A window’s place, as
DebugUi::placesgives it.
Enums§
- Console
Error - Why the console did not run a line, or a command failed: the console prints it in red.
Constants§
- CONSOLE_
HISTORY - How many command lines the console’s history keeps.
- CONSOLE_
LINES - How many lines the console’s log keeps: as many as the engine’s log
ring (
nadir_core::log::RING_CAPACITY). - PROFILER_
FRAMES - The frames a
Profilerkeeps: five seconds at 60 frames a second. - PROFILER_
RECORDS - The scope records a
Profilerkeeps, of all its frames together: a frame of many records leaves room for fewer frames, and one of more than this keeps this many.
Traits§
- Text
Metrics - The measures of the debug UI’s font, in UI units (a UI unit is a pixel of the font).