From eec3e779bd59ee8f99e4b732436f61ba830ff5f8 Mon Sep 17 00:00:00 2001 From: rkfg Date: Thu, 17 Mar 2016 03:19:04 +0300 Subject: [PATCH 1/3] Simplify controlling of advanced deck generator sliders. --- .../deck/generator/DeckGeneratorDialog.java | 18 ++- .../generator/RatioAdjustingSliderPanel.java | 110 +++++++++--------- 2 files changed, 61 insertions(+), 67 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java index 3d541a75eb..d723a09aba 100644 --- a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java +++ b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java @@ -289,15 +289,15 @@ public class DeckGeneratorDialog { adjustingSliderPanel = new RatioAdjustingSliderPanel(); // Restore saved slider values - String creaturePercentage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_CREATURE_PERCENTAGE, - Integer.toString(DeckGeneratorPool.DEFAULT_CREATURE_PERCENTAGE)); - adjustingSliderPanel.setCreaturePercentage(Integer.parseInt(creaturePercentage)); - String nonCreaturePercentage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_NON_CREATURE_PERCENTAGE, - Integer.toString(DeckGeneratorPool.DEFAULT_NON_CREATURE_PERCENTAGE)); - adjustingSliderPanel.setNonCreaturePercentage(Integer.parseInt(nonCreaturePercentage)); String landPercentage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_LAND_PERCENTAGE, Integer.toString(DeckGeneratorPool.DEFAULT_LAND_PERCENTAGE)); adjustingSliderPanel.setLandPercentage(Integer.parseInt(landPercentage)); + String nonCreaturePercentage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_NON_CREATURE_PERCENTAGE, + Integer.toString(DeckGeneratorPool.DEFAULT_NON_CREATURE_PERCENTAGE)); + adjustingSliderPanel.setNonCreaturePercentage(Integer.parseInt(nonCreaturePercentage)); + String creaturePercentage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_CREATURE_PERCENTAGE, + Integer.toString(DeckGeneratorPool.DEFAULT_CREATURE_PERCENTAGE)); + adjustingSliderPanel.setCreaturePercentage(Integer.parseInt(creaturePercentage)); advancedPanel.add(adjustingSliderPanel, c); // Reset @@ -306,7 +306,7 @@ public class DeckGeneratorDialog { c.ipady = 0; c.weightx = 1.0; c.anchor = GridBagConstraints.LAST_LINE_END; - c.insets = new Insets(10,10, 0, 0); + c.insets = new Insets(10, 10, 0, 0); c.gridx = 2; c.gridwidth = 1; c.gridy = 2; @@ -418,13 +418,11 @@ public class DeckGeneratorDialog { } public DeckGeneratorCMC getDeckGeneratorCMC() { - DeckGeneratorCMC selectedCMC = (DeckGeneratorCMC)cbCMC.getSelectedItem(); + DeckGeneratorCMC selectedCMC = (DeckGeneratorCMC) cbCMC.getSelectedItem(); PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_ADVANCED_CMC, selectedCMC.name()); return selectedCMC; } - - public String getSelectedColors() { if (selectedColors != null) { PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_DECK_SIZE, cbDeckSize.getSelectedItem().toString()); diff --git a/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java b/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java index 665fdbbfef..e25f42a7ad 100644 --- a/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java +++ b/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java @@ -45,20 +45,21 @@ import java.util.List; */ public class RatioAdjustingSliderPanel extends JPanel { + private static final int MAXIMUM = 100; private JStorageSlider creatureSlider, nonCreatureSlider, landSlider; private List textLabels = new ArrayList<>(); private AdjustingSliderGroup sg; private class JStorageSlider extends JSlider { - // Slider stores its initial value to revert to when reset + // Slider stores its initial value to revert to when reset private int defaultValue; - private int previousValue; + private boolean adjust; - public JStorageSlider(int min, int max, int value) { + public JStorageSlider(int min, int max, int value, boolean adjust) { super(min, max, value); - previousValue = value; defaultValue = value; + this.adjust = adjust; setMinorTickSpacing(5); setMajorTickSpacing(10); setPaintTicks(true); @@ -66,30 +67,23 @@ public class RatioAdjustingSliderPanel extends JPanel { setLabelTable(createStandardLabels(10)); } - public int getPreviousValue() { - return previousValue; - } - - public void setPreviousValue(int value) { - previousValue = value; - } - public void resetDefault() { this.setValue(defaultValue); - previousValue = defaultValue; + } + + public boolean isAdjust() { + return adjust; } } - private class AdjustingSliderGroup - { + private class AdjustingSliderGroup { private final ArrayList storageSliders; private int sliderIndex = 0; - AdjustingSliderGroup(JStorageSlider... sliders) - { + AdjustingSliderGroup(JStorageSlider... sliders) { storageSliders = new ArrayList<>(); - for(JStorageSlider slider: sliders) { + for (JStorageSlider slider : sliders) { storageSliders.add(slider); slider.addChangeListener(new ChangeListener() { @Override @@ -99,35 +93,40 @@ public class RatioAdjustingSliderPanel extends JPanel { }); } } + public void fireSliderChangedEvent(JStorageSlider source) { // We don't want to do anything if the value isn't changing - if(!source.getValueIsAdjusting()) + if (!source.getValueIsAdjusting()) return; - // Update the slider depending on how much it's changed relative to its previous position - int change = (source.getValue() - source.getPreviousValue()); - updateSliderPosition(change, source); + // Update the slider depending on how much it's changed relative to its previous position + updateSliderPosition(source); } - private void updateSliderPosition(int change, JStorageSlider source) { - int remaining = change; - while (remaining != 0) { - // Get the currently indexed slider - JStorageSlider slider = storageSliders.get(sliderIndex); - // If it's not the slider that fired the event - if (slider != source) { - // Check we don't go over the upper and lower bounds - if (remaining < 0 || (remaining > 0 && slider.getValue() > 0)) { - // Adjust the currently selected slider by +/- 1 - int adjustment = Integer.signum(remaining); - slider.setValue(slider.getValue() - adjustment); - remaining -= adjustment; - } - } - // Select the next slider in the list of sliders - sliderIndex = (sliderIndex + 1) % storageSliders.size(); + private void updateSliderPosition(JStorageSlider source) { + int maximum = MAXIMUM; + int excess = 100; + for (JStorageSlider slider : storageSliders) { + excess -= slider.getValue(); } - for (JStorageSlider slider : storageSliders) { - slider.setPreviousValue(slider.getValue()); + int addition = excess / (storageSliders.size() - 1); // divide the deficit between all sliders except this one + if (addition == 0 && excess != 0) { + addition = Integer.signum(excess); + } + for (int i = storageSliders.size() - 1; i >= 0; i--) { + JStorageSlider slider = storageSliders.get(i); + int value = slider.getValue(); + if (slider != source && slider.isAdjust()) { + slider.setMaximum(maximum); + if (excess > addition) { + value += addition; + excess -= addition; + } else { + value += excess; + excess = 0; + } + slider.setValue(value); + } + maximum -= value; } } @@ -143,10 +142,10 @@ public class RatioAdjustingSliderPanel extends JPanel { private void initPanel() { - // Create three sliders with default values - creatureSlider = new JStorageSlider(0, 100, DeckGeneratorPool.DEFAULT_CREATURE_PERCENTAGE); - nonCreatureSlider = new JStorageSlider(0, 100, DeckGeneratorPool.DEFAULT_NON_CREATURE_PERCENTAGE); - landSlider = new JStorageSlider(0, 100, DeckGeneratorPool.DEFAULT_LAND_PERCENTAGE); + // Create three sliders with default values + creatureSlider = new JStorageSlider(0, MAXIMUM, DeckGeneratorPool.DEFAULT_CREATURE_PERCENTAGE, true); + nonCreatureSlider = new JStorageSlider(0, MAXIMUM, DeckGeneratorPool.DEFAULT_NON_CREATURE_PERCENTAGE, true); + landSlider = new JStorageSlider(0, MAXIMUM, DeckGeneratorPool.DEFAULT_LAND_PERCENTAGE, false); sg = new AdjustingSliderGroup(creatureSlider, nonCreatureSlider, landSlider); @@ -178,7 +177,7 @@ public class RatioAdjustingSliderPanel extends JPanel { return sliderPanel; } - private static JLabel createChangingPercentageLabel(final JSlider slider) { + private static JLabel createChangingPercentageLabel(final JSlider slider) { final JLabel label = new JLabel(" " + String.valueOf(slider.getValue()) + "%"); @@ -187,8 +186,8 @@ public class RatioAdjustingSliderPanel extends JPanel { public void stateChanged(ChangeEvent e) { String value = String.valueOf(slider.getValue()); StringBuilder labelBuilder = new StringBuilder(); - // Pad with spaces so all percentage labels are of equal size - for(int i = 0; i < (5-value.length()); i++) { + // Pad with spaces so all percentage labels are of equal size + for (int i = 0; i < (5 - value.length()); i++) { labelBuilder.append(" "); } labelBuilder.append(value); @@ -201,16 +200,16 @@ public class RatioAdjustingSliderPanel extends JPanel { @Override public void setEnabled(boolean enabled) { - for(JStorageSlider slider: sg.getSliders()) { + for (JStorageSlider slider : sg.getSliders()) { slider.setEnabled(enabled); } - for(JLabel label: textLabels) { + for (JLabel label : textLabels) { label.setEnabled(enabled); } } public void resetValues() { - for(JStorageSlider slider: sg.getSliders()) { + for (JStorageSlider slider : sg.getSliders()) { slider.resetDefault(); } } @@ -229,20 +228,17 @@ public class RatioAdjustingSliderPanel extends JPanel { public void setCreaturePercentage(int percentage) { creatureSlider.setValue(percentage); - creatureSlider.previousValue = percentage; + sg.updateSliderPosition(creatureSlider); } public void setNonCreaturePercentage(int percentage) { nonCreatureSlider.setValue(percentage); - nonCreatureSlider.previousValue = percentage; + sg.updateSliderPosition(nonCreatureSlider); } public void setLandPercentage(int percentage) { landSlider.setValue(percentage); - landSlider.previousValue = percentage; + sg.updateSliderPosition(landSlider); } - - - } From 74f925b579d20d88c0b2094ec276d9b8579f1b73 Mon Sep 17 00:00:00 2001 From: rkfg Date: Thu, 17 Mar 2016 06:25:56 +0300 Subject: [PATCH 2/3] Fix deck generator sliders. --- .../generator/RatioAdjustingSliderPanel.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java b/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java index e25f42a7ad..b1c07c9aca 100644 --- a/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java +++ b/Mage.Client/src/main/java/mage/client/deck/generator/RatioAdjustingSliderPanel.java @@ -46,6 +46,7 @@ import java.util.List; public class RatioAdjustingSliderPanel extends JPanel { private static final int MAXIMUM = 100; + private int adjustableCount; private JStorageSlider creatureSlider, nonCreatureSlider, landSlider; private List textLabels = new ArrayList<>(); private AdjustingSliderGroup sg; @@ -91,6 +92,9 @@ public class RatioAdjustingSliderPanel extends JPanel { fireSliderChangedEvent((JStorageSlider) e.getSource()); } }); + if (slider.isAdjust()) { + adjustableCount++; + } } } @@ -105,25 +109,32 @@ public class RatioAdjustingSliderPanel extends JPanel { private void updateSliderPosition(JStorageSlider source) { int maximum = MAXIMUM; int excess = 100; + int sign = 0; for (JStorageSlider slider : storageSliders) { excess -= slider.getValue(); } - int addition = excess / (storageSliders.size() - 1); // divide the deficit between all sliders except this one + sign = Integer.signum(excess); + excess = Math.abs(excess); + int addition = excess / (adjustableCount - 1); // divide the deficit between all adjustable sliders except this one if (addition == 0 && excess != 0) { - addition = Integer.signum(excess); + addition = 1; } for (int i = storageSliders.size() - 1; i >= 0; i--) { JStorageSlider slider = storageSliders.get(i); int value = slider.getValue(); if (slider != source && slider.isAdjust()) { slider.setMaximum(maximum); - if (excess > addition) { - value += addition; + if (excess >= addition) { + value += addition * sign; excess -= addition; } else { - value += excess; + value += excess * sign; excess = 0; } + if (value < 0) { + excess += value; + value = 0; + } slider.setValue(value); } maximum -= value; From e3b5827a76dee472ea93374c0465544beafd5b06 Mon Sep 17 00:00:00 2001 From: rkfg Date: Thu, 17 Mar 2016 06:48:24 +0300 Subject: [PATCH 3/3] Fix imports in Exhaustion.java --- Mage.Sets/src/mage/sets/portalthreekingdoms/Exhaustion.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Mage.Sets/src/mage/sets/portalthreekingdoms/Exhaustion.java b/Mage.Sets/src/mage/sets/portalthreekingdoms/Exhaustion.java index 2f6f38fa62..cd21bc9dd0 100644 --- a/Mage.Sets/src/mage/sets/portalthreekingdoms/Exhaustion.java +++ b/Mage.Sets/src/mage/sets/portalthreekingdoms/Exhaustion.java @@ -27,8 +27,6 @@ */ package mage.sets.portalthreekingdoms; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; import mage.abilities.Ability; import mage.abilities.effects.ContinuousEffect; @@ -44,7 +42,7 @@ import mage.filter.predicate.mageobject.CardTypePredicate; import mage.game.Game; import mage.players.Player; import mage.target.common.TargetOpponent; -import mage.target.targetpointer.FixedTargets; +import mage.target.targetpointer.FixedTarget; /** *