tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file viewport.cpp Handling of all viewports. rubidium@8957: * rubidium@8957: * \verbatim rubidium@8957: * The in-game coordinate system looks like this * rubidium@8957: * * rubidium@8957: * ^ Z * rubidium@8957: * | * rubidium@8957: * | * rubidium@8957: * | * rubidium@8957: * | * rubidium@8957: * / \ * rubidium@8957: * / \ * rubidium@8957: * / \ * rubidium@8957: * / \ * rubidium@8957: * X < > Y * rubidium@8957: * \endverbatim rubidium@8957: */ belugas@6117: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" rubidium@8119: #include "tile_cmd.h" tron@2662: #include "gui.h" tron@1349: #include "spritecache.h" maedhros@6453: #include "landscape.h" rubidium@8224: #include "viewport_func.h" rubidium@8785: #include "station_base.h" truelight@0: #include "town.h" rubidium@8790: #include "signs_base.h" rubidium@8790: #include "signs_func.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" bjarni@2676: #include "train.h" maedhros@6857: #include "roadveh.h" rubidium@7486: #include "vehicle_gui.h" truelight@6937: #include "blitter/factory.hpp" belugas@7849: #include "transparency.h" rubidium@8114: #include "strings_func.h" rubidium@8123: #include "zoom_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8254: #include "player_func.h" rubidium@8270: #include "settings_type.h" rubidium@8785: #include "station_func.h" rubidium@8925: #include "core/alloc_type.hpp" skidd13@9555: #include "core/smallvec_type.hpp" rubidium@9117: #include "window_func.h" rubidium@9127: #include "tilehighlight_func.h" rubidium@9274: #include "window_gui.h" rubidium@8946: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" rubidium@8264: rubidium@8224: PlaceProc *_place_proc; rubidium@8224: Point _tile_fract_coords; truelight@6624: ZoomLevel _saved_scrollpos_zoom; truelight@6624: rubidium@6248: struct StringSpriteToDraw { truelight@0: uint16 string; truelight@0: uint16 color; tron@849: int32 x; tron@849: int32 y; rubidium@7006: uint64 params[2]; truelight@0: uint16 width; rubidium@6248: }; rubidium@6248: rubidium@6248: struct TileSpriteToDraw { peter1138@5668: SpriteID image; peter1138@5668: SpriteID pal; rubidium@7681: const SubSprite *sub; ///< only draw a rectangular part of the sprite tron@849: int32 x; tron@849: int32 y; truelight@0: byte z; rubidium@6248: }; rubidium@6248: rubidium@6248: struct ChildScreenSpriteToDraw { peter1138@5668: SpriteID image; peter1138@5668: SpriteID pal; rubidium@7681: const SubSprite *sub; ///< only draw a rectangular part of the sprite tron@849: int32 x; tron@849: int32 y; frosch@9534: int next; ///< next child to draw (-1 at the end) rubidium@6248: }; rubidium@6248: rubidium@6248: struct ParentSpriteToDraw { belugas@7569: SpriteID image; ///< sprite to draw belugas@7569: SpriteID pal; ///< palette to use rubidium@7681: const SubSprite *sub; ///< only draw a rectangular part of the sprite belugas@7569: rubidium@7580: int32 x; ///< screen X coordinate of sprite rubidium@7580: int32 y; ///< screen Y coordinate of sprite rubidium@7580: belugas@7569: int32 left; ///< minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites belugas@7569: int32 top; ///< minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites belugas@7569: belugas@7569: int32 xmin; ///< minimal world X coordinate of bounding box belugas@7569: int32 xmax; ///< maximal world X coordinate of bounding box belugas@7569: int32 ymin; ///< minimal world Y coordinate of bounding box belugas@7569: int32 ymax; ///< maximal world Y coordinate of bounding box rubidium@7580: int zmin; ///< minimal world Z coordinate of bounding box rubidium@7580: int zmax; ///< maximal world Z coordinate of bounding box belugas@7569: rubidium@8952: int first_child; ///< the first child to draw. rubidium@7580: bool comparison_done; ///< Used during sprite sorting: true if sprite has been compared with all other sprites rubidium@6248: }; truelight@0: rubidium@7769: /* Enumeration of multi-part foundations */ rubidium@7769: enum FoundationPart { rubidium@7769: FOUNDATION_PART_NONE = 0xFF, ///< Neither foundation nor groundsprite drawn yet. rubidium@7769: FOUNDATION_PART_NORMAL = 0, ///< First part (normal foundation or no foundation) rubidium@7769: FOUNDATION_PART_HALFTILE = 1, ///< Second part (halftile foundation) rubidium@7769: FOUNDATION_PART_END rubidium@7769: }; rubidium@7769: peter1138@8949: typedef SmallVector TileSpriteToDrawVector; peter1138@8949: typedef SmallVector StringSpriteToDrawVector; rubidium@8951: typedef SmallVector ParentSpriteToDrawVector; rubidium@8951: typedef SmallVector ParentSpriteToSortVector; rubidium@8952: typedef SmallVector ChildScreenSpriteToDrawVector; rubidium@8946: rubidium@6248: struct ViewportDrawer { truelight@0: DrawPixelInfo dpi; truelight@193: rubidium@8946: StringSpriteToDrawVector string_sprites_to_draw; rubidium@8947: TileSpriteToDrawVector tile_sprites_to_draw; rubidium@8951: ParentSpriteToDrawVector parent_sprites_to_draw; rubidium@8951: ParentSpriteToSortVector parent_sprites_to_sort; rubidium@8952: ChildScreenSpriteToDrawVector child_screen_sprites_to_draw; rubidium@8952: rubidium@8952: int *last_child; truelight@0: truelight@0: byte combine_sprites; truelight@0: rubidium@8952: int foundation[FOUNDATION_PART_END]; ///< Foundation sprites (index into parent_sprites_to_draw). rubidium@8952: FoundationPart foundation_part; ///< Currently active foundation for ground sprite drawing. rubidium@8952: int *last_foundation_child[FOUNDATION_PART_END]; ///< Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw) rubidium@8952: Point foundation_offset[FOUNDATION_PART_END]; ///< Pixeloffset for ground sprites on the foundations. rubidium@6248: }; truelight@0: rubidium@8953: static ViewportDrawer _vd; truelight@0: tron@1863: TileHighlightData _thd; truelight@0: static TileInfo *_cur_ti; rubidium@8979: bool _draw_bounding_boxes = false; 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: Darkvater@5122: void DeleteWindowViewport(Window *w) Darkvater@5122: { glx@9184: free(w->viewport); Darkvater@5122: w->viewport = NULL; Darkvater@5122: } Darkvater@5122: rubidium@8992: /** rubidium@8992: * Initialize viewport of the window for use. rubidium@8992: * @param w Window to use/display the viewport in rubidium@8992: * @param x Offset of left edge of viewport with respect to left edge window \a w rubidium@8992: * @param y Offset of top edge of viewport with respect to top edge window \a w rubidium@8992: * @param width Width of the viewport rubidium@8992: * @param height Height of the viewport rubidium@8992: * @param follow_flags Flags controlling the viewport. rubidium@8992: * - If bit 31 is set, the lower 16 bits are the vehicle that the viewport should follow. rubidium@8992: * - If bit 31 is clear, it is a tile position. rubidium@8992: * @param zoom Zoomlevel to display rubidium@8992: */ rubidium@8994: void InitializeWindowViewport(Window *w, int x, int y, truelight@6624: int width, int height, uint32 follow_flags, ZoomLevel zoom) truelight@0: { rubidium@8957: assert(w->viewport == NULL); rubidium@8957: glx@9184: ViewportData *vp = CallocT(1); 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@6654: vp->virtual_width = ScaleByZoom(width, zoom); truelight@6654: vp->virtual_height = ScaleByZoom(height, zoom); truelight@0: rubidium@8957: Point pt; rubidium@8957: truelight@0: if (follow_flags & 0x80000000) { tron@2116: const Vehicle *veh; tron@2116: glx@9184: vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFF); glx@9184: veh = GetVehicle(vp->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: glx@9184: vp->follow_vehicle = INVALID_VEHICLE; tron@2116: pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y)); truelight@0: } truelight@0: glx@9184: vp->scrollpos_x = pt.x; glx@9184: vp->scrollpos_y = pt.y; glx@9184: vp->dest_scrollpos_x = pt.x; glx@9184: vp->dest_scrollpos_y = pt.y; peter1138@6730: 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@7905: /* viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower) smatz@7905: * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL) smatz@7905: */ smatz@7905: old_left = UnScaleByZoomLower(old_left, vp->zoom); smatz@7905: old_top = UnScaleByZoomLower(old_top, vp->zoom); smatz@7905: x = UnScaleByZoomLower(x, vp->zoom); smatz@7905: 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: rubidium@8992: /** rubidium@8992: * Is a xy position inside the viewport of the window? rubidium@8992: * @param w Window to examine its viewport rubidium@8992: * @param x X coordinate of the xy position rubidium@8992: * @param y Y coordinate of the xy position rubidium@8992: * @return Pointer to the viewport if the xy position is in the viewport of the window, rubidium@8992: * otherwise \c NULL is returned. rubidium@8992: */ 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@8562: IsInsideMM(x, vp->left, vp->left + vp->width) && skidd13@7954: 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@6626: x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> 2; truelight@6626: 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@7922: a = Clamp(a, 0, (int)(MapMaxX() * TILE_SIZE) - 1); skidd13@7922: b = Clamp(b, 0, (int)(MapMaxY() * TILE_SIZE) - 1); Darkvater@5014: rubidium@7764: /* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0. rubidium@7764: * Now find the Z-world coordinate by fix point iteration. rubidium@7764: * This is a bit tricky because the tile height is non-continuous at foundations. rubidium@7764: * The clicked point should be approached from the back, otherwise there are regions that are not clickable. rubidium@7764: * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely) rubidium@7764: * So give it a z-malus of 4 in the first iterations. rubidium@7764: */ rubidium@7764: z = 0; rubidium@7764: for (int i = 0; i < 5; i++) z = GetSlopeZ(a + max(z, 4u) - 4, b + max(z, 4u) - 4) / 2; rubidium@7764: for (uint malus = 3; malus > 0; malus--) z = GetSlopeZ(a + max(z, malus) - malus, b + max(z, malus) - malus) / 2; rubidium@7764: 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@6247: 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; glx@9184: ViewPort *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@6443: * @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@7997: w->SetWidgetDisabledState(widget_zoom_in, vp->zoom == ZOOM_LVL_MIN); glx@8028: w->InvalidateWidget(widget_zoom_in); Darkvater@5045: rubidium@7997: w->SetWidgetDisabledState(widget_zoom_out, vp->zoom == ZOOM_LVL_MAX); glx@8028: w->InvalidateWidget(widget_zoom_out); Darkvater@5045: } Darkvater@5045: rubidium@7681: /** rubidium@7681: * Draws a ground sprite at a specific world-coordinate. rubidium@7681: * rubidium@7681: * @param image the image to draw. rubidium@7681: * @param pal the provided palette. rubidium@7681: * @param x position x of the sprite. rubidium@7681: * @param y position y of the sprite. rubidium@7681: * @param z position z of the sprite. rubidium@7681: * @param sub Only draw a part of the sprite. rubidium@7681: * rubidium@7681: */ rubidium@7681: void DrawGroundSpriteAt(SpriteID image, SpriteID pal, int32 x, int32 y, byte z, const SubSprite *sub) truelight@0: { celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: rubidium@8953: TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append(); peter1138@8949: ts->image = image; peter1138@8949: ts->pal = pal; peter1138@8949: ts->sub = sub; peter1138@8949: ts->x = x; peter1138@8949: ts->y = y; peter1138@8949: ts->z = z; truelight@0: } truelight@0: rubidium@7681: /** rubidium@7726: * Adds a child sprite to the active foundation. rubidium@7726: * rubidium@7726: * The pixel offset of the sprite relative to the ParentSprite is the sum of the offset passed to OffsetGroundSprite() and extra_offs_?. rubidium@7726: * rubidium@7726: * @param image the image to draw. rubidium@7726: * @param pal the provided palette. rubidium@7726: * @param sub Only draw a part of the sprite. rubidium@7769: * @param foundation_part Foundation part. rubidium@7726: * @param extra_offs_x Pixel X offset for the sprite position. rubidium@7726: * @param extra_offs_y Pixel Y offset for the sprite position. rubidium@7726: */ rubidium@7769: static void AddChildSpriteToFoundation(SpriteID image, SpriteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y) rubidium@7726: { skidd13@7954: assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END)); rubidium@8953: assert(_vd.foundation[foundation_part] != -1); rubidium@8953: Point offs = _vd.foundation_offset[foundation_part]; rubidium@7726: rubidium@7726: /* Change the active ChildSprite list to the one of the foundation */ rubidium@8953: int *old_child = _vd.last_child; rubidium@8953: _vd.last_child = _vd.last_foundation_child[foundation_part]; rubidium@7726: rubidium@7726: AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub); rubidium@7726: rubidium@7726: /* Switch back to last ChildSprite list */ rubidium@8953: _vd.last_child = old_child; rubidium@7726: } rubidium@7726: rubidium@7726: /** rubidium@7681: * Draws a ground sprite for the current tile. rubidium@7681: * If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite. rubidium@7681: * rubidium@7681: * @param image the image to draw. rubidium@7681: * @param pal the provided palette. rubidium@7681: * @param sub Only draw a part of the sprite. rubidium@7681: */ rubidium@7681: void DrawGroundSprite(SpriteID image, SpriteID pal, const SubSprite *sub) truelight@0: { rubidium@7769: /* Switch to first foundation part, if no foundation was drawn */ rubidium@8953: if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL; rubidium@8953: rubidium@8953: if (_vd.foundation[_vd.foundation_part] != -1) { rubidium@8953: AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, 0, 0); truelight@0: } else { rubidium@7681: DrawGroundSpriteAt(image, pal, _cur_ti->x, _cur_ti->y, _cur_ti->z, sub); truelight@0: } truelight@0: } truelight@0: truelight@0: rubidium@7726: /** rubidium@7726: * Called when a foundation has been drawn for the current tile. rubidium@7726: * Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites. rubidium@7726: * rubidium@7726: * @param x sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite. rubidium@7726: * @param y sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite. rubidium@7726: */ truelight@0: void OffsetGroundSprite(int x, int y) truelight@0: { rubidium@7769: /* Switch to next foundation part */ rubidium@8953: switch (_vd.foundation_part) { rubidium@7769: case FOUNDATION_PART_NONE: rubidium@8953: _vd.foundation_part = FOUNDATION_PART_NORMAL; rubidium@7769: break; rubidium@7769: case FOUNDATION_PART_NORMAL: rubidium@8953: _vd.foundation_part = FOUNDATION_PART_HALFTILE; rubidium@7769: break; rubidium@7769: default: NOT_REACHED(); rubidium@7769: } rubidium@7726: rubidium@8953: /* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */ skidd13@9553: if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1; rubidium@8953: rubidium@8953: _vd.foundation_offset[_vd.foundation_part].x = x; rubidium@8953: _vd.foundation_offset[_vd.foundation_part].y = y; rubidium@8953: _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child; truelight@0: } truelight@0: rubidium@7681: /** rubidium@7681: * Adds a child sprite to a parent sprite. rubidium@7681: * In contrast to "AddChildSpriteScreen()" the sprite position is in world coordinates rubidium@7681: * rubidium@7681: * @param image the image to draw. rubidium@7681: * @param pal the provided palette. rubidium@7681: * @param x position x of the sprite. rubidium@7681: * @param y position y of the sprite. rubidium@7681: * @param z position z of the sprite. rubidium@7681: * @param sub Only draw a part of the sprite. rubidium@7681: */ rubidium@7681: static void AddCombinedSprite(SpriteID image, SpriteID pal, int x, int y, byte z, const SubSprite *sub) truelight@0: { truelight@0: Point pt = RemapCoords(x, y, z); tron@2319: const Sprite* spr = GetSprite(image & SPRITE_MASK); truelight@0: rubidium@8953: if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width || rubidium@8953: pt.x + spr->x_offs + spr->width <= _vd.dpi.left || rubidium@8953: pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height || rubidium@8953: pt.y + spr->y_offs + spr->height <= _vd.dpi.top) truelight@0: return; truelight@0: rubidium@8953: const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1; rubidium@8951: AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub); truelight@0: } truelight@0: rubidium@7580: /** Draw a (transparent) sprite at given coordinates with a given bounding box. rubidium@7580: * 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@7617: * Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices. rubidium@7580: * rubidium@7580: * @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@7580: * defined by the sprite offset in the grf file. rubidium@7580: * 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@7580: * rubidium@7617: * @pre w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored. rubidium@7580: * rubidium@7333: * @param image the image to combine and draw, rubidium@7333: * @param pal the provided palette, rubidium@7580: * @param x position X (world) of the sprite, rubidium@7580: * @param y position Y (world) of the sprite, rubidium@7580: * @param w bounding box extent towards positive X (world), rubidium@7580: * @param h bounding box extent towards positive Y (world), rubidium@7580: * @param dz bounding box extent towards positive Z (world), rubidium@7580: * @param z position Z (world) of the sprite, rubidium@7580: * @param transparent if true, switch the palette between the provided palette and the transparent palette, rubidium@7580: * @param bb_offset_x bounding box extent towards negative X (world), rubidium@7580: * @param bb_offset_y bounding box extent towards negative Y (world), rubidium@7580: * @param bb_offset_z bounding box extent towards negative Z (world) rubidium@7681: * @param sub Only draw a part of the sprite. rubidium@7333: */ rubidium@7681: 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: { rubidium@7643: int32 left, right, top, bottom; truelight@0: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: rubidium@7333: /* make the sprites transparent with the right palette */ rubidium@7333: if (transparent) { skidd13@7931: SetBit(image, PALETTE_MODIFIER_TRANSPARENT); rubidium@7333: pal = PALETTE_TO_TRANSPARENT; rubidium@7333: } rubidium@7333: rubidium@8953: if (_vd.combine_sprites == 2) { rubidium@7681: AddCombinedSprite(image, pal, x, y, z, sub); truelight@0: return; truelight@0: } truelight@0: rubidium@8953: _vd.last_child = NULL; truelight@0: rubidium@8951: Point pt = RemapCoords(x, y, z); rubidium@8951: int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y; rubidium@7643: rubidium@7643: /* Compute screen extents of sprite */ rubidium@7601: if (image == SPR_EMPTY_BOUNDING_BOX) { rubidium@8951: left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x; rubidium@7643: right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1; rubidium@8951: top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y; rubidium@7643: bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1; rubidium@7601: } else { rubidium@7601: const Sprite *spr = GetSprite(image & SPRITE_MASK); rubidium@8951: left = tmp_left = (pt.x += spr->x_offs); rubidium@7643: right = (pt.x + spr->width ); rubidium@8951: top = tmp_top = (pt.y += spr->y_offs); rubidium@7643: bottom = (pt.y + spr->height); rubidium@7601: } belugas@8562: rubidium@7643: if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) { rubidium@7643: /* Compute maximal extents of sprite and it's bounding box */ rubidium@7643: left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x); rubidium@7643: right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1); rubidium@7643: top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y); rubidium@7643: bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1); rubidium@7643: } belugas@8562: rubidium@7643: /* Do not add the sprite to the viewport, if it is outside */ rubidium@8953: if (left >= _vd.dpi.left + _vd.dpi.width || rubidium@8953: right <= _vd.dpi.left || rubidium@8953: top >= _vd.dpi.top + _vd.dpi.height || rubidium@8953: bottom <= _vd.dpi.top) { tron@4189: return; tron@4189: } tron@4189: rubidium@8953: ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append(); rubidium@8951: ps->x = tmp_x; rubidium@8951: ps->y = tmp_y; rubidium@8951: rubidium@8951: ps->left = tmp_left; rubidium@8951: ps->top = tmp_top; truelight@0: truelight@0: ps->image = image; peter1138@5668: ps->pal = pal; rubidium@7681: ps->sub = sub; rubidium@7580: ps->xmin = x + bb_offset_x; rubidium@7617: ps->xmax = x + max(bb_offset_x, w) - 1; rubidium@7580: rubidium@7580: ps->ymin = y + bb_offset_y; rubidium@7617: ps->ymax = y + max(bb_offset_y, h) - 1; rubidium@7580: rubidium@7580: ps->zmin = z + bb_offset_z; rubidium@7617: ps->zmax = z + max(bb_offset_z, dz) - 1; rubidium@7580: rubidium@7580: ps->comparison_done = false; frosch@9534: ps->first_child = -1; frosch@9534: frosch@9534: _vd.last_child = &ps->first_child; rubidium@8953: rubidium@8953: if (_vd.combine_sprites == 1) _vd.combine_sprites = 2; truelight@0: } truelight@0: rubidium@6247: void StartSpriteCombine() truelight@0: { rubidium@8953: _vd.combine_sprites = 1; truelight@0: } truelight@0: rubidium@6247: void EndSpriteCombine() truelight@0: { rubidium@8953: _vd.combine_sprites = 0; truelight@0: } truelight@0: rubidium@7681: /** rubidium@7681: * Add a child sprite to a parent sprite. rubidium@7681: * rubidium@7681: * @param image the image to draw. rubidium@7681: * @param pal the provided palette. rubidium@7681: * @param x sprite x-offset (screen coordinates) relative to parent sprite. rubidium@7681: * @param y sprite y-offset (screen coordinates) relative to parent sprite. rubidium@7681: * @param transparent if true, switch the palette between the provided palette and the transparent palette, rubidium@7681: * @param sub Only draw a part of the sprite. rubidium@7681: */ rubidium@7681: void AddChildSpriteScreen(SpriteID image, SpriteID pal, int x, int y, bool transparent, const SubSprite *sub) truelight@0: { celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: rubidium@8952: /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */ rubidium@8953: if (_vd.last_child == NULL) return; rubidium@8952: rubidium@7659: /* make the sprites transparent with the right palette */ rubidium@7659: if (transparent) { skidd13@7931: SetBit(image, PALETTE_MODIFIER_TRANSPARENT); rubidium@7659: pal = PALETTE_TO_TRANSPARENT; rubidium@7659: } rubidium@7659: skidd13@9553: *_vd.last_child = _vd.child_screen_sprites_to_draw.Length(); frosch@9534: rubidium@8953: ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append(); truelight@0: cs->image = image; peter1138@5668: cs->pal = pal; rubidium@7681: cs->sub = sub; truelight@0: cs->x = x; truelight@0: cs->y = y; frosch@9534: cs->next = -1; frosch@9534: frosch@9534: /* Append the sprite to the active ChildSprite list. frosch@9534: * If the active ParentSprite is a foundation, update last_foundation_child as well. frosch@9534: * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */ frosch@9534: if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next; frosch@9534: if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next; frosch@9534: _vd.last_child = &cs->next; truelight@0: } truelight@0: truelight@0: /* Returns a StringSpriteToDraw */ rubidium@8946: void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, uint16 color, uint16 width) truelight@0: { rubidium@8953: StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append(); peter1138@8949: ss->string = string; peter1138@8949: ss->x = x; peter1138@8949: ss->y = y; peter1138@8949: ss->params[0] = params_1; peter1138@8949: ss->params[1] = params_2; peter1138@8949: ss->width = width; peter1138@8949: ss->color = color; truelight@0: } truelight@0: tron@4000: rubidium@7726: /** rubidium@7726: * Draws sprites between ground sprite and everything above. rubidium@7726: * rubidium@7726: * The sprite is either drawn as TileSprite or as ChildSprite of the active foundation. rubidium@7726: * rubidium@7726: * @param image the image to draw. rubidium@7726: * @param pal the provided palette. rubidium@7726: * @param ti TileInfo Tile that is being drawn rubidium@7726: * @param z_offset Z offset relative to the groundsprite. Only used for the sprite position, not for sprite sorting. rubidium@7769: * @param foundation_part Foundation part the sprite belongs to. rubidium@7726: */ rubidium@7769: static void DrawSelectionSprite(SpriteID image, SpriteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part) truelight@0: { rubidium@7726: /* FIXME: This is not totally valid for some autorail highlights, that extent over the edges of the tile. */ rubidium@8953: if (_vd.foundation[foundation_part] == -1) { rubidium@7726: /* draw on real ground */ rubidium@7726: DrawGroundSpriteAt(image, pal, ti->x, ti->y, ti->z + z_offset); rubidium@7726: } else { rubidium@7726: /* draw on top of foundation */ rubidium@7769: AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset); dominik@1083: } truelight@0: } truelight@0: rubidium@7679: /** rubidium@7679: * Draws a selection rectangle on a tile. rubidium@7679: * rubidium@7679: * @param ti TileInfo Tile that is being drawn rubidium@7679: * @param pal Palette to apply. rubidium@7679: */ rubidium@7679: static void DrawTileSelectionRect(const TileInfo *ti, SpriteID pal) rubidium@7679: { rubidium@7769: SpriteID sel; rubidium@7769: if (IsHalftileSlope(ti->tileh)) { rubidium@7769: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@7769: SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner; rubidium@7769: DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE); rubidium@7769: rubidium@7769: Corner opposite_corner = OppositeCorner(halftile_corner); rubidium@7769: if (IsSteepSlope(ti->tileh)) { rubidium@7769: sel = SPR_HALFTILE_SELECTION_DOWN; rubidium@7769: } else { rubidium@7769: sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT); rubidium@7769: } rubidium@7769: sel += opposite_corner; rubidium@7769: } else { rubidium@7769: sel = SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]; rubidium@7769: } rubidium@7769: DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL); rubidium@7679: } rubidium@7679: 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@8224: if ((_thd.drawstyle & ~HT_DIR_MASK) != HT_LINE) return false; rubidium@8224: rubidium@8224: switch (_thd.drawstyle & HT_DIR_MASK) { rubidium@8224: case HT_DIR_X: return py == 0; // x direction rubidium@8224: case HT_DIR_Y: return px == 0; // y direction rubidium@8224: case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper rubidium@8224: case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower rubidium@8224: case HT_DIR_VL: return px == py || px == py + 16; // vertival left rubidium@8224: case HT_DIR_VR: return px == py || px == py - 16; // vertical right rubidium@8224: default: rubidium@8224: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: dominik@1070: // [direction][side] rubidium@8224: 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@7078: /** rubidium@7679: * Draws autorail highlights. rubidium@7679: * rubidium@7679: * @param *ti TileInfo Tile that is being drawn rubidium@7679: * @param autorail_type Offset into _AutorailTilehSprite[][] rubidium@7679: */ rubidium@7679: static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type) rubidium@7679: { rubidium@7679: SpriteID image; rubidium@7679: SpriteID pal; rubidium@7679: int offset; rubidium@7679: rubidium@7769: FoundationPart foundation_part = FOUNDATION_PART_NORMAL; frosch@8413: Slope autorail_tileh = RemoveHalftileSlope(ti->tileh); rubidium@7769: if (IsHalftileSlope(ti->tileh)) { rubidium@7769: static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U }; rubidium@7769: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@7769: if (autorail_type != _lower_rail[halftile_corner]) { rubidium@7769: foundation_part = FOUNDATION_PART_HALFTILE; rubidium@7769: /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */ rubidium@7769: autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner)); rubidium@7769: } rubidium@7769: } rubidium@7769: rubidium@7769: offset = _AutorailTilehSprite[autorail_tileh][autorail_type]; rubidium@7679: if (offset >= 0) { rubidium@7679: image = SPR_AUTORAIL_BASE + offset; rubidium@7679: pal = PAL_NONE; rubidium@7679: } else { rubidium@7679: image = SPR_AUTORAIL_BASE - offset; rubidium@7679: pal = PALETTE_SEL_TILE_RED; rubidium@7679: } rubidium@7679: rubidium@7769: DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part); rubidium@7679: } rubidium@7679: rubidium@7679: /** belugas@7078: * Checks if the specified tile is selected and if so draws selection using correct selectionstyle. belugas@7078: * @param *ti TileInfo Tile that is being drawn belugas@7078: */ truelight@0: static void DrawTileSelection(const TileInfo *ti) truelight@0: { belugas@6423: /* Draw a red error square? */ rubidium@8743: bool is_redsq = _thd.redsq != 0 && _thd.redsq == ti->tile; rubidium@8743: if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING); truelight@0: belugas@6423: /* no selection active? */ tron@2116: if (_thd.drawstyle == 0) return; truelight@0: belugas@6423: /* Inside the inner area? */ skidd13@7954: if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) && skidd13@7954: IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) { tron@1863: if (_thd.drawstyle & HT_RECT) { rubidium@8743: if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE); tron@1863: } else if (_thd.drawstyle & HT_POINT) { belugas@6423: /* Figure out the Z coordinate for the single dot. */ rubidium@7726: byte z = 0; rubidium@7769: FoundationPart foundation_part = FOUNDATION_PART_NORMAL; tron@3636: if (ti->tileh & SLOPE_N) { tron@3645: z += TILE_HEIGHT; frosch@8413: if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT; truelight@0: } rubidium@7769: if (IsHalftileSlope(ti->tileh)) { rubidium@7769: Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh); rubidium@7769: if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT; rubidium@7769: if (halftile_corner != CORNER_S) { rubidium@7769: foundation_part = FOUNDATION_PART_HALFTILE; rubidium@7769: if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT; rubidium@7769: } rubidium@7769: } rubidium@7769: 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@6423: /* autorail highlight piece under cursor */ tron@2116: uint type = _thd.drawstyle & 0xF; tron@2116: assert(type <= 5); rubidium@8224: DrawAutorailSelection(ti, _autorail_type[type][0]); tron@2116: } else if (IsPartOfAutoLine(ti->x, ti->y)) { belugas@6423: /* 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@7970: side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile))); tron@2116: } tron@1109: rubidium@8224: DrawAutorailSelection(ti, _autorail_type[dir][side]); tron@2116: } truelight@0: return; truelight@0: } truelight@0: belugas@6423: /* Check if it's inside the outer area? */ rubidium@8743: if (!is_redsq && _thd.outersize.x && tron@1863: _thd.size.x < _thd.size.x + _thd.outersize.x && skidd13@7954: IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) && skidd13@7954: IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) { belugas@6423: /* Draw a blue rect. */ rubidium@7679: DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE); truelight@0: return; truelight@0: } truelight@0: } truelight@0: rubidium@6247: static void ViewportAddLandscape() truelight@0: { 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@6423: /* Transform into tile coordinates and round to closest full tile */ rubidium@8953: x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~0xF; rubidium@8953: y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - 0x10) & ~0xF; peter1138@5501: belugas@6423: /* determine size of area */ truelight@0: { truelight@0: Point pt = RemapCoords(x, y, 241); rubidium@8953: width = (_vd.dpi.left + _vd.dpi.width - pt.x + 95) >> 6; rubidium@8953: 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@5501: rubidium@8953: _vd.foundation_part = FOUNDATION_PART_NONE; rubidium@8953: _vd.foundation[0] = -1; rubidium@8953: _vd.foundation[1] = -1; rubidium@8953: _vd.last_foundation_child[0] = NULL; rubidium@8953: _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@5501: if ((direction ^= 1) != 0) { truelight@0: y += 0x10; peter1138@5501: } else { truelight@0: x += 0x10; peter1138@5501: } 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@7928: 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@6624: 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, rubidium@9413: _settings_client.gui.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@6624: 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 && rubidium@8969: left < t->sign.left + t->sign.width_1 * 2) { tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, rubidium@9413: _settings_client.gui.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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: right += ScaleByZoom(1, dpi->zoom); truelight@6653: bottom += ScaleByZoom(1, dpi->zoom) + 1; tron@5027: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && truelight@6653: top < t->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > t->sign.left && truelight@6653: 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@6653: truelight@6653: 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: { rubidium@8946: AddStringToDraw(st->sign.left + 1, st->sign.top + 1, str, st->index, st->facilities, (st->owner == OWNER_NONE || st->facilities == 0) ? 0xE : _player_colors[st->owner], width); 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@7928: 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@6624: 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@6624: 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 && rubidium@8969: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: right += ScaleByZoom(1, dpi->zoom); truelight@6653: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@6653: tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && truelight@6653: top < st->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > st->sign.left && truelight@6653: 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@6653: truelight@6653: 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: { rubidium@8946: AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->index, 0, (si->owner == OWNER_NONE) ? 14 : _player_colors[si->owner], width); 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: smatz@8806: /* Signs are turned off or are invisible */ smatz@8806: if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS)) 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@6624: 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@6624: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: right += ScaleByZoom(1, dpi->zoom); truelight@6653: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@6653: tron@5027: FOR_ALL_SIGNS(si) { tron@5027: if (bottom > si->sign.top && truelight@6653: top < si->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > si->sign.left && truelight@6653: left < si->sign.left + ScaleByZoom(si->sign.width_2, dpi->zoom)) { rubidium@7951: AddSign(si, IsTransparencySet(TO_SIGNS) ? STR_2002_WHITE : STR_2002, si->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@6653: truelight@6653: 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: { rubidium@8946: AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, str, wp->index, 0, (wp->deleted ? 0xE : 11), width); 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@7928: 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@6624: 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@6624: 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 && rubidium@8969: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: right += ScaleByZoom(1, dpi->zoom); truelight@6653: bottom += ScaleByZoom(1, dpi->zoom) + 1; truelight@6653: tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && truelight@6653: top < wp->sign.top + ScaleByZoom(12, dpi->zoom) && tron@5027: right > wp->sign.left && truelight@6653: 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@6653: truelight@6653: 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: rubidium@8947: static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv) truelight@193: { peter1138@8949: const TileSpriteToDraw *tsend = tstdv->End(); peter1138@8949: for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) { truelight@0: Point pt = RemapCoords(ts->x, ts->y, ts->z); rubidium@7681: DrawSprite(ts->image, ts->pal, pt.x, pt.y, ts->sub); rubidium@8947: } truelight@0: } truelight@0: rubidium@8951: static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv) truelight@0: { rubidium@8951: ParentSpriteToDraw **psdvend = psdv->End(); rubidium@8951: ParentSpriteToDraw **psd = psdv->Begin(); rubidium@8951: while (psd != psdvend) { rubidium@8948: ParentSpriteToDraw *ps = *psd; rubidium@8948: smatz@8950: if (ps->comparison_done) { smatz@8950: psd++; smatz@8950: continue; smatz@8950: } rubidium@8948: rubidium@8948: ps->comparison_done = true; rubidium@8948: rubidium@8951: for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) { rubidium@8948: ParentSpriteToDraw *ps2 = *psd2; rubidium@8948: rubidium@8948: if (ps2->comparison_done) continue; rubidium@8948: rubidium@8948: /* Decide which comparator to use, based on whether the bounding rubidium@8948: * boxes overlap rubidium@8948: */ rubidium@8948: if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X? rubidium@8948: ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y? rubidium@8948: ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z? rubidium@8948: /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of rubidium@8948: * the screen and with higher Z elevation, are drawn in front. rubidium@8948: * Here X,Y,Z are the coordinates of the "center of mass" of the sprite, rubidium@8948: * i.e. X=(left+right)/2, etc. rubidium@8948: * However, since we only care about order, don't actually divide / 2 tron@4187: */ rubidium@8948: if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <= rubidium@8948: ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) { rubidium@8948: continue; hackykid@1934: } rubidium@8948: } else { rubidium@8948: /* We only change the order, if it is definite. rubidium@8948: * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap. rubidium@8948: * That is: If one partial order says ps behind ps2, do not change the order. rubidium@8948: */ rubidium@8948: if (ps->xmax < ps2->xmin || rubidium@8948: ps->ymax < ps2->ymin || rubidium@8948: ps->zmax < ps2->zmin) { rubidium@8948: continue; rubidium@8948: } truelight@0: } rubidium@8948: rubidium@8948: /* Swap the two sprites ps and ps2 using bubble-sort algorithm. */ rubidium@8948: ParentSpriteToDraw **psd3 = psd; rubidium@8948: do { rubidium@8951: ParentSpriteToDraw *temp = *psd3; rubidium@8948: *psd3 = ps2; rubidium@8948: ps2 = temp; rubidium@8948: rubidium@8948: psd3++; rubidium@8948: } while (psd3 <= psd2); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@8952: static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv) truelight@0: { rubidium@8951: const ParentSpriteToDraw * const *psd_end = psd->End(); rubidium@8951: for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) { rubidium@8951: const ParentSpriteToDraw *ps = *it; rubidium@7681: if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSprite(ps->image, ps->pal, ps->x, ps->y, ps->sub); truelight@0: frosch@9534: int child_idx = ps->first_child; frosch@9534: while (child_idx >= 0) { frosch@9534: const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx); frosch@9534: child_idx = cs->next; rubidium@7681: DrawSprite(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@7643: /** rubidium@7643: * Draws the bounding boxes of all ParentSprites rubidium@7643: * @param psd Array of ParentSprites rubidium@7643: */ rubidium@8951: static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd) rubidium@7643: { rubidium@8951: const ParentSpriteToDraw * const *psd_end = psd->End(); rubidium@8951: for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) { rubidium@8951: const ParentSpriteToDraw *ps = *it; rubidium@7643: Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner rubidium@7643: Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner rubidium@7643: Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner rubidium@7643: Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner rubidium@7643: rubidium@7643: DrawBox( pt1.x, pt1.y, rubidium@7643: pt2.x - pt1.x, pt2.y - pt1.y, rubidium@7643: pt3.x - pt1.x, pt3.y - pt1.y, rubidium@7643: pt4.x - pt1.x, pt4.y - pt1.y); rubidium@7643: } rubidium@7643: } rubidium@7643: rubidium@8946: static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDrawVector *sstdv) truelight@0: { truelight@0: DrawPixelInfo dp; truelight@6624: ZoomLevel zoom; truelight@193: truelight@0: _cur_dpi = &dp; truelight@0: dp = *dpi; truelight@0: tron@2116: zoom = dp.zoom; truelight@6624: dp.zoom = ZOOM_LVL_NORMAL; truelight@0: truelight@6654: dp.left = UnScaleByZoom(dp.left, zoom); truelight@6654: dp.top = UnScaleByZoom(dp.top, zoom); truelight@6654: dp.width = UnScaleByZoom(dp.width, zoom); truelight@6654: dp.height = UnScaleByZoom(dp.height, zoom); truelight@0: peter1138@8949: const StringSpriteToDraw *ssend = sstdv->End(); peter1138@8949: for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) { tron@5025: uint16 colour; tron@5025: truelight@0: if (ss->width != 0) { smatz@8806: /* Do not draw signs nor station names if they are set invisible */ rubidium@8946: if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_2806) continue; smatz@8806: truelight@6654: int x = UnScaleByZoom(ss->x, zoom) - 1; truelight@6654: 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@7849: if (!IsTransparencySet(TO_SIGNS) || ss->string == STR_2806) { tron@2116: DrawFrameRect( tron@2116: x, y, x + w, bottom, ss->color, belugas@7849: IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE tron@2116: ); peter1138@6427: } 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@7849: 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@7824: colour = TC_BLACK; truelight@0: } tron@5025: DrawString( truelight@6654: UnScaleByZoom(ss->x, zoom), UnScaleByZoom(ss->y, zoom) - (ss->width & 0x8000 ? 2 : 0), tron@5025: ss->string, colour tron@5025: ); rubidium@8947: } truelight@0: } truelight@0: tron@430: void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { rubidium@8953: DrawPixelInfo *old_dpi = _cur_dpi; rubidium@8953: _cur_dpi = &_vd.dpi; rubidium@8953: rubidium@8953: _vd.dpi.zoom = vp->zoom; rubidium@8953: int mask = ScaleByZoom(-1, vp->zoom); rubidium@8953: rubidium@8953: _vd.combine_sprites = 0; rubidium@8953: rubidium@8953: _vd.dpi.width = (right - left) & mask; rubidium@8953: _vd.dpi.height = (bottom - top) & mask; rubidium@8953: _vd.dpi.left = left & mask; rubidium@8953: _vd.dpi.top = top & mask; rubidium@8953: _vd.dpi.pitch = old_dpi->pitch; rubidium@8953: _vd.last_child = NULL; rubidium@8953: rubidium@8953: int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left; rubidium@8953: int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top; rubidium@8953: rubidium@8953: _vd.dpi.dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top); truelight@0: truelight@0: ViewportAddLandscape(); rubidium@8953: ViewportAddVehicles(&_vd.dpi); rubidium@8953: DrawTextEffects(&_vd.dpi); rubidium@8953: rubidium@8953: ViewportAddTownNames(&_vd.dpi); rubidium@8953: ViewportAddStationNames(&_vd.dpi); rubidium@8953: ViewportAddSigns(&_vd.dpi); rubidium@8953: ViewportAddWaypoints(&_vd.dpi); rubidium@8953: skidd13@9553: if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw); rubidium@8953: rubidium@8953: ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End(); rubidium@8953: for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) { rubidium@8953: *_vd.parent_sprites_to_sort.Append() = it; rubidium@8951: } rubidium@8951: rubidium@8953: ViewportSortParentSprites(&_vd.parent_sprites_to_sort); rubidium@8953: ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw); rubidium@8953: rubidium@8953: if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort); rubidium@8953: skidd13@9553: if (_vd.string_sprites_to_draw.Length() != 0) ViewportDrawStrings(&_vd.dpi, &_vd.string_sprites_to_draw); truelight@193: truelight@0: _cur_dpi = old_dpi; rubidium@8953: skidd13@9553: _vd.string_sprites_to_draw.Clear(); skidd13@9553: _vd.tile_sprites_to_draw.Clear(); skidd13@9553: _vd.parent_sprites_to_draw.Clear(); skidd13@9553: _vd.parent_sprites_to_sort.Clear(); skidd13@9553: _vd.child_screen_sprites_to_draw.Clear(); truelight@0: } truelight@0: belugas@6423: /** Make sure we don't draw a too big area at a time. belugas@6423: * 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@6626: 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@6626: ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left, truelight@6626: ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top, truelight@6626: ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left, truelight@6626: 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: rubidium@9273: void Window::DrawViewport() const tron@2116: { truelight@0: DrawPixelInfo *dpi = _cur_dpi; truelight@0: rubidium@9273: dpi->left += this->left; rubidium@9273: dpi->top += this->top; rubidium@9273: rubidium@9273: ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height); rubidium@9273: rubidium@9273: dpi->left -= this->left; rubidium@9273: dpi->top -= this->top; truelight@0: } truelight@0: peter1138@7069: static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y) peter1138@7069: { peter1138@7069: /* Centre of the viewport is hot spot */ peter1138@7069: x += vp->virtual_width / 2; peter1138@7069: y += vp->virtual_height / 2; peter1138@7069: peter1138@7069: /* Convert viewport coordinates to map coordinates peter1138@7069: * Calculation is scaled by 4 to avoid rounding errors */ peter1138@7069: int vx = -x + y * 2; peter1138@7069: int vy = x + y * 2; peter1138@7069: peter1138@7069: /* clamp to size of map */ skidd13@7922: vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4); skidd13@7922: vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4); peter1138@7069: peter1138@7069: /* Convert map coordinates to viewport coordinates */ peter1138@7069: x = (-vx + vy) / 2; peter1138@7069: y = ( vx + vy) / 4; peter1138@7069: peter1138@7069: /* Remove centreing */ peter1138@7069: x -= vp->virtual_width / 2; peter1138@7069: y -= vp->virtual_height / 2; peter1138@7069: } peter1138@7069: truelight@0: void UpdateViewportPosition(Window *w) truelight@0: { tron@2116: const ViewPort *vp = w->viewport; truelight@0: glx@9184: if (w->viewport->follow_vehicle != INVALID_VEHICLE) { glx@9184: const Vehicle* veh = GetVehicle(w->viewport->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@7069: /* Ensure the destination location is within the map */ glx@9184: ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y); glx@9184: glx@9184: int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x; glx@9184: int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y; peter1138@6730: peter1138@6730: if (delta_x != 0 || delta_y != 0) { rubidium@9413: if (_settings_client.gui.smooth_scroll) { peter1138@6731: int max_scroll = ScaleByMapSize1D(512); peter1138@6731: /* Not at our desired positon yet... */ glx@9184: w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll); glx@9184: w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll); peter1138@6731: } else { glx@9184: w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x; glx@9184: w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y; peter1138@6731: } peter1138@6730: } peter1138@6730: glx@9184: ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y); glx@9184: glx@9184: SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y); truelight@0: } truelight@0: } truelight@0: rubidium@7545: /** rubidium@8992: * Marks a viewport as dirty for repaint if it displays (a part of) the area the needs to be repainted. rubidium@8992: * @param vp The viewport to mark as dirty rubidium@8992: * @param left Left edge of area to repaint rubidium@8992: * @param top Top edge of area to repaint rubidium@8992: * @param right Right edge of area to repaint rubidium@8992: * @param bottom Bottom edge of area to repaint rubidium@7545: * @ingroup dirty rubidium@7545: */ 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@6626: UnScaleByZoom(left, vp->zoom) + vp->left, truelight@6626: UnScaleByZoom(top, vp->zoom) + vp->top, truelight@6626: UnScaleByZoom(right, vp->zoom) + vp->left, truelight@6626: UnScaleByZoom(bottom, vp->zoom) + vp->top truelight@0: ); truelight@0: } truelight@0: rubidium@8992: /** rubidium@8992: * Mark all viewports that display an area as dirty (in need of repaint). rubidium@8992: * @param left Left edge of area to repaint rubidium@8992: * @param top Top edge of area to repaint rubidium@8992: * @param right Right edge of area to repaint rubidium@8992: * @param bottom Bottom edge of area to repaint rubidium@8992: * @ingroup dirty rubidium@8992: */ truelight@0: void MarkAllViewportsDirty(int left, int top, int right, int bottom) truelight@0: { rubidium@8957: Window **wz; rubidium@8957: rubidium@8957: FOR_ALL_WINDOWS(wz) { rubidium@8957: ViewPort *vp = (*wz)->viewport; rubidium@8957: if (vp != NULL) { truelight@0: assert(vp->width != 0); truelight@0: MarkViewportDirty(vp, left, top, right, bottom); truelight@0: } rubidium@8957: } 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@7954: if (IsInsideMM(x, 0, MapSizeX() * TILE_SIZE) && skidd13@7954: 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@7545: /** rubidium@7545: * Marks the selected tiles as dirty. rubidium@7545: * rubidium@7545: * This function marks the selected tiles as dirty for repaint rubidium@7545: * rubidium@7545: * @note Documentation may be wrong (Progman) rubidium@7545: * @ingroup dirty rubidium@7545: */ rubidium@6247: 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@7928: if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@6624: 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@6624: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@6653: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@6653: tron@4471: FOR_ALL_TOWNS(t) { tron@4471: if (y >= t->sign.top && truelight@6653: y < t->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= t->sign.left && truelight@6653: 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@6653: truelight@6653: 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: smatz@9755: if (!HasBit(_display_opt, DO_SHOW_STATION_NAMES) || IsInvisibilitySet(TO_SIGNS)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@6624: 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@6624: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@6653: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@6653: tron@4471: FOR_ALL_STATIONS(st) { tron@4471: if (y >= st->sign.top && truelight@6653: y < st->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= st->sign.left && truelight@6653: 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@6653: truelight@6653: 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: smatz@8806: /* Signs are turned off, or they are transparent and invisibility is ON, or player is a spectator */ smatz@8806: if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _current_player == PLAYER_SPECTATOR) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@6624: 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) { smatz@9510: HandleClickOnSign(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@6624: 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) { smatz@9510: HandleClickOnSign(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: truelight@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@6653: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@6653: tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && truelight@6653: y < si->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= si->sign.left && truelight@6653: x < si->sign.left + ScaleByZoom(si->sign.width_2, vp->zoom)) { smatz@9510: HandleClickOnSign(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; truelight@6653: truelight@6653: 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: smatz@9755: if (!HasBit(_display_opt, DO_WAYPOINTS) || IsInvisibilitySet(TO_SIGNS)) return false; truelight@0: tron@4471: switch (vp->zoom) { truelight@6624: 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@6624: 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@6624: case ZOOM_LVL_OUT_4X: truelight@6653: case ZOOM_LVL_OUT_8X: truelight@6653: x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left; truelight@6653: y = ScaleByZoom(y - vp->top + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top; truelight@6653: tron@4471: FOR_ALL_WAYPOINTS(wp) { tron@4471: if (y >= wp->sign.top && truelight@6653: y < wp->sign.top + ScaleByZoom(12, vp->zoom) && tron@4471: x >= wp->sign.left && truelight@6653: 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@6653: truelight@6653: 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@7497: if (!IsFrontEngine(v)) v = v->First(); rubidium@7486: ShowVehicleViewWindow(v); tron@2662: } tron@2662: maedhros@6857: static void SafeShowRoadVehViewWindow(const Vehicle *v) maedhros@6857: { rubidium@7497: if (!IsRoadVehFront(v)) v = v->First(); rubidium@7486: ShowVehicleViewWindow(v); maedhros@6857: } maedhros@6857: 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@6857: SafeShowRoadVehViewWindow, rubidium@7486: ShowVehicleViewWindow, rubidium@7486: 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@5380: DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v); bjarni@5955: _on_vehicle_click_proc[v->type](v); celestar@3809: } truelight@0: } truelight@0: rubidium@6247: 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@6247: 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(); rubidium@9166: if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y)); truelight@0: } truelight@0: darkvater@152: darkvater@152: /* scrolls the viewport in a window to a given location */ peter1138@6730: bool ScrollWindowTo(int x , int y, Window *w, bool instant) darkvater@152: { rubidium@8473: /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */ rubidium@8473: Point pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(Clamp(x, 0, MapSizeX()), Clamp(y, 0, MapSizeY()))); glx@9184: w->viewport->follow_vehicle = INVALID_VEHICLE; glx@9184: glx@9184: if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) tron@2116: return false; darkvater@152: peter1138@6731: if (instant) { glx@9184: w->viewport->scrollpos_x = pt.x; glx@9184: w->viewport->scrollpos_y = pt.y; peter1138@6730: } peter1138@6730: glx@9184: w->viewport->dest_scrollpos_x = pt.x; glx@9184: w->viewport->dest_scrollpos_y = pt.y; darkvater@152: return true; darkvater@152: } darkvater@152: peter1138@6730: bool ScrollMainWindowToTile(TileIndex tile, bool instant) truelight@0: { peter1138@6730: 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@6423: /** returns the best autorail highlight type from map coordinates */ rubidium@8224: static HighLightStyle GetAutorailHT(int x, int y) dominik@1070: { rubidium@8224: return HT_RAIL | _autorail_piece[x & 0xF][y & 0xF]; dominik@1070: } truelight@0: belugas@7078: /** belugas@7078: * Updates tile highlighting for all cases. belugas@7078: * Uses _thd.selstart and _thd.selend and _thd.place_mode (set elsewhere) to determine _thd.pos and _thd.size belugas@7078: * Also drawstyle is determined. Uses _thd.new.* as a buffer and calls SetSelectionTilesDirty() twice, belugas@7078: * Once for the old and once for the new selection. belugas@7078: * _thd is TileHighlightData, found in viewport.h belugas@7078: * Called by MouseLoop() in windows.cpp belugas@7078: */ rubidium@6247: 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@7912: int x2 = _thd.selstart.x & ~0xF; smatz@7912: int y2 = _thd.selstart.y & ~0xF; truelight@0: x1 &= ~0xF; truelight@0: y1 &= ~0xF; truelight@193: tron@6106: if (x1 >= x2) Swap(x1, x2); tron@6106: 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@7918: break; smatz@7918: default: smatz@7918: NOT_REACHED(); smatz@7918: 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@6423: /* 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@7078: _thd.outersize.x != _thd.new_outersize.x || belugas@7078: _thd.outersize.y != _thd.new_outersize.y) { belugas@6423: /* 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@6423: /* draw the new selection? */ tron@1863: if (_thd.new_drawstyle) SetSelectionTilesDirty(); truelight@0: } truelight@0: } truelight@0: frosch@9781: /** Displays the measurement tooltips when selecting multiple tiles frosch@9781: * @param str String to be displayed frosch@9781: * @param paramcount number of params to deal with frosch@9781: * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip frosch@9781: */ frosch@9781: static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[]) frosch@9781: { frosch@9781: if (!_settings_client.gui.measure_tooltip) return; frosch@9781: GuiShowTooltips(str, paramcount, params, true); frosch@9781: } frosch@9781: belugas@6423: /** highlighting tiles while only going over them with the mouse */ rubidium@9147: void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process) truelight@0: { maedhros@6669: _thd.select_method = method; maedhros@6669: _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@7912: smatz@7912: /* Needed so several things (road, autoroad, bridges, ...) are placed correctly. smatz@7912: * In effect, placement starts from the centre of a tile smatz@7912: */ smatz@7912: if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) { smatz@7912: _thd.selend.x += TILE_SIZE / 2; smatz@7912: _thd.selend.y += TILE_SIZE / 2; smatz@7912: _thd.selstart.x += TILE_SIZE / 2; smatz@7912: _thd.selstart.y += TILE_SIZE / 2; smatz@7912: } smatz@7912: 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@7006: 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 */ frosch@9781: if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance); truelight@0: } truelight@0: rubidium@6247: static void VpStartPreSizing() truelight@0: { truelight@0: _thd.selend.x = -1; truelight@0: _special_mouse_mode = WSM_PRESIZE; truelight@0: } truelight@0: belugas@6423: /** returns information about the 2x1 piece to be build. dominik@1070: * The lower bits (0-3) are the track type. */ rubidium@8224: 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@8224: default: NOT_REACHED(); rubidium@8224: case 0: // end piece is lower right rubidium@8224: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; } rubidium@8224: if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return HT_DIR_VR; } rubidium@8224: return HT_DIR_Y; rubidium@8224: rubidium@8224: case 1: rubidium@8224: if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_VL; } rubidium@8224: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; } rubidium@8224: return HT_DIR_Y; rubidium@8224: rubidium@8224: case 2: rubidium@8224: if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return HT_DIR_VL; } rubidium@8224: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; } rubidium@8224: return HT_DIR_X; rubidium@8224: rubidium@8224: case 3: rubidium@8224: if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return HT_DIR_VR; } rubidium@8224: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; } rubidium@8224: 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@6484: * @param start_tile start tile of drag belugas@6484: * @param end_tile end tile of drag belugas@6484: * @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@6491: * 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@5733: 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@5601: 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@5601: h1 = max(h1, ht); Darkvater@4834: } break; Darkvater@4834: } Darkvater@4834: tron@5733: 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@6423: /** 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@7923: w = abs(dx) + 16; skidd13@7923: 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: rubidium@9413: if (_settings_client.gui.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@7006: 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: frosch@9781: ShowMeasurementTooltips(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@7888: 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@7912: /* 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@7923: 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:; rubidium@9413: if (_settings_client.gui.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@7006: 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: frosch@9781: ShowMeasurementTooltips(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@7922: x = sx + Clamp(x - sx, -limit, limit); skidd13@7922: y = sy + Clamp(y - sy, -limit, limit); rubidium@5587: } /* Fallthrough */ rubidium@5587: case VPM_X_AND_Y: { /* drag an X by Y area */ rubidium@9413: if (_settings_client.gui.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@7970: uint dx = Delta(TileX(t0), TileX(t1)) + 1; skidd13@7970: uint dy = Delta(TileY(t0), TileY(t1)) + 1; Darkvater@4884: byte index = 0; rubidium@7006: 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@8224: 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: frosch@9781: ShowMeasurementTooltips(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: rubidium@9314: /** rubidium@9314: * Handle the mouse while dragging for placement/resizing. rubidium@9314: * @return Boolean whether search for a handler should continue rubidium@9314: */ rubidium@6247: bool VpHandlePlaceSizingDrag() truelight@0: { tron@2116: if (_special_mouse_mode != WSM_SIZING) return true; truelight@0: belugas@6423: /* stop drag mode if the window has been closed */ rubidium@9166: Window *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@6423: /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */ truelight@0: if (_left_button_down) { rubidium@9166: w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor()); truelight@0: return false; truelight@0: } truelight@0: belugas@6423: /* mouse button released.. belugas@6423: * 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; rubidium@9166: } else if (_thd.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: rubidium@9166: w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y)); truelight@193: truelight@0: return false; truelight@0: } truelight@0: rubidium@7889: void SetObjectToPlaceWnd(CursorID icon, SpriteID pal, ViewportHighlightMode mode, Window *w) truelight@0: { peter1138@5668: SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number); truelight@0: } truelight@0: truelight@0: #include "table/animcursors.h" truelight@0: rubidium@7889: void SetObjectToPlace(CursorID icon, SpriteID pal, ViewportHighlightMode mode, WindowClass window_class, WindowNumber window_num) truelight@0: { smatz@8585: /* undo clicking on button and drag & drop */ smatz@8585: if (_thd.place_mode != VHM_NONE || _special_mouse_mode == WSM_DRAGDROP) { rubidium@9244: Window *w = FindWindowById(_thd.window_class, _thd.window_number); rubidium@9244: if (w != NULL) { rubidium@9244: /* Call the abort function, but set the window class to something rubidium@9244: * that will never be used to avoid infinite loops. Setting it to rubidium@9244: * the 'next' window class must not be done because recursion into rubidium@9244: * this function might in some cases reset the newly set object to rubidium@9244: * place or not properly reset the original selection. */ rubidium@9244: _thd.window_class = WC_INVALID; rubidium@9244: w->OnPlaceObjectAbort(); rubidium@9244: } truelight@0: } truelight@193: truelight@0: SetTileSelectSize(1, 1); truelight@193: tron@1863: _thd.make_square_red = false; truelight@0: rubidium@7889: if (mode == VHM_DRAG) { // VHM_DRAG is for dragdropping trains in the depot window rubidium@7889: 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: rubidium@9238: if ((int)icon < 0) { Darkvater@4334: SetAnimatedMouseCursor(_animcursors[~icon]); rubidium@9238: } else { peter1138@5668: SetMouseCursor(icon, pal); rubidium@9238: } rubidium@9238: truelight@0: } truelight@0: rubidium@6247: void ResetObjectToPlace() tron@1093: { rubidium@5893: SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0); truelight@0: } rubidium@9410: rubidium@9410: rubidium@9410: void SaveViewportBeforeSaveGame() rubidium@9410: { rubidium@9410: const Window *w = FindWindowById(WC_MAIN_WINDOW, 0); rubidium@9410: rubidium@9410: if (w != NULL) { rubidium@9410: _saved_scrollpos_x = w->viewport->scrollpos_x; rubidium@9410: _saved_scrollpos_y = w->viewport->scrollpos_y; rubidium@9410: _saved_scrollpos_zoom = w->viewport->zoom; rubidium@9410: } rubidium@9410: } rubidium@9410: rubidium@9410: void ResetViewportAfterLoadGame() rubidium@9410: { rubidium@9410: Window *w = FindWindowById(WC_MAIN_WINDOW, 0); rubidium@9410: rubidium@9410: w->viewport->scrollpos_x = _saved_scrollpos_x; rubidium@9410: w->viewport->scrollpos_y = _saved_scrollpos_y; rubidium@9410: w->viewport->dest_scrollpos_x = _saved_scrollpos_x; rubidium@9410: w->viewport->dest_scrollpos_y = _saved_scrollpos_y; rubidium@9410: rubidium@9410: ViewPort *vp = w->viewport; rubidium@9410: vp->zoom = min(_saved_scrollpos_zoom, ZOOM_LVL_MAX); rubidium@9410: vp->virtual_width = ScaleByZoom(vp->width, vp->zoom); rubidium@9410: vp->virtual_height = ScaleByZoom(vp->height, vp->zoom); rubidium@9410: rubidium@9410: DoZoomInOutWindow(ZOOM_NONE, w); // update button status rubidium@9410: MarkWholeScreenDirty(); rubidium@9410: }