1
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
/// <summary>
|
||||
/// 项目拖拽
|
||||
/// </summary>
|
||||
public interface InventoryDrag {
|
||||
/// <summary> 数量 </summary>
|
||||
public int Count { get; }
|
||||
/// <summary> 物品 </summary>
|
||||
public InventoryItem Item { get; }
|
||||
/// <summary> 锚点 </summary>
|
||||
public VisualElement Anchor { get; }
|
||||
|
||||
/// <summary> 取消拖拽 </summary>
|
||||
public void Cancel();
|
||||
/// <summary> 设置物品 </summary>
|
||||
public void Settings(InventoryItem item, int count);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc8dab09130da0a43bcd57ddecde2332
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+1
-1
@@ -6,7 +6,7 @@ using UnityEngine;
|
||||
/// 物品 - 常量
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "MuHua/物品系统/物品")]
|
||||
public class ConstInventoryItem : ScriptableObject {
|
||||
public class InventoryItemConst : ScriptableObject {
|
||||
/// <summary> 图片 </summary>
|
||||
public Sprite sprite;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48193b9c6d7c6af4088a00876ccaa1c3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 791ea0dc13231374f91db42c5f083cc2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,38 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI库存
|
||||
/// </summary>
|
||||
public class UIInventory : ModuleUIPanel {
|
||||
|
||||
public Inventory inventory;
|
||||
public ModuleUIItems<UIInventorySlot, InventorySlot> items;
|
||||
|
||||
public VisualElement Container => Q<VisualElement>("Container");
|
||||
|
||||
public UIInventory(VisualElement element, VisualTreeAsset templateAsset) : base(element) {
|
||||
items = new ModuleUIItems<UIInventorySlot, InventorySlot>(Container, templateAsset,
|
||||
(data, element) => new UIInventorySlot(data, element));
|
||||
}
|
||||
public void Dispose() {
|
||||
items.Release();
|
||||
if (inventory == null) { return; }
|
||||
inventory.OnChange -= Inventory_OnChange;
|
||||
}
|
||||
|
||||
public void Settings(Inventory inventory) {
|
||||
if (this.inventory != null) { Dispose(); }
|
||||
this.inventory = inventory;
|
||||
if (inventory == null) { return; }
|
||||
items.Create(inventory.slots);
|
||||
inventory.OnChange += Inventory_OnChange;
|
||||
}
|
||||
|
||||
private void Inventory_OnChange(Inventory inventory) {
|
||||
items.Create(inventory.slots);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7c8f41e9e4833a4bb32d179036ebaf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,83 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI拖拽物品
|
||||
/// </summary>
|
||||
public class UIInventoryDrag : ModuleUIPanel, UIControl {
|
||||
/// <summary> 画布 </summary>
|
||||
public VisualElement canvas;
|
||||
/// <summary> 是否启用 </summary>
|
||||
public bool isEnable;
|
||||
/// <summary> 鼠标位置 </summary>
|
||||
public Vector2 mousePosition;
|
||||
/// <summary> 偏移位置 </summary>
|
||||
public Vector2 offsetPosition;
|
||||
/// <summary> 物品容器 </summary>
|
||||
public InventoryDrag container;
|
||||
/// <summary> 目标容器 </summary>
|
||||
public InventoryDrag targetContainer;
|
||||
|
||||
public Label Count => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
public VisualElement Preview => Q<VisualElement>("Preview");
|
||||
|
||||
public UIInventoryDrag(VisualElement element, VisualElement canvas) : base(element) {
|
||||
this.canvas = canvas;
|
||||
ModuleUI.AddControl(this);
|
||||
canvas.RegisterCallback<MouseUpEvent>(MouseUpEvent);
|
||||
canvas.RegisterCallback<MouseMoveEvent>(FloatingFunc);
|
||||
}
|
||||
private void MouseUpEvent(MouseUpEvent evt) {
|
||||
if (!isEnable) { return; }
|
||||
isEnable = false;
|
||||
container?.Cancel();
|
||||
element.EnableInClassList("document-page-hide", !isEnable);
|
||||
// 交换物品
|
||||
if (container == null || targetContainer == null) { return; }
|
||||
int count = targetContainer.Count;
|
||||
InventoryItem item = targetContainer.Item;
|
||||
targetContainer.Settings(container.Item, container.Count);
|
||||
container.Settings(item, count);
|
||||
}
|
||||
private void FloatingFunc(MouseMoveEvent evt) {
|
||||
mousePosition = evt.mousePosition;
|
||||
}
|
||||
public void Update() {
|
||||
Preview.transform.position = mousePosition + offsetPosition;
|
||||
// if (targetContainer == null) { return; }
|
||||
// Preview.transform.position = targetContainer.Anchor.worldBound.position;
|
||||
}
|
||||
public void Dispose() {
|
||||
canvas.UnregisterCallback<MouseUpEvent>(MouseUpEvent);
|
||||
canvas.UnregisterCallback<MouseMoveEvent>(FloatingFunc);
|
||||
}
|
||||
|
||||
/// <summary> 设置容器 </summary>
|
||||
public void Settings(InventoryDrag container) {
|
||||
this.container = container;
|
||||
|
||||
isEnable = container.Item != null;
|
||||
element.EnableInClassList("document-page-hide", !isEnable);
|
||||
if (isEnable) { UpdatePreview(); }
|
||||
}
|
||||
/// <summary> 进入容器 </summary>
|
||||
public void EnterContainer(InventoryDrag container) {
|
||||
targetContainer = container;
|
||||
}
|
||||
/// <summary> 退出容器 </summary>
|
||||
public void ExitContainer(InventoryDrag container) {
|
||||
if (targetContainer == container) { targetContainer = null; }
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
offsetPosition = container.Anchor.worldBound.position - mousePosition;
|
||||
|
||||
Count.text = container.Count.ToString();
|
||||
Count.EnableInClassList("dragitem-count-hide", container.Count <= 1);
|
||||
Image.style.backgroundImage = new StyleBackground(container.Item.sprite);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58ee0d6bee6b6254a8871407bb3a0574
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,53 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI库存插槽
|
||||
/// </summary>
|
||||
public class UIInventorySlot : ModuleUIItem<InventorySlot>, InventoryDrag {
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => value.count;
|
||||
public InventoryItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIInventorySlot(InventorySlot value, VisualElement element) : base(value, element) {
|
||||
UpdatePreview();
|
||||
element.RegisterCallback<MouseDownEvent>(MouseDownEvent);
|
||||
element.RegisterCallback<MouseOverEvent>(MouseOverEvent);
|
||||
element.RegisterCallback<MouseLeaveEvent>(MouseLeaveEvent);
|
||||
element.RegisterCallback<ClickEvent>(ClickEvent);
|
||||
}
|
||||
private void MouseDownEvent(MouseDownEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.Settings(this);
|
||||
Image.EnableInClassList("inventory-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.EnterContainer(this);
|
||||
}
|
||||
private void MouseLeaveEvent(MouseLeaveEvent evt) {
|
||||
UIPopupManager.I.inventoryDrag.ExitContainer(this);
|
||||
}
|
||||
private void ClickEvent(ClickEvent evt) {
|
||||
// Debug.Log("sss");
|
||||
}
|
||||
|
||||
public void Settings(InventoryItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("inventory-image-drag", false);
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
CountLabel.text = value.count.ToString();
|
||||
CountLabel.EnableInClassList("inventory-count-hide", value.count <= 1);
|
||||
Image.style.backgroundImage = new StyleBackground(value.Sprite);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef816982615753c4c9d3ebcf38d51598
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,16 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// 库存提示
|
||||
/// </summary>
|
||||
public class UIInventoryTip : ModuleUIPanel {
|
||||
|
||||
public UIInventoryTip(VisualElement element) : base(element) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 029d4912285f5c74c8d041ea54164d8b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user