From 94554d7678a5fce143df0c6c5253469643ea2873 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 23 Oct 2013 18:40:04 +1300 Subject: [PATCH] Support nested scissor rectangles. --- OpenRA.Game/Graphics/Renderer.cs | 22 ++++++++++++++++--- OpenRA.Game/Graphics/WorldRenderer.cs | 2 +- OpenRA.Game/Widgets/ChatDisplayWidget.cs | 2 +- OpenRA.Game/Widgets/HotkeyEntryWidget.cs | 4 ++-- OpenRA.Game/Widgets/ScrollPanelWidget.cs | 2 +- OpenRA.Game/Widgets/TextFieldWidget.cs | 4 ++-- .../Widgets/ProductionTabsWidget.cs | 2 +- OpenRA.Mods.RA/Widgets/RadarWidget.cs | 2 +- 8 files changed, 28 insertions(+), 12 deletions(-) diff --git a/OpenRA.Game/Graphics/Renderer.cs b/OpenRA.Game/Graphics/Renderer.cs index 2d510ff999..f75316a2d9 100644 --- a/OpenRA.Game/Graphics/Renderer.cs +++ b/OpenRA.Game/Graphics/Renderer.cs @@ -39,12 +39,14 @@ namespace OpenRA.Graphics Queue> tempBuffers = new Queue>(); public Dictionary Fonts; + Stack scissorState; public Renderer() { TempBufferSize = Game.Settings.Graphics.BatchSize; TempBufferCount = Game.Settings.Graphics.NumTempBuffers; SheetSize = Game.Settings.Graphics.SheetSize; + scissorState = new Stack(); WorldSpriteRenderer = new SpriteRenderer(this, device.CreateShader("shp")); WorldRgbaSpriteRenderer = new SpriteRenderer(this, device.CreateShader("rgba")); @@ -183,16 +185,30 @@ namespace OpenRA.Graphics } } - public void EnableScissor(int left, int top, int width, int height) + public void EnableScissor(Rectangle rect) { + // Must remain inside the current scissor rect + if (scissorState.Any()) + rect.Intersect(scissorState.Peek()); + Flush(); - Device.EnableScissor(left, top, width, height); + Device.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); + scissorState.Push(rect); } public void DisableScissor() { + scissorState.Pop(); Flush(); - Device.DisableScissor(); + + // Restore previous scissor rect + if (scissorState.Any()) + { + var rect = scissorState.Peek(); + Device.EnableScissor(rect.Left, rect.Top, rect.Width, rect.Height); + } + else + Device.DisableScissor(); } public void EnableDepthBuffer() diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 281d1c0198..3712197359 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -115,7 +115,7 @@ namespace OpenRA.Graphics var renderables = GenerateRenderables(); var bounds = Viewport.ScissorBounds; - Game.Renderer.EnableScissor(bounds.Left, bounds.Top, bounds.Width, bounds.Height); + Game.Renderer.EnableScissor(bounds); terrainRenderer.Draw(this, Viewport); Game.Renderer.Flush(); diff --git a/OpenRA.Game/Widgets/ChatDisplayWidget.cs b/OpenRA.Game/Widgets/ChatDisplayWidget.cs index 163b6dc6a2..04e140ea73 100644 --- a/OpenRA.Game/Widgets/ChatDisplayWidget.cs +++ b/OpenRA.Game/Widgets/ChatDisplayWidget.cs @@ -38,7 +38,7 @@ namespace OpenRA.Widgets var chatpos = new int2(chatLogArea.X + 5, chatLogArea.Bottom - 5); var font = Game.Renderer.Fonts["Regular"]; - Game.Renderer.EnableScissor(chatLogArea.Left, chatLogArea.Top, chatLogArea.Width, chatLogArea.Height); + Game.Renderer.EnableScissor(chatLogArea); foreach (var line in recentLines.AsEnumerable().Reverse()) { diff --git a/OpenRA.Game/Widgets/HotkeyEntryWidget.cs b/OpenRA.Game/Widgets/HotkeyEntryWidget.cs index daa83ff1d8..3c5ffdf51d 100644 --- a/OpenRA.Game/Widgets/HotkeyEntryWidget.cs +++ b/OpenRA.Game/Widgets/HotkeyEntryWidget.cs @@ -124,8 +124,8 @@ namespace OpenRA.Widgets // Scissor when the text overflows if (textSize.X > Bounds.Width - LeftMargin - RightMargin) { - Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, - Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom); + Game.Renderer.EnableScissor(new Rectangle(pos.X + LeftMargin, pos.Y, + Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom)); } var color = disabled ? DisabledColor : TextColor; diff --git a/OpenRA.Game/Widgets/ScrollPanelWidget.cs b/OpenRA.Game/Widgets/ScrollPanelWidget.cs index 909ba00690..878402c79a 100644 --- a/OpenRA.Game/Widgets/ScrollPanelWidget.cs +++ b/OpenRA.Game/Widgets/ScrollPanelWidget.cs @@ -115,7 +115,7 @@ namespace OpenRA.Widgets WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", DownPressed || downDisabled ? "down_pressed" : "down_arrow"), new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset)); - Game.Renderer.EnableScissor(backgroundRect.X + 1, backgroundRect.Y + 1, backgroundRect.Width - 2, backgroundRect.Height - 2); + Game.Renderer.EnableScissor(backgroundRect.InflateBy(-1, -1, -1, -1)); foreach (var child in Children) child.DrawOuter(); diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index 7afcbad503..b4e8bb5d5e 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -216,8 +216,8 @@ namespace OpenRA.Widgets if (HasKeyboardFocus) textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0); - Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, - Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom); + Game.Renderer.EnableScissor(new Rectangle(pos.X + LeftMargin, pos.Y, + Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom)); } var color = disabled ? DisabledColor : TextColor; diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs index 336b9fa97e..cf05ad0bf6 100755 --- a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs @@ -157,7 +157,7 @@ namespace OpenRA.Mods.Cnc.Widgets new float2(rightButtonRect.Left + 2, rightButtonRect.Top + 2)); // Draw tab buttons - Game.Renderer.EnableScissor(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height); + Game.Renderer.EnableScissor(new Rectangle(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height)); var origin = new int2(leftButtonRect.Right - 1 + (int)listOffset, leftButtonRect.Y); SpriteFont font = Game.Renderer.Fonts["TinyBold"]; contentWidth = 0; diff --git a/OpenRA.Mods.RA/Widgets/RadarWidget.cs b/OpenRA.Mods.RA/Widgets/RadarWidget.cs index ad14b6865e..eb3c518e11 100755 --- a/OpenRA.Mods.RA/Widgets/RadarWidget.cs +++ b/OpenRA.Mods.RA/Widgets/RadarWidget.cs @@ -156,7 +156,7 @@ namespace OpenRA.Mods.RA.Widgets var tl = CellToMinimapPixel(worldRenderer.Position(worldRenderer.Viewport.TopLeft).ToCPos()); var br = CellToMinimapPixel(worldRenderer.Position(worldRenderer.Viewport.BottomRight).ToCPos()); - Game.Renderer.EnableScissor(mapRect.Left, mapRect.Top, mapRect.Width, mapRect.Height); + Game.Renderer.EnableScissor(mapRect); Game.Renderer.LineRenderer.DrawRect(tl, br, Color.White); Game.Renderer.DisableScissor(); }