changeset 5824:19c4635a0be0 draft

(svn r8390) -Codechange (r8384): Rewrote ResizeButtons() Now it only works on first and last widget to resize It now works with offsets correctly (no longer assumes the left is 0) It's no longer needed to have a widget right of the ones you resize Can handle any number of widgets
author bjarni <bjarni@openttd.org>
date Wed, 24 Jan 2007 14:32:20 +0000
parents 4f8ecdfccbd3
children 8529f116ed88
files src/build_vehicle_gui.cpp src/depot_gui.cpp src/train_gui.cpp src/widget.cpp src/window.h
diffstat 5 files changed, 62 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -913,7 +913,7 @@
 			break;
 
 		case WE_RESIZE:
-			if (e->we.sizing.diff.x != 0) ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RESIZE);
+			if (e->we.sizing.diff.x != 0) ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
 			if (e->we.sizing.diff.y == 0) break;
 
 			w->vscroll.cap += e->we.sizing.diff.y / GetVehicleListHeight(bv->vehicle_type);
@@ -975,7 +975,7 @@
 			break;
 	}
 	SetupWindowStrings(w, type);
-	ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RESIZE);
+	ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
 
 	w->resize.width  = w->width;
 	w->resize.height = w->height;
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -497,7 +497,7 @@
 
 static void ResizeDepotButtons(Window *w)
 {
-	ResizeButtons(w, DEPOT_WIDGET_BUILD, DEPOT_WIDGET_VEHICLE_LIST);
+	ResizeButtons(w, DEPOT_WIDGET_BUILD, DEPOT_WIDGET_LOCATION);
 
 	if (WP(w, depot_d).type == VEH_Train) {
 		/* Divide the size of DEPOT_WIDGET_SELL into two equally big buttons so DEPOT_WIDGET_SELL and DEPOT_WIDGET_SELL_CHAIN will get the same size.
--- a/src/train_gui.cpp
+++ b/src/train_gui.cpp
@@ -560,7 +560,7 @@
 		break;
 
 	case WE_RESIZE:
-		if (e->we.sizing.diff.x != 0) ResizeButtons(w, 9, 13);
+		if (e->we.sizing.diff.x != 0) ResizeButtons(w, 9, 12);
 		if (e->we.sizing.diff.y == 0) break;
 
 		w->vscroll.cap += e->we.sizing.diff.y / 14;
@@ -582,7 +582,7 @@
 { WWT_PUSHTXTBTN, RESIZE_TB,     14,   0,  89, 152, 163, STR_013C_CARGO,       STR_884F_SHOW_DETAILS_OF_CARGO_CARRIED},
 { WWT_PUSHTXTBTN, RESIZE_TB,     14,  90, 178, 152, 163, STR_013D_INFORMATION, STR_8850_SHOW_DETAILS_OF_TRAIN_VEHICLES},
 { WWT_PUSHTXTBTN, RESIZE_TB,     14, 179, 268, 152, 163, STR_013E_CAPACITIES,  STR_8851_SHOW_CAPACITIES_OF_EACH},
-{ WWT_PUSHTXTBTN, RESIZE_TB,     14, 269, 357, 152, 163, STR_013E_TOTAL_CARGO, STR_8852_SHOW_TOTAL_CARGO},
+{ WWT_PUSHTXTBTN, RESIZE_RTB,    14, 269, 357, 152, 163, STR_013E_TOTAL_CARGO, STR_8852_SHOW_TOTAL_CARGO},
 {  WWT_RESIZEBOX, RESIZE_LRTB,   14, 358, 369, 152, 163, 0x0,                  STR_RESIZE_BUTTON},
 {   WIDGETS_END},
 };
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -699,49 +699,75 @@
 	WP(w2,dropdown_d).drag_mode = true;
 }
 
-/* Make the buttons in the bottom equal in size */
-void ResizeButtons(Window *w, byte a, byte b, byte c, byte d, byte right)
-{
-	w->widget[d].right = w->widget[right].left - 1; // now we set the right of the widgets
 
-	/* Now we will find the middle, then the middle of each of the two blocks on each side of the middle.
-	 * This way, if we got leftover pixels from the division, they will be somewhat evenly distributed */
-	w->widget[b].right = w->widget[d].right / 2;
-	w->widget[a].right = w->widget[b].right / 2;
-	w->widget[c].right = (w->widget[b].right + w->widget[d].right)/2;
-	/* Now the right side of the buttons are set. We will now set the left sides next to them */
+static void ResizeWidgets(Window *w, byte a, byte b)
+{
+	int16 offset = w->widget[a].left;
+	int16 length = w->widget[b].right - offset;
+
+	w->widget[a].right  = (length / 2) + offset;
+
 	w->widget[b].left  = w->widget[a].right + 1;
-	w->widget[c].left  = w->widget[b].right + 1;
-	w->widget[d].left  = w->widget[c].right + 1;
 }
 
-void ResizeButtons(Window *w, byte a, byte b, byte c, byte right)
+static void ResizeWidgets(Window *w, byte a, byte b, byte c)
 {
-	w->widget[c].right = w->widget[right].left - 1; // now we set the right of the widgets
+	int16 offset = w->widget[a].left;
+	int16 length = w->widget[c].right - offset;
 
-	w->widget[a].right = w->widget[c].right / 3;
+	w->widget[a].right = length / 3;
 	w->widget[b].right = w->widget[a].right * 2;
 
+	w->widget[a].right += offset;
+	w->widget[b].right += offset;
+
 	/* Now the right side of the buttons are set. We will now set the left sides next to them */
 	w->widget[b].left  = w->widget[a].right + 1;
 	w->widget[c].left  = w->widget[b].right + 1;
 }
 
-void ResizeButtons(Window *w, byte a, byte b, byte right)
-{
-	w->widget[b].right = w->widget[right].left - 1; // now we set the right of the widgets
-
-	w->widget[a].right  = w->widget[b].right / 2;
-
-	w->widget[b].left  = w->widget[a].right + 1;
-}
-
+/** Evenly distribute some widgets when resizing horizontally (often a button row)
+ *  When only two arguments are given, the widgets are presumed to be on a line and only the ends are given
+ * @param w widow to modify
+ * @param left The leftmost widget to resize
+ * @param right The rightmost widget to resize. Since right side of it is used, remember to set it to RESIZE_RIGHT
+ */
 void ResizeButtons(Window *w, byte left, byte right)
 {
-	switch (right - left) {
-		case 2: ResizeButtons(w, left, left + 1, left + 2); break;
-		case 3: ResizeButtons(w, left, left + 1, left + 2, left + 3); break;
-		case 4: ResizeButtons(w, left, left + 1, left + 2, left + 3, left + 4); break;
-		default: NOT_REACHED();
+	int16 num_widgets = right - left + 1;
+
+	if (num_widgets < 2) NOT_REACHED();
+
+	switch (num_widgets) {
+		case 2: ResizeWidgets(w, left, right); break;
+		case 3: ResizeWidgets(w, left, left + 1, right); break;
+		default: {
+			/* Looks like we got more than 3 widgets to resize
+			 * Now we will find the middle of the space desinated for the widgets
+			 * and place half of the widgets on each side of it and call recursively.
+			 * Eventually we will get down to blocks of 2-3 widgets and we got code to handle those cases */
+			int16 offset = w->widget[left].left;
+			int16 length = w->widget[right].right - offset;
+			byte widget = ((num_widgets - 1)/ 2) + left; // rightmost widget of the left side
+
+			/* Now we need to find the middle of the widgets.
+			 * It will not always be the middle because if we got an uneven number of widgets,
+			 *   we will need it to be 2/5, 3/7 and so on
+			 * To get this, we multiply with num_widgets/num_widgets. Since we calculate in int, we will get:
+			 *
+			 *    num_widgets/2 (rounding down)
+			 *   ---------------
+			 *     num_widgets
+			 *
+			 * as multiplier to length. We just multiply before divide to that we stay in the int area though */
+			int16 middle = ((length * num_widgets) / (2 * num_widgets)) + offset;
+
+			/* Set left and right on the widgets, that's next to our "middle" */
+			w->widget[widget].right = middle;
+			w->widget[widget + 1].left = w->widget[widget].right + 1;
+			/* Now resize the left and right of the middle */
+			ResizeButtons(w, left, widget);
+			ResizeButtons(w, widget + 1, right);
+		}
 	}
 }
--- a/src/window.h
+++ b/src/window.h
@@ -768,19 +768,10 @@
 void ScrollbarClickHandler(Window *w, const Widget *wi, int x, int y);
 
 /** Evenly distribute some widgets when resizing horizontally (often a button row)
- * @param w widow to modify
- * @param a,b,c,d the widgets to resize (left to right, order matters)
- * @param right the widget right of the buttons, that needs resizing
- */
-void ResizeButtons(Window *w, byte a, byte b, byte right);
-void ResizeButtons(Window *w, byte a, byte b, byte c, byte right);
-void ResizeButtons(Window *w, byte a, byte b, byte c, byte d, byte right);
-
-/** Evenly distribute some widgets when resizing horizontally (often a button row)
- *  When only two arguments are given, the widgets are presumed to be on a line and only the ends are given
+ *  The widgets are presumed to be in a line and numberef from left to right (without gaps)
  * @param w widow to modify
  * @param left The leftmost widget to resize
- * @param right The widget just right of the widgets to resize
+ * @param right The rightmost widget to resize. Since right side of it is used, remember to set it to RESIZE_RIGHT
  */
 void ResizeButtons(Window *w, byte left, byte right);