Skip to content

Commit dab46b0

Browse files
committed
Added focus-follows-mouse logic to kOS for CTB
1 parent e373d02 commit dab46b0

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,20 @@ Setting Up Your Environment
215215

216216
3. Make sure your installation of KSP has LinuxGuruGamer's ClivkThroughBlocker
217217
mod installed. kOS now needs it in order to compile. After it is
218-
installed, create a reference for it in your kOS project's References
219-
like so:
218+
installed, copy its DLL file into Resources/ from your GameData folder.
219+
220+
Copy This file:
220221

221222
* $KSP/GameData/00_ClickThroughBlocker/Plugins/ClickThroughBlocker.dll
222223

224+
To here:
225+
226+
* Resources/
227+
228+
Then *make a Reference to Resoruces/ClickThroughBlocker.dll in your kOS project
229+
file*. This is needed for it to let you compile parts of kOS that use classes
230+
from ClickThroughBlocker.
231+
223232
4. If you do not have a copy of KSP locally, you may
224233
download dummy assemblies at https://github.com/KSP-KOS/KSP_LIB
225234

src/kOS/Screen/KOSManagedWindow.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/kOS/kOS.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
<Reference Include="Assembly-CSharp-firstpass">
3838
<HintPath>..\..\Resources\Assembly-CSharp-firstpass.dll</HintPath>
3939
</Reference>
40-
<Reference Include="ClickThroughBlocker">
40+
<Reference Include="ClickThroughBlocker, Version=0.1.9.5, Culture=neutral, processorArchitecture=MSIL">
41+
<SpecificVersion>False</SpecificVersion>
4142
<HintPath>..\..\Resources\ClickThroughBlocker.dll</HintPath>
4243
</Reference>
4344
<Reference Include="ICSharpCode.SharpZipLib">

0 commit comments

Comments
 (0)