summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbjarni <bjarni@openttd.org>2007-01-24 14:32:20 +0000
committerbjarni <bjarni@openttd.org>2007-01-24 14:32:20 +0000
commit8c098db7a81546f3154b845e123bfcb2731d4f35 (patch)
tree3a5b9fe40052ed1cff61cb6dbf3a984c0ecf84f0
parent0996de79df839647968838498d1953319705606d (diff)
downloadopenttd-8c098db7a81546f3154b845e123bfcb2731d4f35.tar.xz
(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
-rw-r--r--src/build_vehicle_gui.cpp4
-rw-r--r--src/depot_gui.cpp2
-rw-r--r--src/train_gui.cpp4
-rw-r--r--src/widget.cpp82
-rw-r--r--src/window.h13
5 files changed, 61 insertions, 44 deletions
diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
index 3355199d9..7be31791b 100644
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -913,7 +913,7 @@ static void NewVehicleWndProc(Window *w, WindowEvent *e)
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 @@ void ShowBuildVehicleWindow(TileIndex tile, byte type)
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;
diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp
index 4b5166da3..e4163fcf2 100644
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -497,7 +497,7 @@ static void ClonePlaceObj(const Window *w)
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.
diff --git a/src/train_gui.cpp b/src/train_gui.cpp
index b952b3534..874b9cb55 100644
--- a/src/train_gui.cpp
+++ b/src/train_gui.cpp
@@ -560,7 +560,7 @@ do_change_service_int:
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 @@ static const Widget _train_details_widgets[] = {
{ 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},
};
diff --git a/src/widget.cpp b/src/widget.cpp
index 2c21cd74c..3b1cc8d21 100644
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -699,49 +699,75 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int butt
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)
+
+static void ResizeWidgets(Window *w, byte a, byte b)
{
- w->widget[d].right = w->widget[right].left - 1; // now we set the right of the widgets
+ int16 offset = w->widget[a].left;
+ int16 length = w->widget[b].right - offset;
+
+ w->widget[a].right = (length / 2) + offset;
- /* 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 */
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);
+ }
}
}
diff --git a/src/window.h b/src/window.h
index 5e5fcf6ba..90600ad44 100644
--- a/src/window.h
+++ b/src/window.h
@@ -768,19 +768,10 @@ enum SpecialMouseMode {
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);