Merge pull request #326 from sdburns1998/Config-depth

Config depth
This commit is contained in:
Tyler Bochard
2019-05-20 14:08:30 -04:00
committed by GitHub
4 changed files with 196 additions and 3 deletions

View File

@@ -49,4 +49,7 @@ public @interface ConfigItem
String group() default "";
String unhide() 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

@@ -64,6 +64,8 @@ import javax.swing.ScrollPaneConstants;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
@@ -78,10 +80,12 @@ import net.runelite.client.config.ConfigItem;
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;
import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.config.Stub;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginInstantiationException;
@@ -417,6 +421,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;
@@ -457,23 +465,97 @@ public class ConfigPanel extends PluginPanel
continue;
}
for (ConfigItemDescriptor cid : cig.getItems())
allItems.addAll(cig.getItems());
int maxDepth = 3;
do
{
for (ConfigItemDescriptor cid : new ArrayList<>(allItems))
{
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'
}
if (cid.getItem().hidden())
{
continue;
boolean show = false;
String unhideat = cid.getItem().unhide();
for (ConfigItemDescriptor cid2 : cd.getItems())
{
if (cid2.getItem().keyName().equals(unhideat))
{
if (cid2.getType() == boolean.class)
{
show = Boolean.parseBoolean(configManager.getConfiguration(cd.getGroup().value(), cid2.getItem().keyName()));
}
}
}
if (!show)
{
continue;
}
}
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.setForeground(Color.WHITE);
configEntryName.setToolTipText("<html>" + name + ":<br>" + cid.getItem().description() + "</html>");
item.add(configEntryName, BorderLayout.CENTER);
if (cid.getType() == Stub.class)
{
Border border = item.getBorder();
Border margin = new EmptyBorder(10, 0, 0, 0);
item.setBorder(new CompoundBorder(border, margin));
configEntryName.setForeground(Color.ORANGE);
}
if (cid.getType() == boolean.class)
{
JCheckBox checkbox = new JCheckBox();
@@ -503,7 +585,7 @@ public class ConfigPanel extends PluginPanel
{
JSlider slider = new JSlider(min, max, value);
configEntryName.setText(name.concat(": ").concat(String.valueOf(slider.getValue())));
slider.setPreferredSize(new Dimension(100, 25));
slider.setPreferredSize(new Dimension(85, 25));
String finalName = name;
slider.addChangeListener((l) ->
{
@@ -762,6 +844,18 @@ public class ConfigPanel extends PluginPanel
{
JCheckBox checkbox = (JCheckBox) component;
configManager.setConfiguration(cd.getGroup().value(), cid.getItem().keyName(), "" + checkbox.isSelected());
for (ConfigItemDescriptor cid2 : cd.getItems())
{
if (cid2.getItem().hidden())
{
if (cid2.getItem().unhide().equals(cid.getItem().keyName()))
{ // If another options visibility changes depending on the value of this checkbox, then render the entire menu again
openGroupConfigPanel(listItem, config, cd);
return;
}
}
}
}
else if (component instanceof JSpinner)
{