src/viewport.cpp
branchnoai
changeset 9625 3301b1b3889c
parent 9624 b71483f2330f
child 9626 79f2b5a0cdd7
equal deleted inserted replaced
9624:b71483f2330f 9625:3301b1b3889c
   181 		pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y));
   181 		pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y));
   182 	}
   182 	}
   183 
   183 
   184 	WP(w, vp_d).scrollpos_x = pt.x;
   184 	WP(w, vp_d).scrollpos_x = pt.x;
   185 	WP(w, vp_d).scrollpos_y = pt.y;
   185 	WP(w, vp_d).scrollpos_y = pt.y;
       
   186 	WP(w, vp_d).dest_scrollpos_x = pt.x;
       
   187 	WP(w, vp_d).dest_scrollpos_y = pt.y;
       
   188 
   186 	w->viewport = vp;
   189 	w->viewport = vp;
   187 	vp->virtual_left = 0;//pt.x;
   190 	vp->virtual_left = 0;//pt.x;
   188 	vp->virtual_top = 0;//pt.y;
   191 	vp->virtual_top = 0;//pt.y;
   189 }
   192 }
   190 
   193 
  1384 		int vy;
  1387 		int vy;
  1385 
  1388 
  1386 		/* Center of the viewport is hot spot */
  1389 		/* Center of the viewport is hot spot */
  1387 		x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2;
  1390 		x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2;
  1388 		y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2;
  1391 		y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2;
       
  1392 
       
  1393 		int dest_x = WP(w,vp_d).dest_scrollpos_x + vp->virtual_width / 2;
       
  1394 		int dest_y = WP(w,vp_d).dest_scrollpos_y + vp->virtual_height / 2;
       
  1395 
       
  1396 		int delta_x = dest_x - x;
       
  1397 		int delta_y = dest_y - y;
       
  1398 
       
  1399 		if (delta_x != 0 || delta_y != 0) {
       
  1400 			if (_patches.smooth_scroll) {
       
  1401 				int max_scroll = ScaleByMapSize1D(512);
       
  1402 				/* Not at our desired positon yet... */
       
  1403 				x += clamp(delta_x / 8, -max_scroll, max_scroll);
       
  1404 				y += clamp(delta_y / 8, -max_scroll, max_scroll);
       
  1405 			} else {
       
  1406 				x = dest_x;
       
  1407 				y = dest_y;
       
  1408 			}
       
  1409 		}
       
  1410 
  1389 		/* Convert viewport coordinates to map coordinates
  1411 		/* Convert viewport coordinates to map coordinates
  1390 		 * Calculation is scaled by 4 to avoid rounding errors */
  1412 		 * Calculation is scaled by 4 to avoid rounding errors */
  1391 		vx = -x + y * 2;
  1413 		vx = -x + y * 2;
  1392 		vy =  x + y * 2;
  1414 		vy =  x + y * 2;
  1393 		/* clamp to size of map */
  1415 		/* clamp to size of map */
  1839 	}
  1861 	}
  1840 }
  1862 }
  1841 
  1863 
  1842 
  1864 
  1843 /* scrolls the viewport in a window to a given location */
  1865 /* scrolls the viewport in a window to a given location */
  1844 bool ScrollWindowTo(int x , int y, Window *w)
  1866 bool ScrollWindowTo(int x , int y, Window *w, bool instant)
  1845 {
  1867 {
  1846 	Point pt;
  1868 	Point pt;
  1847 
  1869 
  1848 	pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(x, y));
  1870 	pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(x, y));
  1849 	WP(w, vp_d).follow_vehicle = INVALID_VEHICLE;
  1871 	WP(w, vp_d).follow_vehicle = INVALID_VEHICLE;
  1850 
  1872 
  1851 	if (WP(w, vp_d).scrollpos_x == pt.x && WP(w, vp_d).scrollpos_y == pt.y)
  1873 	if (WP(w, vp_d).dest_scrollpos_x == pt.x && WP(w, vp_d).dest_scrollpos_y == pt.y)
  1852 		return false;
  1874 		return false;
  1853 
  1875 
  1854 	WP(w, vp_d).scrollpos_x = pt.x;
  1876 	if (instant) {
  1855 	WP(w, vp_d).scrollpos_y = pt.y;
  1877 		WP(w, vp_d).scrollpos_x = pt.x;
       
  1878 		WP(w, vp_d).scrollpos_y = pt.y;
       
  1879 	}
       
  1880 
       
  1881 	WP(w, vp_d).dest_scrollpos_x = pt.x;
       
  1882 	WP(w, vp_d).dest_scrollpos_y = pt.y;
  1856 	return true;
  1883 	return true;
  1857 }
  1884 }
  1858 
  1885 
  1859 
  1886 
  1860 bool ScrollMainWindowTo(int x, int y)
  1887 bool ScrollMainWindowTo(int x, int y, bool instant)
  1861 {
  1888 {
  1862 	Window *w;
  1889 	Window *w;
  1863 	bool res = ScrollWindowTo(x, y, FindWindowById(WC_MAIN_WINDOW, 0));
  1890 	bool res = ScrollWindowTo(x, y, FindWindowById(WC_MAIN_WINDOW, 0), instant);
  1864 
  1891 
  1865 	/* If a user scrolls to a tile (via what way what so ever) and already is on
  1892 	/* If a user scrolls to a tile (via what way what so ever) and already is on
  1866 	 *  that tile (e.g.: pressed twice), move the smallmap to that location,
  1893 	 *  that tile (e.g.: pressed twice), move the smallmap to that location,
  1867 	 *  so you directly see where you are on the smallmap. */
  1894 	 *  so you directly see where you are on the smallmap. */
  1868 
  1895 
  1875 
  1902 
  1876 	return res;
  1903 	return res;
  1877 }
  1904 }
  1878 
  1905 
  1879 
  1906 
  1880 bool ScrollMainWindowToTile(TileIndex tile)
  1907 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
  1881 {
  1908 {
  1882 	return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2);
  1909 	return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, instant);
  1883 }
  1910 }
  1884 
  1911 
  1885 void SetRedErrorSquare(TileIndex tile)
  1912 void SetRedErrorSquare(TileIndex tile)
  1886 {
  1913 {
  1887 	TileIndex old;
  1914 	TileIndex old;