From fba9ac39abd53656f4500b7dfb90e71ced20ca5d Mon Sep 17 00:00:00 2001 From: ketmar Date: Wed, 10 Nov 2021 11:09:43 +0000 Subject: [PATCH] efgx: some cosmetix FossilOrigin-Name: 42d65d271a6995f59a978d39e2a4955a3a39ef14b45890c5e82d7182228854ce --- egfx/base.d | 143 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 67 insertions(+), 76 deletions(-) diff --git a/egfx/base.d b/egfx/base.d index f8c82be..8569a20 100644 --- a/egfx/base.d +++ b/egfx/base.d @@ -100,6 +100,15 @@ nothrow @safe @nogc: } /// + this (int awidth, int aheight) { + pragma(inline, true); + x0 = 0; + y0 = 0; + width = awidth; + height = aheight; + } + + /// this() (in auto ref GxPoint xy0, int awidth, int aheight) { pragma(inline, true); x0 = xy0.x; @@ -149,7 +158,7 @@ nothrow @safe @nogc: @property void x1 (in int val) { pragma(inline, true); width = val-x0+1; } /// @property void y1 (in int val) { pragma(inline, true); height = val-y0+1; } /// - /// + /// is point inside this rect? bool inside() (in auto ref GxPoint p) pure const { pragma(inline, true); return (width > 0 && height > 0 ? (p.x >= x0 && p.y >= y0 && p.x < x0+width && p.y < y0+height) : false); @@ -171,7 +180,7 @@ nothrow @safe @nogc: r.x0+r.width <= x0+width && r.y0+r.height <= y0+height; } - /// is `r` and `this` overlaps? + /// does `r` and `this` overlap? bool overlaps() (in auto ref GxRect r) pure const { pragma(inline, true); return @@ -201,19 +210,35 @@ nothrow @safe @nogc: /// clip `this` so it will not be larger than `r` /// returns `false` if the resulting rect (this) is empty or invalid - bool intersect() (in auto ref GxRect r) { - if (r.invalid || invalid) { width = height = -1; return false; } - if (r.empty || empty) { width = height = 0; return false; } - if (r.y1 < y0 || r.x1 < x0 || r.x0 > x1 || r.y0 > y1) { width = height = 0; return false; } + bool intersect (in int rx0, in int ry0, in int rwdt, in int rhgt) { + if (rwdt < 0 || rhgt < 0 || invalid) { width = height = -1; return false; } + if (rwdt == 0 || rhgt == 0 || empty) { width = height = 0; return false; } + immutable int rx1 = rx0+rwdt-1; + immutable int ry1 = ry0+rhgt-1; + if (ry1 < y0 || rx1 < x0 || rx0 > x1 || ry0 > y1) { width = height = 0; return false; } // rc is at least partially inside this rect - if (x0 < r.x0) x0 = r.x0; - if (y0 < r.y0) y0 = r.y0; - if (x1 > r.x1) x1 = r.x1; - if (y1 > r.y1) y1 = r.y1; + if (x0 < rx0) x0 = rx0; + if (y0 < ry0) y0 = ry0; + if (x1 > rx1) x1 = rx1; + if (y1 > ry1) y1 = ry1; assert(!empty); // yeah, always return true; } + /// clip `this` so it will not be larger than `r` + /// returns `false` if the resulting rect (this) is empty or invalid + bool intersect (in int rwdt, in int rhgt) { + pragma(inline, true); + return intersect(0, 0, rwdt, rhgt); + } + + /// clip `this` so it will not be larger than `r` + /// returns `false` if the resulting rect (this) is empty or invalid + bool intersect() (in auto ref GxRect r) { + pragma(inline, true); + return intersect(r.x0, r.y0, r.width, r.height); + } + /// void shrinkBy (in int dx, in int dy) { pragma(inline, true); @@ -280,38 +305,36 @@ nothrow @safe @nogc: } /** - * clip (x,y,len) stripe to this rect + * clip (x,y,wdt) stripe to this rect * * Params: * x = stripe start (not relative to rect) * y = stripe start (not relative to rect) - * len = stripe length + * wdt = stripe length * * Returns: * x = fixed x (invalid if result is false) - * len = fixed length (invalid if result is false) + * wdt = fixed length (invalid if result is false) * leftSkip = how much cells skipped at the left side (invalid if result is false) * result = false if stripe is completely clipped out - * - * TODO: - * overflows */ - bool clipHStripe (ref int x, int y, ref int len, int* leftSkip=null) const @trusted { + bool clipHStripe (ref int x, int y, ref int wdt, int* leftSkip=null) const @trusted { if (empty) return false; - if (len <= 0 || y < y0 || y >= y0+height || x >= x0+width) return false; + if (wdt <= 0 || y < y0 || y >= y0+height || x >= x0+width) return false; if (x < x0) { // left clip - if (x+len <= x0) return false; immutable int dx = x0-x; + if (dx >= wdt) return false; // avoid overflow if (leftSkip !is null) *leftSkip = dx; - len -= dx; + wdt -= dx; x = x0; - assert(len > 0); // yeah, always + assert(wdt > 0); // yeah, always } - if (x+len > x0+width) { + if (wdt > width) wdt = width; // avoid overflow + if (x+wdt > x0+width) { // right clip - len = x0+width-x; - assert(len > 0); // yeah, always + wdt = x0+width-x; + assert(wdt > 0); // yeah, always } return true; } @@ -329,22 +352,20 @@ nothrow @safe @nogc: * hgt = fixed length (invalid if result is false) * topSkip = how much cells skipped at the top side (invalid if result is false) * result = false if stripe is completely clipped out - * - * TODO: - * overflows */ bool clipVStripe (int x, ref int y, ref int hgt, int* topSkip=null) const @trusted { if (empty) return false; if (hgt <= 0 || x < x0 || x >= x0+width || y >= y0+height) return false; if (y < y0) { // top clip - if (y+hgt <= y0) return false; immutable int dy = y0-y; + if (dy >= hgt) return false; // avoid overflow if (topSkip !is null) *topSkip = dy; hgt -= dy; y = y0; assert(hgt > 0); // yeah, always } + if (hgt > height) hgt = height; // avoid overflow if (y+hgt > y0+height) { // bottom clip hgt = y0+height-y; @@ -357,37 +378,9 @@ nothrow @safe @nogc: bool clipHVStripes (ref int x, ref int y, ref int wdt, ref int hgt, int* leftSkip=null, int* topSkip=null) const @trusted { if (empty || wdt <= 0 || hgt <= 0) return false; if (y >= y0+height || x >= x0+width) return false; - if (x < x0) { - // left clip - if (x+wdt <= x0) return false; - immutable int dx = x0-x; - if (leftSkip !is null) *leftSkip = dx; - wdt -= dx; - x = x0; - assert(wdt > 0); // yeah, always - } - if (x+wdt > x0+width) { - // right clip - wdt = x0+width-x; - assert(wdt > 0); // yeah, always - } - - if (y < y0) { - // top clip - if (y+hgt <= y0) return false; - immutable int dy = y0-y; - if (topSkip !is null) *topSkip = dy; - hgt -= dy; - y = y0; - assert(hgt > 0); // yeah, always - } - if (y+hgt > y0+height) { - // bottom clip - hgt = y0+height-y; - assert(hgt > 0); // yeah, always - } - - return true; + // use dummy `x` and `y` for horizontal and vertical clippers, because they are only checked for validity there + if (!clipHStripe(ref x, y0, ref wdt, leftSkip)) return false; + return clipVStripe(x0, ref y, ref hgt, topSkip); } } @@ -468,7 +461,7 @@ public enum gxRGBA(int r, int g, int b, int a) = (clampToByte(a)<<24)|(clampToBy // ////////////////////////////////////////////////////////////////////////// // // current clip rect -public __gshared GxRect gxClipRect = GxRect(0, 0, 65535, 65535); +public __gshared GxRect gxClipRect = GxRect(65535, 65535); public void gxWithSavedClip(DG) (scope DG dg) if (is(typeof((inout int=0) { DG dg = void; dg(); }))) @@ -483,7 +476,7 @@ if (is(typeof((inout int=0) { DG dg = void; dg(); }))) public void gxClipReset () nothrow @trusted @nogc { pragma(inline, true); - gxClipRect = GxRect(0, 0, 65535, 65535); + gxClipRect = GxRect(65535, 65535); } @@ -537,7 +530,7 @@ public void gxSetPixel() (in auto ref GxPoint p, in uint c) nothrow @trusted @no public void gxHLine (int x, int y, int w, in uint clr) nothrow @trusted @nogc { if (gxIsTransparent(clr)) return; if (!gxClipRect.clipHStripe(x, y, w)) return; - if (!GxRect(0, 0, VBufWidth, VBufHeight).clipHStripe(x, y, w)) return; + if (!GxRect(VBufWidth, VBufHeight).clipHStripe(x, y, w)) return; if (gxIsSolid(clr)) { immutable uint addr = y*VBufWidth+x; if (gxIsSolidBlack(clr)) { @@ -559,7 +552,7 @@ public void gxHLine() (in auto ref GxPoint p, in int w, in uint clr) nothrow @tr public void gxVLine (int x, int y, int h, in uint clr) nothrow @trusted @nogc { if (gxIsTransparent(clr)) return; if (!gxClipRect.clipVStripe(x, y, h)) return; - if (!GxRect(0, 0, VBufWidth, VBufHeight).clipVStripe(x, y, h)) return; + if (!GxRect(VBufWidth, VBufHeight).clipVStripe(x, y, h)) return; uint* dptr = vglTexBuf+y*VBufWidth+x; if (gxIsSolid(clr)) { while (h-- > 0) { *dptr = clr; dptr += VBufWidth; } @@ -575,7 +568,7 @@ public void gxVLine() (in auto ref GxPoint p, in int h, in uint clr) nothrow @tr public void gxFillRect (int x, int y, int w, int h, in uint clr) nothrow @trusted @nogc { if (gxIsTransparent(clr)) return; if (!gxClipRect.clipHVStripes(x, y, w, h)) return; - if (!GxRect(0, 0, VBufWidth, VBufHeight).clipHVStripes(x, y, w, h)) return; + if (!GxRect(VBufWidth, VBufHeight).clipHVStripes(x, y, w, h)) return; if (gxIsSolid(clr)) { uint addr = y*VBufWidth+x; if (gxIsSolidBlack(clr)) { @@ -645,17 +638,15 @@ public void gxDrawWindow (const(char)[] title, in uint framecolor, in uint title gxFillRect(gxClipRect, windowcolor); gxDrawRect(gxClipRect, framecolor); - if (title !is null) { - gxWithSavedClip{ - immutable GxRect rc = gxClipRect; - int hgt = (gxTextHeightUtf < 10 ? 10 : gxTextHeightUtf+1); - gxClipRect.intersect(GxRect(rc.x0+1, rc.y0+1, rc.width-2, hgt)); - if (!gxClipRect.empty) { - gxFillRect(gxClipRect, titlebackcolor); - gxDrawTextUtf(gxClipRect.x0+(gxClipRect.width-gxTextWidthUtf(title))/2, gxClipRect.y0+(hgt-gxTextHeightUtf)/2, title, titlecolor); - } - }; - } + if (title is null) return; + gxWithSavedClip{ + immutable GxRect rc = gxClipRect; + immutable int hgt = (gxTextHeightUtf < 10 ? 10 : gxTextHeightUtf+1); + if (gxClipRect.intersect(rc.x0+1, rc.y0+1, rc.width-2, hgt)) { + gxFillRect(gxClipRect, titlebackcolor); + gxDrawTextUtf(gxClipRect.x0+(gxClipRect.width-gxTextWidthUtf(title))/2, gxClipRect.y0+(hgt-gxTextHeightUtf)/2, title, titlecolor); + } + }; } @@ -975,7 +966,7 @@ public void gxDrawLine (int x0, int y0, int x1, int y1, in uint clr, in bool las if (gxIsTransparent(clr)) return; GxRect realClip = gxClipRect; - if (!realClip.intersect(GxRect(0, 0, VBufWidth, VBufHeight))) return; + if (!realClip.intersect(VBufWidth, VBufHeight)) return; // just a point? if (x0 == x1 && y0 == y1) { -- 2.11.4.GIT