public EnumField AddEnumField(Enum value, bool alternate = false, bool secondAlternate = false, bool thirdAlternate = false) { EnumField el = new EnumField(value); el.AddToClassList("list-item-input"); el.AddToClassList("list-item-enum-input"); AddAlternates(el, alternate, secondAlternate, thirdAlternate); el.RegisterValueChangedCallback(e => { eventManager.Raise <ListItemInputChange>(new ListItemInputChange(el)); }); this.Add(el); return(el); }
internal override void Apply(VisualElement container) { /// <sample> // Get a reference to the field from UXML, // initialize it with an Enum type, // and assign it its value. var uxmlField = container.Q <EnumField>("the-uxml-field"); uxmlField.Init(TextAlignment.Center); uxmlField.value = TextAlignment.Left; // Create a new field, disable it, and give it a style class. var csharpField = new EnumField("C# Field", TextAlignment.Center); csharpField.SetEnabled(false); csharpField.AddToClassList("some-styled-field"); csharpField.value = uxmlField.value; container.Add(csharpField); // Mirror value of uxml field into the C# field. uxmlField.RegisterCallback <ChangeEvent <Enum> >((evt) => { csharpField.value = evt.newValue; }); /// </sample> }
public VariableField(Guid id) { guid = id; fieldName = $"worldvar-value-field-{worldVars.GetName(guid)}"; AddToClassList(USS_CLASS_NAME); label = new Label(worldVars.GetName(guid)); Add(label); // Create the type changer dropdown typeChoice = new EnumField(worldVars.GetType(guid)); typeChoice.AddToClassList("worldvar-type-dropdown"); Add(typeChoice); // Register type update callback on dropdown value change typeChoice.RegisterCallback <ChangeEvent <Enum> >(UpdateWorldvarType); // Add a type-specific class based on the current type AddToClassList(TYPE_CLASS_NAME_TEMPLATE + typeChoice.text.ToLower()); // Create a simple container to hold the value field fieldContainer = new VisualElement(); fieldContainer.AddToClassList("worldvar-value-container"); Add(fieldContainer); DrawValueField(); // Create a button at the end to remove the variable var button = new Button() { text = "-", name = $"remove-{worldVars.GetName(guid)}" }; button.AddToClassList("worldvar-remove-button"); // Method to actually remove it is the Remove() method so link that button.clickable.clicked += Remove; Add(button); // Register to collection change events worldVars.VariableRemoved += (guid) => { if (guid == id) { RemoveFromHierarchy(); } }; worldVars.GetVariable(guid).ValueChanged += SetFieldValue; worldVars.GetVariable(guid).NameChanged += SetLabelText; worldVars.GetVariable(guid).TypeChanged += OnTypeChange; }
protected void Init() { AddToClassList(USS_CLASS_NAME); AddToClassList("rounded"); m_KeyField = new TextField("World State") { value = m_WorldStateDescription.Key }; m_KeyField.tooltip = "The identifier of the world state."; m_KeyField.maxLength = 30; hierarchy.Add(m_KeyField); m_KeyField.RegisterCallback <ChangeEvent <string> >(KeyFieldChanged); hierarchy.Add(new Label("Desired Value")); ConfiguratorValueType?enumType = ConfiguratorTypeConverter.GetTypeFromObject(m_WorldStateDescription.Value); m_EnumFieldValueType = new EnumField("Type", enumType != null ? enumType : ConfiguratorValueType.BOOL) { tooltip = "Data type of the world state." }; m_EnumFieldValueType.AddToClassList("desiredValue"); hierarchy.Add(m_EnumFieldValueType); m_EnumFieldValueType.RegisterCallback <ChangeEvent <Enum> >(EnumChanged); m_ValueField = ConfiguratorTypeConverter.CreateFieldBasedOnType(enumType.Value, m_WorldStateDescription.Value, "Value", ValueFieldChanged); m_ValueField.tooltip = "Value of the world state."; m_ValueField.AddToClassList("desiredValue"); hierarchy.Add(m_ValueField); this.AddManipulator(new Clickable(() => OnMouseDown?.Invoke(this))); this.AddManipulator(new ContextualMenuManipulator(_menubuilder => { _menubuilder.menu.AppendAction("Delete", _dropDownMenuAction => OnDelete?.Invoke(this), DropdownMenuAction.Status.Normal); _menubuilder.menu.AppendAction("Duplicate", _dropDownMenuAction => OnDuplicate?.Invoke(this), DropdownMenuAction.Status.Normal); })); }
private void SetupUI() { var styleSheet = AssetDatabase.LoadAssetAtPath <StyleSheet>($"{Utils.packageFolderName}/Editor/StyleSheets/PerformanceTrackerWindow.uss"); rootVisualElement.styleSheets.Add(styleSheet); m_RefreshFromSearch = Delayer.Throttle(RefreshFromSearch); var toolbar = new UnityEditor.UIElements.Toolbar(); toolbar.AddToClassList("perf-toolbar"); toolbar.style.height = PtStyles.itemHeight; var searchBox = new VisualElement(); searchBox.AddToClassList("perf-search-box"); AddSelectorLabel(toolbar, "Tracker"); m_SearchField = new ToolbarSearchField(); m_SearchField.AddToClassList("perf-search-tracker"); m_SearchField.value = m_FilterText; m_SearchField.RegisterCallback <ChangeEvent <string> >((evt) => { m_RefreshFromSearch.Execute(evt.newValue); }); searchBox.Add(m_SearchField); toolbar.Add(searchBox); var resetAllCountersButton = new Button(ResetAllCounters); resetAllCountersButton.text = "Reset all counters"; resetAllCountersButton.AddToClassList("perf-tracker-toolbar-button"); toolbar.Add(resetAllCountersButton); AddSelectorLabel(toolbar, "Update Speed"); var choices = PtModel.RefreshRates.Select(r => r.label).ToList(); var updateSpeedSelector = new UnityEditor.UIElements.PopupField <string>(choices, 0); updateSpeedSelector.value = PtModel.RefreshRates[m_UpdateSpeedIndex].label; updateSpeedSelector.AddToClassList("perf-update-speed-selector"); updateSpeedSelector.RegisterCallback <ChangeEvent <string> >((evt) => { m_UpdateSpeedIndex = Array.FindIndex(PtModel.RefreshRates, (r) => r.label == evt.newValue); ScheduleNextRefresh(); SendAnalytics(Analytics.WindowUsageType.ChangeMonitoringSpeed); }); toolbar.Add(updateSpeedSelector); AddSelectorLabel(toolbar, "Columns"); var columnsChoice = PtModel.ColumnDescriptors.Where(desc => desc.columnsSelectorMaskId > 0).Select(desc => desc.label).ToList(); var columnsSelector = new MaskField(columnsChoice, m_ShownColumn); columnsSelector.RegisterCallback <ChangeEvent <int> >((evt) => { m_ShownColumn = evt.newValue; CreateHeaders(); RefreshTrackers(); SendAnalytics(Analytics.WindowUsageType.ChangeColumnLayout); }); columnsSelector.AddToClassList("perf-columns-selector"); toolbar.Add(columnsSelector); AddSelectorLabel(toolbar, "Sort By"); m_SortBySelector = new EnumField(ColumnId.Name); m_SortBySelector.value = m_SortBy; m_SortBySelector.AddToClassList("perf-sort-by-selector"); m_SortBySelector.RegisterCallback <ChangeEvent <Enum> >((evt) => { ChangeSortOrder((ColumnId)evt.newValue); SendAnalytics(Analytics.WindowUsageType.ChangeSortBy); }); toolbar.Add(m_SortBySelector); var settingsBtn = new Button(() => { SettingsService.OpenUserPreferences(PerformanceTrackerSettings.settingsKey); SendAnalytics(Analytics.WindowUsageType.OpenPreferences); }); settingsBtn.style.backgroundImage = Icons.settings; settingsBtn.style.width = PtStyles.itemHeight - 3; settingsBtn.style.height = PtStyles.itemHeight - 3; toolbar.Add(settingsBtn); rootVisualElement.Add(toolbar); // Set List View Header: m_HeaderRow = new VisualElement(); m_HeaderRow.AddToClassList("perf-header-row-container"); m_HeaderRow.style.flexDirection = FlexDirection.Row; rootVisualElement.Add(m_HeaderRow); CreateHeaders(); m_PinnedTrackersView = new ListView(m_PinnedTrackers, PtStyles.itemHeight, MakeItem, BindPinnedItem); m_PinnedTrackersView.name = k_PinnedTrackerList; m_PinnedTrackersView.AddToClassList("perf-tracker-list"); m_PinnedTrackersView.selectionType = SelectionType.Multiple; m_PinnedTrackersView.style.flexGrow = 0; m_PinnedTrackersView.Q <VisualElement>(null, "unity-scroll-view__content-viewport").RegisterCallback <GeometryChangedEvent>(SyncHeaderAndListsGeometry); rootVisualElement.Add(m_PinnedTrackersView); m_TrackersView = new ListView(m_FilteredTrackers, PtStyles.itemHeight, MakeItem, BindUnpinnedItem); m_TrackersView.name = k_TrackerList; m_TrackersView.AddToClassList("perf-tracker-list"); m_TrackersView.selectionType = SelectionType.Multiple; m_TrackersView.style.flexGrow = 1.0f; m_TrackersView.Q <VisualElement>(null, "unity-scroll-view__content-viewport").RegisterCallback <GeometryChangedEvent>(SyncHeaderAndListsGeometry); rootVisualElement.Add(m_TrackersView); m_NeedsUpdate = true; ScheduleNextRefresh(); UpdateTrackers(); }
private void RefreshUI() { rootVisualElement.Clear(); VisualElement topRowElement = new VisualElement(); topRowElement.AddToClassList("svbm-row"); rootVisualElement.Add(topRowElement); Button refreshButton = new Button(RefreshUI) { name = "Refresh", text = "Refresh" }; refreshButton.AddToClassList("svbm-cell"); topRowElement.Add(refreshButton); Button saveButton = new Button(() => Bookmarks.Instance?.SaveToJson(Bookmarks.path)) { name = "Save", text = "Save" }; saveButton.AddToClassList("svbm-cell"); topRowElement.Add(saveButton); IMGUIContainer header = new IMGUIContainer(OnCustomGUI); header.AddToClassList("sceneLightingButton"); rootVisualElement.Add(header); if (Bookmarks.Count == 0) { return; } for (int i = 0; i < Bookmarks.Count; i++) { var index = i; VisualElement bookmarkElement = new VisualElement(); bookmarkElement.AddToClassList("svbm-row"); Button loadButton = new Button(() => Bookmarks.Instance[index].Load(SceneView.lastActiveSceneView)) { name = "LOAD", text = "LOAD" }; loadButton.AddToClassList("svbm-cell-button"); bookmarkElement.Add(loadButton); Button updateButton = new Button(() => Bookmarks.Instance[index].Save(SceneView.lastActiveSceneView)) { name = "UPDATE", text = "UPDATE" }; updateButton.AddToClassList("svbm-cell-button"); bookmarkElement.Add(updateButton); Button deleteButton = new Button(() => { Bookmarks.Instance.viewpoints.RemoveAt(index); RefreshUI(); }) { name = "DELETE", text = "DELETE" }; deleteButton.AddToClassList("svbm-cell-button"); bookmarkElement.Add(deleteButton); TextField nameField = new TextField("") { tooltip = "Set this bookmark's menu path / name.\nUse '/' to create a submenu." }; nameField.value = Bookmarks.Instance[index].name; nameField.AddToClassList("svbm-cell-text"); nameField.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].name = x.newValue); bookmarkElement.Add(nameField); MaskField overridesMask = new MaskField(Enum.GetNames(typeof(Viewpoint.Overrides)).ToList(), (int)Bookmarks.Instance[index].overrides) { tooltip = "Set this bookmark's Overrides." }; overridesMask.AddToClassList("svbm-cell-mask"); overridesMask.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].overrides = (Viewpoint.Overrides)x.newValue); bookmarkElement.Add(overridesMask); Toggle orthoToggle = new Toggle("") { tooltip = "Enable to set this bookmark's SceneView to Orthographic." }; orthoToggle.value = Bookmarks.Instance[index].settings.ortho; orthoToggle.AddToClassList("svbm-cell-checkbox"); orthoToggle.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.ortho = x.newValue); bookmarkElement.Add(orthoToggle); Toggle is2dToggle = new Toggle("") { tooltip = "Enable to set this bookmark's SceneView to 2D Mode." }; is2dToggle.value = Bookmarks.Instance[index].settings.is2D; is2dToggle.AddToClassList("svbm-cell-checkbox"); is2dToggle.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.is2D = x.newValue); bookmarkElement.Add(is2dToggle); FloatField fovField = new FloatField() { tooltip = "Set this bookmark's Camera Field of View." }; fovField.value = Bookmarks.Instance[index].settings.fov; fovField.AddToClassList("svbm-cell-float"); fovField.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.fov = Mathf.Clamp(x.newValue, 4, 120)); bookmarkElement.Add(fovField); EnumField cameraModeMask = new EnumField("", Bookmarks.Instance[index].settings.mode.drawMode) { tooltip = "Set this bookmark's Camera Shading Mode." }; cameraModeMask.AddToClassList("svbm-cell-mask"); // Catching cases where SceneView.GetBuiltinCameraMode() will fail on some DrawCameraMode such as Normal and User Defined cameraModeMask.RegisterValueChangedCallback((x) => { try { Bookmarks.Instance[index].settings.mode = SceneView.GetBuiltinCameraMode((DrawCameraMode)x.newValue); } catch { cameraModeMask.SetValueWithoutNotify(x.previousValue); } }); bookmarkElement.Add(cameraModeMask); Toggle lightingToggle = new Toggle("") { tooltip = "Enable this bookmark's SceneView Lighting." }; lightingToggle.value = Bookmarks.Instance[index].settings.sceneLighting; lightingToggle.AddToClassList("svbm-cell-checkbox"); lightingToggle.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.sceneLighting = x.newValue); bookmarkElement.Add(lightingToggle); MaskField viewStatesMask = new MaskField(SceneViewStatesLabels, (int)Bookmarks.Instance[index].settings.sceneViewState.GetFlags()) { tooltip = "Set this bookmark's SceneView States." }; viewStatesMask.AddToClassList("svbm-cell-mask"); viewStatesMask.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.sceneViewState.SetFlags((SceneViewExtensions.SceneViewStateFlags)x.newValue)); bookmarkElement.Add(viewStatesMask); Toggle gridToggle = new Toggle("") { tooltip = "Enable this bookmark's SceneView Grid." }; gridToggle.value = Bookmarks.Instance[index].settings.showGrid; gridToggle.AddToClassList("svbm-cell-checkbox"); gridToggle.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.showGrid = x.newValue); bookmarkElement.Add(gridToggle); Toggle gizmosToggle = new Toggle("") { tooltip = "Enable this bookmark's SceneView Gizmos." }; gizmosToggle.value = Bookmarks.Instance[index].settings.drawGizmos; gizmosToggle.AddToClassList("svbm-cell-checkbox"); gizmosToggle.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].settings.drawGizmos = x.newValue); bookmarkElement.Add(gizmosToggle); LayerMaskField visibleLayerMask = new LayerMaskField("", Bookmarks.Instance[index].visibleLayers) { tooltip = "Set this bookmark's Visible Layers." }; visibleLayerMask.AddToClassList("svbm-cell-mask"); visibleLayerMask.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].visibleLayers = x.newValue); bookmarkElement.Add(visibleLayerMask); LayerMaskField lockedLayerMask = new LayerMaskField("", Bookmarks.Instance[index].lockedLayers) { tooltip = "Set this bookmark's Locked Layers." }; lockedLayerMask.AddToClassList("svbm-cell-mask"); lockedLayerMask.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].lockedLayers = x.newValue); bookmarkElement.Add(lockedLayerMask); EnumField shortcutMask = new EnumField("", Bookmarks.Instance[index].shortcut) { tooltip = "Set this bookmark's shortcut\nBeware this will override any other function the shortcut is associated with." }; shortcutMask.AddToClassList("svbm-cell-mask"); shortcutMask.RegisterValueChangedCallback((x) => Bookmarks.Instance[index].shortcut = (KeyCode)x.newValue); bookmarkElement.Add(shortcutMask); Button moveUpButton = new Button(() => { if (index <= 0) { return; } var item = Bookmarks.Instance.viewpoints[index]; Bookmarks.Instance.viewpoints.RemoveAt(index); Bookmarks.Instance.viewpoints.Insert(index - 1, item); RefreshUI(); }) { name = "Up", text = "Up" }; moveUpButton.AddToClassList("svbm-cell-button"); bookmarkElement.Add(moveUpButton); Button moveDownButton = new Button(() => { if (index >= Bookmarks.Instance.viewpoints.Count - 1) { return; } var item = Bookmarks.Instance.viewpoints[index]; Bookmarks.Instance.viewpoints.RemoveAt(index); Bookmarks.Instance.viewpoints.Insert(index + 1, item); RefreshUI(); }) { name = "Down", text = "Dn" }; moveDownButton.AddToClassList("svbm-cell-button"); bookmarkElement.Add(moveDownButton); rootVisualElement.Add(bookmarkElement); } }