Created
August 5, 2019 10:22
-
-
Save karljj1/94760e1d01d281c002f8cf08a9cbe34f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine.UIElements; | |
public class SelectableVisualElement : VisualElement | |
{ | |
internal new class UxmlFactory : UxmlFactory<SelectableVisualElement> { } | |
static SelectableVisualElement CurrentSelected; | |
public virtual string HoverStyle { get => "selectable__hover"; } | |
public virtual string SelectedStyle { get => "selectable__selected"; } | |
public bool Selected { get; set; } | |
public SelectableVisualElement() | |
{ | |
RegisterCallback<MouseEnterEvent>((evt) => EnableInClassList(HoverStyle, true)); | |
RegisterCallback<MouseLeaveEvent>((evt) => EnableInClassList(HoverStyle, false)); | |
RegisterCallback<MouseDownEvent>((evt) => Select()); | |
} | |
void Select() | |
{ | |
if (CurrentSelected != null) | |
{ | |
CurrentSelected.EnableInClassList(SelectedStyle, false); | |
Selected = false; | |
CurrentSelected = null; | |
} | |
EnableInClassList(SelectedStyle, true); | |
CurrentSelected = this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple way to create a VisualElement that can be selected.