Browse Source

Fix item views not unsubscribing themselves from model

master
Devin 4 years ago
parent
commit
a56e740473
  1. 13
      Assets/Prefabs/GridViewItem.prefab
  2. 8
      Assets/Scripts/Shop/Components.meta
  3. 24
      Assets/Scripts/Shop/Components/ItemNameDisplay.cs
  4. 11
      Assets/Scripts/Shop/Components/ItemNameDisplay.cs.meta
  5. 3
      Assets/Scripts/Shop/Controller/MouseController.cs
  6. 4
      Assets/Scripts/Shop/Model/BuyModel.cs
  7. 4
      Assets/Scripts/Shop/Model/ShopModel.cs
  8. 11
      Assets/Scripts/Shop/View/ViewItemContainer.cs
  9. BIN
      Assets/Sprites/LeftArrow.png
  10. BIN
      Assets/Sprites/RightArrow.png
  11. BIN
      Assets/Unity Packages/TextMesh Pro/Fonts/LiberationSans.ttf
  12. BIN
      Assets/Unity Packages/TextMesh Pro/Sprites/EmojiOne.png

13
Assets/Prefabs/GridViewItem.prefab

@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 1355062804588728591} - component: {fileID: 1355062804588728591}
- component: {fileID: 6276336569359545557} - component: {fileID: 6276336569359545557}
- component: {fileID: 4391249473562572445} - component: {fileID: 4391249473562572445}
- component: {fileID: 2521131620876806752}
m_Layer: 5 m_Layer: 5
m_Name: Name m_Name: Name
m_TagString: Untagged m_TagString: Untagged
@ -280,6 +281,18 @@ MonoBehaviour:
- {fileID: 0} - {fileID: 0}
m_baseMaterial: {fileID: 0} m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0} m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!114 &2521131620876806752
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1245163151616179787}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1bf98089f9db1b4aacb219d05609744, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1522873078225282773 --- !u!1 &1522873078225282773
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

8
Assets/Scripts/Shop/Components.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fa069fdb6941fa247b6d3ca2cfcc0307
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

24
Assets/Scripts/Shop/Components/ItemNameDisplay.cs

@ -0,0 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
/// <summary>
/// This class' only purpose is to set the name of an item view prototype. Could be the item itself, or the infobox, or anything!
/// </summary>
[RequireComponent(typeof(TextMeshProUGUI))]
public class ItemNameDisplay : MonoBehaviour
{
private TextMeshProUGUI text;
private void Start()
{
text = GetComponent<TextMeshProUGUI>(); // Because of the meta tag, Unity will make sure this exists. No sanity checks
}
public void SetName(string name)
{
if(text == null) text = GetComponent<TextMeshProUGUI>(); // Just in case this gets called before Start()
text.text = name;
}
}

11
Assets/Scripts/Shop/Components/ItemNameDisplay.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1bf98089f9db1b4aacb219d05609744
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

3
Assets/Scripts/Shop/Controller/MouseController.cs

@ -25,8 +25,9 @@ public class MouseController : ShopController, IPointerClickHandler
{ {
if (itemToSelect != null) if (itemToSelect != null)
{ {
SelectItem(itemToSelect); var item = itemToSelect; // Temporarily remember the selected item
itemToSelect = null;//Now that the item was selected, set itemToSelect back to null itemToSelect = null;//Now that the item was selected, set itemToSelect back to null
SelectItem(item); // Use the item we remembered, so itemToSelect can be reset even in case of errors or so
} }
} }

4
Assets/Scripts/Shop/Model/BuyModel.cs

@ -18,9 +18,9 @@ public class BuyModel : ShopModel
public override void ConfirmSelectedItem() public override void ConfirmSelectedItem()
{ {
OnRemove(GetSelectedItem()); // If there's a view subscribed, this will probably remove the item from it
inventory.RemoveItemByIndex(selectedItemIndex); // Before removing the item from the model's actual inventory inventory.RemoveItemByIndex(selectedItemIndex); // Before removing the item from the model's actual inventory
SelectItemByIndex(--selectedItemIndex); OnRemove(GetSelectedItem()); // If there's a view subscribed, this will probably remove the item from it
SelectItemByIndex(selectedItemIndex > inventory.GetItemCount() ? --selectedItemIndex : selectedItemIndex);
} }
} }

4
Assets/Scripts/Shop/Model/ShopModel.cs

@ -108,7 +108,9 @@ public abstract class ShopModel : IModelObservable<Item>
public void OnRemove(Item val) public void OnRemove(Item val)
{ {
foreach (var observer in observers) // We copy the list so we don't break it while iterating, as it's likely OnRemove will cause an object to unsub
var observerCopy = observers.ToArray();
foreach (var observer in observerCopy)
observer.OnRemoved(val); observer.OnRemoved(val);
} }

11
Assets/Scripts/Shop/View/ViewItemContainer.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro;
using Unity.Collections.LowLevel.Unsafe; using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -50,6 +51,7 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
//Stores the item //Stores the item
this.item = item; this.item = item;
_unsubscriber = unsubscriber;
//Sets the highlight image and infoPanel's visibility //Sets the highlight image and infoPanel's visibility
//if (isSelected) { //if (isSelected) {
// highLight.SetActive(true); // highLight.SetActive(true);
@ -59,6 +61,13 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
// Clones the first Sprite in the icon atlas that matches the iconName and uses it as the sprite of the icon image. // Clones the first Sprite in the icon atlas that matches the iconName and uses it as the sprite of the icon image.
Sprite sprite = iconAtlas.GetSprite(item.iconName); Sprite sprite = iconAtlas.GetSprite(item.iconName);
// Sets name in all locations the prototype has a name component attached to
var nameComps = GetComponentsInChildren<ItemNameDisplay>(true); // Include inactives!
foreach (var name in nameComps)
{
name.SetName(item.iconName); // Use iconname for now, debugging
}
if (sprite != null) { if (sprite != null) {
icon.sprite = sprite; icon.sprite = sprite;
} }
@ -73,7 +82,7 @@ public class ViewItemContainer : MonoBehaviour, IItemContainer, IShopModelObserv
public void OnRemoved(Item item) public void OnRemoved(Item item)
{ {
if (item != Item) return; // Well we only want this if the item removed from the model is actually ours! if (item != Item) return; // Well we only want this if the item removed from the model is actually ours!
_unsubscriber?.Dispose(); // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak _unsubscriber.Dispose(); // Careful! If we don't make this thing aware that it manages itself, and we forget to manage it, we have a memory leak
Destroy(gameObject); Destroy(gameObject);
} }

BIN
Assets/Sprites/LeftArrow.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 411 B

After

Width:  |  Height:  |  Size: 128 B

BIN
Assets/Sprites/RightArrow.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 413 B

After

Width:  |  Height:  |  Size: 128 B

BIN
Assets/Unity Packages/TextMesh Pro/Fonts/LiberationSans.ttf

Binary file not shown.

BIN
Assets/Unity Packages/TextMesh Pro/Sprites/EmojiOne.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 130 B

Loading…
Cancel
Save