par_term_render/custom_shader_renderer/
cursor.rs1use par_term_config::color_u8_to_f32_a;
7use par_term_emu_core_rust::cursor::CursorStyle;
8
9use super::CustomShaderRenderer;
10
11impl CustomShaderRenderer {
12 pub fn update_cursor(
24 &mut self,
25 col: usize,
26 row: usize,
27 opacity: f32,
28 cursor_color: [f32; 4],
29 style: CursorStyle,
30 ) {
31 let new_pos = (col, row);
32 let style_changed = style != self.current_cursor_style;
33 let pos_changed = new_pos != self.current_cursor_pos;
34
35 if pos_changed || style_changed {
36 self.previous_cursor_pos = self.current_cursor_pos;
38 self.previous_cursor_opacity = self.current_cursor_opacity;
39 self.previous_cursor_color = self.current_cursor_color;
40 self.previous_cursor_style = self.current_cursor_style;
41 self.current_cursor_pos = new_pos;
42 self.current_cursor_style = style;
43
44 self.cursor_change_time = if self.animation_enabled {
46 self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
47 } else {
48 0.0
49 };
50
51 if pos_changed {
52 log::trace!(
53 "Cursor moved: ({}, {}) -> ({}, {}), change_time={:.3}",
54 self.previous_cursor_pos.0,
55 self.previous_cursor_pos.1,
56 col,
57 row,
58 self.cursor_change_time
59 );
60 }
61 }
62 self.current_cursor_opacity = opacity;
63 self.current_cursor_color = cursor_color;
64 }
65
66 pub fn update_cell_dimensions(&mut self, cell_width: f32, cell_height: f32, padding: f32) {
73 self.cursor_cell_width = cell_width;
74 self.cursor_cell_height = cell_height;
75 self.cursor_window_padding = padding;
76 }
77
78 pub fn set_content_offset_y(&mut self, offset: f32) {
80 self.cursor_content_offset_y = offset;
81 }
82
83 pub fn set_content_offset_x(&mut self, offset: f32) {
85 self.cursor_content_offset_x = offset;
86 }
87
88 pub fn set_scale_factor(&mut self, scale_factor: f32) {
90 self.scale_factor = scale_factor;
91 }
92
93 pub(super) fn cursor_to_pixels(&self, col: usize, row: usize) -> (f32, f32) {
97 let x = self.cursor_window_padding
98 + self.cursor_content_offset_x
99 + (col as f32 * self.cursor_cell_width);
100 let y = self.cursor_window_padding
101 + self.cursor_content_offset_y
102 + (row as f32 * self.cursor_cell_height);
103 (x, y)
104 }
105
106 pub(super) fn cursor_width_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
109 match style {
110 CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_width,
112 CursorStyle::SteadyBar | CursorStyle::BlinkingBar => 2.0 * scale_factor,
114 CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => self.cursor_cell_width,
116 }
117 }
118
119 pub(super) fn cursor_height_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
122 match style {
123 CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_height,
125 CursorStyle::SteadyBar | CursorStyle::BlinkingBar => self.cursor_cell_height,
127 CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => 2.0 * scale_factor,
129 }
130 }
131
132 pub fn cursor_needs_animation(&self) -> bool {
137 if self.animation_enabled {
138 let current_time =
139 self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0);
140 (current_time - self.cursor_change_time) < 1.0
142 } else {
143 false
144 }
145 }
146
147 pub fn update_cursor_shader_config(
155 &mut self,
156 color: [u8; 3],
157 trail_duration: f32,
158 glow_radius: f32,
159 glow_intensity: f32,
160 ) {
161 self.cursor_shader_color = color_u8_to_f32_a(color, 1.0);
162 self.cursor_trail_duration = trail_duration.max(0.0);
163 self.cursor_glow_radius = glow_radius.max(0.0);
164 self.cursor_glow_intensity = glow_intensity.clamp(0.0, 1.0);
165 }
166}