BagItemList.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Windows.Forms;
  3. using DDogClient.Protocol.Modules;
  4. using DDogClient.Protocol.Modules.Package;
  5. namespace CommonRPG.Client.Win32.Contorols
  6. {
  7. public class BagItemList : ListView, IPackageListener
  8. {
  9. public BagItemList()
  10. {
  11. }
  12. private CommonBag mBag;
  13. private void ResetSize(int s)
  14. {
  15. this.Items.Clear();
  16. for (var i = 0; i < s; i++)
  17. {
  18. this.Items.Add(new ListViewItem("EMPTY"));
  19. }
  20. }
  21. private void Init()
  22. {
  23. ResetSize(mBag.Size);
  24. foreach (var slot in mBag.AllSlots)
  25. {
  26. UpdateItem(slot.Index, slot.Item as ItemData);
  27. }
  28. }
  29. public void Init(CommonBag bag)
  30. {
  31. mBag = bag;
  32. bag.AddListener(this);
  33. Init();
  34. }
  35. public void UpdateItem(int pos, ItemData data)
  36. {
  37. if (pos >= Items.Count)
  38. {
  39. Init();
  40. }
  41. else
  42. {
  43. Items[pos].Text = data == null ? "EMPTY" : $"{data.TemplateID}-{data.Count}";
  44. }
  45. }
  46. public bool Match(IPackageItem item)
  47. {
  48. return true;
  49. }
  50. public void OnItemAdded(BasePackage basePackage, int index)
  51. {
  52. UpdateItem(index, basePackage.GetItemAt<ItemData>(index));
  53. }
  54. public void OnItemRemoved(BasePackage basePackage, int index, IPackageItem lastItem)
  55. {
  56. UpdateItem(index, null);
  57. }
  58. public void OnItemCountChanged(BasePackage basePackage, int index, uint from, uint to)
  59. {
  60. OnItemAdded(basePackage, index);
  61. }
  62. public void OnUpdateAction(BasePackage package, UpdateAction[] acts)
  63. {
  64. foreach (var act in acts)
  65. {
  66. switch (act.Type)
  67. {
  68. case UpdateAction.ActionType.Init:
  69. Init();
  70. break;
  71. case UpdateAction.ActionType.Add:
  72. UpdateItem(act.Index, package.GetItemAt<ItemData>(act.Index));
  73. break;
  74. case UpdateAction.ActionType.Remove:
  75. UpdateItem(act.Index, package.GetItemAt<ItemData>(act.Index));
  76. break;
  77. case UpdateAction.ActionType.UpdateCount:
  78. UpdateItem(act.Index, package.GetItemAt<ItemData>(act.Index));
  79. break;
  80. default:
  81. throw new ArgumentOutOfRangeException();
  82. }
  83. }
  84. }
  85. }
  86. }