/* $Id: copy_paste_gui.c 7150 2006-11-14 20:49:22Z glx $ */ #include "stdafx.h" #include "openttd.h" #include "table/sprites.h" #include "table/strings.h" #include "functions.h" #include "player.h" #include "tile.h" #include "window.h" #include "gui.h" #include "viewport.h" #include "gfx.h" #include "sound.h" #include "variables.h" #include "copy_paste.h" /* GUI Settings */ static RailType _last_railtype = 0; static bool _paste_vacant_terrain = false; static bool _copy_with_rail = true; static bool _copy_with_road = true; static bool _copy_with_other = false; static bool _clear_before_build = true; static bool _toggle_signal_direction = false; static bool _convert_railtype = true; /** * GUI Code for Copy&Paste **/ typedef void OnButtonClick(Window *w); void PlaceProc_Copy(TileIndex tile) { VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED | GUI_PlaceProc_Copy); VpSetPlaceSizingLimit(63); } void PlaceProc_Paste(TileIndex tile) { SafePasteArea(tile, _paste_vacant_terrain, _convert_railtype, _clear_before_build, _toggle_signal_direction); } static void CopyPasteClick_Copy(Window *w) { HandlePlacePushButton(w, 4, SPR_CURSOR_COPY, VHM_RECT, PlaceProc_Copy); } static void CopyPasteClick_Paste(Window *w) { HandlePlacePushButton(w, 5, SPR_CURSOR_PASTE, VHM_PREVIEW, PlaceProc_Paste); } static void CopyPasteClick_CopyVacantTerrain(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 6); _paste_vacant_terrain = IsWindowWidgetLowered(w, 6); } static void CopyPasteClick_RotateLeft(Window *w) { HandleButtonClick(w, 7); SndPlayFx(SND_15_BEEP); RotateSelectionCCW(); } static void CopyPasteClick_RotateRight(Window *w) { HandleButtonClick(w, 8); SndPlayFx(SND_15_BEEP); RotateSelectionCW(); } static void CopyPasteClick_Save(Window *w) { HandleButtonClick(w, 9); SndPlayFx(SND_15_BEEP); ShowSaveLoadDialog(SLD_SAVE_TEMPLATE); } static void CopyPasteClick_Load(Window *w) { HandleButtonClick(w, 10); SndPlayFx(SND_15_BEEP); ShowSaveLoadDialog(SLD_LOAD_TEMPLATE); } static void CopyPasteClick_ToggleSignalDirection(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 14); _toggle_signal_direction = IsWindowWidgetLowered(w, 14); } static void CopyPasteClick_MirrorHorzizontal(Window *w) { HandleButtonClick(w, 11); MirrorSelectionHorizontal(); //Auto Toggle SignalDir CopyPasteClick_ToggleSignalDirection(w); } static void CopyPasteClick_MirrorVertical(Window *w) { HandleButtonClick(w, 12); MirrorSelectionVertical(); //Auto Toggle SignalDir CopyPasteClick_ToggleSignalDirection(w); } static void CopyPasteClick_BulldozeBeforeBuild(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 13); _clear_before_build = IsWindowWidgetLowered(w, 13); } static void CopyPasteClick_ConvertRailtype(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 15); _convert_railtype = IsWindowWidgetLowered(w, 15); } static void CopyPasteClick_WithoutRail(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 16); _copy_with_rail = IsWindowWidgetLowered(w, 16); } static void CopyPasteClick_WithoutRoad(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 17); _copy_with_road = IsWindowWidgetLowered(w, 17); } static void CopyPasteClick_OnlyOwn(Window *w) { SetWindowDirty(w); SndPlayFx(SND_15_BEEP); ToggleWidgetLoweredState(w, 18); _copy_with_other = IsWindowWidgetLowered(w, 18); } static OnButtonClick * const _copy_paste_button_proc[] = { CopyPasteClick_Copy, CopyPasteClick_Paste, CopyPasteClick_CopyVacantTerrain, CopyPasteClick_RotateLeft, CopyPasteClick_RotateRight, CopyPasteClick_Save, CopyPasteClick_Load, CopyPasteClick_MirrorHorzizontal, CopyPasteClick_MirrorVertical, CopyPasteClick_BulldozeBeforeBuild, CopyPasteClick_ToggleSignalDirection, CopyPasteClick_ConvertRailtype, CopyPasteClick_WithoutRail, CopyPasteClick_WithoutRoad, CopyPasteClick_OnlyOwn }; static void CopyPasteWndProc(Window *w, WindowEvent *e) { switch (e->event) { case WE_CREATE: if (!IsSomethingCopied()) { DisableWindowWidget(w, 5); DisableWindowWidget(w, 7); DisableWindowWidget(w, 8); DisableWindowWidget(w, 9); DisableWindowWidget(w, 11); DisableWindowWidget(w, 12); } SetWindowWidgetLoweredState(w, 6, _paste_vacant_terrain); SetWindowWidgetLoweredState(w, 13, _clear_before_build); SetWindowWidgetLoweredState(w, 14, _toggle_signal_direction); SetWindowWidgetLoweredState(w, 15, _convert_railtype); SetWindowWidgetLoweredState(w, 16, _copy_with_rail); SetWindowWidgetLoweredState(w, 17, _copy_with_road); SetWindowWidgetLoweredState(w, 18, _copy_with_other); break; case WE_PAINT: DrawWindowWidgets(w); //Draw CylinderGuy in CompanyColor, if unclicked if (!IsWindowWidgetLowered(w, 18)) DrawSprite(SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_current_player) + SPR_IMG_ONLY_OWN), w->widget[18].left + 1, w->widget[18].top + 1); //0x2EB //DrawSprite(SPR_ARROW_LEFT, 34, 93); //DrawSprite(SPR_ARROW_RIGHT, 102, 93); break; case WE_CLICK: if ((e->we.click.widget >= 4) && (e->we.click.widget <= 18)) { _copy_paste_button_proc[e->we.click.widget - 4](w); /* Set Selection size to Copy size */ if (IsWindowWidgetLowered(w, 5)) { if (_copy_width > 0) SetTileSelectSize(_copy_width - 1, _copy_height - 1); } else SetTileSelectSize(1, 1); } break; case WE_MOUSELOOP: if (WP(w,def_d).close) DeleteWindow(w); return; case WE_DESTROY: if (!WP(w,def_d).close) ResetObjectToPlace(); break; case WE_PLACE_OBJ: _place_proc(e->we.place.tile); return; case WE_PLACE_DRAG: VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.userdata & 0xF); break; case WE_PLACE_MOUSEUP: if (e->we.click.pt.x != -1) { if ((e->we.place.userdata & 0xF) == VPM_X_AND_Y) // dragged actions if (GUIPlaceProcDragXY(e)) break; if ((e->we.place.userdata >> 4) == GUI_PlaceProc_Copy >> 4) { bool copy_made; CopyArea(e->we.place.tile, e->we.place.starttile, _copy_with_rail, _copy_with_road, _copy_with_other); //Enable Buttons after Copy copy_made = (_copy_width == 0) || (_copy_height == 0); SetWindowWidgetDisabledState(w, 5, copy_made); SetWindowWidgetDisabledState(w, 7, copy_made); SetWindowWidgetDisabledState(w, 8, copy_made); SetWindowWidgetDisabledState(w, 9, copy_made); SetWindowWidgetDisabledState(w, 11, copy_made); SetWindowWidgetDisabledState(w, 12, copy_made); SetWindowDirty(w); } } break; case WE_ABORT_PLACE_OBJ: //UnclickWindowButtons 4 + 5 RaiseWindowWidget(w, 4); RaiseWindowWidget(w, 5); SetWindowDirty(w); break; case WE_TIMEOUT: { int i; for (i = 7; i < 13; i++) RaiseWindowWidget(w, i); } break; case WE_TICK: //Enable buttons, after load if (IsSomethingCopied()) { if (IsWindowWidgetDisabled(w, 5)) { EnableWindowWidget(w, 5); EnableWindowWidget(w, 7); EnableWindowWidget(w, 8); EnableWindowWidget(w, 9); EnableWindowWidget(w, 11); EnableWindowWidget(w, 12); SetWindowDirty(w); } } else { if (!IsWindowWidgetDisabled(w, 5)) { DisableWindowWidget(w, 5); DisableWindowWidget(w, 7); DisableWindowWidget(w, 8); DisableWindowWidget(w, 9); DisableWindowWidget(w, 11); DisableWindowWidget(w, 12); SetWindowDirty(w); } } if (_last_railtype != _cur_railtype) { const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype); _last_railtype = _cur_railtype; w->widget[15].data = rti->gui_sprites.convert_rail; SetWindowDirty(w); } break; } } static const Widget _copy_paste_widgets[] = { /* 0 */{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, /* 1 */{ WWT_CAPTION, RESIZE_NONE, 7, 11, 329, 0, 13, STR_COPY_PASTE_TOOLBAR, STR_018C_WINDOW_TITLE_DRAG_THIS}, /* 2 */{ WWT_STICKYBOX, RESIZE_NONE, 7, 330, 341, 0, 13, STR_NULL, STR_STICKY_BUTTON}, /* 3 */{ WWT_PANEL, RESIZE_NONE, 7, 92, 95, 14, 35, STR_NULL, STR_NULL}, /* 4 */{ WWT_IMGBTN, RESIZE_NONE, 7, 0, 21, 14, 35, SPR_IMG_COPY, STR_COPY_PASTE_COPY_TOOLTIP}, /* 5 */{ WWT_IMGBTN, RESIZE_NONE, 7, 22, 43, 14, 35, SPR_IMG_PASTE, STR_COPY_PASTE_PASTE_TOOLTIP}, /* 6 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 188, 209, 14, 35, SPR_IMG_MINUS_TERRAIN, STR_COPY_PASTE_VACANT_TERRAIN_TOOLTIP}, /* 7 */{ WWT_IMGBTN, RESIZE_NONE, 7, 96, 117, 14, 35, SPR_IMG_ROTATE_LEFT, STR_COPY_PASTE_ROTATE_LEFT_TOOLTIP}, /* 8 */{ WWT_IMGBTN, RESIZE_NONE, 7, 118, 139, 14, 35, SPR_IMG_ROTATE_RIGHT, STR_COPY_PASTE_ROTATE_RIGHT_TOOLTIP}, /* 9 */{ WWT_IMGBTN, RESIZE_NONE, 7, 70, 91, 14, 35, SPR_IMG_SAVE, STR_COPY_PASTE_SAVE_TOOLTIP}, /* 10 */{ WWT_IMGBTN, RESIZE_NONE, 7, 48, 69, 14, 35, SPR_IMG_LOAD, STR_COPY_PASTE_LOAD_TOOLTIP}, /* 11 */{ WWT_IMGBTN, RESIZE_NONE, 7, 140, 161, 14, 35, SPR_IMG_MIRROR_HORIZONTAL, STR_COPY_PASTE_MIRROR_HORIZONTAL_TOOLTIP}, /* 12 */{ WWT_IMGBTN, RESIZE_NONE, 7, 162, 183, 14, 35, SPR_IMG_MIRROR_VERTICAL, STR_COPY_PASTE_MIRROR_VERTICAL_TOOLTIP}, /* 13 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 210, 231, 14, 35, SPR_IMG_MINUS_DYNAMITE, STR_COPY_PASTE_BULLDOZE_BEFORE_BUILD_TOOLTIP}, /* 14 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 276, 297, 14, 35, SPR_IMG_MINUS_SIGNAL, STR_COPY_PASTE_TOGGLE_SIGNAL_DIRECTION_TOOLTIP}, /* 15 */{ WWT_IMGBTN, RESIZE_NONE, 7, 298, 319, 14, 35, SPR_IMG_CONVERT_RAIL, STR_COPY_PASTE_CONVERT_RAIL_TOOLTIP}, /* 16 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 232, 253, 14, 35, SPR_IMG_NO_RAIL, STR_COPY_PASTE_WITHOUT_RAIL_TOOLTIP}, /* 17 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 254, 275, 14, 35, SPR_IMG_NO_ROAD, STR_COPY_PASTE_WITHOUT_ROAD_TOOLTIP}, /* 18 */{ WWT_IMGBTN_2, RESIZE_NONE, 7, 320, 341, 14, 35, SPR_IMG_ONLY_OWN, STR_COPY_PASTE_ONLY_OWN_TOOLTIP}, /* 19 */{ WWT_PANEL, RESIZE_NONE, 7, 184, 187, 14, 35, STR_NULL, STR_NULL}, /* 20 */{ WWT_PANEL, RESIZE_NONE, 7, 44, 47, 14, 35, STR_NULL, STR_NULL}, { WIDGETS_END}, }; static const WindowDesc _copy_paste_desc = { WDP_AUTO, WDP_AUTO, 342, 36, WC_COPY_PASTE, WC_SCEN_LAND_GEN, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON, _copy_paste_widgets, CopyPasteWndProc }; void ShowCopyPasteToolbar(int button) { Window *w; // don't recreate the window if we're clicking on a button and the window exists. if (button < 4 || !(w = FindWindowById(WC_COPY_PASTE, 0)) || w->wndproc != CopyPasteWndProc) { DeleteWindowById(WC_COPY_PASTE, 0); w = AllocateWindowDesc(&_copy_paste_desc); } if ((button >= 4) && (w != NULL)) { _copy_paste_button_proc[button - 4](w); /* Set Selection size to Copy size */ if (IsWindowWidgetLowered(w, 5)) { if (_copy_width > 0) SetTileSelectSize(_copy_width - 1, _copy_height - 1); } else SetTileSelectSize(1, 1); } }