/// <summary> /// Draw the button and property to which it is attached /// </summary> public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Get the ButtonAttribute ButtonAttribute button = attribute as ButtonAttribute; if (button.groupHorizontal) // Horizontal layout (same line) { float buttonWidth = position.width * buttonSize; float propertyWidth = position.width - buttonWidth - buttonPadding; position.width = propertyWidth; EditorGUI.PropertyField(position, property); position.x += propertyWidth + buttonPadding; position.width = buttonWidth; if (GUI.Button(position, button.displayName)) { MonoBehaviour parent = property.serializedObject.targetObject as MonoBehaviour; button.Invoke(parent); } } else // Vertical layout (two lines) { position.height = (position.height - buttonPadding) / 2; if (GUI.Button(position, button.displayName)) { MonoBehaviour parent = property.serializedObject.targetObject as MonoBehaviour; button.Invoke(parent); } position.y += position.height + buttonPadding; EditorGUI.PropertyField(position, property); } }
/// <inheritdoc /> /// <summary> /// <para>Draws a button for the <see cref="ButtonAttribute"/>.</para> /// </summary> /// <param name="position">Rectangle on the screen to use for the property GUI.</param> /// <param name="property">The SerializedProperty to make the custom GUI for.</param> /// <param name="label">The label of this property.</param> public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // If the property type is not a boolean return. if (property.propertyType != SerializedPropertyType.Boolean) { EditorGUI.LabelField(position, label.text, "Use Button with a bool field."); return; } // Get the ButtonAttribute. ButtonAttribute buttonAttribute = ((ButtonAttribute)this.fieldInfo.GetCustomAttribute(typeof(ButtonAttribute))); // If the validation method exists Validate! if (!buttonAttribute.Validate()) { GUI.enabled = false; } // Draw the button. if (GUI.Button(position, label)) { // If the button is clicked call the method from the attribute. buttonAttribute.Callback(); } GUI.enabled = true; }
private static UIButton AddButton <T>(this UIHelperBase group, string text, ButtonAttribute attr) { return((UIButton)group.AddButton(text, () => { attr.Action().Invoke(); })); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ButtonAttribute attribute = (ButtonAttribute)this.attribute; if (!attribute.showOnPlay && Application.isPlaying) { return; } if (attribute.funcNames == null || attribute.funcNames.Length == 0) { position = EditorGUI.IndentedRect(position); EditorGUI.HelpBox(position, "[Button] funcNames is Not Set!", MessageType.Warning); return; } float width = position.width / attribute.funcNames.Length; position.width = width; for (int i = 0; i < attribute.funcNames.Length; i++) { string funcName = attribute.funcNames[i]; if (GUI.Button(position, funcName)) { CalledFunc(property.serializedObject.targetObject, funcName); } position.x += width; } }
private void Invoke(UnityEngine.Object target) { if (_attribute == null) { _attribute = (ButtonAttribute)attribute; } Type eventOwnerType = target.GetType(); string eventName = _attribute.MethodName; if (_eventMethodInfo == null) { _eventMethodInfo = eventOwnerType.GetMethod(eventName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); } if (_eventMethodInfo != null) { _eventMethodInfo.Invoke(target, null); } else { Debug.LogWarning(string.Format("InspectorButtonAttribute caused: Unable to find method {0} in {1}", eventName, eventOwnerType)); } }
private void ClickedbuttonCart(object o, EventArgs e) { RemoveChildrenInStocklayout(); StockChildrenDetailRowsRemove(); listButtonProduct = new List <Button>(); Button b = (Button)o; // DisplayAlert(b.ClassId.ToString(), "", "OK"); int valuLength = 0; if (Convert.ToInt32(b.ClassId) == 0) { valuLength = 15; } else { valuLength = 5; } for (int ctr = 0; ctr <= valuLength; ctr++) { Button button = new Button { Text = "Produc" + ctr, Style = ButtonAttribute.setButtonAttribProduct(), ClassId = ctr.ToString() }; button.Clicked += ClickedButtonProduct; ProductbyCartLayout.Children.Add(button); listButtonProduct.Add(button); } }
public CommandDetailViewBuilder(Command command) { _command = command; actionButtonCallback = OnActionButton; actionButtonIcon = "execute"; Node topNode = CreateCategory(_command.info.fullPath, "command"); AddCommandFields(_command, topNode); // Add custom button (if have) foreach (var methodInfo in _command.info.customButtonInfos) { Node methodParent = topNode; ButtonAttribute attribute = methodInfo.GetCustomAttribute <ButtonAttribute>(); if (!string.IsNullOrEmpty(attribute.category)) { methodParent = CreateCategories(attribute.category); } string icon = !string.IsNullOrEmpty(attribute.icon) ? attribute.icon : "action"; Node buttonNode = AddButton(methodInfo.Name.GetReadableName(), icon, OnCustomButtom, methodParent); buttonNode.data = methodInfo; } }
public static void Button(UnityEngine.Object target, MethodInfo methodInfo) { if (methodInfo.GetParameters().Length == 0) { ButtonAttribute buttonAttribute = (ButtonAttribute)methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0]; string buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? methodInfo.Name : buttonAttribute.Text; if (GUILayout.Button(buttonText)) { methodInfo.Invoke(target, null); // Set target object and scene dirty to serialize changes to disk EditorUtility.SetDirty(target); PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage(); if (stage != null) { // Prefab mode EditorSceneManager.MarkSceneDirty(stage.scene); } else { // Normal scene EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); } } } else { string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters"; HelpBox_Layout(warning, MessageType.Warning, context: target, logToConsole: true); } }
private void DrawAdditional(CyberAttribute cyberAttribute) { ButtonAttribute attribute = cyberAttribute as ButtonAttribute; var type = CyberEdit.Current.GetFinalTargetType(); DrawMethod(type.GetMethod(attribute.Method, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static), attribute); }
public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) { ButtonAttribute inspectorButtonAttribute = (ButtonAttribute)attribute; float buttonLength = position.width; Rect buttonRect = new Rect(position.x, position.y, buttonLength, position.height); GUI.skin.button.alignment = TextAnchor.MiddleLeft; if (GUI.Button(buttonRect, inspectorButtonAttribute.MethodName)) { System.Type eventOwnerType = prop.serializedObject.targetObject.GetType(); string eventName = inspectorButtonAttribute.MethodName; if (_eventMethodInfo == null) { _eventMethodInfo = eventOwnerType.GetMethod(eventName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); } if (_eventMethodInfo != null) { _eventMethodInfo.Invoke(prop.serializedObject.targetObject, null); } else { Debug.LogWarning(string.Format("InspectorButton: Unable to find method {0} in {1}", eventName, eventOwnerType)); } } }
public void Button_GetValue_ReturnsNameAsValue() { var systemUnderTest = new ButtonAttribute("ButtonName", "foo"); Assert.IsTrue(systemUnderTest.GetValue() == "ButtonName"); }
public override void DrawMethod(UnityEngine.Object target, MethodInfo methodInfo) { if (methodInfo.GetParameters().Length == 0) { ButtonAttribute buttonAttribute = (ButtonAttribute)methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0]; string buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? methodInfo.Name : buttonAttribute.Text; // Check are we selecting im project folder or in hierarchy window. var IsAssetSelection = Selection.activeTransform == null; var activeInPlayMode = EditorApplication.isPlaying && !buttonAttribute.activeInPlayMode; var activeInEditMode = !EditorApplication.isPlaying && !buttonAttribute.activeInEditMode; var enabled = activeInPlayMode ? false : activeInEditMode ? false : true; if (enabled) { if (!activeInEditMode && IsAssetSelection) // Disable button if we select an asset (like prefab). It does not matter if we are in playMode. { enabled = false; } } GUI.enabled = enabled; if (GUILayout.Button(buttonText)) { methodInfo.Invoke(target, null); } GUI.enabled = true; } else { string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target); } }
public override void OnGUI(Rect p_Position, SerializedProperty p_Property, GUIContent p_Label) { Rect _Position = p_Position; //EditorGUI.RectField (_Position,_Position); _Position.width -= 100; EditorGUI.PropertyField(_Position, p_Property); _Position.x += _Position.width; _Position.width = 100; ButtonAttribute _button = attribute as ButtonAttribute; if (_button.FunName == "") { if (p_Property.propertyType == SerializedPropertyType.String) { if (GUI.Button(_Position, p_Property.stringValue)) { Click(p_Property); } } else { EditorGUI.LabelField(p_Position, p_Label.text, "[Button] Only for String , you can use [Button(Funtion Name)]"); } } else { if (GUI.Button(_Position, _button.FunName)) { Click(p_Property, _button.FunName); } } }
public override void OnInspectorGUI() { // draw default stuff base.OnInspectorGUI(); if (null == m_targetType) { m_targetType = target.GetType(); } while (m_targetType != null) { // try find member function and static function :) MethodInfo[] methods = m_targetType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); foreach (var method in methods) { ButtonAttribute button = method.GetCustomAttribute <ButtonAttribute>(); if (button != null && method.GetParameters().Length > 0) { EditorGUILayout.HelpBox("ButtonAttribute: method cannot have parameters.", MessageType.Warning); } else if (button != null && GUILayout.Button(button.m_methodName)) { method.Invoke(target, new object[] { }); } } m_targetType = m_targetType.BaseType; } }
public void Button_GetOrderNotSetDefaultsToIntMaxValue_ReturnsDefaultOrderValue() { var systemUnderTest = new ButtonAttribute("ButtonName", "foo"); var order = systemUnderTest.Order; Assert.IsTrue(order == int.MaxValue); }
public void Button_GetTypeId_ReturnsTypeId() { var systemUnderTest = new ButtonAttribute("ButtonName", "foo"); systemUnderTest.SubmitType = "SubmitType"; Assert.IsNotNull(systemUnderTest.TypeId); }
public void Button_GetValueWithSubmitType_ReturnsSubmitTypeAsValue() { var systemUnderTest = new ButtonAttribute("ButtonName", "foo"); systemUnderTest.SubmitType = "SubmitType"; Assert.IsTrue(systemUnderTest.GetValue() == "SubmitType"); }
public void DrawButton(ButtonAttribute buttonAttribute, MethodInfo method) { var label = buttonAttribute.Label ?? method.Name; if (GUILayout.Button(label)) { method.Invoke(target, null); } }
public void DrawAfter(CyberAttrribute cyberAttributer) { if (CyberEdit.Current.CurrentInspectedMember != null) { ButtonAttribute attribute = cyberAttributer as ButtonAttribute; var type = CyberEdit.Current.GetFinalTargetType(); DrawMethod(type.GetMethod(attribute.Method, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static), attribute); } EditorGUILayout.EndHorizontal(); }
public void Button_GetOrderSetValue_ReturnsSetOrderValue() { var systemUnderTest = new ButtonAttribute("ButtonName", "foo"); systemUnderTest.Order = 1; var order = systemUnderTest.Order; Assert.IsTrue(order == 1); }
void DrawButtonAndInvokeMethod(ButtonAttribute attribute, MethodInfo methodInfo) { if (GUILayout.Button(attribute.ButtonName.Equals("") ? methodInfo.Name : attribute.ButtonName)) { foreach (var item in targets) { methodInfo.Invoke(item, null); } } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ButtonAttribute attribute = (ButtonAttribute)this.attribute; if (!attribute.showOnPlay && Application.isPlaying) { return(-2); } return(attribute.height); }
public void DrawBefore(CyberAttribute cyberAttributer) { EditorGUILayout.BeginHorizontal(); ButtonAttribute attribute = cyberAttributer as ButtonAttribute; if (attribute.After == false) { DrawAdditional(cyberAttributer); EditorGUILayout.EndHorizontal(); } }
/// <summary> /// Initializes this instance. /// </summary> protected override void Initialize() { this.expanded = false; this.buttonAttribute = this.Property.GetAttribute <ButtonAttribute>(); this.buttonHeight = this.Property.Context.GetGlobal("ButtonHeight", 0).Value; this.style = this.Property.Context.GetGlobal("ButtonStyle", (GUIStyle)null).Value; this.toggle = this.GetPersistentValue <bool>("toggle", false); this.hasGUIColorAttribute = this.Property.GetAttribute <GUIColorAttribute>() != null; this.drawParameters = this.Property.Children.Count > 0 && !DontDrawMethodParamaters; this.name = this.Property.NiceName; this.label = new GUIContent(name); if (buttonAttribute != null) { this.btnStyle = this.buttonAttribute.Style; this.expanded = buttonAttribute.Expanded; if (!string.IsNullOrEmpty(this.buttonAttribute.Name)) { this.mh = new StringMemberHelper(this.Property, this.buttonAttribute.Name); } if (this.buttonHeight == 0 && buttonAttribute.ButtonHeight > 0) { this.buttonHeight = buttonAttribute.ButtonHeight; } } if (this.style == null) { if (this.buttonHeight > 20) { this.style = SirenixGUIStyles.Button; } else { this.style = EditorStyles.miniButton; } } if (this.drawParameters && this.btnStyle == ButtonStyle.FoldoutButton && !this.expanded) { if (this.buttonHeight > 20) { this.style = SirenixGUIStyles.ButtonLeft; this.toggleBtnStyle = SirenixGUIStyles.ButtonRight; } else { this.style = EditorStyles.miniButtonLeft; this.toggleBtnStyle = EditorStyles.miniButtonRight; } } }
public void ButtonTest3() { var button = new ButtonAttribute("Name", "foo"); var actual = html.Button(button); Assert.IsNotNull(actual); var s = actual.ToHtmlString(); //<button class="name button" name="submitType" type="submit" value="Name">Name</button> Assert.IsTrue(s.Contains("button")); Assert.IsTrue(s.Contains("type=\"submit\"")); }
public void ButtonRolesTest_Invalid() { mockUserService.Setup(m => m.IsInRole(It.IsAny <IEnumerable <string> >())).Returns(false); var button = new ButtonAttribute("Name", "foo") { Roles = new[] { "FOO" } }; var result = html.Button(button); Assert.IsNotNull(result); Assert.IsTrue(string.IsNullOrEmpty(result.ToString())); }
/// <summary> /// Get the height of the whole button + property group /// </summary> public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ButtonAttribute button = attribute as ButtonAttribute; if (button.groupHorizontal) { return(base.GetPropertyHeight(property, label)); } else { return(base.GetPropertyHeight(property, label) * 2 + buttonPadding); } }
internal static Button Create(MethodInfo method, ButtonAttribute buttonAttribute) { var parameters = method.GetParameters(); if (parameters.Length == 0) { return(new ButtonWithoutParams(method, buttonAttribute)); } else { return(new ButtonWithParams(method, buttonAttribute, parameters)); } }
public void DrawMethod(MethodInfo method, CyberAttrribute cyberAttrribute) { object locker = new object(); if (method == null) { throw new ArgumentNullException(nameof(method)); } if (cyberAttrribute == null) { throw new ArgumentNullException(nameof(method)); } ButtonAttribute button = cyberAttrribute as ButtonAttribute; TheEditor.PrepareToRefuseGui(locker); if (button.CustomColor) { GUI.color = button.CurColor; } if ((Application.isPlaying == false && button.WhenCanPress == UnityEventCallState.RuntimeOnly) || (button.WhenCanPress == UnityEventCallState.Off)) { GUI.enabled = false; } string text = button.Text; if (string.IsNullOrEmpty(text)) { text = method.Name; } if (GUILayout.Button(text, GUILayout.Height(button.Height))) { if (method.IsStatic == false) { method.Invoke(CyberEdit.Current.Target, null); } else { method.Invoke(null, null); } } TheEditor.RefuseGui(locker); }
public override bool DrawMethod(UnityEngine.Object target, MethodInfo methodInfo) { if (methodInfo.GetParameters().Length == 0) { ButtonAttribute buttonAttribute = (ButtonAttribute)methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0]; string buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? methodInfo.Name : buttonAttribute.Text; if (GUILayout.Button(buttonText)) { EditorGUI.BeginChangeCheck(); var gameObjects = Selection.gameObjects; if (gameObjects.Length <= 1 || buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.Default) { methodInfo.Invoke(target, null); } else if (target is Component) { var targetType = target.GetType(); if (buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.First) { var gameObject = Selection.gameObjects[0]; var targetComponent = gameObject.GetComponent(targetType); methodInfo.Invoke(targetComponent, null); } else if (buttonAttribute.Target == ButtonAttribute.ButtonInvocationTarget.All) { foreach (var gameObject in Selection.gameObjects) { var targetComponent = gameObject.GetComponent(targetType); methodInfo.Invoke(targetComponent, null); } } } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Method Invocation: " + buttonText); } return(true); } } else { string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target); } return(false); }