Skip to content

Commit

Permalink
Add NINE_PATCH_CENTER Axis Mode
Browse files Browse the repository at this point in the history
This mode keeps the NinePatch's middle always centered and always at its original size, stretching its edges if necessary. Useful for tooltips, handles & similar.
  • Loading branch information
Mickeon committed Jan 5, 2024
1 parent 179dfdc commit cbeaecc
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions doc/classes/NinePatchRect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@
<constant name="AXIS_STRETCH_MODE_TILE_FIT" value="2" enum="AxisStretchMode">
Repeats the center texture across the NinePatchRect, but will also stretch the texture to make sure each tile is visible in full. This may cause the texture to be distorted, but less than [constant AXIS_STRETCH_MODE_STRETCH]. The texture must be seamless for this to work without displaying artifacts between edges.
</constant>
<constant name="AXIS_STRETCH_MODE_CENTER" value="3" enum="AxisStretchMode">
Keep center slices centered. Useful for tooltips with arrow or controls with handles.
</constant>
</constants>
</class>
3 changes: 3 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5137,6 +5137,9 @@
<constant name="NINE_PATCH_TILE_FIT" value="2" enum="NinePatchAxisMode">
The nine patch gets filled with tiles where needed and stretches them a bit if needed.
</constant>
<constant name="NINE_PATCH_CENTER" value="3" enum="NinePatchAxisMode">
The nine patch gets stretched where needed, keeping the middle centered at its original size.
</constant>
<constant name="CANVAS_ITEM_TEXTURE_FILTER_DEFAULT" value="0" enum="CanvasItemTextureFilter">
Uses the default filter mode for this [Viewport].
</constant>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/StyleBoxTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,8 @@
<constant name="AXIS_STRETCH_MODE_TILE_FIT" value="2" enum="AxisStretchMode">
Repeats the stylebox's texture to match the stylebox's size according to the nine-patch system. Unlike [constant AXIS_STRETCH_MODE_TILE], the texture may be slightly stretched to make the nine-patch texture tile seamlessly.
</constant>
<constant name="AXIS_STRETCH_MODE_CENTER" value="3" enum="AxisStretchMode">
Keep center slices centered. Useful for tooltips with arrow or controls with handles.
</constant>
</constants>
</class>
3 changes: 3 additions & 0 deletions drivers/gles3/shaders/canvas.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, flo
ratio = mod(ratio * scale, 1.0);
// Scale to source texture.
return (margin_begin + ratio * dst_area) * tex_pixel_size;
} else if (np_repeat == 3) { // Center.
const float CMP_EPSILON = 1e-5;
return clamp(pixel - (draw_size - tex_size) / 2.0, margin_begin + CMP_EPSILON, tex_size - margin_end - CMP_EPSILON) * tex_pixel_size;
} else { // Shouldn't happen, but silences compiler warning.
return 0.0;
}
Expand Down
5 changes: 3 additions & 2 deletions scene/gui/nine_patch_rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ void NinePatchRect::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_RIGHT);
ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_BOTTOM);
ADD_GROUP("Axis Stretch", "axis_stretch_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit,Center"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit,Center"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");

BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_CENTER);
}

void NinePatchRect::_texture_changed() {
Expand Down
1 change: 1 addition & 0 deletions scene/gui/nine_patch_rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NinePatchRect : public Control {
AXIS_STRETCH_MODE_STRETCH,
AXIS_STRETCH_MODE_TILE,
AXIS_STRETCH_MODE_TILE_FIT,
AXIS_STRETCH_MODE_CENTER,
};

bool draw_center = true;
Expand Down
5 changes: 3 additions & 2 deletions scene/resources/style_box_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ void StyleBoxTexture::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM);

ADD_GROUP("Axis Stretch", "axis_stretch_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit,Center"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit,Center"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");

ADD_GROUP("Sub-Region", "region_");
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
Expand All @@ -236,6 +236,7 @@ void StyleBoxTexture::_bind_methods() {
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_CENTER);
}

StyleBoxTexture::StyleBoxTexture() {}
Expand Down
1 change: 1 addition & 0 deletions scene/resources/style_box_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class StyleBoxTexture : public StyleBox {
AXIS_STRETCH_MODE_STRETCH,
AXIS_STRETCH_MODE_TILE,
AXIS_STRETCH_MODE_TILE_FIT,
AXIS_STRETCH_MODE_CENTER,
};

private:
Expand Down
3 changes: 3 additions & 0 deletions servers/rendering/renderer_rd/shaders/canvas.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, flo
ratio = mod(ratio * scale, 1.0);
// Scale to source texture.
return (margin_begin + ratio * dst_area) * tex_pixel_size;
} else if (np_repeat == 3) { // Center.
const float CMP_EPSILON = 1e-5;
return clamp(pixel - (draw_size - tex_size) / 2.0, margin_begin + CMP_EPSILON, tex_size - margin_end - CMP_EPSILON) * tex_pixel_size;
} else { // Shouldn't happen, but silences compiler warning.
return 0.0;
}
Expand Down
1 change: 1 addition & 0 deletions servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3202,6 +3202,7 @@ void RenderingServer::_bind_methods() {
BIND_ENUM_CONSTANT(NINE_PATCH_STRETCH);
BIND_ENUM_CONSTANT(NINE_PATCH_TILE);
BIND_ENUM_CONSTANT(NINE_PATCH_TILE_FIT);
BIND_ENUM_CONSTANT(NINE_PATCH_CENTER);

BIND_ENUM_CONSTANT(CANVAS_ITEM_TEXTURE_FILTER_DEFAULT);
BIND_ENUM_CONSTANT(CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
Expand Down
1 change: 1 addition & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ class RenderingServer : public Object {
NINE_PATCH_STRETCH,
NINE_PATCH_TILE,
NINE_PATCH_TILE_FIT,
NINE_PATCH_CENTER,
};

virtual void canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = -1.0, bool p_antialiased = false) = 0;
Expand Down

0 comments on commit cbeaecc

Please sign in to comment.