Exemplo n.º 1
0
    public async void CreateNewAction(string action_id, IActionProvider actionProvider, string newName = null)
    {
        try {
            ActionMetadata    actionMetadata   = actionProvider.GetActionMetadata(action_id);
            List <IParameter> actionParameters = await Base.Parameter.InitActionParameters(actionProvider.GetProviderId(),
                                                                                           actionMetadata.ParametersMetadata.Values.ToList(), HiddenPlace, OnChangeParameterHandler, HiddenPlaceLayout, HiddenPlace, currentActionPoint, false, CanvasGroup);

            string newActionName;

            if (string.IsNullOrEmpty(newName))
            {
                newActionName = Base.ProjectManager.Instance.GetFreeActionName(actionMetadata.Name);
            }
            else
            {
                newActionName = Base.ProjectManager.Instance.GetFreeActionName(newName);
            }
            if (Base.Parameter.CheckIfAllValuesValid(actionParameters))
            {
                List <IO.Swagger.Model.ActionParameter> parameters = new List <IO.Swagger.Model.ActionParameter>();
                foreach (IParameter actionParameter in actionParameters)
                {
                    if (!actionMetadata.ParametersMetadata.TryGetValue(actionParameter.GetName(), out Base.ParameterMetadata actionParameterMetadata))
                    {
                        Base.Notifications.Instance.ShowNotification("Failed to create new action", "Failed to get metadata for action parameter: " + actionParameter.GetName());
                        return;
                    }
                    IO.Swagger.Model.ActionParameter ap = new IO.Swagger.Model.ActionParameter(name: actionParameter.GetName(), value: JsonConvert.SerializeObject(actionParameter.GetValue()), type: actionParameter.GetCurrentType());
                    parameters.Add(ap);
                }
                try {
                    addedActionName = newActionName;
                    Hide();
                    AREditorResources.Instance.LeftMenuProject.SetActiveSubmenu(AREditorResources.Instance.LeftMenuProject.CurrentSubmenuOpened);

                    await Base.WebsocketManager.Instance.AddAction(currentActionPoint.GetId(), parameters, Base.Action.BuildActionType(
                                                                       actionProvider.GetProviderId(), actionMetadata.Name), newActionName, actionMetadata.GetFlows(newActionName));

                    foreach (Transform t in HiddenPlace.transform)
                    {
                        if (!t.CompareTag("Persistent"))
                        {
                            Destroy(t.gameObject);
                        }
                    }
                } catch (Base.RequestFailedException e) {
                    Base.Notifications.Instance.ShowNotification("Failed to add action", e.Message);
                    addedActionName = null;
                }
            }
        } catch (Base.RequestFailedException e) {
            Base.Notifications.Instance.ShowNotification("Failed to add action", e.Message);
        }
    }