• Content Count

  • Joined

  • Last visited

Community Reputation

46 Excellent

About fatcargo

  • Rank
    Spacecraft Engineer

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. Ok here it goes. The following code should be self-contained so copy/paste into IDE would work out of the box. It is derived from my code which is a mess and posting it directly would not be helpful. I had to make a separate block of code in editor just to make sure no errors creep in. This is demonstration of how to maintain top/left position of window while it changes size. No reset to coordinates given at SpawnPopupDialog(), no flickering during position update. public Vector3 popupOldPosition = Vector3.zero; public Vector2 popupOldSize = Vector2.zero; public Vector3 popupCorrectedPosition = Vector3.zero; public bool popupIsDragging = false; public bool doRefresh = false; public PopupDialog myDialog = null; public bool showDescription = false; public void ShowDialog() { myDialog = PopupDialog.SpawnPopupDialog( new MultiOptionDialog( "visible title text", "", "", HighLogic.UISkin, new Rect(0.5f, 0.5f, 300f, 300f), new DialogGUIVerticalLayout( new DialogGUIButton("Close", () => { }, true), new DialogGUIToggleButton(false, "show/hide", (b) => { showDescription = b; RecordOldPos(); }, 100f, 15f), new DialogGUILabel("long text that needs to fit inside available width of window so it has to break on multiple lines", 100f, -1f) { OptionEnabledCondition = () => { return showDescription; } } ) ), false, HighLogic.UISkin, false); myDialog.dialogToDisplay.OnResize = () => { if (!popupIsDragging && myDialog != null && doRefresh) { doRefresh = false; RestoreOldPos(); } }; EventTrigger popupDialogEvent = myDialog.gameObject.AddOrGetComponent<EventTrigger>(); EventTrigger.Entry popupBeginDrag = new EventTrigger.Entry() { eventID = EventTriggerType.BeginDrag }; popupBeginDrag.callback.AddListener((e) => { popupIsDragging = true; }); popupDialogEvent.triggers.Add(popupBeginDrag); EventTrigger.Entry popupEndDrag = new EventTrigger.Entry() { eventID = EventTriggerType.EndDrag }; popupEndDrag.callback.AddListener((e) => { popupIsDragging = false; RecordOldPos(); }); popupDialogEvent.triggers.Add(popupEndDrag); } public void RecordOldPos() { if (myDialog != null) { popupOldPosition = myDialog.RTrf.position; popupOldSize = myDialog.RTrf.rect.size; } doRefresh = true; } public void RestoreOldPos() { if (myDialog != null) { popupCorrectedPosition.x = popupOldPosition.x + (popupOldSize.x - myDialog.RTrf.rect.size.x) / 2; popupCorrectedPosition.y = popupOldPosition.y + (popupOldSize.y - myDialog.RTrf.rect.size.y) / 2; popupCorrectedPosition.z = popupOldPosition.z; myDialog.RTrf.position = popupCorrectedPosition; } } Note the use of custom added events to properly capture window position. Until i understood the coordinate system used for positioning and size of window i couldn't make heads or tails from available positional data. I'll add comments to code here instead of code box to keep it clean. "showDescription" is used to set visibility of label via toogle button. "popupIsDragging" is used for checking if dragging has ended (BeginDrag event is needed just to reinitializeset this variable. In this case it may appear superfluous, but may be necessary for more complex code that may update window contents during dragging operation). "popupOldPosition" , "popupOldSize" and "popupCorrectedPosition" are used to capture old and calculate new coordinates to maintain visually position of window while it's contents change. Now for the DRAWBACKS. The RecordOldPos() function HAS to be manually inserted into callback for any UI element that may cause window resizing event. I have not found any callback that fires just before window resize event, if there was one, it would capture all changes in one neat function. In above example a toggle button calls RecordOldPos() inside its onToggled() callback event. Note that myDialog.dialogToDisplay.OnResize() callback first sets "doRefresh" to false then calls RestoreOldPos(). Ordering is important because it may trigger another OnResize() event. During initial testing i haven't added this check and managed to crash KSP by causing self-triggering. The coordinate system used for positioning is centered inside display area, so setting window at (0,0) will put in middle. Also, a window transform is also put in center of window itself, not on any of corners. ASCII art time Deptiction of center coordinates on "screen space". X and Y are positive going right and upward. Negative coordinates move to left and below of screen's center. /|\ Y+ | ____________|___________ | | | | | | | (0,0) ------------- |-------------> X+ | | | | |______________________| Similar is with popup window's transform. It is also located at center of window and has same orientation. Now, a few words of caution. While i tested my code, i managed to get window completely outside visible area. It seems that authors intentionally added coordinate origin to center of screen, so if plugin sets window XY position to 0, it won't disappear. Also, whenever window is resized, its vital UI elements may end up outside visible area (for some rare cases for gigantic-sized windows) so it tries to reset to center, maximizing utility and minimizing loss of control. Bear in mind that modal windows will block user input and if they "fly" outside available screen space... Well, you get the picture
  2. Quick update : i'm still trying to find a way to keep window top/left position constant. No luck so far...
  3. HA! I managed to make it clean, no more stutter when doing visibility toggle on UI element ! I'll post an example tomorrow, but for now a hint : use yourDialog.dialogToDisplay.OnResize() callback to set position with yourDialog.RTrf.position Vector3 variable. It will nicely reposition the window BEFORE it gets rendered. Oh, i also added a bool refresh flag which is set to true at point where visibility toggle runs and then clear it in OnResize(). That way, callback won't cause runaway self-triggering (hmm i'll try without that refresh flag to see if it indeed self-triggers). The only thing left (avoidable) is when hide/show happens, the whole window changes size, so top/left position still jumps. I'll try to cook up some math to compensate for it.
  4. Yes it is pretty much same issue - i switch visibility of label via toggle button and window gets reset to positions defined at spawn. I did manage to set it back to last position prior to switching visibility, but you can still see for a brief moment how it jumps to old position then moves back. I will do more tests to see if i can hide that visual "stutter".
  5. I've made a window that has a label with multiline text and a toggle button that controls label's visibility of that label (via OptionEnabledCondition). EXAMPLE SNIPPET public bool showDescription = false; public DialogGUILabel lab = new DialogGUILabel("example text for\nmultiple\nlines") { OptionEnabledCondition = () => { return showDescription; } }; public DialogGUIToggleButton toggle = new DialogGUIToggleButton(false, "show/hide", (b) => { showDescription = b; }, 100f, 20f); When toggling label visibility, my popup window changes size as it should, but it also resets back to screen coordinates set at SpawnPopupDialog(). Can this be prevented ? I can manage repositioning the window to original coordinates, but is is way too visible and kind of ugly. For anyone interested, i used same EventTrigger() / AddListener() trick as with my jog-shuttle slider. Oh, and does anyone know how to stop ESC from closing the popup window ? I assume it can be done by intercepting ESC keypress event game-wide, though i'd like to target event stream just for my popup window.
  6. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    I'm starting to seriously doubt myself, i need sleep... Anyway i (mistakenly ?) thought that docking ports do not have as strong connection as editor-built parts. I tried looking at KAS sources to learn more but i am tired. I'll look at it later. Is there a somewhat comprehensive list of unity3d joint types mapped to KSP vanilla/modded parts ?
  7. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    Correct, but i used Konstrucion ports merely to demonstrate part of my idea, not as desired effect. I'll try to explain this ... EXAMPLE : Imagine this scenario where Konstruction ports were never invented, instead RoverDude made a plugin that allows player to arbitrarily connect any part to any vessel in flight. Player would be able to simply use its vessel in orbit to get close enough to derelict Mk1 Command Pod, right click on it and use a new option "Join to my vessel". The pod (and its Kerbal inhabitant) would be instanly added to ship. The newly attached pod would have no visible connection to player's vessel. Of course, this scenario makes no sense. But when you add a part such as Konstruction port, and a rule that docked craft can join to your vessel using only these ports, then the whole idea of joining craft/parts in flight starts to make sense. Same with my idea : join AND RELEASE parts in flight that are connected via Unity3D FixedJoint connection (this is used when building crafts in VAB/SPH), used via separate part that introduces this functionality into the game.
  8. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    Bingo! But with one important caveat : once connected, there is no way to separate joined parts. And as far as game logic regarding Konstruction ports is concerned, this is ok. What i am proposing is a feature that allows the same part connection that is reverisble (ie a part can be detached). Problem is how and when to determine which part needs to be disconnected. In KAS, this is achieved by letting player target the part using an ingame-avatar representation of himeslf/herself in game - a Kerbal Engineer on EVA using tools. If attempting to just plainly add option to every part that can be detached in flight without any representing entity, player will be confused by this inconsistency. Thus, there must be a separate part that will introduce such functionality with its rules that will make it consistent with rest of game logic. A sort of Konstrucion port with detach action. All above considering a detachable parts without Kerbal, a fully automatic operation. Same as not needing a Kerbal to dock/undock two vessels with docking ports.
  9. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    I'll try to reply with least stress/impact on work : i was asking if it would be possible to add part(s) that perform same function as Engineer on EVA. A type of port/connector (or better yet a pair of such ports) that can attach part to vessel in flight with connection that is of equal type and strength as any other part normally built in editor. Such connection would be autostrut/KJR compatible. If such connection would be implemented, it would not matter if (for example) a resource container was added in VAB/SPH or in flight. In both cases, resource container would be connected to vessel the same way. All without EVA Engineer, just parts and vessels. It could VISUALLY look like a docking port or a hooking clamp for securing payload. If this is added to KAS, it would enable "payload reintegration" to vessels for example a unmanned truck but without wobbling associated with claws, magnets or docking ports.
  10. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    A question about reintegrating payloads into craft's body/frame : is there a separate plugin from other addon or a functionality within KAS to dock/attach part to craft as it if was added in VAB/SPH, WITHOUT Engineer on EVA ? Something like weldable ports from Konstruction, but separable again ?
  11. fatcargo

    [1.5+] Kerbal Attachment System (KAS) v1.1

    Idea : synchronized winches. When dealing with bulky loads, it may be needed to perform winching operations from multiple points. But only simple start/stop/speed synchronization toggle from winch UI. Anything more complicated and it will turn into Infernal Robotics (which is, sadly, badly out of date). It may be possible to make a auto-sync function that allows player to manipulate loads by having all synced winches working as one (there is YT video about it, can't remember the link). In simpler version (one i recommend implementing, if this idea is accepted) player is responsible for positioning of winches. Lots of trial and error, but that's the name of the game.
  12. While i was reading your code i realized that i already worked with lambdas in other part of my plugin. I tried too hard to make all-in-one solution for anyone to use, in my case i actually won't have problems to using refs inside anonymous functions. As for adding more triggers/listeners, i added a if- getcomponent-return line to prevent just that
  13. Here is the newest iteration, i adapted code from this post on unity answers site public DialogGUISlider CreateJogShuttle() { bool isDragging = false; float val = 0f; return new DialogGUISlider( setValue: () => { return isDragging ? val : 5f; }, // this one. can't set any ref or out parameters here min: 0f, max: 10f, wholeNumbers: true, width: 200, height: 10, setCallback: (float f) => { val = f; }) { OnUpdate = () => { //commented out for testing purposes //test_slider.OnUpdate = () => { }; //longwinded but reliable, good enough for my tests if (test_slider.slider.GetComponent<EventTrigger>()?.triggers.Exists(e => e.eventID == EventTriggerType.BeginDrag || e.eventID == EventTriggerType.EndDrag) ?? false) return; EventTrigger sliderEvent = test_slider.slider.gameObject.AddComponent<EventTrigger>(); EventTrigger.Entry sliderOnEndDrag = new EventTrigger.Entry(); sliderOnEndDrag.eventID = EventTriggerType.EndDrag; sliderOnEndDrag.callback.AddListener((e) => { Debug.Log("OnEndDrag\n"); isDragging = false; }); sliderEvent.triggers.Add(sliderOnEndDrag); EventTrigger.Entry sliderOnBegindDrag = new EventTrigger.Entry(); sliderOnBegindDrag.eventID = EventTriggerType.BeginDrag; sliderOnBegindDrag.callback.AddListener((e) => { Debug.Log("OnBeginDrag\n"); isDragging = true; }); sliderEvent.triggers.Add(sliderOnBegindDrag); } }; } Note that "isDragging" is updated by two event handlers, so it properly resets slider thumb. Also i used "slider" field instead of "uItem". @xEvilReeperx thanks for the example, i'll give it a try.
  14. Not-so-good-news-update : i made some more tests and used different approach. I want to make a function to create a jog-shuttle type of slider. I need test how it performs when called multiple times because it uses some local variables (it should be ok), but i also wanted to bind output value of slider to a float variable passed by reference, so the whole thing stays neatly inside a single function. Guess what ? "out" and "ref" variables can't be used inside anonymous functions, which is how all callbacks are implemented for slider events. Sigh... what now ? I'll keep picking at this but feel free to chime in. If all else fails, after i post the example code, whoever wants to use it will have to use class-level variables in callbacks to get usable values ... yuck. Read this stackoverflow post if insterested.
  15. @maja : Thanks for the example code ! It helped me construct a working example. uItem field is indeed the right way to obtain handle to GameObject for DialogGUI element, i just used it wrong. NOTE : uItem field appears to be valid only after PopupDialog.SpawnPopupDialog() is called. Kudos for clever way of disabling further calls to Update by assigning an empty code block. However, i noticed that when calling PopupDialog.SpawnPopupDialog() multiple times, DialogGUISlider.Update() stays disabled (so for anyone trying this - if there is a problem with code not running correctly, this may be one of the reasons). @DMagic : Thanks for the idea to call default events from my custom handlers, though i still have to check logic and flow of code. Example code is working i just need to make a more robust solution. Namely, there will be multiple sliders and i need to know which one fired the event. PointerEventData contains source object fields that i can use, but not all fields are valid for all types of events, some of them are null (i've got a few NREs when experimenting). Once i clean the code i'll post what i did.