CrabUI
Loading...
Searching...
No Matches
CUICanvas.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5using Barotrauma;
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Input;
8using Microsoft.Xna.Framework.Graphics;
9
10namespace CrabUI
11{
12
13 /// <summary>
14 /// Allows you to manipulate pixel data of its texture
15 /// </summary>
16 public class CUICanvas : CUIComponent, IDisposable
17 {
18 public Color[] Data;
19
20 public RenderTarget2D Texture;
21
22
23 /// <summary>
24 /// Size of the internal texture
25 /// Will automatically resize the texture and data array of set
26 /// </summary>
27 public virtual Point Size
28 {
29 get => new Point(Texture.Width, Texture.Height);
30 set
31 {
32 if (value.X == Texture?.Width && value.Y == Texture?.Height) return;
33
34 RenderTarget2D oldTexture = Texture;
35 Texture = new RenderTarget2D(GameMain.Instance.GraphicsDevice, value.X, value.Y);
36 Data = new Color[Texture.Width * Texture.Height];
37 BackgroundSprite = new CUISprite(Texture);
38 oldTexture?.Dispose();
39 }
40 }
41
42 public void Clear(Color? color = null)
43 {
44 Color cl = color ?? Color.Transparent;
45 for (int i = 0; i < Data.Length; i++)
46 {
47 Data[i] = cl;
48 }
49
50 SetData();
51 }
52
53 public Color GetPixel(int x, int y)
54 {
55 return Data[y * Texture.Width + x];
56 }
57
58 public void SetPixel(int x, int y, Color cl)
59 {
60 Data[y * Texture.Width + x] = cl;
61 }
62
63 /// <summary>
64 /// Call this method to transfer values from Data array into texture
65 /// </summary>
66 public void SetData()
67 {
68 Texture.SetData<Color>(Data);
69 }
70
71 /// <summary>
72 /// Uses renderFunc to render stuff directy onto Canvas.Texture
73 /// You can for example use GUI "Draw" methods with provided spriteBatch
74 /// </summary>
75 /// <param name="renderFunc"> Action<SpriteBatch> where you can draw whatever you want </param>
76 public void Render(Action<SpriteBatch> renderFunc)
77 {
78 GameMain.Instance.GraphicsDevice.SetRenderTarget(Texture);
79
80 //TODO save and restore scissor rect
81 spriteBatch.Begin(SpriteSortMode.Deferred, null, GUI.SamplerState, null, GameMain.ScissorTestEnable);
82
83 renderFunc(spriteBatch);
84
85 spriteBatch.End();
86
87 GameMain.Instance.GraphicsDevice.SetRenderTarget(null);
88 }
89
90 public SpriteBatch spriteBatch;
91
92 public CUICanvas(int x, int y) : base()
93 {
94 Size = new Point(x, y);
95 spriteBatch = new SpriteBatch(GameMain.Instance.GraphicsDevice);
96 }
97
98 public CUICanvas() : this(100, 100) { }
99
100 public override void CleanUp()
101 {
102 Texture?.Dispose();
103 }
104 }
105}
Allows you to manipulate pixel data of its texture.
Definition CUICanvas.cs:17
void Render(Action< SpriteBatch > renderFunc)
Uses renderFunc to render stuff directy onto Canvas.Texture You can for example use GUI "Draw" method...
Definition CUICanvas.cs:76
void SetData()
Call this method to transfer values from Data array into texture.
Definition CUICanvas.cs:66
virtual Point Size
Size of the internal texture Will automatically resize the texture and data array of set.
Definition CUICanvas.cs:28
Base class for all components.
CUISprite BackgroundSprite
Will be drawn in background with BackgroundColor Default is solid white 1x1 texture.
Wrapper Containing link to texture and drawing settings, like SourceRedt, DrawMode,...
Definition CUISprite.cs:27