Simplify checkbox checks in skill calc

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-09-21 11:05:20 +02:00
parent 7000c252a0
commit a2b72e9aa5
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 UICombinedActionSlot combinedActionSlot = new UICombinedActionSlot();
private ArrayList<UIActionSlot> combinedActionSlots = new ArrayList<>(); 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 currentLevel = 1;
private int currentXP = Experience.getXpForLevel(currentLevel); private int currentXP = Experience.getXpForLevel(currentLevel);
@@ -193,7 +193,7 @@ class SkillCalculator extends JPanel
{ {
JPanel uiOption = new JPanel(new BorderLayout()); JPanel uiOption = new JPanel(new BorderLayout());
JLabel uiLabel = new JLabel(bonus.getName()); JLabel uiLabel = new JLabel(bonus.getName());
BonusCheckBox uiCheckbox = new BonusCheckBox(); JCheckBox uiCheckbox = new JCheckBox();
uiLabel.setForeground(Color.WHITE); uiLabel.setForeground(Color.WHITE);
uiLabel.setFont(FontManager.getRunescapeSmallFont()); uiLabel.setFont(FontManager.getRunescapeSmallFont());
@@ -202,43 +202,32 @@ class SkillCalculator extends JPanel
uiOption.setBackground(ColorScheme.DARKER_GRAY_COLOR); uiOption.setBackground(ColorScheme.DARKER_GRAY_COLOR);
// Adjust XP bonus depending on check-state of the boxes. // 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); uiCheckbox.setBackground(ColorScheme.MEDIUM_GRAY_COLOR);
uiOption.add(uiLabel, BorderLayout.WEST); uiOption.add(uiLabel, BorderLayout.WEST);
uiOption.add(uiCheckbox, BorderLayout.EAST); uiOption.add(uiCheckbox, BorderLayout.EAST);
uiCheckbox.setBonus(bonus);
bonusCheckBoxes.add(uiCheckbox); bonusCheckBoxes.add(uiCheckbox);
return uiOption; return uiOption;
} }
private void adjustCheckboxes(BonusCheckBox target) private void adjustCheckboxes(JCheckBox target, SkillDataBonus bonus)
{ {
adjustAllExcept(target); adjustXPBonus(0);
adjustXPBonus(target.isSelected(), target.getBonus().getValue()); bonusCheckBoxes.forEach(otherSelectedCheckbox ->
} {
if (otherSelectedCheckbox != target)
/**
* 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 ->
{ {
otherSelectedCheckbox.setSelected(false); otherSelectedCheckbox.setSelected(false);
adjustXPBonus(false, }
otherSelectedCheckbox.getBonus().getValue()); });
});
if (target.isSelected())
{
adjustXPBonus(bonus.getValue());
}
} }
private void renderActionSlots() private void renderActionSlots()
@@ -326,9 +315,9 @@ class SkillCalculator extends JPanel
calculate(); calculate();
} }
private void adjustXPBonus(boolean addBonus, float value) private void adjustXPBonus(float value)
{ {
xpFactor += addBonus ? value : -value; xpFactor = 1f + value;
calculate(); calculate();
} }