peter1138@5237: /* $Id$ */ peter1138@5237: rubidium@10429: /** @file newgrf_gui.cpp GUI to change NewGRF settings. */ belugas@6674: peter1138@5237: #include "stdafx.h" peter1138@5237: #include "openttd.h" peter1138@5237: #include "variables.h" peter1138@5237: #include "gui.h" rubidium@8603: #include "window_gui.h" rubidium@8603: #include "textbuf_gui.h" Darkvater@5352: #include "newgrf.h" peter1138@5237: #include "newgrf_config.h" rubidium@8610: #include "strings_func.h" rubidium@8627: #include "window_func.h" rubidium@8709: #include "core/alloc_func.hpp" rubidium@8710: #include "string_func.h" rubidium@8720: #include "gfx_func.h" smatz@10824: #include "gamelog.h" peter1138@5237: rubidium@8760: #include "table/strings.h" rubidium@8760: #include "table/sprites.h" rubidium@8760: peter1138@5237: /** Parse an integerlist string and set each found value peter1138@5237: * @param p the string to be parsed. Each element in the list is seperated by a peter1138@5237: * comma or a space character peter1138@5237: * @param items pointer to the integerlist-array that will be filled with values peter1138@5237: * @param maxitems the maximum number of elements the integerlist-array has peter1138@5237: * @return returns the number of items found, or -1 on an error */ peter1138@5237: static int parse_intlist(const char *p, int *items, int maxitems) peter1138@5237: { peter1138@5237: int n = 0, v; peter1138@5237: char *end; peter1138@5237: peter1138@5237: for (;;) { peter1138@5237: v = strtol(p, &end, 0); peter1138@5237: if (p == end || n == maxitems) return -1; peter1138@5237: p = end; peter1138@5237: items[n++] = v; peter1138@5237: if (*p == '\0') break; peter1138@5237: if (*p != ',' && *p != ' ') return -1; peter1138@5237: p++; peter1138@5237: } peter1138@5237: peter1138@5237: return n; peter1138@5237: } peter1138@5237: peter1138@5237: maedhros@6703: static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, uint bottom, bool show_params) peter1138@5237: { Darkvater@5885: char buff[256]; peter1138@5237: maedhros@6429: if (c->error != NULL) { rubidium@11157: char message[512]; rubidium@11157: if (c->error->custom_message == NULL) { rubidium@11157: SetDParamStr(0, c->filename); rubidium@11157: SetDParamStr(1, c->error->data); rubidium@11157: for (uint i = 0; i < c->error->num_params; i++) { rubidium@11157: uint32 param = 0; rubidium@11157: byte param_number = c->error->param_number[i]; maedhros@6465: rubidium@11157: if (param_number < c->num_params) param = c->param[param_number]; maedhros@6465: rubidium@11157: SetDParam(2 + i, param); rubidium@11157: } rubidium@11157: rubidium@11157: GetString(message, c->error->message, lastof(message)); rubidium@11157: } else { rubidium@11157: SetDParamStr(0, c->error->custom_message); rubidium@11157: GetString(message, STR_JUST_RAW_STRING, lastof(message)); maedhros@6465: } maedhros@6465: maedhros@6465: SetDParamStr(0, message); maedhros@6703: y += DrawStringMultiLine(x, y, c->error->severity, w, bottom - y); maedhros@6429: } maedhros@6429: rubidium@5339: /* Draw filename or not if it is not known (GRF sent over internet) */ rubidium@5339: if (c->filename != NULL) { rubidium@5339: SetDParamStr(0, c->filename); maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_FILENAME, w, bottom - y); rubidium@5339: } peter1138@5237: peter1138@5237: /* Prepare and draw GRF ID */ Darkvater@5894: snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->grfid)); peter1138@5237: SetDParamStr(0, buff); maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_GRF_ID, w, bottom - y); peter1138@5237: peter1138@5237: /* Prepare and draw MD5 sum */ Darkvater@5885: md5sumToString(buff, lastof(buff), c->md5sum); peter1138@5237: SetDParamStr(0, buff); maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_MD5SUM, w, bottom - y); peter1138@5237: peter1138@5237: /* Show GRF parameter list */ peter1138@5237: if (show_params) { peter1138@5237: if (c->num_params > 0) { peter1138@5308: GRFBuildParamList(buff, c, lastof(buff)); rubidium@11157: SetDParam(0, STR_JUST_RAW_STRING); rubidium@11157: SetDParamStr(1, buff); peter1138@5237: } else { peter1138@5237: SetDParam(0, STR_01A9_NONE); peter1138@5237: } maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_PARAMETER, w, bottom - y); peter1138@5237: } peter1138@5237: peter1138@5237: /* Show flags */ maedhros@6703: if (c->status == GCS_NOT_FOUND) y += DrawStringMultiLine(x, y, STR_NEWGRF_NOT_FOUND, w, bottom - y); maedhros@6703: if (c->status == GCS_DISABLED) y += DrawStringMultiLine(x, y, STR_NEWGRF_DISABLED, w, bottom - y); skidd13@8424: if (HasBit(c->flags, GCF_COMPATIBLE)) y += DrawStringMultiLine(x, y, STR_NEWGRF_COMPATIBLE_LOADED, w, bottom - y); peter1138@5237: peter1138@5237: /* Draw GRF info if it exists */ tron@5889: if (c->info != NULL && !StrEmpty(c->info)) { rubidium@11157: SetDParam(0, STR_JUST_RAW_STRING); rubidium@11157: SetDParamStr(1, c->info); maedhros@6703: y += DrawStringMultiLine(x, y, STR_02BD, w, bottom - y); peter1138@5237: } else { maedhros@6703: y += DrawStringMultiLine(x, y, STR_NEWGRF_NO_INFO, w, bottom - y); peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@5237: peter1138@10529: /** peter1138@10529: * Window for adding NewGRF files peter1138@10529: */ peter1138@10529: struct NewGRFAddWindow : public Window { peter1138@10529: /* Names of the add a newgrf window widgets */ peter1138@10529: enum AddNewGRFWindowWidgets { peter1138@10529: ANGRFW_CLOSEBOX = 0, peter1138@10529: ANGRFW_CAPTION, peter1138@10529: ANGRFW_BACKGROUND, peter1138@10529: ANGRFW_GRF_LIST, peter1138@10529: ANGRFW_SCROLLBAR, peter1138@10529: ANGRFW_GRF_INFO, peter1138@10529: ANGRFW_ADD, peter1138@10529: ANGRFW_RESCAN, peter1138@10529: ANGRFW_RESIZE, peter1138@10529: }; peter1138@10529: peter1138@5237: GRFConfig **list; peter1138@5237: const GRFConfig *sel; peter1138@5237: peter1138@10529: NewGRFAddWindow(const WindowDesc *desc, GRFConfig **list) : Window(desc, 0) peter1138@10529: { peter1138@10529: this->list = list; peter1138@10529: this->resize.step_height = 10; peter1138@5237: peter1138@10529: this->FindWindowPlacementAndResize(desc); peter1138@10529: } peter1138@5237: peter1138@10529: virtual void OnPaint() peter1138@10529: { peter1138@10529: const GRFConfig *c; peter1138@10529: const Widget *wl = &this->widget[ANGRFW_GRF_LIST]; peter1138@10529: int n = 0; peter1138@5237: peter1138@10529: /* Count the number of GRFs */ terom@11180: for (c = _all_grfs; c != NULL; c = c->next) terom@11180: if (!HasBit(c->flags, GCF_CACHE)) terom@11180: n++; peter1138@10529: peter1138@10529: this->vscroll.cap = (wl->bottom - wl->top) / 10; peter1138@10529: SetVScrollCount(this, n); peter1138@10529: peter1138@10529: this->SetWidgetDisabledState(ANGRFW_ADD, this->sel == NULL || this->sel->IsOpenTTDBaseGRF()); rubidium@10595: this->DrawWidgets(); peter1138@10529: peter1138@10529: GfxFillRect(wl->left + 1, wl->top + 1, wl->right, wl->bottom, 0xD7); peter1138@10529: peter1138@10529: uint y = wl->top + 1; peter1138@10529: for (c = _all_grfs, n = 0; c != NULL && n < (this->vscroll.pos + this->vscroll.cap); c = c->next, n++) { terom@11180: if (!HasBit(c->flags, GCF_CACHE) && n >= this->vscroll.pos) { peter1138@10529: bool h = c == this->sel; peter1138@10529: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@10529: peter1138@10529: /* Draw selection background */ peter1138@10529: if (h) GfxFillRect(3, y, this->width - 15, y + 9, 156); peter1138@10529: DoDrawStringTruncated(text, 4, y, h ? TC_WHITE : TC_ORANGE, this->width - 18); peter1138@10529: y += 10; peter1138@10529: } peter1138@10529: } peter1138@10529: peter1138@10529: if (this->sel != NULL) { peter1138@10529: const Widget *wi = &this->widget[ANGRFW_GRF_INFO]; peter1138@10529: ShowNewGRFInfo(this->sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, false); peter1138@10529: } peter1138@10529: } peter1138@10529: peter1138@10529: virtual void OnDoubleClick(Point pt, int widget) peter1138@10529: { peter1138@10529: if (widget == ANGRFW_GRF_LIST) this->OnClick(pt, ANGRFW_ADD); peter1138@10529: } peter1138@10529: peter1138@10529: virtual void OnClick(Point pt, int widget) peter1138@10529: { peter1138@10529: switch (widget) { peter1138@10529: case ANGRFW_GRF_LIST: { peter1138@10529: /* Get row... */ peter1138@10529: const GRFConfig *c; peter1138@10529: uint i = (pt.y - this->widget[ANGRFW_GRF_LIST].top) / 10 + this->vscroll.pos; peter1138@10529: peter1138@10529: for (c = _all_grfs; c != NULL && i > 0; c = c->next, i--) {} peter1138@10529: this->sel = c; peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@5237: } peter1138@5237: peter1138@10529: case ANGRFW_ADD: // Add selection to list peter1138@10529: if (this->sel != NULL) { peter1138@10529: const GRFConfig *src = this->sel; peter1138@10529: GRFConfig **list; peter1138@5243: peter1138@10529: /* Find last entry in the list, checking for duplicate grfid on the way */ peter1138@10529: for (list = this->list; *list != NULL; list = &(*list)->next) { peter1138@10529: if ((*list)->grfid == src->grfid) { peter1138@10529: ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DUPLICATE_GRFID, 0, 0); peter1138@10529: return; peter1138@5243: } peter1138@10529: } peter1138@5243: peter1138@10529: /* Copy GRF details from scanned list */ peter1138@10529: GRFConfig *c = CallocT(1); peter1138@10529: *c = *src; peter1138@10529: c->filename = strdup(src->filename); peter1138@10529: if (src->name != NULL) c->name = strdup(src->name); peter1138@10529: if (src->info != NULL) c->info = strdup(src->info); peter1138@10529: c->next = NULL; peter1138@5237: peter1138@10529: /* Append GRF config to configuration list */ peter1138@10529: *list = c; peter1138@10529: peter1138@10529: DeleteWindowByClass(WC_SAVELOAD); peter1138@10529: InvalidateWindowData(WC_GAME_OPTIONS, 0); peter1138@10529: } peter1138@10529: break; peter1138@10529: peter1138@10529: case ANGRFW_RESCAN: // Rescan list peter1138@10529: this->sel = NULL; peter1138@10529: ScanNewGRFFiles(); peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@10529: } peter1138@5237: } peter1138@10529: }; peter1138@5237: skidd13@8521: /* Widget definition for the add a newgrf window */ peter1138@5237: static const Widget _newgrf_add_dlg_widgets[] = { skidd13@8521: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // ANGRFW_CLOSEBOX skidd13@8521: { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 306, 0, 13, STR_NEWGRF_ADD_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // ANGRFW_CAPTION skidd13@8521: { WWT_PANEL, RESIZE_RB, 14, 0, 294, 14, 121, 0x0, STR_NULL }, // ANGRFW_BACKGROUND skidd13@8521: { WWT_INSET, RESIZE_RB, 14, 2, 292, 16, 119, 0x0, STR_NULL }, // ANGRFW_GRF_LIST skidd13@8521: { WWT_SCROLLBAR, RESIZE_LRB, 14, 295, 306, 14, 121, 0x0, STR_NULL }, // ANGRFW_SCROLLBAR skidd13@8521: { WWT_PANEL, RESIZE_RTB, 14, 0, 306, 122, 224, 0x0, STR_NULL }, // ANGRFW_GRF_INFO skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_RTB, 14, 0, 146, 225, 236, STR_NEWGRF_ADD_FILE, STR_NEWGRF_ADD_FILE_TIP }, // ANGRFW_ADD skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_LRTB, 14, 147, 294, 225, 236, STR_NEWGRF_RESCAN_FILES, STR_NEWGRF_RESCAN_FILES_TIP }, // ANGRFW_RESCAN skidd13@8521: { WWT_RESIZEBOX, RESIZE_LRTB, 14, 295, 306, 225, 236, 0x0, STR_RESIZE_BUTTON }, // ANGRFW_RESIZE peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: skidd13@8521: /* Window definition for the add a newgrf window */ peter1138@5237: static const WindowDesc _newgrf_add_dlg_desc = { rubidium@7837: WDP_CENTER, WDP_CENTER, 307, 237, 307, 337, rubidium@6144: WC_SAVELOAD, WC_NONE, belugas@8515: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, peter1138@5237: _newgrf_add_dlg_widgets, peter1138@5237: }; peter1138@5237: peter1138@5237: peter1138@10529: static void NewGRFConfirmationCallback(Window *w, bool confirmed); peter1138@10529: peter1138@10529: /** peter1138@10529: * Window for showing NewGRF files peter1138@10529: */ peter1138@10529: struct NewGRFWindow : public Window { peter1138@10529: /* Names of the manage newgrfs window widgets */ peter1138@10529: enum ShowNewGRFStateWidgets { peter1138@10529: SNGRFS_CLOSEBOX = 0, peter1138@10529: SNGRFS_CAPTION, peter1138@10529: SNGRFS_BACKGROUND, peter1138@10529: SNGRFS_ADD, peter1138@10529: SNGRFS_REMOVE, peter1138@10529: SNGRFS_MOVE_UP, peter1138@10529: SNGRFS_MOVE_DOWN, peter1138@10529: SNGRFS_FILE_LIST, peter1138@10529: SNGRFS_SCROLLBAR, peter1138@10529: SNGRFS_NEWGRF_INFO, peter1138@10529: SNGRFS_SET_PARAMETERS, peter1138@10529: SNGRFS_APPLY_CHANGES, peter1138@10529: SNGRFS_RESIZE, peter1138@10529: }; peter1138@10529: Darkvater@5352: GRFConfig **orig_list; ///< grf list the window is shown with peter1138@10529: GRFConfig *list; ///< temporary grf list to which changes are made Darkvater@5352: GRFConfig *sel; ///< selected grf item Darkvater@5352: bool editable; ///< is the window editable Darkvater@5352: bool show_params; ///< are the grf-parameters shown in the info-panel Darkvater@5352: bool execute; ///< on pressing 'apply changes' are grf changes applied immediately, or only list is updated Darkvater@5352: peter1138@10529: NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool exec_changes, GRFConfig **config) : Window(desc, 0) peter1138@10529: { peter1138@10529: this->resize.step_height = 14; peter1138@10529: this->sel = NULL; peter1138@10529: this->list = NULL; peter1138@10529: this->orig_list = config; peter1138@10529: this->editable = editable; peter1138@10529: this->execute = exec_changes; peter1138@10529: this->show_params = show_params; peter1138@5237: peter1138@10529: CopyGRFConfigList(&this->list, *config, false); maedhros@6555: peter1138@10529: this->FindWindowPlacementAndResize(desc); peter1138@10529: this->SetupNewGRFWindow(); peter1138@10529: } peter1138@5237: peter1138@10529: ~NewGRFWindow() peter1138@10529: { peter1138@10529: if (!this->execute) { peter1138@10529: CopyGRFConfigList(this->orig_list, this->list, true); peter1138@10529: ResetGRFConfig(false); peter1138@10529: ReloadNewGRFData(); peter1138@5237: } peter1138@5237: peter1138@10529: /* Remove the temporary copy of grf-list used in window */ peter1138@10529: ClearGRFConfigList(&this->list); peter1138@10529: } peter1138@5248: peter1138@10529: void SetupNewGRFWindow() peter1138@10529: { peter1138@10529: const GRFConfig *c; peter1138@10529: int i; peter1138@5248: peter1138@10529: for (c = this->list, i = 0; c != NULL; c = c->next, i++) {} peter1138@5237: peter1138@10529: this->vscroll.cap = (this->widget[SNGRFS_FILE_LIST].bottom - this->widget[SNGRFS_FILE_LIST].top) / 14 + 1; peter1138@10529: SetVScrollCount(this, i); peter1138@10529: peter1138@10529: this->SetWidgetDisabledState(SNGRFS_ADD, !this->editable); peter1138@10529: this->SetWidgetDisabledState(SNGRFS_APPLY_CHANGES, !this->editable); peter1138@10529: } peter1138@10529: peter1138@10529: virtual void OnPaint() peter1138@10529: { peter1138@10529: bool disable_all = this->sel == NULL || !this->editable; peter1138@10529: peter1138@10529: this->SetWidgetsDisabledState(disable_all, peter1138@10529: SNGRFS_REMOVE, peter1138@10529: SNGRFS_MOVE_UP, peter1138@10529: SNGRFS_MOVE_DOWN, peter1138@10529: WIDGET_LIST_END peter1138@10529: ); peter1138@10529: this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all); peter1138@10529: peter1138@10529: if (!disable_all) { peter1138@10529: /* All widgets are now enabled, so disable widgets we can't use */ peter1138@10529: if (this->sel == this->list) this->DisableWidget(SNGRFS_MOVE_UP); peter1138@10529: if (this->sel->next == NULL) this->DisableWidget(SNGRFS_MOVE_DOWN); peter1138@10529: if (this->sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE); peter1138@10529: } peter1138@10529: rubidium@10595: this->DrawWidgets(); peter1138@10529: peter1138@10529: /* Draw NewGRF list */ peter1138@10529: int y = this->widget[SNGRFS_FILE_LIST].top; peter1138@10529: int i = 0; peter1138@10529: for (const GRFConfig *c = this->list; c != NULL; c = c->next, i++) { peter1138@10529: if (i >= this->vscroll.pos && i < this->vscroll.pos + this->vscroll.cap) { peter1138@10529: const char *text = (c->name != NULL && !StrEmpty(c->name)) ? c->name : c->filename; peter1138@10529: SpriteID pal; peter1138@10529: byte txtoffset; peter1138@10529: peter1138@10529: /* Pick a colour */ peter1138@10529: switch (c->status) { peter1138@10529: case GCS_NOT_FOUND: peter1138@10529: case GCS_DISABLED: peter1138@10529: pal = PALETTE_TO_RED; peter1138@10529: break; peter1138@10529: case GCS_ACTIVATED: peter1138@10529: pal = PALETTE_TO_GREEN; peter1138@10529: break; peter1138@10529: default: peter1138@10529: pal = PALETTE_TO_BLUE; peter1138@10529: break; peter1138@5237: } peter1138@5237: peter1138@10529: /* Do not show a "not-failure" colour when it actually failed to load */ peter1138@10529: if (pal != PALETTE_TO_RED) { peter1138@10529: if (HasBit(c->flags, GCF_STATIC)) { peter1138@10529: pal = PALETTE_TO_GREY; peter1138@10529: } else if (HasBit(c->flags, GCF_COMPATIBLE)) { peter1138@10529: pal = PALETTE_TO_ORANGE; peter1138@5237: } peter1138@5237: } peter1138@5237: peter1138@10529: DrawSprite(SPR_SQUARE, pal, 5, y + 2); peter1138@10529: if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, 20, y + 2); peter1138@10529: txtoffset = c->error != NULL ? 35 : 25; peter1138@10529: DoDrawStringTruncated(text, txtoffset, y + 3, this->sel == c ? TC_WHITE : TC_BLACK, this->width - txtoffset - 10); peter1138@10529: y += 14; peter1138@5237: } peter1138@10529: } peter1138@5237: peter1138@10529: if (this->sel != NULL) { peter1138@10529: /* Draw NewGRF file info */ peter1138@10529: const Widget *wi = &this->widget[SNGRFS_NEWGRF_INFO]; peter1138@10529: ShowNewGRFInfo(this->sel, wi->left + 2, wi->top + 2, wi->right - wi->left - 2, wi->bottom, this->show_params); peter1138@10529: } peter1138@10529: } peter1138@5237: peter1138@10529: virtual void OnClick(Point pt, int widget) peter1138@10529: { peter1138@10529: switch (widget) { peter1138@10529: case SNGRFS_ADD: // Add GRF peter1138@10529: DeleteWindowByClass(WC_SAVELOAD); peter1138@10529: new NewGRFAddWindow(&_newgrf_add_dlg_desc, &this->list); peter1138@10529: break; Darkvater@5352: peter1138@10529: case SNGRFS_REMOVE: { // Remove GRF peter1138@10529: GRFConfig **pc, *c, *newsel; peter1138@10529: peter1138@10529: /* Choose the next GRF file to be the selected file */ peter1138@10529: newsel = this->sel->next; peter1138@10529: peter1138@10529: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@10529: /* If the new selection is empty (i.e. we're deleting the last item peter1138@10529: * in the list, pick the file just before the selected file */ peter1138@10529: if (newsel == NULL && c->next == this->sel) newsel = c; peter1138@10529: peter1138@10529: if (c == this->sel) { peter1138@10529: *pc = c->next; peter1138@10529: free(c); peter1138@10529: break; peter1138@10529: } peter1138@10529: } peter1138@10529: peter1138@10529: this->sel = newsel; peter1138@10529: this->SetupNewGRFWindow(); peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@9087: } peter1138@10529: peter1138@10529: case SNGRFS_MOVE_UP: { // Move GRF up peter1138@10529: GRFConfig **pc, *c; peter1138@10529: if (this->sel == NULL) break; peter1138@10529: peter1138@10529: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@10529: if (c->next == this->sel) { peter1138@10529: c->next = this->sel->next; peter1138@10529: this->sel->next = c; peter1138@10529: *pc = this->sel; peter1138@10529: break; peter1138@10529: } peter1138@10529: } peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@10529: } peter1138@10529: peter1138@10529: case SNGRFS_MOVE_DOWN: { // Move GRF down peter1138@10529: GRFConfig **pc, *c; peter1138@10529: if (this->sel == NULL) break; peter1138@10529: peter1138@10529: for (pc = &this->list; (c = *pc) != NULL; pc = &c->next) { peter1138@10529: if (c == this->sel) { peter1138@10529: *pc = c->next; peter1138@10529: c->next = c->next->next; peter1138@10529: (*pc)->next = c; peter1138@10529: break; peter1138@10529: } peter1138@10529: } peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@10529: } peter1138@10529: peter1138@10529: case SNGRFS_FILE_LIST: { // Select a GRF peter1138@10529: GRFConfig *c; peter1138@10529: uint i = (pt.y - this->widget[SNGRFS_FILE_LIST].top) / 14 + this->vscroll.pos; peter1138@10529: peter1138@10529: for (c = this->list; c != NULL && i > 0; c = c->next, i--) {} peter1138@10529: this->sel = c; peter1138@10529: peter1138@10529: this->SetDirty(); peter1138@10529: break; peter1138@10529: } peter1138@10529: peter1138@10529: case SNGRFS_APPLY_CHANGES: // Apply changes made to GRF list peter1138@10529: if (this->execute) { peter1138@10529: ShowQuery( peter1138@10529: STR_POPUP_CAUTION_CAPTION, peter1138@10529: STR_NEWGRF_CONFIRMATION_TEXT, peter1138@10529: this, peter1138@10529: NewGRFConfirmationCallback peter1138@10529: ); peter1138@10529: } else { peter1138@10529: CopyGRFConfigList(this->orig_list, this->list, true); peter1138@10529: ResetGRFConfig(false); peter1138@10529: ReloadNewGRFData(); peter1138@10529: } peter1138@10529: break; peter1138@10529: peter1138@10529: case SNGRFS_SET_PARAMETERS: { // Edit parameters peter1138@10529: if (this->sel == NULL) break; peter1138@10529: rubidium@11157: static char buff[512]; peter1138@10529: GRFBuildParamList(buff, this->sel, lastof(buff)); rubidium@11157: SetDParamStr(0, buff); rubidium@11157: ShowQueryString(STR_JUST_RAW_STRING, STR_NEWGRF_PARAMETER_QUERY, 63, 250, this, CS_ALPHANUMERAL); peter1138@10529: break; peter1138@10529: } peter1138@10529: } peter1138@5237: } peter1138@10529: peter1138@10529: virtual void OnQueryTextFinished(char *str) peter1138@10529: { peter1138@10529: if (str == NULL) return; peter1138@10529: peter1138@10529: /* Parse our new "int list" */ peter1138@10529: GRFConfig *c = this->sel; peter1138@10529: c->num_params = parse_intlist(str, (int*)c->param, lengthof(c->param)); peter1138@10529: peter1138@10529: /* parse_intlist returns -1 on error */ peter1138@10529: if (c->num_params == (byte)-1) c->num_params = 0; peter1138@10529: peter1138@10529: this->SetDirty(); peter1138@10529: } peter1138@10529: peter1138@10529: virtual void OnResize(Point new_size, Point delta) peter1138@10529: { peter1138@10529: if (delta.x != 0) { peter1138@10529: ResizeButtons(this, SNGRFS_ADD, SNGRFS_MOVE_DOWN); peter1138@10529: ResizeButtons(this, SNGRFS_SET_PARAMETERS, SNGRFS_APPLY_CHANGES); peter1138@10529: } peter1138@10529: peter1138@10529: this->vscroll.cap += delta.y / 14; peter1138@10529: this->widget[SNGRFS_FILE_LIST].data = (this->vscroll.cap << 8) + 1; peter1138@10529: peter1138@10529: this->SetupNewGRFWindow(); peter1138@10529: } peter1138@10529: peter1138@10529: virtual void OnInvalidateData(int data = 0) peter1138@10529: { peter1138@10529: this->SetupNewGRFWindow(); peter1138@10529: } peter1138@10529: }; peter1138@5237: skidd13@8521: /* Widget definition of the manage newgrfs window */ peter1138@5237: static const Widget _newgrf_widgets[] = { skidd13@8521: { WWT_CLOSEBOX, RESIZE_NONE, 10, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, // SNGRFS_CLOSEBOX skidd13@8521: { WWT_CAPTION, RESIZE_RIGHT, 10, 11, 299, 0, 13, STR_NEWGRF_SETTINGS_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS }, // SNGRFS_CAPTION skidd13@8521: { WWT_PANEL, RESIZE_RIGHT, 10, 0, 299, 14, 29, STR_NULL, STR_NULL }, // SNGRFS_BACKGROUND skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 10, 79, 16, 27, STR_NEWGRF_ADD, STR_NEWGRF_ADD_TIP }, // SNGRFS_ADD skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 80, 149, 16, 27, STR_NEWGRF_REMOVE, STR_NEWGRF_REMOVE_TIP }, // SNGRFS_REMOVE skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_NONE, 3, 150, 219, 16, 27, STR_NEWGRF_MOVEUP, STR_NEWGRF_MOVEUP_TIP }, // SNGRFS_MOVE_UP peter1138@9087: { WWT_PUSHTXTBTN, RESIZE_RIGHT, 3, 220, 289, 16, 27, STR_NEWGRF_MOVEDOWN, STR_NEWGRF_MOVEDOWN_TIP }, // SNGRFS_MOVE_DOWN skidd13@8521: { WWT_MATRIX, RESIZE_RB, 10, 0, 287, 30, 99, 0x501, STR_NEWGRF_FILE_TIP }, // SNGRFS_FILE_LIST skidd13@8521: { WWT_SCROLLBAR, RESIZE_LRB, 10, 288, 299, 30, 99, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST }, // SNGRFS_SCROLLBAR skidd13@8521: { WWT_PANEL, RESIZE_RTB, 10, 0, 299, 100, 212, STR_NULL, STR_NULL }, // SNGRFS_NEWGRF_INFO skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_TB, 10, 0, 143, 213, 224, STR_NEWGRF_SET_PARAMETERS, STR_NULL }, // SNGRFS_SET_PARAMETERS skidd13@8521: { WWT_PUSHTXTBTN, RESIZE_RTB, 10, 144, 287, 213, 224, STR_NEWGRF_APPLY_CHANGES, STR_NULL }, // SNGRFS_APPLY_CHANGES skidd13@8521: { WWT_RESIZEBOX, RESIZE_LRTB, 10, 288, 299, 213, 224, 0x0, STR_RESIZE_BUTTON }, // SNGRFS_RESIZE peter1138@5237: { WIDGETS_END }, peter1138@5237: }; peter1138@5237: skidd13@8521: /* Window definition of the manage newgrfs window */ peter1138@5237: static const WindowDesc _newgrf_desc = { rubidium@7837: WDP_CENTER, WDP_CENTER, 300, 225, 300, 225, rubidium@6144: WC_GAME_OPTIONS, WC_NONE, peter1138@5237: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, peter1138@5237: _newgrf_widgets, peter1138@5237: }; peter1138@5237: peter1138@10529: /** Callback function for the newgrf 'apply changes' confirmation window peter1138@10529: * @param w Window which is calling this callback peter1138@10529: * @param confirmed boolean value, true when yes was clicked, false otherwise peter1138@10529: */ peter1138@10529: static void NewGRFConfirmationCallback(Window *w, bool confirmed) peter1138@10529: { peter1138@10529: if (confirmed) { peter1138@10529: NewGRFWindow *nw = dynamic_cast(w); peter1138@10529: GRFConfig *c; peter1138@10529: int i = 0; peter1138@10529: smatz@10824: GamelogStartAction(GLAT_GRF); smatz@10824: GamelogGRFUpdate(_grfconfig, nw->list); // log GRF changes peter1138@10529: CopyGRFConfigList(nw->orig_list, nw->list, false); peter1138@10529: ReloadNewGRFData(); smatz@10824: GamelogStopAction(); peter1138@10529: peter1138@10529: /* Show new, updated list */ peter1138@10529: for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {} peter1138@10529: CopyGRFConfigList(&nw->list, *nw->orig_list, false); peter1138@10529: for (c = nw->list; c != NULL && i > 0; c = c->next, i--) {} peter1138@10529: nw->sel = c; peter1138@10529: peter1138@10529: w->SetDirty(); peter1138@10529: } peter1138@10529: } peter1138@10529: peter1138@10529: peter1138@5237: Darkvater@5352: /** Setup the NewGRF gui Darkvater@5352: * @param editable allow the user to make changes to the grfconfig in the window Darkvater@5352: * @param show_params show information about what parameters are set for the grf files Darkvater@5352: * @param exec_changes if changes are made to the list (editable is true), apply these Darkvater@5352: * changes immediately or only update the list Darkvater@5352: * @param config pointer to a linked-list of grfconfig's that will be shown */ Darkvater@5352: void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config) peter1138@5237: { peter1138@5237: DeleteWindowByClass(WC_GAME_OPTIONS); peter1138@10529: new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config); peter1138@5237: }