tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file engine_gui.cpp GUI to show engine related information. */ belugas@6125: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@0: #include "gui.h" rubidium@8107: #include "window_gui.h" rubidium@8224: #include "gfx_func.h" rubidium@8786: #include "engine_func.h" peter1138@9070: #include "engine_base.h" rubidium@8116: #include "command_func.h" rubidium@8116: #include "economy_func.h" rubidium@8763: #include "news_func.h" tron@2159: #include "variables.h" peter1138@2962: #include "newgrf_engine.h" rubidium@8114: #include "strings_func.h" peter1138@9380: #include "engine_gui.h" peter1138@9567: #include "articulated_vehicles.h" truelight@0: rubidium@8264: #include "table/strings.h" rubidium@8264: #include "table/sprites.h" truelight@0: rubidium@9199: StringID GetEngineCategoryName(EngineID engine) truelight@0: { rubidium@6209: switch (GetEngine(engine)->type) { rubidium@6209: default: NOT_REACHED(); rubidium@6259: case VEH_ROAD: return STR_8103_ROAD_VEHICLE; rubidium@6259: case VEH_AIRCRAFT: return STR_8104_AIRCRAFT; rubidium@6259: case VEH_SHIP: return STR_8105_SHIP; rubidium@6259: case VEH_TRAIN: rubidium@6209: switch (RailVehInfo(engine)->railtype) { rubidium@6209: default: NOT_REACHED(); rubidium@6209: case RAILTYPE_RAIL: return STR_8102_RAILROAD_LOCOMOTIVE; rubidium@6209: case RAILTYPE_ELECTRIC: return STR_8102_RAILROAD_LOCOMOTIVE; rubidium@6209: case RAILTYPE_MONO: return STR_8106_MONORAIL_LOCOMOTIVE; rubidium@6209: case RAILTYPE_MAGLEV: return STR_8107_MAGLEV_LOCOMOTIVE; rubidium@6209: } darkvater@460: } truelight@0: } truelight@0: truelight@0: static const Widget _engine_preview_widgets[] = { belugas@9744: { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, belugas@9744: { WWT_CAPTION, RESIZE_NONE, COLOUR_LIGHT_BLUE, 11, 299, 0, 13, STR_8100_MESSAGE_FROM_VEHICLE_MANUFACTURE, STR_018C_WINDOW_TITLE_DRAG_THIS}, belugas@9744: { WWT_PANEL, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 299, 14, 191, 0x0, STR_NULL}, belugas@9744: { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 85, 144, 172, 183, STR_00C9_NO, STR_NULL}, belugas@9744: { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 155, 214, 172, 183, STR_00C8_YES, STR_NULL}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: peter1138@5668: typedef void DrawEngineProc(int x, int y, EngineID engine, SpriteID pal); tron@2477: typedef void DrawEngineInfoProc(EngineID, int x, int y, int maxw); truelight@0: rubidium@6248: struct DrawEngineInfo { truelight@0: DrawEngineProc *engine_proc; truelight@0: DrawEngineInfoProc *info_proc; rubidium@6248: }; truelight@0: tron@2477: static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw); tron@2477: static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw); tron@2477: static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw); tron@2477: static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw); tron@1998: truelight@0: static const DrawEngineInfo _draw_engine_list[4] = { rubidium@6209: { DrawTrainEngine, DrawTrainEngineInfo }, rubidium@6209: { DrawRoadVehEngine, DrawRoadVehEngineInfo }, rubidium@6209: { DrawShipEngine, DrawShipEngineInfo }, rubidium@6209: { DrawAircraftEngine, DrawAircraftEngineInfo }, truelight@0: }; truelight@0: rubidium@9302: struct EnginePreviewWindow : Window { rubidium@9302: EnginePreviewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) rubidium@9302: { peter1138@9333: this->FindWindowPlacementAndResize(desc); rubidium@9302: } tron@2477: rubidium@9302: virtual void OnPaint() rubidium@9302: { rubidium@9302: this->DrawWidgets(); truelight@0: rubidium@9302: EngineID engine = this->window_number; tron@534: SetDParam(0, GetEngineCategoryName(engine)); truelight@0: DrawStringMultiCenter(150, 44, STR_8101_WE_HAVE_JUST_DESIGNED_A, 296); truelight@0: peter1138@7059: SetDParam(0, engine); rubidium@9302: DrawStringCentered(this->width >> 1, 80, STR_ENGINE_NAME, TC_BLACK); truelight@0: rubidium@9302: const DrawEngineInfo *dei = &_draw_engine_list[GetEngine(engine)->type]; truelight@0: rubidium@9302: int width = this->width; truelight@0: dei->engine_proc(width >> 1, 100, engine, 0); truelight@0: dei->info_proc(engine, width >> 1, 130, width - 52); tron@2477: } truelight@0: rubidium@9302: virtual void OnClick(Point pt, int widget) rubidium@9302: { rubidium@9302: switch (widget) { tron@2639: case 4: rubidium@9302: DoCommandP(0, this->window_number, 0, NULL, CMD_WANT_ENGINE_PREVIEW); Darkvater@5125: /* Fallthrough */ Darkvater@5125: case 3: rubidium@9302: delete this; tron@2639: break; truelight@0: } truelight@0: } rubidium@9302: }; truelight@0: truelight@0: static const WindowDesc _engine_preview_desc = { rubidium@7341: WDP_CENTER, WDP_CENTER, 300, 192, 300, 192, rubidium@5893: WC_ENGINE_PREVIEW, WC_NONE, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, truelight@0: _engine_preview_widgets, truelight@0: }; truelight@0: truelight@0: tron@2477: void ShowEnginePreviewWindow(EngineID engine) truelight@0: { rubidium@9302: AllocateWindowDescFront(&_engine_preview_desc, engine); truelight@0: } truelight@0: peter1138@9567: static uint GetTotalCapacityOfArticulatedParts(EngineID engine, VehicleType type) peter1138@9567: { peter1138@9567: uint total = 0; peter1138@9567: peter1138@9567: uint16 *cap = GetCapacityOfArticulatedParts(engine, type); peter1138@9567: for (uint c = 0; c < NUM_CARGO; c++) { peter1138@9567: total += cap[c]; peter1138@9567: } peter1138@9567: peter1138@9567: return total; peter1138@9567: } peter1138@9567: tron@2477: static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw) tron@1998: { tron@1998: const RailVehicleInfo *rvi = RailVehInfo(engine); rubidium@7763: int multihead = (rvi->railveh_type == RAILVEH_MULTIHEAD) ? 1 : 0; tron@1998: peter1138@9923: SetDParam(0, (_price.build_railvehicle >> 3) * GetEngineProperty(engine, 0x17, rvi->cost_factor) >> 5); peter1138@9567: SetDParam(2, GetEngineProperty(engine, 0x09, rvi->max_speed) * 10 / 16); frosch@9720: SetDParam(3, GetEngineProperty(engine, 0x0B, rvi->power)); peter1138@9567: SetDParam(1, GetEngineProperty(engine, 0x16, rvi->weight) << multihead); tron@1998: frosch@9720: SetDParam(4, GetEngineProperty(engine, 0x0D, rvi->running_cost) * GetPriceByIndex(rvi->running_cost_class) >> 8); tron@1998: peter1138@9567: uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN); peter1138@9567: if (capacity != 0) { peter1138@4896: SetDParam(5, rvi->cargo_type); frosch@9720: SetDParam(6, capacity); tron@2549: } else { peter1138@4898: SetDParam(5, CT_INVALID); tron@1998: } peter1138@3401: DrawStringMultiCenter(x, y, STR_VEHICLE_INFO_COST_WEIGHT_SPEED_POWER, maxw); tron@1998: } tron@1998: tron@2477: static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw) tron@1998: { tron@1998: const AircraftVehicleInfo *avi = AircraftVehInfo(engine); peter1138@9923: SetDParam(0, (_price.aircraft_base >> 3) * GetEngineProperty(engine, 0x0B, avi->cost_factor) >> 5); celestar@6321: SetDParam(1, avi->max_speed * 10 / 16); tron@1998: SetDParam(2, avi->passenger_capacity); tron@1998: SetDParam(3, avi->mail_capacity); peter1138@9567: SetDParam(4, GetEngineProperty(engine, 0x0E, avi->running_cost) * _price.aircraft_running >> 8); tron@1998: tron@1998: DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw); tron@1998: } tron@1998: tron@2477: static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw) tron@1998: { tron@1998: const RoadVehicleInfo *rvi = RoadVehInfo(engine); tron@1998: peter1138@9923: SetDParam(0, (_price.roadveh_base >> 3) * GetEngineProperty(engine, 0x11, rvi->cost_factor) >> 5); peter1138@5931: SetDParam(1, rvi->max_speed * 10 / 32); peter1138@8626: SetDParam(2, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) >> 8); peter1138@4896: SetDParam(3, rvi->cargo_type); peter1138@9567: SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD)); tron@1998: tron@1998: DrawStringMultiCenter(x, y, STR_902A_COST_SPEED_RUNNING_COST, maxw); tron@1998: } tron@1998: tron@2477: static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw) tron@1998: { tron@1998: const ShipVehicleInfo *svi = ShipVehInfo(engine); peter1138@9923: SetDParam(0, GetEngineProperty(engine, 0x0A, svi->cost_factor) * (_price.ship_base >> 3) >> 5); peter1138@9567: SetDParam(1, GetEngineProperty(engine, 0x0B, svi->max_speed) * 10 / 32); peter1138@4896: SetDParam(2, svi->cargo_type); peter1138@9567: SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity)); peter1138@9567: SetDParam(4, GetEngineProperty(engine, 0x0F, svi->running_cost) * _price.ship_running >> 8); tron@1998: DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw); tron@1998: } tron@1998: rubidium@8763: void DrawNewsNewVehicleAvail(Window *w, const NewsItem *ni) rubidium@6209: { rubidium@9199: EngineID engine = ni->data_a; rubidium@6209: const DrawEngineInfo *dei = &_draw_engine_list[GetEngine(engine)->type]; truelight@193: rubidium@6209: SetDParam(0, GetEngineCategoryName(engine)); rubidium@6209: DrawStringMultiCenter(w->width >> 1, 20, STR_NEW_VEHICLE_NOW_AVAILABLE, w->width - 2); rubidium@6209: truelight@0: GfxFillRect(25, 56, w->width - 25, w->height - 2, 10); truelight@0: peter1138@7059: SetDParam(0, engine); rubidium@6209: DrawStringMultiCenter(w->width >> 1, 57, STR_NEW_VEHICLE_TYPE, w->width - 2); truelight@0: rubidium@6209: dei->engine_proc(w->width >> 1, 88, engine, 0); frosch@9607: GfxFillRect(25, 56, w->width - 56, 112, PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOR); rubidium@6209: dei->info_proc(engine, w->width >> 1, 129, w->width - 52); truelight@0: } peter1138@9380: peter1138@9380: peter1138@9380: /** Sort all items using qsort() and given 'CompareItems' function peter1138@9380: * @param el list to be sorted peter1138@9380: * @param compare function for evaluation of the quicksort peter1138@9380: */ peter1138@9403: void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare) peter1138@9380: { peter1138@9403: uint size = el->Length(); peter1138@9380: /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems) peter1138@9380: * generally, do not sort if there are less than 2 items */ peter1138@9380: if (size < 2) return; peter1138@9403: qsort(el->Begin(), size, sizeof(*el->Begin()), compare); // MorphOS doesn't know vector::at(int) ... peter1138@9380: } peter1138@9380: peter1138@9380: /** Sort selected range of items (on indices @ ) peter1138@9380: * @param el list to be sorted peter1138@9380: * @param compare function for evaluation of the quicksort peter1138@9380: * @param begin start of sorting peter1138@9380: * @param num_items count of items to be sorted peter1138@9380: */ peter1138@9403: void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items) peter1138@9380: { peter1138@9404: if (num_items < 2) return; peter1138@9403: assert(begin < el->Length()); peter1138@9403: assert(begin + num_items <= el->Length()); peter1138@9403: qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare); peter1138@9380: } peter1138@9380: