CrabUI
Loading...
Searching...
No Matches
CUIComponent.Events.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
7using System.IO;
8
9using Barotrauma;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
13
14using System.Xml;
15using System.Xml.Linq;
16
17namespace CrabUI
18{
19 public partial class CUIComponent
20 {
21 #region Events --------------------------------------------------------
22
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; }
27
28 //HACK no one will ever find it, hehehe
29 public void CascadeRefresh()
30 {
31 if (this is IRefreshable refreshable) refreshable.Refresh();
32 Children.ForEach(c => c.CascadeRefresh());
33 }
34
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;
54
55
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; } }
74
75 //TODO add more CUISpriteDrawModes
76 public virtual bool IsPointOnTransparentPixel(Vector2 point)
77 {
78 if (BackgroundSprite.DrawMode != CUISpriteDrawMode.Resize) return true;
79
80 //TODO hangle case where offset != sprite.origin
81 Vector2 RotationCenter = new Vector2(
84 );
85
86 Vector2 v = (point - Real.Position - RotationCenter).Rotate(-BackgroundSprite.Rotation) + RotationCenter;
87
88 float x = v.X / Real.Width;
89 float y = v.Y / Real.Height;
90
91 Rectangle bounds = BackgroundSprite.Texture.Bounds;
92 Rectangle SourceRect = BackgroundSprite.SourceRect;
93
94 int textureX = (int)Math.Round(SourceRect.X + x * SourceRect.Width);
95 int textureY = (int)Math.Round(SourceRect.Y + y * SourceRect.Height);
96
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;
99
100 Color cl = TextureData[textureY * bounds.Width + textureX];
101
102 return cl.A == 0;
103 }
104
105
106 public virtual bool ShouldInvoke(CUIInput e)
107 {
109 {
110 return !IsPointOnTransparentPixel(e.MousePosition);
111 }
112
113 return true;
114 }
115
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();
134
135 #endregion
136 #region Handles --------------------------------------------------------
137
138 internal CUIDragHandle DragHandle = new CUIDragHandle();
139 [CUISerializable]
140 public bool Draggable
141 {
142 get => DragHandle.Draggable;
143 set => DragHandle.Draggable = value;
144 }
145 //HACK Do i really need this?
146 internal CUIFocusHandle FocusHandle = new CUIFocusHandle();
147 [CUISerializable]
148 public bool Focusable
149 {
150 get => FocusHandle.Focusable;
151 set => FocusHandle.Focusable = value;
152 }
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
156 {
157 get => ResizibleLeft || ResizibleRight;
158 set { ResizibleLeft = value; ResizibleRight = value; }
159 }
160
161 [CUISerializable]
162 public bool ResizibleLeft
163 {
164 get => LeftResizeHandle.Visible;
165 set => LeftResizeHandle.Visible = value;
166 }
167
168 [CUISerializable]
169 public bool ResizibleRight
170 {
171 get => RightResizeHandle.Visible;
172 set => RightResizeHandle.Visible = value;
173 }
174
175 [CUISerializable]
176 public CUIBool2 ResizeDirection
177 {
178 get => RightResizeHandle.Direction;
179 set
180 {
181 LeftResizeHandle.Direction = value;
182 RightResizeHandle.Direction = value;
183 }
184 }
185
186 internal CUISwipeHandle SwipeHandle = new CUISwipeHandle();
187 [CUISerializable]
188 public bool Swipeable
189 {
190 get => SwipeHandle.Swipeable;
191 set => SwipeHandle.Swipeable = value;
192 }
193
194 #endregion
195 }
196}
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.
Definition CUISprite.cs:103
float Rotation
In radians.
Definition CUISprite.cs:73
Rectangle SourceRect
Part of the texture that is drawn Won't work in Wrap mode becase it will loop the whole texture.
Definition CUISprite.cs:55
CUISpriteDrawMode DrawMode
Resize - will resize the sprite to component Wrap - will loop the texture Static - sprite ignores...
Definition CUISprite.cs:50
Texture2D Texture
The link to the texture Multiple sprites can use the same texture.
Definition CUISprite.cs:62