Only redraw text box when needed
This commit is contained in:
parent
bacb9e6848
commit
2425c7eb49
1 changed files with 33 additions and 35 deletions
|
|
@ -74,24 +74,6 @@ pub fn text_box<'a, Editor>(editor: &'a Mutex<Editor>) -> TextBox<'a, Editor> {
|
||||||
TextBox::new(editor)
|
TextBox::new(editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_pixel(buffer: &mut [u32], width: i32, height: i32, x: i32, y: i32, color: u32) {
|
|
||||||
if y < 0 || y >= height {
|
|
||||||
// Skip if y out of bounds
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if x < 0 || x >= width {
|
|
||||||
// Skip if x out of bounds
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let alpha = (color >> 24) & 0xFF;
|
|
||||||
if alpha == 0 {
|
|
||||||
// Do not draw if alpha is zero
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: improve performance
|
//TODO: improve performance
|
||||||
fn draw_rect(
|
fn draw_rect(
|
||||||
buffer: &mut [u32],
|
buffer: &mut [u32],
|
||||||
|
|
@ -266,38 +248,51 @@ where
|
||||||
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
let mut font_system = FONT_SYSTEM.lock().unwrap();
|
||||||
let mut editor = editor.borrow_with(&mut font_system);
|
let mut editor = editor.borrow_with(&mut font_system);
|
||||||
|
|
||||||
|
/*TODO: have buffer able to scale during drawing
|
||||||
// Scale metrics
|
// Scale metrics
|
||||||
let metrics = editor.buffer().metrics();
|
let metrics = editor.buffer().metrics();
|
||||||
editor
|
editor
|
||||||
.buffer_mut()
|
.buffer_mut()
|
||||||
.set_metrics(metrics.scale(scale_factor as f32));
|
.set_metrics(metrics.scale(scale_factor as f32));
|
||||||
|
|
||||||
|
// Restore original metrics
|
||||||
|
editor.buffer_mut().set_metrics(metrics);
|
||||||
|
*/
|
||||||
|
|
||||||
// Set size
|
// Set size
|
||||||
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
|
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
|
||||||
|
|
||||||
// Shape and layout
|
// Shape and layout
|
||||||
editor.shape_as_needed();
|
editor.shape_as_needed();
|
||||||
|
|
||||||
// Draw to pixel buffer
|
if editor.buffer().redraw() {
|
||||||
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
|
// Draw to pixel buffer
|
||||||
{
|
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
|
||||||
let buffer = unsafe {
|
{
|
||||||
std::slice::from_raw_parts_mut(pixels.as_mut_ptr() as *mut u32, pixels.len() / 4)
|
let buffer = unsafe {
|
||||||
};
|
std::slice::from_raw_parts_mut(
|
||||||
|
pixels.as_mut_ptr() as *mut u32,
|
||||||
|
pixels.len() / 4,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
editor.draw(
|
editor.draw(
|
||||||
&mut state.cache.lock().unwrap(),
|
&mut state.cache.lock().unwrap(),
|
||||||
text_color,
|
text_color,
|
||||||
|x, y, w, h, color| {
|
|x, y, w, h, color| {
|
||||||
draw_rect(buffer, image_w, image_h, x, y, w as i32, h as i32, color.0);
|
draw_rect(buffer, image_w, image_h, x, y, w as i32, h as i32, color.0);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear redraw flag
|
||||||
|
editor.buffer_mut().set_redraw(false);
|
||||||
|
|
||||||
|
*state.handle.lock().unwrap() =
|
||||||
|
image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore original metrics
|
let handle = state.handle.lock().unwrap().clone();
|
||||||
editor.buffer_mut().set_metrics(metrics);
|
|
||||||
|
|
||||||
let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels);
|
|
||||||
image::Renderer::draw(
|
image::Renderer::draw(
|
||||||
renderer,
|
renderer,
|
||||||
handle,
|
handle,
|
||||||
|
|
@ -443,6 +438,7 @@ where
|
||||||
pub struct State {
|
pub struct State {
|
||||||
is_dragging: bool,
|
is_dragging: bool,
|
||||||
cache: Mutex<SwashCache>,
|
cache: Mutex<SwashCache>,
|
||||||
|
handle: Mutex<image::Handle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
|
@ -451,6 +447,8 @@ impl State {
|
||||||
State {
|
State {
|
||||||
is_dragging: false,
|
is_dragging: false,
|
||||||
cache: Mutex::new(SwashCache::new()),
|
cache: Mutex::new(SwashCache::new()),
|
||||||
|
//TODO: make option!
|
||||||
|
handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue