mod effects;
mod interaction;
mod selection;
use crate::chart::cursor_modes::CursorModeState;
use crate::drawings::DrawingInteractionMode;
use crate::drawings::DrawingManager;
use crate::widget::Chart;
use egui::{Painter, Rect, Response, Ui};
impl Chart {
pub fn handle_drawings(
&self,
ui: &mut Ui,
drawing_manager: &mut DrawingManager,
cursor_modes: &mut CursorModeState,
response: &Response,
price_rect: Rect,
adjusted_min: f64,
adjusted_max: f64,
painter: &Painter,
last_close_price: Option<f64>,
timescale: &crate::model::TimeScale,
) {
let adjusted_range = (adjusted_max - adjusted_min).max(1e-12);
let rect_min_x = price_rect.min.x;
let rect_max_y = price_rect.max.y;
let rect_height = price_rect.height();
let rect_width = price_rect.width();
let bar_to_x = |bar_idx: f32| -> f32 {
timescale.idx_to_coord_precise(bar_idx, rect_min_x, rect_width)
};
let x_to_bar_snapped =
|x: f32| -> f32 { timescale.coord_to_idx(x, rect_min_x, rect_width).round() };
let x_to_bar_precise =
|x: f32| -> f32 { timescale.coord_to_idx(x, rect_min_x, rect_width) };
let price_to_y = |price: f64| -> f32 {
let ratio = (price - adjusted_min) / adjusted_range;
rect_max_y - (ratio as f32 * rect_height)
};
let y_to_price = |y: f32| -> f64 {
let ratio = ((rect_max_y - y) / rect_height).clamp(0.0, 1.0) as f64;
adjusted_min + ratio * adjusted_range
};
drawing_manager.update_all_screen_coords(bar_to_x, price_to_y);
if let Some(price) = last_close_price {
drawing_manager.update_pos_prices(price);
}
if let Some(active_tool) = drawing_manager.active_tool {
let uses_precise =
active_tool.interaction_mode() == DrawingInteractionMode::ContinuousDraw;
if uses_precise {
interaction::handle_active_tool(
ui,
drawing_manager,
response,
price_rect,
&x_to_bar_precise,
&y_to_price,
active_tool,
);
} else {
interaction::handle_active_tool(
ui,
drawing_manager,
response,
price_rect,
&x_to_bar_snapped,
&y_to_price,
active_tool,
);
}
} else {
selection::handle_selection(
ui,
drawing_manager,
cursor_modes,
response,
price_rect,
&x_to_bar_snapped,
&y_to_price,
);
}
selection::handle_keyboard_shortcuts(ui, drawing_manager);
drawing_manager.render_all(painter, price_rect);
}
pub fn render_eraser_highlight(
&self,
painter: &Painter,
drawing_manager: &DrawingManager,
cursor_modes: &CursorModeState,
) {
selection::render_eraser_highlight(painter, drawing_manager, cursor_modes);
}
}