2using System.Collections.Generic;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
19 public partial class CUIComponent
21 #region Events --------------------------------------------------------
23 [CUISerializable]
public bool ConsumeMouseClicks {
get;
set; }
24 [CUISerializable]
public bool ConsumeDragAndDrop {
get;
set; }
25 [CUISerializable]
public bool ConsumeSwipe {
get;
set; }
26 [CUISerializable]
public bool ConsumeMouseScroll {
get;
set; }
29 public void CascadeRefresh()
31 if (
this is IRefreshable refreshable) refreshable.Refresh();
32 Children.ForEach(c => c.CascadeRefresh());
35 public event Action OnTreeChanged;
36 public event Action<double> OnUpdate;
37 public event Action<CUIInput> OnMouseLeave;
38 public event Action<CUIInput> OnMouseEnter;
39 public event Action<CUIInput> OnMouseDown;
40 public event Action<CUIInput> OnMouseUp;
41 public event Action<CUIInput> OnMouseMove;
42 public event Action<CUIInput> OnMouseOn;
43 public event Action<CUIInput> OnMouseOff;
44 public event Action<CUIInput> OnClick;
45 public event Action<CUIInput> OnDClick;
46 public event Action<CUIInput> OnScroll;
47 public event Action<float, float> OnDrag;
48 public event Action<float, float> OnSwipe;
49 public event Action<CUIInput> OnKeyDown;
50 public event Action<CUIInput> OnKeyUp;
51 public event Action<CUIInput> OnTextInput;
52 public event Action OnFocus;
53 public event Action OnFocusLost;
56 public Action<double> AddOnUpdate {
set { OnUpdate += value; } }
57 public Action<CUIInput> AddOnMouseLeave {
set { OnMouseLeave += value; } }
58 public Action<CUIInput> AddOnMouseEnter {
set { OnMouseEnter += value; } }
59 public Action<CUIInput> AddOnMouseDown {
set { OnMouseDown += value; } }
60 public Action<CUIInput> AddOnMouseUp {
set { OnMouseUp += value; } }
61 public Action<CUIInput> AddOnMouseMove {
set { OnMouseMove += value; } }
62 public Action<CUIInput> AddOnMouseOn {
set { OnMouseOn += value; } }
63 public Action<CUIInput> AddOnMouseOff {
set { OnMouseOff += value; } }
64 public Action<CUIInput> AddOnClick {
set { OnClick += value; } }
65 public Action<CUIInput> AddOnDClick {
set { OnDClick += value; } }
66 public Action<CUIInput> AddOnScroll {
set { OnScroll += value; } }
67 public Action<float, float> AddOnDrag {
set { OnDrag += value; } }
68 public Action<float, float> AddOnSwipe {
set { OnSwipe += value; } }
69 public Action<CUIInput> AddOnKeyDown {
set { OnKeyDown += value; } }
70 public Action<CUIInput> AddOnKeyUp {
set { OnKeyUp += value; } }
71 public Action<CUIInput> AddOnTextInput {
set { OnTextInput += value; } }
72 public Action AddOnFocus {
set { OnFocus += value; } }
73 public Action AddOnFocusLost {
set { OnFocusLost += value; } }
76 public virtual bool IsPointOnTransparentPixel(Vector2 point)
81 Vector2 RotationCenter =
new Vector2(
88 float x = v.X /
Real.Width;
89 float y = v.Y /
Real.Height;
94 int textureX = (int)Math.Round(SourceRect.X + x * SourceRect.Width);
95 int textureY = (int)Math.Round(SourceRect.Y + y * SourceRect.Height);
97 if (textureX < SourceRect.X || (SourceRect.X + SourceRect.Width - 1) < textureX)
return true;
98 if (textureY < SourceRect.Y || (SourceRect.Y + SourceRect.Height - 1) < textureY)
return true;
100 Color cl =
TextureData[textureY * bounds.Width + textureX];
106 public virtual bool ShouldInvoke(CUIInput e)
110 return !IsPointOnTransparentPixel(e.MousePosition);
116 internal void InvokeOnUpdate(
double totalTime) => OnUpdate?.Invoke(totalTime);
117 internal void InvokeOnMouseLeave(CUIInput e) { OnMouseLeave?.Invoke(e); }
118 internal void InvokeOnMouseEnter(CUIInput e) {
if (ShouldInvoke(e)) OnMouseEnter?.Invoke(e); }
119 internal void InvokeOnMouseDown(CUIInput e) {
if (ShouldInvoke(e)) OnMouseDown?.Invoke(e); }
120 internal void InvokeOnMouseUp(CUIInput e) {
if (ShouldInvoke(e)) OnMouseUp?.Invoke(e); }
121 internal void InvokeOnMouseMove(CUIInput e) {
if (ShouldInvoke(e)) OnMouseMove?.Invoke(e); }
122 internal void InvokeOnMouseOn(CUIInput e) {
if (ShouldInvoke(e)) OnMouseOn?.Invoke(e); }
123 internal void InvokeOnMouseOff(CUIInput e) {
if (ShouldInvoke(e)) OnMouseOff?.Invoke(e); }
124 internal void InvokeOnClick(CUIInput e) {
if (ShouldInvoke(e)) OnClick?.Invoke(e); }
125 internal void InvokeOnDClick(CUIInput e) {
if (ShouldInvoke(e)) OnDClick?.Invoke(e); }
126 internal void InvokeOnScroll(CUIInput e) {
if (ShouldInvoke(e)) OnScroll?.Invoke(e); }
127 internal void InvokeOnDrag(
float x,
float y) => OnDrag?.Invoke(x, y);
128 internal void InvokeOnSwipe(
float x,
float y) => OnSwipe?.Invoke(x, y);
129 internal void InvokeOnKeyDown(CUIInput e) {
if (ShouldInvoke(e)) OnKeyDown?.Invoke(e); }
130 internal void InvokeOnKeyUp(CUIInput e) {
if (ShouldInvoke(e)) OnKeyUp?.Invoke(e); }
131 internal void InvokeOnTextInput(CUIInput e) {
if (ShouldInvoke(e)) OnTextInput?.Invoke(e); }
132 internal void InvokeOnFocus() => OnFocus?.Invoke();
133 internal void InvokeOnFocusLost() => OnFocusLost?.Invoke();
136 #region Handles --------------------------------------------------------
138 internal CUIDragHandle DragHandle =
new CUIDragHandle();
140 public bool Draggable
142 get => DragHandle.Draggable;
143 set => DragHandle.Draggable = value;
146 internal CUIFocusHandle FocusHandle =
new CUIFocusHandle();
148 public bool Focusable
150 get => FocusHandle.Focusable;
151 set => FocusHandle.Focusable = value;
153 public CUIResizeHandle LeftResizeHandle =
new CUIResizeHandle(
new Vector2(0, 1),
new CUIBool2(
false,
false));
154 public CUIResizeHandle RightResizeHandle =
new CUIResizeHandle(
new Vector2(1, 1),
new CUIBool2(
true,
false));
155 public bool Resizible
157 get => ResizibleLeft || ResizibleRight;
158 set { ResizibleLeft = value; ResizibleRight = value; }
162 public bool ResizibleLeft
164 get => LeftResizeHandle.Visible;
165 set => LeftResizeHandle.Visible = value;
169 public bool ResizibleRight
171 get => RightResizeHandle.Visible;
172 set => RightResizeHandle.Visible = value;
176 public CUIBool2 ResizeDirection
178 get => RightResizeHandle.Direction;
181 LeftResizeHandle.Direction = value;
182 RightResizeHandle.Direction = value;
186 internal CUISwipeHandle SwipeHandle =
new CUISwipeHandle();
188 public bool Swipeable
190 get => SwipeHandle.Swipeable;
191 set => SwipeHandle.Swipeable = value;
Color[] TextureData
Buffer for texture data, for IgnoreTransparent checks.
CUIRect Real
Calculated prop, position on real screen in pixels Should be fully calculated after CUIMainComponent....
bool IgnoreTransparent
If true, mouse events on transparent pixels will be ignored Note: this will buffer texture data and...
CUISprite BackgroundSprite
Will be drawn in background with BackgroundColor Default is solid white 1x1 texture.
Vector2 Offset
Draw offset from CUIComponent Position For your convenience also sets origin.
float Rotation
In radians.
Rectangle SourceRect
Part of the texture that is drawn Won't work in Wrap mode becase it will loop the whole texture.
CUISpriteDrawMode DrawMode
Resize - will resize the sprite to component Wrap - will loop the texture Static - sprite ignores...
Texture2D Texture
The link to the texture Multiple sprites can use the same texture.