Skip to content

Commit

Permalink
Add anchor options
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusey committed Jan 6, 2025
1 parent 61785ad commit 3e2326f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF },
/// * `tiled` - Image is repeated horizontally and vertically to fill the window.
/// * `centered` - Image is centered in the window and displayed 1-to-1 pixel
/// scale, preserving both the aspect ratio and the image size.
/// * `upper-left` - Image is anchored to the upper left corner of the window,
/// preserving the aspect ratio.
/// * `upper-right` - Image is anchored to the upper right corner of the window,
/// preserving the aspect ratio.
/// * `lower-left` - Image is anchored to the lower left corner of the window,
/// preserving the aspect ratio.
/// * `lower-right` - Image is anchored to the lower right corner of the window,
/// preserving the aspect ratio.
///
@"background-image-mode": BackgroundImageMode = .zoomed,

Expand Down Expand Up @@ -5699,6 +5707,10 @@ pub const BackgroundImageMode = enum(u8) {
stretched = 1,
tiled = 2,
centered = 3,
upper_left = 4,
upper_right = 5,
lower_left = 6,
lower_right = 7,
};

/// See freetype-load-flag
Expand Down
30 changes: 29 additions & 1 deletion src/renderer/shaders/bgimage.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const uint MODE_ZOOMED = 0u;
const uint MODE_STRETCHED = 1u;
const uint MODE_TILED = 2u;
const uint MODE_CENTERED = 3u;
const uint MODE_UPPER_LEFT = 4u;
const uint MODE_UPPER_RIGHT = 5u;
const uint MODE_LOWER_LEFT = 6u;
const uint MODE_LOWER_RIGHT = 7u;

layout (location = 0) in vec2 terminal_size;
layout (location = 1) in uint mode;
Expand Down Expand Up @@ -43,6 +47,10 @@ void main() {
}
break;
case MODE_CENTERED:
case MODE_UPPER_LEFT:
case MODE_UPPER_RIGHT:
case MODE_LOWER_LEFT:
case MODE_LOWER_RIGHT:
// If centered, the final scale of the image should match the actual
// size of the image and should be centered
scale.x = image_size.x / terminal_size.x;
Expand All @@ -55,7 +63,27 @@ void main() {
}

vec2 final_image_size = terminal_size * position * scale;
vec2 offset = (terminal_size * (1.0 - scale)) / 2.0;
vec2 offset = vec2(0.0, 0.0);
switch (mode) {
case MODE_ZOOMED:
case MODE_STRETCHED:
case MODE_TILED:
case MODE_CENTERED:
offset = (terminal_size * (1.0 - scale)) / 2.0;
break;
case MODE_UPPER_LEFT:
offset = vec2(0.0, 0.0);
break;
case MODE_UPPER_RIGHT:
offset = vec2(terminal_size.x - image_size.x, 0.0);
break;
case MODE_LOWER_LEFT:
offset = vec2(0.0, terminal_size.y - image_size.y);
break;
case MODE_LOWER_RIGHT:
offset = vec2(terminal_size.x - image_size.x, terminal_size.y - image_size.y);
break;
}
gl_Position = projection * vec4(final_image_size.xy + offset, 0.0, 1.0);
tex_coord = position;
if (mode == MODE_TILED) {
Expand Down

0 comments on commit 3e2326f

Please sign in to comment.