Example #1
0
        public ActorEditLogic(Widget widget, World world, WorldRenderer worldRenderer, Dictionary <string, MiniYaml> logicArgs)
        {
            this.worldRenderer  = worldRenderer;
            editorActorLayer    = world.WorldActor.Trait <EditorActorLayer>();
            editorActionManager = world.WorldActor.Trait <EditorActionManager>();

            editor         = widget.Parent.Get <EditorViewportControllerWidget>("MAP_EDITOR");
            actorEditPanel = editor.Get <BackgroundWidget>("ACTOR_EDIT_PANEL");

            typeLabel    = actorEditPanel.Get <LabelWidget>("ACTOR_TYPE_LABEL");
            actorIDField = actorEditPanel.Get <TextFieldWidget>("ACTOR_ID");

            initContainer   = actorEditPanel.Get("ACTOR_INIT_CONTAINER");
            buttonContainer = actorEditPanel.Get("BUTTON_CONTAINER");

            checkboxOptionTemplate = initContainer.Get("CHECKBOX_OPTION_TEMPLATE");
            sliderOptionTemplate   = initContainer.Get("SLIDER_OPTION_TEMPLATE");
            dropdownOptionTemplate = initContainer.Get("DROPDOWN_OPTION_TEMPLATE");
            initContainer.RemoveChildren();

            var deleteButton = actorEditPanel.Get <ButtonWidget>("DELETE_BUTTON");
            var cancelButton = actorEditPanel.Get <ButtonWidget>("CANCEL_BUTTON");
            var okButton     = actorEditPanel.Get <ButtonWidget>("OK_BUTTON");

            actorIDErrorLabel           = actorEditPanel.Get <LabelWidget>("ACTOR_ID_ERROR_LABEL");
            actorIDErrorLabel.IsVisible = () => actorIDStatus != ActorIDStatus.Normal;
            actorIDErrorLabel.GetText   = () => actorIDStatus == ActorIDStatus.Duplicate ?
                                          "Duplicate Actor ID" : "Enter an Actor ID";

            MiniYaml yaml;

            if (logicArgs.TryGetValue("EditPanelPadding", out yaml))
            {
                editPanelPadding = FieldLoader.GetValue <int>("EditPanelPadding", yaml.Value);
            }

            okButton.IsDisabled      = () => !IsValid() || !editActorPreview.IsDirty;
            okButton.OnClick         = Save;
            cancelButton.OnClick     = Cancel;
            deleteButton.OnClick     = Delete;
            actorEditPanel.IsVisible = () => CurrentActor != null &&
                                       editor.CurrentBrush == editor.DefaultBrush &&
                                       Game.RunTime > lastScrollTime + scrollVisibleTimeout;

            actorIDField.OnEscKey = () =>
            {
                actorIDField.YieldKeyboardFocus();
                return(true);
            };

            actorIDField.OnTextEdited = () =>
            {
                var actorId = actorIDField.Text.Trim();
                if (string.IsNullOrWhiteSpace(actorId))
                {
                    nextActorIDStatus = ActorIDStatus.Empty;
                    return;
                }

                // Check for duplicate actor ID
                if (CurrentActor.ID.Equals(actorId, StringComparison.OrdinalIgnoreCase))
                {
                    if (editorActorLayer[actorId] != null)
                    {
                        nextActorIDStatus = ActorIDStatus.Duplicate;
                        return;
                    }
                }

                SetActorID(actorId);
                nextActorIDStatus = ActorIDStatus.Normal;
            };

            actorIDField.OnLoseFocus = () =>
            {
                // Reset invalid IDs back to their starting value
                if (actorIDStatus != ActorIDStatus.Normal)
                {
                    SetActorID(initialActorID);
                }
            };
        }