nadir_debug/lib.rs
1// Copyright 2026 Joseph Anthony Abbott III
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Developer tools for the Nadir engine: an immediate-mode debug UI, a
16//! stats overlay ([`Stats`]), a profiler with Chrome trace export
17//! ([`Profiler`]), an inspector of the world's entities ([`Inspector`]) and
18//! a console of commands ([`Console`]). The debug drawing and the time
19//! controls are the engine's, in nadir_engine's `dev` (roadmap M7.6).
20//! They are for development builds only: a
21//! game depends on this crate behind its `dev` feature, and
22//! `cargo xtask release-no-dev` checks that a release does not.
23//!
24//! # The debug UI
25//!
26//! A [`DebugUi`] keeps what lasts from frame to frame. Each frame,
27//! [`DebugUi::frame`] takes that frame's events from a [`DebugInput`], code
28//! fills windows with [`Frame::window`] and their widgets through a
29//! [`Ui`], each widget answering what was done to it (a [`Response`]), and
30//! [`Frame::finish`] pushes what they draw into the frame's
31//! `RenderFrame::ui`, after the game's UI and in front of it.
32//!
33//! - **Input:** the engine feeds a [`DebugInput`] with the window's events
34//! (nadir_engine's `dev`); a test feeds it its own. A frame takes at most one
35//! press and one release of the left button, so that a click is judged
36//! where it was pressed and where it was released.
37//! - **Identity:** a widget is the hash of its scope's identity and its
38//! text, or its explicit ID after `##`: `"Pause (3)##pause"`.
39//! - **Hot and active:** the widget under the pointer is hot; the one the
40//! button went down on is active until it goes up, and a click is a
41//! release over the widget that was pressed.
42//! - **Capture:** [`DebugUi::wants_mouse`] and [`DebugUi::wants_keyboard`]
43//! tell the game to leave the mouse or the keys alone, and
44//! [`DebugUi::is_over`] whether a point is over a window.
45//! - **Places:** each window's place is kept as data
46//! ([`DebugUi::places`], [`DebugUi::set_place`]); the engine saves it.
47//! - **Text:** measured by [`TextMetrics`]: [`FontMetrics`] for a `Font`.
48//!
49//! # The tools
50//!
51//! Each fills a window it is given. [`Stats::show`] shows the last frame's
52//! numbers, which the engine copies in. A [`Profiler`] keeps the last
53//! [`PROFILER_FRAMES`] frames, each with the scopes
54//! `nadir_core::profile::take_frame` collected, and the GPU's time over
55//! its drawing once the engine reads it back ([`GpuTiming`],
56//! [`Profiler::set_gpu`]): [`Profiler::show`] draws their processor and GPU
57//! times as graphs and the scopes of the frame clicked as a tree, and
58//! [`Profiler::trace_json`] writes them as Chrome trace event JSON, with a
59//! track of the GPU's passes, which Perfetto UI opens. An [`Inspector`]
60//! lists a `nadir-ecs` world's entities and edits the components of the one
61//! clicked, by the types of the world's `nadir-scene` `TypeRegistry`,
62//! through their reflection: [`Inspector::show`] fills its window, and
63//! [`Inspector::prefab`] writes the entity as a prefab when its
64//! [`InspectorResponse`] asks. A [`Console`] is a command line over a log:
65//! commands registered by their usage, a line checked against it and handed
66//! back with its [`Args`] to run ([`Console::show`], [`Console::run_line`]),
67//! with history and Tab completion, and the engine's log records added by
68//! the caller ([`Console::log_record`]).
69//!
70//! ```
71//! use nadir_core::{GenIndex, Handle, Key, MouseButton, Rect, UVec2, Vec2};
72//! use nadir_debug::{DebugInput, DebugUi, TextMetrics};
73//! use nadir_render::UiList;
74//!
75//! /// A font of 8 units a line, each character 6 wide.
76//! struct Mono;
77//! impl TextMetrics for Mono {
78//! fn line_height(&self) -> f32 {
79//! 8.0
80//! }
81//! fn caret_x(&self, text: &str, byte: usize) -> f32 {
82//! 6.0 * text[..byte].chars().count() as f32
83//! }
84//! }
85//!
86//! fn handle<T>(index: u32) -> Handle<T> {
87//! Handle::new(GenIndex::new(index, std::num::NonZeroU32::MIN))
88//! }
89//! let mut ui = DebugUi::new(handle(0), Mono, handle(1));
90//! let mut input = DebugInput::new();
91//! let mut paused = false;
92//! let mut name = String::from("hero");
93//! let mut renamed = false;
94//! let mut out = UiList::new();
95//! let output = UVec2::new(640, 360);
96//! for _ in 0..2 {
97//! // A click on the checkbox, in the second frame, where the first drew it.
98//! let mut frame = ui.frame(&mut input, output, output);
99//! frame.window("Tools", Rect::new(Vec2::ZERO, Vec2::new(200.0, 120.0)), |ui| {
100//! ui.checkbox("Paused", &mut paused);
101//! // Enter in the field, once it has the keyboard.
102//! renamed |= ui.text_input("Name", &mut name).submitted;
103//! });
104//! frame.finish(&mut out);
105//! input.pointer_moved(Vec2::new(8.0, 18.0));
106//! input.button(MouseButton::Left, true);
107//! input.button(MouseButton::Left, false);
108//! }
109//! assert!(paused && !renamed);
110//! assert!(!ui.wants_keyboard());
111//! # let _ = Key::Enter;
112//! ```
113
114// The README's examples are compiled as doc tests too.
115#[cfg(doctest)]
116#[doc = include_str!("../README.md")]
117struct ReadmeDoctests;
118
119mod console;
120mod edit;
121mod id;
122mod input;
123mod inspector;
124mod metrics;
125mod profiler;
126mod stats;
127mod style;
128#[cfg(test)]
129mod tests;
130mod ui;
131mod widgets;
132
133pub use console::{Args, CONSOLE_HISTORY, CONSOLE_LINES, Console, ConsoleError, ConsoleOut};
134pub use input::DebugInput;
135pub use inspector::{Inspector, InspectorResponse};
136pub use metrics::{FontMetrics, TextMetrics};
137pub use profiler::{
138 FrameTiming, GpuTiming, PROFILER_FRAMES, PROFILER_RECORDS, ProfiledFrame, Profiler,
139 ProfilerResponse,
140};
141pub use stats::Stats;
142pub use style::Style;
143pub use ui::{DebugUi, Frame, WindowPlace};
144pub use widgets::{Plot, PlotResponse, Response, Ui};