Jump to content

How to add PAW elements from a KSPAddon


Recommended Posts

As a memo for myself, and as a quick guide for others :

Add PAW buttons to all parts from a KSPAddon, using KSPField decoration :

    [KSPAddon(KSPAddon.Startup.EditorAny, false)]
    public class MyClass : MonoBehaviour
    {
        [KSPField(guiActive = true, guiActiveEditor = true, guiName = "Some boolean"), UI_Toggle(enabledText = "Enabled", disabledText = "Disabled")]
        public bool someBool = false;

        private void Start()
        {
            GameEvents.onPartActionUICreate.Add(OnPartActionUICreate);
        }

        private void OnDisable()
        {
            GameEvents.onPartActionUICreate.Remove(OnPartActionUICreate);
        }

        private void OnPartActionUICreate(Part part)
        {
            // Get the PAW window
            UIPartActionWindow paw = UIPartActionController.Instance.GetItem(part);
            // Get the attribute
            System.Reflection.FieldInfo fieldInfo = typeof(MyClass).GetField("someBool");
            if (fieldInfo == null) return;
            object fieldObject = fieldInfo.GetCustomAttributes(false).FirstOrDefault(p => p is KSPField);
            if (fieldObject == null) return;
            KSPField field = (KSPField)fieldObject;
            BaseField baseField = new BaseField(field, fieldInfo, this);
            // Add a callback, using the part as arg1
            baseField.uiControlEditor.onFieldChanged = delegate { MyCallback(baseField, part); } ;
            // Add the field to the PAW
            paw.AddFieldControl(baseField, part, null);
        }
      
        private void MyCallback(BaseField field, Part part)
        {
          // Do something with the part when the toggle is used
        }
    }

 

Link to comment
Share on other sites

1 minute ago, Gotmachine said:

As a memo for myself, and as a quick guide for others :

Add PAW buttons to all parts from a KSPAddon, using KSPField decoration :


    [KSPAddon(KSPAddon.Startup.EditorAny, false)]
    public class MyClass : MonoBehaviour
    {
        [KSPField(guiActive = true, guiActiveEditor = true, guiName = "Some boolean"), UI_Toggle(enabledText = "Enabled", disabledText = "Disabled")]
        public bool someBool = false;

        private void Start()
        {
            GameEvents.onPartActionUICreate.Add(OnPartActionUICreate);
        }

        private void OnDisable()
        {
            GameEvents.onPartActionUICreate.Remove(OnPartActionUICreate);
        }

        private void OnPartActionUICreate(Part part)
        {
            // Get the PAW window
            UIPartActionWindow paw = UIPartActionController.Instance.GetItem(part);
            // Get the attribute
            System.Reflection.FieldInfo fieldInfo = typeof(MyClass).GetField("someBool");
            if (fieldInfo == null) return;
            object fieldObject = fieldInfo.GetCustomAttributes(false).FirstOrDefault(p => p is KSPField);
            if (fieldObject == null) return;
            KSPField field = (KSPField)fieldObject;
            BaseField baseField = new BaseField(field, fieldInfo, this);
            // Add a callback, using the part as arg1
            baseField.uiControlEditor.onFieldChanged = delegate { MyCallback(baseField, part); } ;
            // Add the field to the PAW
            paw.AddFieldControl(baseField, part, null);
        }
      
        private void MyCallback(BaseField field, Part part)
        {
          // Do something with the part when the toggle is used
        }
    }

 

You can also ust KSPAction

Link to comment
Share on other sites

8 minutes ago, linuxgurugamer said:

You can also ust KSPAction

But then how to get the part at the origin of the action ? This is running from a KSPAddon, not a PartModule...

I also managed to get a KSPEvent working, this is easier :

        private void OnPartActionUICreate(Part part)
        {
            // Get the PAW window
            UIPartActionWindow paw = UIPartActionController.Instance.GetItem(part);

            // Prevent the button to be re-added once created
            if (paw.ListItems.Exists(p => p is UIPartActionButton && ((UIPartActionButton)p).Evt.name == "MyButtonID"))
            {
                return;
            }

            BaseEventDelegate del = new BaseEventDelegate(delegate { OnPawClick(part);});

            BaseEvent baseEvent = new BaseEvent(null, "MyButtonID", del, new KSPEvent());
            baseEvent.guiName = "My button title";
            baseEvent.guiActive = true;
            baseEvent.guiActiveEditor = true;
            paw.AddEventControl(baseEvent, part, null);
        }
        private void OnPawClick(Part part)
        {
           // do something with the part
        }

 

Link to comment
Share on other sites

  • 3 months later...

Sorry if this is a Thread Necro, but...   All the googling I'm doing on this comes up with stuff from a few to many years ago,
and going to Page2 of a thread list isnt really digging that deep in the archives to find what your looking for.

Been looking for the way to add gui items to part dialogs, but I keep finding discussions about the nuts & bolts of how to layout the additions but nothing ~clearly explaining to new modders~ how to make those additions happen with kspfield/addon (well, found lots of 2,3 and 5yr old stuff that doesnt work anymore)

From op code, I get a compile error  on

object fieldObject = fieldInfo.GetCustomAttributes(false).FirstOrDefault(p => p is KSPField);

My compiler says .FirtsOrDefault is no bueno

This stuff is NOT from 5yrs ago, its from October... is that FirtsOrDefault really gone, or is my Mono still not setup on the right version (it does claim to be making net3.5 dlls)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...