Add clamped to option background_image_layout

This commit is contained in:
itepechi 2021-10-26 03:18:36 +09:00
parent cae31ab336
commit 86d7aaa03a
9 changed files with 17 additions and 19 deletions

View File

@ -8,7 +8,7 @@
#define tex_right 1
#define tex_bottom 1
uniform float tiled;
uniform float unscaled;
uniform vec2 translate; // [ left, top ]
uniform vec4 sizes; // [ window_width, window_height, image_width, image_height ]
@ -28,16 +28,12 @@ const vec2 tex_map[] = vec2[4](
);
float scale_factor(float window, float image) {
return window / image;
}
float tiling_factor(int i) {
return tiled * scale_factor(sizes[i], sizes[i + 2]) + (1 - tiled);
float unscaling_factor(int i) {
return unscaled * (sizes[i] / sizes[i + 2]) + (1 - unscaled);
}
void main() {
vec2 tex_coords = tex_map[gl_VertexID];
texcoord = vec2(tex_coords[0] * tiling_factor(0) - translate[0], tex_coords[1] * tiling_factor(1) - translate[1]);
texcoord = vec2(tex_coords[0] * unscaling_factor(0) - translate[0], tex_coords[1] * unscaling_factor(1) - translate[1]);
gl_Position = vec4(pos_map[gl_VertexID], 0, 1);
}

View File

@ -64,7 +64,7 @@ typedef enum MouseTrackingModes { NO_TRACKING, BUTTON_MODE, MOTION_MODE, ANY_MOD
typedef enum MouseTrackingProtocols { NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOCOL, URXVT_PROTOCOL, SGR_PIXEL_PROTOCOL} MouseTrackingProtocol;
typedef enum MouseShapes { BEAM, HAND, ARROW } MouseShape;
typedef enum { NONE, MENUBAR, WINDOW, ALL } WindowTitleIn;
typedef enum { TILING, SCALED, MIRRORED } BackgroundImageLayout;
typedef enum { TILING, SCALED, MIRRORED, CLAMPED } BackgroundImageLayout;
typedef enum { NORTHWEST, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, CENTER } BackgroundImageAnchor;
#define MAX_CHILDREN 512

View File

@ -1106,8 +1106,8 @@ opt('background_image', 'none',
)
opt('background_image_layout', 'tiled',
choices=('mirror-tiled', 'scaled', 'tiled'), ctype='bglayout',
long_text='Whether to tile or scale the background image.'
choices=('mirror-tiled', 'scaled', 'tiled', 'clamped'), ctype='bglayout',
long_text='Whether to tile, scale or clamp the background image.'
)
opt('background_image_anchor', 'northwest',

View File

@ -71,7 +71,7 @@ class Parser:
raise ValueError(f"The value {val} is not a valid choice for background_image_layout")
ans["background_image_layout"] = val
choices_for_background_image_layout = frozenset(('mirror-tiled', 'scaled', 'tiled'))
choices_for_background_image_layout = frozenset(('mirror-tiled', 'scaled', 'tiled', 'clamped'))
def background_image_linear(self, val: str, ans: typing.Dict[str, typing.Any]) -> None:
ans['background_image_linear'] = to_bool(val)

View File

@ -72,6 +72,7 @@ bglayout(PyObject *layout_name) {
case 't': return TILING;
case 'm': return MIRRORED;
case 's': return SCALED;
case 'c': return CLAMPED;
default: break;
}
return TILING;

View File

@ -15,7 +15,7 @@ import kitty.types
if typing.TYPE_CHECKING:
choices_for_background_image_anchor = typing.Literal['northwest', 'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'center']
choices_for_background_image_layout = typing.Literal['mirror-tiled', 'scaled', 'tiled']
choices_for_background_image_layout = typing.Literal['mirror-tiled', 'scaled', 'tiled', 'clamped']
choices_for_default_pointer_shape = typing.Literal['arrow', 'beam', 'hand']
choices_for_linux_display_server = typing.Literal['auto', 'wayland', 'x11']
choices_for_macos_show_window_title_in = typing.Literal['all', 'menubar', 'none', 'window']

View File

@ -49,7 +49,7 @@ Change the configured background image which is used for new OS windows.
--layout
type=choices
choices=tiled,scaled,mirror-tiled,configured
choices=tiled,scaled,mirror-tiled,clamped,configured
How the image should be displayed. The value of configured will use the configured value.

View File

@ -166,7 +166,7 @@ typedef struct {
static CellProgramLayout cell_program_layouts[NUM_PROGRAMS];
static ssize_t blit_vertex_array;
typedef struct {
GLint image_location, tiled_location, translate_location, sizes_location, opacity_location, premult_location;
GLint image_location, unscaled_location, translate_location, sizes_location, opacity_location, premult_location;
} BGImageProgramLayout;
static BGImageProgramLayout bgimage_program_layout = {0};
typedef struct {
@ -198,7 +198,7 @@ init_cell_program(void) {
bgimage_program_layout.image_location = get_uniform_location(BGIMAGE_PROGRAM, "image");
bgimage_program_layout.opacity_location = get_uniform_location(BGIMAGE_PROGRAM, "opacity");
bgimage_program_layout.sizes_location = get_uniform_location(BGIMAGE_PROGRAM, "sizes");
bgimage_program_layout.tiled_location = get_uniform_location(BGIMAGE_PROGRAM, "tiled");
bgimage_program_layout.unscaled_location = get_uniform_location(BGIMAGE_PROGRAM, "unscaled");
bgimage_program_layout.translate_location = get_uniform_location(BGIMAGE_PROGRAM, "translate");
bgimage_program_layout.premult_location = get_uniform_location(BGIMAGE_PROGRAM, "premult");
tint_program_layout.tint_color_location = get_uniform_location(TINT_PROGRAM, "tint_color");
@ -408,16 +408,16 @@ draw_bg(OSWindow *w) {
bind_program(BGIMAGE_PROGRAM);
bind_vertex_array(blit_vertex_array);
const bool unscaled = OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED || OPT(background_image_layout) == CLAMPED;
static bool bgimage_constants_set = false;
if (!bgimage_constants_set) {
glUniform1i(bgimage_program_layout.image_location, BGIMAGE_UNIT);
glUniform1f(bgimage_program_layout.opacity_location, OPT(background_opacity));
GLfloat tiled = (OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) ? 1 : 0;
glUniform1f(bgimage_program_layout.tiled_location, tiled);
glUniform1f(bgimage_program_layout.unscaled_location, (GLfloat)unscaled);
bgimage_constants_set = true;
}
float translate_left = 0.0f, translate_top = 0.0f;
if (OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) {
if (unscaled) {
if (OPT(background_image_anchor) == NORTH || OPT(background_image_anchor) == CENTER || OPT(background_image_anchor) == SOUTH) {
translate_left = ((float)w->window_width / 2.0f - (float)w->bgimage->width / 2.0f) / (float)w->bgimage->width;
} else if (OPT(background_image_anchor) == NORTHEAST || OPT(background_image_anchor) == EAST || OPT(background_image_anchor) == SOUTHEAST) {

View File

@ -137,6 +137,7 @@ send_bgimage_to_gpu(BackgroundImageLayout layout, BackgroundImage *bgimage) {
RepeatStrategy r;
switch (layout) {
case SCALED:
case CLAMPED:
r = REPEAT_CLAMP; break;
case MIRRORED:
r = REPEAT_MIRROR; break;