示例#1
0
        public static ItemData MakeItem(int itemId)
        {
            if (!Game1.objectInformation.ContainsKey(itemId))
            {
                throw new System.ArgumentException($"Tried creating an item with an invalid id: {itemId}");
            }

            string objectInfo = Game1.objectInformation[itemId];

            string[] parts = objectInfo.Split('/');

            return(new ItemData
            {
                Name = parts[ItemData.NameIndex],
                DisplayName = parts[ItemData.DisplayNameIndex],
                Price = int.Parse(parts[ItemData.PriceIndex]),
                Edibility = int.Parse(parts[ItemData.EdibilityIndex]),
                ID = itemId,
                Category = ItemData.GetCategory(objectInfo),
                TileSheetSourceRect = Game1.getSourceRectForStandardTileSheet(Game1.objectSpriteSheet, itemId, 16, 16),
                NameSize = SVector2.MeasureString(parts[ItemData.DisplayNameIndex], Game1.smallFont)
            });
        }
        protected void DrawGiftTooltip(GiftDrawData drawData, string title, string originalTooltipText = "")
        {
            int numItemsToDraw = CalculateNumberOfGiftsToDisplay(drawData.Gifts.Length, this.GiftConfig.MaxGiftsToDisplay);

            if (numItemsToDraw == 0 && this.GiftConfig.HideTooltipWhenNoGiftsKnown)
            {
                return;
            }

            SVector2 maxNameSize = new SVector2(0, 0);

            for (int i = 0; i < numItemsToDraw; ++i)
            {
                maxNameSize = Utils.CreateMax(maxNameSize, drawData.Gifts[i].Item.NameSize);
            }

            float     spriteScale      = 2.0f * this.ZoomLevel;                                              // 16x16 is pretty small
            Rectangle spriteRect       = numItemsToDraw > 0 ? drawData.IconSize : new Rectangle(0, 0, 0, 0); // We just need the dimensions which we assume are all the same
            SVector2  scaledSpriteSize = new SVector2(spriteRect.Width * spriteScale, spriteRect.Height * spriteScale);

            // The longest length of text will help us determine how wide the tooltip box should be
            SVector2 titleSize   = SVector2.MeasureString(title, Game1.smallFont);
            SVector2 maxTextSize = (titleSize.X - scaledSpriteSize.X > maxNameSize.X) ? titleSize : maxNameSize;

            SVector2 mouse = new SVector2(Game1.getOldMouseX(), Game1.getOldMouseY());

            int padding   = 4; // Chosen by fair dice roll
            int rowHeight = (int)Math.Max(maxTextSize.Y * this.ZoomLevel, scaledSpriteSize.YInt) + padding;
            int width     = this.AdjustForTileSize((maxTextSize.X * this.ZoomLevel) + scaledSpriteSize.XInt) + padding;
            int height    = this.AdjustForTileSize(rowHeight * (numItemsToDraw + 1)); // Add one to make room for the title
            int x         = this.AdjustForTileSize(mouse.X, 0.5f, this.ZoomLevel);
            int y         = this.AdjustForTileSize(mouse.Y, 0.5f, this.ZoomLevel);

            int viewportW = Game1.viewport.Width;
            int viewportH = Game1.viewport.Height;

            // Let derived classes adjust the positioning
            this.AdjustTooltipPosition(ref x, ref y, width, height, viewportW, viewportH);

            // Approximate where the original tooltip will be positioned if there is an existing one we need to account for
            this.OrigHoverTextSize = SVector2.MeasureString(originalTooltipText, Game1.dialogueFont);
            int origTToffsetX = this.OrigHoverTextSize.X > 0
                ? Math.Max(0, this.AdjustForTileSize(this.OrigHoverTextSize.X + mouse.X, 1.0f) - viewportW) + width
                : 0;

            // Consider the position of the original tooltip and ensure we don't cover it up
            SVector2 tooltipPos = this.ClampToViewport(x - origTToffsetX, y, width, height, viewportW, viewportH, mouse);

            // Reduce the number items shown if it will go off screen.
            // TODO: perhaps add a second column
            if (height > viewportH)
            {
                numItemsToDraw = (viewportH / rowHeight) - 1; // Remove an item to make space for the title
                height         = this.AdjustForTileSize(rowHeight * numItemsToDraw);
            }

            // Draw the background of the tooltip
            SpriteBatch spriteBatch = Game1.spriteBatch;

            // Part of the spritesheet containing the texture we want to draw
            Rectangle menuTextureSourceRect = new Rectangle(0, 256, 60, 60);

            IClickableMenu.drawTextureBox(spriteBatch, Game1.menuTexture, menuTextureSourceRect, tooltipPos.XInt, tooltipPos.YInt, width, height, Color.White, this.ZoomLevel);

            // Offset the sprite from the corner of the bg, and the text to the right and centered vertically of the sprite
            SVector2 spriteOffset = new SVector2(this.AdjustForTileSize(tooltipPos.X, 0.25f), this.AdjustForTileSize(tooltipPos.Y, 0.25f));
            SVector2 textOffset   = new SVector2(spriteOffset.X, spriteOffset.Y + (spriteRect.Height / 2));

            // TODO: fix weird title y offset when there are > 0 items.
            // Draw the title then set up the offset for the remaining text
            this.DrawText(title, textOffset);
            textOffset.X   += scaledSpriteSize.X + padding;
            textOffset.Y   += rowHeight;
            spriteOffset.Y += rowHeight;

            // Draw all the items
            for (int i = 0; i < numItemsToDraw; ++i)
            {
                GiftInfo gift = drawData.Gifts[i];
                ItemData item = gift.Item;

                // Draw the sprite for the item then the item text
                var textColor = gift.Universal && this.GiftConfig.ColorizeUniversalGiftNames ? Color.Blue : Game1.textColor;
                this.DrawText(item.DisplayName, textOffset, textColor);
                this.DrawTexture(Game1.objectSpriteSheet, spriteOffset, item.TileSheetSourceRect, spriteScale);

                // Move to the next row
                spriteOffset.Y += rowHeight;
                textOffset.Y   += rowHeight;
            }
        }