Merge pull request #5576 from deathbeam/skill-calc-proper-reset

Simplify checkbox checks in skill calc
This commit is contained in:
Tomas Slusny
2018-09-22 12:32:55 +02:00
committed by GitHub
2 changed files with 17 additions and 66 deletions

View File

@@ -1,38 +0,0 @@
/*
* Copyright (c) 2018, Robbie McLeod <https://github.com/rbbi>
* 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.plugins.skillcalculator;
import javax.swing.JCheckBox;
import lombok.Getter;
import lombok.Setter;
import net.runelite.client.plugins.skillcalculator.beans.SkillDataBonus;
class BonusCheckBox extends JCheckBox
{
@Getter
@Setter
private SkillDataBonus bonus;
}

View File

@@ -67,7 +67,7 @@ class SkillCalculator extends JPanel
private UICombinedActionSlot combinedActionSlot = new UICombinedActionSlot();
private ArrayList<UIActionSlot> combinedActionSlots = new ArrayList<>();
private final List<BonusCheckBox> bonusCheckBoxes = new ArrayList<>();
private final List<JCheckBox> bonusCheckBoxes = new ArrayList<>();
private int currentLevel = 1;
private int currentXP = Experience.getXpForLevel(currentLevel);
@@ -193,7 +193,7 @@ class SkillCalculator extends JPanel
{
JPanel uiOption = new JPanel(new BorderLayout());
JLabel uiLabel = new JLabel(bonus.getName());
BonusCheckBox uiCheckbox = new BonusCheckBox();
JCheckBox uiCheckbox = new JCheckBox();
uiLabel.setForeground(Color.WHITE);
uiLabel.setFont(FontManager.getRunescapeSmallFont());
@@ -202,43 +202,32 @@ class SkillCalculator extends JPanel
uiOption.setBackground(ColorScheme.DARKER_GRAY_COLOR);
// Adjust XP bonus depending on check-state of the boxes.
uiCheckbox.addActionListener(event -> adjustCheckboxes(uiCheckbox));
uiCheckbox.addActionListener(event -> adjustCheckboxes(uiCheckbox, bonus));
uiCheckbox.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
uiOption.add(uiLabel, BorderLayout.WEST);
uiOption.add(uiCheckbox, BorderLayout.EAST);
uiCheckbox.setBonus(bonus);
bonusCheckBoxes.add(uiCheckbox);
return uiOption;
}
private void adjustCheckboxes(BonusCheckBox target)
private void adjustCheckboxes(JCheckBox target, SkillDataBonus bonus)
{
adjustAllExcept(target);
adjustXPBonus(target.isSelected(), target.getBonus().getValue());
}
/**
* Adjusts and deselects all <b>other</b> checkboxes
* contained within {@link SkillCalculator#bonusCheckBoxes}
* except the checkbox provided as the parameter
*
* @param except The checkbox to keep unchanged
*/
private void adjustAllExcept(JCheckBox except)
{
bonusCheckBoxes.stream()
.filter(checkbox -> checkbox != except)
.filter(JCheckBox::isSelected)
.forEach(otherSelectedCheckbox ->
adjustXPBonus(0);
bonusCheckBoxes.forEach(otherSelectedCheckbox ->
{
if (otherSelectedCheckbox != target)
{
otherSelectedCheckbox.setSelected(false);
adjustXPBonus(false,
otherSelectedCheckbox.getBonus().getValue());
});
}
});
if (target.isSelected())
{
adjustXPBonus(bonus.getValue());
}
}
private void renderActionSlots()
@@ -326,9 +315,9 @@ class SkillCalculator extends JPanel
calculate();
}
private void adjustXPBonus(boolean addBonus, float value)
private void adjustXPBonus(float value)
{
xpFactor += addBonus ? value : -value;
xpFactor = 1f + value;
calculate();
}