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 clear_cursor(&mut self) {
68 if self.current_cursor_opacity == 0.0 && self.current_cursor_color == [0.0, 0.0, 0.0, 0.0] {
69 return;
70 }
71
72 self.previous_cursor_pos = self.current_cursor_pos;
73 self.previous_cursor_opacity = self.current_cursor_opacity;
74 self.previous_cursor_color = self.current_cursor_color;
75 self.previous_cursor_style = self.current_cursor_style;
76 self.current_cursor_opacity = 0.0;
77 self.current_cursor_color = [0.0, 0.0, 0.0, 0.0];
78 self.cursor_change_time = if self.animation_enabled {
79 self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
80 } else {
81 0.0
82 };
83 }
84
85 pub fn update_cell_dimensions(&mut self, cell_width: f32, cell_height: f32, padding: f32) {
92 self.cursor_cell_width = cell_width;
93 self.cursor_cell_height = cell_height;
94 self.cursor_window_padding = padding;
95 }
96
97 pub fn set_content_offset_y(&mut self, offset: f32) {
99 self.cursor_content_offset_y = offset;
100 }
101
102 pub fn set_content_offset_x(&mut self, offset: f32) {
104 self.cursor_content_offset_x = offset;
105 }
106
107 pub fn set_scale_factor(&mut self, scale_factor: f32) {
109 self.scale_factor = scale_factor;
110 }
111
112 pub(super) fn cursor_to_pixels(&self, col: usize, row: usize) -> (f32, f32) {
116 let x = self.cursor_window_padding
117 + self.cursor_content_offset_x
118 + (col as f32 * self.cursor_cell_width);
119 let y = self.cursor_window_padding
120 + self.cursor_content_offset_y
121 + (row as f32 * self.cursor_cell_height);
122 (x, y)
123 }
124
125 pub(super) fn cursor_width_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
128 match style {
129 CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_width,
131 CursorStyle::SteadyBar | CursorStyle::BlinkingBar => 2.0 * scale_factor,
133 CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => self.cursor_cell_width,
135 }
136 }
137
138 pub(super) fn cursor_height_for_style(&self, style: CursorStyle, scale_factor: f32) -> f32 {
141 match style {
142 CursorStyle::SteadyBlock | CursorStyle::BlinkingBlock => self.cursor_cell_height,
144 CursorStyle::SteadyBar | CursorStyle::BlinkingBar => self.cursor_cell_height,
146 CursorStyle::SteadyUnderline | CursorStyle::BlinkingUnderline => 2.0 * scale_factor,
148 }
149 }
150
151 pub fn cursor_needs_animation(&self) -> bool {
156 if self.animation_enabled {
157 let current_time =
158 self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0);
159 (current_time - self.cursor_change_time) < 1.0
161 } else {
162 false
163 }
164 }
165
166 pub fn update_cursor_shader_config(
174 &mut self,
175 color: [u8; 3],
176 trail_duration: f32,
177 glow_radius: f32,
178 glow_intensity: f32,
179 ) {
180 self.cursor_shader_color = color_u8_to_f32_a(color, 1.0);
181 self.cursor_trail_duration = trail_duration.max(0.0);
182 self.cursor_glow_radius = glow_radius.max(0.0);
183 self.cursor_glow_intensity = glow_intensity.clamp(0.0, 1.0);
184 }
185}