summaryrefslogtreecommitdiff
path: root/src/client/guiscalingfilter.cpp
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2019-06-22 15:03:54 +0100
committerSmallJoker <SmallJoker@users.noreply.github.com>2019-06-22 16:03:54 +0200
commit429a98964859b83016f2eb47a47a08ab8dc3c57e (patch)
treeb02a16132e5cdf6696917910921942c464bc39c6 /src/client/guiscalingfilter.cpp
parent4e3c1916f731058a137ec1f9eecf2b7eff4d8fcc (diff)
downloadminetest-429a98964859b83016f2eb47a47a08ab8dc3c57e.tar.gz
minetest-429a98964859b83016f2eb47a47a08ab8dc3c57e.tar.bz2
minetest-429a98964859b83016f2eb47a47a08ab8dc3c57e.zip
Add support for 9-sliced backgrounds (#8600)
9-slice textures are commonly used in GUIs to allow scaling them to match any resolution without distortion. https://en.wikipedia.org/wiki/9-slice_scaling
Diffstat (limited to 'src/client/guiscalingfilter.cpp')
-rw-r--r--src/client/guiscalingfilter.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/client/guiscalingfilter.cpp b/src/client/guiscalingfilter.cpp
index 312f93939..3490c47e8 100644
--- a/src/client/guiscalingfilter.cpp
+++ b/src/client/guiscalingfilter.cpp
@@ -167,3 +167,62 @@ void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
driver->draw2DImage(scaled, destrect, mysrcrect, cliprect, colors, usealpha);
}
+
+void draw2DImage9Slice(video::IVideoDriver *driver, video::ITexture *texture,
+ const core::rect<s32> &rect, const core::rect<s32> &middle)
+{
+ const video::SColor color(255,255,255,255);
+ const video::SColor colors[] = {color,color,color,color};
+
+ auto originalSize = texture->getOriginalSize();
+ core::vector2di lowerRightOffset = core::vector2di(originalSize.Width, originalSize.Height) - middle.LowerRightCorner;
+
+ for (int y = 0; y < 3; ++y) {
+ for (int x = 0; x < 3; ++x) {
+ core::rect<s32> src({0, 0}, originalSize);
+ core::rect<s32> dest = rect;
+
+ switch (x) {
+ case 0:
+ dest.LowerRightCorner.X = rect.UpperLeftCorner.X + middle.UpperLeftCorner.X;
+ src.LowerRightCorner.X = middle.UpperLeftCorner.X;
+ break;
+
+ case 1:
+ dest.UpperLeftCorner.X += middle.UpperLeftCorner.X;
+ dest.LowerRightCorner.X -= lowerRightOffset.X;
+ src.UpperLeftCorner.X = middle.UpperLeftCorner.X;
+ src.LowerRightCorner.X = middle.LowerRightCorner.X;
+ break;
+
+ case 2:
+ dest.UpperLeftCorner.X = rect.LowerRightCorner.X - lowerRightOffset.X;
+ src.UpperLeftCorner.X = middle.LowerRightCorner.X;
+ break;
+ }
+
+ switch (y) {
+ case 0:
+ dest.LowerRightCorner.Y = rect.UpperLeftCorner.Y + middle.UpperLeftCorner.Y;
+ src.LowerRightCorner.Y = middle.UpperLeftCorner.Y;
+ break;
+
+ case 1:
+ dest.UpperLeftCorner.Y += middle.UpperLeftCorner.Y;
+ dest.LowerRightCorner.Y -= lowerRightOffset.Y;
+ src.UpperLeftCorner.Y = middle.UpperLeftCorner.Y;
+ src.LowerRightCorner.Y = middle.LowerRightCorner.Y;
+ break;
+
+ case 2:
+ dest.UpperLeftCorner.Y = rect.LowerRightCorner.Y - lowerRightOffset.Y;
+ src.UpperLeftCorner.Y = middle.LowerRightCorner.Y;
+ break;
+ }
+
+ draw2DImageFilterScaled(driver, texture, dest,
+ src,
+ NULL/*&AbsoluteClippingRect*/, colors, true);
+ }
+ }
+}