-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScissorStack.cs
75 lines (60 loc) · 1.43 KB
/
ScissorStack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections.Generic;
using SFML.Graphics;
namespace Tiler
{
public static class ScissorStack
{
private static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
private static void Apply(RenderTarget renderTarget)
{
if (Stack.Count == 0)
{
UtilsDrawing.EnableScissor(false);
return;
}
var top = Stack.Peek();
UtilsDrawing.EnableScissor(true);
UtilsDrawing.SetScissor(renderTarget, top.Left, top.Top, top.Width, top.Height);
}
public static Stack<IntRect> Stack = new Stack<IntRect>();
public static void Push(RenderTarget renderTarget, IntRect rect)
{
if (Stack.Count == 0)
{
Stack.Push(rect);
}
else
{
var top = Stack.Peek();
rect.Left = Clamp(rect.Left, top.Left, top.Left + top.Width);
rect.Top = Clamp(rect.Top, top.Top, top.Top + top.Height);
rect.Width = Clamp(rect.Width, 0, top.Left + top.Width - rect.Left);
rect.Height = Clamp(rect.Height, 0, top.Top + top.Height - rect.Top);
Stack.Push(rect);
}
Apply(renderTarget);
}
public static void Pop(RenderTarget renderTarget)
{
if (Stack.Count == 0)
return;
Stack.Pop();
Apply(renderTarget);
}
public static void PushScissor(this RenderTarget target, IntRect rect)
{
Push(target, rect);
}
public static void PopScissor(this RenderTarget target)
{
Pop(target);
}
}
}