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