Jump to content

Adding a second function to a UI button in the VAB/SPH


Recommended Posts

Hi all

So I have 2 buttons that I would like to add a second module call and function to

Currently I am asking for ksp to list all parts that have a part module and then I have assigned functions to the buttons asscoiated with that module ... The idea is to associate those buttons to the functions of another part module to the effect of making those buttons trigger 2 functions from different part modules with one click

Any help is appreciated ... here's the code I am looking to do this for:

 

https://github.com/DoctorDavinci/DCK/blob/master/DCKinc_Plugin/DCKPaintshop.cs

Link to comment
Share on other sites

14 minutes ago, HebaruSan said:

What problems are you encountering with the current code?

There are no problems as it works as is quite well

What I want to do is to find modules implementing DCKtextureswitch2 (which is what the code does now) as well as for parts implementing InterstellarTextureSwitch2 and associate next texture and previous texture to one button (respectively) for both modules

This saves me from having to put another pair of buttons in DCK Paintshop for parts that are using InterstellarTextureSwitch2 ... having next and previous in the paintshop controlling the next and previous of both modules would help with keeping the clutter of buttons down to a minimum

I've got 4 buttons in DCK Paintshop now (2 for textures on parts and the other controlling the tranparent/invisible armor) ... I'd like to not have to add another pair of buttons

Link to comment
Share on other sites

46 minutes ago, DoctorDavinci said:

What I want to do is to find modules implementing DCKtextureswitch2 (which is what the code does now) as well as for parts implementing InterstellarTextureSwitch2 and associate next texture and previous texture to one button (respectively) for both modules

So something like this?

            List<DCKtextureswitch2> dckParts = new List<DCKtextureswitch2>(200);
            List<DCKtextureswitch2> intParts = new List<InterstellarTextureSwitch2>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                dckParts.AddRange(p.FindModulesImplementing<DCKtextureswitch2>());
                intParts.AddRange(p.FindModulesImplementing<InterstellarTextureSwitch2>());
            }
            foreach (DCKtextureswitch2 dckPart in dckParts)
            {
                dckPart.updateSymmetry = false;             //FIX symmetry problems because DCK also applies its own logic here
                                                            // send previous or next command
                if (next)
                    dckPart.nextTextureEvent();
                else
                    dckPart.previousTextureEvent();
            }
            foreach (InterstellarTextureSwitch2 intPart in intParts)
            {
                intPart.updateSymmetry = false;
                if (next)
                    intPart.nextTextureEvent();
                else
                    intPart.previousTextureEvent();
            }

 

Link to comment
Share on other sites

2 minutes ago, HebaruSan said:

So something like this?


            List<DCKtextureswitch2> dckParts = new List<DCKtextureswitch2>(200);
            List<DCKtextureswitch2> intParts = new List<InterstellarTextureSwitch2>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                dckParts.AddRange(p.FindModulesImplementing<DCKtextureswitch2>());
                intParts.AddRange(p.FindModulesImplementing<InterstellarTextureSwitch2>());
            }
            foreach (DCKtextureswitch2 dckPart in dckParts)
            {
                dckPart.updateSymmetry = false;             //FIX symmetry problems because DCK also applies its own logic here
                                                            // send previous or next command
                if (next)
                    dckPart.nextTextureEvent();
                else
                    dckPart.previousTextureEvent();
            }
            foreach (InterstellarTextureSwitch2 intPart in intParts)
            {
                intPart.updateSymmetry = false;
                if (next)
                    intPart.nextTextureEvent();
                else
                    intPart.previousTextureEvent();
            }

 

Looks close to what I tried earlier although I did it as a separate piece of code:

Spoiler

        void SendEventDCK(bool next)  //true: next texture, false: previous texture
        {
            Part root = EditorLogic.RootPart;
            if (!root)
                return;            // find all DCKtextureswitch2 modules on all parts
            List<DCKtextureswitch2> dckParts = new List<DCKtextureswitch2>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                dckParts.AddRange(p.FindModulesImplementing<DCKtextureswitch2>());
            }
            foreach (DCKtextureswitch2 dckPart in dckParts)
            {
                dckPart.updateSymmetry = false;             //FIX symmetry problems because DCK also applies its own logic here
                                                            // send previous or next command
                if (next)
                    dckPart.nextTextureEvent();
                else
                    dckPart.previousTextureEvent();
            }
        }


        void SendEventDCKAA(bool next)  //true: next texture, false: previous texture
        {
            Part root = EditorLogic.RootPart;
            if (!root)
                return;            // find all DCKAAtextureswitch2 modules on all parts
            List<DCKAAtextureswitch2> dckAAParts = new List<DCKAAtextureswitch2>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                dckAAParts.AddRange(p.FindModulesImplementing<DCKAAtextureswitch2>());
            }
            foreach (DCKAAtextureswitch2 dckAAPart in dckAAParts)
            {
                dckAAPart.updateSymmetry = false;             //FIX symmetry problems because DCK also applies its own logic here
                                                              // send previous or next command
                if (next)
                    dckAAPart.nextTextureEvent();
                else
                    dckAAPart.previousTextureEvent();
            }
        }

        void SendEventITS(bool next)  //true: next texture, false: previous texture
        {
            Part root = EditorLogic.RootPart;
            if (!root)
                return;            // find all InterstellarTextureSwitch2 modules on all parts
            List<InterstellarTextureSwitch2> itsParts = new List<InterstellarTextureSwitch2>(200);
            foreach (Part p in EditorLogic.fetch.ship.Parts)
            {
                itsParts.AddRange(p.FindModulesImplementing<InterstellarTextureSwitch2>());
            }
            foreach (InterstellarTextureSwitch2 itsPart in itsParts)
            {
                itsPart.updateSymmetry = false;             //FIX symmetry problems because DCK also applies its own logic here
                                                            // send previous or next command
                if (next)
                    itsPart.nextTextureEvent();
                else
                    itsPart.previousTextureEvent();
            }

 

 

I then attached the actions to their respective buttons like this:

Spoiler

        void DCKWindow(int windowID)
        {
            if (GUI.Button(new Rect(15, 30, 75, 25), "DCK Prev", HighLogic.Skin.button))    //change rect here for button size, position and text
            {
                SendEventDCK(false);
                SendEventITS(false);
            }

            if (GUI.Button(new Rect(100, 30, 75, 25), "DCK Next", HighLogic.Skin.button))       //change rect here for button size, position and text
            {
                SendEventDCK(true);
                SendEventITS(true);
            }
            if (GUI.Button(new Rect(15, 60, 75, 25), "Armor Prev", HighLogic.Skin.button))    //change rect here for button size, position and text
            {
                SendEventDCKAA(false);
            }

            if (GUI.Button(new Rect(100, 60, 75, 25), "Armor Next", HighLogic.Skin.button))       //change rect here for button size, position and text
            {
                SendEventDCKAA(true);
            }


            GUI.DragWindow();
        }

 

Long story short, didn't work

So I then created a paintshop for only Its2 and it didn't work where the code will work for both the DCKts2 and the DCKAAts2 ... Looks like I need to use a different approach to interface with the Its2 module as even within the confines of it's own paintshop the texture switching will not work :/

Link to comment
Share on other sites

Just now, HebaruSan said:

This is probably a case where the long story would be better, since it may be possible to solve whatever the problem was.

Well, in order for my Mk2 Expansion mm configs to work with DCK Paintshop, I have to use InterstellarTextureSwitch2 due to the way the models were created as they are calling textures from multiple stock parts which the code I used will not support (DCKtextureswitch2)

So I created all the configs and they work perfectly with DCK textures when using Its2 to switch the textures on each part individually, but I want to be able to use the feature of DCK Paintshop to switch all part textures in one click ... This works perfect with parts using DCKts2 and DCKAAts2 but using the exact same code with Its2 does not work

I have IFS in my project as a reference and have no problems building the above code but it just won't work with parts using Its2

I tried a few iterations of the above and I'm at a loss (although that is to be expected since I know nothing of C# coding :rolleyes:)

Link to comment
Share on other sites

3 hours ago, DoctorDavinci said:

This works perfect with parts using DCKts2 and DCKAAts2 but using the exact same code with Its2 does not work

Alright, so either it's not finding the ITS2 part modules, or the part modules aren't working the way you expect. Do you know which it is? If not, you could use Debug.Log() calls to add messages to KSP.log, like this, continuing from my previous post:

            foreach (InterstellarTextureSwitch2 intPart in intParts)
            {
                Debug.Log("Updating ITS2 module..."); // <-- Add stuff like this as needed
                intPart.updateSymmetry = false;
                if (next)
                    intPart.nextTextureEvent();
                else
                    intPart.previousTextureEvent();
                Debug.Log("ITS2 module updated."); // <-- Add stuff like this as needed
            }

If it's finding the part modules, those messages will show up in KSP.log, otherwise they'll be absent.

Link to comment
Share on other sites

@HebaruSan - I actually figured out a way to use my current code to do the switching although it was a bit of a laborious job to edit all the M2X mm configs ... but they work now with DCKtextureswitch2 as well as the DCK Paintshop which was my original hope since there are some mods out there that do not play nicely with IFS (configurable containers for instance)

Anyways, thanx for the assistance :)

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...