1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use crate::color::Color;
use crate::error::{Error, Result};
use crate::font::GlyphAtlas;
use crate::input::{InputEvent, InteractionMode};
use crate::renderer::Renderer;
use crate::text::{self, TextStyle};
use crate::vertex::{DrawList, Vertex};
use crate::window::{OverlayTarget, OverlayWindow};
use std::f32::consts::PI;
const CIRCLE_SEGMENTS: usize = 32;
/// A transparent, click-through overlay rendered on top of a target window.
///
/// Create an overlay by specifying the target window, then draw shapes and text
/// each frame between `begin_frame` and `end_frame` calls.
pub struct Overlay {
window: OverlayWindow,
renderer: Renderer,
draw_list: DrawList,
font_atlas: GlyphAtlas,
in_frame: bool,
}
impl Overlay {
/// Create a new overlay positioned over the target window.
pub fn new(target: OverlayTarget) -> Result<Self> {
let window = OverlayWindow::create(&target)?;
let (w, h) = window.size();
let mut renderer = Renderer::new(window.hwnd, w, h)?;
let font_atlas = GlyphAtlas::new();
renderer.upload_font_atlas(&font_atlas)?;
Ok(Self {
window,
renderer,
draw_list: DrawList::new(),
font_atlas,
in_frame: false,
})
}
/// Returns true if the target window is the foreground window.
pub fn is_visible(&self) -> bool {
self.window.is_target_foreground()
}
/// Returns true if the target window is the foreground window.
pub fn is_target_foreground(&self) -> bool {
self.window.is_target_foreground()
}
/// Returns true if the overlay window is the foreground window.
pub fn is_overlay_foreground(&self) -> bool {
self.window.is_overlay_foreground()
}
/// Begin a new frame. Call drawing methods after this, then call `end_frame`.
pub fn begin_frame(&mut self) -> Result<()> {
if !self.window.pump_messages() {
return Err(Error::OverlayClosed);
}
if !self.window.sync_position() {
return Err(Error::TargetWindowLost);
}
let (w, h) = self.window.size();
self.renderer.resize(w, h)?;
if self.renderer.device_lost() {
self.renderer.recover()?;
self.renderer.upload_font_atlas(&self.font_atlas)?;
}
self.draw_list.clear();
self.renderer.begin_frame()?;
self.in_frame = true;
Ok(())
}
/// Finish the frame and present the rendered content.
pub fn end_frame(&mut self) -> Result<()> {
if !self.in_frame {
return Err(Error::NoActiveFrame);
}
self.renderer.submit(
&self.draw_list.vertices,
&self.draw_list.indices,
&self.draw_list.commands,
)?;
self.renderer.end_frame()?;
self.in_frame = false;
Ok(())
}
/// Current input behavior of the overlay window.
pub fn interaction_mode(&self) -> InteractionMode {
self.window.interaction_mode()
}
/// Switch between click-through gameplay mode and interactive control mode.
pub fn set_interaction_mode(&mut self, mode: InteractionMode) -> Result<()> {
self.window.set_interaction_mode(mode)
}
/// Drain input and focus events received since the previous call.
pub fn drain_input_events(&mut self) -> Vec<InputEvent> {
self.window.drain_events()
}
/// Return and reset the number of events discarded because the input queue was full.
pub fn take_dropped_input_event_count(&mut self) -> usize {
self.window.take_dropped_event_count()
}
/// Return and reset the number of times the graphics device was lost and rebuilt.
///
/// A driver reset, a GPU hang, or an adapter change destroys the device. The overlay
/// rebuilds it during the next `begin_frame` and carries on, so drawing code needs no
/// changes. The frame in flight when the device went away is lost. This reports how
/// often that happened, for applications that log it or reset frame timing.
/// [`Error::DeviceLost`] is only returned when the replacement device cannot be built.
pub fn take_device_reset_count(&mut self) -> usize {
self.renderer.take_resets()
}
/// Close the overlay window.
///
/// The next call to `begin_frame` returns [`Error::OverlayClosed`].
pub fn close(&mut self) {
self.window.close();
}
/// Current overlay client size in pixels.
pub fn size(&self) -> (u32, u32) {
self.window.size()
}
/// Draw a rectangle outline.
pub fn rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color) {
let t = 1.0;
self.line(x, y, x + w, y, t, color);
self.line(x + w, y, x + w, y + h, t, color);
self.line(x + w, y + h, x, y + h, t, color);
self.line(x, y + h, x, y, t, color);
}
/// Draw a filled rectangle.
pub fn rect_filled(&mut self, x: f32, y: f32, w: f32, h: f32, color: Color) {
let c = color.to_f32_array();
self.draw_list.add_solid_quad(
Vertex::new(x, y, c),
Vertex::new(x + w, y, c),
Vertex::new(x + w, y + h, c),
Vertex::new(x, y + h, c),
);
}
/// Draw a line with the given thickness.
pub fn line(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, color: Color) {
let dx = x2 - x1;
let dy = y2 - y1;
let len = (dx * dx + dy * dy).sqrt();
if len < f32::EPSILON {
return;
}
let nx = -dy / len * thickness * 0.5;
let ny = dx / len * thickness * 0.5;
let c = color.to_f32_array();
self.draw_list.add_solid_quad(
Vertex::new(x1 + nx, y1 + ny, c),
Vertex::new(x1 - nx, y1 - ny, c),
Vertex::new(x2 - nx, y2 - ny, c),
Vertex::new(x2 + nx, y2 + ny, c),
);
}
/// Draw a circle outline.
pub fn circle(&mut self, cx: f32, cy: f32, radius: f32, color: Color) {
let step = 2.0 * PI / CIRCLE_SEGMENTS as f32;
for i in 0..CIRCLE_SEGMENTS {
let a1 = step * i as f32;
let a2 = step * (i + 1) as f32;
self.line(
cx + a1.cos() * radius,
cy + a1.sin() * radius,
cx + a2.cos() * radius,
cy + a2.sin() * radius,
1.0,
color,
);
}
}
/// Draw a filled circle.
pub fn circle_filled(&mut self, cx: f32, cy: f32, radius: f32, color: Color) {
let c = color.to_f32_array();
let step = 2.0 * PI / CIRCLE_SEGMENTS as f32;
let center = Vertex::new(cx, cy, c);
let mut verts = vec![center];
for i in 0..=CIRCLE_SEGMENTS {
let angle = step * i as f32;
verts.push(Vertex::new(
cx + angle.cos() * radius,
cy + angle.sin() * radius,
c,
));
}
let mut indices = Vec::with_capacity(CIRCLE_SEGMENTS * 3);
for i in 1..=CIRCLE_SEGMENTS {
indices.push(0);
indices.push(i as u32);
indices.push((i % CIRCLE_SEGMENTS + 1) as u32);
}
self.draw_list.add_solid_triangles(&verts, &indices);
}
/// Draw left-aligned text at the given position.
pub fn text(&mut self, x: f32, y: f32, text: &str, size: f32, color: Color) {
self.text_styled(x, y, text, &TextStyle::new(size, color));
}
/// Draw text with an outline, alignment, or both.
///
/// Outlined text stays legible over unknown backgrounds. The outline is rasterized
/// from the glyph's distance field, so it costs one extra draw call per string
/// regardless of length.
///
/// ```no_run
/// # use procmod_overlay::{Color, Overlay, OverlayTarget, TextAlign, TextStyle};
/// # fn main() -> procmod_overlay::Result<()> {
/// # let mut overlay = Overlay::new(OverlayTarget::Title("game".into()))?;
/// let label = TextStyle::new(16.0, Color::WHITE)
/// .outlined(Color::BLACK, 1.5)
/// .aligned(TextAlign::Center);
///
/// overlay.text_styled(320.0, 12.0, "Player1 [100HP]", &label);
/// # Ok(())
/// # }
/// ```
pub fn text_styled(&mut self, x: f32, y: f32, text: &str, style: &TextStyle) {
text::emit(&mut self.draw_list, &self.font_atlas, x, y, text, style);
}
/// Measure the bounding box of text at the given size.
///
/// The result covers the glyphs only. An outline extends past it by its width.
pub fn text_bounds(&self, text: &str, size: f32) -> (f32, f32) {
let scale = size / crate::font::ATLAS_FONT_SIZE;
let (w, h) = self.font_atlas.measure(text);
(w * scale, h * scale)
}
}