Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
feat: add help buttons to conditions and behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
JulieAtInnoactive authored and tomwim committed Feb 19, 2021
1 parent 3cd02d8 commit a80ca83
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 10 deletions.
22 changes: 21 additions & 1 deletion .Documentation/articles/developer/05-behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ public class ScalingBehaviorData : IBehaviorData
}
```

Each behavior or condition of our base template has a help button in their header. The button is linked to a webpage.
If you would like to add your own link, add a `[HelpLink]` attribute above the behavior class.

[![Help Button](../images/developer/help-link.png)](../images/developer/help-link.png "The HelpLink attribute allows to insert guidance for your training designers.")


```csharp
namespace Innoactive.Creator.BaseTemplate.Behaviors
{
[DataContract(IsReference = true)]
[HelpLink("http://my-documentation.org/scale-object.html")]
public class ScalingBehavior: Behavior<ScalingBehavior.EntityData>
{
//Your Behavior code as shown above
}
}

```


## Stage Process

Now we need to define the process of the scaling behavior. It will read and modify the data as it will take the target and apply a new scale to it over a given duration in seconds.
Expand Down Expand Up @@ -332,4 +352,4 @@ public class ScalingBehavior : Behavior<ScalingBehavior.EntityData>
}
```

[To the next chapter!](06-menu-items.md)
[To the next chapter!](06-menu-items.md)
3 changes: 3 additions & 0 deletions .Documentation/articles/images/developer/help-link.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Editor/Resources/icon_help_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Editor/Resources/icon_help_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Editor/UI/Drawers/BehaviorInstantiatiorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public override Rect Draw(Rect rect, object currentValue, Action<object> changeV
TestableEditorElements.DisplayContextMenu(options);
}
EditorGUI.EndDisabledGroup();

if (EditorDrawingHelper.DrawHelpButton(ref rect))
{
Application.OpenURL("https://developers.innoactive.de/documentation/creator/latest/articles/innoactive-creator/default-behaviors.html");
}

if (EditorConfigurator.Instance.AllowedMenuItemsSettings.GetBehaviorMenuOptions().Any() == false)
{
Expand Down
4 changes: 4 additions & 0 deletions Editor/UI/Drawers/ConditionInstantiatorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public override Rect Draw(Rect rect, object currentValue, Action<object> changeV
}
EditorGUI.EndDisabledGroup();

if (EditorDrawingHelper.DrawHelpButton(ref rect))
{
Application.OpenURL("https://developers.innoactive.de/documentation/creator/latest/articles/innoactive-creator/default-conditions.html");
}
if (EditorConfigurator.Instance.AllowedMenuItemsSettings.GetConditionMenuOptions().Any() == false)
{
rect.y += rect.height + EditorDrawingHelper.VerticalSpacing;
Expand Down
36 changes: 31 additions & 5 deletions Editor/UI/Drawers/MetadataWrapperDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@ internal class MetadataWrapperDrawer : AbstractDrawer
private readonly string keepPopulatedName = typeof(KeepPopulatedAttribute).FullName;
private readonly string reorderableListOfName = typeof(ReorderableListOfAttribute).FullName;
private readonly string listOfName = typeof(ListOfAttribute).FullName;

private readonly string showHelpName = typeof(HelpAttribute).FullName;
private static readonly EditorIcon deleteIcon = new EditorIcon("icon_delete");
private static readonly EditorIcon arrowUpIcon = new EditorIcon("icon_arrow_up");
private static readonly EditorIcon arrowDownIcon = new EditorIcon("icon_arrow_down");
private static readonly EditorIcon helpIcon = new EditorIcon("icon_help");


/// <inheritdoc />
public override Rect Draw(Rect rect, object currentValue, Action<object> changeValueCallback, GUIContent label)
{
MetadataWrapper wrapper = (MetadataWrapper)currentValue;
// If the drawn object is a ITransition, IBehavior or ICondition the list object will be part of a header.
bool isPartOfHeader = wrapper.ValueDeclaredType == typeof(ITransition) || wrapper.ValueDeclaredType == typeof(IBehavior) || wrapper.ValueDeclaredType == typeof(ICondition);

if (wrapper.Metadata.ContainsKey(showHelpName))
{
return DrawHelp(rect, wrapper, changeValueCallback, label, isPartOfHeader);
}
if (wrapper.Metadata.ContainsKey(reorderableName))
{
return DrawReorderable(rect, wrapper, changeValueCallback, label, isPartOfHeader);
Expand Down Expand Up @@ -133,16 +139,35 @@ private GUIStyle GetStyle(bool isPartOfHeader = false)
return style;
}

private Rect DrawHelp(Rect rect, MetadataWrapper wrapper, Action<object> changeValueCallback, GUIContent label, bool isPartOfHeader)
{
rect = DrawRecursively(rect, wrapper, showHelpName, changeValueCallback, label);
Vector2 buttonSize = new Vector2(EditorGUIUtility.singleLineHeight + 3f, EditorDrawingHelper.SingleLineHeight);
GUIStyle style = GetStyle(isPartOfHeader);
if(wrapper.Value != null && wrapper.Value.GetType() != null)
{
HelpLinkAttribute helpLinkAttribute = wrapper.Value.GetType().GetCustomAttribute(typeof(HelpLinkAttribute)) as HelpLinkAttribute;
if (helpLinkAttribute != null)
{
if (GUI.Button(new Rect(rect.x + rect.width - buttonSize.x * 4 - 0.1f, rect.y + 1, buttonSize.x, buttonSize.y), helpIcon.Texture, style))
{
Application.OpenURL(helpLinkAttribute.HelpLink);
}
}
}
return rect;
}

private Rect DrawReorderable(Rect rect, MetadataWrapper wrapper, Action<object> changeValueCallback, GUIContent label, bool isPartOfHeader)
{
rect = DrawRecursively(rect, wrapper, reorderableName, changeValueCallback, label);

Vector2 buttonSize = new Vector2(EditorGUIUtility.singleLineHeight + 3f, EditorDrawingHelper.SingleLineHeight);

GUIStyle style = GetStyle(isPartOfHeader);

GUI.enabled = ((ReorderableElementMetadata)wrapper.Metadata[reorderableName]).IsLast == false;
if (GUI.Button(new Rect(rect.x + rect.width - buttonSize.x * 2 - 0.1f, rect.y + 1, buttonSize.x, buttonSize.y), arrowDownIcon.Texture, style))
if (GUI.Button(new Rect(rect.x + rect.width - buttonSize.x * 2, rect.y + 1, buttonSize.x, buttonSize.y), arrowDownIcon.Texture, style))
{
object oldValue = wrapper.Value;
ChangeValue(() =>
Expand All @@ -160,7 +185,8 @@ private Rect DrawReorderable(Rect rect, MetadataWrapper wrapper, Action<object>
}

GUI.enabled = ((ReorderableElementMetadata)wrapper.Metadata[reorderableName]).IsFirst == false;
if (GUI.Button(new Rect(rect.x + rect.width - buttonSize.x * 3 - 0.1f, rect.y + 1, buttonSize.x, buttonSize.y), arrowUpIcon.Texture, style))

if (GUI.Button(new Rect(rect.x + rect.width - buttonSize.x * 3, rect.y + 1, buttonSize.x, buttonSize.y), arrowUpIcon.Texture, style))
{
object oldValue = wrapper.Value;
ChangeValue(() =>
Expand Down
34 changes: 32 additions & 2 deletions Editor/UI/EditorDrawingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Expand All @@ -10,6 +10,7 @@ namespace Innoactive.CreatorEditor.UI
internal static class EditorDrawingHelper
{
private static readonly Vector2 addComponentButtonSize = new Vector2(228, 22);
private static readonly Vector2 addHelpButtonSize = new Vector2(20, 22);
private static readonly int StepTransitionStrokeSize = 3;
/// <summary>
/// Default spacing between Step Inspector elements.
Expand All @@ -32,6 +33,7 @@ public static float SingleLineHeight
}
}

private static readonly EditorIcon helpIcon = new EditorIcon("icon_help");
/// <summary>
/// Height of slightly bigger line in the Step Inspector.
/// </summary>
Expand All @@ -43,17 +45,28 @@ public static float HeaderLineHeight
}
}


/// <summary>
/// Draw button which is similar to default "Add Component" Unity button.
/// </summary>
public static bool DrawAddButton(ref Rect rect, string label)
public static Rect CalculateAddButtonRect(ref Rect rect)
{
rect.height = SingleLineHeight + addComponentButtonSize.y;

Rect buttonRect = rect;
buttonRect.size = addComponentButtonSize;
buttonRect.x = rect.x + (rect.width - buttonRect.width) / 2f;
buttonRect.y = rect.y + (rect.height - buttonRect.height) / 2f;
return buttonRect;
}


/// <summary>
/// Draw button which is similar to default "Add Component" Unity button.
/// </summary>
public static bool DrawAddButton(ref Rect rect, string label)
{
Rect buttonRect = EditorDrawingHelper.CalculateAddButtonRect(ref rect);

GUIStyle style = new GUIStyle(GUI.skin.button)
{
Expand All @@ -64,6 +77,23 @@ public static bool DrawAddButton(ref Rect rect, string label)
return GUI.Button(buttonRect, new GUIContent(label), style);
}

/// <summary>
/// Draw a help button next to the 'Add Behavior' / 'Add Condition' button.
/// </summary>
public static bool DrawHelpButton(ref Rect rect)
{
Rect addbuttonRect = EditorDrawingHelper.CalculateAddButtonRect(ref rect);
Rect helpbuttonRect = addbuttonRect;
helpbuttonRect.size = addHelpButtonSize;
helpbuttonRect.x = addbuttonRect.x + addbuttonRect.width + 5;

GUIStyle style = new GUIStyle(GUI.skin.button)
{
fontSize = 12
};

return GUI.Button(helpbuttonRect, helpIcon.Texture, style);
}
/// <summary>
/// Draw a circle.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions Runtime/Attributes/HelpAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Reflection;

namespace Innoactive.Creator.Core.Attributes
{
/// <summary>
/// Declares that "Help" button has to be drawn.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class HelpAttribute : MetadataAttribute
{
/// <inheritdoc />
public override object GetDefaultMetadata(MemberInfo owner)
{
return null;
}

/// <inheritdoc />
public override bool IsMetadataValid(object metadata)
{
return metadata == null;
}
}
}
21 changes: 21 additions & 0 deletions Runtime/Attributes/HelpLinkAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Innoactive.Creator.Core.Attributes
{
/// <summary>
/// Adds a link to a documentation that explains a behavior or condition.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Class)]
public class HelpLinkAttribute : Attribute
{
/// <summary>
/// An HTML link to the documentation explaining the behavior or condition.
/// </summary>
public string HelpLink { get; private set; }

public HelpLinkAttribute(string helpLink)
{
HelpLink = helpLink;
}
}
}
2 changes: 1 addition & 1 deletion Runtime/BehaviorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EntityData : EntityCollectionData<IBehavior>, IBehaviorCollectionDa
/// List of all <see cref="IBehavior"/>s added.
/// </summary>
[DataMember]
[DisplayName(""), ReorderableListOf(typeof(FoldableAttribute), typeof(DeletableAttribute), typeof(DrawIsBlockingToggleAttribute)), ExtendableList]
[DisplayName(""), ReorderableListOf(typeof(FoldableAttribute), typeof(DeletableAttribute), typeof(DrawIsBlockingToggleAttribute), typeof(HelpAttribute)), ExtendableList]
public virtual IList<IBehavior> Behaviors { get; set; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Transition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class EntityData : EntityCollectionData<ICondition>, ITransitionData
{
///<inheritdoc />
[DataMember]
[DisplayName("Conditions"), Foldable, ReorderableListOf(typeof(FoldableAttribute), typeof(DeletableAttribute)), ExtendableList]
[DisplayName("Conditions"), Foldable, ReorderableListOf(typeof(FoldableAttribute), typeof(DeletableAttribute), typeof(HelpAttribute)), ExtendableList]
public IList<ICondition> Conditions { get; set; }

///<inheritdoc />
Expand Down

0 comments on commit a80ca83

Please sign in to comment.