Skip to content

Commit

Permalink
Accessory window scrolling + whole numbers in maker slider (#27)
Browse files Browse the repository at this point in the history
* Scrolling feature with workaround

uncertain how scrolling affects newSlotTransform as the unhooked controls are not removed nor are the correct ones applied

* add whole number functionality

* Update MakerSlider.cs

* Revert "Update MakerSlider.cs"

This reverts commit 356185a.

* Revert "add whole number functionality"

This reverts commit 06535ed.

* Made separate Slider for whole number support

* really dumb of me

* Fixed Makeshift

* forgot to add IF KK incase other games don't like it
  • Loading branch information
jalil49 committed Apr 21, 2021
1 parent 2dcdf7a commit 4c49815
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 65 deletions.
145 changes: 87 additions & 58 deletions src/Shared.KKalike/Maker/MakerInterfaceCreator.KK.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using ChaCustom;
using IllusionUtility.GetUtility;
using KKAPI.Maker.UI;
using KKAPI.Maker.UI.Sidebar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using BepInEx;
using ChaCustom;
using IllusionUtility.GetUtility;
using KKAPI.Maker.UI;
using KKAPI.Maker.UI.Sidebar;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
Expand Down Expand Up @@ -263,63 +262,59 @@ private static void CreateCustomAccessoryWindowControls()
text.Cast<Transform>().First().gameObject.SetActive(false);
}
}

CreateCustomControlsInSubCategory(slotTransform, _accessoryWindowEntries);

// todo unfinished scrolling of accessory windows, tell Keel to finish this
//var listParent = slotTransform.Cast<Transform>().Where(x => x.name.EndsWith("Top")).First();
//var elements = new List<Transform>();
//foreach(Transform t in listParent)
// elements.Add(t);
//
//var fitter = listParent.GetComponent<ContentSizeFitter>();
//fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
//fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
//
//var go = DefaultControls.CreateScrollView(new DefaultControls.Resources());
//go.name = $"{slotTransform.name}ScrollView";
//go.transform.SetParent(listParent.transform, false);
//
//var scroll = go.GetComponent<ScrollRect>();
//scroll.horizontal = false;
//scroll.scrollSensitivity = 40f;
//scroll.movementType = ScrollRect.MovementType.Clamped;
//scroll.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
//scroll.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
//GameObject.DestroyImmediate(scroll.GetComponent<Image>());
//GameObject.DestroyImmediate(scroll.horizontalScrollbar.gameObject);
//GameObject.DestroyImmediate(scroll.verticalScrollbar.gameObject);
//
//var le = scroll.gameObject.AddComponent<LayoutElement>();
//var height = (GameObject.Find("CustomScene/CustomRoot/FrontUIGroup/CustomUIGroup/CvsMenuTree/04_AccessoryTop/Slots").transform as RectTransform).rect.height;
//le.preferredHeight = height;
//le.preferredWidth = 360f;
//
//var vlg = scroll.content.gameObject.AddComponent<VerticalLayoutGroup>();
//vlg.childControlWidth = true;
//vlg.childControlHeight = true;
//vlg.childForceExpandWidth = true;
//vlg.childForceExpandHeight = false;
//
//scroll.content.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
//
//foreach(var item in elements)
// item.SetParent(scroll.content);
//
//public static void SetRect(this Transform self, float anchorLeft = 0f, float anchorBottom = 0f, float anchorRight = 1f, float anchorTop = 1f, float offsetLeft = 0f, float offsetBottom = 0f, float offsetRight = 0f, float offsetTop = 0f)
//{
// RectTransform rt = self as RectTransform;
// rt.anchorMin = new Vector2(anchorLeft, anchorBottom);
// rt.anchorMax = new Vector2(anchorRight, anchorTop);
// rt.offsetMin = new Vector2(offsetLeft, offsetBottom);
// rt.offsetMax = new Vector2(offsetRight, offsetTop);
//}
#if KK
var listParent = slotTransform.Cast<Transform>().Where(x => x.name.EndsWith("Top")).First();
var elements = new List<Transform>();
foreach (Transform t in listParent)
elements.Add(t);

var fitter = listParent.GetComponent<ContentSizeFitter>();
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;

var go = DefaultControls.CreateScrollView(new DefaultControls.Resources());
go.name = $"{slotTransform.name}ScrollView";
go.transform.SetParent(listParent.transform, false);

var scroll = go.GetComponent<ScrollRect>();
scroll.horizontal = false;
scroll.scrollSensitivity = 40f;
scroll.movementType = ScrollRect.MovementType.Clamped;
scroll.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
scroll.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
GameObject.DestroyImmediate(scroll.GetComponent<Image>());
GameObject.DestroyImmediate(scroll.horizontalScrollbar.gameObject);
GameObject.DestroyImmediate(scroll.verticalScrollbar.gameObject);

var le = scroll.gameObject.AddComponent<LayoutElement>();
var height = (GameObject.Find("CustomScene/CustomRoot/FrontUIGroup/CustomUIGroup/CvsMenuTree/04_AccessoryTop/Slots").transform as RectTransform).rect.height;
le.preferredHeight = height;
le.preferredWidth = 360f;

var vlg = scroll.content.gameObject.AddComponent<VerticalLayoutGroup>();
vlg.childControlWidth = true;
vlg.childControlHeight = true;
vlg.childForceExpandWidth = true;
vlg.childForceExpandHeight = false;

scroll.content.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;

foreach (var item in elements)
item.SetParent(scroll.content);
slotTransform.SetParent(scroll.content);
CreateCustomControlsInSubCategory(listParent.parent.transform, _accessoryWindowEntries);

This comment has been minimized.

Copy link
@jalil49

jalil49 May 1, 2021

Author Contributor

forgotten duplicate line, needs to be removed not sure why it wasn't visible in Maker

#endif
}
}

internal static void OnMakerAccSlotAdded(Transform newSlotTransform)
{
// Necessary because MoreAccessories copies existing slot, so controls get copied but with no events hooked up
#if KK
//KeelsChildNeglect(newSlotTransform, 0); //used to print children paths in case it's needed in the future, like horizontal support or something
newSlotTransform = newSlotTransform.GetChild(1).GetChild(0).GetChild(0);
#endif
RemoveCustomControlsInSubCategory(newSlotTransform);

CreateCustomControlsInSubCategory(newSlotTransform, _accessoryWindowEntries);
Expand All @@ -328,9 +323,28 @@ internal static void OnMakerAccSlotAdded(Transform newSlotTransform)
private static void RemoveCustomControlsInSubCategory(Transform newSlotTransform)
{
var contentParent = FindSubcategoryContentParent(newSlotTransform);

//string children = "";
//for (int i = 0, n = contentParent.childCount; i < n; i++)
//{
// children += contentParent.GetChild(i).name;
// if (i == n - 2)
// {
// children += " and ";
// }
// else if (i == n - 1)
// {
// children += ".";
// }
// else
// {
// children += ", ";
// }
//}
//KoikatuAPI.Logger.LogWarning($"Content Parent is {contentParent.name}, has {contentParent.childCount} children, and child of {contentParent.parent} and is the parent of " + children);
foreach (var customControl in contentParent.Cast<Transform>().Where(x => x.name.EndsWith(BaseGuiEntry.GuiApiNameAppendix)))
{
Object.Destroy(customControl.gameObject);
}
}

internal static Transform FindSubcategoryContentParent(Transform categorySubTransform)
Expand Down Expand Up @@ -431,8 +445,23 @@ public static CoordinateLoadFlags GetCoordinateLoadFlags()

var accs = GameObject.Find("CustomScene/CustomRoot/FrontUIGroup/CustomUIGroup/CvsMenuTree/06_SystemTop/cosFileControl/charaFileWindow/WinRect/CoordinateLoad/Select/tglItem02")?.GetComponentInChildren<Toggle>(true);
if (accs == null) return null;

return new CoordinateLoadFlags { Clothes = clothes.isOn, Accessories = accs.isOn };
}

//private static void KeelsChildNeglect(Transform parent, int generation)
//{
// generation++;
// string tabs = "";
// for (int i = 0; i < generation; i++)
// {
// tabs += "\t";
// }
// for (int i = 0, n = parent.childCount; i < n; i++)
// {
// var child = parent.GetChild(i);
// KoikatuAPI.Logger.LogWarning(tabs + $"{child.name} (child {i} of {generation} generation) has {child.childCount} children; Parent is {child.parent}");
// KeelsChildNeglect(child, generation);
// }
//}
}
}
34 changes: 27 additions & 7 deletions src/Shared.KKalike/Maker/UI/MakerSlider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using BepInEx;
using BepInEx;
using System;
using TMPro;
using UniRx;
using UniRx.Triggers;
Expand Down Expand Up @@ -52,6 +52,11 @@ public MakerSlider(MakerCategory category, string settingName, float minValue, f
/// </summary>
public Func<float, string> ValueToString { get; set; }

/// <summary>
/// Use integers instead of floats
/// </summary>
public bool WholeNumbers { get; set; }

private static Transform SliderCopy
{
get
Expand Down Expand Up @@ -110,8 +115,9 @@ protected override GameObject OnCreateControl(Transform subCategoryList)
var slider = tr.Find("Slider").GetComponent<Slider>();
slider.minValue = _minValue;
slider.maxValue = _maxValue;
slider.wholeNumbers = WholeNumbers;
slider.onValueChanged.AddListener(SetValue);

slider.value = _defaultValue;
slider.GetComponent<ObservableScrollTrigger>()
.OnScrollAsObservable()
.Subscribe(
Expand All @@ -128,6 +134,12 @@ protected override GameObject OnCreateControl(Transform subCategoryList)

var inputField = tr.Find("InputField").GetComponent<TMP_InputField>();
if (MakerAPI.InsideMaker) Singleton<ChaCustom.CustomBase>.Instance.lstTmpInputField.Add(inputField);

if (WholeNumbers)
{
InputField(_defaultValue, inputField);
}

inputField.onEndEdit.AddListener(
txt =>
{
Expand All @@ -145,10 +157,7 @@ protected override GameObject OnCreateControl(Transform subCategoryList)
slider.onValueChanged.AddListener(
f =>
{
if (ValueToString != null)
inputField.text = ValueToString(f);
else
inputField.text = Mathf.RoundToInt(f * 100).ToString();
InputField(f, inputField);
});

var resetButton = tr.Find("Button").GetComponent<Button>();
Expand All @@ -158,5 +167,16 @@ protected override GameObject OnCreateControl(Transform subCategoryList)

return tr.gameObject;
}

private void InputField(float f, TMP_InputField inputField)
{
if (ValueToString != null)
inputField.text = ValueToString(f);
else if (WholeNumbers)
inputField.text = f.ToString();
else
inputField.text = Mathf.RoundToInt(f * 100).ToString();
}

}
}

0 comments on commit 4c49815

Please sign in to comment.