tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" tron@2662: #include "gui.h" tron@1349: #include "spritecache.h" tron@1309: #include "strings.h" tron@1363: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" truelight@0: #include "viewport.h" truelight@0: #include "window.h" truelight@0: #include "vehicle.h" truelight@0: #include "station.h" truelight@0: #include "gfx.h" truelight@0: #include "town.h" truelight@988: #include "signs.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" bjarni@2676: #include "train.h" truelight@0: truelight@133: #define VIEWPORT_DRAW_MEM (65536 * 2) truelight@0: Darkvater@5122: /* viewport.c */ Darkvater@5122: // XXX - maximum viewports is maximum windows - 2 (main toolbar + status bar) Darkvater@5122: static ViewPort _viewports[25 - 2]; Darkvater@5122: static uint32 _active_viewports; ///< bitmasked variable where each bit signifies if a viewport is in use or not Darkvater@5122: assert_compile(lengthof(_viewports) < sizeof(_active_viewports) * 8); Darkvater@5122: truelight@0: static bool _added_tile_sprite; truelight@0: static bool _offset_ground_sprites; truelight@0: tron@4186: /* The in-game coordiante system looks like this * tron@4186: * * tron@4186: * ^ Z * tron@4186: * | * tron@4186: * | * tron@4186: * | * tron@4186: * | * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * / \ * tron@4186: * X < > Y * tron@4186: */ tron@4186: truelight@0: typedef struct StringSpriteToDraw { truelight@0: uint16 string; truelight@0: uint16 color; truelight@0: struct StringSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; tron@5026: uint32 params[2]; truelight@0: uint16 width; truelight@0: } StringSpriteToDraw; truelight@0: truelight@0: typedef struct TileSpriteToDraw { truelight@0: uint32 image; truelight@0: struct TileSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; truelight@0: byte z; truelight@0: } TileSpriteToDraw; truelight@0: truelight@0: typedef struct ChildScreenSpriteToDraw { truelight@0: uint32 image; tron@849: int32 x; tron@849: int32 y; truelight@0: struct ChildScreenSpriteToDraw *next; truelight@0: } ChildScreenSpriteToDraw; truelight@0: truelight@0: typedef struct ParentSpriteToDraw { truelight@0: uint32 image; tron@849: int32 left; tron@849: int32 top; tron@849: int32 right; tron@849: int32 bottom; tron@4186: int32 xmin; tron@4186: int32 ymin; tron@4186: int32 xmax; tron@4186: int32 ymax; truelight@0: ChildScreenSpriteToDraw *child; truelight@0: byte unk16; tron@4186: byte zmin; tron@4186: byte zmax; truelight@0: } ParentSpriteToDraw; truelight@0: ludde@2109: // Quick hack to know how much memory to reserve when allocating from the spritelist ludde@2109: // to prevent a buffer overflow. ludde@2109: #define LARGEST_SPRITELIST_STRUCT ParentSpriteToDraw ludde@2109: truelight@0: typedef struct ViewportDrawer { truelight@0: DrawPixelInfo dpi; truelight@193: tron@2116: byte *spritelist_mem; tron@2116: const byte *eof_spritelist_mem; truelight@193: truelight@0: StringSpriteToDraw **last_string, *first_string; truelight@0: TileSpriteToDraw **last_tile, *first_tile; truelight@0: truelight@0: ChildScreenSpriteToDraw **last_child; truelight@0: truelight@0: ParentSpriteToDraw **parent_list; tron@2116: ParentSpriteToDraw * const *eof_parent_list; truelight@0: truelight@0: byte combine_sprites; truelight@0: truelight@0: int offs_x, offs_y; // used when drawing ground sprites relative truelight@0: } ViewportDrawer; truelight@0: truelight@0: static ViewportDrawer *_cur_vd; truelight@0: tron@1863: TileHighlightData _thd; truelight@0: static TileInfo *_cur_ti; truelight@0: truelight@4340: extern void SmallMapCenterOnCurrentPos(Window *w); truelight@0: tron@2116: static Point MapXYZToViewport(const ViewPort *vp, uint x, uint y, uint z) truelight@0: { truelight@0: Point p = RemapCoords(x, y, z); tron@2116: p.x -= vp->virtual_width / 2; tron@2116: p.y -= vp->virtual_height / 2; truelight@0: return p; truelight@0: } truelight@0: Darkvater@5122: void InitViewports(void) { Darkvater@5122: memset(_viewports, 0, sizeof(_viewports)); Darkvater@5122: _active_viewports = 0; Darkvater@5122: } Darkvater@5122: Darkvater@5122: void DeleteWindowViewport(Window *w) Darkvater@5122: { Darkvater@5122: CLRBIT(_active_viewports, w->viewport - _viewports); Darkvater@5122: w->viewport->width = 0; Darkvater@5122: w->viewport = NULL; Darkvater@5122: } Darkvater@5122: truelight@193: void AssignWindowViewport(Window *w, int x, int y, truelight@0: int width, int height, uint32 follow_flags, byte zoom) truelight@0: { truelight@0: ViewPort *vp; truelight@0: Point pt; truelight@0: uint32 bit; truelight@0: Darkvater@5122: for (vp = _viewports, bit = 0; ; vp++, bit++) { tron@2116: assert(vp != endof(_viewports)); tron@2116: if (vp->width == 0) break; truelight@0: } Darkvater@5122: SETBIT(_active_viewports, bit); truelight@0: truelight@0: vp->left = x + w->left; truelight@0: vp->top = y + w->top; truelight@0: vp->width = width; truelight@0: vp->height = height; truelight@193: truelight@0: vp->zoom = zoom; truelight@0: truelight@0: vp->virtual_width = width << zoom; truelight@0: vp->virtual_height = height << zoom; truelight@0: truelight@0: if (follow_flags & 0x80000000) { tron@2116: const Vehicle *veh; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = (VehicleID)(follow_flags & 0xFFFF); tron@2116: veh = GetVehicle(WP(w, vp_d).follow_vehicle); truelight@0: pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: } else { celestar@3421: uint x = TileX(follow_flags) * TILE_SIZE; celestar@3421: uint y = TileY(follow_flags) * TILE_SIZE; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; tron@2116: pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y)); truelight@0: } truelight@0: tron@2116: WP(w, vp_d).scrollpos_x = pt.x; tron@2116: WP(w, vp_d).scrollpos_y = pt.y; 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: truelight@0: old_left >>= vp->zoom; truelight@0: old_top >>= vp->zoom; truelight@0: x >>= vp->zoom; truelight@0: y >>= vp->zoom; truelight@0: truelight@0: old_left -= x; truelight@0: old_top -= y; truelight@0: tron@2116: if (old_top == 0 && old_left == 0) return; truelight@0: truelight@0: _vp_move_offs.x = old_left; truelight@0: _vp_move_offs.y = old_top; truelight@0: truelight@0: left = vp->left; truelight@0: top = vp->top; truelight@0: width = vp->width; truelight@0: height = vp->height; truelight@0: truelight@0: if (left < 0) { truelight@0: width += left; truelight@0: left = 0; truelight@0: } truelight@0: tron@2116: i = left + width - _screen.width; tron@2116: if (i >= 0) width -= i; truelight@0: truelight@0: if (width > 0) { truelight@0: if (top < 0) { truelight@0: height += top; truelight@0: top = 0; truelight@0: } truelight@193: tron@2116: i = top + height - _screen.height; tron@2116: if (i >= 0) height -= i; truelight@0: Darkvater@5124: if (height > 0) DoSetViewportPosition(FindWindowZPosition(w) + 1, left, top, width, height); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@2116: ViewPort *IsPtInWindowViewport(const Window *w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; tron@2116: truelight@0: if (vp != NULL && truelight@0: IS_INT_INSIDE(x, vp->left, vp->left + vp->width) && truelight@0: IS_INT_INSIDE(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@0: x = ((x << vp->zoom) + vp->virtual_left) >> 2; truelight@0: y = ((y << vp->zoom) + vp->virtual_top) >> 1; truelight@0: truelight@0: #if !defined(NEW_ROTATION) truelight@0: a = y-x; truelight@0: b = y+x; truelight@0: #else truelight@0: a = x+y; truelight@0: b = x-y; truelight@0: #endif 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 */ Darkvater@5014: a = clamp(a, 0, (int)(MapMaxX() * TILE_SIZE) - 1); Darkvater@5014: b = clamp(b, 0, (int)(MapMaxY() * TILE_SIZE) - 1); Darkvater@5014: Darkvater@5014: z = GetSlopeZ(a, b ) / 2; Darkvater@5014: z = GetSlopeZ(a + z, b + z) / 2; Darkvater@5014: z = GetSlopeZ(a + z, b + z) / 2; Darkvater@5014: z = GetSlopeZ(a + z, b + z) / 2; Darkvater@5014: 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: tron@1093: Point GetTileBelowCursor(void) truelight@0: { darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y); truelight@0: } truelight@0: darkvater@152: darkvater@152: Point GetTileZoomCenterWindow(bool in, Window * w) truelight@0: { truelight@0: int x, y; darkvater@152: ViewPort * vp; darkvater@152: darkvater@152: vp = w->viewport; darkvater@152: tron@2026: if (in) { tron@2116: x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2); tron@2116: y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2); tron@2116: } else { darkvater@152: x = vp->width - (_cursor.pos.x - vp->left); darkvater@152: y = vp->height - (_cursor.pos.y - vp->top); truelight@0: } darkvater@981: /* Get the tile below the cursor and center on the zoomed-out center */ darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top); truelight@0: } truelight@0: Darkvater@5045: /** Update the status of the zoom-buttons according to the zoom-level Darkvater@5045: * of the viewport. This will update their status and invalidate accordingly Darkvater@5045: * @param 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: { Darkvater@5045: SetWindowWidgetDisabledState(w, widget_zoom_in, vp->zoom == 0); Darkvater@5045: InvalidateWidget(w, widget_zoom_in); Darkvater@5045: Darkvater@5045: SetWindowWidgetDisabledState(w, widget_zoom_out, vp->zoom == 2); Darkvater@5045: InvalidateWidget(w, widget_zoom_out); Darkvater@5045: } Darkvater@5045: tron@849: void DrawGroundSpriteAt(uint32 image, int32 x, int32 y, byte z) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: TileSpriteToDraw *ts; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: ts = (TileSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(TileSpriteToDraw); truelight@193: truelight@0: ts->image = image; truelight@0: ts->next = NULL; truelight@0: ts->x = x; truelight@0: ts->y = y; truelight@0: ts->z = z; truelight@0: *vd->last_tile = ts; truelight@193: vd->last_tile = &ts->next; truelight@0: } truelight@0: truelight@0: void DrawGroundSprite(uint32 image) truelight@0: { truelight@0: if (_offset_ground_sprites) { truelight@0: // offset ground sprite because of foundation? truelight@0: AddChildSpriteScreen(image, _cur_vd->offs_x, _cur_vd->offs_y); truelight@0: } else { dominik@1083: _added_tile_sprite = true; truelight@0: DrawGroundSpriteAt(image, _cur_ti->x, _cur_ti->y, _cur_ti->z); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: void OffsetGroundSprite(int x, int y) truelight@0: { truelight@0: _cur_vd->offs_x = x; truelight@0: _cur_vd->offs_y = y; truelight@0: _offset_ground_sprites = true; truelight@0: } truelight@0: truelight@0: static void AddCombinedSprite(uint32 image, int x, int y, byte z) truelight@0: { tron@2116: const ViewportDrawer *vd = _cur_vd; truelight@0: Point pt = RemapCoords(x, y, z); tron@2319: const Sprite* spr = GetSprite(image & SPRITE_MASK); truelight@0: tron@2319: if (pt.x + spr->x_offs >= vd->dpi.left + vd->dpi.width || tron@2319: pt.x + spr->x_offs + spr->width <= vd->dpi.left || tron@2319: pt.y + spr->y_offs >= vd->dpi.top + vd->dpi.height || tron@2319: pt.y + spr->y_offs + spr->height <= vd->dpi.top) truelight@0: return; truelight@0: tron@2116: AddChildSpriteScreen(image, pt.x - vd->parent_list[-1]->left, pt.y - vd->parent_list[-1]->top); truelight@0: } truelight@0: truelight@0: truelight@0: void AddSortableSpriteToDraw(uint32 image, int x, int y, int w, int h, byte dz, byte z) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ParentSpriteToDraw *ps; belugas@4171: const Sprite *spr; truelight@0: Point pt; truelight@0: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: truelight@0: if (vd->combine_sprites == 2) { truelight@0: AddCombinedSprite(image, x, y, z); truelight@0: return; truelight@0: } truelight@0: truelight@0: vd->last_child = NULL; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: ps = (ParentSpriteToDraw*)vd->spritelist_mem; tron@2116: truelight@133: if (vd->parent_list >= vd->eof_parent_list) { truelight@133: // This can happen rarely, mostly when you zoom out completely truelight@193: // and have a lot of stuff that moves (and is added to the truelight@133: // sort-list, this function). To solve it, increase truelight@133: // parent_list somewhere below to a higher number. truelight@133: // This can not really hurt you, it just gives some black truelight@133: // spots on the screen ;) celestar@2013: DEBUG(misc, 0) ("Out of sprite mem (parent_list)"); truelight@133: return; truelight@193: } truelight@193: tron@4189: pt = RemapCoords(x, y, z); tron@4189: spr = GetSprite(image & SPRITE_MASK); tron@4189: if ((ps->left = (pt.x += spr->x_offs)) >= vd->dpi.left + vd->dpi.width || tron@4189: (ps->right = (pt.x + spr->width )) <= vd->dpi.left || tron@4189: (ps->top = (pt.y += spr->y_offs)) >= vd->dpi.top + vd->dpi.height || tron@4189: (ps->bottom = (pt.y + spr->height)) <= vd->dpi.top) { tron@4189: return; tron@4189: } tron@4189: truelight@193: vd->spritelist_mem += sizeof(ParentSpriteToDraw); truelight@0: truelight@0: ps->image = image; tron@4186: ps->xmin = x; tron@4186: ps->xmax = x + w - 1; truelight@0: tron@4186: ps->ymin = y; tron@4186: ps->ymax = y + h - 1; truelight@0: tron@4186: ps->zmin = z; tron@4186: ps->zmax = z + dz - 1; truelight@0: truelight@0: ps->unk16 = 0; truelight@0: ps->child = NULL; truelight@0: vd->last_child = &ps->child; truelight@0: truelight@0: *vd->parent_list++ = ps; truelight@0: tron@2116: if (vd->combine_sprites == 1) vd->combine_sprites = 2; truelight@0: } truelight@0: tron@1093: void StartSpriteCombine(void) truelight@0: { truelight@0: _cur_vd->combine_sprites = 1; truelight@0: } truelight@0: tron@1093: void EndSpriteCombine(void) truelight@0: { truelight@0: _cur_vd->combine_sprites = 0; truelight@0: } truelight@0: truelight@0: void AddChildSpriteScreen(uint32 image, int x, int y) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ChildScreenSpriteToDraw *cs; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: cs = (ChildScreenSpriteToDraw*)vd->spritelist_mem; truelight@0: tron@2116: if (vd->last_child == NULL) return; truelight@193: truelight@193: vd->spritelist_mem += sizeof(ChildScreenSpriteToDraw); truelight@0: truelight@0: *vd->last_child = cs; truelight@0: vd->last_child = &cs->next; truelight@0: truelight@0: cs->image = image; truelight@0: cs->x = x; truelight@0: cs->y = y; truelight@0: cs->next = NULL; truelight@0: } truelight@0: truelight@0: /* Returns a StringSpriteToDraw */ tron@5026: void *AddStringToDraw(int x, int y, StringID string, uint32 params_1, uint32 params_2) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: StringSpriteToDraw *ss; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return NULL; truelight@0: } tron@2116: ss = (StringSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(StringSpriteToDraw); truelight@0: truelight@0: ss->string = string; truelight@0: ss->next = NULL; truelight@0: ss->x = x; truelight@0: ss->y = y; truelight@0: ss->params[0] = params_1; truelight@0: ss->params[1] = params_2; truelight@0: ss->width = 0; truelight@0: truelight@0: *vd->last_string = ss; truelight@0: vd->last_string = &ss->next; truelight@0: truelight@0: return ss; truelight@0: } truelight@0: tron@4000: truelight@0: static void DrawSelectionSprite(uint32 image, const TileInfo *ti) truelight@0: { tron@1863: if (_added_tile_sprite && !(_thd.drawstyle & HT_LINE)) { // draw on real ground dominik@1083: DrawGroundSpriteAt(image, ti->x, ti->y, ti->z + 7); dominik@1083: } else { // draw on top of foundation dominik@1083: AddSortableSpriteToDraw(image, ti->x, ti->y, 0x10, 0x10, 1, ti->z + 7); dominik@1083: } truelight@0: } truelight@0: 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: tron@2952: switch (_thd.drawstyle) { dominik@1070: case HT_LINE | HT_DIR_X: return py == 0; // x direction dominik@1070: case HT_LINE | HT_DIR_Y: return px == 0; // y direction dominik@1070: case HT_LINE | HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper dominik@1070: case HT_LINE | HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower dominik@1070: case HT_LINE | HT_DIR_VL: return px == py || px == py + 16; // vertival left dominik@1070: case HT_LINE | HT_DIR_VR: return px == py || px == py - 16; // vertical right truelight@0: default: truelight@0: NOT_REACHED(); truelight@0: } truelight@193: truelight@0: /* useless, but avoids compiler warning this way */ truelight@0: return 0; truelight@0: } truelight@0: dominik@1070: // [direction][side] Darkvater@2710: static const int _AutorailType[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: truelight@0: static void DrawTileSelection(const TileInfo *ti) truelight@0: { truelight@0: uint32 image; truelight@0: truelight@0: // Draw a red error square? celestar@3281: if (_thd.redsq != 0 && _thd.redsq == ti->tile) { tron@1864: DrawSelectionSprite(PALETTE_TILE_RED_PULSATING | (SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]), ti); truelight@0: return; truelight@0: } truelight@0: truelight@0: // no selection active? tron@2116: if (_thd.drawstyle == 0) return; truelight@0: truelight@0: // Inside the inner area? tron@2116: if (IS_INSIDE_1D(ti->x, _thd.pos.x, _thd.size.x) && tron@2116: IS_INSIDE_1D(ti->y, _thd.pos.y, _thd.size.y)) { tron@1863: if (_thd.drawstyle & HT_RECT) { tron@1864: image = SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]; tron@1864: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; truelight@0: DrawSelectionSprite(image, ti); tron@1863: } else if (_thd.drawstyle & HT_POINT) { truelight@0: // Figure out the Z coordinate for the single dot. truelight@0: byte z = ti->z; tron@3636: if (ti->tileh & SLOPE_N) { tron@3645: z += TILE_HEIGHT; tron@3645: if (ti->tileh == SLOPE_STEEP_N) z += TILE_HEIGHT; truelight@0: } tron@1864: DrawGroundSpriteAt(_cur_dpi->zoom != 2 ? SPR_DOT : SPR_DOT_SMALL, ti->x, ti->y, z); tron@2116: } else if (_thd.drawstyle & HT_RAIL /*&& _thd.place_mode == VHM_RAIL*/) { tron@2116: // autorail highlight piece under cursor tron@2116: uint type = _thd.drawstyle & 0xF; tron@2116: assert(type <= 5); Darkvater@2710: image = SPR_AUTORAIL_BASE + _AutorailTilehSprite[ti->tileh][_AutorailType[type][0]]; dominik@1070: tron@1864: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; dominik@1070: DrawSelectionSprite(image, ti); dominik@1070: tron@2116: } else if (IsPartOfAutoLine(ti->x, ti->y)) { tron@2116: // 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); tron@2116: int diffx = myabs(TileX(start) - TileX(ti->tile)); tron@2116: int diffy = myabs(TileY(start) - TileY(ti->tile)); tron@2116: side = myabs(diffx - diffy); tron@2116: } tron@1109: Darkvater@2710: image = SPR_AUTORAIL_BASE + _AutorailTilehSprite[ti->tileh][_AutorailType[dir][side]]; dominik@1070: tron@2116: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; tron@2116: DrawSelectionSprite(image, ti); tron@2116: } truelight@0: return; truelight@0: } truelight@0: truelight@0: // Check if it's inside the outer area? tron@1863: if (_thd.outersize.x && tron@1863: _thd.size.x < _thd.size.x + _thd.outersize.x && tron@1863: IS_INSIDE_1D(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) && tron@1863: IS_INSIDE_1D(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) { truelight@0: // Draw a blue rect. tron@1864: DrawSelectionSprite(PALETTE_SEL_TILE_BLUE | (SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]), ti); truelight@0: return; truelight@0: } truelight@0: } truelight@0: tron@1093: static void ViewportAddLandscape(void) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: int x, y, width, height; truelight@0: TileInfo ti; truelight@0: bool direction; truelight@0: truelight@0: _cur_ti = &ti; truelight@0: truelight@0: // Transform into tile coordinates and round to closest full tile truelight@0: #if !defined(NEW_ROTATION) truelight@0: x = ((vd->dpi.top >> 1) - (vd->dpi.left >> 2)) & ~0xF; truelight@0: y = ((vd->dpi.top >> 1) + (vd->dpi.left >> 2) - 0x10) & ~0xF; truelight@0: #else truelight@0: x = ((vd->dpi.top >> 1) + (vd->dpi.left >> 2) - 0x10) & ~0xF; truelight@0: y = ((vd->dpi.left >> 2) - (vd->dpi.top >> 1)) & ~0xF; truelight@0: #endif truelight@0: // determine size of area truelight@0: { truelight@0: Point pt = RemapCoords(x, y, 241); truelight@0: width = (vd->dpi.left + vd->dpi.width - pt.x + 95) >> 6; truelight@0: height = (vd->dpi.top + vd->dpi.height - pt.y) >> 5 << 1; truelight@0: } truelight@0: truelight@0: assert(width > 0); truelight@0: assert(height > 0); truelight@193: truelight@0: direction = false; truelight@0: truelight@0: do { truelight@0: int width_cur = width; truelight@0: int x_cur = x; truelight@0: int y_cur = y; truelight@193: truelight@0: do { tron@4238: TileType tt; tron@4238: tron@4238: ti.x = x_cur; tron@4238: ti.y = y_cur; tron@4238: if (0 <= x_cur && x_cur < (int)MapMaxX() * TILE_SIZE && tron@4238: 0 <= y_cur && y_cur < (int)MapMaxY() * TILE_SIZE) { tron@4238: TileIndex tile = TileVirtXY(x_cur, y_cur); tron@4238: tron@4238: ti.tile = tile; tron@4238: ti.tileh = GetTileSlope(tile, &ti.z); tron@4238: tt = GetTileType(tile); tron@4238: } else { tron@4238: ti.tileh = SLOPE_FLAT; tron@4238: ti.tile = 0; tron@4238: ti.z = 0; tron@4238: tt = MP_VOID; tron@4238: } tron@4238: truelight@0: #if !defined(NEW_ROTATION) truelight@0: y_cur += 0x10; truelight@0: x_cur -= 0x10; truelight@0: #else truelight@0: y_cur += 0x10; truelight@0: x_cur += 0x10; truelight@0: #endif truelight@0: _added_tile_sprite = false; truelight@0: _offset_ground_sprites = false; truelight@193: tron@4238: _tile_type_procs[tt]->draw_tile_proc(&ti); truelight@0: DrawTileSelection(&ti); truelight@0: } while (--width_cur); truelight@193: truelight@0: #if !defined(NEW_ROTATION) truelight@0: if ( (direction^=1) != 0) truelight@0: y += 0x10; truelight@0: else truelight@0: x += 0x10; truelight@0: #else truelight@0: if ( (direction^=1) != 0) truelight@0: x += 0x10; truelight@0: else truelight@0: y -= 0x10; truelight@0: #endif 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: truelight@0: if (!(_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) { tron@5027: case 0: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && tron@5027: top < t->sign.top + 12 && tron@5027: right > t->sign.left && tron@5027: left < t->sign.left + t->sign.width_1) { tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, tron@5027: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, tron@5027: t->index, t->population); tron@5027: } truelight@0: } tron@5027: break; tron@5027: tron@5027: case 1: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && tron@5027: top < t->sign.top + 24 && tron@5027: right > t->sign.left && tron@5027: left < t->sign.left + t->sign.width_1*2) { tron@5027: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, tron@5027: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, tron@5027: t->index, t->population); tron@5027: } truelight@193: } tron@5027: break; tron@5027: tron@5027: default: NOT_REACHED(); tron@5027: case 2: tron@5027: right += 4; tron@5027: bottom += 5; tron@5027: tron@5027: FOR_ALL_TOWNS(t) { tron@5027: if (bottom > t->sign.top && tron@5027: top < t->sign.top + 24 && tron@5027: right > t->sign.left && tron@5027: left < t->sign.left + t->sign.width_2*4) { 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@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddStation(const Station *st, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: tron@5026: sstd = AddStringToDraw(st->sign.left + 1, st->sign.top + 1, str, st->index, st->facilities); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (st->owner == OWNER_NONE || st->facilities == 0) ? 0xE : _player_colors[st->owner]; tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddStationNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: int left, top, right, bottom; tron@5025: const Station *st; truelight@0: truelight@0: if (!(_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) { tron@5027: case 0: 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: tron@5027: case 1: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && tron@5027: top < st->sign.top + 24 && tron@5027: right > st->sign.left && tron@5027: left < st->sign.left + st->sign.width_1*2) { tron@5027: AddStation(st, STR_305C_0, st->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: tron@5027: default: NOT_REACHED(); tron@5027: case 2: tron@5027: right += 4; tron@5027: bottom += 5; tron@5027: FOR_ALL_STATIONS(st) { tron@5027: if (bottom > st->sign.top && tron@5027: top < st->sign.top + 24 && tron@5027: right > st->sign.left && tron@5027: left < st->sign.left + st->sign.width_2*4) { tron@5027: AddStation(st, STR_STATION_SIGN_TINY, st->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddSign(const Sign *si, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: tron@5026: sstd = AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->str, 0); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (si->owner == OWNER_NONE) ? 14 : _player_colors[si->owner]; tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddSigns(DrawPixelInfo *dpi) truelight@0: { tron@5025: const Sign *si; truelight@0: int left, top, right, bottom; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_SIGNS)) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: tron@5027: switch (dpi->zoom) { tron@5027: case 0: 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: tron@5027: case 1: 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: tron@5027: default: NOT_REACHED(); tron@5027: case 2: tron@5027: right += 4; tron@5027: bottom += 5; 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_2 * 4) { tron@5027: AddSign(si, STR_2002, si->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@0: } truelight@0: } truelight@0: tron@5025: tron@5025: static void AddWaypoint(const Waypoint *wp, StringID str, uint16 width) tron@5025: { tron@5025: StringSpriteToDraw *sstd; tron@5025: tron@5026: sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, str, wp->index, 0); tron@5025: if (sstd != NULL) { tron@5025: sstd->color = (wp->deleted ? 0xE : 11); tron@5025: sstd->width = width; tron@5025: } tron@5025: } tron@5025: tron@5025: tron@410: static void ViewportAddWaypoints(DrawPixelInfo *dpi) truelight@0: { tron@5025: const Waypoint *wp; truelight@0: int left, top, right, bottom; truelight@0: darkvater@395: if (!(_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) { tron@5027: case 0: 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: tron@5027: case 1: tron@5027: right += 2; tron@5027: bottom += 2; tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && tron@5027: top < wp->sign.top + 24 && tron@5027: right > wp->sign.left && tron@5027: left < wp->sign.left + wp->sign.width_1*2) { tron@5027: AddWaypoint(wp, STR_WAYPOINT_VIEWPORT, wp->sign.width_1); tron@5027: } truelight@0: } tron@5027: break; tron@5027: tron@5027: default: NOT_REACHED(); tron@5027: case 2: tron@5027: right += 4; tron@5027: bottom += 5; tron@5027: FOR_ALL_WAYPOINTS(wp) { tron@5027: if (bottom > wp->sign.top && tron@5027: top < wp->sign.top + 24 && tron@5027: right > wp->sign.left && tron@5027: left < wp->sign.left + wp->sign.width_2*4) { tron@5027: AddWaypoint(wp, STR_WAYPOINT_VIEWPORT_TINY, wp->sign.width_2 | 0x8000); tron@5027: } truelight@0: } tron@5027: break; truelight@0: } truelight@0: } truelight@0: truelight@0: void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str) truelight@0: { truelight@0: char buffer[128]; tron@2116: uint w; truelight@0: truelight@0: sign->top = top; truelight@0: Darkvater@4912: GetString(buffer, str, lastof(buffer)); Darkvater@4609: w = GetStringBoundingBox(buffer).width + 3; truelight@0: sign->width_1 = w; tron@2116: sign->left = left - w / 2; truelight@0: Darkvater@4609: /* zoomed out version */ peter1138@3798: _cur_fontsize = FS_SMALL; Darkvater@4609: w = GetStringBoundingBox(buffer).width + 3; peter1138@3798: _cur_fontsize = FS_NORMAL; Darkvater@1390: sign->width_2 = w; truelight@0: } truelight@0: truelight@0: tron@410: static void ViewportDrawTileSprites(TileSpriteToDraw *ts) truelight@193: { truelight@0: do { truelight@0: Point pt = RemapCoords(ts->x, ts->y, ts->z); truelight@0: DrawSprite(ts->image, pt.x, pt.y); tron@2116: ts = ts->next; tron@2116: } while (ts != NULL); truelight@0: } truelight@0: belugas@4171: static void ViewportSortParentSprites(ParentSpriteToDraw *psd[]) truelight@0: { tron@2116: while (*psd != NULL) { tron@2116: ParentSpriteToDraw* ps = *psd; truelight@0: truelight@0: if (!(ps->unk16 & 1)) { tron@2116: ParentSpriteToDraw** psd2 = psd; tron@2116: truelight@0: ps->unk16 |= 1; truelight@193: tron@2116: while (*++psd2 != NULL) { tron@2116: ParentSpriteToDraw* ps2 = *psd2; tron@4188: ParentSpriteToDraw** psd3; hackykid@1934: tron@2116: if (ps2->unk16 & 1) continue; truelight@0: tron@4187: /* Decide which comparator to use, based on whether the bounding tron@4187: * boxes overlap tron@4187: */ tron@4187: if (ps->xmax > ps2->xmin && ps->xmin < ps2->xmax && // overlap in X? tron@4187: ps->ymax > ps2->ymin && ps->ymin < ps2->ymax && // overlap in Y? tron@4187: ps->zmax > ps2->zmin && ps->zmin < ps2->zmax) { // overlap in Z? tron@4188: /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of tron@4188: * the screen and with higher Z elevation, are drawn in front. tron@4188: * Here X,Y,Z are the coordinates of the "center of mass" of the sprite, tron@4188: * i.e. X=(left+right)/2, etc. tron@4188: * However, since we only care about order, don't actually divide / 2 tron@4188: */ tron@4188: if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <= tron@4188: ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) { tron@4188: continue; tron@4188: } hackykid@1934: } else { tron@4188: if (ps->xmax < ps2->xmin || tron@4188: ps->ymax < ps2->ymin || tron@4188: ps->zmax < ps2->zmin || ( tron@4188: ps->xmin < ps2->xmax && tron@4188: ps->ymin < ps2->ymax && tron@4188: ps->zmin < ps2->zmax tron@4188: )) { tron@4188: continue; tron@4188: } hackykid@1934: } tron@2116: tron@4188: // Swap the two sprites ps and ps2 using bubble-sort algorithm. tron@4188: psd3 = psd; tron@4188: do { tron@4188: ParentSpriteToDraw* temp = *psd3; tron@4188: *psd3 = ps2; tron@4188: ps2 = temp; truelight@0: tron@4188: psd3++; tron@4188: } while (psd3 <= psd2); truelight@0: } truelight@0: } else { truelight@0: psd++; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2116: static void ViewportDrawParentSprites(ParentSpriteToDraw *psd[]) truelight@0: { tron@2116: for (; *psd != NULL; psd++) { tron@2116: const ParentSpriteToDraw* ps = *psd; tron@4186: Point pt = RemapCoords(ps->xmin, ps->ymin, ps->zmin); tron@2116: const ChildScreenSpriteToDraw* cs; truelight@0: truelight@0: DrawSprite(ps->image, pt.x, pt.y); truelight@0: tron@2116: for (cs = ps->child; cs != NULL; cs = cs->next) { truelight@0: DrawSprite(cs->image, ps->left + cs->x, ps->top + cs->y); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2116: static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDraw *ss) truelight@0: { truelight@0: DrawPixelInfo dp; truelight@0: byte zoom; truelight@193: truelight@0: _cur_dpi = &dp; truelight@0: dp = *dpi; truelight@0: tron@2116: zoom = dp.zoom; truelight@0: dp.zoom = 0; truelight@0: truelight@0: dp.left >>= zoom; truelight@0: dp.top >>= zoom; truelight@0: dp.width >>= zoom; truelight@0: dp.height >>= zoom; truelight@0: truelight@0: do { tron@5025: uint16 colour; tron@5025: truelight@0: if (ss->width != 0) { tron@2116: int x = (ss->x >> zoom) - 1; tron@2116: int y = (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) */ tron@2116: if (!(_display_opt & DO_TRANS_SIGNS) || ss->string == STR_2806) tron@2116: DrawFrameRect( tron@2116: x, y, x + w, bottom, ss->color, tron@4439: (_display_opt & DO_TRANS_BUILDINGS) ? FR_TRANSPARENT : 0 tron@2116: ); 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 */ tron@2116: if (( tron@2116: (_display_opt & DO_TRANS_BUILDINGS) || tron@2116: (_display_opt & DO_TRANS_SIGNS && ss->string != STR_2806) tron@2116: ) && 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 { tron@5025: colour = 16; truelight@0: } tron@5025: DrawString( tron@5025: ss->x >> zoom, (ss->y >> zoom) - (ss->width & 0x8000 ? 2 : 0), tron@5025: ss->string, colour tron@5025: ); tron@2116: tron@2116: ss = ss->next; tron@2116: } while (ss != NULL); truelight@0: } truelight@0: tron@430: void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: ViewportDrawer vd; truelight@0: int mask; tron@2116: int x; tron@2116: int y; truelight@0: DrawPixelInfo *old_dpi; truelight@0: truelight@0: byte mem[VIEWPORT_DRAW_MEM]; truelight@137: ParentSpriteToDraw *parent_list[6144]; truelight@0: truelight@0: _cur_vd = &vd; truelight@0: truelight@0: old_dpi = _cur_dpi; truelight@0: _cur_dpi = &vd.dpi; truelight@0: truelight@0: vd.dpi.zoom = vp->zoom; truelight@0: mask = (-1) << vp->zoom; truelight@0: truelight@0: vd.combine_sprites = 0; truelight@0: truelight@0: vd.dpi.width = (right - left) & mask; truelight@0: vd.dpi.height = (bottom - top) & mask; truelight@0: vd.dpi.left = left & mask; truelight@0: vd.dpi.top = top & mask; truelight@0: vd.dpi.pitch = old_dpi->pitch; truelight@0: truelight@0: x = ((vd.dpi.left - (vp->virtual_left&mask)) >> vp->zoom) + vp->left; truelight@0: y = ((vd.dpi.top - (vp->virtual_top&mask)) >> vp->zoom) + vp->top; truelight@0: truelight@0: vd.dpi.dst_ptr = old_dpi->dst_ptr + x - old_dpi->left + (y - old_dpi->top) * old_dpi->pitch; truelight@0: truelight@0: vd.parent_list = parent_list; tron@2116: vd.eof_parent_list = endof(parent_list); truelight@0: vd.spritelist_mem = mem; tron@2116: vd.eof_spritelist_mem = endof(mem) - sizeof(LARGEST_SPRITELIST_STRUCT); truelight@0: vd.last_string = &vd.first_string; truelight@0: vd.first_string = NULL; truelight@0: vd.last_tile = &vd.first_tile; truelight@0: vd.first_tile = NULL; truelight@193: truelight@0: ViewportAddLandscape(); truelight@0: #if !defined(NEW_ROTATION) truelight@0: ViewportAddVehicles(&vd.dpi); truelight@0: DrawTextEffects(&vd.dpi); truelight@0: truelight@0: ViewportAddTownNames(&vd.dpi); truelight@0: ViewportAddStationNames(&vd.dpi); truelight@0: ViewportAddSigns(&vd.dpi); darkvater@395: ViewportAddWaypoints(&vd.dpi); truelight@0: #endif truelight@0: truelight@133: // This assert should never happen (because the length of the parent_list truelight@133: // is checked) tron@979: assert(vd.parent_list <= endof(parent_list)); truelight@133: tron@2116: if (vd.first_tile != NULL) ViewportDrawTileSprites(vd.first_tile); truelight@0: truelight@0: /* null terminate parent sprite list */ truelight@0: *vd.parent_list = NULL; truelight@0: truelight@0: ViewportSortParentSprites(parent_list); truelight@0: ViewportDrawParentSprites(parent_list); truelight@193: tron@2116: if (vd.first_string != NULL) ViewportDrawStrings(&vd.dpi, vd.first_string); truelight@193: truelight@0: _cur_dpi = old_dpi; truelight@0: } truelight@0: truelight@0: // Make sure we don't draw a too big area at a time. truelight@0: // 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: { Darkvater@5417: if (((bottom - top) * (right - left) << (2 * 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@0: ((left - vp->left) << vp->zoom) + vp->virtual_left, truelight@0: ((top - vp->top) << vp->zoom) + vp->virtual_top, truelight@0: ((right - vp->left) << vp->zoom) + vp->virtual_left, truelight@0: ((bottom - vp->top) << vp->zoom) + vp->virtual_top truelight@0: ); truelight@0: } truelight@0: } truelight@0: Darkvater@5120: static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { tron@2116: if (right <= vp->left || bottom <= vp->top) return; truelight@0: tron@2116: if (left >= vp->left + vp->width) return; truelight@0: truelight@0: if (left < vp->left) left = vp->left; tron@2116: if (right > vp->left + vp->width) right = vp->left + vp->width; truelight@0: tron@2116: if (top >= vp->top + vp->height) return; truelight@0: truelight@0: if (top < vp->top) top = vp->top; tron@2116: if (bottom > vp->top + vp->height) bottom = vp->top + vp->height; truelight@0: truelight@0: ViewportDrawChk(vp, left, top, right, bottom); truelight@0: } truelight@0: Darkvater@5120: void DrawWindowViewport(const Window *w) tron@2116: { truelight@0: DrawPixelInfo *dpi = _cur_dpi; truelight@0: truelight@0: dpi->left += w->left; truelight@0: dpi->top += w->top; truelight@0: truelight@0: ViewportDraw(w->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height); truelight@0: truelight@0: dpi->left -= w->left; truelight@0: dpi->top -= w->top; truelight@0: } truelight@0: truelight@0: void UpdateViewportPosition(Window *w) truelight@0: { tron@2116: const ViewPort *vp = w->viewport; truelight@0: tron@2116: if (WP(w, vp_d).follow_vehicle != INVALID_VEHICLE) { tron@2116: const Vehicle* veh = GetVehicle(WP(w,vp_d).follow_vehicle); tron@2116: Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: truelight@0: SetViewportPosition(w, pt.x, pt.y); truelight@0: } else { tron@632: #if !defined(NEW_ROTATION) tron@632: int x; tron@632: int y; tron@632: int vx; tron@632: int vy; tron@632: tron@632: // Center of the viewport is hot spot tron@632: x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2; tron@632: y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2; tron@632: // Convert viewport coordinates to map coordinates tron@632: // Calculation is scaled by 4 to avoid rounding errors tron@632: vx = -x + y * 2; tron@632: vy = x + y * 2; tron@632: // clamp to size of map celestar@3421: vx = clamp(vx, 0 * 4, MapMaxX() * TILE_SIZE * 4); celestar@3421: vy = clamp(vy, 0 * 4, MapMaxY() * TILE_SIZE * 4); tron@632: // Convert map coordinates to viewport coordinates tron@632: x = (-vx + vy) / 2; tron@632: y = ( vx + vy) / 4; tron@632: // Set position tron@2116: WP(w, vp_d).scrollpos_x = x - vp->virtual_width / 2; tron@2116: WP(w, vp_d).scrollpos_y = y - vp->virtual_height / 2; tron@632: #else truelight@0: int x,y,t; truelight@0: int err; truelight@0: truelight@0: x = WP(w,vp_d).scrollpos_x >> 2; truelight@0: y = WP(w,vp_d).scrollpos_y >> 1; truelight@0: truelight@0: t = x; truelight@0: x = x + y; truelight@0: y = x - y; truelight@0: err= 0; truelight@0: truelight@0: if (err != 0) { truelight@0: /* coordinate remap */ truelight@0: Point pt = RemapCoords(x, y, 0); truelight@193: t = (-1) << vp->zoom; truelight@0: WP(w,vp_d).scrollpos_x = pt.x & t; truelight@0: WP(w,vp_d).scrollpos_y = pt.y & t; truelight@0: } tron@632: #endif tron@632: tron@2116: SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y); truelight@0: } truelight@0: } truelight@0: 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@0: (left >> vp->zoom) + vp->left, truelight@0: (top >> vp->zoom) + vp->top, truelight@0: (right >> vp->zoom) + vp->left, truelight@0: (bottom >> vp->zoom) + vp->top truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkAllViewportsDirty(int left, int top, int right, int bottom) truelight@0: { tron@2116: const ViewPort *vp = _viewports; truelight@0: uint32 act = _active_viewports; truelight@0: do { truelight@0: if (act & 1) { truelight@0: assert(vp->width != 0); truelight@0: MarkViewportDirty(vp, left, top, right, bottom); truelight@0: } truelight@0: } while (vp++,act>>=1); truelight@0: } truelight@0: tron@2116: void MarkTileDirtyByTile(TileIndex tile) tron@2116: { celestar@3421: Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTileZ(tile)); truelight@0: MarkAllViewportsDirty( truelight@0: pt.x - 31, truelight@0: pt.y - 122, truelight@0: pt.x - 31 + 67, truelight@0: pt.y - 122 + 154 truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkTileDirty(int x, int y) truelight@0: { tron@2116: uint z = 0; truelight@0: Point pt; tron@2116: celestar@3421: if (IS_INT_INSIDE(x, 0, MapSizeX() * TILE_SIZE) && celestar@3421: IS_INT_INSIDE(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: tron@1093: static void SetSelectionTilesDirty(void) 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: tron@2116: if (!(_display_opt & DO_SHOW_TOWN_NAMES)) return false; truelight@0: tron@4471: switch (vp->zoom) { tron@4471: case 0: 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: tron@4471: case 1: 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: tron@4471: default: tron@4471: x = (x - vp->left + 3) * 4 + vp->virtual_left; tron@4471: y = (y - vp->top + 3) * 4 + 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_2 * 4) { tron@4471: ShowTownViewWindow(t->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; 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: tron@2116: if (!(_display_opt & DO_SHOW_STATION_NAMES)) return false; truelight@0: tron@4471: switch (vp->zoom) { tron@4471: case 0: 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: tron@4471: case 1: 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: tron@4471: default: tron@4471: x = (x - vp->left + 3) * 4 + vp->virtual_left; tron@4471: y = (y - vp->top + 3) * 4 + 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_2 * 4) { tron@4471: ShowStationViewWindow(st->index); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; 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: Darkvater@5443: if (!(_display_opt & DO_SHOW_SIGNS) || _current_player == PLAYER_SPECTATOR) return false; truelight@0: tron@4471: switch (vp->zoom) { tron@4471: case 0: tron@4471: x = x - vp->left + vp->virtual_left; tron@4471: y = y - vp->top + vp->virtual_top; tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && tron@4471: y < si->sign.top + 12 && tron@4471: x >= si->sign.left && tron@4471: x < si->sign.left + si->sign.width_1) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: tron@4471: case 1: tron@4471: x = (x - vp->left + 1) * 2 + vp->virtual_left; tron@4471: y = (y - vp->top + 1) * 2 + vp->virtual_top; tron@4471: FOR_ALL_SIGNS(si) { tron@4471: if (y >= si->sign.top && tron@4471: y < si->sign.top + 24 && tron@4471: x >= si->sign.left && tron@4471: x < si->sign.left + si->sign.width_1 * 2) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; tron@4471: tron@4471: default: tron@4471: x = (x - vp->left + 3) * 4 + vp->virtual_left; tron@4471: y = (y - vp->top + 3) * 4 + 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_2 * 4) { tron@4471: ShowRenameSignWindow(si); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; 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: tron@2116: if (!(_display_opt & DO_WAYPOINTS)) return false; truelight@0: tron@4471: switch (vp->zoom) { tron@4471: case 0: 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: tron@4471: case 1: 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: tron@4471: default: tron@4471: x = (x - vp->left + 3) * 4 + vp->virtual_left; tron@4471: y = (y - vp->top + 3) * 4 + 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_2 * 4) { tron@4471: ShowRenameWaypointWindow(wp); tron@4471: return true; tron@4471: } truelight@0: } tron@4471: break; 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: { tron@4077: if (!IsFrontEngine(v)) v = GetFirstVehicleInChain(v); tron@4077: ShowTrainViewWindow(v); tron@2662: } tron@2662: 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, tron@2662: ShowRoadVehViewWindow, tron@2662: ShowShipViewWindow, tron@2662: ShowAircraftViewWindow, 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) { celestar@3809: DEBUG(misc, 2) ("Vehicle %d at %p", v->index, v); celestar@3809: _on_vehicle_click_proc[v->type - 0x10](v); celestar@3809: } truelight@0: } truelight@0: tron@1093: Vehicle *CheckMouseOverVehicle(void) 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: tron@1093: void PlaceObject(void) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@193: truelight@0: pt = GetTileBelowCursor(); tron@2116: if (pt.x == -1) return; truelight@0: dominik@1070: if (_thd.place_mode == VHM_POINT) { truelight@0: pt.x += 8; truelight@0: pt.y += 8; truelight@0: } truelight@0: truelight@0: _tile_fract_coords.x = pt.x & 0xF; truelight@0: _tile_fract_coords.y = pt.y & 0xF; truelight@0: tron@2116: w = GetCallbackWnd(); tron@2116: if (w != NULL) { tron@2116: WindowEvent e; tron@2116: truelight@0: e.event = WE_PLACE_OBJ; belugas@4634: e.we.place.pt = pt; belugas@4634: e.we.place.tile = TileVirtXY(pt.x, pt.y); truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: } truelight@0: darkvater@152: darkvater@152: /* scrolls the viewport in a window to a given location */ belugas@4171: bool ScrollWindowTo(int x , int y, Window *w) darkvater@152: { darkvater@152: Point pt; darkvater@152: darkvater@152: pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(x, y)); tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; darkvater@152: tron@2116: if (WP(w, vp_d).scrollpos_x == pt.x && WP(w, vp_d).scrollpos_y == pt.y) tron@2116: return false; darkvater@152: tron@2116: WP(w, vp_d).scrollpos_x = pt.x; tron@2116: WP(w, vp_d).scrollpos_y = pt.y; darkvater@152: return true; darkvater@152: } darkvater@152: darkvater@152: truelight@0: bool ScrollMainWindowTo(int x, int y) truelight@0: { truelight@4339: Window *w; truelight@4339: bool res = ScrollWindowTo(x, y, FindWindowById(WC_MAIN_WINDOW, 0)); truelight@4339: truelight@4339: /* If a user scrolls to a tile (via what way what so ever) and already is on truelight@4339: * that tile (e.g.: pressed twice), move the smallmap to that location, truelight@4339: * so you directly see where you are on the smallmap. */ truelight@4339: truelight@4339: if (res) return res; truelight@4339: truelight@4339: w = FindWindowById(WC_SMALLMAP, 0); truelight@4339: if (w == NULL) return res; truelight@4339: truelight@4339: SmallMapCenterOnCurrentPos(w); truelight@4339: truelight@4339: return res; truelight@0: } truelight@0: truelight@0: truelight@0: bool ScrollMainWindowToTile(TileIndex tile) truelight@0: { tron@3645: return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2); 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: dominik@1070: /* returns the best autorail highlight type from map coordinates */ dominik@1070: static byte GetAutorailHT(int x, int y) dominik@1070: { Darkvater@2710: return HT_RAIL | _AutorailPiece[x & 0xF][y & 0xF]; dominik@1070: } truelight@0: dominik@1070: // called regular to update tile highlighting in all cases tron@1093: void UpdateTileSelection(void) 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) { tron@1863: int x2 = _thd.selstart.x; tron@1863: int y2 = _thd.selstart.y; truelight@0: x1 &= ~0xF; truelight@0: y1 &= ~0xF; truelight@193: truelight@0: if (x1 >= x2) intswap(x1,x2); truelight@0: if (y1 >= y2) intswap(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 truelight@0: } tron@1863: _thd.new_pos.x = x1 & ~0xF; tron@1863: _thd.new_pos.y = y1 & ~0xF; truelight@0: } truelight@0: } truelight@0: dominik@1070: // 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 || Darkvater@4539: _thd.outersize.x != _thd.new_outersize.x || Darkvater@4539: _thd.outersize.y != _thd.new_outersize.y) { truelight@0: // 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: truelight@0: // draw the new selection? tron@1863: if (_thd.new_drawstyle) SetSelectionTilesDirty(); truelight@0: } truelight@0: } truelight@0: dominik@1070: // highlighting tiles while only going over them with the mouse tron@1977: void VpStartPlaceSizing(TileIndex tile, int user) truelight@0: { tron@1863: _thd.userdata = user; 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; 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: { Darkvater@4834: uint 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: tron@2817: static void VpStartPreSizing(void) truelight@0: { truelight@0: _thd.selend.x = -1; truelight@0: _special_mouse_mode = WSM_PRESIZE; truelight@0: } truelight@0: tron@1109: /* returns information about the 2x1 piece to be build. dominik@1070: * The lower bits (0-3) are the track type. */ dominik@1070: static byte 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) { dominik@1070: case 0: // end piece is lower right dominik@1070: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return 3; } dominik@1070: if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return 5; } dominik@1070: return 1; dominik@1070: dominik@1070: case 1: dominik@1070: if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return 4; } dominik@1070: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return 2; } dominik@1070: return 1; dominik@1070: dominik@1070: case 2: dominik@1070: if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return 4; } dominik@1070: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return 3; } dominik@1070: return 0; dominik@1070: dominik@1070: case 3: dominik@1070: if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return 5; } dominik@1070: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return 2; } dominik@1070: return 0; dominik@1070: } dominik@1070: dominik@1070: return 0; // avoids compiler warnings 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 Darkvater@4834: * @param start_tile, end_tile start and end tile of drag Darkvater@4834: * @param 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 Darkvater@4834: * 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; Darkvater@4834: if (swap) swap_tile(&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]))); Darkvater@4834: h0 = maxu(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]))); Darkvater@4834: h1 = maxu(h1, ht); Darkvater@4834: } break; Darkvater@4834: } Darkvater@4834: Darkvater@4834: if (swap) swap_uint32(&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: dominik@1070: // 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); dominik@1070: w = myabs(dx) + 16; dominik@1070: h = myabs(dy) + 16; truelight@0: tron@1980: if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile tron@4077: if (method == VPM_RAILDIRS) { tron@2116: b = GetAutorailHT(x, y); tron@4077: } else { // rect for autosignals on one tile tron@2116: b = HT_RECT; tron@4077: } dominik@1070: } else if (h == 16) { // Is this in X direction? tron@4077: if (dx == 16) { // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(3)) | HT_LINE; tron@4077: } else if (dx == -16) { dominik@1070: b = (Check2x1AutoRail(2)) | HT_LINE; tron@4077: } else { dominik@1070: b = HT_LINE | HT_DIR_X; tron@4077: } dominik@1070: y = thd->selstart.y; dominik@1070: } else if (w == 16) { // Or Y direction? tron@4077: if (dy == 16) { // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(1)) | HT_LINE; tron@4077: } else if (dy == -16) { // 2x1 other direction dominik@1070: b = (Check2x1AutoRail(0)) | HT_LINE; tron@4077: } else { dominik@1070: b = HT_LINE | HT_DIR_Y; tron@4077: } truelight@0: x = thd->selstart.x; dominik@1070: } else if (w > h * 2) { // still count as x dir? tron@2116: b = HT_LINE | HT_DIR_X; truelight@0: y = thd->selstart.y; dominik@1070: } else if (h > w * 2) { // still count as y dir? tron@2116: b = HT_LINE | HT_DIR_Y; dominik@1070: x = thd->selstart.x; dominik@1070: } else { // complicated direction Darkvater@4799: int d = w - h; tron@2116: thd->selend.x = thd->selend.x & ~0xF; tron@2116: thd->selend.y = thd->selend.y & ~0xF; truelight@0: truelight@0: // four cases. truelight@0: if (x > thd->selstart.x) { truelight@0: if (y > thd->selstart.y) { truelight@0: // south tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: // return px == py || px == py + 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: } // return px == py || px == py - 16; truelight@0: } else { truelight@0: // west tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: } truelight@0: } truelight@0: } else { truelight@0: if (y > thd->selstart.y) { truelight@0: // east tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: // return px == -py || px == -py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } // return px == -py || px == -py + 16; truelight@0: } else { truelight@0: // north tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: // return px == py || px == py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: } //return px == py || px == py + 16; truelight@0: } truelight@0: } truelight@0: } Darkvater@4834: Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4834: TileIndex t0 = TileVirtXY(thd->selstart.x, thd->selstart.y); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); Darkvater@4834: uint distance = DistanceManhattan(t0, t1) + 1; Darkvater@4884: byte index = 0; Darkvater@4834: uint 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 */ truelight@0: void VpSelectTilesWithMethod(int x, int y, int 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: 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 */ tron@4077: if (myabs(sy - y) < myabs(sx - x)) { tron@4077: y = sy; Darkvater@4834: style = HT_DIR_X; tron@4077: } else { tron@4077: x = sx; Darkvater@4834: style = HT_DIR_Y; tron@4077: } Darkvater@4834: goto calc_heightdiff_single_direction; Darkvater@4834: case VPM_FIX_X: /* drag in Y direction */ Darkvater@4834: x = sx; Darkvater@4834: style = HT_DIR_Y; Darkvater@4834: goto calc_heightdiff_single_direction; Darkvater@4834: case VPM_FIX_Y: /* drag in X direction */ Darkvater@4834: y = sy; Darkvater@4834: style = HT_DIR_X; Darkvater@4834: Darkvater@4834: calc_heightdiff_single_direction:; Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4834: TileIndex t0 = TileVirtXY(sx, sy); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); Darkvater@4834: uint distance = DistanceManhattan(t0, t1) + 1; Darkvater@4884: byte index = 0; Darkvater@4834: uint 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; tron@2116: x = sx + clamp(x - sx, -limit, limit); tron@2116: y = sy + clamp(y - sy, -limit, limit); Darkvater@4834: /* Fallthrough */ Darkvater@4884: case VPM_X_AND_Y: /* drag an X by Y area */ Darkvater@4834: if (_patches.measure_tooltip) { Darkvater@4884: static const StringID measure_strings_area[] = { Darkvater@4884: STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF Darkvater@4884: }; Darkvater@4884: Darkvater@4834: TileIndex t0 = TileVirtXY(sx, sy); Darkvater@4834: TileIndex t1 = TileVirtXY(x, y); Darkvater@4834: uint dx = abs(TileX(t0) - TileX(t1)) + 1; Darkvater@4834: uint dy = abs(TileY(t0) - TileY(t1)) + 1; Darkvater@4884: byte index = 0; Darkvater@4834: uint 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 */ Darkvater@4838: style = _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: tron@1109: // while dragging tron@1093: bool VpHandlePlaceSizingDrag(void) truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@193: tron@2116: if (_special_mouse_mode != WSM_SIZING) return true; truelight@0: belugas@4634: e.we.place.userdata = _thd.userdata; truelight@0: dominik@1070: // stop drag mode if the window has been closed truelight@0: w = FindWindowById(_thd.window_class,_thd.window_number); truelight@0: if (w == NULL) { truelight@0: ResetObjectToPlace(); truelight@0: return false; truelight@0: } truelight@0: dominik@1070: // while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) truelight@0: if (_left_button_down) { truelight@0: e.event = WE_PLACE_DRAG; belugas@4634: e.we.place.pt = GetTileBelowCursor(); truelight@0: w->wndproc(w, &e); truelight@0: return false; truelight@0: } truelight@0: truelight@0: // mouse button released.. truelight@0: // 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; belugas@4634: } else if ((e.we.place.userdata & 0xF) == 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: truelight@0: // and call the mouseup event. truelight@0: e.event = WE_PLACE_MOUSEUP; belugas@4634: e.we.place.pt = _thd.selend; belugas@4634: e.we.place.tile = TileVirtXY(e.we.place.pt.x, e.we.place.pt.y); belugas@4634: e.we.place.starttile = TileVirtXY(_thd.selstart.x, _thd.selstart.y); truelight@0: w->wndproc(w, &e); truelight@193: truelight@0: return false; truelight@0: } truelight@0: Darkvater@1914: void SetObjectToPlaceWnd(CursorID icon, byte mode, Window *w) truelight@0: { Darkvater@1914: SetObjectToPlace(icon, mode, w->window_class, w->window_number); truelight@0: } truelight@0: truelight@0: #include "table/animcursors.h" truelight@0: Darkvater@1914: void SetObjectToPlace(CursorID icon, byte mode, WindowClass window_class, WindowNumber window_num) truelight@0: { truelight@0: Window *w; truelight@0: dominik@1070: // undo clicking on button tron@1863: if (_thd.place_mode != 0) { tron@1863: _thd.place_mode = 0; tron@1863: w = FindWindowById(_thd.window_class, _thd.window_number); tron@2116: if (w != NULL) CallWindowEventNP(w, WE_ABORT_PLACE_OBJ); truelight@0: } truelight@193: truelight@0: SetTileSelectSize(1, 1); truelight@193: tron@1863: _thd.make_square_red = false; truelight@0: dominik@1070: if (mode == VHM_DRAG) { // mode 4 is for dragdropping trains in the depot window truelight@0: mode = 0; truelight@0: _special_mouse_mode = WSM_DRAGDROP; truelight@0: } else { truelight@0: _special_mouse_mode = WSM_NONE; truelight@0: } truelight@0: tron@1863: _thd.place_mode = mode; tron@1863: _thd.window_class = window_class; tron@1863: _thd.window_number = window_num; truelight@0: dominik@1070: if (mode == VHM_SPECIAL) // special tools, like tunnels or docks start with presizing mode truelight@0: VpStartPreSizing(); truelight@193: truelight@0: if ( (int)icon < 0) Darkvater@4334: SetAnimatedMouseCursor(_animcursors[~icon]); truelight@0: else truelight@0: SetMouseCursor(icon); truelight@0: } truelight@0: tron@1093: void ResetObjectToPlace(void) tron@1093: { tron@4077: SetObjectToPlace(SPR_CURSOR_MOUSE, VHM_NONE, 0, 0); truelight@0: }