CrabUI
Loading...
Searching...
No Matches
CUIDrawing.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6
7using Barotrauma;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Graphics;
11using System.IO;
12
13namespace CrabUI
14{
15 public partial class CUI
16 {
17 public const float Pi2 = (float)(Math.PI / 2.0);
18
19
20 public static SamplerState NoSmoothing = new SamplerState()
21 {
22 Filter = TextureFilter.Point,
23 AddressU = TextureAddressMode.Clamp,
24 AddressV = TextureAddressMode.Clamp,
25 AddressW = TextureAddressMode.Clamp,
26 BorderColor = Color.White,
27 MaxAnisotropy = 4,
28 MaxMipLevel = 0,
29 MipMapLevelOfDetailBias = -0.8f,
30 ComparisonFunction = CompareFunction.Never,
31 FilterMode = TextureFilterMode.Default,
32 };
33
34 public static void DrawTexture(SpriteBatch sb, CUIRect cuirect, Texture2D texture, Color cl, CUISpriteDrawMode drawMode = CUISpriteDrawMode.Resize)
35 {
36 Rectangle sourceRect = drawMode switch
37 {
38 CUISpriteDrawMode.Resize => texture.Bounds,
39 CUISpriteDrawMode.Wrap => new Rectangle(0, 0, (int)cuirect.Width, (int)cuirect.Height),
40 CUISpriteDrawMode.Static => cuirect.Box,
41 CUISpriteDrawMode.StaticDeep => cuirect.Zoom(0.9f),
42 _ => texture.Bounds,
43 };
44
45 sb.Draw(texture, cuirect.Box, sourceRect, cl, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
46 }
47 public static void DrawRectangle(SpriteBatch sb, CUIRect cuirect, Color cl, CUISprite sprite, float depth = 0.0f)
48 {
49 Rectangle sourceRect = sprite.DrawMode switch
50 {
51 CUISpriteDrawMode.Resize => sprite.SourceRect,
52 CUISpriteDrawMode.Wrap => new Rectangle(0, 0, (int)cuirect.Width, (int)cuirect.Height),
53 CUISpriteDrawMode.Static => cuirect.Box,
54 CUISpriteDrawMode.StaticDeep => cuirect.Zoom(0.9f),
55 _ => sprite.SourceRect,
56 };
57
58 Rectangle rect = new Rectangle(
59 (int)(cuirect.Left + sprite.Offset.X * cuirect.Width),
60 (int)(cuirect.Top + sprite.Offset.Y * cuirect.Height),
61 (int)(cuirect.Width),
62 (int)(cuirect.Height)
63 );
64
65 //rect = cuirect.Box;
66
67 sb.Draw(sprite.Texture, rect, sourceRect, cl, sprite.Rotation, sprite.Origin, sprite.Effects, depth);
68 }
69
70 //TODO i can calculate those rects in advance
71 // perhaps i should check nulls on border set and not in draw
72 public static void DrawBorders(SpriteBatch sb, CUIComponent component, float depth = 0.0f)
73 {
74 Texture2D texture = component.BorderSprite.Texture;
75 Rectangle sourceRect = texture.Bounds;
76
77 Rectangle targetRect;
78 Color cl;
79 float rotation = 0.0f;
80 float thickness = 1.0f;
81 bool visible = false;
82
83 // Right
84 visible = component.RigthBorder?.Visible ?? component.Border?.Visible ?? false;
85 thickness = component.RigthBorder?.Thickness ?? component.Border?.Thickness ?? 0;
86 cl = component.RigthBorder?.Color ?? component.Border?.Color ?? Color.Transparent;
87 targetRect = CUIRect.CreateRect(
88 component.Real.Left + component.Real.Width,
89 component.Real.Top,
90 component.Real.Height,
91 thickness
92 );
93 sourceRect = CUIRect.CreateRect(
94 0, 0,
95 targetRect.Width, texture.Height
96 );
97 rotation = Pi2;
98 sb.Draw(texture, targetRect, sourceRect, cl, rotation, Vector2.Zero, SpriteEffects.None, depth);
99
100 //Left
101 visible = component.LeftBorder?.Visible ?? component.Border?.Visible ?? false;
102 thickness = component.LeftBorder?.Thickness ?? component.Border?.Thickness ?? 0;
103 cl = component.LeftBorder?.Color ?? component.Border?.Color ?? Color.Transparent;
104 targetRect = CUIRect.CreateRect(
105 component.Real.Left + thickness,
106 component.Real.Top,
107 component.Real.Height,
108 thickness
109 );
110 sourceRect = CUIRect.CreateRect(
111 0, 0,
112 targetRect.Width, texture.Height
113 );
114 rotation = Pi2;
115 sb.Draw(texture, targetRect, sourceRect, cl, rotation, Vector2.Zero, SpriteEffects.FlipVertically, depth);
116
117
118 //Top
119 visible = component.TopBorder?.Visible ?? component.Border?.Visible ?? false;
120 thickness = component.TopBorder?.Thickness ?? component.Border?.Thickness ?? 0;
121 cl = component.TopBorder?.Color ?? component.Border?.Color ?? Color.Transparent;
122 targetRect = CUIRect.CreateRect(
123 component.Real.Left,
124 component.Real.Top,
125 component.Real.Width,
126 thickness
127 );
128 sourceRect = CUIRect.CreateRect(
129 0, 0,
130 targetRect.Width, texture.Height
131 );
132 rotation = 0.0f;
133 sb.Draw(texture, targetRect, sourceRect, cl, rotation, Vector2.Zero, SpriteEffects.None, depth);
134
135
136
137 //Bottom
138 visible = component.BottomBorder?.Visible ?? component.Border?.Visible ?? false;
139 thickness = component.BottomBorder?.Thickness ?? component.Border?.Thickness ?? 0;
140 cl = component.BottomBorder?.Color ?? component.Border?.Color ?? Color.Transparent;
141 targetRect = CUIRect.CreateRect(
142 component.Real.Left,
143 component.Real.Bottom - thickness,
144 component.Real.Width,
145 thickness
146 );
147 sourceRect = CUIRect.CreateRect(
148 0, 0,
149 targetRect.Width, texture.Height
150 );
151 rotation = 0;
152 sb.Draw(texture, targetRect, sourceRect, cl, rotation, Vector2.Zero, SpriteEffects.FlipVertically, depth);
153
154
155 }
156 }
157}