| 297 | } |
| 298 | |
| 299 | fn update_data_rectangles( |
| 300 | materials: Res<Assets<CanvasMaterial>>, |
| 301 | canvas_query: Query<(&CanvasParams, &CanvasMaterialHandle)>, |
| 302 | mut data_query: Query<(&mut Transform, &mut Visibility, &DataRect)>, |
| 303 | ) { |
| 304 | if let Ok((canvas_params, material_handle)) = canvas_query.single() { |
| 305 | if let Some(material) = materials.get(&material_handle.0) { |
| 306 | // Get canvas bounds from the material |
| 307 | let bound_lo = material.bound_lo; |
| 308 | let bound_up = material.bound_up; |
| 309 | let canvas_size = canvas_params.original_size; |
| 310 | |
| 311 | for (mut transform, mut visibility, data_rect) in data_query.iter_mut() { |
| 312 | let x = data_rect.x; |
| 313 | let y = data_rect.y; |
| 314 | |
| 315 | // Check if the data point is within the visible bounds |
| 316 | if x >= bound_lo.x && x <= bound_up.x && y >= bound_lo.y && y <= bound_up.y { |
| 317 | // Show the rectangle |
| 318 | *visibility = Visibility::Visible; |
| 319 | |
| 320 | // Transform from data coordinates to canvas pixel coordinates |
| 321 | let range_x = bound_up.x - bound_lo.x; |
| 322 | let range_y = bound_up.y - bound_lo.y; |
| 323 | |
| 324 | // Normalize to [0, 1] within the current bounds |
| 325 | let norm_x = (x - bound_lo.x) / range_x; |
| 326 | let norm_y = (y - bound_lo.y) / range_y; |
| 327 | |
| 328 | // Convert to canvas pixel coordinates (centered at canvas position) |
| 329 | let pixel_x = (norm_x - 0.5) * canvas_size.x; |
| 330 | let pixel_y = (norm_y - 0.5) * canvas_size.y; |
| 331 | |
| 332 | // Update transform position |
| 333 | transform.translation.x = canvas_params.position.x + pixel_x; |
| 334 | transform.translation.y = canvas_params.position.y + pixel_y; |
| 335 | transform.translation.z = 0.1; // Always above canvas |
| 336 | |
| 337 | // Scale rectangles based on zoom level (smaller when zoomed out) |
| 338 | let zoom_scale = 1.0 / (range_x / 100.0).max(0.1); // Base scale on x range |
| 339 | transform.scale = Vec3::splat(zoom_scale.clamp(0.1, 3.0)); |
| 340 | } else { |
| 341 | // Hide rectangles outside bounds |
| 342 | *visibility = Visibility::Hidden; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |