Example #1
0
        private void doGridLayoutLeftClick(ButtonPressedEventArgs e)
        {
            var       forSale           = Helper.Reflection.GetField <List <ISalable> >(shop, "forSale").GetValue();
            var       itemPriceAndStock = Helper.Reflection.GetField <Dictionary <ISalable, int[]> >(shop, "itemPriceAndStock").GetValue();
            var       currency          = Helper.Reflection.GetField <int>(shop, "currency").GetValue();
            var       animations        = Helper.Reflection.GetField <List <TemporaryAnimatedSprite> >(shop, "animations").GetValue();
            var       poof             = Helper.Reflection.GetField <TemporaryAnimatedSprite>(shop, "poof").GetValue();
            var       heldItem         = Helper.Reflection.GetField <ISalable>(shop, "heldItem").GetValue();
            var       currentItemIndex = Helper.Reflection.GetField <int>(shop, "currentItemIndex").GetValue();
            var       sellPercentage   = Helper.Reflection.GetField <float>(shop, "sellPercentage").GetValue();
            var       scrollBar        = Helper.Reflection.GetField <ClickableTextureComponent>(shop, "scrollBar").GetValue();
            var       scrollBarRunner  = Helper.Reflection.GetField <Rectangle>(shop, "scrollBarRunner").GetValue();
            var       downArrow        = Helper.Reflection.GetField <ClickableTextureComponent>(shop, "downArrow").GetValue();
            var       upArrow          = Helper.Reflection.GetField <ClickableTextureComponent>(shop, "upArrow").GetValue();
            const int UNIT_WIDTH       = 160;
            const int UNIT_HEIGHT      = 144;
            int       unitsWide        = (shop.width - 32) / UNIT_WIDTH;

            int x = (int)e.Cursor.ScreenPixels.X;
            int y = (int)e.Cursor.ScreenPixels.Y;

            if (shop.upperRightCloseButton.containsPoint(x, y))
            {
                shop.exitThisMenu(true);
                return;
            }

            // Copying a lot from left click code
            if (downArrow.containsPoint(x, y) && currentItemIndex < Math.Max(0, forSale.Count - 18))
            {
                downArrow.scale = downArrow.baseScale;
                Helper.Reflection.GetField <int>(shop, "currentItemIndex").SetValue(currentItemIndex += 1);
                if (forSale.Count > 0)
                {
                    scrollBar.bounds.Y = scrollBarRunner.Height / Math.Max(1, (forSale.Count / 6) - 1 + 1) * currentItemIndex + upArrow.bounds.Bottom + 4;
                    if (currentItemIndex == forSale.Count / 6 - 1)
                    {
                        scrollBar.bounds.Y = downArrow.bounds.Y - scrollBar.bounds.Height - 4;
                    }
                }
                Game1.playSound("shwip");
            }
            else if (upArrow.containsPoint(x, y) && currentItemIndex > 0)
            {
                upArrow.scale = upArrow.baseScale;
                Helper.Reflection.GetField <int>(shop, "currentItemIndex").SetValue(currentItemIndex -= 1);
                if (forSale.Count > 0)
                {
                    scrollBar.bounds.Y = scrollBarRunner.Height / Math.Max(1, (forSale.Count / 6) - 1 + 1) * currentItemIndex + upArrow.bounds.Bottom + 4;
                    if (currentItemIndex == forSale.Count / 6 - 1)
                    {
                        scrollBar.bounds.Y = downArrow.bounds.Y - scrollBar.bounds.Height - 4;
                    }
                }
                Game1.playSound("shwip");
            }
            else if (scrollBarRunner.Contains(x, y))
            {
                int y1 = scrollBar.bounds.Y;
                scrollBar.bounds.Y = Math.Min(shop.yPositionOnScreen + shop.height - 64 - 12 - scrollBar.bounds.Height, Math.Max(y, shop.yPositionOnScreen + upArrow.bounds.Height + 20));
                currentItemIndex   = Math.Min(forSale.Count / 6 - 1, Math.Max(0, (int)((double)forSale.Count / 6 * (double)((float)(y - scrollBarRunner.Y) / (float)scrollBarRunner.Height))));
                Helper.Reflection.GetField <int>(shop, "currentItemIndex").SetValue(currentItemIndex);
                if (forSale.Count > 0)
                {
                    scrollBar.bounds.Y = scrollBarRunner.Height / Math.Max(1, (forSale.Count / 6) - 1 + 1) * currentItemIndex + upArrow.bounds.Bottom + 4;
                    if (currentItemIndex == forSale.Count / 6 - 1)
                    {
                        scrollBar.bounds.Y = downArrow.bounds.Y - scrollBar.bounds.Height - 4;
                    }
                }
                int y2 = scrollBar.bounds.Y;
                if (y1 == y2)
                {
                    return;
                }
                Game1.playSound("shiny4");
            }
            Vector2 clickableComponent = shop.inventory.snapToClickableComponent(x, y);

            if (heldItem == null)
            {
                Item obj = shop.inventory.leftClick(x, y, null, false);
                if (obj != null)
                {
                    if (shop.onSell != null)
                    {
                        shop.onSell(obj);
                    }
                    else
                    {
                        ShopMenu.chargePlayer(Game1.player, currency, -((obj is StardewValley.Object ? (int)((double)(obj as StardewValley.Object).sellToStorePrice() * (double)sellPercentage) : (int)((double)(obj.salePrice() / 2) * (double)sellPercentage)) * obj.Stack));
                        int num = obj.Stack / 8 + 2;
                        for (int index = 0; index < num; ++index)
                        {
                            animations.Add(new TemporaryAnimatedSprite("TileSheets\\debris", new Rectangle(Game1.random.Next(2) * 16, 64, 16, 16), 9999f, 1, 999, clickableComponent + new Vector2(32f, 32f), false, false)
                            {
                                alphaFade    = 0.025f,
                                motion       = new Vector2((float)Game1.random.Next(-3, 4), -4f),
                                acceleration = new Vector2(0.0f, 0.5f),
                                delayBeforeAnimationStart = index * 25,
                                scale = 2f
                            });
                            animations.Add(new TemporaryAnimatedSprite("TileSheets\\debris", new Rectangle(Game1.random.Next(2) * 16, 64, 16, 16), 9999f, 1, 999, clickableComponent + new Vector2(32f, 32f), false, false)
                            {
                                scale     = 4f,
                                alphaFade = 0.025f,
                                delayBeforeAnimationStart = index * 50,
                                motion       = Utility.getVelocityTowardPoint(new Point((int)clickableComponent.X + 32, (int)clickableComponent.Y + 32), new Vector2((float)(shop.xPositionOnScreen - 36), (float)(shop.yPositionOnScreen + shop.height - shop.inventory.height - 16)), 8f),
                                acceleration = Utility.getVelocityTowardPoint(new Point((int)clickableComponent.X + 32, (int)clickableComponent.Y + 32), new Vector2((float)(shop.xPositionOnScreen - 36), (float)(shop.yPositionOnScreen + shop.height - shop.inventory.height - 16)), 0.5f)
                            });
                        }
                        if (obj is StardewValley.Object && (obj as StardewValley.Object).Edibility != -300)
                        {
                            Item one = obj.getOne();
                            one.Stack = obj.Stack;
                            (Game1.getLocationFromName("SeedShop") as StardewValley.Locations.SeedShop).itemsToStartSellingTomorrow.Add(one);
                        }
                        Game1.playSound("sell");
                        Game1.playSound("purchase");
                    }
                }
            }
            else
            {
                heldItem = shop.inventory.leftClick(x, y, (Item)heldItem, true);
                Helper.Reflection.GetField <ISalable>(shop, "heldItem").SetValue(heldItem);
            }
            for (int i = currentItemIndex * unitsWide; i < forSale.Count && i < currentItemIndex * unitsWide + unitsWide * 3; ++i)
            {
                int       ix   = i % unitsWide;
                int       iy   = i / unitsWide;
                Rectangle rect = new Rectangle(shop.xPositionOnScreen + 16 + ix * UNIT_WIDTH, shop.yPositionOnScreen + 16 + iy * UNIT_HEIGHT - currentItemIndex * UNIT_HEIGHT, UNIT_WIDTH, UNIT_HEIGHT);
                if (rect.Contains(x, y) && forSale[i] != null)
                {
                    int numberToBuy = Math.Min(Game1.oldKBState.IsKeyDown(Keys.LeftShift) ? Math.Min(Math.Min(5, ShopMenu.getPlayerCurrencyAmount(Game1.player, currency) / Math.Max(1, itemPriceAndStock[forSale[i]][0])), Math.Max(1, itemPriceAndStock[forSale[i]][1])) : 1, forSale[i].maximumStackSize());
                    if (numberToBuy == -1)
                    {
                        numberToBuy = 1;
                    }
                    var tryToPurchaseItem = Helper.Reflection.GetMethod(shop, "tryToPurchaseItem");
                    if (numberToBuy > 0 && tryToPurchaseItem.Invoke <bool>(forSale[i], heldItem, numberToBuy, x, y, i))
                    {
                        itemPriceAndStock.Remove(forSale[i]);
                        forSale.RemoveAt(i);
                    }
                    else if (numberToBuy <= 0)
                    {
                        Game1.dayTimeMoneyBox.moneyShakeTimer = 1000;
                        Game1.playSound("cancel");
                    }
                    if (heldItem != null && Game1.options.SnappyMenus && (Game1.activeClickableMenu != null && Game1.activeClickableMenu is ShopMenu) && Game1.player.addItemToInventoryBool((Item)heldItem, false))
                    {
                        heldItem = (Item)null;
                        Helper.Reflection.GetField <ISalable>(shop, "heldItem").SetValue(heldItem);
                        DelayedAction.playSoundAfterDelay("coin", 100, (GameLocation)null);
                    }
                }
            }
        }