Parent / child configs

This commit is contained in:
Scott Burns
2019-05-20 18:59:36 +02:00
parent f4ae6fcc5e
commit f8c639d185
4 changed files with 160 additions and 8 deletions

View File

@@ -49,4 +49,5 @@ public @interface ConfigItem
String group() default "";
String parent() default "";
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2018, Craftiii4 <craftiii4@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.config;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
public class ConfigPanelItem
{
@Getter(AccessLevel.PUBLIC)
private ConfigPanelItem parent;
@Getter(AccessLevel.PUBLIC)
private List<ConfigPanelItem> children;
@Getter(AccessLevel.PUBLIC)
private ConfigItemDescriptor item;
public ConfigPanelItem(ConfigPanelItem parent, ConfigItemDescriptor item)
{
this.parent = parent;
this.children = new ArrayList<>();
this.item = item;
}
public List<ConfigPanelItem> getItemsAsList()
{
List<ConfigPanelItem> items = new ArrayList<>();
items.add(this);
for (ConfigPanelItem child : children)
{
items.addAll(child.getItemsAsList());
}
return items;
}
public int getDepth()
{
return (parent == null ? 0 : parent.getDepth() + 1);
}
public boolean addChildIfMatchParent(ConfigItemDescriptor cid)
{
if (item != null && item.getItem().keyName().equals(cid.getItem().parent()))
{
children.add(new ConfigPanelItem(this, cid));
return true;
}
else
{
for (ConfigPanelItem child : children)
{
if (child.addChildIfMatchParent(cid))
{
return true;
}
}
return false;
}
}
}

View File

@@ -0,0 +1,5 @@
package net.runelite.client.config;
public class Stub
{
}

View File

@@ -75,9 +75,10 @@ import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigDescriptor;
import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem;
import net.runelite.client.config.ConfigItemsGroup;
import net.runelite.client.config.ConfigItemDescriptor;
import net.runelite.client.config.ConfigItemsGroup;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.config.ConfigPanelItem;
import net.runelite.client.config.Keybind;
import net.runelite.client.config.ModifierlessKeybind;
import net.runelite.client.config.Range;
@@ -417,6 +418,10 @@ public class ConfigPanel extends PluginPanel
title.setToolTipText("<html>" + name + ":<br>" + listItem.getDescription() + "</html>");
topPanel.add(title);
ConfigPanelItem mainParent = new ConfigPanelItem(null, null);
List<ConfigPanelItem> parents = new ArrayList<>();
List<ConfigItemDescriptor> allItems = new ArrayList<>();
for (ConfigItemsGroup cig : cd.getItemGroups())
{
boolean collapsed = false;
@@ -430,11 +435,13 @@ public class ConfigPanel extends PluginPanel
JLabel headerLabel = new JLabel(header);
headerLabel.setForeground(Color.ORANGE);
headerLabel.setPreferredSize(new Dimension(PANEL_WIDTH, (int)headerLabel.getPreferredSize().getHeight()));
headerLabel.setPreferredSize(new Dimension(PANEL_WIDTH, (int) headerLabel.getPreferredSize().getHeight()));
String sCollapsed = configManager.getConfiguration(cd.getGroup().value(), cig.getGroup() + "_collapse");
if (sCollapsed != null)
{
collapsed = Boolean.parseBoolean(sCollapsed);
}
JButton collapse = new JButton(collapsed ? "+" : "-");
collapse.setPreferredSize(new Dimension(20, 20));
@@ -451,21 +458,65 @@ public class ConfigPanel extends PluginPanel
}
if (collapsed)
continue;
for (ConfigItemDescriptor cid : cig.getItems())
{
if (cid.getItem().hidden())
continue;
}
allItems.addAll(cig.getItems());
int maxDepth = 3;
do
{
for (ConfigItemDescriptor cid : new ArrayList<>(allItems))
{
continue;
String parent = cid.getItem().parent();
if (parent.equals(""))
{
mainParent.getChildren().add(new ConfigPanelItem(mainParent, cid));
allItems.remove(cid);
}
else
{
if (mainParent.addChildIfMatchParent(cid))
{
allItems.remove(cid);
}
}
}
maxDepth--;
} while (allItems.size() > 0 && maxDepth > 0);
List<ConfigPanelItem> orderedList = mainParent.getItemsAsList();
for (ConfigPanelItem cpi : orderedList)
{
ConfigItemDescriptor cid = cpi.getItem();
if (cid == null)
{
continue; // Ignore main 'parent'
}
JPanel item = new JPanel();
item.setLayout(new BorderLayout());
item.setMinimumSize(new Dimension(PANEL_WIDTH, 0));
name = cid.getItem().name();
StringBuilder depthOffset = new StringBuilder();
for (int depth = 1; depth < cpi.getDepth(); depth++)
{
depthOffset.append(" ");
}
name = depthOffset + name;
JLabel configEntryName = new JLabel(name);
configEntryName.setPreferredSize(new Dimension(PANEL_WIDTH, (int)configEntryName.getPreferredSize().getHeight()));
configEntryName.setPreferredSize(new Dimension(PANEL_WIDTH, (int) configEntryName.getPreferredSize().getHeight()));
configEntryName.setForeground(Color.WHITE);
configEntryName.setToolTipText("<html>" + name + ":<br>" + cid.getItem().description() + "</html>");
item.add(configEntryName, BorderLayout.CENTER);
@@ -505,7 +556,9 @@ public class ConfigPanel extends PluginPanel
{
configEntryName.setText(finalName.concat(": ").concat(String.valueOf(slider.getValue())));
if (!slider.getValueIsAdjusting())
{
changeConfiguration(listItem, config, slider, cd, cid);
}
}
);
item.add(slider, BorderLayout.EAST);
@@ -726,7 +779,9 @@ public class ConfigPanel extends PluginPanel
boolean collapse = true;
if (sCollapsed != null)
{
collapse = !Boolean.parseBoolean(sCollapsed);
}
configManager.setConfiguration(cd.getGroup().value(), cig.getGroup() + "_collapse", collapse);
openGroupConfigPanel(listItem, config, cd);