truelight@0: #include "stdafx.h" truelight@0: #include "ttd.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@0: truelight@133: #define VIEWPORT_DRAW_MEM (65536 * 2) truelight@0: truelight@0: static bool _added_tile_sprite; truelight@0: static bool _offset_ground_sprites; truelight@0: 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; truelight@835: uint32 params[3]; 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@849: int32 tile_x; tron@849: int32 tile_y; tron@849: int32 tile_right; tron@849: int32 tile_bottom; truelight@0: ChildScreenSpriteToDraw *child; truelight@0: byte unk16; truelight@0: byte tile_z; truelight@0: byte tile_z_bottom; truelight@0: } ParentSpriteToDraw; truelight@0: truelight@0: typedef struct ViewportDrawer { truelight@0: DrawPixelInfo dpi; truelight@193: truelight@0: byte *spritelist_mem, *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; truelight@133: ParentSpriteToDraw **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: bool ground_child; truelight@0: } ViewportDrawer; truelight@0: truelight@0: static ViewportDrawer *_cur_vd; truelight@0: truelight@0: TileHighlightData * const _thd_ptr = &_thd; truelight@0: static TileInfo *_cur_ti; truelight@0: truelight@0: truelight@0: Point MapXYZToViewport(ViewPort *vp, uint x, uint y, uint z) truelight@0: { truelight@0: Point p = RemapCoords(x, y, z); truelight@0: p.x -= (vp->virtual_width>>1); truelight@0: p.y -= (vp->virtual_height>>1); truelight@0: return p; truelight@0: } truelight@0: 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: byte z; truelight@0: uint32 bit; truelight@0: truelight@0: for(vp=_viewports,bit=1; ; vp++, bit<<=1) { truelight@0: assert(vp!=endof(_viewports)); truelight@0: if (vp->width == 0) truelight@0: break; truelight@0: } truelight@0: _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) { truelight@0: Vehicle *veh; truelight@0: WP(w,vp_d).follow_vehicle = (VehicleID)(follow_flags & 0xFFFF); truelight@919: 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 { tron@926: int x = TileX(follow_flags) * 16; tron@926: int y = TileY(follow_flags) * 16; truelight@0: WP(w,vp_d).follow_vehicle = 0xFFFF; truelight@0: z = GetSlopeZ(x,y); truelight@0: pt = MapXYZToViewport(vp, x,y, z); truelight@0: } truelight@0: truelight@0: WP(w,vp_d).scrollpos_x = pt.x; truelight@0: 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: truelight@0: static void DoSetViewportPosition(Window *w, int left, int top, int width, int height) truelight@0: { truelight@0: for (; w < _last_window; w++) { truelight@0: if (left + width > w->left && truelight@0: w->left+w->width > left && truelight@0: top + height > w->top && truelight@0: w->top+w->height > top) { truelight@193: truelight@0: if (left < w->left) { truelight@0: DoSetViewportPosition(w, left, top, w->left - left, height); truelight@0: DoSetViewportPosition(w, 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) { truelight@0: DoSetViewportPosition(w, left, top, (w->left + w->width - left), height); truelight@0: DoSetViewportPosition(w, 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) { truelight@0: DoSetViewportPosition(w, left, top, width, (w->top - top)); truelight@0: DoSetViewportPosition(w, 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) { truelight@0: DoSetViewportPosition(w, left, top, width, (w->top + w->height - top)); truelight@0: DoSetViewportPosition(w, 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 */ truelight@0: 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: truelight@0: 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: truelight@0: if (old_top == 0 && old_left == 0) truelight@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: truelight@0: if ( (i=(left + width - _screen.width)) >= 0) truelight@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: truelight@0: if ( (i=(top + height - _screen.height)) >= 0) { truelight@0: height -= i; truelight@0: } truelight@0: truelight@0: if (height > 0) truelight@0: DoSetViewportPosition(w+1, left, top, width, height); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: ViewPort *IsPtInWindowViewport(Window *w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; 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@1095: static Point TranslateXYToTileCoord(ViewPort *vp, int x, int y) tron@1095: { truelight@0: int z; truelight@0: Point pt; truelight@0: int a,b; 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: z = GetSlopeZ(a, b) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: truelight@0: pt.x = a+z; truelight@0: pt.y = b+z; truelight@193: tron@856: if ((uint)pt.x >= MapMaxX() * 16 || (uint)pt.y >= MapMaxY() * 16) { truelight@0: pt.x = pt.y = -1; truelight@0: } 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: truelight@0: if (in) { darkvater@152: x = ( (_cursor.pos.x - vp->left ) >> 1) + (vp->width >> 2); darkvater@152: y = ( (_cursor.pos.y - vp->top ) >> 1) + (vp->height >> 2); truelight@0: } truelight@0: 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: 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: truelight@0: assert( (image & 0x3fff) < NUM_SPRITES); truelight@0: truelight@0: ts = (TileSpriteToDraw*)vd->spritelist_mem; truelight@0: if ((byte*)ts >= vd->eof_spritelist_mem) { truelight@0: DEBUG(misc, 0) ("Out of sprite mem\n"); truelight@0: return; truelight@0: } 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: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: int t; truelight@0: uint32 image_org = image; truelight@0: const SpriteDimension *sd; truelight@0: Point pt = RemapCoords(x, y, z); truelight@0: truelight@0: sd = GetSpriteDimension(image & 0x3FFF); truelight@0: truelight@193: if ((t = pt.x + sd->xoffs) >= vd->dpi.left + vd->dpi.width || truelight@0: (t + sd->xsize) <= vd->dpi.left || truelight@0: (t = pt.y + sd->yoffs) >= vd->dpi.top + vd->dpi.height || truelight@0: (t + sd->ysize) <= vd->dpi.top) truelight@0: return; truelight@0: truelight@0: AddChildSpriteScreen(image_org, 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; truelight@0: const SpriteDimension *sd; truelight@0: Point pt; truelight@0: truelight@0: assert( (image & 0x3fff) < NUM_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: truelight@0: ps = (ParentSpriteToDraw*)vd->spritelist_mem; truelight@0: if ((byte*)ps >= vd->eof_spritelist_mem) { truelight@0: DEBUG(misc, 0) ("Out of sprite mem\n"); truelight@0: return; truelight@0: } 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 ;) truelight@133: DEBUG(misc, 0) ("Out of sprite mem (parent_list)\n"); truelight@133: return; truelight@193: } truelight@193: truelight@193: vd->spritelist_mem += sizeof(ParentSpriteToDraw); truelight@0: truelight@0: ps->image = image; truelight@0: ps->tile_x = x; truelight@0: ps->tile_right = x + w - 1; truelight@0: truelight@0: ps->tile_y = y; truelight@0: ps->tile_bottom = y + h - 1; truelight@0: truelight@0: ps->tile_z = z; truelight@0: ps->tile_z_bottom = z + dz - 1; truelight@0: truelight@0: pt = RemapCoords(x, y, z); truelight@0: truelight@0: sd = GetSpriteDimension(image & 0x3FFF); truelight@193: if ((ps->left = (pt.x += sd->xoffs)) >= vd->dpi.left + vd->dpi.width || truelight@0: (ps->right = (pt.x + sd->xsize)) <= vd->dpi.left || truelight@0: (ps->top = (pt.y += sd->yoffs)) >= vd->dpi.top + vd->dpi.height || truelight@0: (ps->bottom = (pt.y + sd->ysize)) <= vd->dpi.top) { truelight@0: return; truelight@0: } 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: truelight@0: if (vd->combine_sprites == 1) { truelight@0: vd->combine_sprites = 2; truelight@0: } 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: truelight@133: assert( (image & 0x3fff) < NUM_SPRITES); truelight@0: truelight@0: cs = (ChildScreenSpriteToDraw*) vd->spritelist_mem; truelight@0: if ((byte*)cs >= vd->eof_spritelist_mem) { truelight@0: DEBUG(misc,0) ("Out of sprite mem\n"); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (vd->last_child == NULL) truelight@0: 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 */ truelight@835: void *AddStringToDraw(int x, int y, StringID string, uint32 params_1, uint32 params_2, uint32 params_3) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: StringSpriteToDraw *ss; truelight@0: truelight@0: ss = (StringSpriteToDraw*)vd->spritelist_mem; truelight@0: if ((byte*)ss >= vd->eof_spritelist_mem) { truelight@0: DEBUG(misc, 0) ("Out of sprite mem\n"); truelight@0: return NULL; truelight@0: } 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@835: ss->params[2] = params_3; 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: truelight@0: /* Debugging code */ truelight@0: truelight@0: #ifdef DEBUG_TILE_PUSH truelight@0: static int _num_push; truelight@0: static uint _pushed_tile[200]; truelight@0: static int _pushed_track[200]; truelight@0: truelight@0: static uint _stored_tile[200]; truelight@0: static int _stored_track[200]; truelight@0: static int _num_stored; truelight@0: tron@1093: void dbg_store_path(void) truelight@0: { truelight@0: memcpy(_stored_tile, _pushed_tile, sizeof(_stored_tile)); truelight@0: memcpy(_stored_track, _pushed_track, sizeof(_stored_tile)); truelight@0: _num_stored = _num_push; truelight@0: MarkWholeScreenDirty(); truelight@0: } truelight@0: truelight@0: void dbg_push_tile(uint tile, int track) truelight@0: { truelight@0: _pushed_tile[_num_push] = tile; truelight@0: _pushed_track[_num_push++] = track; truelight@0: dbg_store_path(); truelight@0: } truelight@0: tron@1093: void dbg_pop_tile(void) truelight@0: { truelight@0: _num_push--; truelight@0: } truelight@0: truelight@0: static const uint16 _dbg_track_sprite[] = { truelight@0: 0x3F4, truelight@0: 0x3F3, truelight@0: 0x3F5, truelight@0: 0x3F6, truelight@0: 0x3F8, truelight@0: 0x3F7, truelight@0: }; truelight@0: tron@410: static int dbg_draw_pushed(const TileInfo *ti) truelight@0: { truelight@0: int i; truelight@0: if (ti->tile==0) truelight@0: return 0; truelight@0: for(i=0; i!=_num_stored; i++) truelight@0: if (_stored_tile[i] == ti->tile) { truelight@0: DrawGroundSpriteAt(_dbg_track_sprite[_stored_track[i]&7], ti->x, ti->y, ti->z); truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: truelight@0: #endif truelight@0: truelight@0: static void DrawSelectionSprite(uint32 image, const TileInfo *ti) truelight@0: { dominik@1083: if (_added_tile_sprite && !(_thd_ptr->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: { truelight@0: const TileHighlightData *thd = _thd_ptr; truelight@0: truelight@0: px -= thd->selstart.x; truelight@0: py -= thd->selstart.y; truelight@0: truelight@0: 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] dominik@1070: 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: const TileHighlightData *thd = _thd_ptr; truelight@0: truelight@0: #ifdef DEBUG_TILE_PUSH truelight@0: dbg_draw_pushed(ti); truelight@0: #endif truelight@193: truelight@0: // Draw a red error square? truelight@0: if (thd->redsq != 0 && thd->redsq == (TileIndex)ti->tile) { truelight@0: DrawSelectionSprite(0x030382F0 | _tileh_to_sprite[ti->tileh], ti); truelight@0: return; truelight@0: } truelight@0: truelight@0: // no selection active? truelight@0: if (thd->drawstyle == 0) truelight@0: return; truelight@0: truelight@0: // Inside the inner area? truelight@0: if (IS_INSIDE_1D(ti->x, thd->pos.x, thd->size.x) && IS_INSIDE_1D(ti->y, thd->pos.y, thd->size.y)) { truelight@0: if (thd->drawstyle & HT_RECT) { truelight@0: image = 0x2F0 + _tileh_to_sprite[ti->tileh]; truelight@0: if (thd->make_square_red) image |= 0x3048000; truelight@0: DrawSelectionSprite(image, ti); truelight@0: } else if (thd->drawstyle & HT_POINT) { truelight@0: // Figure out the Z coordinate for the single dot. truelight@0: byte z = ti->z; truelight@0: if (ti->tileh & 8) { truelight@193: z += 8; truelight@193: if (!(ti->tileh & 2) && (ti->tileh & 0x10)) { truelight@0: z += 8; truelight@0: } truelight@0: } truelight@0: DrawGroundSpriteAt(_cur_dpi->zoom != 2 ? 0x306 : 0xFEE,ti->x, ti->y, z); dominik@1070: dominik@1070: } else if (thd->drawstyle & HT_RAIL /*&& thd->place_mode == VHM_RAIL*/) { // autorail highlight piece under cursor dominik@1070: int type = thd->drawstyle & 0xF; dominik@1070: assert(type<=5); dominik@1070: image = SPR_AUTORAIL_BASE + AutorailTilehSprite[ ti->tileh ][ AutorailType[type][0] ]; dominik@1070: dominik@1070: if (thd->make_square_red) image |= 0x3048000; dominik@1070: DrawSelectionSprite(image, ti); dominik@1070: dominik@1070: } else if (IsPartOfAutoLine(ti->x, ti->y)) { // autorail highlighting long line dominik@1070: int dir = thd->drawstyle & ~0xF0; dominik@1070: uint start = TILE_FROM_XY(thd->selstart.x, thd->selstart.y); dominik@1070: int diffx, diffy; dominik@1070: int side; dominik@1070: dominik@1070: diffx = myabs(TileX(start)-TileX(ti->tile)); dominik@1070: diffy = myabs(TileY(start)-TileY(ti->tile)); tron@1109: dominik@1070: side = myabs( diffx-diffy ); dominik@1070: if(dir<2) side = 0; dominik@1070: dominik@1070: image = SPR_AUTORAIL_BASE + AutorailTilehSprite[ ti->tileh ][ AutorailType[dir][side] ]; dominik@1070: truelight@0: if (thd->make_square_red) image |= 0x3048000; truelight@0: DrawSelectionSprite(image, ti); tron@1109: } truelight@0: return; truelight@0: } truelight@0: truelight@0: // Check if it's inside the outer area? truelight@0: if (thd->outersize.x && truelight@0: thd->size.x < thd->size.x + thd->outersize.x && truelight@0: IS_INSIDE_1D(ti->x, thd->pos.x + thd->offs.x, thd->size.x + thd->outersize.x) && truelight@0: IS_INSIDE_1D(ti->y, thd->pos.y + thd->offs.y, thd->size.y + thd->outersize.y)) { truelight@0: // Draw a blue rect. truelight@0: DrawSelectionSprite(0x30582F0 + _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 { truelight@0: FindLandscapeHeight(&ti, x_cur, y_cur); 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: truelight@0: DrawTile(&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: truelight@0: if (dpi->zoom < 1) { truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 12 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_1) { truelight@193: truelight@835: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_2001, t->townnametype, t->townnameparts, t->population); truelight@0: } truelight@0: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@0: truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 24 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_1*2) { truelight@193: truelight@835: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_2001, t->townnametype, t->townnameparts, t->population); truelight@193: } truelight@0: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@0: assert(dpi->zoom == 2); truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 24 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_2*4) { truelight@193: truelight@835: AddStringToDraw(t->sign.left + 5, t->sign.top + 1, STR_2002, t->townnametype, t->townnameparts, 0); truelight@835: AddStringToDraw(t->sign.left + 1, t->sign.top - 3, STR_2003, t->townnametype, t->townnameparts, 0); truelight@193: } truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddStationNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: int left, top, right, bottom; truelight@0: Station *st; truelight@0: StringSpriteToDraw *sstd; 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: truelight@0: if (dpi->zoom < 1) { truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 12 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_1) { truelight@193: truelight@835: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_1; truelight@0: } truelight@0: } truelight@0: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 24 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_1*2) { truelight@193: truelight@835: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_1; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: } else { truelight@0: assert(dpi->zoom == 2); truelight@0: truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 24 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_2*4) { truelight@193: truelight@835: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305D_0, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_2 | 0x8000; truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddSigns(DrawPixelInfo *dpi) truelight@0: { truelight@0: SignStruct *ss; truelight@0: int left, top, right, bottom; truelight@0: StringSpriteToDraw *sstd; 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: truelight@0: if (dpi->zoom < 1) { truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 12 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_1) { truelight@193: truelight@835: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; dominik@1165: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 24 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_1*2) { truelight@193: truelight@835: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; dominik@1172: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 24 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_2*4) { truelight@193: dominik@1172: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2002, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_2 | 0x8000; dominik@1172: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddWaypoints(DrawPixelInfo *dpi) truelight@0: { darkvater@395: Waypoint *cp; truelight@0: truelight@0: int left, top, right, bottom; truelight@0: StringSpriteToDraw *sstd; 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: truelight@0: if (dpi->zoom < 1) { darkvater@395: for(cp=_waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: bottom > cp->sign.top && truelight@0: top < cp->sign.top + 12 && truelight@0: right > cp->sign.left && truelight@0: left < cp->sign.left + cp->sign.width_1) { truelight@193: truelight@835: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = cp->sign.width_1; truelight@0: sstd->color = (cp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; darkvater@395: for(cp=_waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: bottom > cp->sign.top && truelight@0: top < cp->sign.top + 24 && truelight@0: right > cp->sign.left && truelight@0: left < cp->sign.left + cp->sign.width_1*2) { truelight@193: truelight@835: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = cp->sign.width_1; truelight@0: sstd->color = (cp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: darkvater@395: for(cp=_waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: bottom > cp->sign.top && truelight@0: top < cp->sign.top + 24 && truelight@0: right > cp->sign.left && truelight@0: left < cp->sign.left + cp->sign.width_2*4) { truelight@193: truelight@835: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT_TINY, cp - _waypoints, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = cp->sign.width_2 | 0x8000; truelight@0: sstd->color = (cp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } 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]; truelight@0: int w; truelight@0: truelight@0: sign->top = top; truelight@0: truelight@0: GetString(buffer, str); truelight@0: w = GetStringWidth(buffer) + 3; truelight@0: sign->width_1 = w; truelight@0: sign->left = left - (w >> 1); truelight@0: truelight@0: _stringwidth_base = 0xE0; truelight@0: w = GetStringWidth(buffer); truelight@0: _stringwidth_base = 0; truelight@0: sign->width_2 = w + 1; 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); truelight@0: } while ( (ts = ts->next) != NULL); truelight@0: } truelight@0: tron@410: static void ViewportSortParentSprites(ParentSpriteToDraw **psd) truelight@0: { truelight@0: ParentSpriteToDraw *ps, *ps2,*ps3, **psd2, **psd3; truelight@0: truelight@0: while((ps=*psd) != NULL) { truelight@0: if (!(ps->unk16 & 1)) { truelight@0: ps->unk16 |= 1; truelight@0: psd2 = psd; truelight@193: truelight@0: while ( (ps2=*++psd2) != NULL) { truelight@0: if (ps2->unk16 & 1) truelight@0: continue; truelight@0: truelight@0: if (ps->tile_right >= ps2->tile_x && truelight@0: ps->tile_bottom >= ps2->tile_y && truelight@0: ps->tile_z_bottom >= ps2->tile_z && ( truelight@0: ps->tile_x >= ps2->tile_right || truelight@0: ps->tile_y >= ps2->tile_bottom || truelight@0: ps->tile_z >= ps2->tile_z_bottom truelight@0: )) { truelight@0: psd3 = psd; truelight@0: do { truelight@0: ps3 = *psd3; truelight@0: *psd3 = ps2; truelight@0: ps2 = ps3; truelight@0: truelight@0: psd3++; truelight@0: } while (psd3 <= psd2); truelight@0: } truelight@0: } truelight@0: } else { truelight@0: psd++; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportDrawParentSprites(ParentSpriteToDraw **psd) truelight@0: { truelight@0: ParentSpriteToDraw *ps; truelight@0: ChildScreenSpriteToDraw *cs; truelight@0: truelight@0: for (;(ps=*psd) != NULL;psd++) { truelight@0: Point pt = RemapCoords(ps->tile_x, ps->tile_y, ps->tile_z); truelight@0: DrawSprite(ps->image, pt.x, pt.y); truelight@0: truelight@0: cs = ps->child; truelight@0: while (cs) { truelight@0: DrawSprite(cs->image, ps->left + cs->x, ps->top + cs->y); truelight@0: cs = cs->next; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportDrawStrings(DrawPixelInfo *dpi, 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: truelight@0: zoom = (byte)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 { truelight@0: if (ss->width != 0) { truelight@0: int x, y, w, bottom; truelight@193: truelight@0: x = (ss->x >> zoom) - 1; truelight@0: y = (ss->y >> zoom) - 1; truelight@0: truelight@0: bottom = y + 11; truelight@0: truelight@0: w = ss->width; 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@497: DrawFrameRect(x,y, x+w, bottom, ss->color, (_display_opt & DO_TRANS_BUILDINGS) ? 0x9 : 0); truelight@0: } truelight@0: tron@534: SetDParam(0, ss->params[0]); tron@534: SetDParam(1, ss->params[1]); truelight@835: SetDParam(2, ss->params[2]); tron@497: if (_display_opt & DO_TRANS_BUILDINGS && ss->width != 0) { dominik@657: /* Real colors need the IS_PALETTE_COLOR flag, otherwise colors from _string_colormap are assumed. */ truelight@835: DrawString(ss->x >> zoom, (ss->y >> zoom) - (ss->width&0x8000?2:0), ss->string, dominik@657: (_color_list[ss->color].window_color_bgb | IS_PALETTE_COLOR)); truelight@0: } else { truelight@0: DrawString(ss->x >> zoom, (ss->y >> zoom) - (ss->width&0x8000?2:0), ss->string, 16); truelight@0: } truelight@0: } while ( (ss = ss->next) != NULL); truelight@0: truelight@0: _cur_dpi = dpi; 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; truelight@0: int x,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: vd.ground_child = 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; truelight@133: vd.eof_parent_list = &parent_list[lengthof(parent_list)]; truelight@0: vd.spritelist_mem = mem; truelight@0: vd.eof_spritelist_mem = &mem[sizeof(mem) - 0x40]; 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: truelight@0: if (vd.first_tile != NULL) truelight@0: 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: truelight@0: if (vd.first_string != NULL) truelight@0: 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. truelight@0: static void ViewportDrawChk(ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: if (((bottom - top) * (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@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: tron@500: static inline void ViewportDraw(ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: int t; truelight@0: truelight@0: if (right <= vp->left || truelight@0: bottom <= vp->top) truelight@0: return; truelight@0: truelight@0: if (left >= (t=vp->left + vp->width)) truelight@0: return; truelight@0: truelight@0: if (left < vp->left) left = vp->left; truelight@0: if (right > t) right = t; truelight@0: truelight@0: if (top >= (t = vp->top + vp->height)) truelight@0: return; truelight@0: truelight@0: if (top < vp->top) top = vp->top; truelight@0: if (bottom > t) bottom = t; truelight@0: truelight@0: ViewportDrawChk(vp, left, top, right, bottom); truelight@0: } truelight@0: truelight@0: void DrawWindowViewport(Window *w) { 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: truelight@0: void UpdateViewportPosition(Window *w) truelight@0: { truelight@0: ViewPort *vp = w->viewport; truelight@0: truelight@0: if (WP(w,vp_d).follow_vehicle != 0xFFFF) { truelight@0: Vehicle *veh; truelight@0: Point pt; truelight@0: truelight@919: veh = GetVehicle(WP(w,vp_d).follow_vehicle); truelight@0: pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); 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 tron@856: vx = clamp(vx, 0 * 4, MapMaxX() * 16 * 4); tron@856: vy = clamp(vy, 0 * 4, MapMaxY() * 16 * 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@632: WP(w,vp_d).scrollpos_x = x - vp->virtual_width / 2; tron@632: 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: truelight@0: SetViewportPosition(w, WP(w,vp_d).scrollpos_x, WP(w,vp_d).scrollpos_y); truelight@0: } truelight@0: } truelight@0: tron@410: static void MarkViewportDirty(ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: if ( (right -= vp->virtual_left) <= 0) truelight@0: return; truelight@0: truelight@0: if ( (bottom -= vp->virtual_top) <= 0) truelight@0: return; truelight@0: truelight@0: if ( (left -= vp->virtual_left) < 0) truelight@0: left = 0; truelight@0: truelight@0: if ((uint)left >= (uint)vp->virtual_width) truelight@0: return; truelight@0: truelight@0: if ( (top -= vp->virtual_top) < 0) truelight@0: top = 0; truelight@0: truelight@0: if ((uint)top >= (uint)vp->virtual_height) truelight@0: 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: { truelight@0: 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: truelight@0: void MarkTileDirtyByTile(TileIndex tile) { tron@926: Point pt = RemapCoords(TileX(tile) * 16, TileY(tile) * 16, 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: { truelight@0: int z = 0; truelight@0: Point pt; tron@863: if (IS_INT_INSIDE(x, 0, MapSizeX() * 16) && tron@863: IS_INT_INSIDE(y, 0, MapSizeY() * 16)) truelight@0: z = GetTileZ(TILE_FROM_XY(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; truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: int x = thd->pos.x; truelight@0: int y = thd->pos.y; truelight@0: truelight@0: x_size=thd->size.x; truelight@0: y_size=thd->size.y; truelight@0: truelight@0: if (thd->outersize.x) { truelight@0: x_size += thd->outersize.x; truelight@0: x += thd->offs.x; truelight@0: y_size += thd->outersize.y; truelight@0: 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); truelight@0: } while ( (y+=16) != y_size); truelight@0: y = y_bk; truelight@0: } while ( (x+=16) != x_size); truelight@0: } truelight@0: truelight@0: truelight@0: static bool CheckClickOnTown(ViewPort *vp, int x, int y) truelight@0: { truelight@0: Town *t; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_TOWN_NAMES)) truelight@0: return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 12 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_1) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 24 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_1 * 2) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 24 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_2 * 4) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: static bool CheckClickOnStation(ViewPort *vp, int x, int y) truelight@0: { truelight@0: Station *st; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_STATION_NAMES)) truelight@0: return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 12 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_1) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 24 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_1 * 2) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 24 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_2 * 4) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: static bool CheckClickOnSign(ViewPort *vp, int x, int y) truelight@0: { truelight@0: SignStruct *ss; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_SIGNS)) truelight@0: return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 12 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_1) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 24 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_1 * 2) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 24 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_2 * 4) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: darkvater@395: static bool CheckClickOnWaypoint(ViewPort *vp, int x, int y) truelight@0: { darkvater@395: Waypoint *cp; truelight@0: darkvater@395: if (!(_display_opt & DO_WAYPOINTS)) truelight@0: return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: darkvater@395: for(cp = _waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: y >= cp->sign.top && truelight@0: y < cp->sign.top + 12 && truelight@0: x >= cp->sign.left && truelight@0: x < cp->sign.left + cp->sign.width_1) { darkvater@395: ShowRenameWaypointWindow(cp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; darkvater@395: for(cp = _waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: y >= cp->sign.top && truelight@0: y < cp->sign.top + 24 && truelight@0: x >= cp->sign.left && truelight@0: x < cp->sign.left + cp->sign.width_1 * 2) { darkvater@395: ShowRenameWaypointWindow(cp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; darkvater@395: for(cp = _waypoints; cp != endof(_waypoints); cp++) { truelight@0: if (cp->xy && truelight@0: y >= cp->sign.top && truelight@0: y < cp->sign.top + 24 && truelight@0: x >= cp->sign.left && truelight@0: x < cp->sign.left + cp->sign.width_2 * 4) { darkvater@395: ShowRenameWaypointWindow(cp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: truelight@0: static void CheckClickOnLandscape(ViewPort *vp, int x, int y) truelight@0: { truelight@0: Point pt = TranslateXYToTileCoord(vp,x,y); truelight@0: if (pt.x != -1) { truelight@0: uint tile = TILE_FROM_XY(pt.x, pt.y); truelight@0: ClickTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: void HandleClickOnTrain(Vehicle *v); truelight@0: void HandleClickOnRoadVeh(Vehicle *v); truelight@0: void HandleClickOnAircraft(Vehicle *v); truelight@0: void HandleClickOnShip(Vehicle *v); tron@1095: static void HandleClickOnSpecialVeh(Vehicle *v) {} truelight@0: void HandleClickOnDisasterVeh(Vehicle *v); truelight@0: typedef void OnVehicleClickProc(Vehicle *v); truelight@0: static OnVehicleClickProc * const _on_vehicle_click_proc[6] = { truelight@0: HandleClickOnTrain, truelight@0: HandleClickOnRoadVeh, truelight@0: HandleClickOnShip, truelight@0: HandleClickOnAircraft, truelight@0: HandleClickOnSpecialVeh, truelight@0: HandleClickOnDisasterVeh, truelight@0: }; truelight@0: truelight@0: void HandleViewportClicked(ViewPort *vp, int x, int y) truelight@0: { truelight@0: if (CheckClickOnTown(vp, x, y)) truelight@0: return; truelight@0: truelight@0: if (CheckClickOnStation(vp, x, y)) truelight@0: return; truelight@0: truelight@0: if (CheckClickOnSign(vp, x, y)) truelight@0: return; truelight@0: darkvater@395: if (CheckClickOnWaypoint(vp, x, y)) truelight@0: return; truelight@0: truelight@0: CheckClickOnLandscape(vp, x, y); truelight@0: truelight@0: { truelight@0: Vehicle *v = CheckClickOnVehicle(vp, x, y); truelight@0: if (v) _on_vehicle_click_proc[v->type - 0x10](v); truelight@0: } truelight@0: } truelight@0: tron@1093: Vehicle *CheckMouseOverVehicle(void) truelight@0: { truelight@0: Window *w; truelight@0: 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); truelight@0: if (w == NULL) truelight@0: return NULL; truelight@0: truelight@0: vp = IsPtInWindowViewport(w, x, y); truelight@0: if (vp) { truelight@0: return CheckClickOnVehicle(vp, x, y); truelight@0: } else { truelight@0: return NULL; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: tron@1093: void PlaceObject(void) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@0: WindowEvent e; truelight@193: truelight@0: pt = GetTileBelowCursor(); truelight@0: if (pt.x == -1) truelight@0: 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: truelight@0: if ((w = GetCallbackWnd()) != NULL) { truelight@0: e.event = WE_PLACE_OBJ; truelight@0: e.place.pt = pt; truelight@0: e.place.tile = TILE_FROM_XY(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 */ darkvater@152: 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)); darkvater@152: WP(w,vp_d).follow_vehicle = -1; darkvater@152: darkvater@152: if (WP(w,vp_d).scrollpos_x == pt.x && darkvater@152: WP(w,vp_d).scrollpos_y == pt.y) darkvater@152: return false; darkvater@152: darkvater@152: WP(w,vp_d).scrollpos_x = pt.x; darkvater@152: WP(w,vp_d).scrollpos_y = pt.y; darkvater@152: return true; darkvater@152: } darkvater@152: darkvater@152: /* scrolls the viewport in a window to a given tile */ darkvater@152: bool ScrollWindowToTile(TileIndex tile, Window * w) darkvater@152: { tron@926: return ScrollWindowTo(TileX(tile) * 16 + 8, TileY(tile) * 16 + 8, w); darkvater@152: } darkvater@152: darkvater@152: darkvater@152: truelight@0: bool ScrollMainWindowTo(int x, int y) truelight@0: { truelight@0: Window *w = FindWindowById(WC_MAIN_WINDOW, 0); truelight@0: Point pt; truelight@0: truelight@0: pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(x, y)); truelight@0: WP(w,vp_d).follow_vehicle = -1; truelight@0: truelight@0: if (WP(w,vp_d).scrollpos_x == pt.x && truelight@0: WP(w,vp_d).scrollpos_y == pt.y) truelight@0: return false; truelight@0: truelight@0: WP(w,vp_d).scrollpos_x = pt.x; truelight@0: WP(w,vp_d).scrollpos_y = pt.y; truelight@0: return true; truelight@0: } truelight@0: truelight@0: truelight@0: bool ScrollMainWindowToTile(TileIndex tile) truelight@0: { tron@926: return ScrollMainWindowTo(TileX(tile) * 16 + 8, TileY(tile) * 16 + 8); 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) { truelight@0: if (tile != 0) MarkTileDirtyByTile(tile); truelight@0: if (old != 0) MarkTileDirtyByTile(old); truelight@0: } truelight@0: } truelight@0: truelight@0: void SetTileSelectSize(int w, int h) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: thd->new_size.x = w * 16; truelight@0: thd->new_size.y = h * 16; truelight@0: thd->new_outersize.x = 0; truelight@0: thd->new_outersize.y = 0; truelight@0: } truelight@0: truelight@0: void SetTileSelectBigSize(int ox, int oy, int sx, int sy) { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: thd->offs.x = ox * 16; truelight@0: thd->offs.y = oy * 16; truelight@0: thd->new_outersize.x = sx * 16; truelight@0: thd->new_outersize.y = sy * 16; 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: { dominik@1070: int i; dominik@1070: i = AutorailPiece[x&0xF][y&0xF]; dominik@1070: return HT_RAIL | i; dominik@1070: } truelight@0: dominik@1070: // called regular to update tile highlighting in all cases tron@1093: void UpdateTileSelection(void) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: Point pt; truelight@0: int x1,y1; truelight@0: truelight@0: thd->new_drawstyle = 0; truelight@0: dominik@1070: if (thd->place_mode == VHM_SPECIAL) { truelight@0: x1 = thd->selend.x; truelight@0: y1 = thd->selend.y; truelight@0: if (x1 != -1) { truelight@0: int x2 = thd->selstart.x; truelight@0: 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); truelight@0: thd->new_pos.x = x1; truelight@0: thd->new_pos.y = y1; truelight@0: thd->new_size.x = x2 - x1 + 16; truelight@0: thd->new_size.y = y2 - y1 + 16; truelight@0: thd->new_drawstyle = thd->next_drawstyle; truelight@0: } dominik@1070: } else if (thd->place_mode != VHM_NONE) { truelight@0: pt = GetTileBelowCursor(); truelight@0: x1 = pt.x; truelight@0: y1 = pt.y; truelight@0: if (x1 != -1) { dominik@1070: switch (thd->place_mode) { dominik@1070: case VHM_RECT: dominik@1070: thd->new_drawstyle = HT_RECT; dominik@1070: break; dominik@1070: case VHM_POINT: dominik@1070: thd->new_drawstyle = HT_POINT; dominik@1070: x1 += 8; dominik@1070: y1 += 8; dominik@1070: break; dominik@1070: case VHM_RAIL: dominik@1070: thd->new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile truelight@0: } truelight@0: thd->new_pos.x = x1 & ~0xF; truelight@0: thd->new_pos.y = y1 & ~0xF; truelight@0: } truelight@0: } truelight@0: dominik@1070: // redraw selection truelight@193: if (thd->drawstyle != thd->new_drawstyle || truelight@193: thd->pos.x != thd->new_pos.x || thd->pos.y != thd->new_pos.y || truelight@0: thd->size.x != thd->new_size.x || thd->size.y != thd->new_size.y) { truelight@193: truelight@0: // clear the old selection? truelight@0: if (thd->drawstyle) SetSelectionTilesDirty(); truelight@0: truelight@0: thd->drawstyle = thd->new_drawstyle; truelight@0: thd->pos = thd->new_pos; truelight@0: thd->size = thd->new_size; truelight@0: thd->outersize = thd->new_outersize; truelight@0: thd->dirty = 0xff; truelight@0: truelight@0: // draw the new selection? truelight@0: if (thd->new_drawstyle) SetSelectionTilesDirty(); truelight@0: } truelight@0: } truelight@0: dominik@1070: // highlighting tiles while only going over them with the mouse truelight@0: void VpStartPlaceSizing(uint tile, int user) truelight@0: { truelight@0: TileHighlightData *thd; truelight@0: thd = _thd_ptr; truelight@0: thd->userdata = user; tron@926: thd->selend.x = TileX(tile) * 16; tron@926: thd->selstart.x = TileX(tile) * 16; tron@926: thd->selend.y = TileY(tile) * 16; tron@926: thd->selstart.y = TileY(tile) * 16; dominik@1070: if (thd->place_mode == VHM_RECT) { dominik@1070: thd->place_mode = VHM_SPECIAL; truelight@0: thd->next_drawstyle = HT_RECT; dominik@1070: } else if (thd->place_mode == VHM_RAIL) { // autorail one piece dominik@1070: thd->place_mode = VHM_SPECIAL; dominik@1070: thd->next_drawstyle = thd->drawstyle; truelight@0: } else { dominik@1070: thd->place_mode = VHM_SPECIAL; truelight@0: 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: truelight@0: void VpSetPresizeRange(uint from, uint to) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; tron@926: thd->selend.x = TileX(to) * 16; tron@926: thd->selend.y = TileY(to) * 16; tron@926: thd->selstart.x = TileX(from) * 16; tron@926: thd->selstart.y = TileY(from) * 16; truelight@0: thd->next_drawstyle = HT_RECT; truelight@0: } truelight@0: tron@1093: 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: TileHighlightData *thd = &_thd; dominik@1070: int fxpy = _tile_fract_coords.x + _tile_fract_coords.y; dominik@1070: int sxpy = (thd->selend.x & 0xF) + (thd->selend.y & 0xF); dominik@1070: int fxmy = _tile_fract_coords.x - _tile_fract_coords.y; dominik@1070: int sxmy = (thd->selend.x & 0xF) - (thd->selend.y & 0xF); dominik@1070: dominik@1070: 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: break; 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: break; 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: break; 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: break; dominik@1070: } dominik@1070: dominik@1070: return 0; // avoids compiler warnings dominik@1070: } dominik@1070: dominik@1070: dominik@1070: // while dragging dominik@1070: static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method) truelight@0: { truelight@0: int d; dominik@1070: byte b=6; truelight@0: uint w,h; truelight@193: dominik@1070: int dx = thd->selstart.x - (thd->selend.x&~0xF); dominik@1070: int dy = thd->selstart.y - (thd->selend.y&~0xF); dominik@1070: w = myabs(dx) + 16; dominik@1070: h = myabs(dy) + 16; truelight@0: dominik@1070: if (TILE_FROM_XY(thd->selstart.x, thd->selstart.y) == TILE_FROM_XY(x,y)) { // check if we're only within one tile dominik@1070: if(method == VPM_RAILDIRS) dominik@1070: b = GetAutorailHT(x, y); dominik@1070: else // rect for autosignals on one tile dominik@1070: b = HT_RECT; dominik@1070: } else if (h == 16) { // Is this in X direction? dominik@1070: if (dx==16) // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(3)) | HT_LINE; dominik@1070: else if (dx==-16) dominik@1070: b = (Check2x1AutoRail(2)) | HT_LINE; tron@1109: else dominik@1070: b = HT_LINE | HT_DIR_X; dominik@1070: y = thd->selstart.y; dominik@1070: } else if (w == 16) { // Or Y direction? dominik@1070: if (dy==16) // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(1)) | HT_LINE; dominik@1070: else if (dy==-16) // 2x1 other direction dominik@1070: b = (Check2x1AutoRail(0)) | HT_LINE; tron@1109: else dominik@1070: b = HT_LINE | HT_DIR_Y; truelight@0: x = thd->selstart.x; dominik@1070: } else if (w > h * 2) { // still count as x dir? dominik@1070: b = HT_LINE | HT_DIR_X; truelight@0: y = thd->selstart.y; dominik@1070: } else if (h > w * 2) { // still count as y dir? dominik@1070: b = HT_LINE | HT_DIR_Y; dominik@1070: x = thd->selstart.x; dominik@1070: } else { // complicated direction truelight@0: d = w - h; dominik@1070: thd->selend.x = thd->selend.x&~0xF; dominik@1070: 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 dominik@1070: if (d ==0) b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; dominik@1070: else if (d >= 0) { x = thd->selstart.x + h; b = HT_LINE | HT_DIR_VL; } // return px == py || px == py + 16; dominik@1070: else { y = thd->selstart.y + w; b = HT_LINE | HT_DIR_VR; } // return px == py || px == py - 16; truelight@0: } else { truelight@0: // west dominik@1070: if (d ==0) b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; dominik@1070: else if (d >= 0) { x = thd->selstart.x + h; b = HT_LINE | HT_DIR_HL; } dominik@1070: else { y = thd->selstart.y - w; b = HT_LINE | HT_DIR_HU; } truelight@0: } truelight@0: } else { truelight@0: if (y > thd->selstart.y) { truelight@0: // east dominik@1070: if (d ==0) b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; dominik@1070: else if (d >= 0) { x = thd->selstart.x - h; b = HT_LINE | HT_DIR_HU; } // return px == -py || px == -py - 16; dominik@1070: else { y = thd->selstart.y + w; b = HT_LINE | HT_DIR_HL; } // return px == -py || px == -py + 16; truelight@0: } else { truelight@0: // north dominik@1070: if (d ==0) b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; dominik@1070: else if (d >= 0) { x = thd->selstart.x - h; b = HT_LINE | HT_DIR_VR; } // return px == py || px == py - 16; dominik@1070: else { y = thd->selstart.y - w; b = HT_LINE | HT_DIR_VL; } //return px == py || px == py + 16; truelight@0: } truelight@0: } truelight@0: } truelight@0: thd->selend.x = x; truelight@0: thd->selend.y = y; truelight@0: thd->next_drawstyle = b; truelight@0: } truelight@0: dominik@1070: // while dragging truelight@0: void VpSelectTilesWithMethod(int x, int y, int method) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: int sx,sy; truelight@0: if (x == -1) { truelight@0: thd->selend.x = -1; truelight@0: return; truelight@0: } truelight@0: truelight@0: // allow drag in any rail direction darkvater@58: if (method == VPM_RAILDIRS || method == VPM_SIGNALDIRS) { dominik@1070: thd->selend.x = x; dominik@1070: thd->selend.y = y; dominik@1070: CalcRaildirsDrawstyle(thd, x, y, method); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (_thd.next_drawstyle == HT_POINT) { x += 8; y += 8; } truelight@0: truelight@0: sx = thd->selstart.x; truelight@0: sy = thd->selstart.y; truelight@0: truelight@0: switch(method) { truelight@0: case VPM_FIX_X: truelight@0: x = sx; truelight@0: break; truelight@0: truelight@0: case VPM_FIX_Y: truelight@0: y = sy; truelight@0: break; truelight@0: truelight@0: case VPM_X_OR_Y: truelight@0: if (myabs(sy - y) < myabs(sx - x)) y = sy; else x = sx; truelight@0: break; truelight@0: truelight@0: case VPM_X_AND_Y: truelight@0: break; truelight@0: truelight@0: // limit the selected area to a 10x10 rect. truelight@0: case VPM_X_AND_Y_LIMITED: { truelight@0: int limit = (thd->sizelimit-1) * 16; truelight@0: x = sx + clamp(x - sx, -limit, limit); truelight@0: y = sy + clamp(y - sy, -limit, limit); truelight@0: break; truelight@0: } truelight@0: } truelight@0: truelight@0: thd->selend.x = x; truelight@0: 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: truelight@0: if (_special_mouse_mode != WSM_SIZING) truelight@0: return true; truelight@0: truelight@0: e.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; truelight@0: e.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@1109: if (_thd.next_drawstyle == HT_RECT) dominik@1070: _thd.place_mode = VHM_RECT; dominik@1070: else if ((e.place.userdata & 0xF) == VPM_SIGNALDIRS) // some might call this a hack... -- Dominik dominik@1070: _thd.place_mode = VHM_RECT; dominik@1070: else if (_thd.next_drawstyle & HT_LINE) dominik@1070: _thd.place_mode = VHM_RAIL; dominik@1070: else if (_thd.next_drawstyle & HT_RAIL) dominik@1070: _thd.place_mode = VHM_RAIL; dominik@1070: else dominik@1070: _thd.place_mode = VHM_POINT; truelight@0: SetTileSelectSize(1, 1); truelight@0: truelight@0: // and call the mouseup event. truelight@0: e.event = WE_PLACE_MOUSEUP; truelight@0: e.place.pt = _thd.selend; truelight@0: e.place.tile = TILE_FROM_XY(e.place.pt.x, e.place.pt.y); truelight@0: e.place.starttile = TILE_FROM_XY(_thd.selstart.x, _thd.selstart.y); truelight@0: w->wndproc(w, &e); truelight@193: truelight@0: return false; truelight@0: } truelight@0: truelight@0: void SetObjectToPlaceWnd(int icon, byte mode, Window *w) truelight@0: { truelight@0: SetObjectToPlace(icon,mode,w->window_class, w->window_number); truelight@0: } truelight@0: truelight@0: #include "table/animcursors.h" truelight@0: tron@873: void SetObjectToPlace(int icon, byte mode, WindowClass window_class, WindowNumber window_num) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: Window *w; truelight@0: dominik@1070: // undo clicking on button truelight@0: if (thd->place_mode != 0) { truelight@0: thd->place_mode = 0; truelight@0: w = FindWindowById(thd->window_class, thd->window_number); truelight@0: if (w != NULL) truelight@0: CallWindowEventNP(w, WE_ABORT_PLACE_OBJ); truelight@0: } truelight@193: truelight@0: SetTileSelectSize(1, 1); truelight@193: truelight@0: 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: truelight@0: thd->place_mode = mode; truelight@0: thd->window_class = window_class; truelight@0: 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) truelight@0: SetAnimatedMouseCursor(_animcursors[~icon]); truelight@0: else truelight@0: SetMouseCursor(icon); truelight@0: } truelight@0: tron@1093: void ResetObjectToPlace(void) tron@1093: { truelight@0: SetObjectToPlace(0,0,0,0); truelight@0: }