From 5f56b96dd2b1f9b730a86f7c84c1bb8f135afb98 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 31 May 2022 21:49:12 -0700 Subject: [PATCH] background: allow repeat to mirror --- config/src/background.rs | 4 ++++ wezterm-gui/src/quad.rs | 16 ++++++++++++---- wezterm-gui/src/termwindow/background.rs | 24 ++++++++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/config/src/background.rs b/config/src/background.rs index 78a564dc4..26ae676c2 100644 --- a/config/src/background.rs +++ b/config/src/background.rs @@ -222,6 +222,10 @@ pub enum BackgroundRepeat { /// Repeat as much as possible to cover the area. /// The last image will be clipped if it doesn't fit. Repeat, + /// Like Repeat, except that the image is alternately + /// mirrored. Helpful when the image doesn't seamlessly + /// tile. + Mirror, /* /// Repeat as much as possible without clipping. /// The first and last images are aligned with the edges, diff --git a/wezterm-gui/src/quad.rs b/wezterm-gui/src/quad.rs index 53802bb8d..87f76a3bc 100644 --- a/wezterm-gui/src/quad.rs +++ b/wezterm-gui/src/quad.rs @@ -45,10 +45,18 @@ pub struct Quad<'a> { impl<'a> Quad<'a> { /// Assign the texture coordinates pub fn set_texture(&mut self, coords: TextureRect) { - self.vert[V_TOP_LEFT].tex = (coords.min_x(), coords.min_y()); - self.vert[V_TOP_RIGHT].tex = (coords.max_x(), coords.min_y()); - self.vert[V_BOT_LEFT].tex = (coords.min_x(), coords.max_y()); - self.vert[V_BOT_RIGHT].tex = (coords.max_x(), coords.max_y()); + let x1 = coords.min_x(); + let x2 = coords.max_x(); + let y1 = coords.min_y(); + let y2 = coords.max_y(); + self.set_texture_discrete(x1, x2, y1, y2); + } + + pub fn set_texture_discrete(&mut self, x1: f32, x2: f32, y1: f32, y2: f32) { + self.vert[V_TOP_LEFT].tex = (x1, y1); + self.vert[V_TOP_RIGHT].tex = (x2, y1); + self.vert[V_BOT_LEFT].tex = (x1, y2); + self.vert[V_BOT_RIGHT].tex = (x2, y2); } /// Set the color glyph "flag" diff --git a/wezterm-gui/src/termwindow/background.rs b/wezterm-gui/src/termwindow/background.rs index 413664177..c2c448ed8 100644 --- a/wezterm-gui/src/termwindow/background.rs +++ b/wezterm-gui/src/termwindow/background.rs @@ -465,19 +465,22 @@ impl crate::TermWindow { .unwrap_or(height); // log::info!("computed {width}x{height}"); + + let mut start_tile = 0; if let Some(factor) = layer.def.attachment.scroll_factor() { let distance = top as f32 * self.render_metrics.cell_size.height as f32 * factor; let num_tiles = distance / repeat_y; origin_y -= num_tiles.fract() * repeat_y; + start_tile = num_tiles.floor() as usize; } let limit_y = top_pixel + pixel_height; - for y_step in 0.. { - let offset_y = y_step as f32 * repeat_y; + for y_step in start_tile.. { + let offset_y = (y_step - start_tile) as f32 * repeat_y; let origin_y = origin_y + offset_y; if origin_y >= limit_y - || (y_step > 0 && layer.def.repeat_y == BackgroundRepeat::NoRepeat) + || (y_step > start_tile && layer.def.repeat_y == BackgroundRepeat::NoRepeat) { break; } @@ -493,7 +496,20 @@ impl crate::TermWindow { let mut quad = layer0.allocate()?; // log::info!("quad {origin_x},{origin_y} {width}x{height}"); quad.set_position(origin_x, origin_y, origin_x + width, origin_y + height); - quad.set_texture(sprite.texture_coords()); + + let coords = sprite.texture_coords(); + let mut x1 = coords.min_x(); + let mut x2 = coords.max_x(); + let mut y1 = coords.min_y(); + let mut y2 = coords.max_y(); + if layer.def.repeat_x == BackgroundRepeat::Mirror && x_step % 2 == 1 { + std::mem::swap(&mut x1, &mut x2); + } + if layer.def.repeat_y == BackgroundRepeat::Mirror && y_step % 2 == 1 { + std::mem::swap(&mut y1, &mut y2); + } + + quad.set_texture_discrete(x1, x2, y1, y2); quad.set_is_background_image(); quad.set_hsv(Some(layer.def.hsb)); quad.set_fg_color(color);