playMaker

Author Topic: Custom Actions Dropdown Selections  (Read 5645 times)

izzycoding

  • Playmaker Newbie
  • *
  • Posts: 29
Custom Actions Dropdown Selections
« on: May 15, 2013, 03:37:38 AM »
Hi,

I have started to create a few custom actions for one of the plugins I use.
I am wondering if it is possible to create a dropdown list in my custom action that I can populate from a string array that I get from my plugin?

Code: [Select]
using UnityEngine;
using MyPlugin;

namespace MyPlugin.PlayMaker.Actions {
[ActionCategory("Plugin")]
[Tooltip("This will raise an event.")]
public class SendPluginEvent : FsmStateAction {
[RequiredField]
[Tooltip("The event name to send.")]
[Options(EventManager.Instance._events)]
public FsmString eventName;

public override void Reset() {
eventName = "";
}

public override void OnEnter() {
EventManager.Instance.PostEvent(eventName.Value);
Finish();
}
}
}

is there anything like this available?
if not is there another way I can do this? what I want is to be able to provide a dropdown for the possible options, but I still want to have the ability to allow users to use an Fsm Variable instead of the dropdown.

Regards,
« Last Edit: May 15, 2013, 03:39:30 AM by izzycoding »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Custom Actions Dropdown Selections
« Reply #1 on: May 15, 2013, 05:42:59 AM »
Hi,

 Tricky at the moment. I whish I had this ability too, especially with enums.

 **BUT**, now we have custom editor in beta! and that's huge! so you will be able to create custom action AND custom editors, so you wil be able to do that soon.

Contact Alex if you want to join the beta if you are not in there already.

bye,

 Jean

TrentSterling

  • Junior Playmaker
  • **
  • Posts: 89
  • Someday I'll make games!
    • My Blog
Re: Custom Actions Dropdown Selections
« Reply #2 on: May 20, 2014, 09:31:55 PM »
Super necro: Is this a possibility yet? I've been playing with the 2dtoolkit actions lately, and I would really like to load a list of sprite names from the atlas array.



Kinda sucks to enter sprite names manually :(
« Last Edit: May 20, 2014, 09:34:16 PM by TrentSterling »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Custom Actions Dropdown Selections
« Reply #3 on: July 16, 2014, 07:54:37 AM »
Hi,

 Yeah, I know. I looked at this when I started creating the first actions for that, but gave up, I think it's doable, but would be quite a lot of work.

Bye,

 Jean

Celtc

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Custom Actions Dropdown Selections
« Reply #4 on: October 25, 2015, 01:21:50 PM »
I've been working on this lately, try this action:

Tk2dPlayAnimationExtended.cs
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using System.Collections;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("2D Toolkit/SpriteAnimator")]
[Tooltip("Plays a sprite animation. \nNOTE: The Game Object must have a tk2dSpriteAnimator attached.")]
public class Tk2dPlayAnimationExtended : FsmStateAction
{
[RequiredField]
[Tooltip("The Game Object to work with. NOTE: The Game Object must have a tk2dSpriteAnimator component attached.")]
[CheckForComponent(typeof(tk2dSpriteAnimator))]
public FsmOwnerDefault gameObject;

        [Tooltip("Wait until the animation is finished. If the action is entered again before this animation has ended, it will not interrupt the previous one.")]
        public FsmBool waitForEnd;

        [Tooltip("Plays the animation that was originally playing before entering this action.")]
        public FsmBool leaveDefaultAnim;


        public int clipId;
        public tk2dSpriteAnimation library;

private tk2dSpriteAnimator _sprite;
        private tk2dSpriteAnimationClip _clip;

private void _getSprite()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

_sprite =  go.GetComponent<tk2dSpriteAnimator>();
}

        private void _getClip()
        {
            _clip = library.GetClipById(clipId);
        }

public override void Reset()
{
gameObject = null;
            waitForEnd = null;
            leaveDefaultAnim = null;

            clipId = 0;
            library = null;

            _sprite = null;
            _clip = null;
}

public override void OnEnter()
{
_getSprite();
            _getClip();

            DoPlayAnimation();

            if (!waitForEnd.Value)
                Finish();
}

        public override void OnUpdate()
        {
            DoCheckPlaying();
        }

void DoPlayAnimation()
{
if (_sprite == null)
{
LogWarning("Missing tk2dSpriteAnimator component");
                Finish();
return;
}

            if (_sprite.Library != library)
                _sprite.Library = library;

            else if (_sprite.IsPlaying(_clip) && !waitForEnd.Value)
                _sprite.StopAndResetFrame();

            _sprite.Play(_clip);

            if (leaveDefaultAnim.Value)
                _sprite.AnimationCompleted = (sprite, clip) => sprite.Play(sprite.DefaultClip);

            return;
}
       
        void DoCheckPlaying()
        {
            if (!_sprite.IsPlaying(_clip))
                Finish();

            return;
        }
}
}

And the editor, Tk2dPlayAnimationExtendedEditor.cs:
Code: [Select]
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMakerEditor;
using System.Collections.Generic;


[CustomActionEditor(typeof(Tk2dPlayAnimationExtended))]
public class Tk2dPlayAnimationExtendedEditor : CustomActionEditor
{
    Tk2dPlayAnimationExtended action;

    string[] animLibNames = null;
    tk2dGenericIndexItem[] animLibs = null;

    GameObject _owner = null;

    // Get the owner
    GameObject GetOwner()
    {
        GameObject owner = null;
        if (action.Fsm != null)
            owner = action.Fsm.GetOwnerDefaultTarget(action.gameObject);

        return owner;
    }

    // Get the sprite of the go
    tk2dSpriteAnimator GetSprite(GameObject go)
    {
        tk2dSpriteAnimator sprite = null;
        if (go != null)
            sprite = go.GetComponent<tk2dSpriteAnimator>();

        return sprite;
    }

    // Assign to the action a default lib an clip
    void SetDefaultLibrary()
    {
        action.library = animLibs[0].GetAsset<tk2dSpriteAnimation>();
        action.clipId = action.library.GetClipIdByName(action.library.clips[0].name);
    }

    void SetSpritesLibrary(tk2dSpriteAnimator sprite)
    {
        action.library = sprite.Library;
        action.clipId = sprite.DefaultClipId;
    }

    // Init
    public override void OnEnable()
    {
        // For referencing the action directly:
        action = target as Tk2dPlayAnimationExtended;

        animLibs = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations();
        if (animLibs != null)
        {
            animLibNames = new string[animLibs.Length];
            for (int i = 0; i < animLibs.Length; ++i)
            {
                animLibNames[i] = animLibs[i].AssetName;
            }
        }

        //if (action.library == null)
        //    SetDefaultLibrary();
    }

    public override bool OnGUI()
    {
        // Default inspector for gameObject
        EditField("gameObject");

        // Check for Owner change
        var currOwner = GetOwner();
        if (currOwner != _owner && currOwner != null)
        {
            // If new owner and the new is replacing an old one, import the animation
            if (_owner != null)
            {
                var sprite = GetSprite(currOwner);
                if (sprite != null)
                {
                    action.library = sprite.Library;
                    action.clipId = sprite.DefaultClipId;
                }
            }
            _owner = currOwner;
        }

        // Check for library
        if (action.library == null)
            SetDefaultLibrary();

        #region Library
        // Get selected library
        int selAnimLib = 0;
        string selectedGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(action.library));
        for (int i = 0; i < animLibs.Length; ++i)
        {
            if (animLibs[i].assetGUID == selectedGUID)
            {
                selAnimLib = i;
                break;
            }
        }

        // Display libraries
        int newAnimLib = EditorGUILayout.Popup("Anim Lib", selAnimLib, animLibNames);
        if (newAnimLib != selAnimLib)
        {
            action.library = animLibs[newAnimLib].GetAsset<tk2dSpriteAnimation>();
            action.clipId = 0;
        }
        #endregion

        #region Clip
        List<string> clipNames = new List<string>(action.library.clips.Length);
        List<int> clipIds = new List<int>(action.library.clips.Length);

        // Fill names (with ids if necessary)
        for (int i = 0; i < action.library.clips.Length; ++i)
{
            if (action.library.clips[i].name != null && action.library.clips[i].name.Length > 0)
            {
                string name = action.library.clips[i].name;
if (tk2dPreferences.inst.showIds) {
name += "\t[" + i.ToString() + "]";
}
clipNames.Add( name );
clipIds.Add( i );
}
}

        // Show options
        int newClipId = EditorGUILayout.IntPopup("Clip", action.clipId, clipNames.ToArray(), clipIds.ToArray());
        if (newClipId != action.clipId)
        {
            action.clipId = newClipId;
        }
        #endregion

        // Default inspector for other fields
        EditField("waitForEnd");
        EditField("leaveDefaultAnim");

        return GUI.changed;
    }
}
« Last Edit: October 27, 2015, 04:48:59 PM by Celtc »

acknickulous

  • Playmaker Newbie
  • *
  • Posts: 20
Re: Custom Actions Dropdown Selections
« Reply #5 on: September 07, 2020, 06:50:37 PM »
Can we get some better documentation for the CustomActionEditor? The actual documentation page for it doesn't really explain how to use it in the actual action

https://hutonggames.fogbugz.com/default.asp?W1104

What is DebugFloat? Is this some kind of custom data type that subsclasses the FSMVar or something?


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Custom Actions Dropdown Selections
« Reply #6 on: September 08, 2020, 09:02:22 AM »
Hi.
The Playmaker API might be helpful