Skip to content

Commit e5da314

Browse files
authored
[ME,DBE] Prevent dragging window off-screen (#373)
1 parent 59e6108 commit e5da314

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

src/DynamicBoneEditor.Core/DynamicBoneEditor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using KKAPI.Maker;
99
using MessagePack;
1010
using System.Collections.Generic;
11+
using BepInEx.Configuration;
1112
#if AI || HS2
1213
using AIChara;
1314
#endif
@@ -29,12 +30,16 @@ public class Plugin : BaseUnityPlugin
2930
internal static new ManualLogSource Logger;
3031
internal static Plugin PluginInstance;
3132

33+
public static ConfigEntry<bool> PreventDragout {get; set;}
34+
3235
private void Start()
3336
{
3437
Logger = base.Logger;
3538
PluginInstance = this;
3639
Harmony.CreateAndPatchAll(typeof(Hooks));
3740

41+
PreventDragout = Config.Bind("Config", "Prevent Window Dragout", true, "Prevent dragging the DBE window outside of the game window (Requires restart to apply!)");
42+
3843
MakerAPI.MakerBaseLoaded += MakerAPI_MakerBaseLoaded;
3944
MakerAPI.MakerFinishedLoading += MakerAPI_MakerFinishedLoading;
4045
MakerAPI.MakerExiting += MakerAPI_MakerExiting;

src/DynamicBoneEditor.Core/UI/DynamicBoneEditor.UI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void InitUI()
9090
DragPanel = UIUtility.CreatePanel("Draggable", EditorMainPanel.transform);
9191
DragPanel.transform.SetRect(0f, 1f, 1f, 1f, 0f, -HeaderSize);
9292
DragPanel.color = Color.gray;
93-
UIUtility.MakeObjectDraggable(DragPanel.rectTransform, EditorMainPanel.rectTransform);
93+
UIUtility.MakeObjectDraggable(DragPanel.rectTransform, EditorMainPanel.rectTransform, PreventDragout.Value);
9494

9595
var nametext = UIUtility.CreateText("Nametext", DragPanel.transform, "Dynamic Bone Editor");
9696
nametext.transform.SetRect();

src/MaterialEditor.Base/PluginBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public partial class MaterialEditorPluginBase : BaseUnityPlugin
7979
/// </summary>
8080
public static ConfigEntry<float> DragSensitivity { get; set; }
8181
/// <summary>
82+
/// Prevent dragging the ME window outside of the game window
83+
/// </summary>
84+
public static ConfigEntry<bool> PreventDragout { get; set; }
85+
/// <summary>
8286
/// Configuration entry for watching for file changes and reloading textures on change
8387
/// </summary>
8488
public static ConfigEntry<bool> WatchTexChanges { get; set; }
@@ -161,6 +165,7 @@ public virtual void Awake()
161165
UIHeight = Config.Bind("Config", "UI Height", 0.3f, new ConfigDescription("Controls the size of the window.", new AcceptableValueRange<float>(0f, 1f), new ConfigurationManagerAttributes { Order = 5, ShowRangeAsPercent = false }));
162166
UIListWidth = Config.Bind("Config", "UI List Width", 180f, new ConfigDescription("Controls width of the renderer/materials lists to the side of the window", new AcceptableValueRange<float>(100f, 500f), new ConfigurationManagerAttributes { Order = 4, ShowRangeAsPercent = false }));
163167
DragSensitivity = Config.Bind("Config", "Drag Sensitivity", 30f, new ConfigDescription("Controls the sensitivity of dragging labels to edit float values", new AcceptableValueRange<float>(1f, 100f), new ConfigurationManagerAttributes { Order = 3, ShowRangeAsPercent = false }));
168+
PreventDragout = Config.Bind("Config", "Prevent Window Dragout", true, "Prevent dragging the ME window outside of the game window (Requires restart to apply!)");
164169
WatchTexChanges = Config.Bind("Config", "Watch File Changes", true, new ConfigDescription("Watch for file changes and reload textures on change. Can be toggled in the UI.", null, new ConfigurationManagerAttributes { Order = 2 }));
165170
ShaderOptimization = Config.Bind("Config", "Shader Optimization", true, new ConfigDescription("Replaces every loaded shader with the MaterialEditor copy of the shader. Reduces the number of copies of shaders loaded which reduces RAM usage and improves performance.", null, new ConfigurationManagerAttributes { Order = 1 }));
166171
ExportBakedMesh = Config.Bind("Config", "Export Baked Mesh", false, new ConfigDescription("When enabled, skinned meshes will be exported in their current state with all customization applied as well as in the current pose.", null, new ConfigurationManagerAttributes { Order = 1 }));

src/MaterialEditor.Base/UI/UI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected void InitUI()
141141
DragPanel = UIUtility.CreatePanel("Draggable", MaterialEditorMainPanel.transform);
142142
DragPanel.transform.SetRect(0f, 1f, 1f, 1f, 0f, -HeaderSize);
143143
DragPanel.color = Color.gray;
144-
UIUtility.MakeObjectDraggable(DragPanel.rectTransform, MaterialEditorMainPanel.rectTransform);
144+
UIUtility.MakeObjectDraggable(DragPanel.rectTransform, MaterialEditorMainPanel.rectTransform, PreventDragout.Value);
145145

146146
var nametext = UIUtility.CreateText("Nametext", DragPanel.transform, "Material Editor");
147147
nametext.transform.SetRect();

src/UIUtility/MovableWindow.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class MovableWindow : UIBehaviour, IPointerDownHandler, IDragHandler, I
1515
public event Action<PointerEventData> OnPointerUpEvent;
1616

1717
public RectTransform ToDrag;
18+
public bool PreventDragout;
1819

1920
public void OnPointerDown(PointerEventData eventData)
2021
{
@@ -28,7 +29,15 @@ public void OnDrag(PointerEventData eventData)
2829
{
2930
if (PointerDownCalled == false)
3031
return;
31-
ToDrag.position = CachedDragPosition + ((Vector2)Input.mousePosition - CachedMousePosition);
32+
Vector3 newPos = CachedDragPosition + ((Vector2)Input.mousePosition - CachedMousePosition);
33+
if (PreventDragout)
34+
{
35+
float height = Screen.height * (ToDrag.anchorMax.y - ToDrag.anchorMin.y) + ToDrag.sizeDelta.y;
36+
float newX = Mathf.Clamp(newPos.x, 0, Screen.width);
37+
float newY = Mathf.Clamp(newPos.y, height / 2, Screen.height - height / 2);
38+
newPos = new Vector3(newX, newY, newPos.z);
39+
}
40+
ToDrag.position = newPos;
3241
OnDragEvent?.Invoke(eventData);
3342
}
3443

src/UIUtility/UIUtility.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,11 @@ public static Image AddImageToObject(Transform t, Sprite sprite = null)
334334
return i;
335335
}
336336

337-
public static MovableWindow MakeObjectDraggable(RectTransform clickableDragZone, RectTransform draggableObject)
337+
public static MovableWindow MakeObjectDraggable(RectTransform clickableDragZone, RectTransform draggableObject, bool preventDragout = false)
338338
{
339339
MovableWindow mv = clickableDragZone.gameObject.AddComponent<MovableWindow>();
340340
mv.ToDrag = draggableObject;
341+
mv.PreventDragout = preventDragout;
341342
return mv;
342343
}
343344
}

0 commit comments

Comments
 (0)