@@ -231,14 +231,13 @@ public bool IsOpen
231231 }
232232
233233 /// <summary>
234- /// Pass in the absolute GUI screen location of a mouse click to decide whether
235- /// or not this widget gets keyboard focus because of that click.
236- /// (Clicking outside the window takes focus away. Clicking inside
237- /// the window gives focus to the window and brings it to the front.)
238- /// </summary>
234+ /// Pass in the absolute GUI screen location of the mouse to decide whether
235+ /// or not this window gets keyboard focus because of that position.
236+ /// (If you want focus-follows-mouse logic, call this every Update(). If you
237+ /// want click-to-focus logic, only call this in Update()s where a click just happened.)
239238 /// <param name="absMousePos">Absolute position of mouse on whole screen</param>
240239 /// <returns>True if the window got focused, false if it didn't.</returns>
241- public bool FocusClickLocationCheck ( Vector2 absMousePos )
240+ public bool FocusMouseLocationCheck ( Vector2 absMousePos )
242241 {
243242 bool wasInside = false ;
244243 if ( IsInsideMyExposedPortion ( absMousePos ) )
@@ -295,42 +294,55 @@ public bool IsInsideMyExposedPortion(Vector2 posAbsolute)
295294
296295 /// <summary>
297296 /// When you subclass KOSManagedWindow, make sure that you call this
298- /// from inside your Update. It does not use OnGUI because of the fact
299- /// that the OnGUI event handler is broken - it only sends MouseDown
300- /// and MouseUp events when the mouse is OUTSIDE the window, which is
301- /// utterly backward, and it's hard to work out how to fix this,
302- /// given how badly documented the Unity GUI API is. If anyone who
303- /// actually understands the Unity GUI system wants to fix this,
304- /// please feel free to do so .
297+ /// from inside your Update() to check for focus change on the window.
298+ /// Calling this will maybe call GetFocus() or LoseFocus() depending on
299+ /// what the mouse is doing.
300+ /// Note, you call this during *Update()*, NOT the OnGUI() call.
301+ /// It does not use OnGUI() because the raw mousebutton state it
302+ /// needs to see can get consumed and wiped by Unity's IMGUI widgets
303+ /// before application code like this can see it .
305304 /// </summary>
306- /// <returns>True if there was a mouseclick within this window.</returns>
307- public bool UpdateLogic ( )
305+ public void UpdateLogic ( )
308306 {
309- if ( ! IsOpen ) return false ;
307+ if ( ! IsOpen ) return ;
310308
311309 // Input.mousePosition, unlike Event.current.MousePosition, puts the origin at the
312310 // lower-left instead of upper-left of the screen, thus the subtraction in the y coord below:
313311 mousePosAbsolute = new Vector2 ( Input . mousePosition . x , UnityEngine . Screen . height - Input . mousePosition . y ) ;
314312
315313 // Mouse coord within the window, rather than within the screen.
316314 mousePosRelative = new Vector2 ( mousePosAbsolute . x - windowRect . xMin , mousePosAbsolute . y - windowRect . yMin ) ;
317-
318- bool clickUp = false ;
315+
316+ // Could maybe cache the CustomParams call once up front to get a reference to the CTB instance, then only
317+ // repeat the ".focusFollowsclick" part each update. The reason that's not being done here is that I
318+ // noticed ClickThroughBlocker's OWN code always does it like this, and for all I know there might be
319+ // an important reason. It always gets this value by using the fully qualified long chain you see
320+ // here, starting from HighLogic, each update. :
321+ bool clickToFocus = HighLogic . CurrentGame . Parameters . CustomParams < ClickThroughFix . CTB > ( ) . focusFollowsclick ;
322+
319323 if ( Input . GetMouseButtonDown ( 0 ) )
320324 {
321325 mouseButtonDownPosAbsolute = mousePosAbsolute ;
322326 mouseButtonDownPosRelative = mousePosRelative ;
323327 }
324-
325- if ( Input . GetMouseButtonUp ( 0 ) )
328+
329+ bool mousePositionCanSetFocus = ! ( clickToFocus ) ; // Always true in focus-follows-mouse mode
330+
331+ if ( clickToFocus )
326332 {
327- clickUp = true ;
328- if ( Vector2 . Distance ( mousePosAbsolute , mouseButtonDownPosAbsolute ) <= dragTolerance )
333+ if ( Input . GetMouseButtonUp ( 0 ) )
329334 {
330- FocusClickLocationCheck ( mousePosAbsolute ) ;
335+ if ( Vector2 . Distance ( mousePosAbsolute , mouseButtonDownPosAbsolute ) <= dragTolerance )
336+ {
337+ mousePositionCanSetFocus = true ;
338+ }
331339 }
332340 }
333- return IsInsideMyExposedPortion ( mousePosAbsolute ) && clickUp ;
341+
342+ if ( mousePositionCanSetFocus )
343+ {
344+ FocusMouseLocationCheck ( mousePosAbsolute ) ;
345+ }
334346 }
335347 }
336348}
0 commit comments