Skip to content

Commit

Permalink
Can now drag the window by its edges; position saves
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Oct 16, 2020
1 parent 0621227 commit 0edb758
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 23 deletions.
21 changes: 14 additions & 7 deletions Shared_QuickAccessBox/QuickAccessBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public partial class QuickAccessBox : BaseUnityPlugin
private const string DESCRIPTION_MANUALADJUST = "After spawning the object, generator will wait for you to adjust the camera position to get the object in " +
"the middle of the screen. Press left Shift to accept and move to the next item.";
private const string DESCRIPTION_THUMBDIR = "Directory to save newly generated thumbs into. The directory must exist. Existing thumbs are not overwritten, remove them to re-create.";
private const string DESCRIPTION_WINPOS = "Position at which the search window first opens. Can be changed by dragging the edges of the window.";

public static ConfigEntry<KeyboardShortcut> KeyShowBox { get; private set; }
public static ConfigEntry<bool> SearchDeveloperInfo { get; private set; }
Expand All @@ -48,6 +49,8 @@ public partial class QuickAccessBox : BaseUnityPlugin
public static ConfigEntry<bool> ThumbManualAdjust { get; private set; }
public static ConfigEntry<int> RecentsCount { get; private set; }

public static ConfigEntry<Vector2> WindowPosition { get; private set; }

[Browsable(false)]
public bool ShowBox
{
Expand All @@ -64,22 +67,26 @@ private void Start()
{
Logger = base.Logger;

var advanced = new ConfigurationManagerAttributes { IsAdvanced = true };

KeyShowBox = Config.Bind("General", "Show quick access box", new KeyboardShortcut(KeyCode.Space, KeyCode.LeftControl), "Toggles the item search box on and off.");
RecentsCount = Config.Bind("General", "Number of recents to remember", 20, new ConfigDescription(DESCRIPTION_RECENTS, new AcceptableValueRange<int>(0, 200)));
SearchDeveloperInfo = Config.Bind("General", "Search developer information", false, new ConfigDescription(DESCRIPTION_DEVINFO, null, new ConfigurationManagerAttributes { IsAdvanced = true }));
SearchDeveloperInfo = Config.Bind("General", "Search developer information", false, new ConfigDescription(DESCRIPTION_DEVINFO, null, advanced));

ThumbGenerateKey = Config.Bind("Thumbnail generation", "Generate item thumbnails", new KeyboardShortcut(), new ConfigDescription(DESCRIPTION_THUMBGENKEY, null, new ConfigurationManagerAttributes { IsAdvanced = true }));
ThumbManualAdjust = Config.Bind("Thumbnail generation", "Manual mode", false, new ConfigDescription(DESCRIPTION_MANUALADJUST, null, new ConfigurationManagerAttributes { IsAdvanced = true }));
ThumbDarkBackground = Config.Bind("Thumbnail generation", "Dark background", false, new ConfigDescription(DESCRIPTION_DARKBG, null, new ConfigurationManagerAttributes { IsAdvanced = true }));
ThumbStoreLocation = Config.Bind("Thumbnail generation", "Output directory", string.Empty, new ConfigDescription(DESCRIPTION_THUMBDIR, null, new ConfigurationManagerAttributes { IsAdvanced = true }));
ThumbGenerateKey = Config.Bind("Thumbnail generation", "Generate item thumbnails", new KeyboardShortcut(), new ConfigDescription(DESCRIPTION_THUMBGENKEY, null, advanced));
ThumbManualAdjust = Config.Bind("Thumbnail generation", "Manual mode", false, new ConfigDescription(DESCRIPTION_MANUALADJUST, null, advanced));
ThumbDarkBackground = Config.Bind("Thumbnail generation", "Dark background", false, new ConfigDescription(DESCRIPTION_DARKBG, null, advanced));
ThumbStoreLocation = Config.Bind("Thumbnail generation", "Output directory", string.Empty, new ConfigDescription(DESCRIPTION_THUMBDIR, null, advanced));

WindowPosition = Config.Bind("General", "Initial window position", Vector2.zero, new ConfigDescription(DESCRIPTION_WINPOS, null, new ConfigurationManagerAttributes { Browsable = false }));

StartCoroutine(LoadingCo());
}

private void OnDestroy()
{
//_interface?.Dispose();
//ThumbnailLoader.Dispose();
_interface?.Dispose();
ThumbnailLoader.Dispose();
ItemInfoLoader.Dispose();
//SaveRecents();
}
Expand Down
1 change: 1 addition & 0 deletions Shared_QuickAccessBox/Shared_QuickAccessBox.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Thumbs\ThumbnailCache.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TranslationCacheEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TranslationHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UI\MovableWindow.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UI\InterfaceManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UI\SimpleListEntry.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UI\SimpleVirtualList.cs" />
Expand Down
19 changes: 11 additions & 8 deletions Shared_QuickAccessBox/Thumbs/ThumbnailCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using BepInEx;
using UnityEngine;
using Object = UnityEngine.Object;

namespace KK_QuickAccessBox.Thumbs
{
Expand Down Expand Up @@ -61,14 +62,16 @@ public static void LoadAssetBundle()
_thumbSound = thumbSound.ToSprite();
}

//public static void Dispose()
//{
// foreach (var thumb in _thumbnailCache.Values)
// UnityEngine.Object.Destroy(thumb);
//
// _thumbnailCache.Clear();
// _pngNameCache = null;
//}
public static void Dispose()
{
#if DEBUG
foreach (var thumb in _thumbnailCache.Values)
Object.Destroy(thumb);

_thumbnailCache.Clear();
_pngNameCache = null;
#endif
}

public static bool CustomThumbnailAvailable(ItemInfo itemInfo)
{
Expand Down
34 changes: 26 additions & 8 deletions Shared_QuickAccessBox/UI/InterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace KK_QuickAccessBox.UI
internal class InterfaceManager
{
private readonly GameObject _canvasRoot;
private readonly RectTransform _windowRootRt;
private readonly InputField _inputField;

private readonly SimpleVirtualList _simpleVirtualList;
Expand All @@ -31,6 +32,20 @@ public InterfaceManager(Action<ItemInfo> onClicked, Action<string> onSearchStrin
{
_canvasRoot = CreateCanvas();

_windowRootRt = _canvasRoot.GetComponentInChildren<Image>().GetComponent<RectTransform>();
var savedPos = QuickAccessBox.WindowPosition.Value;
if (savedPos != Vector2.zero)
{
var sd = _windowRootRt.sizeDelta;
// Prevent window from getting stuck off-screen
if (savedPos.x > 0 && savedPos.x + sd.x < 1920 && -savedPos.y > 0 && savedPos.y + sd.y < 1080)
{
_windowRootRt.offsetMin = savedPos;
_windowRootRt.offsetMax = savedPos + sd;
}
}
MovableWindow.MakeObjectDraggable(_windowRootRt, _windowRootRt, false);

_inputField = _canvasRoot.transform.FindLoop("InputField").GetComponent<InputField>() ?? throw new ArgumentNullException(nameof(_inputField));
_inputField.onValueChanged.AddListener(new UnityAction<string>(onSearchStringChanged));
_inputField.textComponent.MarkXuaIgnored();
Expand All @@ -55,13 +70,16 @@ public InterfaceManager(Action<ItemInfo> onClicked, Action<string> onSearchStrin
CreateSearchToolbarButton();
}

//public void Dispose()
//{
// _simpleVirtualList.Clear();
// Object.Destroy(_canvasRoot);
// Object.Destroy(_searchMenuButton);
// Object.Destroy(_searchToolbarButton);
//}
public void Dispose()
{
QuickAccessBox.WindowPosition.Value = _windowRootRt.offsetMin;
#if DEBUG
_simpleVirtualList.Clear();
Object.Destroy(_canvasRoot);
Object.Destroy(_searchMenuButton);
Object.Destroy(_searchToolbarButton);
#endif
}

public void SelectSearchBox()
{
Expand Down Expand Up @@ -113,7 +131,7 @@ public bool Visible
{
SelectSearchBox();

if(string.IsNullOrEmpty(_inputField.text))
if (string.IsNullOrEmpty(_inputField.text))
_inputField.onValueChanged.Invoke("");
}

Expand Down
67 changes: 67 additions & 0 deletions Shared_QuickAccessBox/UI/MovableWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace KK_QuickAccessBox.UI
{
internal class MovableWindow : UIBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public static MovableWindow MakeObjectDraggable(RectTransform clickableDragZone, RectTransform draggableObject, bool preventCameraControl = true)
{
MovableWindow mv = clickableDragZone.gameObject.AddComponent<MovableWindow>();
mv.toDrag = draggableObject;
mv.preventCameraControl = preventCameraControl;
return mv;
}

private Vector2 _cachedDragPosition;
private Vector2 _cachedMousePosition;
private bool _pointerDownCalled;
private BaseCameraControl _cameraControl;
private BaseCameraControl.NoCtrlFunc _noControlFunctionCached;

public event Action<PointerEventData> onPointerDown;
public event Action<PointerEventData> onDrag;
public event Action<PointerEventData> onPointerUp;

public RectTransform toDrag;
public bool preventCameraControl;

protected override void Awake()
{
base.Awake();
_cameraControl = FindObjectOfType<BaseCameraControl>();
}

public void OnPointerDown(PointerEventData eventData)
{
if (preventCameraControl && _cameraControl)
{
_noControlFunctionCached = _cameraControl.NoCtrlCondition;
_cameraControl.NoCtrlCondition = () => true;
}
_pointerDownCalled = true;
_cachedDragPosition = toDrag.position;
_cachedMousePosition = Input.mousePosition;
onPointerDown?.Invoke(eventData);
}

public void OnDrag(PointerEventData eventData)
{
if (_pointerDownCalled == false)
return;
toDrag.position = _cachedDragPosition + ((Vector2)Input.mousePosition - _cachedMousePosition);
onDrag?.Invoke(eventData);
}

public void OnPointerUp(PointerEventData eventData)
{
if (_pointerDownCalled == false)
return;
if (preventCameraControl && _cameraControl)
_cameraControl.NoCtrlCondition = _noControlFunctionCached;
_pointerDownCalled = false;
onPointerUp?.Invoke(eventData);
}
}
}

0 comments on commit 0edb758

Please sign in to comment.