diff mde/gui/widget/Window.d @ 46:03fa79a48c48

Fixed resizing bugs in previous commit and made code cleaner and more efficient. setSize replaced by setWidth & setHeight. setPosition must be called after setWidth/Height. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 22 May 2008 12:51:47 +0100
parents 0fd51d2c6c8a
children d43523ed4b62
line wrap: on
line diff
--- a/mde/gui/widget/Window.d	Thu May 22 11:34:09 2008 +0100
+++ b/mde/gui/widget/Window.d	Thu May 22 12:51:47 2008 +0100
@@ -86,6 +86,11 @@
         widgetY = y + border.t;         // must be updated if the window is moved
         widget.setPosition (widgetX, widgetY);
         
+        // Calculate mw/mh and xw/yh (cached data):
+        widget.getMinimalSize (mw, mh);
+        mw += border.l + border.r;
+        mh += border.t + border.b;
+        
         xw = x+w;
         yh = y+h;
     }
@@ -206,11 +211,7 @@
     }
     
     void getMinimalSize (out int wM, out int hM) {
-        if (mh < 0) {       // calculate if necessary
-            widget.getMinimalSize (mw, mh);
-            mw += border.l + border.r;
-            mh += border.t + border.b;
-        }
+        // mw/mh are calculated by finalise();
         wM = mw;
         hM = mh;
     }
@@ -219,17 +220,17 @@
         ch = h;
     }
     
-    void setSize (int nw, int nh, bool wB, bool hB) {
-        getMinimalSize (w,h);
-        if (nw > w) w = nw;     // expand if new size is larger, but don't go smaller
-        if (nh > h) h = nh;
-        
-        xw = x+w;
-        yh = y+h;
-        
-        widget.setSize (w - border.l - border.r, h - border.t - border.b, wB, hB);
-        
-        gui_.requestRedraw ();  // obviously necessary whenever the window's size is changed
+    void setWidth (int nw, int dir) {
+        if (nw < mw) w = mw;	// clamp
+        else w = nw;
+        xw = x + w;
+        widget.setWidth (w - border.l - border.r, dir);
+    }
+    void setHeight (int nh, int dir) {
+        if (nh < mh) h = mh;	// clamp
+        else h = nh;
+        yh = y + h;
+        widget.setHeight (h - border.t - border.b, dir);
     }
     
     void setPosition (int nx, int ny) {
@@ -244,7 +245,7 @@
         
         widget.setPosition (widgetX, widgetY);
         
-        gui_.requestRedraw ();  // obviously necessary whenever the window is moved
+        gui_.requestRedraw ();  // necessary whenever the window is moved; setPosition is called after resizes and moves
     }
     
     IWidget getWidget (int cx, int cy) {
@@ -307,35 +308,31 @@
                 logger.trace ("resizeCallback: failure");
         
         // This function is only called if some resize is going to happen.
-        // To improve efficiency, store parameters to resize to.
-        int xSize = w, ySize = h;	// new size
-        int xDiff, yDiff;		// difference to new position
-        bool xHigh, yHigh;		// resize from positive side
+        // x,y are used as parameters to setPosition as well as being affected by it; somewhat
+        // pointless but fairly effective.
         
         if (resizeType & RESIZE_TYPE.L) {
-            getMinimalSize (xDiff, xSize);	// (only want xDiff, temporarily used as mw)
-            xSize = xDrag - cx;
-            if (xSize < xDiff) xSize = xDiff;	// clamp
-            xDiff = w - xSize;			// now used as amount to move
+            int xSize = xDrag - cx;
+            if (xSize < mw) xSize = mw;	// clamp
+            x += w - xSize;
+            setWidth (xSize, 1);
         }
         else if (resizeType & RESIZE_TYPE.R) {
-            xSize = xDrag + cx;
-            xHigh = true;
+            setWidth (xDrag + cx, -1);
         }
         if (resizeType & RESIZE_TYPE.T) {
-            getMinimalSize (ySize, yDiff);
-            ySize = yDrag - cy;
-            if (ySize < yDiff) ySize = yDiff;
-            yDiff = h - ySize;
+            int ySize = yDrag - cy;
+            if (ySize < mh) ySize = mh;
+            y += h - ySize;
+            setHeight (ySize, 1);
         }
         else if (resizeType & RESIZE_TYPE.B) {
-            ySize = yDrag + cy;
-            yHigh = true;
+            setHeight (yDrag + cy, -1);
         }
         
-        setSize (xSize, ySize, xHigh, yHigh);
-        if (xDiff != 0 || yDiff != 0)
-            setPosition (x + xDiff, y + yDiff);
+        // Moves lower (x,y) coordinate if necessary and repositions any sub-widgets moved by the
+        // resizing:
+        setPosition (x, y);
     }
     bool endCallback (ushort cx, ushort cy, ubyte b, bool state) {
         if (b == 1 && state == false) {