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@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; truelight@0: int16 x; truelight@0: int16 y; truelight@0: 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; truelight@0: int16 x, y; truelight@0: byte z; truelight@0: } TileSpriteToDraw; truelight@0: truelight@0: typedef struct ChildScreenSpriteToDraw { truelight@0: uint32 image; truelight@0: int16 x,y; truelight@0: struct ChildScreenSpriteToDraw *next; truelight@0: } ChildScreenSpriteToDraw; truelight@0: truelight@0: typedef struct ParentSpriteToDraw { truelight@0: uint32 image; truelight@0: int16 left, top, right, bottom; truelight@0: int16 tile_x, tile_y; truelight@0: int16 tile_right, 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@0: veh = &_vehicles[WP(w,vp_d).follow_vehicle]; truelight@0: pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: } else { truelight@0: int x = GET_TILE_X(follow_flags) * 16; truelight@0: int y = GET_TILE_Y(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: truelight@0: Point TranslateXYToTileCoord(ViewPort *vp, int x, int y) { 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: truelight@0: if ((uint)pt.x >= TILE_X_MAX*16 || (uint)pt.y >= TILE_Y_MAX*16) { truelight@0: pt.x = pt.y = -1; truelight@0: } truelight@0: truelight@0: return pt; truelight@0: } truelight@0: truelight@0: static Point GetTileFromScreenXY(int x, int 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) truelight@0: return TranslateXYToTileCoord(vp, x, y); truelight@0: truelight@0: pt.y = pt.x = -1; truelight@0: return pt; truelight@0: } truelight@0: truelight@0: Point GetTileBelowCursor() truelight@0: { truelight@0: return GetTileFromScreenXY(_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@152: return GetTileFromScreenXY(x+vp->left, y+vp->top); truelight@0: } truelight@0: truelight@133: void DrawGroundSpriteAt(uint32 image, int16 x, int16 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 { truelight@0: _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: truelight@0: void StartSpriteCombine() truelight@0: { truelight@0: _cur_vd->combine_sprites = 1; truelight@0: } truelight@0: truelight@0: void EndSpriteCombine() 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@0: 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: 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@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: truelight@0: void dbg_store_path() 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: truelight@0: void dbg_pop_tile() 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: { truelight@0: if (_added_tile_sprite) { truelight@0: DrawGroundSpriteAt(image, ti->x, ti->y, ti->z + 7); truelight@0: } else { truelight@0: AddSortableSpriteToDraw(image, ti->x, ti->y, 0x10, 0x10, 1, ti->z + 7); truelight@0: } 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) { truelight@0: case HT_LINE | 0: return px == py || px == py + 16; truelight@0: case HT_LINE | 1: return px == py || px == py - 16; truelight@0: case HT_LINE | 2: return px == -py || px == -py + 16; truelight@0: case HT_LINE | 3: return px == -py || px == -py - 16; 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: 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); truelight@0: } else { truelight@0: if (IsPartOfAutoLine(ti->x, ti->y)) { 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: } truelight@0: } 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@410: static void ViewportAddLandscape() truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: int x, y, width, height; truelight@0: TileInfo ti; truelight@0: bool direction; truelight@0: truelight@0: _cur_ti = &ti; truelight@0: 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@0: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, STR_2001, t->townnametype, t->townnameparts); 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@0: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, STR_2001, t->townnametype, t->townnameparts); 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@0: AddStringToDraw(t->sign.left + 5, t->sign.top + 1, STR_2002, t->townnametype, t->townnameparts); truelight@0: AddStringToDraw(t->sign.left + 1, t->sign.top - 3, STR_2003, t->townnametype, t->townnameparts); 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@0: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities); 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@0: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities); 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@0: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305D_0, st->index, st->facilities); 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@0: for(ss=_sign_list; ss != endof(_sign_list); 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@0: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; truelight@0: sstd->color = 14; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@0: for(ss=_sign_list; ss != endof(_sign_list); 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@0: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; truelight@0: sstd->color = 14; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@0: for(ss=_sign_list; ss != endof(_sign_list); 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: truelight@0: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2807, ss->str, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_2 | 0x8000; truelight@0: sstd->color = 14; 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: darkvater@395: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 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: darkvater@395: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 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: darkvater@395: sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT_TINY, cp - _waypoints, 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]); 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. */ dominik@657: 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) truelight@133: assert(vd.parent_list - parent_list <= lengthof(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@0: veh = &_vehicles[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@632: vx = clamp(vx, 0 * 4, TILE_X_MAX * 16 * 4); tron@632: vy = clamp(vy, 0 * 4, TILE_Y_MAX * 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) { truelight@0: Point pt = RemapCoords(GET_TILE_X(tile) * 16, GET_TILE_Y(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; truelight@0: if (IS_INT_INSIDE(x, 0, TILES_X*16) && IS_INT_INSIDE(y, 0, TILES_Y*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@410: static void SetSelectionTilesDirty() 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@0: for(ss = _sign_list; ss != endof(_sign_list); 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@0: for(ss = _sign_list; ss != endof(_sign_list); 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@0: for(ss = _sign_list; ss != endof(_sign_list); 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); truelight@0: 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: truelight@0: Vehicle *CheckMouseOverVehicle() 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: truelight@0: void PlaceObject() 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: truelight@0: if (_thd.place_mode == 2) { 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: { darkvater@152: return ScrollWindowTo(GET_TILE_X(tile)*16+8, GET_TILE_Y(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: { truelight@0: return ScrollMainWindowTo(GET_TILE_X(tile)*16+8, GET_TILE_Y(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: truelight@0: truelight@0: void UpdateTileSelection() 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: truelight@0: if (thd->place_mode == 3) { 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: } truelight@0: } else if (thd->place_mode != 0) { truelight@0: pt = GetTileBelowCursor(); truelight@0: x1 = pt.x; truelight@0: y1 = pt.y; truelight@0: if (x1 != -1) { truelight@0: if (thd->place_mode == 1) { truelight@0: thd->new_drawstyle = HT_RECT; truelight@0: } else { truelight@0: thd->new_drawstyle = HT_POINT; truelight@0: x1 += 8; truelight@0: y1 += 8; truelight@0: } truelight@0: thd->new_pos.x = x1 & ~0xF; truelight@0: thd->new_pos.y = y1 & ~0xF; truelight@0: } truelight@0: } truelight@0: 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: truelight@0: void VpStartPlaceSizing(uint tile, int user) truelight@0: { truelight@0: TileHighlightData *thd; truelight@0: truelight@0: thd = _thd_ptr; truelight@0: thd->userdata = user; truelight@0: thd->selend.x = GET_TILE_X(tile)*16; truelight@0: thd->selstart.x = GET_TILE_X(tile)*16; truelight@0: thd->selend.y = GET_TILE_Y(tile)*16; truelight@0: thd->selstart.y = GET_TILE_Y(tile)*16; truelight@0: if (thd->place_mode == 1) { truelight@0: thd->place_mode = 3; truelight@0: thd->next_drawstyle = HT_RECT; truelight@0: } else { truelight@0: thd->place_mode = 3; 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; truelight@0: thd->selend.x = GET_TILE_X(to)*16; truelight@0: thd->selend.y = GET_TILE_Y(to)*16; truelight@0: thd->selstart.x = GET_TILE_X(from)*16; truelight@0: thd->selstart.y = GET_TILE_Y(from)*16; truelight@0: thd->next_drawstyle = HT_RECT; truelight@0: } truelight@0: truelight@0: void VpStartPreSizing() truelight@0: { truelight@0: _thd.selend.x = -1; truelight@0: _special_mouse_mode = WSM_PRESIZE; truelight@0: } truelight@0: truelight@0: static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y) truelight@0: { truelight@0: int d; truelight@0: bool b; truelight@0: uint w,h; truelight@193: truelight@0: w = myabs((x & ~0xF) - thd->selstart.x) + 16; truelight@0: h = myabs((y & ~0xF) - thd->selstart.y) + 16; truelight@0: truelight@0: // vertical and horizontal lines are really simple truelight@0: if (w == 16 || h == 16) { truelight@0: b = HT_RECT; truelight@0: } else if (w * 2 < h) { // see if we're closer to rect? truelight@0: x = thd->selstart.x; truelight@0: b = HT_RECT; truelight@0: } else if (w > h * 2) { truelight@0: y = thd->selstart.y; truelight@0: b = HT_RECT; truelight@0: } else { truelight@0: d = w - h; truelight@0: truelight@0: // four cases. truelight@0: if (x > thd->selstart.x) { truelight@0: if (y > thd->selstart.y) { truelight@0: // south truelight@0: if (d ==0) b = (x & 0xF) > (y & 0xF) ? HT_LINE | 0 : HT_LINE | 1; truelight@0: else if (d >= 0) { x = thd->selstart.x + h; b = HT_LINE | 0; } // return px == py || px == py + 16; truelight@0: else { y = thd->selstart.y + w; b = HT_LINE | 1; } // return px == py || px == py - 16; truelight@0: } else { truelight@0: // west truelight@0: if (d ==0) b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | 2 : HT_LINE | 3; truelight@0: else if (d >= 0) { x = thd->selstart.x + h; b = HT_LINE | 2; } truelight@0: else { y = thd->selstart.y - w; b = HT_LINE | 3; } truelight@0: } truelight@0: } else { truelight@0: if (y > thd->selstart.y) { truelight@0: // east truelight@0: if (d ==0) b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | 2 : HT_LINE | 3; truelight@0: else if (d >= 0) { x = thd->selstart.x - h; b = HT_LINE | 3; } // return px == -py || px == -py - 16; truelight@0: else { y = thd->selstart.y + w; b = HT_LINE | 2; } // return px == -py || px == -py + 16; truelight@0: } else { truelight@0: // north truelight@0: if (d ==0) b = (x & 0xF) > (y & 0xF) ? HT_LINE | 0 : HT_LINE | 1; truelight@0: else if (d >= 0) { x = thd->selstart.x - h; b = HT_LINE | 1; } // return px == py || px == py - 16; truelight@0: else { y = thd->selstart.y - w; b = HT_LINE | 0; } //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: 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: 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) { truelight@0: CalcRaildirsDrawstyle(thd, x, y); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (_thd.next_drawstyle == HT_POINT) { x += 8; y += 8; } truelight@0: truelight@0: //thd->next_drawstyle = HT_RECT; 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: truelight@0: bool VpHandlePlaceSizingDrag() 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: 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: truelight@0: // while dragging... 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; truelight@0: _thd.place_mode = (_thd.next_drawstyle == HT_RECT || _thd.next_drawstyle & HT_LINE) ? 1 : 2; truelight@193: 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: truelight@0: void SetObjectToPlace(int icon, byte mode, byte window_class, uint16 window_num) truelight@0: { truelight@0: TileHighlightData *thd = _thd_ptr; truelight@0: Window *w; truelight@0: 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: truelight@0: if (mode == 4) { 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: truelight@0: if (mode == 3) 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: truelight@0: void ResetObjectToPlace(){ truelight@0: SetObjectToPlace(0,0,0,0); truelight@0: }