mirror of
https://github.com/correl/mage.git
synced 2025-03-17 09:16:26 -09:00
Merge origin/master
This commit is contained in:
commit
6dde3e7374
2 changed files with 76 additions and 71 deletions
|
@ -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
|
||||
|
@ -423,8 +423,6 @@ public class DeckGeneratorDialog {
|
|||
return selectedCMC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getSelectedColors() {
|
||||
if (selectedColors != null) {
|
||||
PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_DECK_GENERATOR_DECK_SIZE, cbDeckSize.getSelectedItem().toString());
|
||||
|
|
|
@ -45,6 +45,8 @@ 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<JLabel> textLabels = new ArrayList<>();
|
||||
private AdjustingSliderGroup sg;
|
||||
|
@ -53,12 +55,12 @@ public class RatioAdjustingSliderPanel extends JPanel {
|
|||
|
||||
// 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,28 +68,21 @@ 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<JStorageSlider> storageSliders;
|
||||
private int sliderIndex = 0;
|
||||
|
||||
AdjustingSliderGroup(JStorageSlider... sliders)
|
||||
{
|
||||
AdjustingSliderGroup(JStorageSlider... sliders) {
|
||||
storageSliders = new ArrayList<>();
|
||||
for (JStorageSlider slider : sliders) {
|
||||
storageSliders.add(slider);
|
||||
|
@ -97,37 +92,52 @@ public class RatioAdjustingSliderPanel extends JPanel {
|
|||
fireSliderChangedEvent((JStorageSlider) e.getSource());
|
||||
}
|
||||
});
|
||||
if (slider.isAdjust()) {
|
||||
adjustableCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void fireSliderChangedEvent(JStorageSlider source) {
|
||||
// We don't want to do anything if the value isn't changing
|
||||
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);
|
||||
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;
|
||||
int sign = 0;
|
||||
for (JStorageSlider slider : storageSliders) {
|
||||
slider.setPreviousValue(slider.getValue());
|
||||
excess -= slider.getValue();
|
||||
}
|
||||
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 = 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 * sign;
|
||||
excess -= addition;
|
||||
} else {
|
||||
value += excess * sign;
|
||||
excess = 0;
|
||||
}
|
||||
if (value < 0) {
|
||||
excess += value;
|
||||
value = 0;
|
||||
}
|
||||
slider.setValue(value);
|
||||
}
|
||||
maximum -= value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,9 +154,9 @@ 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);
|
||||
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);
|
||||
|
||||
|
@ -229,20 +239,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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue