Mercurial > hg > openttd
comparison src/network/network_gui.cpp @ 13439:0e90ffa69389 draft
(svn r17948) -Codechange: remove some magic numbers from the network client list GUIs / use ResizeWindow instead of custom resize code.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Mon, 02 Nov 2009 10:52:57 +0000 |
parents | 1ab8e5dfef6c |
children | 53a39cddf2be |
comparison
equal
deleted
inserted
replaced
13438:588cc335bf57 | 13439:0e90ffa69389 |
---|---|
1760 /* Max 10 actions per client */ | 1760 /* Max 10 actions per client */ |
1761 #define MAX_CLIENTLIST_ACTION 10 | 1761 #define MAX_CLIENTLIST_ACTION 10 |
1762 | 1762 |
1763 enum { | 1763 enum { |
1764 CLNWND_OFFSET = 16, | 1764 CLNWND_OFFSET = 16, |
1765 CLNWND_ROWSIZE = 10 | |
1766 }; | 1765 }; |
1767 | 1766 |
1768 static const Widget _client_list_popup_widgets[] = { | 1767 static const Widget _client_list_popup_widgets[] = { |
1769 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 99, 0, 0, 0, STR_NULL}, | 1768 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 99, 0, 0, 0, STR_NULL}, |
1770 { WIDGETS_END}, | 1769 { WIDGETS_END}, |
1926 if (this->action[i][0] == '\0') continue; | 1925 if (this->action[i][0] == '\0') continue; |
1927 if (this->proc[i] == NULL) continue; | 1926 if (this->proc[i] == NULL) continue; |
1928 num++; | 1927 num++; |
1929 } | 1928 } |
1930 | 1929 |
1931 num *= CLNWND_ROWSIZE; | 1930 num *= FONT_HEIGHT_NORMAL; |
1932 | 1931 |
1933 return num + 1; | 1932 return num + 1; |
1934 } | 1933 } |
1935 | 1934 |
1936 | 1935 |
1939 this->DrawWidgets(); | 1938 this->DrawWidgets(); |
1940 | 1939 |
1941 /* Draw the actions */ | 1940 /* Draw the actions */ |
1942 int sel = this->sel_index; | 1941 int sel = this->sel_index; |
1943 int y = 1; | 1942 int y = 1; |
1944 for (int i = 0; i < MAX_CLIENTLIST_ACTION; i++, y += CLNWND_ROWSIZE) { | 1943 for (int i = 0; i < MAX_CLIENTLIST_ACTION; i++, y += FONT_HEIGHT_NORMAL) { |
1945 if (this->action[i][0] == '\0') continue; | 1944 if (this->action[i][0] == '\0') continue; |
1946 if (this->proc[i] == NULL) continue; | 1945 if (this->proc[i] == NULL) continue; |
1947 | 1946 |
1948 TextColour colour; | 1947 TextColour colour; |
1949 if (sel-- == 0) { // Selected item, highlight it | 1948 if (sel-- == 0) { // Selected item, highlight it |
1950 GfxFillRect(1, y, 150 - 2, y + CLNWND_ROWSIZE - 1, 0); | 1949 GfxFillRect(1, y, 150 - 2, y + FONT_HEIGHT_NORMAL - 1, 0); |
1951 colour = TC_WHITE; | 1950 colour = TC_WHITE; |
1952 } else { | 1951 } else { |
1953 colour = TC_BLACK; | 1952 colour = TC_BLACK; |
1954 } | 1953 } |
1955 | 1954 |
1958 } | 1957 } |
1959 | 1958 |
1960 virtual void OnMouseLoop() | 1959 virtual void OnMouseLoop() |
1961 { | 1960 { |
1962 /* We selected an action */ | 1961 /* We selected an action */ |
1963 int index = (_cursor.pos.y - this->top) / CLNWND_ROWSIZE; | 1962 int index = (_cursor.pos.y - this->top) / FONT_HEIGHT_NORMAL; |
1964 | 1963 |
1965 if (_left_button_down) { | 1964 if (_left_button_down) { |
1966 if (index == -1 || index == this->sel_index) return; | 1965 if (index == -1 || index == this->sel_index) return; |
1967 | 1966 |
1968 this->sel_index = index; | 1967 this->sel_index = index; |
2008 static const Widget _client_list_widgets[] = { | 2007 static const Widget _client_list_widgets[] = { |
2009 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW}, | 2008 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW}, |
2010 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 237, 0, 13, STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS}, | 2009 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 237, 0, 13, STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS}, |
2011 { WWT_STICKYBOX, RESIZE_NONE, COLOUR_GREY, 238, 249, 0, 13, STR_NULL, STR_TOOLTIP_STICKY}, | 2010 { WWT_STICKYBOX, RESIZE_NONE, COLOUR_GREY, 238, 249, 0, 13, STR_NULL, STR_TOOLTIP_STICKY}, |
2012 | 2011 |
2013 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 249, 14, 14 + CLNWND_ROWSIZE + 1, 0x0, STR_NULL}, | 2012 { WWT_PANEL, RESIZE_BOTTOM, COLOUR_GREY, 0, 249, 14, 15, 0x0, STR_NULL}, |
2014 { WIDGETS_END}, | 2013 { WIDGETS_END}, |
2015 }; | 2014 }; |
2016 | 2015 |
2017 static const NWidgetPart _nested_client_list_widgets[] = { | 2016 static const NWidgetPart _nested_client_list_widgets[] = { |
2018 NWidget(NWID_HORIZONTAL), | 2017 NWidget(NWID_HORIZONTAL), |
2019 NWidget(WWT_CLOSEBOX, COLOUR_GREY, CLW_CLOSE), | 2018 NWidget(WWT_CLOSEBOX, COLOUR_GREY, CLW_CLOSE), |
2020 NWidget(WWT_CAPTION, COLOUR_GREY, CLW_CAPTION), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), | 2019 NWidget(WWT_CAPTION, COLOUR_GREY, CLW_CAPTION), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), |
2021 NWidget(WWT_STICKYBOX, COLOUR_GREY, CLW_STICKY), | 2020 NWidget(WWT_STICKYBOX, COLOUR_GREY, CLW_STICKY), |
2022 EndContainer(), | 2021 EndContainer(), |
2023 NWidget(WWT_PANEL, COLOUR_GREY, CLW_PANEL), SetMinimalSize(250, CLNWND_ROWSIZE + 2), EndContainer(), | 2022 NWidget(WWT_PANEL, COLOUR_GREY, CLW_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(), |
2024 }; | 2023 }; |
2025 | 2024 |
2026 static const WindowDesc _client_list_desc( | 2025 static const WindowDesc _client_list_desc( |
2027 WDP_AUTO, WDP_AUTO, 250, 1, 250, 1, | 2026 WDP_AUTO, WDP_AUTO, 250, 16, 250, 16, |
2028 WC_CLIENT_LIST, WC_NONE, | 2027 WC_CLIENT_LIST, WC_NONE, |
2029 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON, | 2028 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE, |
2030 _client_list_widgets, _nested_client_list_widgets, lengthof(_nested_client_list_widgets) | 2029 _client_list_widgets, _nested_client_list_widgets, lengthof(_nested_client_list_widgets) |
2031 ); | 2030 ); |
2032 | 2031 |
2033 /** | 2032 /** |
2034 * Main handle for clientlist | 2033 * Main handle for clientlist |
2035 */ | 2034 */ |
2036 struct NetworkClientListWindow : Window | 2035 struct NetworkClientListWindow : Window { |
2037 { | |
2038 int selected_item; | 2036 int selected_item; |
2039 int selected_y; | 2037 int selected_y; |
2040 | 2038 |
2041 NetworkClientListWindow(const WindowDesc *desc, WindowNumber window_number) : | 2039 NetworkClientListWindow(const WindowDesc *desc, WindowNumber window_number) : |
2042 Window(desc, window_number), | 2040 Window(desc, window_number), |
2057 /* Should be replaced with a loop through all clients */ | 2055 /* Should be replaced with a loop through all clients */ |
2058 FOR_ALL_CLIENT_INFOS(ci) { | 2056 FOR_ALL_CLIENT_INFOS(ci) { |
2059 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++; | 2057 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++; |
2060 } | 2058 } |
2061 | 2059 |
2062 num *= CLNWND_ROWSIZE; | 2060 num *= FONT_HEIGHT_NORMAL; |
2063 | 2061 |
2062 int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->widget[3].bottom - this->widget[3].top + 1); | |
2064 /* If height is changed */ | 2063 /* If height is changed */ |
2065 if (this->height != CLNWND_OFFSET + num + 1) { | 2064 if (diff != 0) { |
2066 /* XXX - magic unfortunately; (num + 2) has to be one bigger than heigh (num + 1) */ | 2065 ResizeWindow(this, 0, diff); |
2067 this->SetDirty(); | |
2068 this->widget[3].bottom = this->widget[3].top + num + 2; | |
2069 this->height = CLNWND_OFFSET + num + 1; | |
2070 this->SetDirty(); | |
2071 return false; | 2066 return false; |
2072 } | 2067 } |
2073 return true; | 2068 return true; |
2074 } | 2069 } |
2075 | 2070 |
2081 /* Check if we need to reset the height */ | 2076 /* Check if we need to reset the height */ |
2082 if (!this->CheckClientListHeight()) return; | 2077 if (!this->CheckClientListHeight()) return; |
2083 | 2078 |
2084 this->DrawWidgets(); | 2079 this->DrawWidgets(); |
2085 | 2080 |
2086 int y = CLNWND_OFFSET; | 2081 int y = this->widget[3].top + WD_FRAMERECT_TOP; |
2087 | 2082 |
2088 FOR_ALL_CLIENT_INFOS(ci) { | 2083 FOR_ALL_CLIENT_INFOS(ci) { |
2089 TextColour colour; | 2084 TextColour colour; |
2090 if (this->selected_item == i++) { // Selected item, highlight it | 2085 if (this->selected_item == i++) { // Selected item, highlight it |
2091 GfxFillRect(1, y, 248, y + CLNWND_ROWSIZE - 1, 0); | 2086 GfxFillRect(1, y, 248, y + FONT_HEIGHT_NORMAL - 1, 0); |
2092 colour = TC_WHITE; | 2087 colour = TC_WHITE; |
2093 } else { | 2088 } else { |
2094 colour = TC_BLACK; | 2089 colour = TC_BLACK; |
2095 } | 2090 } |
2096 | 2091 |
2103 /* Filter out spectators */ | 2098 /* Filter out spectators */ |
2104 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, 64, y + 1); | 2099 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, 64, y + 1); |
2105 | 2100 |
2106 DrawString(81, this->width - 2, y, ci->client_name, colour); | 2101 DrawString(81, this->width - 2, y, ci->client_name, colour); |
2107 | 2102 |
2108 y += CLNWND_ROWSIZE; | 2103 y += FONT_HEIGHT_NORMAL; |
2109 } | 2104 } |
2110 } | 2105 } |
2111 | 2106 |
2112 virtual void OnClick(Point pt, int widget) | 2107 virtual void OnClick(Point pt, int widget) |
2113 { | 2108 { |
2129 /* It did not change.. no update! */ | 2124 /* It did not change.. no update! */ |
2130 if (pt.y == this->selected_y) return; | 2125 if (pt.y == this->selected_y) return; |
2131 | 2126 |
2132 /* Find the new selected item (if any) */ | 2127 /* Find the new selected item (if any) */ |
2133 this->selected_y = pt.y; | 2128 this->selected_y = pt.y; |
2134 if (pt.y > CLNWND_OFFSET) { | 2129 this->selected_item = max((pt.y - this->widget[3].top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL, -1); |
2135 this->selected_item = (pt.y - CLNWND_OFFSET) / CLNWND_ROWSIZE; | |
2136 } else { | |
2137 this->selected_item = -1; | |
2138 } | |
2139 | 2130 |
2140 /* Repaint */ | 2131 /* Repaint */ |
2141 this->SetDirty(); | 2132 this->SetDirty(); |
2142 } | 2133 } |
2143 }; | 2134 }; |