-
Notifications
You must be signed in to change notification settings - Fork 15
/
FormItemSelection.cs
481 lines (447 loc) · 17.6 KB
/
FormItemSelection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Rawr
{
public partial class FormItemSelection : Form
{
private Character _character;
public Character Character
{
get { return _character; }
set
{
if (_character != null)
{
_character.CalculationsInvalidated -= new EventHandler(_character_ItemsChanged);
}
_character = value;
_characterSlot = CharacterSlot.None;
if (_character != null)
{
_character.CalculationsInvalidated += new EventHandler(_character_ItemsChanged);
}
}
}
private CharacterCalculationsBase _currentCalculations;
public CharacterCalculationsBase CurrentCalculations
{
get { return _currentCalculations; }
set { _currentCalculations = value; }
}
private ComparisonCalculationBase[] _itemCalculations;
public ComparisonCalculationBase[] ItemCalculations
{
get { return _itemCalculations; }
set
{
if (value == null)
{
_itemCalculations = new ComparisonCalculationBase[0];
}
else
{
List<ComparisonCalculationBase> calcs = new List<ComparisonCalculationBase>(value);
calcs.Sort(new System.Comparison<ComparisonCalculationBase>(CompareItemCalculations));
_itemCalculations = calcs.ToArray();
}
RebuildItemList();
}
}
private ComparisonGraph.ComparisonSort _sort = ComparisonGraph.ComparisonSort.Overall;
public ComparisonGraph.ComparisonSort Sort
{
get { return _sort; }
set
{
_sort = value;
ItemCalculations = ItemCalculations;
}
}
protected int CompareItemCalculations(ComparisonCalculationBase a, ComparisonCalculationBase b)
{
if (Sort == ComparisonGraph.ComparisonSort.Overall)
return a.OverallPoints.CompareTo(b.OverallPoints);
else if (Sort == ComparisonGraph.ComparisonSort.Alphabetical)
return b.Name.CompareTo(a.Name);
else
return a.SubPoints[(int)Sort].CompareTo(b.SubPoints[(int)Sort]);
}
public FormItemSelection()
{
InitializeComponent();
overallToolStripMenuItem.Tag = -1;
alphabeticalToolStripMenuItem.Tag = -2;
Calculations_ModelChanged(null, null);
this.Activated += new EventHandler(FormItemSelection_Activated);
ItemCache.Instance.ItemsChanged += new EventHandler(ItemCache_ItemsChanged);
Calculations.ModelChanged += new EventHandler(Calculations_ModelChanged);
}
void Calculations_ModelChanged(object sender, EventArgs e)
{
_characterSlot = CharacterSlot.None;
sortToolStripMenuItem_Click(overallToolStripMenuItem, EventArgs.Empty);
toolStripDropDownButtonSort.DropDownItems.Clear();
toolStripDropDownButtonSort.DropDownItems.Add(overallToolStripMenuItem);
toolStripDropDownButtonSort.DropDownItems.Add(alphabeticalToolStripMenuItem);
foreach (string name in Calculations.SubPointNameColors.Keys)
{
ToolStripMenuItem toolStripMenuItemSubPoint = new ToolStripMenuItem(name);
toolStripMenuItemSubPoint.Tag = toolStripDropDownButtonSort.DropDownItems.Count - 2;
toolStripMenuItemSubPoint.Click += new System.EventHandler(this.sortToolStripMenuItem_Click);
toolStripDropDownButtonSort.DropDownItems.Add(toolStripMenuItemSubPoint);
}
}
void ItemCache_ItemsChanged(object sender, EventArgs e)
{
CharacterSlot characterSlot = _characterSlot;
_characterSlot = CharacterSlot.None;
LoadItemsBySlot(characterSlot);
}
void _character_ItemsChanged(object sender, EventArgs e)
{
_characterSlot = CharacterSlot.None;
}
void FormItemSelection_Activated(object sender, EventArgs e)
{
panelItems.AutoScrollPosition = new Point(0, 0);
}
private void CheckToHide(object sender, EventArgs e)
{
if (Visible)
{
ItemToolTip.Instance.Hide(this);
this.Hide();
}
}
private void timerForceActivate_Tick(object sender, System.EventArgs e)
{
this.timerForceActivate.Enabled = false;
this.Activate();
}
public void ForceShow()
{
this.timerForceActivate.Enabled = true;
}
public void Show(ItemButton button, CharacterSlot slot)
{
_button = button;
if (_buttonEnchant != null)
{
_characterSlot = CharacterSlot.None;
_buttonEnchant = null;
}
this.SetAutoLocation(button);
this.LoadItemsBySlot(slot);
base.Show();
}
public void Show(ItemButtonWithEnchant button, CharacterSlot slot)
{
_buttonEnchant = button;
if (_button != null)
{
_characterSlot = CharacterSlot.None;
_button = null;
}
this.SetAutoLocation(button);
this.LoadEnchantsBySlot(slot);
base.Show();
}
public void SetAutoLocation(Control ctrl)
{
Point ctrlScreen = ctrl.Parent.PointToScreen(ctrl.Location);
Point location = new Point(ctrlScreen.X + ctrl.Width, ctrlScreen.Y);
Rectangle workingArea = System.Windows.Forms.Screen.GetWorkingArea(ctrl.Parent);
if (location.X < workingArea.Left)
location.X = workingArea.Left;
if (location.Y < workingArea.Top)
location.Y = workingArea.Top;
if (location.X + this.Width > workingArea.Right)
location.X = ctrlScreen.X - this.Width;
if (location.Y + this.Height > workingArea.Bottom)
location.Y = workingArea.Bottom - this.Height;
this.Location = location;
}
private void sortToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
ComparisonGraph.ComparisonSort sort = ComparisonGraph.ComparisonSort.Overall;
foreach (ToolStripItem item in toolStripDropDownButtonSort.DropDownItems)
{
if (item is ToolStripMenuItem)
{
(item as ToolStripMenuItem).Checked = item == sender;
if ((item as ToolStripMenuItem).Checked)
{
toolStripDropDownButtonSort.Text = item.Text;
sort = (ComparisonGraph.ComparisonSort)((int)item.Tag);
}
}
}
this.Sort = sort;//(ComparisonGraph.ComparisonSort)Enum.Parse(typeof(ComparisonGraph.ComparisonSort), toolStripDropDownButtonSort.Text);
this.Cursor = Cursors.Default;
}
private CharacterSlot _characterSlot = CharacterSlot.None;
private ItemButton _button;
private ItemButtonWithEnchant _buttonEnchant;
public void LoadItemsBySlot(CharacterSlot slot)
{
if (_characterSlot != slot)
{
_characterSlot = slot;
List<ComparisonCalculationBase> itemCalculations = new List<ComparisonCalculationBase>();
if ((int)slot >= 0 && (int)slot <= 20)
{
if (this.Character != null)
{
bool seenEquippedItem = Character[slot] == null;
foreach (ItemInstance item in Character.GetRelevantItemInstances(slot))
{
if (!seenEquippedItem && Character[slot].Equals(item)) seenEquippedItem = true;
if (item.Item.FitsInSlot(slot, Character))
{
itemCalculations.Add(Calculations.GetItemCalculations(item, this.Character, slot));
}
}
if (!seenEquippedItem)
{
itemCalculations.Add(Calculations.GetItemCalculations(Character[slot], this.Character, slot));
}
}
ComparisonCalculationBase emptyCalcs = Calculations.CreateNewComparisonCalculation();
emptyCalcs.Name = "Empty";
emptyCalcs.Item = new Item();
emptyCalcs.Item.Name = "Empty";
emptyCalcs.ItemInstance = new ItemInstance();
emptyCalcs.ItemInstance.Id = -1;
emptyCalcs.Equipped = this.Character[slot] == null;
itemCalculations.Add(emptyCalcs);
}
else
{
if (this.Character != null)
{
bool seenEquippedItem = false;
if (_button != null && _button.SelectedItem == null)
{
seenEquippedItem = true;
}
foreach (Item item in Character.GetRelevantItems(slot))
{
if (!seenEquippedItem && _button != null && item.Equals(_button.SelectedItem)) seenEquippedItem = true;
if (item.FitsInSlot(slot, Character))
{
if(slot == CharacterSlot.Gems)
AddGemToItemCalculations(itemCalculations, item);
else
itemCalculations.Add(Calculations.GetItemCalculations(item, this.Character, slot));
}
}
if (!seenEquippedItem && _button != null && _button.SelectedItem != null)
{
itemCalculations.Add(Calculations.GetItemCalculations(_button.SelectedItem, this.Character, slot));
}
}
ComparisonCalculationBase emptyCalcs = Calculations.CreateNewComparisonCalculation();
emptyCalcs.Name = "Empty";
emptyCalcs.Item = new Item();
emptyCalcs.Item.Name = "Empty";
emptyCalcs.ItemInstance = new ItemInstance();
emptyCalcs.ItemInstance.Id = -1;
if (_button != null && _button.SelectedItem != null)
{
emptyCalcs.Equipped = _button.SelectedItem == null;
}
else
{
emptyCalcs.Equipped = false;
}
itemCalculations.Add(emptyCalcs);
}
itemCalculations.Sort(new System.Comparison<ComparisonCalculationBase>(CompareItemCalculations));
Dictionary<int, int> countItem = new Dictionary<int, int>();
List<ComparisonCalculationBase> filteredItemCalculations = new List<ComparisonCalculationBase>();
for (int i = itemCalculations.Count - 1; i >= 0; i--)
//foreach (ComparisonCalculationBase itemCalculation in itemCalculations)
{
ComparisonCalculationBase itemCalculation = itemCalculations[i];
int itemId = (itemCalculation.ItemInstance == null ? itemCalculation.Item.Id : itemCalculation.ItemInstance.Id);
if (!countItem.ContainsKey(itemId)) countItem.Add(itemId, 0);
if (countItem[itemId]++ < Properties.GeneralSettings.Default.CountGemmingsShown ||
itemCalculation.Equipped || itemCalculation.ItemInstance.ForceDisplay)
{
filteredItemCalculations.Add(itemCalculation);
}
}
ItemCalculations = filteredItemCalculations.ToArray();
}
}
private void AddGemToItemCalculations(List<ComparisonCalculationBase> itemCalculations, Item item)
{
if (item.IsJewelersGem)
{
if (!Rawr.Properties.GeneralSettings.Default.HideProfEnchants || this.Character.HasProfession(Profession.Jewelcrafting))
{
if (Character.JewelersGemCount < 3)
itemCalculations.Add(Calculations.GetItemCalculations(item, this.Character, CharacterSlot.Gems));
else
{
Item nullItem = item.Clone();
nullItem.Stats = new Stats();
itemCalculations.Add(Calculations.GetItemCalculations(nullItem, this.Character, CharacterSlot.Gems));
}
}
}
else if (item.IsLimitedGem)
{
if (!Character.IsUniqueGemEquipped(item))
itemCalculations.Add(Calculations.GetItemCalculations(item, this.Character, CharacterSlot.Gems));
else
{
Item nullItem = item.Clone();
nullItem.Stats = new Stats();
itemCalculations.Add(Calculations.GetItemCalculations(nullItem, this.Character, CharacterSlot.Gems));
}
}
else
itemCalculations.Add(Calculations.GetItemCalculations(item, this.Character, CharacterSlot.Gems));
}
private void LoadEnchantsBySlot(CharacterSlot slot)
{
if (_characterSlot != slot)
{
_characterSlot = slot;
List<ComparisonCalculationBase> itemCalculations = null;
if (Character != null && CurrentCalculations != null)
{
itemCalculations = Calculations.GetEnchantCalculations(Item.GetItemSlotByCharacterSlot(slot), Character, CurrentCalculations, false);
}
itemCalculations.Sort(new System.Comparison<ComparisonCalculationBase>(CompareItemCalculations));
ItemCalculations = itemCalculations.ToArray();
}
}
private List<ItemSelectorItem> _itemSelectorItems = new List<ItemSelectorItem>();
private void RebuildItemList()
{
if (this.InvokeRequired)
{
Invoke((MethodInvoker)RebuildItemList);
//InvokeHelper.Invoke(this, "RebuildItemList", null);
return;
}
panelItems.SuspendLayout();
////Replaced this...
//while (panelItems.Controls.Count < this.ItemCalculations.Length)
// panelItems.Controls.Add(new ItemSelectorItem());
//while (panelItems.Controls.Count > this.ItemCalculations.Length)
// panelItems.Controls.RemoveAt(panelItems.Controls.Count - 1);
////...with this, so as not to constantly create and remove lots of controls
int currentItemLength = panelItems.Controls.Count;
int targetItemLength = this.ItemCalculations.Length;
if (currentItemLength < targetItemLength)
{
int itemSelectorsToCreate = targetItemLength - _itemSelectorItems.Count;
for (int i = 0; i < itemSelectorsToCreate; i++)
{
_itemSelectorItems.Add(new ItemSelectorItem());
}
for (int i = currentItemLength; i < targetItemLength; i++)
{
panelItems.Controls.Add(_itemSelectorItems[i]);
}
}
else if (currentItemLength > targetItemLength)
{
for (int i = currentItemLength; i > targetItemLength; i--)
{
panelItems.Controls.RemoveAt(i - 1);
}
}
float maxRating = 0;
for (int i = 0; i < this.ItemCalculations.Length; i++)
{
ItemSelectorItem ctrl = panelItems.Controls[i] as ItemSelectorItem;
ComparisonCalculationBase calc = this.ItemCalculations[i];
if (_button != null)
{
calc.Equipped = (calc.ItemInstance == _button.SelectedItemInstance && calc.Item == _button.SelectedItem) || (calc.Item.Id == 0 && _button.SelectedItem == null);
ctrl.IsEnchant = false;
}
if (_buttonEnchant != null)
{
calc.Equipped = Math.Abs(calc.Item.Id % 10000) == _buttonEnchant.SelectedEnchantId;
ctrl.IsEnchant = true;
}
ctrl.ItemCalculation = calc;
ctrl.Character = Character;
ctrl.CharacterSlot = _characterSlot;
ctrl.Sort = this.Sort;
ctrl.HideToolTip();
bool visible = string.IsNullOrEmpty(this.toolStripTextBoxFilter.Text) || calc.Name.ToLower().Contains(this.toolStripTextBoxFilter.Text.ToLower());
ctrl.Visible = visible;
if (visible)
{
float calcRating;
if (Sort == ComparisonGraph.ComparisonSort.Overall || this.Sort == ComparisonGraph.ComparisonSort.Alphabetical)
{
calcRating = calc.OverallPoints;
}
else
{
calcRating = calc.SubPoints[(int)Sort];
}
maxRating = Math.Max(maxRating, calcRating);
}
}
panelItems.ResumeLayout(true);
foreach (ItemSelectorItem ctrl in panelItems.Controls)
{
ctrl.MaxRating = maxRating;
}
}
private void toolStripTextBoxFilter_TextChanged(object sender, EventArgs e)
{
RebuildItemList();
}
public void Select(ItemInstance item)
{
if (_button != null)
_button.SelectedItemInstance = item == null ? null : item.Clone();
//if (_buttonEnchant != null)
//{
// ItemInstance copy = _buttonEnchant.SelectedItem.Clone();
// copy.EnchantId = item == null ? 0 : Math.Abs(item.Id % 10000);
// _buttonEnchant.SelectedItem = copy;
//}
_characterSlot = CharacterSlot.None;
ItemToolTip.Instance.Hide(this);
this.Hide();
}
public void Select(Item item)
{
if (_button != null)
_button.SelectedItem = item;
if (_buttonEnchant != null)
{
if(_buttonEnchant.SelectedItem != null)
{
ItemInstance copy = _buttonEnchant.SelectedItem.Clone();
copy.EnchantId = item == null ? 0 : Math.Abs(item.Id % 10000);
_buttonEnchant.SelectedItem = copy;
}
}
_characterSlot = CharacterSlot.None;
ItemToolTip.Instance.Hide(this);
this.Hide();
}
}
public interface IFormItemSelectionProvider
{
FormItemSelection FormItemSelection { get; }
}
}