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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! XYPad atom - 2D pad control for EDM/VJ applications
//!
//! A two-dimensional touch/drag pad for controlling X/Y parameters simultaneously.
//! Commonly used for filter cutoff/resonance, effects, or any 2D parameter space.
//!
//! # Features
//! - 2D drag control with visual feedback
//! - Optional axis labels
//! - Crosshair cursor display
//! - Optional grid overlay
//! - Theme-aware styling
//!
//! # Example
//! ```ignore
//! // Basic XY pad
//! XYPad::new()
//! .show_with(ctx, (model.x, model.y), |(x, y)| Msg::SetXY(x, y));
//!
//! // With labels and grid
//! XYPad::new()
//! .label_x("Cutoff")
//! .label_y("Resonance")
//! .grid(true)
//! .show_with(ctx, (model.cutoff, model.reso), Msg::SetFilter);
//!
//! // Custom size
//! XYPad::new()
//! .size(200.0, 200.0)
//! .show_with(ctx, (model.x, model.y), Msg::SetXY);
//! ```
use crate::Theme;
use egui::{Color32, Pos2, Rect, Response, Sense, Stroke, Ui, Vec2, Widget};
use egui_cha::ViewCtx;
/// A 2D pad control for X/Y parameter adjustment
pub struct XYPad<'a> {
width: Option<f32>,
height: Option<f32>,
label_x: Option<&'a str>,
label_y: Option<&'a str>,
show_grid: bool,
show_crosshair: bool,
disabled: bool,
}
impl<'a> XYPad<'a> {
/// Create a new XY pad
pub fn new() -> Self {
Self {
width: None,
height: None,
label_x: None,
label_y: None,
show_grid: false,
show_crosshair: true,
disabled: false,
}
}
/// Set custom size
pub fn size(mut self, width: f32, height: f32) -> Self {
self.width = Some(width);
self.height = Some(height);
self
}
/// Set X-axis label (displayed at bottom)
pub fn label_x(mut self, label: &'a str) -> Self {
self.label_x = Some(label);
self
}
/// Set Y-axis label (displayed at left, rotated)
pub fn label_y(mut self, label: &'a str) -> Self {
self.label_y = Some(label);
self
}
/// Show/hide grid overlay
pub fn grid(mut self, show: bool) -> Self {
self.show_grid = show;
self
}
/// Show/hide crosshair cursor
pub fn crosshair(mut self, show: bool) -> Self {
self.show_crosshair = show;
self
}
/// Set disabled state
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// TEA-style: Show pad with immutable values, emit Msg on change
///
/// Values are normalized to 0.0..1.0 range
pub fn show_with<Msg>(
self,
ctx: &mut ViewCtx<'_, Msg>,
value: (f64, f64),
on_change: impl FnOnce((f64, f64)) -> Msg,
) {
let mut current = value;
let response = self.show_internal(ctx.ui, &mut current);
if response.changed() {
ctx.emit(on_change(current));
}
}
/// Show pad (modifies values in place)
pub fn show(self, ui: &mut Ui, value: &mut (f64, f64)) -> Response {
self.show_internal(ui, value)
}
fn show_internal(self, ui: &mut Ui, value: &mut (f64, f64)) -> Response {
let theme = Theme::current(ui.ctx());
// Calculate dimensions
let pad_width = self.width.unwrap_or(theme.spacing_xl * 5.0);
let pad_height = self.height.unwrap_or(theme.spacing_xl * 5.0);
// Additional space for labels
let label_x_height = if self.label_x.is_some() {
theme.font_size_xs + theme.spacing_xs
} else {
0.0
};
let label_y_width = if self.label_y.is_some() {
theme.font_size_xs + theme.spacing_xs
} else {
0.0
};
let total_width = pad_width + label_y_width;
let total_height = pad_height + label_x_height;
let (rect, mut response) = ui.allocate_exact_size(
Vec2::new(total_width, total_height),
if self.disabled {
Sense::hover()
} else {
Sense::click_and_drag()
},
);
// Pad area (excluding labels)
let pad_rect = Rect::from_min_size(
rect.min + Vec2::new(label_y_width, 0.0),
Vec2::new(pad_width, pad_height),
);
// Handle interaction
if (response.dragged() || response.clicked()) && !self.disabled {
if let Some(pos) = response.interact_pointer_pos() {
// Clamp to pad area
let clamped_pos = Pos2::new(
pos.x.clamp(pad_rect.min.x, pad_rect.max.x),
pos.y.clamp(pad_rect.min.y, pad_rect.max.y),
);
// Convert to 0..1 range
let x = ((clamped_pos.x - pad_rect.min.x) / pad_width) as f64;
let y = (1.0 - (clamped_pos.y - pad_rect.min.y) / pad_height) as f64; // Invert Y
value.0 = x.clamp(0.0, 1.0);
value.1 = y.clamp(0.0, 1.0);
}
}
// Double-click to reset to center
if response.double_clicked() && !self.disabled {
value.0 = 0.5;
value.1 = 0.5;
}
if ui.is_rect_visible(rect) {
let painter = ui.painter();
// Colors
let (bg_color, border_color, cursor_color, grid_color) = if self.disabled {
(
theme.bg_tertiary,
theme.border,
theme.text_muted,
theme.border,
)
} else if response.hovered() || response.dragged() {
(
theme.bg_secondary,
theme.primary,
theme.primary,
theme.border,
)
} else {
(
theme.bg_secondary,
theme.border,
theme.primary,
theme.border,
)
};
// Background
painter.rect_filled(pad_rect, theme.radius_md, bg_color);
// Grid
if self.show_grid {
let grid_stroke = Stroke::new(theme.stroke_width * 0.5, grid_color);
let grid_divisions = 4;
// Vertical lines
for i in 1..grid_divisions {
let x = pad_rect.min.x + pad_width * (i as f32 / grid_divisions as f32);
painter.line_segment(
[Pos2::new(x, pad_rect.min.y), Pos2::new(x, pad_rect.max.y)],
grid_stroke,
);
}
// Horizontal lines
for i in 1..grid_divisions {
let y = pad_rect.min.y + pad_height * (i as f32 / grid_divisions as f32);
painter.line_segment(
[Pos2::new(pad_rect.min.x, y), Pos2::new(pad_rect.max.x, y)],
grid_stroke,
);
}
}
// Center cross (light)
let center_stroke = Stroke::new(theme.stroke_width * 0.5, theme.text_muted);
painter.line_segment(
[
Pos2::new(pad_rect.center().x, pad_rect.min.y),
Pos2::new(pad_rect.center().x, pad_rect.max.y),
],
center_stroke,
);
painter.line_segment(
[
Pos2::new(pad_rect.min.x, pad_rect.center().y),
Pos2::new(pad_rect.max.x, pad_rect.center().y),
],
center_stroke,
);
// Current position
let cursor_x = pad_rect.min.x + value.0 as f32 * pad_width;
let cursor_y = pad_rect.max.y - value.1 as f32 * pad_height; // Invert Y
let cursor_pos = Pos2::new(cursor_x, cursor_y);
// Crosshair lines
if self.show_crosshair {
let crosshair_stroke = Stroke::new(theme.stroke_width, cursor_color);
// Horizontal line
painter.line_segment(
[
Pos2::new(pad_rect.min.x, cursor_y),
Pos2::new(pad_rect.max.x, cursor_y),
],
crosshair_stroke,
);
// Vertical line
painter.line_segment(
[
Pos2::new(cursor_x, pad_rect.min.y),
Pos2::new(cursor_x, pad_rect.max.y),
],
crosshair_stroke,
);
}
// Cursor dot
let dot_radius = theme.spacing_sm;
painter.circle_filled(cursor_pos, dot_radius, cursor_color);
painter.circle_stroke(
cursor_pos,
dot_radius,
Stroke::new(theme.stroke_width, theme.bg_primary),
);
// Border
painter.rect_stroke(
pad_rect,
theme.radius_md,
Stroke::new(theme.border_width, border_color),
egui::StrokeKind::Outside,
);
// X-axis label
if let Some(label) = self.label_x {
let label_pos =
Pos2::new(pad_rect.center().x, rect.max.y - theme.font_size_xs / 2.0);
painter.text(
label_pos,
egui::Align2::CENTER_CENTER,
label,
egui::FontId::proportional(theme.font_size_xs),
if self.disabled {
theme.text_muted
} else {
theme.text_secondary
},
);
}
// Y-axis label (rotated text simulation - just draw at left)
if let Some(label) = self.label_y {
// For simplicity, draw vertically by placing each char
// In a real app, you'd use proper text rotation
let label_x = rect.min.x + theme.font_size_xs / 2.0;
let label_y = pad_rect.center().y;
// Draw label horizontally for now (egui doesn't easily support rotation)
painter.text(
Pos2::new(label_x, label_y),
egui::Align2::CENTER_CENTER,
label.chars().next().unwrap_or(' '), // First char as indicator
egui::FontId::proportional(theme.font_size_xs),
if self.disabled {
theme.text_muted
} else {
theme.text_secondary
},
);
}
// Value display (top right corner)
let value_text = format!("({:.2}, {:.2})", value.0, value.1);
painter.text(
Pos2::new(
pad_rect.max.x - theme.spacing_xs,
pad_rect.min.y + theme.spacing_xs,
),
egui::Align2::RIGHT_TOP,
&value_text,
egui::FontId::proportional(theme.font_size_xs),
Color32::from_rgba_unmultiplied(
theme.text_secondary.r(),
theme.text_secondary.g(),
theme.text_secondary.b(),
if self.disabled { 80 } else { 180 },
),
);
}
// Mark as changed if interacted
if response.dragged() || response.clicked() || response.double_clicked() {
response.mark_changed();
}
response
}
}
impl Default for XYPad<'_> {
fn default() -> Self {
Self::new()
}
}
impl Widget for XYPad<'_> {
fn ui(self, ui: &mut Ui) -> Response {
let mut dummy = (0.5, 0.5);
self.show_internal(ui, &mut dummy)
}
}