tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @file viewport.cpp */ belugas@6443: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" rubidium@8615: #include "tile_cmd.h" tron@2662: #include "gui.h" tron@1349: #include "spritecache.h" maedhros@6949: #include "landscape.h" rubidium@8720: #include "viewport_func.h" truelight@0: #include "station.h" truelight@0: #include "town.h" truelight@988: #include "signs.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" bjarni@2676: #include "train.h" maedhros@7353: #include "roadveh.h" rubidium@7982: #include "vehicle_gui.h" truelight@7433: #include "blitter/factory.hpp" belugas@8345: #include "transparency.h" rubidium@8610: #include "strings_func.h" rubidium@8619: #include "zoom_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8750: #include "player_func.h" rubidium@8766: #include "settings_type.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: truelight@133: #define VIEWPORT_DRAW_MEM (65536 * 2) truelight@0: rubidium@8720: PlaceProc *_place_proc; rubidium@8720: Point _tile_fract_coords; truelight@7120: ZoomLevel _saved_scrollpos_zoom; truelight@7120: rubidium@8564: /** rubidium@8564: * The maximum number of viewports depends on the maximum number rubidium@8564: * of windows. Technically is could be the maximum number of rubidium@8564: * windows, but there is always at least one window that does rubidium@8564: * not need a viewport. Not having 'support' for that viewport rubidium@8564: * saves some time and memory. rubidium@8564: * For the introduction GUI and create game GUIs there is no rubidium@8564: * need for more than one viewport, however in the normal game rubidium@8564: * and scenario editor one can make a lot of viewports. For the rubidium@8564: * normal game one always has a main toolbar and a status bar, rubidium@8564: * however the statusbar does not exist on the scenario editor. rubidium@8564: * rubidium@8564: * This means that we can only safely assume that there is one rubidium@8564: * window without viewport. rubidium@8564: */ rubidium@8564: static ViewPort _viewports[MAX_NUMBER_OF_WINDOWS - 1]; Darkvater@5122: static uint32 _active_viewports; ///< bitmasked variable where each bit signifies if a viewport is in use or not Darkvater@5122: assert_compile(lengthof(_viewports) < sizeof(_active_viewports) * 8); Darkvater@5122: tron@4186: /* The in-game coordiante system looks like this * tron@4186: * * tron@4186: * ^ Z * tron@4186: * | * tron@4186: * | * tron@4186: * | * tron@4186: * | * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * X < > Y * tron@4186: */ tron@4186: rubidium@6574: struct StringSpriteToDraw { truelight@0: uint16 string; truelight@0: uint16 color; rubidium@6574: StringSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; rubidium@7502: uint64 params[2]; truelight@0: uint16 width; rubidium@6574: }; rubidium@6574: rubidium@6574: struct TileSpriteToDraw { peter1138@5919: SpriteID image; peter1138@5919: SpriteID pal; rubidium@8177: const SubSprite *sub; ///< only draw a rectangular part of the sprite rubidium@6574: TileSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; truelight@0: byte z; rubidium@6574: }; rubidium@6574: rubidium@6574: struct ChildScreenSpriteToDraw { peter1138@5919: SpriteID image; peter1138@5919: SpriteID pal; rubidium@8177: const SubSprite *sub; ///< only draw a rectangular part of the sprite tron@849: int32 x; tron@849: int32 y; rubidium@6574: ChildScreenSpriteToDraw *next; rubidium@6574: }; rubidium@6574: rubidium@6574: struct ParentSpriteToDraw { belugas@8065: SpriteID image; ///< sprite to draw belugas@8065: SpriteID pal; ///< palette to use rubidium@8177: const SubSprite *sub; ///< only draw a rectangular part of the sprite belugas@8065: rubidium@8076: int32 x; ///< screen X coordinate of sprite rubidium@8076: int32 y; ///< screen Y coordinate of sprite rubidium@8076: belugas@8065: int32 left; ///< minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites belugas@8065: int32 top; ///< minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites belugas@8065: belugas@8065: int32 xmin; ///< minimal world X coordinate of bounding box belugas@8065: int32 xmax; ///< maximal world X coordinate of bounding box belugas@8065: int32 ymin; ///< minimal world Y coordinate of bounding box belugas@8065: int32 ymax; ///< maximal world Y coordinate of bounding box rubidium@8076: int zmin; ///< minimal world Z coordinate of bounding box rubidium@8076: int zmax; ///< maximal world Z coordinate of bounding box belugas@8065: belugas@8065: ChildScreenSpriteToDraw *child; ///< head of child list; rubidium@8076: bool comparison_done; ///< Used during sprite sorting: true if sprite has been compared with all other sprites rubidium@6574: }; truelight@0: belugas@6919: /* Quick hack to know how much memory to reserve when allocating from the spritelist belugas@6919: * to prevent a buffer overflow. */ ludde@2109: #define LARGEST_SPRITELIST_STRUCT ParentSpriteToDraw rubidium@8076: assert_compile(sizeof(LARGEST_SPRITELIST_STRUCT) >= sizeof(StringSpriteToDraw)); rubidium@8076: assert_compile(sizeof(LARGEST_SPRITELIST_STRUCT) >= sizeof(TileSpriteToDraw)); rubidium@8076: assert_compile(sizeof(LARGEST_SPRITELIST_STRUCT) >= sizeof(ChildScreenSpriteToDraw)); rubidium@8076: assert_compile(sizeof(LARGEST_SPRITELIST_STRUCT) >= sizeof(ParentSpriteToDraw)); ludde@2109: rubidium@8265: /* Enumeration of multi-part foundations */ rubidium@8265: enum FoundationPart { rubidium@8265: FOUNDATION_PART_NONE = 0xFF, ///< Neither foundation nor groundsprite drawn yet. rubidium@8265: FOUNDATION_PART_NORMAL = 0, ///< First part (normal foundation or no foundation) rubidium@8265: FOUNDATION_PART_HALFTILE = 1, ///< Second part (halftile foundation) rubidium@8265: FOUNDATION_PART_END rubidium@8265: }; rubidium@8265: rubidium@6574: struct ViewportDrawer { truelight@0: DrawPixelInfo dpi; truelight@193: tron@2116: byte *spritelist_mem; tron@2116: const byte *eof_spritelist_mem; truelight@193: truelight@0: StringSpriteToDraw **last_string, *first_string; truelight@0: TileSpriteToDraw **last_tile, *first_tile; truelight@0: truelight@0: ChildScreenSpriteToDraw **last_child; truelight@0: truelight@0: ParentSpriteToDraw **parent_list; tron@2116: ParentSpriteToDraw * const *eof_parent_list; truelight@0: truelight@0: byte combine_sprites; truelight@0: rubidium@8265: ParentSpriteToDraw *foundation[FOUNDATION_PART_END]; ///< Foundation sprites. rubidium@8265: FoundationPart foundation_part; ///< Currently active foundation for ground sprite drawing. rubidium@8265: ChildScreenSpriteToDraw **last_foundation_child[FOUNDATION_PART_END]; ///< Tail of ChildSprite list of the foundations. rubidium@8265: Point foundation_offset[FOUNDATION_PART_END]; ///< Pixeloffset for ground sprites on the foundations. rubidium@6574: }; truelight@0: truelight@0: static ViewportDrawer *_cur_vd; truelight@0: tron@1863: TileHighlightData _thd; truelight@0: static TileInfo *_cur_ti; truelight@0: truelight@4340: extern void SmallMapCenterOnCurrentPos(Window *w); truelight@0: tron@2116: static Point MapXYZToViewport(const ViewPort *vp, uint x, uint y, uint z) truelight@0: { truelight@0: Point p = RemapCoords(x, y, z); tron@2116: p.x -= vp->virtual_width / 2; tron@2116: p.y -= vp->virtual_height / 2; truelight@0: return p; truelight@0: } truelight@0: rubidium@7817: void InitViewports() rubidium@7817: { Darkvater@5122: memset(_viewports, 0, sizeof(_viewports)); Darkvater@5122: _active_viewports = 0; Darkvater@5122: } Darkvater@5122: Darkvater@5122: void DeleteWindowViewport(Window *w) Darkvater@5122: { skidd13@8425: ClrBit(_active_viewports, w->viewport - _viewports); Darkvater@5122: w->viewport->width = 0; Darkvater@5122: w->viewport = NULL; Darkvater@5122: } Darkvater@5122: truelight@193: void AssignWindowViewport(Window *w, int x, int y, truelight@7120: int width, int height, uint32 follow_flags, ZoomLevel zoom) truelight@0: { truelight@0: ViewPort *vp; truelight@0: Point pt; truelight@0: uint32 bit; truelight@0: Darkvater@5122: for (vp = _viewports, bit = 0; ; vp++, bit++) { tron@2116: assert(vp != endof(_viewports)); tron@2116: if (vp->width == 0) break; truelight@0: } skidd13@8427: SetBit(_active_viewports, bit); truelight@0: truelight@0: vp->left = x + w->left; truelight@0: vp->top = y + w->top; truelight@0: vp->width = width; truelight@0: vp->height = height; truelight@193: truelight@0: vp->zoom = zoom; truelight@0: truelight@7150: vp->virtual_width = ScaleByZoom(width, zoom); truelight@7150: vp->virtual_height = ScaleByZoom(height, zoom); truelight@0: truelight@0: if (follow_flags & 0x80000000) { tron@2116: const Vehicle *veh; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = (VehicleID)(follow_flags & 0xFFFF); tron@2116: veh = GetVehicle(WP(w, vp_d).follow_vehicle); truelight@0: pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: } else { celestar@3421: uint x = TileX(follow_flags) * TILE_SIZE; celestar@3421: uint y = TileY(follow_flags) * TILE_SIZE; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; tron@2116: pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y)); truelight@0: } truelight@0: tron@2116: WP(w, vp_d).scrollpos_x = pt.x; tron@2116: WP(w, vp_d).scrollpos_y = pt.y; peter1138@7226: WP(w, vp_d).dest_scrollpos_x = pt.x; peter1138@7226: WP(w, vp_d).dest_scrollpos_y = pt.y; peter1138@7226: truelight@0: w->viewport = vp; truelight@0: vp->virtual_left = 0;//pt.x; truelight@0: vp->virtual_top = 0;//pt.y; truelight@0: } truelight@0: truelight@0: static Point _vp_move_offs; truelight@0: Darkvater@5137: static void DoSetViewportPosition(Window* const *wz, int left, int top, int width, int height) truelight@0: { Darkvater@5124: Darkvater@5124: for (; wz != _last_z_window; wz++) { Darkvater@5124: const Window *w = *wz; Darkvater@5124: truelight@0: if (left + width > w->left && tron@2116: w->left + w->width > left && truelight@0: top + height > w->top && tron@2116: w->top + w->height > top) { truelight@193: truelight@0: if (left < w->left) { Darkvater@5124: DoSetViewportPosition(wz, left, top, w->left - left, height); Darkvater@5124: DoSetViewportPosition(wz, left + (w->left - left), top, width - (w->left - left), height); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (left + width > w->left + w->width) { Darkvater@5124: DoSetViewportPosition(wz, left, top, (w->left + w->width - left), height); Darkvater@5124: DoSetViewportPosition(wz, left + (w->left + w->width - left), top, width - (w->left + w->width - left) , height); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (top < w->top) { Darkvater@5124: DoSetViewportPosition(wz, left, top, width, (w->top - top)); Darkvater@5124: DoSetViewportPosition(wz, left, top + (w->top - top), width, height - (w->top - top)); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (top + height > w->top + w->height) { Darkvater@5124: DoSetViewportPosition(wz, left, top, width, (w->top + w->height - top)); Darkvater@5124: DoSetViewportPosition(wz, left, top + (w->top + w->height - top), width , height - (w->top + w->height - top)); truelight@0: return; truelight@0: } truelight@0: truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: { truelight@0: int xo = _vp_move_offs.x; truelight@0: int yo = _vp_move_offs.y; truelight@0: truelight@0: if (abs(xo) >= width || abs(yo) >= height) { truelight@0: /* fully_outside */ tron@2116: RedrawScreenRect(left, top, left + width, top + height); truelight@0: return; truelight@0: } truelight@0: truelight@0: GfxScroll(left, top, width, height, xo, yo); truelight@0: truelight@0: if (xo > 0) { truelight@0: RedrawScreenRect(left, top, xo + left, top + height); truelight@0: left += xo; truelight@0: width -= xo; truelight@0: } else if (xo < 0) { truelight@0: RedrawScreenRect(left+width+xo, top, left+width, top + height); truelight@0: width += xo; truelight@0: } truelight@0: truelight@0: if (yo > 0) { truelight@0: RedrawScreenRect(left, top, width+left, top + yo); truelight@0: } else if (yo < 0) { truelight@0: RedrawScreenRect(left, top + height + yo, width+left, top + height); truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@4171: static void SetViewportPosition(Window *w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; truelight@0: int old_left = vp->virtual_left; truelight@0: int old_top = vp->virtual_top; truelight@0: int i; truelight@0: int left, top, width, height; truelight@0: truelight@0: vp->virtual_left = x; truelight@0: vp->virtual_top = y; truelight@0: smatz@8401: /* viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower) smatz@8401: * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL) smatz@8401: */ smatz@8401: old_left = UnScaleByZoomLower(old_left, vp->zoom); smatz@8401: old_top = UnScaleByZoomLower(old_top, vp->zoom); smatz@8401: x = UnScaleByZoomLower(x, vp->zoom); smatz@8401: y = UnScaleByZoomLower(y, vp->zoom); truelight@0: truelight@0: old_left -= x; truelight@0: old_top -= y; truelight@0: tron@2116: if (old_top == 0 && old_left == 0) return; truelight@0: truelight@0: _vp_move_offs.x = old_left; truelight@0: _vp_move_offs.y = old_top; truelight@0: truelight@0: left = vp->left; truelight@0: top = vp->top; truelight@0: width = vp->width; truelight@0: height = vp->height; truelight@0: truelight@0: if (left < 0) { truelight@0: width += left; truelight@0: left = 0; truelight@0: } truelight@0: tron@2116: i = left + width - _screen.width; tron@2116: if (i >= 0) width -= i; truelight@0: truelight@0: if (width > 0) { truelight@0: if (top < 0) { truelight@0: height += top; truelight@0: top = 0; truelight@0: } truelight@193: tron@2116: i = top + height - _screen.height; tron@2116: if (i >= 0) height -= i; truelight@0: Darkvater@5124: if (height > 0) DoSetViewportPosition(FindWindowZPosition(w) + 1, left, top, width, height); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@2116: ViewPort *IsPtInWindowViewport(const Window *w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; tron@2116: truelight@0: if (vp != NULL && belugas@9058: IsInsideMM(x, vp->left, vp->left + vp->width) && skidd13@8450: IsInsideMM(y, vp->top, vp->top + vp->height)) truelight@0: return vp; truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: tron@2116: static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y) tron@1095: { truelight@0: Point pt; truelight@0: int a,b; Darkvater@5014: uint z; truelight@0: truelight@0: if ( (uint)(x -= vp->left) >= (uint)vp->width || truelight@0: (uint)(y -= vp->top) >= (uint)vp->height) { truelight@0: Point pt = {-1, -1}; truelight@0: return pt; truelight@0: } truelight@0: truelight@7122: x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> 2; truelight@7122: y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> 1; truelight@0: truelight@0: a = y-x; truelight@0: b = y+x; truelight@0: Darkvater@5014: /* we need to move variables in to the valid range, as the Darkvater@5014: * GetTileZoomCenterWindow() function can call here with invalid x and/or y, Darkvater@5014: * when the user tries to zoom out along the sides of the map */ skidd13@8418: a = Clamp(a, 0, (int)(MapMaxX() * TILE_SIZE) - 1); skidd13@8418: b = Clamp(b, 0, (int)(MapMaxY() * TILE_SIZE) - 1); Darkvater@5014: rubidium@8260: /* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0. rubidium@8260: * Now find the Z-world coordinate by fix point iteration. rubidium@8260: * This is a bit tricky because the tile height is non-continuous at foundations. rubidium@8260: * The clicked point should be approached from the back, otherwise there are regions that are not clickable. rubidium@8260: * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely) rubidium@8260: * So give it a z-malus of 4 in the first iterations. rubidium@8260: */ rubidium@8260: z = 0; rubidium@8260: for (int i = 0; i < 5; i++) z = GetSlopeZ(a + max(z, 4u) - 4, b + max(z, 4u) - 4) / 2; rubidium@8260: for (uint malus = 3; malus > 0; malus--) z = GetSlopeZ(a + max(z, malus) - malus, b + max(z, malus) - malus) / 2; rubidium@8260: for (int i = 0; i < 5; i++) z = GetSlopeZ(a + z, b + z) / 2; Darkvater@5014: Darkvater@5014: pt.x = a + z; Darkvater@5014: pt.y = b + z; truelight@0: truelight@0: return pt; truelight@0: } truelight@0: darkvater@981: /* When used for zooming, check area below current coordinates (x,y) darkvater@981: * and return the tile of the zoomed out/in position (zoom_x, zoom_y) darkvater@981: * when you just want the tile, make x = zoom_x and y = zoom_y */ darkvater@981: static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y) truelight@193: { truelight@0: Window *w; truelight@0: ViewPort *vp; truelight@0: Point pt; truelight@193: truelight@193: if ( (w = FindWindowFromPt(x, y)) != NULL && truelight@0: (vp = IsPtInWindowViewport(w, x, y)) != NULL) darkvater@981: return TranslateXYToTileCoord(vp, zoom_x, zoom_y); truelight@0: truelight@0: pt.y = pt.x = -1; truelight@0: return pt; truelight@0: } truelight@0: rubidium@6573: Point GetTileBelowCursor() truelight@0: { darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y); truelight@0: } truelight@0: darkvater@152: darkvater@152: Point GetTileZoomCenterWindow(bool in, Window * w) truelight@0: { truelight@0: int x, y; darkvater@152: ViewPort * vp; darkvater@152: darkvater@152: vp = w->viewport; darkvater@152: tron@2026: if (in) { tron@2116: x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2); tron@2116: y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2); tron@2116: } else { darkvater@152: x = vp->width - (_cursor.pos.x - vp->left); darkvater@152: y = vp->height - (_cursor.pos.y - vp->top); truelight@0: } darkvater@981: /* Get the tile below the cursor and center on the zoomed-out center */ darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top); truelight@0: } truelight@0: Darkvater@5045: /** Update the status of the zoom-buttons according to the zoom-level Darkvater@5045: * of the viewport. This will update their status and invalidate accordingly belugas@6939: * @param w Window pointer to the window that has the zoom buttons Darkvater@5045: * @param vp pointer to the viewport whose zoom-level the buttons represent Darkvater@5045: * @param widget_zoom_in widget index for window with zoom-in button Darkvater@5045: * @param widget_zoom_out widget index for window with zoom-out button */ Darkvater@5045: void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out) Darkvater@5045: { rubidium@8493: w->SetWidgetDisabledState(widget_zoom_in, vp->zoom == ZOOM_LVL_MIN); glx@8524: w->InvalidateWidget(widget_zoom_in); Darkvater@5045: rubidium@8493: w->SetWidgetDisabledState(widget_zoom_out, vp->zoom == ZOOM_LVL_MAX); glx@8524: w->InvalidateWidget(widget_zoom_out); Darkvater@5045: } Darkvater@5045: rubidium@8177: /** rubidium@8177: * Draws a ground sprite at a specific world-coordinate. rubidium@8177: * rubidium@8177: * @param image the image to draw. rubidium@8177: * @param pal the provided palette. rubidium@8177: * @param x position x of the sprite. rubidium@8177: * @param y position y of the sprite. rubidium@8177: * @param z position z of the sprite. rubidium@8177: * @param sub Only draw a part of the sprite. rubidium@8177: * rubidium@8177: */ rubidium@8177: void DrawGroundSpriteAt(SpriteID image, SpriteID pal, int32 x, int32 y, byte z, const SubSprite *sub) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: TileSpriteToDraw *ts; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { Darkvater@5568: DEBUG(sprite, 0, "Out of sprite memory"); truelight@0: return; truelight@0: } tron@2116: ts = (TileSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(TileSpriteToDraw); truelight@193: truelight@0: ts->image = image; peter1138@5919: ts->pal = pal; rubidium@8177: ts->sub = sub; truelight@0: ts->next = NULL; truelight@0: ts->x = x; truelight@0: ts->y = y; truelight@0: ts->z = z; truelight@0: *vd->last_tile = ts; truelight@193: vd->last_tile = &ts->next; truelight@0: } truelight@0: rubidium@8177: /** rubidium@8222: * Adds a child sprite to the active foundation. rubidium@8222: * rubidium@8222: * The pixel offset of the sprite relative to the ParentSprite is the sum of the offset passed to OffsetGroundSprite() and extra_offs_?. rubidium@8222: * rubidium@8222: * @param image the image to draw. rubidium@8222: * @param pal the provided palette. rubidium@8222: * @param sub Only draw a part of the sprite. rubidium@8265: * @param foundation_part Foundation part. rubidium@8222: * @param extra_offs_x Pixel X offset for the sprite position. rubidium@8222: * @param extra_offs_y Pixel Y offset for the sprite position. rubidium@8222: */ rubidium@8265: static void AddChildSpriteToFoundation(SpriteID image, SpriteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y) rubidium@8222: { rubidium@8222: ViewportDrawer *vd = _cur_vd; skidd13@8450: assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END)); rubidium@8265: assert(vd->foundation[foundation_part] != NULL); rubidium@8265: Point offs = vd->foundation_offset[foundation_part]; rubidium@8222: rubidium@8222: /* Change the active ChildSprite list to the one of the foundation */ rubidium@8222: ChildScreenSpriteToDraw **old_child = vd->last_child; rubidium@8265: vd->last_child = vd->last_foundation_child[foundation_part]; rubidium@8222: rubidium@8222: AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub); rubidium@8222: rubidium@8222: /* Switch back to last ChildSprite list */ rubidium@8222: vd->last_child = old_child; rubidium@8222: } rubidium@8222: rubidium@8222: /** rubidium@8177: * Draws a ground sprite for the current tile. rubidium@8177: * If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite. rubidium@8177: * rubidium@8177: * @param image the image to draw. rubidium@8177: * @param pal the provided palette. rubidium@8177: * @param sub Only draw a part of the sprite. rubidium@8177: */ rubidium@8177: void DrawGroundSprite(SpriteID image, SpriteID pal, const SubSprite *sub) truelight@0: { rubidium@8222: ViewportDrawer *vd = _cur_vd; rubidium@8265: /* Switch to first foundation part, if no foundation was drawn */ rubidium@8265: if (vd->foundation_part == FOUNDATION_PART_NONE) vd->foundation_part = FOUNDATION_PART_NORMAL; rubidium@8265: rubidium@8265: if (vd->foundation[vd->foundation_part] != NULL) { rubidium@8265: AddChildSpriteToFoundation(image, pal, sub, vd->foundation_part, 0, 0); truelight@0: } else { rubidium@8177: DrawGroundSpriteAt(image, pal, _cur_ti->x, _cur_ti->y, _cur_ti->z, sub); truelight@0: } truelight@0: } truelight@0: truelight@0: rubidium@8222: /** rubidium@8222: * Called when a foundation has been drawn for the current tile. rubidium@8222: * Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites. rubidium@8222: * rubidium@8222: * @param x sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite. rubidium@8222: * @param y sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite. rubidium@8222: */ truelight@0: void OffsetGroundSprite(int x, int y) truelight@0: { rubidium@8222: ViewportDrawer *vd = _cur_vd; rubidium@8265: /* Switch to next foundation part */ rubidium@8265: switch (vd->foundation_part) { rubidium@8265: case FOUNDATION_PART_NONE: rubidium@8265: vd->foundation_part = FOUNDATION_PART_NORMAL; rubidium@8265: break; rubidium@8265: case FOUNDATION_PART_NORMAL: rubidium@8265: vd->foundation_part = FOUNDATION_PART_HALFTILE; rubidium@8265: break; rubidium@8265: default: NOT_REACHED(); rubidium@8265: } rubidium@8222: rubidium@8222: /* vd->last_child == NULL if foundation sprite was clipped by the viewport bounds */ rubidium@8265: if (vd->last_child != NULL) vd->foundation[vd->foundation_part] = vd->parent_list[-1]; rubidium@8265: rubidium@8265: vd->foundation_offset[vd->foundation_part].x = x; rubidium@8265: vd->foundation_offset[vd->foundation_part].y = y; rubidium@8265: vd->last_foundation_child[vd->foundation_part] = vd->last_child; truelight@0: } truelight@0: rubidium@8177: /** rubidium@8177: * Adds a child sprite to a parent sprite. rubidium@8177: * In contrast to "AddChildSpriteScreen()" the sprite position is in world coordinates rubidium@8177: * rubidium@8177: * @param image the image to draw. rubidium@8177: * @param pal the provided palette. rubidium@8177: * @param x position x of the sprite. rubidium@8177: * @param y position y of the sprite. rubidium@8177: * @param z position z of the sprite. rubidium@8177: * @param sub Only draw a part of the sprite. rubidium@8177: */ rubidium@8177: static void AddCombinedSprite(SpriteID image, SpriteID pal, int x, int y, byte z, const SubSprite *sub) truelight@0: { tron@2116: const ViewportDrawer *vd = _cur_vd; truelight@0: Point pt = RemapCoords(x, y, z); tron@2319: const Sprite* spr = GetSprite(image & SPRITE_MASK); truelight@0: tron@2319: if (pt.x + spr->x_offs >= vd->dpi.left + vd->dpi.width || tron@2319: pt.x + spr->x_offs + spr->width <= vd->dpi.left || tron@2319: pt.y + spr->y_offs >= vd->dpi.top + vd->dpi.height || tron@2319: pt.y + spr->y_offs + spr->height <= vd->dpi.top) truelight@0: return; truelight@0: rubidium@8177: AddChildSpriteScreen(image, pal, pt.x - vd->parent_list[-1]->left, pt.y - vd->parent_list[-1]->top, false, sub); truelight@0: } truelight@0: rubidium@8076: /** Draw a (transparent) sprite at given coordinates with a given bounding box. rubidium@8076: * The bounding box extends from (x + bb_offset_x, y + bb_offset_y, z + bb_offset_z) to (x + w - 1, y + h - 1, z + dz - 1), both corners included. rubidium@8113: * Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices. rubidium@8076: * rubidium@8076: * @note Bounding boxes are normally specified with bb_offset_x = bb_offset_y = bb_offset_z = 0. The extent of the bounding box in negative direction is rubidium@8076: * defined by the sprite offset in the grf file. rubidium@8076: * However if modifying the sprite offsets is not suitable (e.g. when using existing graphics), the bounding box can be tuned by bb_offset. rubidium@8076: * rubidium@8113: * @pre w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored. rubidium@8076: * rubidium@7829: * @param image the image to combine and draw, rubidium@7829: * @param pal the provided palette, rubidium@8076: * @param x position X (world) of the sprite, rubidium@8076: * @param y position Y (world) of the sprite, rubidium@8076: * @param w bounding box extent towards positive X (world), rubidium@8076: * @param h bounding box extent towards positive Y (world), rubidium@8076: * @param dz bounding box extent towards positive Z (world), rubidium@8076: * @param z position Z (world) of the sprite, rubidium@8076: * @param transparent if true, switch the palette between the provided palette and the transparent palette, rubidium@8076: * @param bb_offset_x bounding box extent towards negative X (world), rubidium@8076: * @param bb_offset_y bounding box extent towards negative Y (world), rubidium@8076: * @param bb_offset_z bounding box extent towards negative Z (world) rubidium@8177: * @param sub Only draw a part of the sprite. rubidium@7829: */ rubidium@8177: void AddSortableSpriteToDraw(SpriteID image, SpriteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ParentSpriteToDraw *ps; truelight@0: Point pt; rubidium@8139: int32 left, right, top, bottom; truelight@0: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: rubidium@7829: /* make the sprites transparent with the right palette */ rubidium@7829: if (transparent) { skidd13@8427: SetBit(image, PALETTE_MODIFIER_TRANSPARENT); rubidium@7829: pal = PALETTE_TO_TRANSPARENT; rubidium@7829: } rubidium@7829: truelight@0: if (vd->combine_sprites == 2) { rubidium@8177: AddCombinedSprite(image, pal, x, y, z, sub); truelight@0: return; truelight@0: } truelight@0: truelight@0: vd->last_child = NULL; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { Darkvater@5568: DEBUG(sprite, 0, "Out of sprite memory"); truelight@0: return; truelight@0: } tron@2116: ps = (ParentSpriteToDraw*)vd->spritelist_mem; tron@2116: truelight@133: if (vd->parent_list >= vd->eof_parent_list) { belugas@6919: /* This can happen rarely, mostly when you zoom out completely belugas@6919: * and have a lot of stuff that moves (and is added to the belugas@6919: * sort-list, this function). To solve it, increase belugas@6919: * parent_list somewhere below to a higher number. belugas@6919: * This can not really hurt you, it just gives some black belugas@6919: * spots on the screen ;) */ Darkvater@5568: DEBUG(sprite, 0, "Out of sprite memory (parent_list)"); truelight@133: return; truelight@193: } truelight@193: tron@4189: pt = RemapCoords(x, y, z); rubidium@8076: ps->x = pt.x; rubidium@8076: ps->y = pt.y; rubidium@8139: rubidium@8139: /* Compute screen extents of sprite */ rubidium@8097: if (image == SPR_EMPTY_BOUNDING_BOX) { rubidium@8139: left = ps->left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x; rubidium@8139: right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1; rubidium@8139: top = ps->top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y; rubidium@8139: bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1; rubidium@8097: } else { rubidium@8097: const Sprite *spr = GetSprite(image & SPRITE_MASK); rubidium@8139: left = ps->left = (pt.x += spr->x_offs); rubidium@8139: right = (pt.x + spr->width ); rubidium@8139: top = ps->top = (pt.y += spr->y_offs); rubidium@8139: bottom = (pt.y + spr->height); rubidium@8097: } belugas@9058: rubidium@8139: if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) { rubidium@8139: /* Compute maximal extents of sprite and it's bounding box */ rubidium@8139: left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x); rubidium@8139: right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1); rubidium@8139: top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y); rubidium@8139: bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1); rubidium@8139: } belugas@9058: rubidium@8139: /* Do not add the sprite to the viewport, if it is outside */ rubidium@8139: if (left >= vd->dpi.left + vd->dpi.width || belugas@9058: right <= vd->dpi.left || rubidium@8139: top >= vd->dpi.top + vd->dpi.height || rubidium@8139: bottom <= vd->dpi.top) { tron@4189: return; tron@4189: } tron@4189: truelight@193: vd->spritelist_mem += sizeof(ParentSpriteToDraw); truelight@0: truelight@0: ps->image = image; peter1138@5919: ps->pal = pal; rubidium@8177: ps->sub = sub; rubidium@8076: ps->xmin = x + bb_offset_x; rubidium@8113: ps->xmax = x + max(bb_offset_x, w) - 1; rubidium@8076: rubidium@8076: ps->ymin = y + bb_offset_y; rubidium@8113: ps->ymax = y + max(bb_offset_y, h) - 1; rubidium@8076: rubidium@8076: ps->zmin = z + bb_offset_z; rubidium@8113: ps->zmax = z + max(bb_offset_z, dz) - 1; rubidium@8076: rubidium@8076: ps->comparison_done = false; truelight@0: ps->child = NULL; truelight@0: vd->last_child = &ps->child; truelight@0: truelight@0: *vd->parent_list++ = ps; truelight@0: tron@2116: if (vd->combine_sprites == 1) vd->combine_sprites = 2; truelight@0: } truelight@0: rubidium@6573: void StartSpriteCombine() truelight@0: { truelight@0: _cur_vd->combine_sprites = 1; truelight@0: } truelight@0: rubidium@6573: void EndSpriteCombine() truelight@0: { truelight@0: _cur_vd->combine_sprites = 0; truelight@0: } truelight@0: rubidium@8177: /** rubidium@8177: * Add a child sprite to a parent sprite. rubidium@8177: * rubidium@8177: * @param image the image to draw. rubidium@8177: * @param pal the provided palette. rubidium@8177: * @param x sprite x-offset (screen coordinates) relative to parent sprite. rubidium@8177: * @param y sprite y-offset (screen coordinates) relative to parent sprite. rubidium@8177: * @param transparent if true, switch the palette between the provided palette and the transparent palette, rubidium@8177: * @param sub Only draw a part of the sprite. rubidium@8177: */ rubidium@8177: void AddChildSpriteScreen(SpriteID image, SpriteID pal, int x, int y, bool transparent, const SubSprite *sub) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ChildScreenSpriteToDraw *cs; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: rubidium@8155: /* make the sprites transparent with the right palette */ rubidium@8155: if (transparent) { skidd13@8427: SetBit(image, PALETTE_MODIFIER_TRANSPARENT); rubidium@8155: pal = PALETTE_TO_TRANSPARENT; rubidium@8155: } rubidium@8155: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { Darkvater@5568: DEBUG(sprite, 0, "Out of sprite memory"); truelight@0: return; truelight@0: } tron@2116: cs = (ChildScreenSpriteToDraw*)vd->spritelist_mem; truelight@0: rubidium@8139: /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */ tron@2116: if (vd->last_child == NULL) return; truelight@193: truelight@193: vd->spritelist_mem += sizeof(ChildScreenSpriteToDraw); truelight@0: rubidium@8222: /* Append the sprite to the active ChildSprite list. rubidium@8222: * If the active ParentSprite is a foundation, update last_foundation_child as well. */ truelight@0: *vd->last_child = cs; rubidium@8265: if (vd->last_foundation_child[0] == vd->last_child) vd->last_foundation_child[0] = &cs->next; rubidium@8265: if (vd->last_foundation_child[1] == vd->last_child) vd->last_foundation_child[1] = &cs->next; truelight@0: vd->last_child = &cs->next; truelight@0: truelight@0: cs->image = image; peter1138@5919: cs->pal = pal; rubidium@8177: cs->sub = sub; truelight@0: cs->x = x; truelight@0: cs->y = y; truelight@0: cs->next = NULL; truelight@0: } truelight@0: truelight@0: /* Returns a StringSpriteToDraw */ rubidium@7502: void *AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: StringSpriteToDraw *ss; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { Darkvater@5568: DEBUG(sprite, 0, "Out of sprite memory"); truelight@0: return NULL; truelight@0: } tron@2116: ss = (StringSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(StringSpriteToDraw); truelight@0: truelight@0: ss->string = string; truelight@0: ss->next = NULL; truelight@0: ss->x = x; truelight@0: ss->y = y; truelight@0: ss->params[0] = params_1; truelight@0: ss->params[1] = params_2; truelight@0: ss->width = 0; truelight@0: truelight@0: *vd->last_string = ss; truelight@0: vd->last_string = &ss->next; truelight@0: truelight@0: return ss; truelight@0: } truelight@0: tron@4000: rubidium@8222: /** rubidium@8222: * Draws sprites between ground sprite and everything above. rubidium@8222: * rubidium@8222: * The sprite is either drawn as TileSprite or as ChildSprite of the active foundation. rubidium@8222: * rubidium@8222: * @param image the image to draw. rubidium@8222: * @param pal the provided palette. rubidium@8222: * @param ti TileInfo Tile that is being drawn rubidium@8222: * @param z_offset Z offset relative to the groundsprite. Only used for the sprite position, not for sprite sorting. rubidium@8265: * @param foundation_part Foundation part the sprite belongs to. rubidium@8222: */ rubidium@8265: static void DrawSelectionSprite(SpriteID image, SpriteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part) truelight@0: { rubidium@8222: /* FIXME: This is not totally valid for some autorail highlights, that extent over the edges of the tile. */ rubidium@8265: if (_cur_vd->foundation[foundation_part] == NULL) { rubidium@8222: /* draw on real ground */ rubidium@8222: DrawGroundSpriteAt(image, pal, ti->x, ti->y, ti->z + z_offset); rubidium@8222: } else { rubidium@8222: /* draw on top of foundation */ rubidium@8265: AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset); dominik@1083: } truelight@0: } truelight@0: rubidium@8175: /** rubidium@8175: * Draws a selection rectangle on a tile. rubidium@8175: * rubidium@8175: * @param ti TileInfo Tile that is being drawn rubidium@8175: * @param pal Palette to apply. rubidium@8175: */ rubidium@8175: static void DrawTileSelectionRect(const TileInfo *ti, SpriteID pal) rubidium@8175: { rubidium@8265: SpriteID sel; rubidium@8265: if (IsHalftileSlope(ti->tileh)) { rubidium@8265: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@8265: SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner; rubidium@8265: DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE); rubidium@8265: rubidium@8265: Corner opposite_corner = OppositeCorner(halftile_corner); rubidium@8265: if (IsSteepSlope(ti->tileh)) { rubidium@8265: sel = SPR_HALFTILE_SELECTION_DOWN; rubidium@8265: } else { rubidium@8265: sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT); rubidium@8265: } rubidium@8265: sel += opposite_corner; rubidium@8265: } else { rubidium@8265: sel = SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]; rubidium@8265: } rubidium@8265: DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL); rubidium@8175: } rubidium@8175: truelight@0: static bool IsPartOfAutoLine(int px, int py) truelight@0: { tron@1863: px -= _thd.selstart.x; tron@1863: py -= _thd.selstart.y; truelight@0: rubidium@8720: if ((_thd.drawstyle & ~HT_DIR_MASK) != HT_LINE) return false; rubidium@8720: rubidium@8720: switch (_thd.drawstyle & HT_DIR_MASK) { rubidium@8720: case HT_DIR_X: return py == 0; // x direction rubidium@8720: case HT_DIR_Y: return px == 0; // y direction rubidium@8720: case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper rubidium@8720: case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower rubidium@8720: case HT_DIR_VL: return px == py || px == py + 16; // vertival left rubidium@8720: case HT_DIR_VR: return px == py || px == py - 16; // vertical right rubidium@8720: default: rubidium@8720: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: dominik@1070: // [direction][side] rubidium@8720: static const HighLightStyle _autorail_type[6][2] = { dominik@1070: { HT_DIR_X, HT_DIR_X }, dominik@1070: { HT_DIR_Y, HT_DIR_Y }, dominik@1070: { HT_DIR_HU, HT_DIR_HL }, dominik@1070: { HT_DIR_HL, HT_DIR_HU }, dominik@1070: { HT_DIR_VL, HT_DIR_VR }, dominik@1070: { HT_DIR_VR, HT_DIR_VL } dominik@1070: }; dominik@1070: dominik@1070: #include "table/autorail.h" dominik@1070: belugas@7574: /** rubidium@8175: * Draws autorail highlights. rubidium@8175: * rubidium@8175: * @param *ti TileInfo Tile that is being drawn rubidium@8175: * @param autorail_type Offset into _AutorailTilehSprite[][] rubidium@8175: */ rubidium@8175: static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type) rubidium@8175: { rubidium@8175: SpriteID image; rubidium@8175: SpriteID pal; rubidium@8175: int offset; rubidium@8175: rubidium@8265: FoundationPart foundation_part = FOUNDATION_PART_NORMAL; frosch@8909: Slope autorail_tileh = RemoveHalftileSlope(ti->tileh); rubidium@8265: if (IsHalftileSlope(ti->tileh)) { rubidium@8265: static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U }; rubidium@8265: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@8265: if (autorail_type != _lower_rail[halftile_corner]) { rubidium@8265: foundation_part = FOUNDATION_PART_HALFTILE; rubidium@8265: /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */ rubidium@8265: autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner)); rubidium@8265: } rubidium@8265: } rubidium@8265: rubidium@8265: offset = _AutorailTilehSprite[autorail_tileh][autorail_type]; rubidium@8175: if (offset >= 0) { rubidium@8175: image = SPR_AUTORAIL_BASE + offset; rubidium@8175: pal = PAL_NONE; rubidium@8175: } else { rubidium@8175: image = SPR_AUTORAIL_BASE - offset; rubidium@8175: pal = PALETTE_SEL_TILE_RED; rubidium@8175: } rubidium@8175: rubidium@8265: DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part); rubidium@8175: } rubidium@8175: rubidium@8175: /** belugas@7574: * Checks if the specified tile is selected and if so draws selection using correct selectionstyle. belugas@7574: * @param *ti TileInfo Tile that is being drawn belugas@7574: */ truelight@0: static void DrawTileSelection(const TileInfo *ti) truelight@0: { belugas@6919: /* Draw a red error square? */ rubidium@9353: bool is_redsq = _thd.redsq != 0 && _thd.redsq == ti->tile; rubidium@9353: if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING); truelight@0: belugas@6919: /* no selection active? */ tron@2116: if (_thd.drawstyle == 0) return; truelight@0: belugas@6919: /* Inside the inner area? */ skidd13@8450: if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) && skidd13@8450: IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) { tron@1863: if (_thd.drawstyle & HT_RECT) { rubidium@9353: if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE); tron@1863: } else if (_thd.drawstyle & HT_POINT) { belugas@6919: /* Figure out the Z coordinate for the single dot. */ rubidium@8222: byte z = 0; rubidium@8265: FoundationPart foundation_part = FOUNDATION_PART_NORMAL; tron@3636: if (ti->tileh & SLOPE_N) { tron@3645: z += TILE_HEIGHT; frosch@8909: if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT; truelight@0: } rubidium@8265: if (IsHalftileSlope(ti->tileh)) { rubidium@8265: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@8265: if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT; rubidium@8265: if (halftile_corner != CORNER_S) { rubidium@8265: foundation_part = FOUNDATION_PART_HALFTILE; rubidium@8265: if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT; rubidium@8265: } rubidium@8265: } rubidium@8265: DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part); tron@2116: } else if (_thd.drawstyle & HT_RAIL /*&& _thd.place_mode == VHM_RAIL*/) { belugas@6919: /* autorail highlight piece under cursor */ tron@2116: uint type = _thd.drawstyle & 0xF; tron@2116: assert(type <= 5); rubidium@8720: DrawAutorailSelection(ti, _autorail_type[type][0]); tron@2116: } else if (IsPartOfAutoLine(ti->x, ti->y)) { belugas@6919: /* autorail highlighting long line */ tron@2116: int dir = _thd.drawstyle & ~0xF0; tron@2116: uint side; dominik@1070: tron@2116: if (dir < 2) { tron@2116: side = 0; tron@2116: } else { tron@2116: TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y); skidd13@8466: side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile))); tron@2116: } tron@1109: rubidium@8720: DrawAutorailSelection(ti, _autorail_type[dir][side]); tron@2116: } truelight@0: return; truelight@0: } truelight@0: belugas@6919: /* Check if it's inside the outer area? */ rubidium@9353: if (!is_redsq && _thd.outersize.x && tron@1863: _thd.size.x < _thd.size.x + _thd.outersize.x && skidd13@8450: IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) && skidd13@8450: IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) { belugas@6919: /* Draw a blue rect. */ rubidium@8175: DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE); truelight@0: return; truelight@0: } truelight@0: } truelight@0: rubidium@6573: static void ViewportAddLandscape() truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: int x, y, width, height; truelight@0: TileInfo ti; truelight@0: bool direction; truelight@0: truelight@0: _cur_ti = &ti; truelight@0: belugas@6919: /* Transform into tile coordinates and round to closest full tile */ truelight@0: x = ((vd->dpi.top >> 1) - (vd->dpi.left >> 2)) & ~0xF; truelight@0: y = ((vd->dpi.top >> 1) + (vd->dpi.left >> 2) - 0x10) & ~0xF; peter1138@5752: belugas@6919: /* determine size of area */ truelight@0: { truelight@0: Point pt = RemapCoords(x, y, 241); truelight@0: width = (vd->dpi.left + vd->dpi.width - pt.x + 95) >> 6; truelight@0: height = (vd->dpi.top + vd->dpi.height - pt.y) >> 5 << 1; truelight@0: } truelight@0: truelight@0: assert(width > 0); truelight@0: assert(height > 0); truelight@193: truelight@0: direction = false; truelight@0: truelight@0: do { truelight@0: int width_cur = width; truelight@0: int x_cur = x; truelight@0: int y_cur = y; truelight@193: truelight@0: do { tron@4238: TileType tt; tron@4238: tron@4238: ti.x = x_cur; tron@4238: ti.y = y_cur; tron@4238: if (0 <= x_cur && x_cur < (int)MapMaxX() * TILE_SIZE && tron@4238: 0 <= y_cur && y_cur < (int)MapMaxY() * TILE_SIZE) { tron@4238: TileIndex tile = TileVirtXY(x_cur, y_cur); tron@4238: tron@4238: ti.tile = tile; tron@4238: ti.tileh = GetTileSlope(tile, &ti.z); tron@4238: tt = GetTileType(tile); tron@4238: } else { tron@4238: ti.tileh = SLOPE_FLAT; tron@4238: ti.tile = 0; tron@4238: ti.z = 0; tron@4238: tt = MP_VOID; tron@4238: } tron@4238: truelight@0: y_cur += 0x10; truelight@0: x_cur -= 0x10; peter1138@5752: rubidium@8265: vd->foundation_part = FOUNDATION_PART_NONE; rubidium@8265: vd->foundation[0] = NULL; rubidium@8265: vd->foundation[1] = NULL; rubidium@8265: vd->last_foundation_child[0] = NULL; rubidium@8265: vd->last_foundation_child[1] = NULL; truelight@193: tron@4238: _tile_type_procs[tt]->draw_tile_proc(&ti); truelight@0: DrawTileSelection(&ti); truelight@0: } while (--width_cur); truelight@193: peter1138@5752: if ((direction ^= 1) != 0) { truelight@0: y += 0x10; peter1138@5752: } else { truelight@0: x += 0x10; peter1138@5752: } truelight@193: } while (--height); truelight@0: } truelight@0: truelight@0: tron@410: static void ViewportAddTownNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: Town *t; truelight@0: int left, top, right, bottom; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: tron@5027: switch (dpi->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && tron@5027: top < t->sign.top + 12 && tron@5027: right > t->sign.left && tron@5027: left < t->sign.left + t->sign.width_1) { tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, tron@5027: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, tron@5027: t->index, t->population); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_2X: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && tron@5027: top < t->sign.top + 24 && tron@5027: right > t->sign.left && tron@5027: left < t->sign.left + t->sign.width_1*2) { tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, tron@5027: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, tron@5027: t->index, t->population); tron@5027: } truelight@193: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: right += ScaleByZoom(1, dpi->zoom); truelight@7149: bottom += ScaleByZoom(1, dpi->zoom) + 1; tron@5027: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && truelight@7149: top < t->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > t->sign.left && truelight@7149: left < t->sign.left + ScaleByZoom(t->sign.width_2, dpi->zoom)) { tron@5027: AddStringToDraw(t->sign.left + 5, t->sign.top + 1, STR_TOWN_LABEL_TINY_BLACK, t->index, 0); tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top - 3, STR_TOWN_LABEL_TINY_WHITE, t->index, 0); tron@5027: } truelight@193: } tron@5027: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddStation(const Station *st, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: rubidium@5838: sstd = (StringSpriteToDraw*)AddStringToDraw(st->sign.left + 1, st->sign.top + 1, str, st->index, st->facilities); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (st->owner == OWNER_NONE || st->facilities == 0) ? 0xE : _player_colors[st->owner]; tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddStationNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: int left, top, right, bottom; tron@5025: const Station *st; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_STATION_NAMES) || _game_mode == GM_MENU) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: tron@5027: switch (dpi->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && tron@5027: top < st->sign.top + 12 && tron@5027: right > st->sign.left && tron@5027: left < st->sign.left + st->sign.width_1) { tron@5027: AddStation(st, STR_305C_0, st->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_2X: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && tron@5027: top < st->sign.top + 24 && tron@5027: right > st->sign.left && tron@5027: left < st->sign.left + st->sign.width_1*2) { tron@5027: AddStation(st, STR_305C_0, st->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: right += ScaleByZoom(1, dpi->zoom); truelight@7149: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@7149: tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && truelight@7149: top < st->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > st->sign.left && truelight@7149: left < st->sign.left + ScaleByZoom(st->sign.width_2, dpi->zoom)) { tron@5027: AddStation(st, STR_STATION_SIGN_TINY, st->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddSign(const Sign *si, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: peter1138@7552: sstd = (StringSpriteToDraw*)AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->index, 0); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (si->owner == OWNER_NONE) ? 14 : _player_colors[si->owner]; tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddSigns(DrawPixelInfo *dpi) truelight@0: { tron@5025: const Sign *si; truelight@0: int left, top, right, bottom; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_SIGNS)) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: tron@5027: switch (dpi->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@5027: FOR_ALL_SIGNS(si) { tron@5027: if (bottom > si->sign.top && tron@5027: top < si->sign.top + 12 && tron@5027: right > si->sign.left && tron@5027: left < si->sign.left + si->sign.width_1) { tron@5027: AddSign(si, STR_2806, si->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_2X: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: FOR_ALL_SIGNS(si) { tron@5027: if (bottom > si->sign.top && tron@5027: top < si->sign.top + 24 && tron@5027: right > si->sign.left && tron@5027: left < si->sign.left + si->sign.width_1 * 2) { tron@5027: AddSign(si, STR_2806, si->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: right += ScaleByZoom(1, dpi->zoom); truelight@7149: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@7149: tron@5027: FOR_ALL_SIGNS(si) { tron@5027: if (bottom > si->sign.top && truelight@7149: top < si->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > si->sign.left && truelight@7149: left < si->sign.left + ScaleByZoom(si->sign.width_2, dpi->zoom)) { rubidium@8447: AddSign(si, IsTransparencySet(TO_SIGNS) ? STR_2002_WHITE : STR_2002, si->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddWaypoint(const Waypoint *wp, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: rubidium@5838: sstd = (StringSpriteToDraw*)AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, str, wp->index, 0); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (wp->deleted ? 0xE : 11); tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddWaypoints(DrawPixelInfo *dpi) truelight@0: { tron@5025: const Waypoint *wp; truelight@0: int left, top, right, bottom; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_WAYPOINTS)) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: tron@5027: switch (dpi->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && tron@5027: top < wp->sign.top + 12 && tron@5027: right > wp->sign.left && tron@5027: left < wp->sign.left + wp->sign.width_1) { tron@5027: AddWaypoint(wp, STR_WAYPOINT_VIEWPORT, wp->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_2X: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && tron@5027: top < wp->sign.top + 24 && tron@5027: right > wp->sign.left && tron@5027: left < wp->sign.left + wp->sign.width_1*2) { tron@5027: AddWaypoint(wp, STR_WAYPOINT_VIEWPORT, wp->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: right += ScaleByZoom(1, dpi->zoom); truelight@7149: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@7149: tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && truelight@7149: top < wp->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > wp->sign.left && truelight@7149: left < wp->sign.left + ScaleByZoom(wp->sign.width_2, dpi->zoom)) { tron@5027: AddWaypoint(wp, STR_WAYPOINT_VIEWPORT_TINY, wp->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: truelight@0: void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str) truelight@0: { truelight@0: char buffer[128]; tron@2116: uint w; truelight@0: truelight@0: sign->top = top; truelight@0: Darkvater@4912: GetString(buffer, str, lastof(buffer)); Darkvater@4609: w = GetStringBoundingBox(buffer).width + 3; truelight@0: sign->width_1 = w; tron@2116: sign->left = left - w / 2; truelight@0: Darkvater@4609: /* zoomed out version */ peter1138@3798: _cur_fontsize = FS_SMALL; Darkvater@4609: w = GetStringBoundingBox(buffer).width + 3; peter1138@3798: _cur_fontsize = FS_NORMAL; Darkvater@1390: sign->width_2 = w; truelight@0: } truelight@0: truelight@0: tron@410: static void ViewportDrawTileSprites(TileSpriteToDraw *ts) truelight@193: { truelight@0: do { truelight@0: Point pt = RemapCoords(ts->x, ts->y, ts->z); rubidium@8177: DrawSprite(ts->image, ts->pal, pt.x, pt.y, ts->sub); tron@2116: ts = ts->next; tron@2116: } while (ts != NULL); truelight@0: } truelight@0: belugas@4171: static void ViewportSortParentSprites(ParentSpriteToDraw *psd[]) truelight@0: { tron@2116: while (*psd != NULL) { tron@2116: ParentSpriteToDraw* ps = *psd; truelight@0: rubidium@8076: if (!ps->comparison_done) { tron@2116: ParentSpriteToDraw** psd2 = psd; tron@2116: rubidium@8076: ps->comparison_done = true; truelight@193: tron@2116: while (*++psd2 != NULL) { tron@2116: ParentSpriteToDraw* ps2 = *psd2; tron@4188: ParentSpriteToDraw** psd3; hackykid@1934: rubidium@8076: if (ps2->comparison_done) continue; truelight@0: tron@4187: /* Decide which comparator to use, based on whether the bounding tron@4187: * boxes overlap tron@4187: */ rubidium@8075: if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X? rubidium@8075: ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y? rubidium@8075: ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z? tron@4188: /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of tron@4188: * the screen and with higher Z elevation, are drawn in front. tron@4188: * Here X,Y,Z are the coordinates of the "center of mass" of the sprite, tron@4188: * i.e. X=(left+right)/2, etc. tron@4188: * However, since we only care about order, don't actually divide / 2 tron@4188: */ tron@4188: if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <= tron@4188: ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) { tron@4188: continue; tron@4188: } hackykid@1934: } else { rubidium@8074: /* We only change the order, if it is definite. rubidium@8074: * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap. rubidium@8074: * That is: If one partial order says ps behind ps2, do not change the order. rubidium@8074: */ tron@4188: if (ps->xmax < ps2->xmin || tron@4188: ps->ymax < ps2->ymin || rubidium@8074: ps->zmax < ps2->zmin) { tron@4188: continue; tron@4188: } hackykid@1934: } tron@2116: belugas@6919: /* Swap the two sprites ps and ps2 using bubble-sort algorithm. */ tron@4188: psd3 = psd; tron@4188: do { tron@4188: ParentSpriteToDraw* temp = *psd3; tron@4188: *psd3 = ps2; tron@4188: ps2 = temp; truelight@0: tron@4188: psd3++; tron@4188: } while (psd3 <= psd2); truelight@0: } truelight@0: } else { truelight@0: psd++; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2116: static void ViewportDrawParentSprites(ParentSpriteToDraw *psd[]) truelight@0: { tron@2116: for (; *psd != NULL; psd++) { tron@2116: const ParentSpriteToDraw* ps = *psd; tron@2116: const ChildScreenSpriteToDraw* cs; truelight@0: rubidium@8177: if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSprite(ps->image, ps->pal, ps->x, ps->y, ps->sub); truelight@0: tron@2116: for (cs = ps->child; cs != NULL; cs = cs->next) { rubidium@8177: DrawSprite(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@8139: /** rubidium@8139: * Draws the bounding boxes of all ParentSprites rubidium@8139: * @param psd Array of ParentSprites rubidium@8139: */ rubidium@8139: static void ViewportDrawBoundingBoxes(ParentSpriteToDraw *psd[]) rubidium@8139: { rubidium@8139: for (; *psd != NULL; psd++) { rubidium@8139: const ParentSpriteToDraw* ps = *psd; rubidium@8139: Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner rubidium@8139: Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner rubidium@8139: Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner rubidium@8139: Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner rubidium@8139: rubidium@8139: DrawBox( pt1.x, pt1.y, rubidium@8139: pt2.x - pt1.x, pt2.y - pt1.y, rubidium@8139: pt3.x - pt1.x, pt3.y - pt1.y, rubidium@8139: pt4.x - pt1.x, pt4.y - pt1.y); rubidium@8139: } rubidium@8139: } rubidium@8139: tron@2116: static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDraw *ss) truelight@0: { truelight@0: DrawPixelInfo dp; truelight@7120: ZoomLevel zoom; truelight@193: truelight@0: _cur_dpi = &dp; truelight@0: dp = *dpi; truelight@0: tron@2116: zoom = dp.zoom; truelight@7120: dp.zoom = ZOOM_LVL_NORMAL; truelight@0: truelight@7150: dp.left = UnScaleByZoom(dp.left, zoom); truelight@7150: dp.top = UnScaleByZoom(dp.top, zoom); truelight@7150: dp.width = UnScaleByZoom(dp.width, zoom); truelight@7150: dp.height = UnScaleByZoom(dp.height, zoom); truelight@0: truelight@0: do { tron@5025: uint16 colour; tron@5025: truelight@0: if (ss->width != 0) { truelight@7150: int x = UnScaleByZoom(ss->x, zoom) - 1; truelight@7150: int y = UnScaleByZoom(ss->y, zoom) - 1; tron@2116: int bottom = y + 11; tron@2116: int w = ss->width; truelight@193: truelight@0: if (w & 0x8000) { truelight@0: w &= ~0x8000; truelight@0: y--; truelight@0: bottom -= 6; truelight@0: w -= 3; truelight@0: } truelight@0: tron@2116: /* Draw the rectangle if 'tranparent station signs' is off, tron@2116: * or if we are drawing a general text sign (STR_2806) */ belugas@8345: if (!IsTransparencySet(TO_SIGNS) || ss->string == STR_2806) { tron@2116: DrawFrameRect( tron@2116: x, y, x + w, bottom, ss->color, belugas@8345: IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE tron@2116: ); peter1138@6923: } truelight@0: } truelight@0: tron@534: SetDParam(0, ss->params[0]); tron@534: SetDParam(1, ss->params[1]); tron@2116: /* if we didn't draw a rectangle, or if transparant building is on, tron@2116: * draw the text in the color the rectangle would have */ belugas@8345: if (IsTransparencySet(TO_SIGNS) && ss->string != STR_2806 && ss->width != 0) { tron@2116: /* Real colors need the IS_PALETTE_COLOR flag tron@2116: * otherwise colors from _string_colormap are assumed. */ tron@5025: colour = _colour_gradient[ss->color][6] | IS_PALETTE_COLOR; truelight@0: } else { belugas@8320: colour = TC_BLACK; truelight@0: } tron@5025: DrawString( truelight@7150: UnScaleByZoom(ss->x, zoom), UnScaleByZoom(ss->y, zoom) - (ss->width & 0x8000 ? 2 : 0), tron@5025: ss->string, colour tron@5025: ); tron@2116: tron@2116: ss = ss->next; tron@2116: } while (ss != NULL); truelight@0: } truelight@0: tron@430: void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: ViewportDrawer vd; truelight@0: int mask; tron@2116: int x; tron@2116: int y; truelight@0: DrawPixelInfo *old_dpi; truelight@0: truelight@0: byte mem[VIEWPORT_DRAW_MEM]; truelight@137: ParentSpriteToDraw *parent_list[6144]; truelight@0: truelight@0: _cur_vd = &vd; truelight@0: truelight@0: old_dpi = _cur_dpi; truelight@0: _cur_dpi = &vd.dpi; truelight@0: truelight@0: vd.dpi.zoom = vp->zoom; truelight@7122: mask = ScaleByZoom(-1, vp->zoom); truelight@0: truelight@0: vd.combine_sprites = 0; truelight@0: truelight@0: vd.dpi.width = (right - left) & mask; truelight@0: vd.dpi.height = (bottom - top) & mask; truelight@0: vd.dpi.left = left & mask; truelight@0: vd.dpi.top = top & mask; truelight@0: vd.dpi.pitch = old_dpi->pitch; truelight@0: truelight@7122: x = UnScaleByZoom(vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left; truelight@7122: y = UnScaleByZoom(vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top; truelight@0: truelight@7433: vd.dpi.dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top); truelight@0: truelight@0: vd.parent_list = parent_list; tron@2116: vd.eof_parent_list = endof(parent_list); truelight@0: vd.spritelist_mem = mem; tron@2116: vd.eof_spritelist_mem = endof(mem) - sizeof(LARGEST_SPRITELIST_STRUCT); truelight@0: vd.last_string = &vd.first_string; truelight@0: vd.first_string = NULL; truelight@0: vd.last_tile = &vd.first_tile; truelight@0: vd.first_tile = NULL; truelight@193: truelight@0: ViewportAddLandscape(); truelight@0: ViewportAddVehicles(&vd.dpi); truelight@0: DrawTextEffects(&vd.dpi); truelight@0: truelight@0: ViewportAddTownNames(&vd.dpi); truelight@0: ViewportAddStationNames(&vd.dpi); truelight@0: ViewportAddSigns(&vd.dpi); darkvater@395: ViewportAddWaypoints(&vd.dpi); truelight@0: belugas@6919: /* This assert should never happen (because the length of the parent_list belugas@6919: * is checked) */ tron@979: assert(vd.parent_list <= endof(parent_list)); truelight@133: tron@2116: if (vd.first_tile != NULL) ViewportDrawTileSprites(vd.first_tile); truelight@0: truelight@0: /* null terminate parent sprite list */ truelight@0: *vd.parent_list = NULL; truelight@0: truelight@0: ViewportSortParentSprites(parent_list); truelight@0: ViewportDrawParentSprites(parent_list); belugas@9058: rubidium@8139: if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(parent_list); truelight@193: tron@2116: if (vd.first_string != NULL) ViewportDrawStrings(&vd.dpi, vd.first_string); truelight@193: truelight@0: _cur_dpi = old_dpi; truelight@0: } truelight@0: belugas@6919: /** Make sure we don't draw a too big area at a time. belugas@6919: * If we do, the sprite memory will overflow. */ Darkvater@5120: static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@7122: if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000) { truelight@0: if ((bottom - top) > (right - left)) { truelight@0: int t = (top + bottom) >> 1; truelight@0: ViewportDrawChk(vp, left, top, right, t); truelight@0: ViewportDrawChk(vp, left, t, right, bottom); truelight@0: } else { truelight@0: int t = (left + right) >> 1; truelight@0: ViewportDrawChk(vp, left, top, t, bottom); truelight@0: ViewportDrawChk(vp, t, top, right, bottom); truelight@0: } truelight@0: } else { truelight@193: ViewportDoDraw(vp, truelight@7122: ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left, truelight@7122: ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top, truelight@7122: ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left, truelight@7122: ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top truelight@0: ); truelight@0: } truelight@0: } truelight@0: Darkvater@5120: static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { tron@2116: if (right <= vp->left || bottom <= vp->top) return; truelight@0: tron@2116: if (left >= vp->left + vp->width) return; truelight@0: truelight@0: if (left < vp->left) left = vp->left; tron@2116: if (right > vp->left + vp->width) right = vp->left + vp->width; truelight@0: tron@2116: if (top >= vp->top + vp->height) return; truelight@0: truelight@0: if (top < vp->top) top = vp->top; tron@2116: if (bottom > vp->top + vp->height) bottom = vp->top + vp->height; truelight@0: truelight@0: ViewportDrawChk(vp, left, top, right, bottom); truelight@0: } truelight@0: Darkvater@5120: void DrawWindowViewport(const Window *w) tron@2116: { truelight@0: DrawPixelInfo *dpi = _cur_dpi; truelight@0: truelight@0: dpi->left += w->left; truelight@0: dpi->top += w->top; truelight@0: truelight@0: ViewportDraw(w->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height); truelight@0: truelight@0: dpi->left -= w->left; truelight@0: dpi->top -= w->top; truelight@0: } truelight@0: peter1138@7565: static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y) peter1138@7565: { peter1138@7565: /* Centre of the viewport is hot spot */ peter1138@7565: x += vp->virtual_width / 2; peter1138@7565: y += vp->virtual_height / 2; peter1138@7565: peter1138@7565: /* Convert viewport coordinates to map coordinates peter1138@7565: * Calculation is scaled by 4 to avoid rounding errors */ peter1138@7565: int vx = -x + y * 2; peter1138@7565: int vy = x + y * 2; peter1138@7565: peter1138@7565: /* clamp to size of map */ skidd13@8418: vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4); skidd13@8418: vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4); peter1138@7565: peter1138@7565: /* Convert map coordinates to viewport coordinates */ peter1138@7565: x = (-vx + vy) / 2; peter1138@7565: y = ( vx + vy) / 4; peter1138@7565: peter1138@7565: /* Remove centreing */ peter1138@7565: x -= vp->virtual_width / 2; peter1138@7565: y -= vp->virtual_height / 2; peter1138@7565: } peter1138@7565: truelight@0: void UpdateViewportPosition(Window *w) truelight@0: { tron@2116: const ViewPort *vp = w->viewport; truelight@0: tron@2116: if (WP(w, vp_d).follow_vehicle != INVALID_VEHICLE) { rubidium@8578: const Vehicle* veh = GetVehicle(WP(w, vp_d).follow_vehicle); tron@2116: Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: truelight@0: SetViewportPosition(w, pt.x, pt.y); truelight@0: } else { peter1138@7565: /* Ensure the destination location is within the map */ peter1138@7565: ClampViewportToMap(vp, WP(w, vp_d).dest_scrollpos_x, WP(w, vp_d).dest_scrollpos_y); peter1138@7565: peter1138@7565: int delta_x = WP(w, vp_d).dest_scrollpos_x - WP(w, vp_d).scrollpos_x; peter1138@7565: int delta_y = WP(w, vp_d).dest_scrollpos_y - WP(w, vp_d).scrollpos_y; peter1138@7226: peter1138@7226: if (delta_x != 0 || delta_y != 0) { peter1138@7227: if (_patches.smooth_scroll) { peter1138@7227: int max_scroll = ScaleByMapSize1D(512); peter1138@7227: /* Not at our desired positon yet... */ skidd13@8418: WP(w, vp_d).scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll); skidd13@8418: WP(w, vp_d).scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll); peter1138@7227: } else { peter1138@7565: WP(w, vp_d).scrollpos_x = WP(w, vp_d).dest_scrollpos_x; peter1138@7565: WP(w, vp_d).scrollpos_y = WP(w, vp_d).dest_scrollpos_y; peter1138@7227: } peter1138@7226: } peter1138@7226: peter1138@7565: ClampViewportToMap(vp, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y); belugas@9058: tron@2116: SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y); truelight@0: } truelight@0: } truelight@0: rubidium@8041: /** rubidium@8041: * Marks a viewport as dirty for repaint. rubidium@8041: * rubidium@8041: * @param vp The viewport to mark as dirty rubidium@8041: * @todo documents the missing parameters @c left, @c top, @c right and @c bottom rubidium@8041: * @todo detailed description missing rubidium@8041: * @ingroup dirty rubidium@8041: */ tron@2116: static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { tron@2116: right -= vp->virtual_left; tron@2116: if (right <= 0) return; truelight@0: tron@2116: bottom -= vp->virtual_top; tron@2116: if (bottom <= 0) return; truelight@0: tron@2116: left = max(0, left - vp->virtual_left); truelight@0: tron@2116: if (left >= vp->virtual_width) return; truelight@0: tron@2116: top = max(0, top - vp->virtual_top); tron@2116: tron@2116: if (top >= vp->virtual_height) return; truelight@0: truelight@0: SetDirtyBlocks( truelight@7122: UnScaleByZoom(left, vp->zoom) + vp->left, truelight@7122: UnScaleByZoom(top, vp->zoom) + vp->top, truelight@7122: UnScaleByZoom(right, vp->zoom) + vp->left, truelight@7122: UnScaleByZoom(bottom, vp->zoom) + vp->top truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkAllViewportsDirty(int left, int top, int right, int bottom) truelight@0: { tron@2116: const ViewPort *vp = _viewports; truelight@0: uint32 act = _active_viewports; truelight@0: do { truelight@0: if (act & 1) { truelight@0: assert(vp->width != 0); truelight@0: MarkViewportDirty(vp, left, top, right, bottom); truelight@0: } truelight@0: } while (vp++,act>>=1); truelight@0: } truelight@0: tron@2116: void MarkTileDirtyByTile(TileIndex tile) tron@2116: { celestar@3421: Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTileZ(tile)); truelight@0: MarkAllViewportsDirty( truelight@0: pt.x - 31, truelight@0: pt.y - 122, truelight@0: pt.x - 31 + 67, truelight@0: pt.y - 122 + 154 truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkTileDirty(int x, int y) truelight@0: { tron@2116: uint z = 0; truelight@0: Point pt; tron@2116: skidd13@8450: if (IsInsideMM(x, 0, MapSizeX() * TILE_SIZE) && skidd13@8450: IsInsideMM(y, 0, MapSizeY() * TILE_SIZE)) tron@1980: z = GetTileZ(TileVirtXY(x, y)); truelight@0: pt = RemapCoords(x, y, z); truelight@0: truelight@0: MarkAllViewportsDirty( truelight@0: pt.x - 31, truelight@0: pt.y - 122, truelight@0: pt.x - 31 + 67, truelight@0: pt.y - 122 + 154 truelight@0: ); truelight@193: } truelight@0: rubidium@8041: /** rubidium@8041: * Marks the selected tiles as dirty. rubidium@8041: * rubidium@8041: * This function marks the selected tiles as dirty for repaint rubidium@8041: * rubidium@8041: * @note Documentation may be wrong (Progman) rubidium@8041: * @ingroup dirty rubidium@8041: */ rubidium@6573: static void SetSelectionTilesDirty() truelight@0: { truelight@0: int y_size, x_size; tron@1863: int x = _thd.pos.x; tron@1863: int y = _thd.pos.y; truelight@0: tron@1863: x_size = _thd.size.x; tron@1863: y_size = _thd.size.y; truelight@0: tron@1863: if (_thd.outersize.x) { tron@1863: x_size += _thd.outersize.x; tron@1863: x += _thd.offs.x; tron@1863: y_size += _thd.outersize.y; tron@1863: y += _thd.offs.y; truelight@0: } truelight@0: truelight@0: assert(x_size > 0); truelight@0: assert(y_size > 0); truelight@0: truelight@0: x_size += x; truelight@0: y_size += y; truelight@0: truelight@0: do { truelight@0: int y_bk = y; truelight@0: do { truelight@0: MarkTileDirty(x, y); celestar@3421: } while ( (y += TILE_SIZE) != y_size); truelight@0: y = y_bk; celestar@3421: } while ( (x += TILE_SIZE) != x_size); truelight@0: } truelight@0: truelight@0: tron@1990: void SetSelectionRed(bool b) tron@1990: { tron@1990: _thd.make_square_red = b; tron@1990: SetSelectionTilesDirty(); tron@1990: } tron@1990: tron@1990: tron@2116: static bool CheckClickOnTown(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Town *t; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@4471: x = x - vp->left + vp->virtual_left; tron@4471: y = y - vp->top + vp->virtual_top; tron@4471: FOR_ALL_TOWNS(t) { tron@4471: if (y >= t->sign.top && tron@4471: y < t->sign.top + 12 && tron@4471: x >= t->sign.left && tron@4471: x < t->sign.left + t->sign.width_1) { tron@4471: ShowTownViewWindow(t->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_2X: tron@4471: x = (x - vp->left + 1) * 2 + vp->virtual_left; tron@4471: y = (y - vp->top + 1) * 2 + vp->virtual_top; tron@4471: FOR_ALL_TOWNS(t) { tron@4471: if (y >= t->sign.top && tron@4471: y < t->sign.top + 24 && tron@4471: x >= t->sign.left && tron@4471: x < t->sign.left + t->sign.width_1 * 2) { tron@4471: ShowTownViewWindow(t->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@7149: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@7149: tron@4471: FOR_ALL_TOWNS(t) { tron@4471: if (y >= t->sign.top && truelight@7149: y < t->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= t->sign.left && truelight@7149: x < t->sign.left + ScaleByZoom(t->sign.width_2, vp->zoom)) { tron@4471: ShowTownViewWindow(t->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@4471: tron@2116: static bool CheckClickOnStation(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Station *st; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_STATION_NAMES)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@4471: x = x - vp->left + vp->virtual_left; tron@4471: y = y - vp->top + vp->virtual_top; tron@4471: FOR_ALL_STATIONS(st) { tron@4471: if (y >= st->sign.top && tron@4471: y < st->sign.top + 12 && tron@4471: x >= st->sign.left && tron@4471: x < st->sign.left + st->sign.width_1) { tron@4471: ShowStationViewWindow(st->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_2X: tron@4471: x = (x - vp->left + 1) * 2 + vp->virtual_left; tron@4471: y = (y - vp->top + 1) * 2 + vp->virtual_top; tron@4471: FOR_ALL_STATIONS(st) { tron@4471: if (y >= st->sign.top && tron@4471: y < st->sign.top + 24 && tron@4471: x >= st->sign.left && tron@4471: x < st->sign.left + st->sign.width_1 * 2) { tron@4471: ShowStationViewWindow(st->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@7149: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@7149: tron@4471: FOR_ALL_STATIONS(st) { tron@4471: if (y >= st->sign.top && truelight@7149: y < st->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= st->sign.left && truelight@7149: x < st->sign.left + ScaleByZoom(st->sign.width_2, vp->zoom)) { tron@4471: ShowStationViewWindow(st->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@4471: tron@2116: static bool CheckClickOnSign(const ViewPort *vp, int x, int y) truelight@0: { truelight@4349: const Sign *si; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_SHOW_SIGNS) || _current_player == PLAYER_SPECTATOR) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@4471: x = x - vp->left + vp->virtual_left; tron@4471: y = y - vp->top + vp->virtual_top; tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && tron@4471: y < si->sign.top + 12 && tron@4471: x >= si->sign.left && tron@4471: x < si->sign.left + si->sign.width_1) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_2X: tron@4471: x = (x - vp->left + 1) * 2 + vp->virtual_left; tron@4471: y = (y - vp->top + 1) * 2 + vp->virtual_top; tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && tron@4471: y < si->sign.top + 24 && tron@4471: x >= si->sign.left && tron@4471: x < si->sign.left + si->sign.width_1 * 2) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@7149: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@7149: tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && truelight@7149: y < si->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= si->sign.left && truelight@7149: x < si->sign.left + ScaleByZoom(si->sign.width_2, vp->zoom)) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@4471: tron@2116: static bool CheckClickOnWaypoint(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Waypoint *wp; truelight@0: skidd13@8424: if (!HasBit(_display_opt, DO_WAYPOINTS)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@7120: case ZOOM_LVL_NORMAL: tron@4471: x = x - vp->left + vp->virtual_left; tron@4471: y = y - vp->top + vp->virtual_top; tron@4471: FOR_ALL_WAYPOINTS(wp) { tron@4471: if (y >= wp->sign.top && tron@4471: y < wp->sign.top + 12 && tron@4471: x >= wp->sign.left && tron@4471: x < wp->sign.left + wp->sign.width_1) { tron@4471: ShowRenameWaypointWindow(wp); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_2X: tron@4471: x = (x - vp->left + 1) * 2 + vp->virtual_left; tron@4471: y = (y - vp->top + 1) * 2 + vp->virtual_top; tron@4471: FOR_ALL_WAYPOINTS(wp) { tron@4471: if (y >= wp->sign.top && tron@4471: y < wp->sign.top + 24 && tron@4471: x >= wp->sign.left && tron@4471: x < wp->sign.left + wp->sign.width_1 * 2) { tron@4471: ShowRenameWaypointWindow(wp); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@7120: case ZOOM_LVL_OUT_4X: truelight@7149: case ZOOM_LVL_OUT_8X: truelight@7149: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@7149: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@7149: tron@4471: FOR_ALL_WAYPOINTS(wp) { tron@4471: if (y >= wp->sign.top && truelight@7149: y < wp->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= wp->sign.left && truelight@7149: x < wp->sign.left + ScaleByZoom(wp->sign.width_2, vp->zoom)) { tron@4471: ShowRenameWaypointWindow(wp); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; truelight@7149: truelight@7149: default: NOT_REACHED(); truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: tron@2116: static void CheckClickOnLandscape(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: Point pt = TranslateXYToTileCoord(vp, x, y); tron@1977: tron@1980: if (pt.x != -1) ClickTile(TileVirtXY(pt.x, pt.y)); truelight@0: } truelight@0: tron@2662: tron@2662: static void SafeShowTrainViewWindow(const Vehicle* v) tron@2662: { rubidium@7993: if (!IsFrontEngine(v)) v = v->First(); rubidium@7982: ShowVehicleViewWindow(v); tron@2662: } tron@2662: maedhros@7353: static void SafeShowRoadVehViewWindow(const Vehicle *v) maedhros@7353: { rubidium@7993: if (!IsRoadVehFront(v)) v = v->First(); rubidium@7982: ShowVehicleViewWindow(v); maedhros@7353: } maedhros@7353: belugas@4171: static void Nop(const Vehicle *v) {} tron@2662: belugas@4171: typedef void OnVehicleClickProc(const Vehicle *v); tron@2662: static OnVehicleClickProc* const _on_vehicle_click_proc[] = { tron@2662: SafeShowTrainViewWindow, maedhros@7353: SafeShowRoadVehViewWindow, rubidium@7982: ShowVehicleViewWindow, rubidium@7982: ShowVehicleViewWindow, tron@2662: Nop, // Special vehicles tron@2662: Nop // Disaster vehicles truelight@0: }; truelight@0: tron@2116: void HandleViewportClicked(const ViewPort *vp, int x, int y) truelight@0: { belugas@4171: const Vehicle *v; truelight@0: tron@2116: if (CheckClickOnTown(vp, x, y)) return; tron@2116: if (CheckClickOnStation(vp, x, y)) return; tron@2116: if (CheckClickOnSign(vp, x, y)) return; tron@2116: if (CheckClickOnWaypoint(vp, x, y)) return; truelight@0: CheckClickOnLandscape(vp, x, y); truelight@0: tron@2116: v = CheckClickOnVehicle(vp, x, y); celestar@3809: if (v != NULL) { Darkvater@5568: DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v); bjarni@6206: _on_vehicle_click_proc[v->type](v); celestar@3809: } truelight@0: } truelight@0: rubidium@6573: Vehicle *CheckMouseOverVehicle() truelight@0: { belugas@4171: const Window *w; belugas@4171: const ViewPort *vp; truelight@0: truelight@0: int x = _cursor.pos.x; truelight@0: int y = _cursor.pos.y; truelight@0: truelight@0: w = FindWindowFromPt(x, y); tron@2116: if (w == NULL) return NULL; truelight@0: truelight@0: vp = IsPtInWindowViewport(w, x, y); tron@2116: return (vp != NULL) ? CheckClickOnVehicle(vp, x, y) : NULL; truelight@0: } truelight@0: truelight@0: truelight@0: rubidium@6573: void PlaceObject() truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@193: truelight@0: pt = GetTileBelowCursor(); tron@2116: if (pt.x == -1) return; truelight@0: dominik@1070: if (_thd.place_mode == VHM_POINT) { truelight@0: pt.x += 8; truelight@0: pt.y += 8; truelight@0: } truelight@0: truelight@0: _tile_fract_coords.x = pt.x & 0xF; truelight@0: _tile_fract_coords.y = pt.y & 0xF; truelight@0: tron@2116: w = GetCallbackWnd(); tron@2116: if (w != NULL) { tron@2116: WindowEvent e; tron@2116: truelight@0: e.event = WE_PLACE_OBJ; belugas@4634: e.we.place.pt = pt; belugas@4634: e.we.place.tile = TileVirtXY(pt.x, pt.y); truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: } truelight@0: darkvater@152: darkvater@152: /* scrolls the viewport in a window to a given location */ peter1138@7226: bool ScrollWindowTo(int x , int y, Window *w, bool instant) darkvater@152: { rubidium@8969: /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */ rubidium@8969: Point pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(Clamp(x, 0, MapSizeX()), Clamp(y, 0, MapSizeY()))); tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; darkvater@152: peter1138@7226: if (WP(w, vp_d).dest_scrollpos_x == pt.x && WP(w, vp_d).dest_scrollpos_y == pt.y) tron@2116: return false; darkvater@152: peter1138@7227: if (instant) { peter1138@7226: WP(w, vp_d).scrollpos_x = pt.x; peter1138@7226: WP(w, vp_d).scrollpos_y = pt.y; peter1138@7226: } peter1138@7226: peter1138@7226: WP(w, vp_d).dest_scrollpos_x = pt.x; peter1138@7226: WP(w, vp_d).dest_scrollpos_y = pt.y; darkvater@152: return true; darkvater@152: } darkvater@152: darkvater@152: peter1138@7226: bool ScrollMainWindowTo(int x, int y, bool instant) truelight@0: { truelight@4339: Window *w; peter1138@7226: bool res = ScrollWindowTo(x, y, FindWindowById(WC_MAIN_WINDOW, 0), instant); truelight@4339: truelight@4339: /* If a user scrolls to a tile (via what way what so ever) and already is on truelight@4339: * that tile (e.g.: pressed twice), move the smallmap to that location, truelight@4339: * so you directly see where you are on the smallmap. */ truelight@4339: truelight@4339: if (res) return res; truelight@4339: truelight@4339: w = FindWindowById(WC_SMALLMAP, 0); truelight@4339: if (w == NULL) return res; truelight@4339: truelight@4339: SmallMapCenterOnCurrentPos(w); truelight@4339: truelight@4339: return res; truelight@0: } truelight@0: truelight@0: peter1138@7226: bool ScrollMainWindowToTile(TileIndex tile, bool instant) truelight@0: { peter1138@7226: return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, instant); truelight@0: } truelight@0: truelight@0: void SetRedErrorSquare(TileIndex tile) truelight@0: { truelight@0: TileIndex old; truelight@0: truelight@0: old = _thd.redsq; truelight@0: _thd.redsq = tile; truelight@0: truelight@0: if (tile != old) { celestar@3281: if (tile != 0) MarkTileDirtyByTile(tile); celestar@3281: if (old != 0) MarkTileDirtyByTile(old); truelight@0: } truelight@0: } truelight@0: truelight@0: void SetTileSelectSize(int w, int h) truelight@0: { celestar@3421: _thd.new_size.x = w * TILE_SIZE; celestar@3421: _thd.new_size.y = h * TILE_SIZE; tron@1863: _thd.new_outersize.x = 0; tron@1863: _thd.new_outersize.y = 0; truelight@0: } truelight@0: tron@2116: void SetTileSelectBigSize(int ox, int oy, int sx, int sy) tron@2116: { celestar@3421: _thd.offs.x = ox * TILE_SIZE; celestar@3421: _thd.offs.y = oy * TILE_SIZE; celestar@3421: _thd.new_outersize.x = sx * TILE_SIZE; celestar@3421: _thd.new_outersize.y = sy * TILE_SIZE; truelight@0: } truelight@0: belugas@6919: /** returns the best autorail highlight type from map coordinates */ rubidium@8720: static HighLightStyle GetAutorailHT(int x, int y) dominik@1070: { rubidium@8720: return HT_RAIL | _autorail_piece[x & 0xF][y & 0xF]; dominik@1070: } truelight@0: belugas@7574: /** belugas@7574: * Updates tile highlighting for all cases. belugas@7574: * Uses _thd.selstart and _thd.selend and _thd.place_mode (set elsewhere) to determine _thd.pos and _thd.size belugas@7574: * Also drawstyle is determined. Uses _thd.new.* as a buffer and calls SetSelectionTilesDirty() twice, belugas@7574: * Once for the old and once for the new selection. belugas@7574: * _thd is TileHighlightData, found in viewport.h belugas@7574: * Called by MouseLoop() in windows.cpp belugas@7574: */ rubidium@6573: void UpdateTileSelection() truelight@0: { tron@2116: int x1; tron@2116: int y1; truelight@0: tron@1863: _thd.new_drawstyle = 0; truelight@0: tron@1863: if (_thd.place_mode == VHM_SPECIAL) { tron@1863: x1 = _thd.selend.x; tron@1863: y1 = _thd.selend.y; truelight@0: if (x1 != -1) { smatz@8408: int x2 = _thd.selstart.x & ~0xF; smatz@8408: int y2 = _thd.selstart.y & ~0xF; truelight@0: x1 &= ~0xF; truelight@0: y1 &= ~0xF; truelight@193: tron@6432: if (x1 >= x2) Swap(x1, x2); tron@6432: if (y1 >= y2) Swap(y1, y2); tron@1863: _thd.new_pos.x = x1; tron@1863: _thd.new_pos.y = y1; celestar@3421: _thd.new_size.x = x2 - x1 + TILE_SIZE; celestar@3421: _thd.new_size.y = y2 - y1 + TILE_SIZE; tron@1863: _thd.new_drawstyle = _thd.next_drawstyle; truelight@0: } tron@1863: } else if (_thd.place_mode != VHM_NONE) { tron@2116: Point pt = GetTileBelowCursor(); truelight@0: x1 = pt.x; truelight@0: y1 = pt.y; truelight@0: if (x1 != -1) { tron@1863: switch (_thd.place_mode) { dominik@1070: case VHM_RECT: tron@1863: _thd.new_drawstyle = HT_RECT; dominik@1070: break; dominik@1070: case VHM_POINT: tron@1863: _thd.new_drawstyle = HT_POINT; dominik@1070: x1 += 8; dominik@1070: y1 += 8; dominik@1070: break; dominik@1070: case VHM_RAIL: tron@1863: _thd.new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile smatz@8414: break; smatz@8414: default: smatz@8414: NOT_REACHED(); smatz@8414: break; truelight@0: } tron@1863: _thd.new_pos.x = x1 & ~0xF; tron@1863: _thd.new_pos.y = y1 & ~0xF; truelight@0: } truelight@0: } truelight@0: belugas@6919: /* redraw selection */ tron@1863: if (_thd.drawstyle != _thd.new_drawstyle || tron@1863: _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y || Darkvater@4539: _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y || belugas@7574: _thd.outersize.x != _thd.new_outersize.x || belugas@7574: _thd.outersize.y != _thd.new_outersize.y) { belugas@6919: /* clear the old selection? */ tron@1863: if (_thd.drawstyle) SetSelectionTilesDirty(); truelight@0: tron@1863: _thd.drawstyle = _thd.new_drawstyle; tron@1863: _thd.pos = _thd.new_pos; tron@1863: _thd.size = _thd.new_size; tron@1863: _thd.outersize = _thd.new_outersize; tron@1863: _thd.dirty = 0xff; truelight@0: belugas@6919: /* draw the new selection? */ tron@1863: if (_thd.new_drawstyle) SetSelectionTilesDirty(); truelight@0: } truelight@0: } truelight@0: belugas@6919: /** highlighting tiles while only going over them with the mouse */ rubidium@8384: void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, byte process) truelight@0: { maedhros@7165: _thd.select_method = method; maedhros@7165: _thd.select_proc = process; celestar@3421: _thd.selend.x = TileX(tile) * TILE_SIZE; celestar@3421: _thd.selstart.x = TileX(tile) * TILE_SIZE; celestar@3421: _thd.selend.y = TileY(tile) * TILE_SIZE; celestar@3421: _thd.selstart.y = TileY(tile) * TILE_SIZE; smatz@8408: smatz@8408: /* Needed so several things (road, autoroad, bridges, ...) are placed correctly. smatz@8408: * In effect, placement starts from the centre of a tile smatz@8408: */ smatz@8408: if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) { smatz@8408: _thd.selend.x += TILE_SIZE / 2; smatz@8408: _thd.selend.y += TILE_SIZE / 2; smatz@8408: _thd.selstart.x += TILE_SIZE / 2; smatz@8408: _thd.selstart.y += TILE_SIZE / 2; smatz@8408: } smatz@8408: tron@1863: if (_thd.place_mode == VHM_RECT) { tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = HT_RECT; tron@1863: } else if (_thd.place_mode == VHM_RAIL) { // autorail one piece tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = _thd.drawstyle; truelight@0: } else { tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = HT_POINT; truelight@0: } truelight@0: _special_mouse_mode = WSM_SIZING; truelight@0: } truelight@0: truelight@0: void VpSetPlaceSizingLimit(int limit) truelight@0: { truelight@0: _thd.sizelimit = limit; truelight@0: } truelight@0: Darkvater@4834: /** Darkvater@4834: * Highlights all tiles between a set of two tiles. Used in dock and tunnel placement Darkvater@4834: * @param from TileIndex of the first tile to highlight Darkvater@4834: * @param to TileIndex of the last tile to highlight */ Darkvater@4834: void VpSetPresizeRange(TileIndex from, TileIndex to) truelight@0: { rubidium@7502: uint64 distance = DistanceManhattan(from, to) + 1; Darkvater@4834: celestar@3421: _thd.selend.x = TileX(to) * TILE_SIZE; celestar@3421: _thd.selend.y = TileY(to) * TILE_SIZE; celestar@3421: _thd.selstart.x = TileX(from) * TILE_SIZE; celestar@3421: _thd.selstart.y = TileY(from) * TILE_SIZE; tron@1863: _thd.next_drawstyle = HT_RECT; Darkvater@4834: Darkvater@4834: /* show measurement only if there is any length to speak of */ Darkvater@4834: if (distance > 1) GuiShowTooltipsWithArgs(STR_MEASURE_LENGTH, 1, &distance); truelight@0: } truelight@0: rubidium@6573: static void VpStartPreSizing() truelight@0: { truelight@0: _thd.selend.x = -1; truelight@0: _special_mouse_mode = WSM_PRESIZE; truelight@0: } truelight@0: belugas@6919: /** returns information about the 2x1 piece to be build. dominik@1070: * The lower bits (0-3) are the track type. */ rubidium@8720: static HighLightStyle Check2x1AutoRail(int mode) dominik@1070: { dominik@1070: int fxpy = _tile_fract_coords.x + _tile_fract_coords.y; tron@1863: int sxpy = (_thd.selend.x & 0xF) + (_thd.selend.y & 0xF); dominik@1070: int fxmy = _tile_fract_coords.x - _tile_fract_coords.y; tron@1863: int sxmy = (_thd.selend.x & 0xF) - (_thd.selend.y & 0xF); dominik@1070: tron@2952: switch (mode) { rubidium@8720: default: NOT_REACHED(); rubidium@8720: case 0: // end piece is lower right rubidium@8720: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; } rubidium@8720: if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return HT_DIR_VR; } rubidium@8720: return HT_DIR_Y; rubidium@8720: rubidium@8720: case 1: rubidium@8720: if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_VL; } rubidium@8720: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; } rubidium@8720: return HT_DIR_Y; rubidium@8720: rubidium@8720: case 2: rubidium@8720: if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return HT_DIR_VL; } rubidium@8720: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; } rubidium@8720: return HT_DIR_X; rubidium@8720: rubidium@8720: case 3: rubidium@8720: if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return HT_DIR_VR; } rubidium@8720: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; } rubidium@8720: return HT_DIR_X; dominik@1070: } dominik@1070: } dominik@1070: Darkvater@4834: /** Check if the direction of start and end tile should be swapped based on Darkvater@4834: * the dragging-style. Default directions are: Darkvater@4834: * in the case of a line (HT_RAIL, HT_LINE): DIR_NE, DIR_NW, DIR_N, DIR_E Darkvater@4834: * in the case of a rect (HT_RECT, HT_POINT): DIR_S, DIR_E Darkvater@4834: * For example dragging a rectangle area from south to north should be swapped to Darkvater@4834: * north-south (DIR_S) to obtain the same results with less code. This is what Darkvater@4834: * the return value signifies. Darkvater@4834: * @param style HighLightStyle dragging style belugas@6980: * @param start_tile start tile of drag belugas@6980: * @param end_tile end tile of drag belugas@6980: * @return boolean value which when true means start/end should be swapped */ Darkvater@4834: static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile) Darkvater@4834: { Darkvater@4834: uint start_x = TileX(start_tile); Darkvater@4834: uint start_y = TileY(start_tile); Darkvater@4834: uint end_x = TileX(end_tile); Darkvater@4834: uint end_y = TileY(end_tile); Darkvater@4834: Darkvater@4834: switch (style & HT_DRAG_MASK) { Darkvater@4834: case HT_RAIL: Darkvater@4834: case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y)); Darkvater@4834: Darkvater@4834: case HT_RECT: Darkvater@4834: case HT_POINT: return (end_x != start_x && end_y < start_y); Darkvater@4834: default: NOT_REACHED(); Darkvater@4834: } Darkvater@4834: Darkvater@4834: return false; Darkvater@4834: } Darkvater@4834: Darkvater@4834: /** Calculates height difference between one tile and another Darkvater@4834: * Multiplies the result to suit the standard given by minimap - 50 meters high Darkvater@4834: * To correctly get the height difference we need the direction we are dragging Darkvater@4834: * in, as well as with what kind of tool we are dragging. For example a horizontal Darkvater@4834: * autorail tool that starts in bottom and ends at the top of a tile will need the rubidium@6987: * maximum of SW, S and SE, N corners respectively. This is handled by the lookup table below Darkvater@4834: * See _tileoffs_by_dir in map.c for the direction enums if you can't figure out Darkvater@4834: * the values yourself. Darkvater@4834: * @param style HightlightStyle of drag. This includes direction and style (autorail, rect, etc.) Darkvater@4834: * @param distance amount of tiles dragged, important for horizontal/vertical drags Darkvater@4834: * ignored for others Darkvater@4834: * @param start_tile, end_tile start and end tile of drag operation Darkvater@4834: * @return height difference between two tiles. Tile measurement tool utilizes Darkvater@4834: * this value in its tooltips */ Darkvater@4834: static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile) Darkvater@4834: { Darkvater@4834: bool swap = SwapDirection(style, start_tile, end_tile); Darkvater@4834: byte style_t; Darkvater@4834: uint h0, h1, ht; // start heigth, end height, and temp variable Darkvater@4834: Darkvater@4834: if (start_tile == end_tile) return 0; tron@5984: if (swap) Swap(start_tile, end_tile); Darkvater@4834: Darkvater@4834: switch (style & HT_DRAG_MASK) { Darkvater@4834: case HT_RECT: { Darkvater@4834: static const TileIndexDiffC heightdiff_area_by_dir[] = { Darkvater@4834: /* Start */ {1, 0}, /* Dragging east */ {0, 0}, /* Dragging south */ Darkvater@4834: /* End */ {0, 1}, /* Dragging east */ {1, 1} /* Dragging south */ Darkvater@4834: }; Darkvater@4834: Darkvater@4834: /* In the case of an area we can determine whether we were dragging south or Darkvater@4834: * east by checking the X-coordinates of the tiles */ Darkvater@4834: style_t = (byte)(TileX(end_tile) > TileX(start_tile)); Darkvater@4834: start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t])); Darkvater@4834: end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t])); Darkvater@4834: } Darkvater@4834: /* Fallthrough */ Darkvater@4834: case HT_POINT: Darkvater@4834: h0 = TileHeight(start_tile); Darkvater@4834: h1 = TileHeight(end_tile); Darkvater@4834: break; Darkvater@4834: default: { /* All other types, this is mostly only line/autorail */ Darkvater@4834: static const HighLightStyle flip_style_direction[] = { Darkvater@4834: HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL Darkvater@4834: }; Darkvater@4834: static const TileIndexDiffC heightdiff_line_by_dir[] = { Darkvater@4834: /* Start */ {1, 0}, {1, 1}, /* HT_DIR_X */ {0, 1}, {1, 1}, /* HT_DIR_Y */ Darkvater@4834: /* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, /* HT_DIR_HL */ Darkvater@4834: /* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, /* HT_DIR_VR */ Darkvater@4834: Darkvater@4834: /* Start */ {0, 1}, {0, 0}, /* HT_DIR_X */ {1, 0}, {0, 0}, /* HT_DIR_Y */ Darkvater@4834: /* End */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, /* HT_DIR_HL */ Darkvater@4834: /* End */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, /* HT_DIR_VR */ Darkvater@4834: }; Darkvater@4834: Darkvater@4834: distance %= 2; // we're only interested if the distance is even or uneven Darkvater@4834: style &= HT_DIR_MASK; Darkvater@4834: Darkvater@4834: /* To handle autorail, we do some magic to be able to use a lookup table. Darkvater@4834: * Firstly if we drag the other way around, we switch start&end, and if needed Darkvater@4834: * also flip the drag-position. Eg if it was on the left, and the distance is even Darkvater@4834: * that means the end, which is now the start is on the right */ Darkvater@4834: if (swap && distance == 0) style = flip_style_direction[style]; Darkvater@4834: Darkvater@4834: /* Use lookup table for start-tile based on HighLightStyle direction */ Darkvater@4834: style_t = style * 2; Darkvater@4834: assert(style_t < lengthof(heightdiff_line_by_dir) - 13); Darkvater@4834: h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t]))); Darkvater@4834: ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1]))); celestar@5852: h0 = max(h0, ht); Darkvater@4834: Darkvater@4834: /* Use lookup table for end-tile based on HighLightStyle direction Darkvater@4834: * flip around side (lower/upper, left/right) based on distance */ Darkvater@4834: if (distance == 0) style_t = flip_style_direction[style] * 2; Darkvater@4834: assert(style_t < lengthof(heightdiff_line_by_dir) - 13); Darkvater@4834: h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t]))); Darkvater@4834: ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1]))); celestar@5852: h1 = max(h1, ht); Darkvater@4834: } break; Darkvater@4834: } Darkvater@4834: tron@5984: if (swap) Swap(h0, h1); Darkvater@4834: /* Minimap shows height in intervals of 50 meters, let's do the same */ Darkvater@4834: return (int)(h1 - h0) * 50; Darkvater@4834: } dominik@1070: glx@4885: static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF}; Darkvater@4884: belugas@6919: /** while dragging */ dominik@1070: static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method) truelight@0: { Darkvater@4799: HighLightStyle b; Darkvater@4799: uint w, h; truelight@193: Darkvater@4799: int dx = thd->selstart.x - (thd->selend.x & ~0xF); Darkvater@4799: int dy = thd->selstart.y - (thd->selend.y & ~0xF); skidd13@8419: w = abs(dx) + 16; skidd13@8419: h = abs(dy) + 16; truelight@0: tron@1980: if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile tron@4077: if (method == VPM_RAILDIRS) { tron@2116: b = GetAutorailHT(x, y); tron@4077: } else { // rect for autosignals on one tile tron@2116: b = HT_RECT; tron@4077: } dominik@1070: } else if (h == 16) { // Is this in X direction? tron@4077: if (dx == 16) { // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(3)) | HT_LINE; tron@4077: } else if (dx == -16) { dominik@1070: b = (Check2x1AutoRail(2)) | HT_LINE; tron@4077: } else { dominik@1070: b = HT_LINE | HT_DIR_X; tron@4077: } dominik@1070: y = thd->selstart.y; dominik@1070: } else if (w == 16) { // Or Y direction? tron@4077: if (dy == 16) { // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(1)) | HT_LINE; tron@4077: } else if (dy == -16) { // 2x1 other direction dominik@1070: b = (Check2x1AutoRail(0)) | HT_LINE; tron@4077: } else { dominik@1070: b = HT_LINE | HT_DIR_Y; tron@4077: } truelight@0: x = thd->selstart.x; dominik@1070: } else if (w > h * 2) { // still count as x dir? tron@2116: b = HT_LINE | HT_DIR_X; truelight@0: y = thd->selstart.y; dominik@1070: } else if (h > w * 2) { // still count as y dir? tron@2116: b = HT_LINE | HT_DIR_Y; dominik@1070: x = thd->selstart.x; dominik@1070: } else { // complicated direction Darkvater@4799: int d = w - h; tron@2116: thd->selend.x = thd->selend.x & ~0xF; tron@2116: thd->selend.y = thd->selend.y & ~0xF; truelight@0: truelight@0: // four cases. truelight@0: if (x > thd->selstart.x) { truelight@0: if (y > thd->selstart.y) { truelight@0: // south tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: // return px == py || px == py + 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: } // return px == py || px == py - 16; truelight@0: } else { truelight@0: // west tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: } truelight@0: } truelight@0: } else { truelight@0: if (y > thd->selstart.y) { truelight@0: // east tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: // return px == -py || px == -py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } // return px == -py || px == -py + 16; truelight@0: } else { truelight@0: // north tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: // return px == py || px == py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: } //return px == py || px == py + 16; truelight@0: } truelight@0: } truelight@0: } Darkvater@4834: Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4834: TileIndex t0 = TileVirtXY(thd->selstart.x, thd->selstart.y); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); Darkvater@4834: uint distance = DistanceManhattan(t0, t1) + 1; Darkvater@4884: byte index = 0; rubidium@7502: uint64 params[2]; Darkvater@4834: Darkvater@4884: if (distance != 1) { Darkvater@4884: int heightdiff = CalcHeightdiff(b, distance, t0, t1); Darkvater@4884: /* If we are showing a tooltip for horizontal or vertical drags, Darkvater@4884: * 2 tiles have a length of 1. To bias towards the ceiling we add Darkvater@4884: * one before division. It feels more natural to count 3 lengths as 2 */ Darkvater@4884: if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) { Darkvater@4884: distance = (distance + 1) / 2; Darkvater@4884: } Darkvater@4884: Darkvater@4884: params[index++] = distance; Darkvater@4884: if (heightdiff != 0) params[index++] = heightdiff; Darkvater@4834: } Darkvater@4834: Darkvater@4884: GuiShowTooltipsWithArgs(measure_strings_length[index], index, params); Darkvater@4834: } Darkvater@4834: truelight@0: thd->selend.x = x; truelight@0: thd->selend.y = y; truelight@0: thd->next_drawstyle = b; truelight@0: } truelight@0: Darkvater@4799: /** Darkvater@4799: * Selects tiles while dragging Darkvater@4799: * @param x X coordinate of end of selection Darkvater@4799: * @param y Y coordinate of end of selection Darkvater@4799: * @param method modifies the way tiles are selected. Possible Darkvater@4799: * methods are VPM_* in viewport.h */ rubidium@8384: void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method) truelight@0: { Darkvater@4799: int sx, sy; Darkvater@4834: HighLightStyle style; tron@1863: truelight@0: if (x == -1) { tron@1863: _thd.selend.x = -1; truelight@0: return; truelight@0: } truelight@0: Darkvater@4799: /* Special handling of drag in any (8-way) direction */ darkvater@58: if (method == VPM_RAILDIRS || method == VPM_SIGNALDIRS) { tron@1863: _thd.selend.x = x; tron@1863: _thd.selend.y = y; tron@1863: CalcRaildirsDrawstyle(&_thd, x, y, method); truelight@0: return; truelight@0: } truelight@0: smatz@8408: /* Needed so level-land is placed correctly */ tron@2116: if (_thd.next_drawstyle == HT_POINT) { Darkvater@4799: x += TILE_SIZE / 2; Darkvater@4799: y += TILE_SIZE / 2; tron@2116: } truelight@0: tron@1863: sx = _thd.selstart.x; tron@1863: sy = _thd.selstart.y; truelight@0: tron@2116: switch (method) { Darkvater@4834: case VPM_X_OR_Y: /* drag in X or Y direction */ skidd13@8419: if (abs(sy - y) < abs(sx - x)) { tron@4077: y = sy; Darkvater@4834: style = HT_DIR_X; tron@4077: } else { tron@4077: x = sx; Darkvater@4834: style = HT_DIR_Y; tron@4077: } Darkvater@4834: goto calc_heightdiff_single_direction; Darkvater@4834: case VPM_FIX_X: /* drag in Y direction */ Darkvater@4834: x = sx; Darkvater@4834: style = HT_DIR_Y; Darkvater@4834: goto calc_heightdiff_single_direction; Darkvater@4834: case VPM_FIX_Y: /* drag in X direction */ Darkvater@4834: y = sy; Darkvater@4834: style = HT_DIR_X; Darkvater@4834: Darkvater@4834: calc_heightdiff_single_direction:; Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4834: TileIndex t0 = TileVirtXY(sx, sy); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); Darkvater@4834: uint distance = DistanceManhattan(t0, t1) + 1; Darkvater@4884: byte index = 0; rubidium@7502: uint64 params[2]; Darkvater@4834: Darkvater@4884: if (distance != 1) { Darkvater@4884: /* With current code passing a HT_LINE style to calculate the height Darkvater@4884: * difference is enough. However if/when a point-tool is created Darkvater@4884: * with this method, function should be called with new_style (below) Darkvater@4884: * instead of HT_LINE | style case HT_POINT is handled specially Darkvater@4884: * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */ Darkvater@4884: int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1); Darkvater@4884: Darkvater@4884: params[index++] = distance; Darkvater@4884: if (heightdiff != 0) params[index++] = heightdiff; Darkvater@4834: } Darkvater@4884: Darkvater@4884: GuiShowTooltipsWithArgs(measure_strings_length[index], index, params); Darkvater@4834: } break; Darkvater@4834: Darkvater@4834: case VPM_X_AND_Y_LIMITED: { /* drag an X by Y constrained rect area */ Darkvater@4834: int limit = (_thd.sizelimit - 1) * TILE_SIZE; skidd13@8418: x = sx + Clamp(x - sx, -limit, limit); skidd13@8418: y = sy + Clamp(y - sy, -limit, limit); rubidium@5838: } /* Fallthrough */ rubidium@5838: case VPM_X_AND_Y: { /* drag an X by Y area */ Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4884: static const StringID measure_strings_area[] = { Darkvater@4884: STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF Darkvater@4884: }; Darkvater@4884: Darkvater@4834: TileIndex t0 = TileVirtXY(sx, sy); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); skidd13@8466: uint dx = Delta(TileX(t0), TileX(t1)) + 1; skidd13@8466: uint dy = Delta(TileY(t0), TileY(t1)) + 1; Darkvater@4884: byte index = 0; rubidium@7502: uint64 params[3]; Darkvater@4834: Darkvater@4834: /* If dragging an area (eg dynamite tool) and it is actually a single Darkvater@4834: * row/column, change the type to 'line' to get proper calculation for height */ rubidium@8720: style = (HighLightStyle)_thd.next_drawstyle; Darkvater@4834: if (style & HT_RECT) { Darkvater@4834: if (dx == 1) { Darkvater@4834: style = HT_LINE | HT_DIR_Y; Darkvater@4834: } else if (dy == 1) { Darkvater@4834: style = HT_LINE | HT_DIR_X; Darkvater@4834: } Darkvater@4834: } Darkvater@4834: Darkvater@4884: if (dx != 1 || dy != 1) { Darkvater@4884: int heightdiff = CalcHeightdiff(style, 0, t0, t1); Darkvater@4884: Darkvater@4884: params[index++] = dx; Darkvater@4884: params[index++] = dy; Darkvater@4884: if (heightdiff != 0) params[index++] = heightdiff; Darkvater@4834: } Darkvater@4884: Darkvater@4884: GuiShowTooltipsWithArgs(measure_strings_area[index], index, params); Darkvater@4834: } Darkvater@4884: break; Darkvater@4834: tron@2116: } Darkvater@4834: default: NOT_REACHED(); truelight@0: } truelight@0: tron@1863: _thd.selend.x = x; tron@1863: _thd.selend.y = y; truelight@0: } truelight@0: belugas@6919: /** while dragging */ rubidium@6573: bool VpHandlePlaceSizingDrag() truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@193: tron@2116: if (_special_mouse_mode != WSM_SIZING) return true; truelight@0: maedhros@7165: e.we.place.select_method = _thd.select_method; maedhros@7165: e.we.place.select_proc = _thd.select_proc; truelight@0: belugas@6919: /* stop drag mode if the window has been closed */ rubidium@6987: w = FindWindowById(_thd.window_class, _thd.window_number); truelight@0: if (w == NULL) { truelight@0: ResetObjectToPlace(); truelight@0: return false; truelight@0: } truelight@0: belugas@6919: /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */ truelight@0: if (_left_button_down) { truelight@0: e.event = WE_PLACE_DRAG; belugas@4634: e.we.place.pt = GetTileBelowCursor(); truelight@0: w->wndproc(w, &e); truelight@0: return false; truelight@0: } truelight@0: belugas@6919: /* mouse button released.. belugas@6919: * keep the selected tool, but reset it to the original mode. */ truelight@193: _special_mouse_mode = WSM_NONE; tron@2116: if (_thd.next_drawstyle == HT_RECT) { dominik@1070: _thd.place_mode = VHM_RECT; maedhros@7165: } else if (e.we.place.select_method == VPM_SIGNALDIRS) { // some might call this a hack... -- Dominik tron@2116: _thd.place_mode = VHM_RECT; tron@2116: } else if (_thd.next_drawstyle & HT_LINE) { dominik@1070: _thd.place_mode = VHM_RAIL; tron@2116: } else if (_thd.next_drawstyle & HT_RAIL) { dominik@1070: _thd.place_mode = VHM_RAIL; tron@2116: } else { dominik@1070: _thd.place_mode = VHM_POINT; tron@2116: } truelight@0: SetTileSelectSize(1, 1); truelight@0: belugas@6919: /* and call the mouseup event. */ truelight@0: e.event = WE_PLACE_MOUSEUP; belugas@4634: e.we.place.pt = _thd.selend; belugas@4634: e.we.place.tile = TileVirtXY(e.we.place.pt.x, e.we.place.pt.y); belugas@4634: e.we.place.starttile = TileVirtXY(_thd.selstart.x, _thd.selstart.y); truelight@0: w->wndproc(w, &e); truelight@193: truelight@0: return false; truelight@0: } truelight@0: rubidium@8385: void SetObjectToPlaceWnd(CursorID icon, SpriteID pal, ViewportHighlightMode mode, Window *w) truelight@0: { peter1138@5919: SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number); truelight@0: } truelight@0: truelight@0: #include "table/animcursors.h" truelight@0: rubidium@8385: void SetObjectToPlace(CursorID icon, SpriteID pal, ViewportHighlightMode mode, WindowClass window_class, WindowNumber window_num) truelight@0: { truelight@0: Window *w; truelight@0: smatz@9081: /* undo clicking on button and drag & drop */ smatz@9081: if (_thd.place_mode != VHM_NONE || _special_mouse_mode == WSM_DRAGDROP) { tron@1863: w = FindWindowById(_thd.window_class, _thd.window_number); tron@2116: if (w != NULL) CallWindowEventNP(w, WE_ABORT_PLACE_OBJ); truelight@0: } truelight@193: truelight@0: SetTileSelectSize(1, 1); truelight@193: tron@1863: _thd.make_square_red = false; truelight@0: rubidium@8385: if (mode == VHM_DRAG) { // VHM_DRAG is for dragdropping trains in the depot window rubidium@8385: mode = VHM_NONE; truelight@0: _special_mouse_mode = WSM_DRAGDROP; truelight@0: } else { truelight@0: _special_mouse_mode = WSM_NONE; truelight@0: } truelight@0: tron@1863: _thd.place_mode = mode; tron@1863: _thd.window_class = window_class; tron@1863: _thd.window_number = window_num; truelight@0: dominik@1070: if (mode == VHM_SPECIAL) // special tools, like tunnels or docks start with presizing mode truelight@0: VpStartPreSizing(); truelight@193: truelight@0: if ( (int)icon < 0) Darkvater@4334: SetAnimatedMouseCursor(_animcursors[~icon]); truelight@0: else peter1138@5919: SetMouseCursor(icon, pal); truelight@0: } truelight@0: rubidium@6573: void ResetObjectToPlace() tron@1093: { rubidium@6144: SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0); truelight@0: }