|
1 /* $Id$ */ |
|
2 |
|
3 /** @file newgrf_download.cpp GUI to download NewGRFs. */ |
|
4 #include "../stdafx.h" |
|
5 #include "../openttd.h" |
|
6 #include "../strings_func.h" |
|
7 #include "../date_func.h" |
|
8 #include "../gui.h" |
|
9 #include "../window_gui.h" |
|
10 #include "../textbuf_gui.h" |
|
11 #include "../variables.h" |
|
12 #include "../newgrf.h" |
|
13 #include "newgrf_download.h" |
|
14 #include "../functions.h" |
|
15 #include "../window_func.h" |
|
16 #include "../core/alloc_func.hpp" |
|
17 #include "../string_func.h" |
|
18 #include "../gfx_func.h" |
|
19 #include "../querystring_gui.h" |
|
20 #include "../sortlist_type.h" |
|
21 |
|
22 #include "table/strings.h" |
|
23 #include "../table/sprites.h" |
|
24 |
|
25 // XXX: copy-pasted from newgrf_gui.cpp |
|
26 static void ShowDownloadNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, uint bottom, bool show_params) |
|
27 { |
|
28 char buff[256]; |
|
29 |
|
30 if (c->error != NULL) { |
|
31 char message[512]; |
|
32 if (c->error->custom_message == NULL) { |
|
33 SetDParamStr(0, c->filename); |
|
34 SetDParamStr(1, c->error->data); |
|
35 for (uint i = 0; i < c->error->num_params; i++) { |
|
36 uint32 param = 0; |
|
37 byte param_number = c->error->param_number[i]; |
|
38 |
|
39 if (param_number < c->num_params) param = c->param[param_number]; |
|
40 |
|
41 SetDParam(2 + i, param); |
|
42 } |
|
43 |
|
44 GetString(message, c->error->message, lastof(message)); |
|
45 } else { |
|
46 SetDParamStr(0, c->error->custom_message); |
|
47 GetString(message, STR_JUST_RAW_STRING, lastof(message)); |
|
48 } |
|
49 |
|
50 SetDParamStr(0, message); |
|
51 y += DrawStringMultiLine(x, y, c->error->severity, w, bottom - y); |
|
52 } |
|
53 |
|
54 /* Draw filename or not if it is not known (GRF sent over internet) */ |
|
55 if (c->filename != NULL) { |
|
56 SetDParamStr(0, c->filename); |
|
57 y += DrawStringMultiLine(x, y, STR_NEWGRF_FILENAME, w, bottom - y); |
|
58 } |
|
59 |
|
60 /* Prepare and draw GRF ID */ |
|
61 snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->grfid)); |
|
62 SetDParamStr(0, buff); |
|
63 y += DrawStringMultiLine(x, y, STR_NEWGRF_GRF_ID, w, bottom - y); |
|
64 |
|
65 /* Prepare and draw MD5 sum */ |
|
66 md5sumToString(buff, lastof(buff), c->md5sum); |
|
67 SetDParamStr(0, buff); |
|
68 y += DrawStringMultiLine(x, y, STR_NEWGRF_MD5SUM, w, bottom - y); |
|
69 |
|
70 /* Show GRF parameter list */ |
|
71 if (show_params) { |
|
72 if (c->num_params > 0) { |
|
73 GRFBuildParamList(buff, c, lastof(buff)); |
|
74 SetDParam(0, STR_JUST_RAW_STRING); |
|
75 SetDParamStr(1, buff); |
|
76 } else { |
|
77 SetDParam(0, STR_01A9_NONE); |
|
78 } |
|
79 y += DrawStringMultiLine(x, y, STR_NEWGRF_PARAMETER, w, bottom - y); |
|
80 } |
|
81 |
|
82 /* Show flags */ |
|
83 if (c->status == GCS_NOT_FOUND) y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w, bottom - y); |
|
84 if (c->status == GCS_DISABLED) y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w, bottom - y); |
|
85 if (HasBit(c->flags, GCF_COMPATIBLE)) y += DrawStringMultiLine(x, y, STR_NEWGRF_COMPATIBLE_LOADED, w, bottom - y); |
|
86 |
|
87 /* Draw GRF info if it exists */ |
|
88 if (c->info != NULL && !StrEmpty(c->info)) { |
|
89 SetDParam(0, STR_JUST_RAW_STRING); |
|
90 SetDParamStr(1, c->info); |
|
91 y += DrawStringMultiLine(x, y, STR_02BD, w, bottom - y); |
|
92 } else { |
|
93 y += DrawStringMultiLine(x, y, STR_NEWGRF_NO_INFO, w, bottom - y); |
|
94 } |
|
95 } |
|
96 |
|
97 /** |
|
98 * Window for downloading NewGRF files |
|
99 */ |
|
100 struct NewGRFDownloadWindow : public Window { |
|
101 /* Names of the add a newgrf window widgets */ |
|
102 enum DownloadNewGRFWindowWidgets { |
|
103 DNGRFS_CLOSEBOX = 0, |
|
104 DNGRFS_CAPTION, |
|
105 DNGRFS_BACKGROUND, |
|
106 DNGRFS_FILE_LIST, |
|
107 DNGRFS_SCROLLBAR, |
|
108 DNGRFS_NEWGRF_INFO, |
|
109 DNGRFS_CHECK_AVAILABLE, |
|
110 DNGRFS_DOWNLOAD_AVAILABLE, |
|
111 DNGRFS_RESIZE, |
|
112 }; |
|
113 |
|
114 // XXX: do we need a temp. copy? I think not |
|
115 GRFConfig *list; ///< temporary grf list to which changes are made |
|
116 const GRFConfig *sel; |
|
117 |
|
118 NewGRFDownloadWindow(const WindowDesc *desc, GRFConfig **list) : Window(desc, 0) |
|
119 { |
|
120 // resize magic |
|
121 this->resize.step_height = 14; |
|
122 |
|
123 // the list of GRFs |
|
124 CopyGRFConfigList(&this->list, *list, false); |
|
125 |
|
126 // display window? |
|
127 this->FindWindowPlacementAndResize(desc); |
|
128 |
|
129 // set up our own state |
|
130 this->SetupDownloadNewGRFWindow(); |
|
131 } |
|
132 |
|
133 ~NewGRFDownloadWindow() |
|
134 { |
|
135 /* Remove the temporary copy of grf-list used in window */ |
|
136 ClearGRFConfigList(&this->list); |
|
137 } |
|
138 |
|
139 void SetupDownloadNewGRFWindow() |
|
140 { |
|
141 const GRFConfig *c; |
|
142 int i; |
|
143 |
|
144 // count how many NewGRFs in our list |
|
145 for (c = this->list, i = 0; c != NULL; c = c->next, i++) {} |
|
146 |
|
147 // something to do with vertical scrolling... |
|
148 this->vscroll.cap = (this->widget[DNGRFS_FILE_LIST].bottom - this->widget[DNGRFS_FILE_LIST].top) / 14 + 1; |
|
149 SetVScrollCount(this, i); |
|
150 |
|
151 // disable the "Check Available" button if there are no incompatible NewGRFs |
|
152 this->SetWidgetDisabledState(DNGRFS_CHECK_AVAILABLE, (i == 0)); |
|
153 |
|
154 // disable the "Download Available" button until we've checked for available NewGRFs |
|
155 this->SetWidgetDisabledState(DNGRFS_DOWNLOAD_AVAILABLE, 1); |
|
156 } |
|
157 |
|
158 virtual void OnPaint() |
|
159 { |
|
160 // superclass widget-drawing |
|
161 this->DrawWidgets(); |
|
162 |
|
163 /* Draw the NewGRF file list */ |
|
164 |
|
165 int y = this->widget[DNGRFS_FILE_LIST].top; |
|
166 int i = 0; |
|
167 |
|
168 // for each GRF... |
|
169 for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) { |
|
170 |
|
171 // is scrolled into view |
|
172 if (i >= this->vscroll.pos && i < this->vscroll.pos + this->vscroll.cap) { |
|
173 |
|
174 SpriteID pal; |
|
175 byte txtoffset; |
|
176 |
|
177 // the NewGRF name |
|
178 const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; |
|
179 |
|
180 /* Pick a colour */ |
|
181 switch (c->status) { |
|
182 case GCS_NOT_FOUND: |
|
183 case GCS_DISABLED: |
|
184 pal = PALETTE_TO_RED; |
|
185 break; |
|
186 |
|
187 // XXX: green if downloadable |
|
188 case GCS_ACTIVATED: |
|
189 pal = PALETTE_TO_GREEN; |
|
190 break; |
|
191 |
|
192 default: |
|
193 pal = PALETTE_TO_BLUE; |
|
194 break; |
|
195 } |
|
196 |
|
197 // XXX: more colour logic that must be fixed... |
|
198 if (pal != PALETTE_TO_RED) { |
|
199 if (HasBit(c->flags, GCF_STATIC)) { |
|
200 pal = PALETTE_TO_GREY; |
|
201 |
|
202 } else if (HasBit(c->flags, GCF_COMPATIBLE)) { |
|
203 pal = PALETTE_TO_ORANGE; |
|
204 |
|
205 } |
|
206 } |
|
207 |
|
208 // draw the square in the chosen colour |
|
209 DrawSprite(SPR_SQUARE, pal, 5, y + 2); |
|
210 |
|
211 // XXX: hmm... use for download errors? |
|
212 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2); |
|
213 |
|
214 // where to draw the text |
|
215 txtoffset = c->error != NULL ? 35 : 25; |
|
216 |
|
217 // draw the chosen text (GRF name/filename) |
|
218 DoDrawStringTruncated(text, txtoffset, y + 3, this->sel == c ? TC_WHITE : TC_BLACK, this->width - txtoffset - 10); |
|
219 |
|
220 // advance on to the next row |
|
221 y += 14; |
|
222 } |
|
223 } |
|
224 |
|
225 if (this->sel != NULL) { |
|
226 /* Draw NewGRF file info */ |
|
227 const Widget *wi = &this->widget[DNGRFS_NEWGRF_INFO]; |
|
228 |
|
229 ShowDownloadNewGRFInfo(this->sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, false); |
|
230 } |
|
231 } |
|
232 |
|
233 virtual void OnClick(Point pt, int widget) |
|
234 { |
|
235 switch (widget) { |
|
236 case DNGRFS_CHECK_AVAILABLE: |
|
237 // XXX: implement |
|
238 |
|
239 break; |
|
240 |
|
241 case DNGRFS_DOWNLOAD_AVAILABLE: |
|
242 // XXX: implement |
|
243 |
|
244 break; |
|
245 |
|
246 case DNGRFS_FILE_LIST: { // Select a GRF |
|
247 GRFConfig *c; |
|
248 uint i = (pt.y - this->widget[DNGRFS_FILE_LIST].top) / 14 + this->vscroll.pos; |
|
249 |
|
250 for (c = this->list; c != NULL && i > 0; c = c->next, i--) {} |
|
251 this->sel = c; |
|
252 |
|
253 this->SetDirty(); |
|
254 break; |
|
255 } |
|
256 } |
|
257 } |
|
258 |
|
259 virtual void OnResize(Point new_size, Point delta) |
|
260 { |
|
261 if (delta.x != 0) { |
|
262 ResizeButtons(this, DNGRFS_CHECK_AVAILABLE, DNGRFS_DOWNLOAD_AVAILABLE); |
|
263 } |
|
264 |
|
265 this->vscroll.cap += delta.y / 14; |
|
266 this->widget[DNGRFS_FILE_LIST].data = (this->vscroll.cap << 8) + 1; |
|
267 |
|
268 this->SetupDownloadNewGRFWindow(); |
|
269 } |
|
270 |
|
271 virtual void OnInvalidateData(int data = 0) |
|
272 { |
|
273 this->SetupDownloadNewGRFWindow(); |
|
274 } |
|
275 }; |
|
276 |
|
277 /* Widget definition of the download newgrfs window */ |
|
278 static const Widget _newgrf_download_widgets[] = { |
|
279 { WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // DNGRFS_CLOSEBOX |
|
280 { WWT_CAPTION, RESIZE_RIGHT, 10, 11, 299, 0, 13, STR_NEWGRF_SETTINGS_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // DNGRFS_CAPTION |
|
281 { WWT_PANEL, RESIZE_RIGHT, 10, 0, 299, 14, 29, STR_NULL, STR_NULL }, // DNGRFS_BACKGROUND |
|
282 |
|
283 /* |
|
284 { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 10, 79, 16, 27, STR_NEWGRF_ADD, STR_NEWGRF_ADD_TIP }, // SNGRFS_ADD |
|
285 { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 80, 149, 16, 27, STR_NEWGRF_REMOVE, STR_NEWGRF_REMOVE_TIP }, // SNGRFS_REMOVE |
|
286 { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 150, 219, 16, 27, STR_NEWGRF_MOVEUP, STR_NEWGRF_MOVEUP_TIP }, // SNGRFS_MOVE_UP |
|
287 { WWT_PUSHTXTBTN, RESIZE_RIGHT, 3, 220, 289, 16, 27, STR_NEWGRF_MOVEDOWN, STR_NEWGRF_MOVEDOWN_TIP }, // SNGRFS_MOVE_DOWN |
|
288 */ |
|
289 |
|
290 { WWT_MATRIX, RESIZE_RB, 10, 0, 287, 30, 99, 0x501, STR_NEWGRF_DOWNLOAD_LIST_TIP }, // DNGRFS_FILE_LIST |
|
291 { WWT_SCROLLBAR, RESIZE_LRB, 10, 288, 299, 30, 99, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, // DNGRFS_SCROLLBAR |
|
292 { WWT_PANEL, RESIZE_RTB, 10, 0, 299, 100, 212, STR_NULL, STR_NULL }, // DNGRFS_NEWGRF_INFO |
|
293 { WWT_PUSHTXTBTN, RESIZE_TB, 10, 0, 143, 213, 224, STR_NEWGRF_CHECK_AVAILABLE, STR_NULL }, // DNGRFS_CHECK_AVILABLE |
|
294 { WWT_PUSHTXTBTN, RESIZE_RTB, 10, 144, 287, 213, 224, STR_NEWGRF_DOWNLOAD_AVAILABLE, STR_NULL }, // DNGRFS_DOWNLOAD_AVAILABLE |
|
295 { WWT_RESIZEBOX, RESIZE_LRTB, 10, 288, 299, 213, 224, 0x0, STR_RESIZE_BUTTON }, // DNGRFS_RESIZE |
|
296 { WIDGETS_END }, |
|
297 }; |
|
298 |
|
299 /* Window definition of the manage newgrfs window */ |
|
300 static const WindowDesc _newgrf_download_desc = { |
|
301 WDP_CENTER, WDP_CENTER, 300, 225, 300, 225, |
|
302 WC_GAME_OPTIONS, WC_NONE, |
|
303 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, |
|
304 _newgrf_download_widgets, |
|
305 }; |
|
306 |
|
307 |
|
308 /** Setup the NewGRF download gui |
|
309 * @param config pointer to a linked-list of grfconfig's needed */ |
|
310 void ShowNewGRFDownload(GRFConfig **config) |
|
311 { |
|
312 DeleteWindowByClass(WC_GAME_OPTIONS); |
|
313 new NewGRFDownloadWindow(&_newgrf_download_desc, config); |
|
314 } |
|
315 |