From 5b4317a1752f5502af7cf79a8e22c3987fc92f5f Mon Sep 17 00:00:00 2001 From: Robert Peter Meyer <73112377+BoBoBaSs84@users.noreply.github.com> Date: Wed, 13 Jul 2022 19:01:25 +0200 Subject: [PATCH] 3 buy sell menu (#14) * good first take, test it * BugFixes after some testing ... * index fix * final clean up Co-authored-by: Bobo --- GUI/DealMenu.cs | 438 +++++++++++---------------------- GUI/Elements/BuyMenu.cs | 52 ++++ GUI/Elements/DrugListItem.cs | 163 ++++++++++++ GUI/Elements/SellMenu.cs | 52 ++++ GUI/Elements/StatisticsMenu.cs | 53 ++++ GUI/NativeMenuLeft.cs | 26 -- GUI/NativeMenuMiddle.cs | 26 -- GUI/NativeMenuRight.cs | 26 -- Los.Santos.Dope.Wars.csproj | 1 + Properties/AssemblyInfo.cs | 2 +- 10 files changed, 466 insertions(+), 373 deletions(-) create mode 100644 GUI/Elements/BuyMenu.cs create mode 100644 GUI/Elements/DrugListItem.cs create mode 100644 GUI/Elements/SellMenu.cs create mode 100644 GUI/Elements/StatisticsMenu.cs delete mode 100644 GUI/NativeMenuLeft.cs delete mode 100644 GUI/NativeMenuMiddle.cs delete mode 100644 GUI/NativeMenuRight.cs diff --git a/GUI/DealMenu.cs b/GUI/DealMenu.cs index 593d8792..b64d091f 100644 --- a/GUI/DealMenu.cs +++ b/GUI/DealMenu.cs @@ -1,9 +1,9 @@ using GTA; using LemonUI; -using LemonUI.Elements; using LemonUI.Menus; using Los.Santos.Dope.Wars.Classes; using Los.Santos.Dope.Wars.Extension; +using Los.Santos.Dope.Wars.GUI.Elements; using Los.Santos.Dope.Wars.Persistence.State; using System; using System.Collections.Generic; @@ -17,32 +17,48 @@ namespace Los.Santos.Dope.Wars.GUI /// public class DealMenu : Script { - private static readonly ObjectPool ObjectPool = new(); - private static NativeMenu SellMenu = null!; - private static NativeMenu BuyMenu = null!; - private static NativeMenu StatsMenu = null!; - private static NativeItem StatsMenuItem = null!; - private static NativeItem ToSellSwitch = null!; - private static NativeItem ToBuySwitch = null!; + #region fields + private static readonly ObjectPool objectPool = new(); + private static SellMenu sellMenu = null!; + private static BuyMenu buyMenu = null!; + private static StatisticsMenu statisticsMenu = null!; + private static NativeItem? toSellMenuSwitch; + private static NativeItem? toBuyMenuSwitch; private static GameState? gameState; private static PlayerStats? playerStats; private static PlayerStash? playerStash; private static DealerStash? dealerStash; - internal bool MenuLoaded = false; + private static bool _dealMenuLoaded; + #endregion + #region properties /// /// The property of type , if set to true the deal menu should popup /// public static bool ShowDealMenu { get; set; } = false; + /// + /// The property indicates if the method was called + /// + public static bool Initialized { get; private set; } + #endregion + + #region constructor /// /// The standard constructor for class /// public DealMenu() { - Tick += DealMenu_OnTick; + toSellMenuSwitch = new NativeItem("Go to sell menu", "Want to sell instead of buying?"); + toSellMenuSwitch.Activated += ToSellMenuSwitchActivated; + toBuyMenuSwitch = new NativeItem("Go to buy menu", "Want to buy instead of selling?"); + toBuyMenuSwitch.Activated += ToBuyMenuSwitchActivated; + + Tick += OnTick; } + #endregion + #region public methods /// /// The method should be called from outside with the needed parameters /// @@ -55,123 +71,132 @@ public static void Init(PlayerStash playerStash, DealerStash dealerStash, GameSt DealMenu.dealerStash = dealerStash; DealMenu.gameState = gameState; playerStats = Utils.GetPlayerStatsFromModel(gameState); + Initialized = true; } + #endregion - private void DealMenu_OnTick(object sender, EventArgs e) + #region private methods + private void OnTick(object sender, EventArgs e) { - while (playerStash is null || dealerStash is null) - Wait(50); + if (!Initialized) + return; if (ShowDealMenu) { - if (!MenuLoaded) + if (!_dealMenuLoaded) { - MenuLoaded = true; - SetupMenu(); - Wait(10); - BuyMenu.Visible = true; - StatsMenu.Visible = true; + LoadDealMenu(); + _dealMenuLoaded = true; + buyMenu.Visible = true; + statisticsMenu.Visible = true; } } else { - BuyMenu.Visible = false; - SellMenu.Visible = false; - StatsMenu.Visible = false; - MenuLoaded = false; + buyMenu.Visible = false; + sellMenu.Visible = false; + statisticsMenu.Visible = false; + UnloadDealMenu(); } - while (SellMenu is null || BuyMenu is null) - Wait(50); - - ObjectPool.Process(); + objectPool.Process(); } - private void SetupMenu() + private void LoadDealMenu() { try { - SellMenu = new NativeMenuRight("Sell", $" ", GetMenuBannerColor()); - BuyMenu = new NativeMenuLeft("Buy", $"Dealer Money: ${dealerStash.DrugMoney}", GetMenuBannerColor()); - StatsMenu = new NativeMenuMiddle($"Statistics - {Utils.GetCharacterFromModel()}", GetMenuBannerColor()) { AcceptsInput = false }; - - StatsMenuItem = GetStatsMenuItem(); - StatsMenu.Add(StatsMenuItem); - - ObjectPool.Add(BuyMenu); - ObjectPool.Add(SellMenu); - ObjectPool.Add(StatsMenu); - - ToSellSwitch = new NativeItem("Go to sell menu", "Want to sell instead of buying?"); - BuyMenu.Add(ToSellSwitch); - - ToBuySwitch = new NativeItem("Go to buy menu", "Want to buy instead of selling?"); - SellMenu.Add(ToBuySwitch); - - SellMenu.ItemActivated += Menu_OnItemActivated; - BuyMenu.ItemActivated += Menu_OnItemActivated; - - foreach (Drug dealerDrug in dealerStash.Drugs) - { - string pon = GetGoodOrBadPrice(dealerDrug.CurrentPrice, dealerDrug.AveragePrice); - string inPercent = GetDifferenceInPercent(dealerDrug.CurrentPrice, dealerDrug.AveragePrice); - - NativeListItem nativeListItem = new($"{dealerDrug.Name}", 0) - { - Description = $"Market price:\t\t${dealerDrug.AveragePrice}\n" + - $"Current price:\t\t{pon}${dealerDrug.CurrentPrice} ({inPercent})\n" + - $"~w~Purchase price:\t{dealerDrug.Quantity} x {pon}${dealerDrug.CurrentPrice} ~w~= {pon}${dealerDrug.Quantity * dealerDrug.CurrentPrice}" - }; - - for (int j = 1; j <= dealerDrug.Quantity; j++) - nativeListItem.Add(j); - - if (dealerDrug.Quantity.Equals(0)) - nativeListItem.Enabled = false; - - nativeListItem.SelectedIndex = dealerDrug.Quantity; - nativeListItem.SelectedIndex = dealerDrug.Quantity; - - nativeListItem.Activated += NativeListItem_OnActivated; - nativeListItem.ItemChanged += BuyListItem_OnItemChanged; - BuyMenu.Add(nativeListItem); - } - - foreach (Drug playerDrug in playerStash.Drugs) - { - // we need the current price of the drug, only the opposing dealer can give that... - int currentDrugPrice = dealerStash.Drugs.Where(x => x.Name.Equals(playerDrug.Name)).Select(x => x.CurrentPrice).FirstOrDefault(); - playerDrug.CurrentPrice = currentDrugPrice; - - string pon = GetGoodOrBadPrice(playerDrug.CurrentPrice, playerDrug.PurchasePrice, true); - string inPercent = GetDifferenceInPercent(playerDrug.CurrentPrice, playerDrug.PurchasePrice, true); - - NativeListItem nativeListItem = new($"{playerDrug.Name}", 0) - { - Description = $"Purchase price:\t${playerDrug.PurchasePrice}\n" + - $"Current price:\t\t{pon}${playerDrug.CurrentPrice} ({inPercent})\n" + - $"~w~Selling price:\t\t{playerDrug.Quantity} x {pon}${playerDrug.CurrentPrice} ~w~= {pon}${playerDrug.Quantity * playerDrug.CurrentPrice}" - }; - - for (int j = 1; j <= playerDrug.Quantity; j++) - nativeListItem.Add(j); + sellMenu = new SellMenu("Sell", $"", GetMenuBannerColor()); + buyMenu = new BuyMenu("Buy", $"Dealer Money: ${dealerStash.DrugMoney}", GetMenuBannerColor()); + + statisticsMenu = new StatisticsMenu($"Statistics - {Utils.GetCharacterFromModel()}", "", GetMenuBannerColor()) { AcceptsInput = false }; + statisticsMenu.Add(GetStatsMenuItem()); + + objectPool.Add(buyMenu); + objectPool.Add(sellMenu); + objectPool.Add(statisticsMenu); + + SetRefreshBuyMenu(dealerStash); + SetRefreshSellMenu(playerStash, dealerStash); + } + catch (Exception ex) + { + Logger.Error($"{nameof(LoadDealMenu)}\n{ex.Message}\n{ex.InnerException}\n{ex.Source}\n{ex.StackTrace}"); + } + } - if (playerDrug.Quantity.Equals(0)) - nativeListItem.Enabled = false; + private static void UnloadDealMenu() + { + statisticsMenu.Clear(); + buyMenu.Clear(); + sellMenu.Clear(); + _dealMenuLoaded = false; + } - nativeListItem.SelectedIndex = playerDrug.Quantity; - nativeListItem.SelectedIndex = playerDrug.Quantity; + /// + /// The method for switching to the buy menu + /// + /// + /// + private void ToBuyMenuSwitchActivated(object sender, EventArgs e) + { + buyMenu.Visible = !buyMenu.Visible; + sellMenu.Visible = !sellMenu.Visible; + } - nativeListItem.Activated += NativeListItem_OnActivated; - nativeListItem.ItemChanged += SellListItem_OnItemChanged; + /// + /// The method for switching to the sell menu + /// + /// + /// + private void ToSellMenuSwitchActivated(object sender, EventArgs e) + { + buyMenu.Visible = !buyMenu.Visible; + sellMenu.Visible = !sellMenu.Visible; + } - SellMenu.Add(nativeListItem); - } + /// + /// The method sets or refreshes the buy menu + /// + /// + private void SetRefreshBuyMenu(DealerStash dealerStash) + { + int index = buyMenu.SelectedIndex; + buyMenu.Clear(); + buyMenu.Subtitle = $"Dealer Money: ${dealerStash.DrugMoney}"; + buyMenu.Add(toSellMenuSwitch); + foreach (Drug drug in dealerStash.Drugs) + { + DrugListItem drugListItem = new(drug); + drugListItem.Activated += NativeListItem_OnActivated; + buyMenu.Add(drugListItem); } - catch (Exception ex) + if (index > -1) + buyMenu.SelectedIndex = index; + } + + /// + /// The method sets or refreshes the sell menu + /// + /// + /// + private void SetRefreshSellMenu(PlayerStash playerStash, DealerStash dealerStash) + { + int index = sellMenu.SelectedIndex; + sellMenu.Clear(); + sellMenu.Add(toBuyMenuSwitch); + foreach (Drug drug in playerStash.Drugs) { - Logger.Error($"{nameof(SetupMenu)}\n{ex.Message}\n{ex.InnerException}\n{ex.Source}\n{ex.StackTrace}"); + // we need the current price of the drug, only the opposing dealer can give that... + int currentDrugPrice = dealerStash.Drugs.Where(x => x.Name.Equals(drug.Name)).Select(x => x.CurrentPrice).FirstOrDefault(); + drug.CurrentPrice = currentDrugPrice; + + DrugListItem drugListItem = new(drug, true); + drugListItem.Activated += NativeListItem_OnActivated; + sellMenu.Add(drugListItem); } + if (index > -1) + sellMenu.SelectedIndex = index; } /// @@ -195,9 +220,9 @@ private static NativeItem GetStatsMenuItem() NativeItem nativeItem = new(title, description) { - //LeftBadge = new ScaledTexture("commonmenu", "shop_new_star"), Panel = new NativeStatsPanel(nativeStatsInfos.ToArray()) }; + return nativeItem; } @@ -216,158 +241,16 @@ private static Color GetMenuBannerColor() }; } - /// - /// Returns price difference in percent, already colored - /// - /// Should be the current price - /// Should be the market or purchase price - /// - /// - private static string GetDifferenceInPercent(int valueOne, int valueTwo, bool isPlayer = false) - { - double resultValue = (valueOne / (double)valueTwo * 100) - 100; - - if (resultValue > 0) - return $"{(isPlayer ? "~g~+" : "~r~+")}{resultValue:n2}%"; - else if (resultValue < 0) - return $"{(isPlayer ? "~r~" : "~g~")}{resultValue:n2}%"; - else - return $"~w~{resultValue:n2}%"; - } - - /// - /// Returns the color it green, red or white string if it good, bad or neutral - /// - /// Should be the current price - /// Should be the market or purchase price - /// - /// - private static string GetGoodOrBadPrice(int valueOne, int valueTwo, bool isPlayer = false) - { - if (valueOne > valueTwo) - return isPlayer ? "~g~" : "~r~"; - if (valueOne < valueTwo) - return isPlayer ? "~r~" : "~g~"; - return "~w~"; - } - - private void DrugMenuRefresh(NativeMenu sourceMenu, NativeMenu targetMenu, string drugName, int drugQuantity, int currentDrugPrice) - { - try - { - if (sourceMenu is NativeMenuLeft) - { - NativeListItem targetMenuItem = targetMenu.Items.Where(x => x.Title.Equals(drugName)).FirstOrDefault() as NativeListItem; - - int currentItemMax = targetMenuItem.Items.Max(); - - for (int i = currentItemMax + 1; i <= currentItemMax + drugQuantity; i++) - targetMenuItem.Items.Add(i); - - if (targetMenuItem.Items.Count > 1) - { - targetMenuItem.SelectedIndex = targetMenuItem.Items.Max(); - targetMenuItem.SelectedItem = targetMenuItem.Items.Max(); - targetMenuItem.Enabled = true; - } - else - { - targetMenuItem.Enabled = false; - } - } - - if (sourceMenu is NativeMenuRight) - { - NativeListItem targetMenuItem = targetMenu.Items.Where(x => x.Title.Equals(drugName)).FirstOrDefault() as NativeListItem; - - int currentItemMax = targetMenuItem.Items.Max(); - - for (int i = currentItemMax + 1; i <= currentItemMax + drugQuantity; i++) - targetMenuItem.Items.Add(i); - - if (targetMenuItem.Items.Count > 1) - { - targetMenuItem.SelectedIndex = targetMenuItem.Items.Max(); - targetMenuItem.SelectedItem = targetMenuItem.Items.Max(); - targetMenuItem.Enabled = true; - } - else - { - targetMenuItem.Enabled = false; - } - } - } - catch (Exception ex) - { - Logger.Error($"{nameof(DrugMenuRefresh)}\n{ex.Message}\n{ex.InnerException}\n{ex.Source}\n{ex.StackTrace}"); - } - } - - private void BuyListItem_OnItemChanged(object sender, ItemChangedEventArgs e) - { - try - { - NativeListItem nli = sender as NativeListItem; - - Drug dealerDrug = dealerStash.Drugs.Where(x => x.Name.Equals(nli.Title)).SingleOrDefault(); - if (dealerDrug is null) - return; - - string pon = GetGoodOrBadPrice(dealerDrug.CurrentPrice, dealerDrug.AveragePrice); - string inPercent = GetDifferenceInPercent(dealerDrug.CurrentPrice, dealerDrug.AveragePrice); - - nli.Description = $"Market price:\t\t${dealerDrug.AveragePrice}\n" + - $"Current price:\t\t{pon}${dealerDrug.CurrentPrice} ({inPercent})\n" + - $"~w~Purchase price:\t{e.Object} x {pon}${dealerDrug.CurrentPrice} ~w~= {pon}${e.Object * dealerDrug.CurrentPrice}"; - } - catch (Exception ex) - { - Logger.Error($"{nameof(BuyListItem_OnItemChanged)}\n{ex.Message}\n{ex.InnerException}\n{ex.Source}\n{ex.StackTrace}"); - } - } - - private void SellListItem_OnItemChanged(object sender, ItemChangedEventArgs e) - { - try - { - NativeListItem nli = sender as NativeListItem; - - Drug playerDrug = playerStash.Drugs.Where(x => x.Name.Equals(nli.Title)).SingleOrDefault(); - if (playerDrug is null) - return; - - string pon = GetGoodOrBadPrice(playerDrug.CurrentPrice, playerDrug.PurchasePrice, true); - string inPercent = GetDifferenceInPercent(playerDrug.CurrentPrice, playerDrug.PurchasePrice, true); - - nli.Description = $"Purchase price:\t${playerDrug.PurchasePrice}\n" + - $"Current price:\t\t{pon}${playerDrug.CurrentPrice} ({inPercent})\n" + - $"~w~Selling price:\t\t{e.Object} x {pon}${playerDrug.CurrentPrice} ~w~= {pon}${e.Object * playerDrug.CurrentPrice}"; - } - catch (Exception ex) - { - Logger.Error($"{nameof(SellListItem_OnItemChanged)}\n{ex.Message}\n{ex.InnerException}\n{ex.Source}\n{ex.StackTrace}"); - } - } - - private void Menu_OnItemActivated(object sender, ItemActivatedArgs e) - { - if (e.Item.Equals(ToSellSwitch) || e.Item.Equals(ToBuySwitch)) - { - BuyMenu.Visible = !BuyMenu.Visible; - SellMenu.Visible = !SellMenu.Visible; - } - } - private void NativeListItem_OnActivated(object sender, EventArgs e) { - NativeMenuLeft nativeMenuLeft; - NativeMenuRight nativeMenuRight; + BuyMenu nativeMenuLeft; + SellMenu nativeMenuRight; try { - if (sender is NativeMenuLeft) + if (sender is BuyMenu) { - nativeMenuLeft = sender as NativeMenuLeft; + nativeMenuLeft = sender as BuyMenu; if (nativeMenuLeft.SelectedItem is NativeListItem) { NativeListItem nativeListItem = nativeMenuLeft.SelectedItem as NativeListItem; @@ -383,7 +266,7 @@ private void NativeListItem_OnActivated(object sender, EventArgs e) // early exit if (Game.Player.Money < transactionValue) { - GTA.UI.Screen.ShowSubtitle("You don't have enough ~y~money bitch! Fuck off!"); + GTA.UI.Screen.ShowSubtitle("You don't have enough ~y~money ~w~bitch! ~r~Fuck off~w~!"); return; } @@ -391,33 +274,17 @@ private void NativeListItem_OnActivated(object sender, EventArgs e) playerStash.BuyDrug(drugName, drugQuantity, drugPrice); dealerStash.SellDrug(drugName, drugQuantity, drugPrice); + SetRefreshBuyMenu(dealerStash); + SetRefreshSellMenu(playerStash, dealerStash); + playerStats.SpentMoney += transactionValue; GTA.UI.Screen.ShowSubtitle($"You got yourself {drugQuantity} packs of ~y~{drugName} ~w~with a total value of ~r~${transactionValue}."); Audio.PlaySoundFrontend("PURCHASE", "HUD_LIQUOR_STORE_SOUNDSET"); - - DrugMenuRefresh(nativeMenuLeft, SellMenu, drugName, drugQuantity, drugPrice); - - // the zero "0" is/should always be the last item - // 30 items plus 0 means 31 take 15 out of it result in 16 - int resultingQuantity = nativeListItem.Items.Count - 1 - drugQuantity; - - for (int i = nativeListItem.Items.Count; i > resultingQuantity; i--) - nativeListItem.Remove(i); - - // back on track set - nativeListItem.SelectedIndex = resultingQuantity; - nativeListItem.SelectedItem = resultingQuantity; - - // deactivate if result would be 0 aka zero - if (resultingQuantity.Equals(0)) - nativeListItem.Enabled = false; - else - nativeListItem.Enabled = true; } } - if (sender is NativeMenuRight) + if (sender is SellMenu) { - nativeMenuRight = sender as NativeMenuRight; + nativeMenuRight = sender as SellMenu; if (nativeMenuRight.SelectedItem is NativeListItem) { NativeListItem nativeListItem = nativeMenuRight.SelectedItem as NativeListItem; @@ -451,30 +318,12 @@ private void NativeListItem_OnActivated(object sender, EventArgs e) GTA.UI.Screen.ShowSubtitle($"You peddled {drugQuantity} packs of ~y~{drugName} ~w~for a total value of ~g~${transactionValue}."); Audio.PlaySoundFrontend("PURCHASE", "HUD_LIQUOR_STORE_SOUNDSET"); - DrugMenuRefresh(nativeMenuRight, BuyMenu, drugName, drugQuantity, currentDrugPrice); - - // the zero "0" is/should always be the last item - // 30 items plus 0 means 31 take 15 out of it result in 16 - int resultingQuantity = nativeListItem.Items.Count - 1 - drugQuantity; - - for (int i = nativeListItem.Items.Count; i > resultingQuantity; i--) - nativeListItem.Remove(i); - - // back on track set - nativeListItem.SelectedIndex = resultingQuantity; - nativeListItem.SelectedItem = resultingQuantity; - - // deactivate if result would be 0 aka zero - if (resultingQuantity.Equals(0)) - nativeListItem.Enabled = false; - else - nativeListItem.Enabled = true; + SetRefreshBuyMenu(dealerStash); + SetRefreshSellMenu(playerStash, dealerStash); } } - - StatsMenu.Remove(StatsMenuItem); - StatsMenuItem = GetStatsMenuItem(); - StatsMenu.Add(StatsMenuItem); + statisticsMenu.Clear(); + statisticsMenu.Add(GetStatsMenuItem()); Utils.SaveGameState(gameState!); } catch (Exception ex) @@ -483,4 +332,5 @@ private void NativeListItem_OnActivated(object sender, EventArgs e) } } } -} + #endregion +} \ No newline at end of file diff --git a/GUI/Elements/BuyMenu.cs b/GUI/Elements/BuyMenu.cs new file mode 100644 index 00000000..f3882f87 --- /dev/null +++ b/GUI/Elements/BuyMenu.cs @@ -0,0 +1,52 @@ +using GTA.UI; +using LemonUI.Menus; +using System; + +namespace Los.Santos.Dope.Wars.GUI.Elements +{ + /// + /// The class serves as the buy menu at the right of the screen, abstracts from + /// + public class BuyMenu : NativeMenu + { + /// + /// The property + /// + public bool BuyMenuShown { get; private set; } + + /// + /// The property + /// + public bool BuyMenuClosed { get; private set; } + + /// + /// The standard constructor with standard values + /// + /// + /// + /// + public BuyMenu(string title, string subtitle, System.Drawing.Color color) : base(title, subtitle) + { + Alignment = Alignment.Left; + Banner.Color = color; + ItemCount = CountVisibility.Always; + Offset = new System.Drawing.PointF(Constants.ScreeSize.Width / 64, Constants.ScreeSize.Height / 36); + UseMouse = false; + TitleFont = Font.Pricedown; + Closed += BuyMenu_Closed; + Shown += BuyMenu_Shown; + } + + private void BuyMenu_Shown(object sender, EventArgs e) + { + BuyMenuClosed = false; + BuyMenuShown = true; + } + + private void BuyMenu_Closed(object sender, EventArgs e) + { + BuyMenuClosed = true; + BuyMenuShown = false; + } + } +} diff --git a/GUI/Elements/DrugListItem.cs b/GUI/Elements/DrugListItem.cs new file mode 100644 index 00000000..e2e4b5f2 --- /dev/null +++ b/GUI/Elements/DrugListItem.cs @@ -0,0 +1,163 @@ +using LemonUI.Menus; +using Los.Santos.Dope.Wars.Classes; +using System; + +namespace Los.Santos.Dope.Wars.GUI.Elements +{ + /// + /// The class serves as the buy and sell menu list item, abstracts from + /// + public class DrugListItem : NativeListItem + { + #region fields + private readonly bool _isPlayerItem; + #endregion + + #region constructor + /// + /// The standard constructor + /// + /// + /// + public DrugListItem(Drug drug, bool isPlayerItem = false) : base(drug.Name, GetIntArrayFromDrugQuantity(drug.Quantity)) + { + _isPlayerItem = isPlayerItem; + if (drug.Quantity.Equals(0)) + Enabled = false; + Tag = drug; + SelectedIndex = drug.Quantity; + LeftBadgeSet = GetDrugBadgeSet(drug.Name); + RefreshDescription(drug, drug.Quantity, isPlayerItem); + ItemChanged += OnItemChanged; + } + #endregion + + #region private methods + /// + /// The simply refreshes the item description if the item value has changed + /// + /// + /// + private void OnItemChanged(object sender, ItemChangedEventArgs e) + { + if (Tag is not Drug drug) + return; + RefreshDescription(drug, e.Object, _isPlayerItem); + } + + /// + /// The returns an array of integers starting from zero ... + /// + /// + /// + private static int[] GetIntArrayFromDrugQuantity(int quantity) + { + int[] array = new int[quantity + 1]; + for (int i = 0; i <= quantity; i++) + array[i] = i; + return array; + } + + /// + /// The method returns a corresponding badgeset + /// + /// + /// + private static BadgeSet GetDrugBadgeSet(string drugName) + { + BadgeSet cokeBadgeSet = new("mpinventory", "mp_specitem_coke", "mp_specitem_coke_black"); + BadgeSet methBadgeSet = new("mpinventory", "mp_specitem_meth", "mp_specitem_meth_black"); + BadgeSet weedBadgeSet = new("mpinventory", "mp_specitem_weed", "mp_specitem_weed_black"); + BadgeSet heroBadgeSet = new("mpinventory", "mp_specitem_heroin", "mp_specitem_heroin_black"); + BadgeSet defaultBadgeSet = new("mpinventory", "mp_specitem_cash", "mp_specitem_cash_black"); + + if (Enums.DrugTypes.Cocaine.ToString().Equals(drugName)) + return cokeBadgeSet; + if (Enums.DrugTypes.Heroin.ToString().Equals(drugName)) + return heroBadgeSet; + if (Enums.DrugTypes.Marijuana.ToString().Equals(drugName)) + return weedBadgeSet; + if (Enums.DrugTypes.Hashish.ToString().Equals(drugName)) + return weedBadgeSet; + if (Enums.DrugTypes.Mushrooms.ToString().Equals(drugName)) + return weedBadgeSet; + if (Enums.DrugTypes.Amphetamine.ToString().Equals(drugName)) + return cokeBadgeSet; + if (Enums.DrugTypes.PCP.ToString().Equals(drugName)) + return cokeBadgeSet; + if (Enums.DrugTypes.Methamphetamine.ToString().Equals(drugName)) + return methBadgeSet; + if (Enums.DrugTypes.Ketamine.ToString().Equals(drugName)) + return cokeBadgeSet; + if (Enums.DrugTypes.Mescaline.ToString().Equals(drugName)) + return methBadgeSet; + if (Enums.DrugTypes.Ecstasy.ToString().Equals(drugName)) + return methBadgeSet; + if (Enums.DrugTypes.Acid.ToString().Equals(drugName)) + return methBadgeSet; + if (Enums.DrugTypes.MDMA.ToString().Equals(drugName)) + return methBadgeSet; + if (Enums.DrugTypes.Crack.ToString().Equals(drugName)) + return heroBadgeSet; + if (Enums.DrugTypes.Oxycodone.ToString().Equals(drugName)) + return methBadgeSet; + else return defaultBadgeSet; + } + + /// + /// The method refreshes the item description + /// + /// + /// + /// + private void RefreshDescription(Drug drug, int drugQuantity, bool isPlayerItem) + { + int valueOne = drug.CurrentPrice; + int valueTwo = isPlayerItem ? drug.PurchasePrice : drug.AveragePrice; + + string pon = GetGoodOrBadPrice(valueOne, valueTwo, isPlayerItem); + string inPercent = GetDifferenceInPercent(valueOne, valueTwo, isPlayerItem); + + Description = isPlayerItem ? $"Purchase price:\t${drug.PurchasePrice}\n" : $"Market price:\t\t${drug.AveragePrice}\n"; + Description = Description + + $"Current price:\t\t{pon}${drug.CurrentPrice} ({inPercent})\n" + + $"~w~Purchase price:\t{drugQuantity} x {pon}${drug.CurrentPrice} ~w~= {pon}${drugQuantity * drug.CurrentPrice}"; + } + + /// + /// Returns price difference in percent, already colored + /// + /// Should be the current price + /// Should be the market or purchase price + /// + /// + private static string GetDifferenceInPercent(int valueOne, int valueTwo, bool isPlayer = false) + { + double resultValue = (valueOne / (double)valueTwo * 100) - 100; + + if (resultValue > 0) + return $"{(isPlayer ? "~g~+" : "~r~+")}{resultValue:n2}%"; + else if (resultValue < 0) + return $"{(isPlayer ? "~r~" : "~g~")}{resultValue:n2}%"; + else + return $"~w~{resultValue:n2}%"; + } + + /// + /// Returns the color it green, red or white string if it good, bad or neutral + /// + /// Should be the current price + /// Should be the market or purchase price + /// + /// + private static string GetGoodOrBadPrice(int valueOne, int valueTwo, bool isPlayer = false) + { + if (valueOne > valueTwo) + return isPlayer ? "~g~" : "~r~"; + if (valueOne < valueTwo) + return isPlayer ? "~r~" : "~g~"; + return "~w~"; + } + #endregion + } +} \ No newline at end of file diff --git a/GUI/Elements/SellMenu.cs b/GUI/Elements/SellMenu.cs new file mode 100644 index 00000000..8e67f01f --- /dev/null +++ b/GUI/Elements/SellMenu.cs @@ -0,0 +1,52 @@ +using GTA.UI; +using LemonUI.Menus; +using System; + +namespace Los.Santos.Dope.Wars.GUI.Elements +{ + /// + /// The class serves as the sell menu at the left of the screen, abstracts from + /// + public class SellMenu : NativeMenu + { + /// + /// The property + /// + public bool SellMenuShown { get; private set; } + + /// + /// The property + /// + public bool SellMenuClosed { get; private set; } + + /// + /// The standard constructor with standard values + /// + /// + /// + /// + public SellMenu(string title, string subtitle, System.Drawing.Color color) : base(title, subtitle) + { + Alignment = Alignment.Right; + Banner.Color = color; + ItemCount = CountVisibility.Always; + Offset = new System.Drawing.PointF(-Constants.ScreeSize.Width / 64, Constants.ScreeSize.Height / 36); + UseMouse = false; + TitleFont = Font.Pricedown; + Closed += SellMenu_Closed; + Shown += SellMenu_Shown; + } + + private void SellMenu_Shown(object sender, EventArgs e) + { + SellMenuClosed = false; + SellMenuShown = true; + } + + private void SellMenu_Closed(object sender, EventArgs e) + { + SellMenuClosed = true; + SellMenuShown = false; + } + } +} diff --git a/GUI/Elements/StatisticsMenu.cs b/GUI/Elements/StatisticsMenu.cs new file mode 100644 index 00000000..ea625562 --- /dev/null +++ b/GUI/Elements/StatisticsMenu.cs @@ -0,0 +1,53 @@ +using GTA.UI; +using LemonUI.Menus; +using System; + +namespace Los.Santos.Dope.Wars.GUI.Elements +{ + /// + /// The class serves as the statistics menu in the middle of the screen, abstracts from + /// + public class StatisticsMenu : NativeMenu + { + /// + /// The property + /// + public bool StatisticsMenuShown { get; private set; } + + /// + /// The property + /// + public bool StatisticsMenuClosed { get; private set; } + + + /// + /// The standard constructor with standard values + /// + /// + /// + /// + public StatisticsMenu(string title, string subtitle, System.Drawing.Color color) : base(title, subtitle) + { + Alignment = Alignment.Left; + Banner.Color = color; + ItemCount = CountVisibility.Never; + Offset = new System.Drawing.PointF(Constants.ScreeSize.Width / 3, Constants.ScreeSize.Width / 3); + UseMouse = false; + Width = Constants.ScreeSize.Width / 3; + Closed += StatisticsMenu_Closed; + Shown += StatisticsMenu_Shown; + } + + private void StatisticsMenu_Shown(object sender, EventArgs e) + { + StatisticsMenuClosed = false; + StatisticsMenuShown = true; + } + + private void StatisticsMenu_Closed(object sender, EventArgs e) + { + StatisticsMenuClosed = true; + StatisticsMenuShown = false; + } + } +} diff --git a/GUI/NativeMenuLeft.cs b/GUI/NativeMenuLeft.cs deleted file mode 100644 index 8f733d58..00000000 --- a/GUI/NativeMenuLeft.cs +++ /dev/null @@ -1,26 +0,0 @@ -using GTA.UI; -using LemonUI.Menus; - -namespace Los.Santos.Dope.Wars.GUI -{ - /// - /// The class serves as the standard left menu class, abstracts from - /// - public class NativeMenuLeft : NativeMenu - { - /// - /// The standard constructor with standard values - /// - /// - /// - /// - public NativeMenuLeft(string title, string subtitle, System.Drawing.Color color) : base(title, subtitle) - { - Alignment = Alignment.Left; - Banner.Color = color; - Offset = new System.Drawing.PointF(Constants.ScreeSize.Width / 64, Constants.ScreeSize.Height / 36); - UseMouse = false; - TitleFont = Font.Pricedown; - } - } -} diff --git a/GUI/NativeMenuMiddle.cs b/GUI/NativeMenuMiddle.cs deleted file mode 100644 index 2cc339ab..00000000 --- a/GUI/NativeMenuMiddle.cs +++ /dev/null @@ -1,26 +0,0 @@ -using GTA.UI; -using LemonUI.Menus; - -namespace Los.Santos.Dope.Wars.GUI -{ - /// - /// The class serves as the standard middle menu class, abstracts from - /// - public class NativeMenuMiddle : NativeMenu - { - /// - /// The standard constructor with standard values - /// - /// - /// - public NativeMenuMiddle(string title, System.Drawing.Color color) : base(title) - { - Alignment = Alignment.Left; - Banner.Color = color; - ItemCount = CountVisibility.Never; - Offset = new System.Drawing.PointF(Constants.ScreeSize.Width / 3, Constants.ScreeSize.Width / 3); - UseMouse = false; - Width = Constants.ScreeSize.Width / 3; - } - } -} diff --git a/GUI/NativeMenuRight.cs b/GUI/NativeMenuRight.cs deleted file mode 100644 index 58362779..00000000 --- a/GUI/NativeMenuRight.cs +++ /dev/null @@ -1,26 +0,0 @@ -using GTA.UI; -using LemonUI.Menus; - -namespace Los.Santos.Dope.Wars.GUI -{ - /// - /// The class serves as the standard right menu class, abstracts from - /// - public class NativeMenuRight : NativeMenu - { - /// - /// The standard constructor with standard values - /// - /// - /// - /// - public NativeMenuRight(string title, string subtitle, System.Drawing.Color color) : base(title, subtitle) - { - Alignment = Alignment.Right; - Banner.Color = color; - Offset = new System.Drawing.PointF(-Constants.ScreeSize.Width / 64, Constants.ScreeSize.Height / 36); - UseMouse = false; - TitleFont = Font.Pricedown; - } - } -} diff --git a/Los.Santos.Dope.Wars.csproj b/Los.Santos.Dope.Wars.csproj index 54fc9e37..4322385b 100644 --- a/Los.Santos.Dope.Wars.csproj +++ b/Los.Santos.Dope.Wars.csproj @@ -2,6 +2,7 @@ net48 + false Latest enable false diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 22fb1d5c..2ba450a6 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -6,7 +6,7 @@ [assembly: AssemblyProduct("Los.Santos.Dope.Wars")] [assembly: AssemblyDescription("Grand Theft Auto V - Los Santos Dope Wars")] [assembly: AssemblyTitle("Los.Santos.Dope.Wars")] -[assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.1.*")] [assembly: AssemblyCopyright("Copyright © 2022")] [assembly: Guid("38E8CEE1-3992-441E-A494-9FEF5CAEDB9E")] [assembly: ComVisible(false)]