@@ -18,11 +18,12 @@ public partial class StatWeighting : UserControl
18
18
public event Action WeightingsChanged ;
19
19
private readonly Debouncer _valueChangedDebouncer = new Debouncer ( ) ;
20
20
private readonly ToolTip _tt = new ToolTip ( ) ;
21
+ private const string NoPreset = "-" ;
22
+ private const string DefaultPreset = "Default" ;
21
23
22
24
public StatWeighting ( )
23
25
{
24
26
InitializeComponent ( ) ;
25
- _tt . SetToolTip ( groupBox1 , "Increase the weights for stats that are more important to you to be high in the offspring.\n Right click for Presets." ) ;
26
27
_currentSpecies = null ;
27
28
28
29
var displayedStats = new int [ ] {
@@ -97,11 +98,11 @@ public double[] Weightings
97
98
get
98
99
{
99
100
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 )
102
103
{
103
104
for ( int i = 0 ; i < Stats . StatsCount ; i ++ )
104
- w [ i ] /= s ;
105
+ w [ i ] /= sum ;
105
106
}
106
107
return w ;
107
108
}
@@ -157,7 +158,7 @@ public byte[] AnyOddEven
157
158
158
159
private void btAllToOne_Click ( object sender , EventArgs e )
159
160
{
160
- cbbPresets . SelectedIndex = 0 ;
161
+ ResetValues ( ) ;
161
162
}
162
163
163
164
/// <summary>
@@ -170,7 +171,7 @@ public bool TrySetPresetBySpecies(Species species, bool useDefaultBackupIfAvaila
170
171
if ( TrySetPresetByName ( species . DescriptiveName ) ) return true ;
171
172
if ( TrySetPresetByName ( species . name ) ) return true ;
172
173
return useDefaultBackupIfAvailable
173
- && TrySetPresetByName ( "Default" ) ;
174
+ && TrySetPresetByName ( DefaultPreset ) ;
174
175
}
175
176
176
177
/// <summary>
@@ -201,10 +202,8 @@ private void cbbPresets_SelectedIndexChanged(object sender, EventArgs e)
201
202
/// <returns>True if the preset was set, false if there is no preset with the given name</returns>
202
203
private bool SelectPresetByName ( string presetName )
203
204
{
204
- if ( presetName == "-" )
205
+ if ( presetName == NoPreset )
205
206
{
206
- WeightValues = Enumerable . Repeat ( 1d , Stats . StatsCount ) . ToArray ( ) ;
207
- AnyOddEven = Enumerable . Repeat ( ( byte ) 0 , Stats . StatsCount ) . ToArray ( ) ;
208
207
return true ;
209
208
}
210
209
if ( ! _customWeightings . TryGetValue ( presetName , out var weightings ) ) return false ;
@@ -213,6 +212,15 @@ private bool SelectPresetByName(string presetName)
213
212
return true ;
214
213
}
215
214
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
+
216
224
/// <summary>
217
225
/// Returns weightings for species. First the blueprint path is checked, then the full species name inclusive mod and variant, then only the base name.
218
226
/// </summary>
@@ -223,14 +231,14 @@ private bool SelectPresetByName(string presetName)
223
231
if ( _customWeightings . TryGetValue ( species . DescriptiveName , out weightings ) ) return weightings ;
224
232
if ( _customWeightings . TryGetValue ( species . name , out weightings ) ) return weightings ;
225
233
return useDefaultBackupIfAvailable
226
- && _customWeightings . TryGetValue ( "Default" , out weightings ) ? weightings : ( null , null ) ;
234
+ && _customWeightings . TryGetValue ( DefaultPreset , out weightings ) ? weightings : ( null , null ) ;
227
235
}
228
236
229
237
public ( double [ ] , byte [ ] ) GetWeightingByPresetName ( string presetName , bool useDefaultBackupIfAvailable = true )
230
238
{
231
239
if ( _customWeightings . TryGetValue ( presetName , out var weightings ) ) return weightings ;
232
240
return useDefaultBackupIfAvailable
233
- && _customWeightings . TryGetValue ( "Default" , out weightings ) ? weightings : ( null , null ) ;
241
+ && _customWeightings . TryGetValue ( DefaultPreset , out weightings ) ? weightings : ( null , null ) ;
234
242
}
235
243
236
244
private void btDelete_Click ( object sender , EventArgs e )
@@ -250,29 +258,57 @@ private void DeletePresetByName(string presetName)
250
258
}
251
259
}
252
260
253
- private void btSavePreset_Click ( object sender , EventArgs e )
261
+ private void BtSavePreset_Click ( object sender , EventArgs e )
254
262
{
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 ) ;
256
267
}
257
268
258
- private void SavePreset ( string presetName )
269
+ private void btSavePresetAs_Click ( object sender , EventArgs e )
259
270
{
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 )
261
281
{
262
- if ( _customWeightings . ContainsKey ( presetName ) )
282
+ suggestions = new [ ]
263
283
{
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?" ,
265
301
MessageBoxButtons . YesNo , MessageBoxIcon . Question ) == DialogResult . Yes )
266
302
{
267
- _customWeightings [ presetName ] = ( WeightValues , AnyOddEven ) ;
303
+ _customWeightings [ presetNameUser ] = ( WeightValues , AnyOddEven ) ;
268
304
}
269
305
else
270
306
return ;
271
307
}
272
308
else
273
- _customWeightings . Add ( presetName , ( WeightValues , AnyOddEven ) ) ;
309
+ _customWeightings . Add ( presetNameUser , ( WeightValues , AnyOddEven ) ) ;
274
310
CustomWeightings = _customWeightings ;
275
- TrySetPresetByName ( presetName ) ;
311
+ TrySetPresetByName ( presetNameUser ) ;
276
312
}
277
313
}
278
314
@@ -285,12 +321,25 @@ private void SavePreset(string presetName)
285
321
_customWeightings = value ;
286
322
// clear custom presets
287
323
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 ( ) ) ;
290
326
cbbPresets . SelectedIndex = 0 ;
327
+ SetComboboxDropdownWidthToMaxItemWidth ( cbbPresets ) ;
291
328
}
292
329
}
293
330
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
+
294
343
private class TriButton : Button
295
344
{
296
345
private readonly ToolTip _tt ;
0 commit comments