From 74f925b579d20d88c0b2094ec276d9b8579f1b73 Mon Sep 17 00:00:00 2001 From: rkfg Date: Thu, 17 Mar 2016 06:25:56 +0300 Subject: [PATCH] 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;