Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup shop window/item #2623

Merged
merged 19 commits into from
Feb 26, 2025
Merged

Conversation

WeylonSantana
Copy link
Contributor

@WeylonSantana WeylonSantana commented Feb 24, 2025

resolves #2558

assets in AscensionGameDev/Intersect-Assets#62

Shop Window inherits from Window
ShopItem inheriting from image
code reduction and cleaning in general

Functions delegated correctly now:

  • The Window only serves as a container, with no context logic when right-clicking on the shop item
  • Shop item dealing with the context menu, since it is the one that is clicked and not the window as before

I recommend looking at the files directly in the ide, github is showing everything strange

working as it should so far

1740370540_Intersect_Client.mp4

@WeylonSantana WeylonSantana changed the title clean up shop window/item cleanup shop window/item Feb 24, 2025
Copy link
Member

@lodicolo lodicolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated shop window should not use the shop.png background image anymore and should be able to regenerate its own JSON from scratch, and the assets PR should just be uploading a freshly regenerated JSON (and one that doesn't change after being generated and the client reopened).

If you need examples to look at:

  • MainMenuWindow
  • CreditsWindow
  • DebugWindow
  • SettingsWindow
  • LoginWindow
  • RegistrationWindow
  • CharacterSelectionWindow
  • HotbarWindow (though this isn't a real window)
  • EventWindow (also not a real window)
  • InputBox / AlertWindow

@WeylonSantana
Copy link
Contributor Author

I believe I have done everything I said, it is like this now

1740543182_Intersect_Client.mp4


public ImagePanel Container;
MinimumSize = new Point(34, 35);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right, shop items should be square (34, 34)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON will need regeneration after fixing this


private bool mIsEquipped;
// Generate our context menu with basic options.
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a // TODO: Refactor so shop only has 1 context menu shared between all items here


//Drag/Drop References
private ShopWindow mShopWindow;
if (Globals.GameShop == default || Globals.GameShop.SellingItems.Count == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Globals.GameShop is not { SellingItems.Count: >0 } gameShop)


public ImagePanel Pnl;
if (!ItemBase.TryGet(Globals.GameShop.SellingItems[_mySlot].CostItemId, out var item))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globals.GameShop -> gameShop

mShopWindow = shopWindow;
mMySlot = index;
}
if (Globals.GameShop.SellingItems[_mySlot].Item != default)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globals.GameShop -> gameShop

Comment on lines 64 to 65
var xPosition = column * itemWidthWithPadding + xPadding;
var yPosition = row * itemHeightWithPadding + yPadding;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xPadding -> slotContainer.Margin.Left
yPadding -> slotContainer.Margin.Top

item*WithPadding should be replaced with the outerSize stuff already mentioned

// Generate our context menu with basic options.
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
{
IsHidden = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ItemFontSize = 10, and ItemFont = GameContentManager.Current.GetFont(name: "sourcesansproblack"),

Comment on lines 57 to 61
var font = GameContentManager.Current.GetFont(name: "sourcesansproblack");
if (font != default)
{
_contextMenu.SetItemFont(font, 10);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this entirely

Comment on lines 416 to 422
public void SetItemFont(IFont font, int fontSize)
{
mItemFont = font;
_itemFontSize = fontSize;
mItemFontInfo = $"{font.Name},{fontSize}";
UpdateItemStyles();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, rename mItemFont to _itemFont, delete mItemFontInfo, add private string? _itemFontName;

Add:

    public string? ItemFontName
    {
        get => _itemFontName;
        set => SetItemFont(GameContentManager.Current.GetFont(_itemFontName), _itemFontName);
    }

    public IFont? ItemFont
    {
        get => _itemFont;
        set => SetItemFont(value, value?.Name);
    }

    private void SetItemFont(IFont? itemFont, string? itemFontName)
    {
        var oldValue = _itemFont;
        _itemFont = itemFont;
        _itemFontName = itemFontName;

        if (itemFont != oldValue)
        {
            OnFontChanged(this, itemFont, oldValue);
        }
    }

    protected virtual void OnFontChanged(Base sender, IFont? newFont, IFont? oldFont)
    {
        UpdateItemStyles();
    }

    public int ItemFontSize
    {
        get => _itemFontSize;
        set
        {
            if (value == _itemFontSize)
            {
                return;
            }

            var oldValue = _itemFontSize;
            _itemFontSize = value;
            OnFontSizeChanged(this, value, oldValue);
        }
    }

    protected virtual void OnFontSizeChanged(Base sender, int newSize, int oldSize)
    {
        UpdateItemStyles();
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For loading JSON

replace the start with

    public override void LoadJson(JToken token, bool isRoot = default)
    {
        base.LoadJson(token, isRoot);

        if (token is not JObject obj)
        {
            return;
        }

and replace the "ItemFont" section with

        string? itemFontName = null;
        int? itemFontSize = null;

        if (obj.TryGetValue(nameof(ItemFont), out var tokenItemFont) &&
            tokenItemFont is JValue { Type: JTokenType.String } valueItemFont)
        {
            var stringItemFont = valueItemFont.Value<string>()?.Trim();
            if (!string.IsNullOrWhiteSpace(stringItemFont))
            {
                var parts = stringItemFont.Split(',');
                itemFontName = parts.FirstOrDefault();

                if (parts.Length > 1 && int.TryParse(parts[1], out var size))
                {
                    itemFontSize = size;
                }
            }
        }

        if (obj.TryGetValue(nameof(ItemFontName), out var tokenItemFontName) &&
            tokenItemFontName is JValue { Type: JTokenType.String } valueItemFontName)
        {
            itemFontName = valueItemFontName.Value<string>();
        }

        if (obj.TryGetValue(nameof(ItemFontSize), out var tokenItemFontSize) &&
            tokenItemFontSize is JValue { Type: JTokenType.Integer } valueItemFontSize)
        {
            itemFontSize = valueItemFontSize.Value<int>();
        }

        if (itemFontSize.HasValue)
        {
            ItemFontSize = itemFontSize.Value;
        }

        itemFontName = itemFontName?.Trim();
        if (!string.IsNullOrWhiteSpace(itemFontName))
        {
            ItemFontName = itemFontName;
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For saving JSON remove the "ItemFont" line and use

        serializedProperties.Add(nameof(ItemFontName), ItemFontName);
        serializedProperties.Add(nameof(ItemFontSize), ItemFontSize);

Comment on lines 182 to 188
public override JObject FixJson(JObject json)
{
json.Remove("HoverSound");
json.Remove("MouseUpSound");
json.Remove("MouseDownSound");
return base.FixJson(json);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed? What would be adding those keys to begin with?

@@ -585,7 +583,7 @@ public bool CloseAllWindows()
closedWindows = true;
}

if (mShopWindow != null && mShopWindow.IsVisible())
if (mShopWindow != null && !mShopWindow.IsHidden)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use IsVisibleInTree

@@ -392,7 +390,7 @@ public void Update(TimeSpan elapsed, TimeSpan total)
GameMenu.OpenInventory();
}

if (mShopWindow != null && (!mShopWindow.IsVisible() || mShouldCloseShop))
if (mShopWindow != null && (mShopWindow.IsHidden || mShouldCloseShop))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use !IsVisibleInTree

// TODO: Refactor so shop only has 1 context menu shared between all items
_contextMenu = new ContextMenu(Interface.CurrentInterface.Root, "ShopContextMenu")
{
IsHidden = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsVisibleInParent = false,

);
}
_contextMenu.UserData = slot;
_contextMenu.SizeToChildren();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary, it's built into the context menu's Open (via OnOpen())

Comment on lines 164 to 169
if (Globals.GameShop?.SellingItems[slot] == default)
{
return;
}

if (Globals.InputManager.IsMouseButtonDown(MouseButton.Left))
if (!ItemBase.TryGet(Globals.GameShop.SellingItems[slot].ItemId, out var item))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globals.GameShop changes need to be done in this method too, also cache the SellingItems[slot] array access instead of duplicating it

Comment on lines +28 to +25
SetSound(ButtonSoundState.Hover, "octave-tap-resonant.wav");
SetSound(ButtonSoundState.MouseDown, "octave-tap-warm.wav");
SetSound(ButtonSoundState.MouseUp, "octave-tap-warm.wav");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I said to add this here I meant specifically the setting of sounds because Dragger shouldn't have default sounds like Button, ScrollbarBar, and SliderBar do.

I didn't mean to add the infrastructure for sound into this class too, since Dragger had the infrastructure that stuff should be cleaned up. _stateSoundNames and _ignoreMouseUpSoundsUntil (and the methods that use them) should be in Dragger just like they are in Button.

Only the SetSound(...); calls that are right here should stay in this class.

@@ -181,7 +181,7 @@ protected override void Dispose(bool disposing)

serializedProperties.Add(nameof(Texture), TextureFilename);
serializedProperties.Add(nameof(TextureNinePatchMargin), TextureNinePatchMargin?.ToString());
serializedProperties.Add("HoverSound", mHoverSound);
serializedProperties.Add("HoverSound", HoverSound);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameof(HoverSound) instead of the string

@@ -19,7 +19,7 @@ public partial class ImagePanel : Base
private readonly float[] _uv;

//Sound Effects
protected string mHoverSound;
public string HoverSound;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ get; set; }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update Left/RightMouseClickSounds to be loaded/saved (and they should be { get; set; } like HoverSound is

Comment on lines 228 to 232
if (obj[nameof(HoverSound)] is JValue { Type: JTokenType.String } hoverSound &&
hoverSound.Value<string>()?.Trim() is { Length: > 0 })
{
mHoverSound = (string) obj["HoverSound"];
HoverSound = hoverSound?.Value<string>();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be slightly different to make sure what we compare is what we set, specifically:

        if (obj[nameof(HoverSound)] is JValue { Type: JTokenType.String } tokenHoverSound &&
            tokenHoverSound.Value<string>()?.Trim() is { Length: > 0 } hoverSound)
        {
            HoverSound = hoverSound;
        }

Comment on lines 234 to 243
if (obj[nameof(LeftMouseClickSound)] is JValue { Type: JTokenType.String } leftMouseClickSound &&
leftMouseClickSound.Value<string>()?.Trim() is { Length: > 0 })
{
mLeftMouseClickSound = (string) obj["LeftMouseClickSound"];
LeftMouseClickSound = leftMouseClickSound?.Value<string>();
}

if (obj["RightMouseClickSound"] != null)
if (obj[nameof(RightMouseClickSound)] is JValue { Type: JTokenType.String } rightMouseClickSound &&
rightMouseClickSound.Value<string>()?.Trim() is { Length: > 0 })
{
mRightMouseClickSound = (string) obj["RightMouseClickSound"];
RightMouseClickSound = rightMouseClickSound?.Value<string>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the same thing as I suggested with HoverSound

@lodicolo lodicolo merged commit a5f748e into AscensionGameDev:main Feb 26, 2025
1 check passed
@lodicolo lodicolo deleted the fix-2558 branch February 26, 2025 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Shop window title alignment
2 participants