Skip to content

Commit

Permalink
Allow multi row MakerRadioButtons by changing ColumnCount
Browse files Browse the repository at this point in the history
Based on #30, thanks to @jalil49 for help
  • Loading branch information
ManlyMarco committed May 16, 2021
1 parent 66fa631 commit 3a882ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
2 changes: 2 additions & 0 deletions doc/KKAPI.Maker.UI.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ Properties
| Type | Name | Summary |
| --- | --- | --- |
| `ReadOnlyCollection<Toggle>` | Buttons | Objects of all of the radio buttons |
| `Int32` | ColumnCount | How many buttons to show in a each row. By default show all buttons in a single row. If set to 3, 3 buttons will be shown in a row, then the next 3 buttons will be shown in a row below, and so on. |


Methods
Expand Down Expand Up @@ -295,6 +296,7 @@ Properties
| --- | --- | --- |
| `Func<String, Single>` | StringToValue | Custom converter from text in the textbox to the slider value. If not set, <code>float.Parse(txt) / 100f</code> is used. |
| `Func<Single, String>` | ValueToString | Custom converter from the slider value to what's displayed in the textbox. If not set, <code>Mathf.RoundToInt(f * 100).ToString()</code> is used. |
| `Boolean` | WholeNumbers | Use integers instead of floats |


Methods
Expand Down
4 changes: 4 additions & 0 deletions src/Shared.KKalike/Maker/MakerInterfaceCreator.KK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ private static void DebugControls()
.ValueChanged.Subscribe(b => KoikatuAPI.Logger.LogMessage(b));
AddControl(new MakerRadioButtons(cat, instance, "radio btns", "b1", "b2"))
.ValueChanged.Subscribe(b => KoikatuAPI.Logger.LogMessage(b));
AddControl(new MakerRadioButtons(cat, instance, "radio btns 1 row", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8"))
.ValueChanged.Subscribe(b => KoikatuAPI.Logger.LogMessage(b));
AddControl(new MakerRadioButtons(cat, instance, "radio btns 5 per row", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8") { ColumnCount = 5 })
.ValueChanged.Subscribe(b => KoikatuAPI.Logger.LogMessage(b));
AddControl(new MakerSlider(cat, "test slider", 0, 1, 1, instance))
.ValueChanged.Subscribe(b => KoikatuAPI.Logger.LogMessage(b));
AddControl(
Expand Down
49 changes: 35 additions & 14 deletions src/Shared.KKalike/Maker/UI/MakerRadioButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class MakerRadioButtons : BaseEditableGuiEntry<int>
/// </summary>
public ReadOnlyCollection<Toggle> Buttons { get; private set; }

/// <summary>
/// How many buttons to show in a each row. By default show all buttons in a single row.
/// If set to 3, 3 buttons will be shown in a row, then the next 3 buttons will be shown in a row below, and so on.
/// </summary>
public int ColumnCount { get; set; } = 9999;

/// <summary>
/// Create a new custom control. Create and register it in <see cref="MakerAPI.RegisterCustomSubCategories"/>.
/// </summary>
Expand Down Expand Up @@ -85,26 +91,41 @@ protected override GameObject OnCreateControl(Transform subCategoryList)
.ToList()
.AsReadOnly();

var singleToggleWidth = 280 / Buttons.Count;
for (var index = 0; index < Buttons.Count; index++)
// If there's less toggles than the number of toggles in a row, expand them to fill all space
var singleToggleWidth = 280 / Mathf.Min(Buttons.Count, ColumnCount);
var rowCount = Mathf.CeilToInt((float)Buttons.Count / ColumnCount);
tr.GetComponent<LayoutElement>().minHeight = 40 * rowCount;
var buttonIndex = 0;
for (var rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
var toggle = Buttons[index];
for (var columnIndex = 0; columnIndex < ColumnCount; columnIndex++)
{
if (buttonIndex >= Buttons.Count) break;
var toggle = Buttons[buttonIndex];

var rt = toggle.GetComponent<RectTransform>();
rt.offsetMin = new Vector2(singleToggleWidth * index - 280, 8);
rt.offsetMax = new Vector2(singleToggleWidth * (index + 1) - 280, -8);
var rt = toggle.GetComponent<RectTransform>();
rt.offsetMin = new Vector2(singleToggleWidth * columnIndex - 280, 8);
rt.offsetMax = new Vector2(singleToggleWidth * (columnIndex + 1) - 280, -8);

toggle.GetComponentInChildren<TextMeshProUGUI>().text = _buttons[index];
rt.anchorMax = new Vector2(1, 1 - rowIndex / (float)rowCount);
rt.anchorMin = new Vector2(1, 1 - (rowIndex + 1) / (float)rowCount);

var indexCopy = index;
toggle.onValueChanged.AddListener(
a =>
{
if (a || indexCopy == Value)
SetValue(indexCopy);
});
toggle.GetComponentInChildren<TextMeshProUGUI>().text = _buttons[buttonIndex];

var indexCopy = buttonIndex;
toggle.onValueChanged.AddListener(
a =>
{
if (a || indexCopy == Value)
SetValue(indexCopy);
});

buttonIndex++;
}
}

UnityEngine.Debug.Assert(buttonIndex >= Buttons.Count, "Didn't loop over all radio buttons, only " + buttonIndex);

BufferedValueChanged.Subscribe(
i =>
{
Expand Down

0 comments on commit 3a882ec

Please sign in to comment.