forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursor.cs
More file actions
110 lines (96 loc) · 3.45 KB
/
Copy pathCursor.cs
File metadata and controls
110 lines (96 loc) · 3.45 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEngine.UIElements
{
/// <summary>
/// Script interface for <see cref="VisualElement"/> cursor style property <see cref="IStyle.cursor"/>.
/// </summary>
public struct Cursor : IEquatable<Cursor>
{
/// <summary>
/// The texture to use for the cursor style. To use a texture as a cursor, import the texture with "Read/Write enabled" in the texture importer (or using the "Cursor" defaults).
/// </summary>
public Texture2D texture { get; set; }
/// <summary>
/// The offset from the top left of the texture to use as the target point (must be within the bounds of the cursor).
/// </summary>
public Vector2 hotspot { get; set; }
// Used to support default cursor in the editor (map to MouseCursor enum)
internal int defaultCursorId { get; set; }
public override bool Equals(object obj)
{
return obj is Cursor && Equals((Cursor)obj);
}
/// <undoc/>
public bool Equals(Cursor other)
{
return EqualityComparer<Texture2D>.Default.Equals(texture, other.texture) &&
hotspot.Equals(other.hotspot) &&
defaultCursorId == other.defaultCursorId;
}
public override int GetHashCode()
{
var hashCode = 1500536833;
hashCode = hashCode * -1521134295 + EqualityComparer<Texture2D>.Default.GetHashCode(texture);
hashCode = hashCode * -1521134295 + EqualityComparer<Vector2>.Default.GetHashCode(hotspot);
hashCode = hashCode * -1521134295 + defaultCursorId.GetHashCode();
return hashCode;
}
internal static IEnumerable<Type> allowedAssetTypes
{
get
{
yield return typeof(Texture2D);
}
}
/// <undoc/>
public static bool operator==(Cursor style1, Cursor style2)
{
return style1.Equals(style2);
}
/// <undoc/>
public static bool operator!=(Cursor style1, Cursor style2)
{
return !(style1 == style2);
}
public override string ToString()
{
return $"texture={texture}, hotspot={hotspot}";
}
}
internal interface ICursorManager
{
void SetCursor(Cursor cursor);
void ResetCursor();
}
internal class CursorManager : ICursorManager
{
public bool isCursorOverriden { get; private set; }
public void SetCursor(Cursor cursor)
{
if (cursor.texture != null)
{
UnityEngine.Cursor.SetCursor(cursor.texture, cursor.hotspot, CursorMode.Auto);
isCursorOverriden = true;
}
else
{
if (cursor.defaultCursorId != 0)
{
Debug.LogWarning(
"Runtime cursors other than the default cursor need to be defined using a texture.");
}
ResetCursor();
}
}
public void ResetCursor()
{
if (isCursorOverriden)
UnityEngine.Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
isCursorOverriden = false;
}
}
}