From d9edcfb16322967fde5f88bd7aff2a9093faa4dd Mon Sep 17 00:00:00 2001 From: Adam Rhodes Date: Fri, 12 Mar 2021 16:53:33 +0000 Subject: [PATCH] Add repair button for items with durability --- Loki/InventorySlot.cs | 18 +++- Loki/InventorySlotEditor.xaml | 181 ++++++++++++++++++++-------------- Loki/Item.cs | 27 ++++- Loki/Loki.csproj | 2 + Loki/RelayCommand.cs | 4 + Loki/Resources/wrench-32.ico | Bin 0 -> 4286 bytes 6 files changed, 157 insertions(+), 75 deletions(-) create mode 100644 Loki/Resources/wrench-32.ico diff --git a/Loki/InventorySlot.cs b/Loki/InventorySlot.cs index 02f66bb..4e5e956 100644 --- a/Loki/InventorySlot.cs +++ b/Loki/InventorySlot.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Diagnostics; using System.Runtime.CompilerServices; using System.Windows.Input; using JetBrains.Annotations; @@ -12,6 +13,8 @@ public class InventorySlot: INotifyPropertyChanged public InventorySlot(Vector2i position) { Position = position; + RepairItem = new RelayCommand(RepairExecuted, RepairCanExecute); + DeleteItem = new RelayCommand(_ => Item = null); if (position.Y == 0) QuickSlotNumber = position.X + 1; } @@ -19,7 +22,20 @@ public InventorySlot(Vector2i position) public int? QuickSlotNumber { get; } public bool IsEmpty => Item == null; - public ICommand DeleteItem => new RelayCommand(_ => Item = null); + public RelayCommand DeleteItem { get; } + + public RelayCommand RepairItem { get; } + + private bool RepairCanExecute(object _) + { + return Item != null && Item.Durability < Item.MaxDurability; + } + + private void RepairExecuted(object _) + { + Item.Durability = (float) Item.MaxDurability; + RepairItem.OnCanExecuteChanged(); + } public Item Item { diff --git a/Loki/InventorySlotEditor.xaml b/Loki/InventorySlotEditor.xaml index 7ffbffd..bc0709e 100644 --- a/Loki/InventorySlotEditor.xaml +++ b/Loki/InventorySlotEditor.xaml @@ -119,6 +119,51 @@ + + @@ -203,80 +248,72 @@ - - - + + + + + + + + - - diff --git a/Loki/Item.cs b/Loki/Item.cs index 96ebbd9..db4b9be 100644 --- a/Loki/Item.cs +++ b/Loki/Item.cs @@ -1,10 +1,14 @@ using System; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; namespace Loki { - public class Item + public class Item: INotifyPropertyChanged { private int _stack; + private float _durability; public string Name { get; } public int Stack @@ -12,6 +16,7 @@ public int Stack get => _stack; set { + if (value == _stack) return; if (SharedData != null) { if (value > SharedData.MaxStack) @@ -19,10 +24,21 @@ public int Stack } if (value < 0) value = 0; _stack = value; + OnPropertyChanged(); + } + } + + public float Durability + { + get => _durability; + set + { + if (value.Equals(_durability)) return; + _durability = value; + OnPropertyChanged(); } } - public float Durability { get; } public Vector2i Pos { get; } public bool Equiped { get; } public int Quality { get; } @@ -64,5 +80,12 @@ public Item(string name, int stack, float durability, Vector2i pos, bool equiped public bool CanStack => SharedData.MaxStack > 1; public override string ToString() => $"{Name} [{Stack}]"; + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } diff --git a/Loki/Loki.csproj b/Loki/Loki.csproj index 81934d0..29c718e 100644 --- a/Loki/Loki.csproj +++ b/Loki/Loki.csproj @@ -19,6 +19,7 @@ + @@ -36,6 +37,7 @@ + diff --git a/Loki/RelayCommand.cs b/Loki/RelayCommand.cs index eb4d9ad..7e4b216 100644 --- a/Loki/RelayCommand.cs +++ b/Loki/RelayCommand.cs @@ -1,5 +1,6 @@ using System; using System.Windows.Input; +using JetBrains.Annotations; namespace Loki { @@ -16,6 +17,9 @@ public RelayCommand(Action execute, Func canExecute = null public bool CanExecute(object param) => _canExecute?.Invoke(param) ?? true; public void Execute(object param) => _execute(param); + + [UsedImplicitly] public event EventHandler CanExecuteChanged; + public virtual void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } diff --git a/Loki/Resources/wrench-32.ico b/Loki/Resources/wrench-32.ico new file mode 100644 index 0000000000000000000000000000000000000000..38096ea76ee676554b55ca90306dd89ae53413b5 GIT binary patch literal 4286 zcmeH~u}dRS5XL8o7!l#*5DOcFm4(Q0g@uKtKIE_xu@DOjOaA~NQ;0%pxzfTytPYE- z#3BclVsV88vC!iX5fM?5AdBNSStp5MH`!#f-W_DaFK_d9cD`@t<-Oe{is6$=k$%%o zFHt`c4Fhyh8fd@o#?(zr-@pa91iK*Z8{g^!OdHS>53=Zseq%cG&^ABnE!vIgcMW~@ z5v>QN?K=9=dSDuZzVnwotOU@Hx(;jqfQQw#2TXUSb{%xp`+w^23topx3pG<5zx%*P z5Pqv&2c5SapbIR5eeeM69&(5MJunYCnl|I@VX2spX#%-Vfob}+$A}qgk%#Q7_AGKf z1<*E6kBI#m(nB6OI>9(70LvY;RS%y+cu3)V9doT`P|fPNL!28A;X&(OowL#3Uqa}A zZ-M?+2>m|+^>zNufD!Pc>YRL2`tJG?%qqE+PWKVYp*xn+9p!=aAaK#w@jNAb+md~J6AWKJAbF@#`y*4J5qTZP1oAy*H}M2 zuYUJlW15w14&HjaPHAZ$52*^}V(7`fdNcJPbhJ=9Fn$dw^rnkA)5_ z(mm{Zq-4OP2oH+nK7gdiSwiF%0R;p&2zb_jV$kxW{6y3_ lC*__JI1nr15Fu~@+=>$`h?8){L84~`$%<->HkU!>%nRoYY+(QZ literal 0 HcmV?d00001