Refactor Viewport.

This commit is contained in:
Paul Chote
2010-11-24 11:57:18 +13:00
parent b8eda5a152
commit 1dfe437641
2 changed files with 17 additions and 33 deletions

View File

@@ -168,7 +168,7 @@ namespace OpenRA
BeforeGameStart(); BeforeGameStart();
var map = modData.PrepareMap(mapUID); var map = modData.PrepareMap(mapUID);
viewport = new Viewport(new int2(Renderer.Resolution), map.TopLeft, map.BottomRight, Renderer); viewport = new Viewport(new int2(Renderer.Resolution), map.Bounds, Renderer);
orderManager.world = new World(modData.Manifest, map, orderManager); orderManager.world = new World(modData.Manifest, map, orderManager);
worldRenderer = new WorldRenderer(orderManager.world); worldRenderer = new WorldRenderer(orderManager.world);

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
@@ -21,8 +22,7 @@ namespace OpenRA.Graphics
readonly int2 screenSize; readonly int2 screenSize;
int2 scrollPosition; int2 scrollPosition;
readonly Renderer renderer; readonly Renderer renderer;
readonly int2 mapStart; readonly Rectangle adjustedMapBounds;
readonly int2 mapEnd;
public float2 Location { get { return scrollPosition; } } public float2 Location { get { return scrollPosition; } }
@@ -52,60 +52,44 @@ namespace OpenRA.Graphics
private int2 NormalizeScrollPosition(int2 newScrollPosition) private int2 NormalizeScrollPosition(int2 newScrollPosition)
{ {
var topLeftBorder = Game.CellSize* mapStart; return new int2(Math.Min(adjustedMapBounds.Right, Math.Max(newScrollPosition.X, adjustedMapBounds.Left)),
var bottomRightBorder = Game.CellSize* mapEnd; Math.Min(adjustedMapBounds.Bottom, Math.Max(newScrollPosition.Y, adjustedMapBounds.Top)));
if(newScrollPosition.Y < topLeftBorder.Y - screenSize.Y/2)
newScrollPosition.Y = topLeftBorder.Y - screenSize.Y/2;
if(newScrollPosition.X < topLeftBorder.X - screenSize.X/2)
newScrollPosition.X = topLeftBorder.X - screenSize.X/2;
if(newScrollPosition.Y > bottomRightBorder.Y - screenSize.Y/2)
newScrollPosition.Y = bottomRightBorder.Y - screenSize.Y/2;
if(newScrollPosition.X > bottomRightBorder.X - screenSize.X/2)
newScrollPosition.X = bottomRightBorder.X - screenSize.X/2;
return newScrollPosition;
} }
public ScrollDirection GetBlockedDirections() public ScrollDirection GetBlockedDirections()
{ {
int2 topLeftBorder = (Game.CellSize* mapStart);
int2 bottomRightBorder = (Game.CellSize* mapEnd);
ScrollDirection blockedDirections = ScrollDirection.None; ScrollDirection blockedDirections = ScrollDirection.None;
if(scrollPosition.Y <= adjustedMapBounds.Top)
if(scrollPosition.Y <= topLeftBorder.Y - screenSize.Y/2)
blockedDirections = blockedDirections.Set(ScrollDirection.Up, true); blockedDirections = blockedDirections.Set(ScrollDirection.Up, true);
if(scrollPosition.X <= topLeftBorder.X - screenSize.X/2) if(scrollPosition.X <= adjustedMapBounds.Left)
blockedDirections = blockedDirections.Set(ScrollDirection.Left, true); blockedDirections = blockedDirections.Set(ScrollDirection.Left, true);
if(scrollPosition.Y >= bottomRightBorder.Y - screenSize.Y/2) if(scrollPosition.Y >= adjustedMapBounds.Bottom)
blockedDirections = blockedDirections.Set(ScrollDirection.Down, true); blockedDirections = blockedDirections.Set(ScrollDirection.Down, true);
if(scrollPosition.X >= bottomRightBorder.X - screenSize.X/2) if(scrollPosition.X >= adjustedMapBounds.Right)
blockedDirections = blockedDirections.Set(ScrollDirection.Right, true); blockedDirections = blockedDirections.Set(ScrollDirection.Right, true);
return blockedDirections; return blockedDirections;
} }
public Viewport(int2 screenSize, int2 mapStart, int2 mapEnd, Renderer renderer) public Viewport(int2 screenSize, Rectangle mapBounds, Renderer renderer)
{ {
this.screenSize = screenSize; this.screenSize = screenSize;
this.renderer = renderer; this.renderer = renderer;
this.mapStart = mapStart; this.adjustedMapBounds = new Rectangle(Game.CellSize*mapBounds.X - screenSize.X/2,
this.mapEnd = mapEnd; Game.CellSize*mapBounds.Y - screenSize.Y/2,
Game.CellSize*mapBounds.Width,
this.scrollPosition = Game.CellSize* mapStart; Game.CellSize*mapBounds.Height);
this.scrollPosition = new int2(adjustedMapBounds.Location);
} }
public void DrawRegions( WorldRenderer wr, IInputHandler inputHandler ) public void DrawRegions( WorldRenderer wr, IInputHandler inputHandler )
{ {
renderer.BeginFrame(scrollPosition); renderer.BeginFrame(scrollPosition);
wr.Draw(); wr.Draw();
Widget.DoDraw( wr ); Widget.DoDraw( wr );
var cursorName = Widget.RootWidget.GetCursorOuter(Viewport.LastMousePos) ?? "default"; var cursorName = Widget.RootWidget.GetCursorOuter(Viewport.LastMousePos) ?? "default";
var c = new Cursor(cursorName); new Cursor(cursorName).Draw(wr, (int)cursorFrame, Viewport.LastMousePos + Location);
c.Draw(wr, (int)cursorFrame, Viewport.LastMousePos + Location);
renderer.EndFrame( inputHandler ); renderer.EndFrame( inputHandler );
} }