Jump to content
Aveyond Studios Community
rujash

HeroKit Listener UI - OnSubmit

Recommended Posts

I implemented myself.

Duplicate UI Listener folder - rename to UI Listener 2D. Rename scripts to have '2D' at end of name.

image.png.1e5bbf4d5f0079baa139bf8644e7031f.png

Replace HeroKitListenerInspector2D code with:

// --------------------------------------------------------------
// Copyright (c) 2016-2017 Aveyond Studios. 
// All Rights Reserved.
// --------------------------------------------------------------
using UnityEditor;
using HeroKit.Editor.HeroField;

/// <summary>
/// This is the inspector for the HeroKitListener script. 
/// This inspector is shown when you click on a game object 
/// in the scene that contains the HeroKitListener script.
/// </summary>
[CustomEditor(typeof(HeroKitListenerUI2D))]
public class HeroKitListenerUI2DInspector : Editor
{
    /// <summary>
    /// The hero kit listener that will use the inspector.
    /// </summary>
    private HeroKitListenerUI2D heroKitListener;

    /// <summary>
    /// Actions to perform when the hero kit listener is enabled.
    /// </summary>
    private void OnEnable()
    {
        heroKitListener = (HeroKitListenerUI2D)target;
    }

    /// <summary>
    /// Display a custom inspector for the hero kit listener.
    /// </summary>
    public override void OnInspectorGUI()
    {
        // show existing inspector
        base.OnInspectorGUI();

        if (heroKitListener != null && heroKitListener.sendNotificationsHere != null && heroKitListener.sendNotificationsHere.heroObject != null)
        {
            ChooseAction();       
        }
    }

    /// <summary>
    /// What should happen when a game object with a hero kit listener receives input (mouse click, key press, etc)
    /// </summary>
    public void ChooseAction()
    {
        string[] items = { "Send Values and Play Event", "Send Values"};
        GenericListField genericList = new GenericListField(items);
        heroKitListener.actionType = genericList.SetValuesInspector(heroKitListener.actionType, "Action Type");

        // play event
        if (heroKitListener.actionType <= 1)
        {
            PlayEvent();
        }
    }

    /// <summary>
    /// Play an event on a hero kit object.
    /// </summary>
    public void PlayEvent()
    {
        // show states and events
        if (heroKitListener.sendNotificationsHere.heroObject.states != null &&
            heroKitListener.sendNotificationsHere.heroObject.states.states != null &&
            heroKitListener.sendNotificationsHere.heroObject.states.states.Count != 0)
        {
            StateListField states = new StateListField();
            heroKitListener.stateID = states.SetValuesInspector(heroKitListener.stateID, heroKitListener.sendNotificationsHere.heroObject.states.states, "State");

            if (heroKitListener.stateID > 0)
            {
                EventListField events = new EventListField();
                heroKitListener.eventID = events.SetValuesInspector(heroKitListener.eventID, heroKitListener.sendNotificationsHere.heroObject.states.states[heroKitListener.stateID - 1].heroEvent, "Event To Execute");
            }
        }
        else
        {
            if (heroKitListener != null)
            {
                heroKitListener.stateID = -1;
                heroKitListener.eventID = -1;
            }
        }
    }
}

Replace HeroKitListenerUI2D code with:

// --------------------------------------------------------------
// Copyright (c) 2016-2017 Aveyond Studios. 
// All Rights Reserved.
// --------------------------------------------------------------
using UnityEngine;
using UnityEngine.EventSystems;
using HeroKit.Scene;

/// <summary>
/// Script that you can attach to any game object. You can use this script to communicate with a hero kit object in the game.
/// </summary>
public class HeroKitListenerUI2D : MonoBehaviour, ISubmitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    //-------------------------------------------
    // Variables
    //-------------------------------------------

    /// <summary>
    /// The type of input that triggers an interaction with a hero kit object.
    /// </summary>
    public enum InputType { onSubmit, onClick, onPress, onDrag, onDoubleClick };
    /// <summary>
    /// The input type to watch for.
    /// </summary>
    public InputType inputType;
    /// <summary>
    /// The ID assigned to the hero kit listener.
    /// </summary>
    public int itemID;
    /// <summary>
    /// A hero object assigned to the hero kit listener.
    /// </summary>
    public HeroObject item;
    /// <summary>
    /// Hero kit object that should receive hero kit objects.
    /// </summary>
    public HeroKitObject sendNotificationsHere = null;
    /// <summary>
    /// Type of action to perform.
    /// </summary>
    [HideInInspector]
    public int actionType;
    /// <summary>
    /// ID assigned to the state on the hero kit object to interact with.
    /// </summary>
    [HideInInspector]
    public int stateID = -1;
    /// <summary>
    /// ID assigned to the event on the hero kit objet to interact with.
    /// </summary>
    [HideInInspector]
    public int eventID = -1;
    /// <summary>
    /// Time between clicks.
    /// </summary>
    private float clickTime;
    /// <summary>
    /// Click count (1=single-click, 2=double-click, etc)
    /// </summary>
    private int clickCount = 0;
    /// <summary>
    /// If this interval passes between clicks, the click count is reset.
    /// </summary>
    private float clickInterval = 0.5f;

    //-------------------------------------------
    // Interaction Types
    //-------------------------------------------
    
    /// <summary>
    /// On submit.
    /// </summary>
    /// <param name="data">The pointer event data.</param>
    public void OnSubmit(BaseEventData data)
    {
        if (data == null || sendNotificationsHere == null) return;

        // submitted on item
        if (inputType == InputType.onSubmit)
        {
            performAction();
        }

    }
    /// <summary>
    /// On click or double click.
    /// </summary>
    /// <param name="data">The pointer event data.</param>
    public void OnPointerClick(PointerEventData data)
	{	
		if (data == null || sendNotificationsHere == null) return;        

        // get interval between this click and the previous one (check for double click)
        float interval = data.clickTime - clickTime;

        // if this is double click, change click count
        if (interval < clickInterval && interval > 0 && clickCount != 2)
            clickCount = 2;
        else
            clickCount = 1;

        // reset click time
        clickTime = data.clickTime;
		
        // clicked on item
		if (inputType == InputType.onClick && clickCount == 1)
		{
            performAction();
        }
		
        // double-clicked item
		else if (inputType == InputType.onDoubleClick && clickCount == 2)
		{
            performAction();
        }		
	}
    /// <summary>
    /// Press down or begin drag.
    /// </summary>
    /// <param name="data">Pointer event data.</param>
    public void OnPointerDown(PointerEventData data)
    {		
		if (data == null || sendNotificationsHere == null) return;	
		
		if (inputType == InputType.onPress || inputType == InputType.onDrag)
		{
            performAction();

            //GameObject go = getTargetGO(data);
            //database.menuActions.sendUIAction(panelID, slotID, template, isClicked, isPressedBegin, isPressedEnd, isDrag, go);
            //if (onPress) database.inputActions.addToPickList(database.inputActions.pressDownList, go);
            //if (onDrag)	database.inputActions.addToPickList(database.inputActions.dragBeginList, go);
        }
    }
    /// <summary>
    /// Release press or end drag.
    /// </summary>
    /// <param name="data">Pointer event data.</param>
    public void OnPointerUp(PointerEventData data)
    {
        // the problem: only works on listener that was pressed down. For example,
        // if you drag icon from shortcut menu to crafting menu, the shortcut menu
        // panel calls onPointerUp. If you drag mouse over crafting menu (not slot) and
        // release mouse over a slot, nothing happens because the mouse did not start
        // dragging over the slot.

        //print ("aml: on pointer up");		

        if (data == null || sendNotificationsHere == null) return; 
		
		if (inputType == InputType.onPress || inputType == InputType.onDrag)
		{
            performAction();

            //GameObject go = getTargetGO(data);			
            //database.menuActions.sendUIAction(panelID, slotID, template, isClicked, isPressedBegin, isPressedEnd, isDrag, go);		
            //if (onPress) database.inputActions.addToPickList(database.inputActions.pressUpList, go);
            //if (onDrag) database.inputActions.addToPickList(database.inputActions.dragEndList, go);
        }	
    }
    /// <summary>
    /// Dragging.
    /// </summary>
    /// <param name="data">Pointer event data.</param>
    public void OnDrag(PointerEventData data)
	{
		if (data == null || sendNotificationsHere == null) return;
		
		if (inputType == InputType.onDrag)
		{
            performAction();

            //database.menuActions.sendUIAction(panelID, slotID, template, isClicked, isPressedBegin, isPressedEnd, isDrag, go);		
            //database.inputActions.addToPickList(database.inputActions.dragList, go);					
        }
	}

    //-------------------------------------------
    // General
    //-------------------------------------------

    /// <summary>
    /// Perform an action on the hero kit that can communicate with this listener. 
    /// </summary>
    private void performAction()
    {
        if (actionType <= 1)
        {
            callEvent(sendNotificationsHere, stateID, eventID);
        }
        else if (actionType == 2)
        {
            passValues(sendNotificationsHere);
        }
        
    }
    /// <summary>
    /// Call an event in a state on a hero kit object.
    /// </summary>
    /// <param name="heroKitObject">The hero kit object.</param>
    /// <param name="stateID">ID assigned to the state on the hero kit object.</param>
    /// <param name="eventID">ID assigned to the event in the hero kit object.</param>
    private void callEvent(HeroKitObject heroKitObject, int stateID, int eventID)
    {
        if (heroKitObject != null)
        {
            if (stateID > 0 && eventID > 0)
            {
                if (heroKitObject.heroStateData.state == stateID - 1)
                {
                    heroKitObject.heroListenerData.active = true;
                    heroKitObject.heroListenerData.itemID = itemID;
                    heroKitObject.heroListenerData.item = item;
                    heroKitObject.PlayEvent(eventID - 1, gameObject);
                }
            }
        }
    }
    /// <summary>
    /// Pass values to this listener from a hero kit object.
    /// </summary>
    /// <param name="heroKitObject">The hero kit object.</param>
    private void passValues(HeroKitObject heroKitObject)
    {
        heroKitObject.heroListenerData.active = true;
        heroKitObject.heroListenerData.itemID = itemID;
        heroKitObject.heroListenerData.item = item;
    }
}

/// <summary>
/// Data for a hero kit listener.
/// </summary>
public struct HeroKitListenerData2D
{
    public bool active;
    public int itemID;
    public HeroObject item;
}

Add HeroKitListenerUI2D, not HeroKitListenerUI

image.png.5e18a88acc81d267ee902547472649ea.png

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×