void AddField(Vector4 initialValue, int index, string subLabel) { var dummy = new VisualElement { name = "dummy" }; var label = new Label(subLabel); dummy.Add(label); Add(dummy); var field = new FloatField { userData = index, value = initialValue[index] }; var dragger = new FieldMouseDragger <double>(field); dragger.SetDragZone(label); field.Q("unity-text-input").RegisterCallback <KeyDownEvent>(evt => { // Record Undo for input field edit if (m_UndoGroup == -1) { m_UndoGroup = Undo.GetCurrentGroup(); m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); } // Handle scaping input field edit if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) { Undo.RevertAllDownToGroup(m_UndoGroup); m_UndoGroup = -1; evt.StopPropagation(); } // Dont record Undo again until input field is unfocused m_UndoGroup++; this.MarkDirtyRepaint(); }); // Called after KeyDownEvent field.RegisterValueChangedCallback(evt => { // Only true when setting value via FieldMouseDragger // Undo recorded once per dragger release if (m_UndoGroup == -1) { m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); } var value = m_Get(); if (value[index] != (float)evt.newValue) { value[index] = (float)evt.newValue; m_Set(value); m_Node.Dirty(ModificationScope.Node); } }); // Reset UndoGroup when done editing input field & update title field.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt => { m_Node.owner.owner.isDirty = true; m_UndoGroup = -1; }); Add(field); }
void AddField(Vector4 initialValue, int index, string subLabel) { var dummy = new VisualElement { name = "dummy" }; var label = new Label(subLabel); dummy.Add(label); Add(dummy); var field = new FloatField { userData = index, value = initialValue[index] }; var dragger = new FieldMouseDragger <double>(field); dragger.SetDragZone(label); field.RegisterValueChangedCallback(evt => { var value = m_Get(); value[index] = (float)evt.newValue; m_Set(value); m_Node.Dirty(ModificationScope.Node); m_UndoGroup = -1; }); field.Q("unity-text-input").RegisterCallback <InputEvent>(evt => { if (m_UndoGroup == -1) { m_UndoGroup = Undo.GetCurrentGroup(); m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); } float newValue; if (!float.TryParse(evt.newData, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out newValue)) { newValue = 0f; } var value = m_Get(); if (Math.Abs(value[index] - newValue) > 1e-9) { value[index] = newValue; m_Set(value); m_Node.Dirty(ModificationScope.Node); } }); field.Q("unity-text-input").RegisterCallback <KeyDownEvent>(evt => { if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) { Undo.RevertAllDownToGroup(m_UndoGroup); m_UndoGroup = -1; evt.StopPropagation(); } this.MarkDirtyRepaint(); }); Add(field); }
FloatField AddField(VisualElement panel, Slider slider, int index, DielectricSpecularNode.DielectricMaterial initMaterial) { float initValue; if (index == 1) { initValue = initMaterial.indexOfRefraction; } else { initValue = initMaterial.range; } var field = new FloatField { userData = index, value = initValue }; field.RegisterCallback <MouseDownEvent>(Repaint); field.RegisterCallback <MouseMoveEvent>(Repaint); field.RegisterValueChangedCallback(evt => { var fieldValue = (float)evt.newValue; if (index == 1) { m_DielectricMaterial.indexOfRefraction = fieldValue; } else { m_DielectricMaterial.range = fieldValue; } m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null); this.MarkDirtyRepaint(); }); field.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt => { if (index == 1) { RedrawIORControls(m_DielectricMaterial.indexOfRefraction); } else { RedrawRangeControls(m_DielectricMaterial.range); } this.MarkDirtyRepaint(); }); panel.Add(field); return(field); }
FloatField AddMinMaxField(VisualElement panel, string label, int index) { var floatField = new FloatField { value = m_Value[index] }; if (label != null) { var labelField = new Label(label); panel.Add(labelField); } floatField.RegisterValueChangedCallback(evt => { m_Value[index] = (float)evt.newValue; m_PropertyInfo.SetValue(m_Node, m_Value, null); this.MarkDirtyRepaint(); }); floatField.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt => { if (index == 1) { m_Value[index] = Mathf.Min(m_Value[index], m_Value.z); m_MinField.value = m_Value[index]; } else { m_Value[index] = Mathf.Max(m_Value[index], m_Value.y); m_MaxField.value = m_Value[index]; } float newValue = Mathf.Max(Mathf.Min(m_Value.x, m_Value.z), m_Value.y); m_Value.x = newValue; m_SliderInput.value = newValue; UpdateSlider(); m_PropertyInfo.SetValue(m_Node, m_Value, null); this.MarkDirtyRepaint(); }); panel.Add(floatField); return(floatField); }
FloatField AddField(VisualElement panel, string label, int index, Vector3 initValiue) { var field = new FloatField { userData = index, value = initValiue[index] }; if (label != "") { var l = new Label(label); panel.Add(l); var dragger = new FieldMouseDragger <double>(field); dragger.SetDragZone(l); } field.RegisterCallback <MouseDownEvent>(Repaint); field.RegisterCallback <MouseMoveEvent>(Repaint); field.RegisterValueChangedCallback(evt => { var value = (Vector3)m_PropertyInfo.GetValue(m_Node, null); value[index] = (float)evt.newValue; m_PropertyInfo.SetValue(m_Node, value, null); m_UndoGroup = -1; this.MarkDirtyRepaint(); }); field.Q("unity-text-input").RegisterCallback <InputEvent>(evt => { if (m_UndoGroup == -1) { m_UndoGroup = Undo.GetCurrentGroup(); m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); } float newValue; if (!float.TryParse(evt.newData, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out newValue)) { newValue = 0f; } var value = (Vector3)m_PropertyInfo.GetValue(m_Node, null); value[index] = newValue; m_PropertyInfo.SetValue(m_Node, value, null); if (evt.newData.Length != 0 && evt.newData[evt.newData.Length - 1] != '.' && evt.newData[evt.newData.Length - 1] != ',') { UpdateSlider(m_SliderPanel, index, value); } this.MarkDirtyRepaint(); }); field.Q("unity-text-input").RegisterCallback <KeyDownEvent>(evt => { if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) { Undo.RevertAllDownToGroup(m_UndoGroup); m_UndoGroup = -1; m_Value = (Vector3)m_PropertyInfo.GetValue(m_Node, null); UpdateSlider(m_SliderPanel, index, m_Value); evt.StopPropagation(); } this.MarkDirtyRepaint(); }); panel.Add(field); return(field); }
public SliderControlView(string label, bool displayMinMax, AbstractMaterialNode node, PropertyInfo propertyInfo) { m_Node = node; m_PropertyInfo = propertyInfo; styleSheets.Add(Resources.Load <StyleSheet>("Styles/Controls/SliderControlView")); m_DisplayMinMax = displayMinMax; if (propertyInfo.PropertyType != typeof(Vector3)) { throw new ArgumentException("Property must be of type Vector3.", "propertyInfo"); } new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name)); m_Value = (Vector3)m_PropertyInfo.GetValue(m_Node, null); m_Slider = new Slider(m_Value.y, m_Value.z) { value = m_Value.x }; m_Slider.RegisterValueChangedCallback((evt) => OnChangeSlider(evt.newValue)); m_SliderInput = new FloatField { value = m_Value.x }; m_SliderInput.RegisterValueChangedCallback(evt => { var value = (float)evt.newValue; m_Value.x = value; m_PropertyInfo.SetValue(m_Node, m_Value, null); this.MarkDirtyRepaint(); }); m_SliderInput.Q("unity-text-input").RegisterCallback <FocusOutEvent>(evt => { float minValue = Mathf.Min(m_Value.x, m_Value.y); float maxValue = Mathf.Max(m_Value.x, m_Value.z); m_Value = new Vector3(m_Value.x, minValue, maxValue); m_MinField.value = minValue; m_MaxField.value = maxValue; UpdateSlider(); m_PropertyInfo.SetValue(m_Node, m_Value, null); this.MarkDirtyRepaint(); }); m_SliderPanel = new VisualElement { name = "SliderPanel" }; if (!string.IsNullOrEmpty(label)) { m_SliderPanel.Add(new Label(label)); } m_SliderPanel.Add(m_Slider); m_SliderPanel.Add(m_SliderInput); Add(m_SliderPanel); if (m_DisplayMinMax) { var fieldsPanel = new VisualElement { name = "FieldsPanel" }; m_MinField = AddMinMaxField(fieldsPanel, "Min", 1); m_MaxField = AddMinMaxField(fieldsPanel, "Max", 2); Add(fieldsPanel); } }
FloatField AddField(VisualElement panel, Slider slider, int index, DielectricSpecularNode.DielectricMaterial initMaterial) { float initValue; if (index == 1) { initValue = initMaterial.indexOfRefraction; } else { initValue = initMaterial.range; } var field = new FloatField { userData = index, value = initValue }; field.RegisterCallback <MouseDownEvent>(Repaint); field.RegisterCallback <MouseMoveEvent>(Repaint); field.RegisterValueChangedCallback(evt => { var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); var fieldValue = (float)evt.newValue; if (index == 1) { value.indexOfRefraction = fieldValue; RedrawIORControls(fieldValue); } else { value.range = fieldValue; RedrawRangeControls(fieldValue); } m_PropertyInfo.SetValue(m_Node, value, null); m_UndoGroup = -1; this.MarkDirtyRepaint(); }); field.Q("unity-text-input").RegisterCallback <InputEvent>(evt => { if (m_UndoGroup == -1) { m_UndoGroup = Undo.GetCurrentGroup(); m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); } float newValue; if (!float.TryParse(evt.newData, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out newValue)) { newValue = 0f; } var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); if (index == 1) { value.indexOfRefraction = newValue; } else { value.range = newValue; } m_PropertyInfo.SetValue(m_Node, value, null); this.MarkDirtyRepaint(); }); field.Q("unity-text-input").RegisterCallback <KeyDownEvent>(evt => { if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) { Undo.RevertAllDownToGroup(m_UndoGroup); m_UndoGroup = -1; var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); if (index == 1) { RedrawIORControls(value.indexOfRefraction); } else { RedrawRangeControls(value.range); } evt.StopPropagation(); } this.MarkDirtyRepaint(); }); panel.Add(field); return(field); }