Jump to content

KSP Plugin Framework - Plugin Examples and Structure v1.1 (Apr 6)


Recommended Posts

I have got that on my long term list, but I keep adding things to the list :( . I wanted to try and make it "simpler" before adding it in here, because you currently need to have a few lines of code in different areas to make it work.

You could add that module to your code as is from TWP or ARP - they both use it - and then pick one ddl to use as example code, and I'm always happy to help... thats on my list too :)

Link to comment
Share on other sites

  • 2 weeks later...

Hey Trigger -- nice set of enhancements here. I'm lovin it.

Question: You talk in the RepeatingFunctions post about how the Unity API is not thread-safe which is why you implemented coroutines. I'm working on a mod to organize screenshots in the background (move them to VesselName/LaunchTime folders) and also sync them automatically to Imgur and other sites such as Photobucket, Flickr, Facebook, and more. Since neither of these functions interact with Unity, would you suggest using your RepeatingFunctions, or should I just spawn a BackgroundWorker thread for my slow internet request/response processes? Thanks.

Link to comment
Share on other sites

Hey Trigger -- nice set of enhancements here. I'm lovin it.

Question: You talk in the RepeatingFunctions post about how the Unity API is not thread-safe which is why you implemented coroutines. I'm working on a mod to organize screenshots in the background (move them to VesselName/LaunchTime folders) and also sync them automatically to Imgur and other sites such as Photobucket, Flickr, Facebook, and more. Since neither of these functions interact with Unity, would you suggest using your RepeatingFunctions, or should I just spawn a BackgroundWorker thread for my slow internet request/response processes? Thanks.

A background worker will be best in that sitaution as it can take a long time without affecting framerate. I use that method for background checking versions (and I know a few other mods do to). If you would like an example then you can see some code in use here - https://github.com/TriggerAu/KerbalAlarmClock/blob/master/KerbalAlarmClock/Settings.cs#L325-331 - thats the start of the worker and obviously stuff happens inside that.

For something a little more complex the TWP mod (in my sig) also has a progress bar /indicator for a long running loop - stores the progress in a shared variable that's read ni the GUI routines (starts here https://github.com/TriggerAu/TransferWindowPlanner/blob/master/TransferWindowPlanner/TWPWindowWorkers.cs#L151)

Let me know if any of that doesnt make sense

Link to comment
Share on other sites

  • 1 month later...

Hey there TriggerAu,

I'm using this framework for TestFlight and it has been a HUGE boon so thank you very much! I noticed though that referencing the UI stuff in your own mods, like ARP, that you have a MonoBehaviourWindowPlus that implements things like Dropdown Lists. Would it be ok to use this code?

- - - Updated - - -

I have got that on my long term list, but I keep adding things to the list :( . I wanted to try and make it "simpler" before adding it in here, because you currently need to have a few lines of code in different areas to make it work.

You could add that module to your code as is from TWP or ARP - they both use it - and then pick one ddl to use as example code, and I'm always happy to help... thats on my list too :)

Ok I should have actually read the posts first before posting my query. Sorry! So I am gogin to go ahead and grab some of your newer GUI code out of ARP if that is cool with you. Bajillion thanks for the GUI leg up.

Link to comment
Share on other sites

Is all good Agathorn. As well as the Window code that has the drop down stuff in it there is a datetime library of sorts in there as well. It would probably simpler to look at the code in the TWP mod as it has less other "stuff" in it.

If you get stuck with getting the DDLs to work then ping back here and I can help out. I do know that its not as polished as i would like it to be for a framework style thing

Link to comment
Share on other sites

Is all good Agathorn. As well as the Window code that has the drop down stuff in it there is a datetime library of sorts in there as well. It would probably simpler to look at the code in the TWP mod as it has less other "stuff" in it.

If you get stuck with getting the DDLs to work then ping back here and I can help out. I do know that its not as polished as i would like it to be for a framework style thing

Thanks. I was actually able to integrate it fairly quickly for the most part. Right now I am just walking through the code trying to figure out why my DDL has -1 pages.

- - - Updated - - -

Turns out it didn't like the height of my window (even though it should have been fine). Its all working now :)

Link to comment
Share on other sites

Awesome Agathorn. BTW I really like that mod and the changes you've been adding

Thanks! Well with your awesome GUI code, it will get even better :) Seriously I have personally always looked at ARP as one of the best GUIs in the game, and it has been my inspiration all along. Now I am in the midst of the voted for GUI revamp so making good use of this.

Link to comment
Share on other sites

Hey TriggerAu two questions for you if I might.

1) How do I make it so that when the tooltip appears it doesn't cause the layout of the window to change to accommodate the tooltip? Shouldn't the tooltip be above the window? When the tooltip is first displayed it is causing the window layout to shift over to make room for it. Adding a proper tooltip style fixed this.

2) Any idea what I might be doing wrong with sing a texture for a button? I just end up with a white square :( The same thing happens even using your images, and my resource loading code is pulled from yours. Texture wasn't properly loading. Fixed.

Edited by Agathorn
Link to comment
Share on other sites

Hey TriggerAu two questions for you if I might.

1) How do I make it so that when the tooltip appears it doesn't cause the layout of the window to change to accommodate the tooltip? Shouldn't the tooltip be above the window? When the tooltip is first displayed it is causing the window layout to shift over to make room for it. Adding a proper tooltip style fixed this.

2) Any idea what I might be doing wrong with sing a texture for a button? I just end up with a white square :( The same thing happens even using your images, and my resource loading code is pulled from yours. Texture wasn't properly loading. Fixed.

Thats my kind of questions - ones where when i get to the site the answers are already there :) cool

EDIT: Actually one other thing I am considering with my plugins is to put the Textures that dont get used via the game library in a PluginData folder - this then means that KSP doesnt load them on startup with two benefits - less time for startup and less memory consumed. You can see this in place in the KSPTips plugin - http://forum.kerbalspaceprogram.com/threads/103537-KSP-Tips. Basically for say TWP this just means I need to rename the Textures folder to PluginData and ensure the loading stuff is right

Edited by TriggerAu
Link to comment
Share on other sites

  • 2 weeks later...

Hey TriggerAu,

Any idea why I can properly draw a 2D GL texture/line on the GUI if I manually override OnGui() myself, but I can't when using DrawWindow() as exposed by MonoBehaviourWindow?

Edited by Agathorn
Link to comment
Share on other sites

Hey TriggerAu,

Any idea why I can properly draw a 2D GL texture/line on the GUI if I manually override OnGui() myself, but I can't when using DrawWindow() as exposed by MonoBehaviourWindow?

Would be because the DrawWindow functionality is attached to the RenderingManager where all good window code goes :wink: but that doesnt let you draw outside of the windows surface. OnGUI is the right place to do this.

In the framework I exposed this via the overridable OnGUIEvery method in the framework (which is an extension of the MonoBehaviourExtended class (which the window inherits from)). You can see its implementation here: https://github.com/TriggerAu/TransferWindowPlanner/blob/master/TransferWindowPlanner/Framework/MonoBehaviourExtended.cs#L334-349 which is basically what you have done, but with some other bits in there for one off and skins support

Link to comment
Share on other sites

Woops, didnt mean that as in good/bad - meant where window layout stuff goes that works with the F2 key, etc.

In ARP for example I draw lines in the scenes, and thats in the OnGUIEvery stuff - that is what its for. eg. This is where the line is drawn

https://github.com/TriggerAu/AlternateResourcePanel/blob/master/AlternateResourcePanel/ARPPartWindow.cs#L351-352

And this is the DrawLine function that handles all the posibilities

https://github.com/TriggerAu/AlternateResourcePanel/blob/master/AlternateResourcePanel/SharedStuff/Drawing.cs#L280-380

That drawline method can in fact be used inside DrawWindow as its drawing a "texture" line - was much easier than the other methods I tried

Link to comment
Share on other sites

That looks similiar to what I snagged from the Unity forums. I have the window and the lien both working together, but the line is being draw UNDER the window.

If you are using the same method as in that ARP code then I'm pretty sure if you call teh code inside the drawwindow routines it will drawit on the window, but you need to define the rect position using the window top left as 0,0 and not using the screen position.

The other method is to look at using GUI.depth and setting it to 0 just after you draw the line which should raise the line above the other stuff.

Link to comment
Share on other sites

This is my current WIP code.

It is using screen position, but that seems to place it in the right spot on the screen, just not the right depth.

https://github.com/jwvanderbeck/TestFlight/blob/push-to-pull/TestFlightCore/TestFlightCore/TestFlightEditor.cs

I'll redo the code using your line code instead and see if that changes.

Ok I think the problem is that the line drawing code I got from the Unity forums doesn't use GUI to draw. For speed reasons it uses the underlying screen space rather than GUI space.

- - - Updated - - -

Ok so even using your Line Drawing code, the line is under the window.

Edited by Agathorn
Link to comment
Share on other sites

I've gotta duck off for the morning now, but I'll download and have a look later if thats OK

No worries I appreciate any help you can give, whenever you are able to give it :)

I'll keep banging on it and if I come up with a solution I'll post it.

Link to comment
Share on other sites

So best I can tell, the DrawWindow always happens *after* the OnGuiEvery(), and since Unity's GUI is simply layered first to last, the line is always draw below the window.

The only way I have managed to get the line above the window is to handle the entire window inside OnGuiEvery(), but that means I guess implementing ALL of the window drawing logic myself, which is taking some time. It works though so I guess that's what i'll have to do.

Link to comment
Share on other sites

Ok so I have it all working. As best I can tell it isn't possible to get it working with your awesome DrawWindow stuff though :( *All* the windw/gui code has to be in OnGUI along with the line drawing, or else the GUI will end up on top of it.

So I simply implemented my GUI manually in OnGUI. Since this is in the Editor and its a simple window for the most part, it wasn't a huge deal. I just had to learn how to do the GUI setup myself rather than relying on your code which makes it much easier :D


[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class TestFlightEditorWindow : MonoBehaviourExtended
{
internal static TestFlightEditorWindow Instance;
internal Part selectedPart;
internal bool showGraph = true;
internal Rect WindowRect;
internal bool Visible;

internal override void Start()
{
LogFormatted_DebugOnly("TestFlightEditor: Initializing Editor Hook");
EditorPartList.Instance.iconPrefab.gameObject.AddComponent<TestFlightEditorHook>();
Instance = this;
WindowRect = new Rect(Screen.width - 350, Screen.height - 550, 300, 500);
}

internal override void OnGUIOnceOnly()
{
base.OnGUIOnceOnly();
Styles.InitStyles();
Styles.InitSkins();
SkinsLibrary.SetCurrent("SolarizedDark");
}
internal override void OnGUIEvery()
{
base.OnGUIEvery();

if (Visible)
{
GUILayout.BeginArea(WindowRect, SkinsLibrary.CurrentSkin.GetStyle("HUD"));
GUILayout.BeginVertical();
GUILayout.Label(String.Format("Selected Part: {0}", selectedPart.name));
GUILayout.Space(250);
GUILayout.EndVertical();
Vector2 startPoint = new Vector2(5, 200);
Vector2 endPoint = new Vector2(100, 500);
Color lineColor = XKCDColors.Yellow;
Drawing.DrawLine(startPoint, endPoint, lineColor, 5f);
GUILayout.EndArea();
}
}
}


public static class Drawing
{
private static Texture2D lineTex = null;
public static void DrawLine(Rect rect) { DrawLine(rect, GUI.contentColor, 1.0f); }
public static void DrawLine(Rect rect, Color color) { DrawLine(rect, color, 1.0f); }
public static void DrawLine(Rect rect, float width) { DrawLine(rect, GUI.contentColor, width); }
public static void DrawLine(Rect rect, Color color, float width) { DrawLine(new Vector2(rect.x, rect.y), new Vector2(rect.x + rect.width, rect.y + rect.height), color, width); }
public static void DrawLine(Vector2 pointA, Vector2 pointB) { DrawLine(pointA, pointB, GUI.contentColor, 1.0f); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color) { DrawLine(pointA, pointB, color, 1.0f); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, float width) { DrawLine(pointA, pointB, GUI.contentColor, width); }
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)
{
// Save the current GUI matrix and color, since we're going to make changes to it.
Matrix4x4 matrix = GUI.matrix;
Color savedColor = GUI.color;
// Generate a single pixel texture if it doesn't exist
if (!lineTex) { lineTex = new Texture2D(1, 1); lineTex.SetPixel(0, 0, color); lineTex.Apply(); }
// and set the GUI color to the color parameter
GUI.color = color;
// Determine the angle of the line.
Single angle = Vector3.Angle(pointB - pointA, Vector2.right);
// Vector3.Angle always returns a positive number.
// If pointB is above pointA, then angle needs to be negative.
if (pointA.y > pointB.y) { angle = -angle; }
// Set the rotation for the line. The angle was calculated with pointA as the origin.
GUIUtility.RotateAroundPivot(angle, pointA);
// Finally, draw the actual line. we've rotated the GUI, so now we draw the length and width
GUI.DrawTexture(new Rect(pointA.x, pointA.y, (pointA-pointB).magnitude, width), lineTex);
// We're done. Restore the GUI matrix and GUI color to whatever they were before.
GUI.matrix = matrix;
GUI.color = savedColor;
}
}

Link to comment
Share on other sites

Nice one Agathorn, Glad you found a way to do it. I'll put line drawing on my long term list now that you have something working :)

One way I think it could work, but that was beyond me to set up, would be to draw the line to an off screen texture and then display that texture in the UI as just another UI element using Label or Box. But that was beyond me :) This works even if it is a bit simple.

Link to comment
Share on other sites

https://ksppluginframework.codeplex.com/SourceControl/latest#Examples/PersistingData/DictionaryTest.cs

Have you verified that this code to store Dictionaries in ConfigNodeStorage still works recently? I'm trying to do *exactly* this, and while I get no errors on save or load, my .cfg file is empty (Just has the parent scope)

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