changeset 12104:2fe8b7e3ca2c draft

(svn r16517) -Codechange: Switched intro-screen and town directory window to nested widget trees.
author alberth <alberth@openttd.org>
date Thu, 04 Jun 2009 14:34:38 +0000
parents eecff8281292
children cbe09c22660a
files src/intro_gui.cpp src/town_gui.cpp src/window.cpp src/window_gui.h
diffstat 4 files changed, 60 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/src/intro_gui.cpp
+++ b/src/intro_gui.cpp
@@ -25,34 +25,6 @@
 #include "table/strings.h"
 #include "table/sprites.h"
 
-static const Widget _select_game_widgets[] = {
-{     WWT_CAPTION,  RESIZE_NONE,  COLOUR_BROWN,     0,  335,    0,   13,  STR_INTRO_CAPTION,          STR_NULL},
-{       WWT_PANEL,  RESIZE_NONE,  COLOUR_BROWN,     0,  335,   14,  212,  0x0,                        STR_NULL},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,   22,   33,  STR_INTRO_NEW_GAME,         STR_INTRO_TOOLTIP_NEW_GAME},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,   22,   33,  STR_INTRO_LOAD_GAME,        STR_INTRO_TOOLTIP_LOAD_GAME},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,   40,   51,  STR_INTRO_PLAY_SCENARIO,    STR_INTRO_TOOLTIP_PLAY_SCENARIO},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,   40,   51,  STR_INTRO_PLAY_HEIGHTMAP,   STR_INTRO_PLAY_HEIGHTMAP_TOOLTIP},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,   58,   69,  STR_INTRO_SCENARIO_EDITOR,  STR_INTRO_TOOLTIP_SCENARIO_EDITOR},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,   58,   69,  STR_INTRO_MULTIPLAYER,      STR_INTRO_TOOLTIP_MULTIPLAYER},
-
-{    WWT_IMGBTN_2,  RESIZE_NONE,  COLOUR_ORANGE,   10,   86,   77,  131,  SPR_SELECT_TEMPERATE,       STR_INTRO_TOOLTIP_TEMPERATE},
-{    WWT_IMGBTN_2,  RESIZE_NONE,  COLOUR_ORANGE,   90,  166,   77,  131,  SPR_SELECT_SUB_ARCTIC,      STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE},
-{    WWT_IMGBTN_2,  RESIZE_NONE,  COLOUR_ORANGE,  170,  246,   77,  131,  SPR_SELECT_SUB_TROPICAL,    STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE},
-{    WWT_IMGBTN_2,  RESIZE_NONE,  COLOUR_ORANGE,  250,  326,   77,  131,  SPR_SELECT_TOYLAND,         STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE},
-
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,  139,  150,  STR_INTRO_GAME_OPTIONS,     STR_INTRO_TOOLTIP_GAME_OPTIONS},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,  139,  150,  STR_INTRO_DIFFICULTY,       STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,  157,  168,  STR_CONFIG_SETTING,         STR_CONFIG_SETTING_TIP},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,  157,  168,  STR_NEWGRF_SETTINGS_BUTTON, STR_NEWGRF_SETTINGS_BUTTON_TIP},
-
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,   10,  167,  175,  186,  STR_CONTENT_INTRO_BUTTON,   STR_CONTENT_INTRO_BUTTON_TIP},
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  168,  325,  175,  186,  STR_AI_SETTINGS_BUTTON,     STR_AI_SETTINGS_BUTTON_TIP},
-
-{  WWT_PUSHTXTBTN,  RESIZE_NONE,  COLOUR_ORANGE,  104,  231,  193,  204,  STR_INTRO_QUIT,             STR_INTRO_TOOLTIP_QUIT},
-
-{     WIDGETS_END},
-};
-
 static inline void SetNewLandscapeType(byte landscape)
 {
 	_settings_newgame.game_creation.landscape = landscape;
@@ -83,10 +55,10 @@
 
 struct SelectGameWindow : public Window {
 
-	SelectGameWindow(const WindowDesc *desc) : Window(desc)
+	SelectGameWindow(const WindowDesc *desc) : Window()
 	{
+		this->InitNested(desc);
 		this->LowerWidget(_settings_newgame.game_creation.landscape + SGI_TEMPERATE_LANDSCAPE);
-		this->FindWindowPlacementAndResize(desc);
 	}
 
 	virtual void OnPaint()
@@ -265,7 +237,7 @@
 	WDP_CENTER, WDP_CENTER, 336, 213, 336, 213,
 	WC_SELECT_GAME, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
-	_select_game_widgets,
+	NULL,
 	_nested_select_game_widgets, lengthof(_nested_select_game_widgets)
 );
 
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -501,19 +501,6 @@
 	TDW_RESIZEBOX,
 };
 
-static const Widget _town_directory_widgets[] = {
-{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_BROWN,     0,    10,     0,    13, STR_BLACK_CROSS,            STR_TOOLTIP_CLOSE_WINDOW},             // TDW_CLOSEBOX
-{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_BROWN,    11,   195,     0,    13, STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS},   // TDW_CAPTION
-{  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_BROWN,   196,   207,     0,    13, 0x0,                        STR_STICKY_BUTTON},                    // TDW_STICKYBOX
-{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,     0,    98,    14,    25, STR_SORT_BY_NAME,           STR_SORT_ORDER_TIP},                   // TDW_SORTNAME
-{ WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,    99,   195,    14,    25, STR_SORT_BY_POPULATION,     STR_SORT_ORDER_TIP},                   // TDW_SORTPOPULATION
-{      WWT_PANEL, RESIZE_BOTTOM,  COLOUR_BROWN,     0,   195,    26,   189, 0x0,                        STR_TOWN_DIRECTORY_LIST_TOOLTIP},      // TDW_CENTERTOWN
-{  WWT_SCROLLBAR, RESIZE_BOTTOM,  COLOUR_BROWN,   196,   207,    14,   189, 0x0,                        STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST}, // TDW_SCROLLBAR
-{      WWT_PANEL,     RESIZE_TB,  COLOUR_BROWN,     0,   195,   190,   201, 0x0,                        STR_NULL},                             // TDW_EMPTYBOTTOM
-{  WWT_RESIZEBOX,     RESIZE_TB,  COLOUR_BROWN,   196,   207,   190,   201, 0x0,                        STR_RESIZE_BUTTON},                    // TDW_RESIZEBOX
-{   WIDGETS_END},
-};
-
 static const NWidgetPart _nested_town_directory_widgets[] = {
 	NWidget(NWID_HORIZONTAL),
 		NWidget(WWT_CLOSEBOX, COLOUR_BROWN, TDW_CLOSEBOX),
@@ -599,17 +586,15 @@
 	}
 
 public:
-	TownDirectoryWindow(const WindowDesc *desc) : Window(desc, 0)
+	TownDirectoryWindow(const WindowDesc *desc) : Window()
 	{
-		this->vscroll.cap = 16;
-		this->resize.step_height = 10;
-		this->resize.height = this->height - 10 * 6; // minimum of 10 items in the list, each item 10 high
+		this->InitNested(desc, 0);
+
+		this->vscroll.cap = this->nested_array[TDW_CENTERTOWN]->current_y / this->resize.step_height;
 
 		this->towns.SetListing(this->last_sorting);
 		this->towns.SetSortFuncs(this->sorter_funcs);
 		this->towns.ForceRebuild();
-
-		this->FindWindowPlacementAndResize(desc);
 	}
 
 	~TownDirectoryWindow()
@@ -726,7 +711,7 @@
 	WDP_AUTO, WDP_AUTO, 208, 202, 208, 202,
 	WC_TOWN_DIRECTORY, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
-	_town_directory_widgets, _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
+	NULL, _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
 );
 
 void ShowTownDirectory()
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -825,19 +825,33 @@
  * This function is called the constructors.
  * See descriptions for those functions for usage
  * Only addition here is window_number, which is the window_number being assigned to the new window
- * @param x offset in pixels from the left of the screen
- * @param y offset in pixels from the top of the screen
- * @param min_width minimum width in pixels of the window
- * @param min_height minimum height in pixels of the window
- * @param cls see WindowClass class of the window, used for identification and grouping
- * @param *widget see Widget pointer to the window layout and various elements
- * @param window_number number being assigned to the new window
+ * @param x             Offset in pixels from the left of the screen of the new window.
+ * @param y             Offset in pixels from the top of the screen of the new window.
+ * @param min_width     Minimum width in pixels of the window
+ * @param min_height    Minimum height in pixels of the window
+ * @param cls           Class of the window, used for identification and grouping. @see WindowClass
+ * @param *widget       Pointer to the window layout and various elements. @see Widget
+ * @param nested_root   Root of the nested widget tree.
+ * @param window_number Number being assigned to the new window
  * @return Window pointer of the newly created window
  */
 void Window::Initialize(int x, int y, int min_width, int min_height,
-				WindowClass cls, const Widget *widget, int window_number)
+				WindowClass cls, const Widget *widget, NWidgetBase *nested_root, int window_number)
 {
-	/* All data members of nested widgets have been set to 0 by the #ZeroedMemoryAllocator base class. */
+	/* If available, initialize nested widget tree. */
+	if (nested_root != NULL) {
+		this->nested_root = nested_root;
+		/* Setup nested_array pointers into the tree. */
+		int biggest_index = this->nested_root->SetupSmallestSize();
+		this->nested_array_size = (uint)(biggest_index + 1);
+		this->nested_array = CallocT<NWidgetCore *>(this->nested_array_size);
+		this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
+		/* Initialize to smallest size. */
+		this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, false, false, false);
+		min_width = this->nested_root->smallest_x;
+		min_height = this->nested_root->smallest_y;
+	}
+	/* Else, all data members of nested widgets have been set to 0 by the #ZeroedMemoryAllocator base class. */
 
 	/* Set up window properties */
 	this->window_class = cls;
@@ -852,8 +866,8 @@
 	this->nested_focus = NULL;
 	this->resize.width = min_width;
 	this->resize.height = min_height;
-	this->resize.step_width = 1;
-	this->resize.step_height = 1;
+	this->resize.step_width  = (this->nested_root != NULL) ? this->nested_root->resize_x : 1;
+	this->resize.step_height = (this->nested_root != NULL) ? this->nested_root->resize_y : 1;
 	this->window_number = window_number;
 
 	/* Give focus to the opened window unless it is the OSK window or a text box
@@ -916,6 +930,8 @@
  */
 void Window::FindWindowPlacementAndResize(int def_width, int def_height)
 {
+	def_width  = max(def_width,  this->width); // Don't allow default size to be smaller than smallest size
+	def_height = max(def_height, this->height);
 	/* Try to make windows smaller when our window is too small.
 	 * w->(width|height) is normally the same as min_(width|height),
 	 * but this way the GUIs can be made a little more dynamic;
@@ -989,7 +1005,7 @@
  */
 Window::Window(int x, int y, int width, int height, WindowClass cls, const Widget *widget)
 {
-	this->Initialize(x, y, width, height, cls, widget, 0);
+	this->Initialize(x, y, width, height, cls, widget, NULL, 0);
 }
 
 /**
@@ -1214,10 +1230,29 @@
 Window::Window(const WindowDesc *desc, WindowNumber window_number)
 {
 	Point pt = LocalGetWindowPlacement(desc, window_number);
-	this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->cls, desc->GetWidgets(), window_number);
+	this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->cls, desc->GetWidgets(), NULL, window_number);
 	this->desc_flags = desc->flags;
 }
 
+/**
+ * Perform initialization of the #Window with nested widgets, to allow use.
+ * @param desc          Window description.
+ * @param window_number Number of the new window.
+ */
+void Window::InitNested(const WindowDesc *desc, WindowNumber window_number)
+{
+	NWidgetBase *nested_root = MakeNWidgets(desc->nwid_parts, desc->nwid_length);
+	Point pt = LocalGetWindowPlacement(desc, window_number);
+	this->Initialize(pt.x, pt.y, desc->minimum_width, desc->minimum_height, desc->cls, NULL, nested_root, window_number); // min_width and min_height are not used.
+	this->desc_flags = desc->flags;
+	this->FindWindowPlacementAndResize(desc->default_width, desc->default_height);
+}
+
+/** Empty constructor, initialization has been moved to #InitNested() called from the constructor of the derived class. */
+Window::Window()
+{
+}
+
 /** Do a search for a window at specific coordinates. For this we start
  * at the topmost window, obviously and work our way down to the bottom
  * @param x position x to query
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -138,13 +138,14 @@
 
 protected:
 	void Initialize(int x, int y, int min_width, int min_height,
-			WindowClass cls, const Widget *widget, int window_number);
+			WindowClass cls, const Widget *widget, NWidgetBase *nwid, int window_number);
 	void FindWindowPlacementAndResize(int def_width, int def_height);
 	void FindWindowPlacementAndResize(const WindowDesc *desc);
 
 public:
 	Window(int x, int y, int width, int height, WindowClass cls, const Widget *widget);
 	Window(const WindowDesc *desc, WindowNumber number = 0);
+	Window();
 
 	virtual ~Window();
 	/* Don't allow arrays; arrays of Windows are pointless as you need
@@ -183,6 +184,8 @@
 	Window *z_front;                 ///< The window in front of us in z-order.
 	Window *z_back;                  ///< The window behind us in z-order.
 
+	void InitNested(const WindowDesc *desc, WindowNumber number = 0);
+
 	/**
 	 * Sets the enabled/disabled status of a widget.
 	 * By default, widgets are enabled.