Skip to content

Commit ee5e65e

Browse files
committed
improved behaviour of stat weight preset organization
1 parent 5ad3629 commit ee5e65e

File tree

5 files changed

+125
-52
lines changed

5 files changed

+125
-52
lines changed

ARKBreedingStats/Form1.extractor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void ShowSumOfChosenLevels()
120120
var checkLowLevels = _lowestLevels.TryGetValue(species, out int[] lowSpeciesLevels);
121121

122122
var customStatNames = species.statNames;
123-
var statWeights = breedingPlan1.StatWeighting.GetWeightingByPresetName(species.name);
123+
var statWeights = breedingPlan1.StatWeighting.GetWeightingForSpecies(species);
124124
if (statWeights.Item1 == null) checkLowLevels = false;
125125
var analysisState = LevelStatus.Neutral;
126126
var newTopStatsText = new List<string>();

ARKBreedingStats/Form1.library.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private void CalculateTopStats(List<Creature> creatures)
354354
List<int> usedAndConsideredStatIndices = new List<int>(Stats.StatsCount);
355355
int[] bestStat = new int[Stats.StatsCount];
356356
int[] lowestStat = new int[Stats.StatsCount];
357-
var statWeights = breedingPlan1.StatWeighting.GetWeightingByPresetName(species.name);
357+
var statWeights = breedingPlan1.StatWeighting.GetWeightingForSpecies(species);
358358
for (int s = 0; s < Stats.StatsCount; s++)
359359
{
360360
bestStat[s] = -1;

ARKBreedingStats/Utils.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Drawing;
66
using System.Globalization;
7+
using System.Linq;
78
using System.Threading.Tasks;
89
using System.Windows.Forms;
910
using ARKBreedingStats.values;
@@ -469,7 +470,7 @@ public static Color ForeColor(Color backColor)
469470
return backColor.R * .3f + backColor.G * .59f + backColor.B * .11f < 110 ? Color.White : Color.Black;
470471
}
471472

472-
public static bool ShowTextInput(string text, out string input, string title = "", string preInput = "")
473+
public static bool ShowTextInput(string text, out string input, string title = null, string preInput = null, params string[] autoCompleteStrings)
473474
{
474475
Form inputForm = new Form
475476
{
@@ -493,6 +494,14 @@ public static bool ShowTextInput(string text, out string input, string title = "
493494
inputForm.AcceptButton = buttonOk;
494495
inputForm.CancelButton = buttonCancel;
495496
textBox.Text = preInput;
497+
if (autoCompleteStrings?.Any() == true)
498+
{
499+
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
500+
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
501+
var ac = new AutoCompleteStringCollection();
502+
ac.AddRange(autoCompleteStrings);
503+
textBox.AutoCompleteCustomSource = ac;
504+
}
496505
textBox.SelectAll();
497506

498507
input = string.Empty;

ARKBreedingStats/uiControls/StatWeighting.Designer.cs

+42-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ARKBreedingStats/uiControls/StatWeighting.cs

+71-22
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public partial class StatWeighting : UserControl
1818
public event Action WeightingsChanged;
1919
private readonly Debouncer _valueChangedDebouncer = new Debouncer();
2020
private readonly ToolTip _tt = new ToolTip();
21+
private const string NoPreset = "-";
22+
private const string DefaultPreset = "Default";
2123

2224
public StatWeighting()
2325
{
2426
InitializeComponent();
25-
_tt.SetToolTip(groupBox1, "Increase the weights for stats that are more important to you to be high in the offspring.\nRight click for Presets.");
2627
_currentSpecies = null;
2728

2829
var displayedStats = new int[]{
@@ -97,11 +98,11 @@ public double[] Weightings
9798
get
9899
{
99100
double[] w = WeightValues;
100-
double s = w.Sum() / Stats.StatsCount;
101-
if (s > 0)
101+
double sum = w.Sum() / Stats.StatsCount;
102+
if (sum > 0)
102103
{
103104
for (int i = 0; i < Stats.StatsCount; i++)
104-
w[i] /= s;
105+
w[i] /= sum;
105106
}
106107
return w;
107108
}
@@ -157,7 +158,7 @@ public byte[] AnyOddEven
157158

158159
private void btAllToOne_Click(object sender, EventArgs e)
159160
{
160-
cbbPresets.SelectedIndex = 0;
161+
ResetValues();
161162
}
162163

163164
/// <summary>
@@ -170,7 +171,7 @@ public bool TrySetPresetBySpecies(Species species, bool useDefaultBackupIfAvaila
170171
if (TrySetPresetByName(species.DescriptiveName)) return true;
171172
if (TrySetPresetByName(species.name)) return true;
172173
return useDefaultBackupIfAvailable
173-
&& TrySetPresetByName("Default");
174+
&& TrySetPresetByName(DefaultPreset);
174175
}
175176

176177
/// <summary>
@@ -201,10 +202,8 @@ private void cbbPresets_SelectedIndexChanged(object sender, EventArgs e)
201202
/// <returns>True if the preset was set, false if there is no preset with the given name</returns>
202203
private bool SelectPresetByName(string presetName)
203204
{
204-
if (presetName == "-")
205+
if (presetName == NoPreset)
205206
{
206-
WeightValues = Enumerable.Repeat(1d, Stats.StatsCount).ToArray();
207-
AnyOddEven = Enumerable.Repeat((byte)0, Stats.StatsCount).ToArray();
208207
return true;
209208
}
210209
if (!_customWeightings.TryGetValue(presetName, out var weightings)) return false;
@@ -213,6 +212,15 @@ private bool SelectPresetByName(string presetName)
213212
return true;
214213
}
215214

215+
/// <summary>
216+
/// Resets all weightings.
217+
/// </summary>
218+
private void ResetValues()
219+
{
220+
WeightValues = Enumerable.Repeat(1d, Stats.StatsCount).ToArray();
221+
AnyOddEven = Enumerable.Repeat((byte)0, Stats.StatsCount).ToArray();
222+
}
223+
216224
/// <summary>
217225
/// Returns weightings for species. First the blueprint path is checked, then the full species name inclusive mod and variant, then only the base name.
218226
/// </summary>
@@ -223,14 +231,14 @@ private bool SelectPresetByName(string presetName)
223231
if (_customWeightings.TryGetValue(species.DescriptiveName, out weightings)) return weightings;
224232
if (_customWeightings.TryGetValue(species.name, out weightings)) return weightings;
225233
return useDefaultBackupIfAvailable
226-
&& _customWeightings.TryGetValue("Default", out weightings) ? weightings : (null, null);
234+
&& _customWeightings.TryGetValue(DefaultPreset, out weightings) ? weightings : (null, null);
227235
}
228236

229237
public (double[], byte[]) GetWeightingByPresetName(string presetName, bool useDefaultBackupIfAvailable = true)
230238
{
231239
if (_customWeightings.TryGetValue(presetName, out var weightings)) return weightings;
232240
return useDefaultBackupIfAvailable
233-
&& _customWeightings.TryGetValue("Default", out weightings) ? weightings : (null, null);
241+
&& _customWeightings.TryGetValue(DefaultPreset, out weightings) ? weightings : (null, null);
234242
}
235243

236244
private void btDelete_Click(object sender, EventArgs e)
@@ -250,29 +258,57 @@ private void DeletePresetByName(string presetName)
250258
}
251259
}
252260

253-
private void btSavePreset_Click(object sender, EventArgs e)
261+
private void BtSavePreset_Click(object sender, EventArgs e)
254262
{
255-
SavePreset(_currentSpecies.name);
263+
var presetName = cbbPresets.SelectedItem.ToString();
264+
if (string.IsNullOrEmpty(presetName) || presetName == NoPreset)
265+
SavePresetAs(_currentSpecies?.name);
266+
else _customWeightings[presetName] = (WeightValues, AnyOddEven);
256267
}
257268

258-
private void SavePreset(string presetName)
269+
private void btSavePresetAs_Click(object sender, EventArgs e)
259270
{
260-
if (Utils.ShowTextInput("Preset-Name", out presetName, "New Preset", presetName) && presetName.Length > 0)
271+
var presetName = cbbPresets.SelectedItem.ToString();
272+
if (string.IsNullOrEmpty(presetName) || presetName == NoPreset || presetName == DefaultPreset)
273+
SavePresetAs(_currentSpecies?.name);
274+
else SavePresetAs(presetName);
275+
}
276+
277+
private void SavePresetAs(string presetName)
278+
{
279+
string[] suggestions;
280+
if (_currentSpecies != null)
261281
{
262-
if (_customWeightings.ContainsKey(presetName))
282+
suggestions = new[]
263283
{
264-
if (MessageBox.Show("Preset-Name exists already, overwrite?", "Overwrite Preset?",
284+
DefaultPreset,
285+
_currentSpecies.name,
286+
_currentSpecies.DescriptiveName,
287+
_currentSpecies.DescriptiveNameAndMod,
288+
_currentSpecies.blueprintPath
289+
};
290+
}
291+
else
292+
suggestions = new[] { DefaultPreset };
293+
294+
295+
if (Utils.ShowTextInput("Preset Name", out var presetNameUser, "New Preset", presetName, suggestions)
296+
&& presetNameUser.Length > 0)
297+
{
298+
if (_customWeightings.ContainsKey(presetNameUser))
299+
{
300+
if (MessageBox.Show("Preset name exists already, overwrite?", "Overwrite Preset?",
265301
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
266302
{
267-
_customWeightings[presetName] = (WeightValues, AnyOddEven);
303+
_customWeightings[presetNameUser] = (WeightValues, AnyOddEven);
268304
}
269305
else
270306
return;
271307
}
272308
else
273-
_customWeightings.Add(presetName, (WeightValues, AnyOddEven));
309+
_customWeightings.Add(presetNameUser, (WeightValues, AnyOddEven));
274310
CustomWeightings = _customWeightings;
275-
TrySetPresetByName(presetName);
311+
TrySetPresetByName(presetNameUser);
276312
}
277313
}
278314

@@ -285,12 +321,25 @@ private void SavePreset(string presetName)
285321
_customWeightings = value;
286322
// clear custom presets
287323
cbbPresets.Items.Clear();
288-
cbbPresets.Items.Add("-");
289-
cbbPresets.Items.AddRange(_customWeightings.Keys.ToArray());
324+
cbbPresets.Items.Add(NoPreset);
325+
cbbPresets.Items.AddRange(_customWeightings.Keys.OrderBy(s => s).ToArray());
290326
cbbPresets.SelectedIndex = 0;
327+
SetComboboxDropdownWidthToMaxItemWidth(cbbPresets);
291328
}
292329
}
293330

331+
private void SetComboboxDropdownWidthToMaxItemWidth(ComboBox cbb)
332+
{
333+
var g = cbb.CreateGraphics();
334+
var verticalScrollBarWidth = cbb.Items.Count > cbb.MaxDropDownItems
335+
? SystemInformation.VerticalScrollBarWidth : 0;
336+
337+
var maxWidth = cbb.Items.Cast<string>().Select(s => (int)g.MeasureString(s, cbb.Font).Width + verticalScrollBarWidth).Max();
338+
maxWidth = Math.Min(600, maxWidth);
339+
if (maxWidth > cbb.DropDownWidth)
340+
cbb.DropDownWidth = maxWidth;
341+
}
342+
294343
private class TriButton : Button
295344
{
296345
private readonly ToolTip _tt;

0 commit comments

Comments
 (0)