仕入れItemView処理追加/DetailView追加

This commit is contained in:
kimura 2021-09-10 11:15:27 +09:00
parent 42c4b83da6
commit f30ca21eb8
7 changed files with 106 additions and 26 deletions

View File

@ -2,29 +2,24 @@ using System;
using UniRx; using UniRx;
using UnityEngine; using UnityEngine;
public enum ShopHeaderTab
{
Material,
Recipe
}
public class ShopHeaderView : MonoBehaviour public class ShopHeaderView : MonoBehaviour
{ {
[SerializeField] private ShopHeaderTabView materialTab; [SerializeField] private ShopHeaderTabView materialTab;
[SerializeField] private ShopHeaderTabView recipeTab; [SerializeField] private ShopHeaderTabView recipeTab;
public IReadOnlyReactiveProperty<ShopHeaderTab> Tab => tab; public IReadOnlyReactiveProperty<ItemCategory> Tab => tab;
private readonly ReactiveProperty<ShopHeaderTab> tab = new ReactiveProperty<ShopHeaderTab>(); private readonly ReactiveProperty<ItemCategory> tab = new ReactiveProperty<ItemCategory>();
private void Start() private void Start()
{ {
tab.AddTo(this); tab.AddTo(this);
tab.Value = ShopHeaderTab.Material; tab.Value = ItemCategory.Material;
tab.Subscribe(tabName => tab.Subscribe(tabName =>
{ {
materialTab.SetToggle(tabName == ShopHeaderTab.Material); materialTab.SetToggle(tabName == ItemCategory.Material);
recipeTab.SetToggle(tabName == ShopHeaderTab.Recipe); recipeTab.SetToggle(tabName == ItemCategory.Recipe);
}).AddTo(this); }).AddTo(this);
materialTab.ClickObservable.Select(_ => ShopHeaderTab.Material) materialTab.ClickObservable.Select(_ => ItemCategory.Material)
.Merge(recipeTab.ClickObservable.Select(_ => ShopHeaderTab.Recipe)) .Merge(recipeTab.ClickObservable.Select(_ => ItemCategory.Recipe))
.Subscribe(tabName => .Subscribe(tabName =>
{ {
tab.Value = tabName; tab.Value = tabName;

View File

@ -0,0 +1,38 @@
using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class ShopItemDetailView : MonoBehaviour
{
public static readonly string ItemDetailTag = "ItemDetail";
private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
[SerializeField] private Animator backgroundAnimator;
[SerializeField] private Text name;
[SerializeField] private Image icon;
[SerializeField] private Text text;
[SerializeField] private Button closeButton;
private void Start()
{
var data = LocalCacheManager.Load<ShopData>(ItemDetailTag);
SetData(data);
closeButton.OnClickAsObservable().Subscribe(_ =>
{
transform.parent.SetLocalScale(0);
backgroundAnimator.SetTrigger(CloseTrigger);
this.CallWaitForSeconds(.25f, () =>
{
TransitionManager.Instance.UnloadScene(GameScenes.ShoppingExplanation);
});
}).AddTo(this);
}
private void SetData(ShopData shopData)
{
name.text = shopData.name;
text.text = shopData.text;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2014f6a58b674f40ad557f809d8b21f2
timeCreated: 1631188062

View File

@ -29,17 +29,17 @@ public class ShopItemPurchaseButton : MonoBehaviour
} }
} }
public void SetItemActive(bool active) public void SetButtonActive(bool active)
{ {
switch (itemType) switch (itemType)
{ {
case ShopItemType.Consumable: case ShopItemType.Consumable:
onObject.SetActive(active); onObject.SetActive(active);
offObject.SetActive(active); offObject.SetActive(!active);
break; break;
case ShopItemType.NonConsumable: case ShopItemType.NonConsumable:
onObject.SetActive(active); onObject.SetActive(active);
soldOutObject.SetActive(active); soldOutObject.SetActive(!active);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();

View File

@ -12,5 +12,27 @@ public class ShopItemView : MonoBehaviour
[SerializeField] private Text price; [SerializeField] private Text price;
[SerializeField] private Text stockCount; [SerializeField] private Text stockCount;
public IObservable<Unit> DetailButtonObservable => detailButton.OnClickAsObservable().TakeUntilDestroy(this);
public IObservable<Unit> PurchaseButtonObservable => purchaseButton.ClickObservable.TakeUntilDestroy(this); public IObservable<Unit> PurchaseButtonObservable => purchaseButton.ClickObservable.TakeUntilDestroy(this);
public void SetData(ShopData shopData)
{
itemName.text = shopData.name;
price.text = $"{shopData.price}";
if (shopData.ConsumeType == ShopItemType.NonConsumable)
{
stockCount.transform.parent.gameObject.SetActive(false);
}
purchaseButton.SetItemType(shopData.ConsumeType);
}
public void SetStockCount(int stock)
{
stockCount.text = $"{stock}";
}
public void SetItemActive(bool active)
{
purchaseButton.SetButtonActive(active);
}
} }

View File

@ -9,7 +9,7 @@ public class ShopListView : MonoBehaviour
[SerializeField] private ScrollRect scrollRect; [SerializeField] private ScrollRect scrollRect;
[SerializeField] private ShopItemView itemViewPrefab; [SerializeField] private ShopItemView itemViewPrefab;
[SerializeField] private ShopHeaderView headerView; [SerializeField] private ShopHeaderView headerView;
private List<(ShopHeaderTab, RectTransform)> tabs = new List<(ShopHeaderTab, RectTransform)>(); private readonly List<(ItemCategory category, RectTransform content)> tabs = new List<(ItemCategory, RectTransform)>();
private void Awake() private void Awake()
{ {
@ -17,35 +17,56 @@ public class ShopListView : MonoBehaviour
{ {
DestroyImmediate(scrollRect.content.GetChild(0).gameObject); DestroyImmediate(scrollRect.content.GetChild(0).gameObject);
} }
tabs.Add((ShopHeaderTab.Recipe, scrollRect.content)); tabs.Add((ItemCategory.Recipe, scrollRect.content));
} }
private void Start() private void Start()
{ {
headerView.Tab.Subscribe(tab => headerView.Tab.Subscribe(tab =>
{ {
// scrollRect.verticalNormalizedPosition = 1; scrollRect.verticalNormalizedPosition = 1;
tabs.ForEach(x => tabs.ForEach(x =>
{ {
x.Item2.gameObject.SetActive(x.Item1 == tab); x.content.gameObject.SetActive(x.category == tab);
if (x.Item1 == tab) if (x.category == tab)
{ {
scrollRect.content = x.Item2; scrollRect.content = x.content;
} }
}); });
}).AddTo(this); }).AddTo(this);
} }
public ShopItemView AddItem() public ShopItemView AddItemView(ShopData shopData)
{ {
var youTab = ShopHeaderTab.Material; var tabCategory = GetTabCategory(shopData.Category);
var index = tabs.FindIndex(x => x.Item1 == youTab); var index = tabs.FindIndex(x => x.category == tabCategory);
if (index == -1) if (index == -1)
{ {
index = tabs.Count; index = tabs.Count;
tabs.Add((youTab, Instantiate(scrollRect.content, scrollRect.content.parent))); tabs.Add((tabCategory, Instantiate(scrollRect.content, scrollRect.content.parent)));
} }
var itemView = Instantiate(itemViewPrefab, tabs[index].Item2); var itemView = Instantiate(itemViewPrefab, tabs[index].content);
itemView.SetData(shopData);
// show detail view
itemView.DetailButtonObservable.Subscribe(_ =>
{
LocalCacheManager.Save(ShopItemDetailView.ItemDetailTag, shopData);
TransitionManager.Instance.LoadSceneAdditive(GameScenes.ShoppingExplanation);
}).AddTo(itemView);
return itemView; return itemView;
} }
private ItemCategory GetTabCategory(ItemCategory category)
{
switch (category)
{
case ItemCategory.Material:
return ItemCategory.Material;
case ItemCategory.Recipe:
case ItemCategory.Pan:
return ItemCategory.Recipe;
default:
throw new ArgumentOutOfRangeException(nameof(category), category, null);
}
}
} }

View File

@ -20,6 +20,7 @@ public enum ShopItemType
public sealed class ShopData { public sealed class ShopData {
public int id; public int id;
public int nameId; public int nameId;
public int itemId;
public string name; public string name;
public int price; public int price;
public string text; public string text;