1
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 791ea0dc13231374f91db42c5f083cc2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,98 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI拖拽物品
|
||||
/// </summary>
|
||||
public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
/// <summary> 画布 </summary>
|
||||
private VisualElement canvas;
|
||||
/// <summary> 是否启用 </summary>
|
||||
private bool isEnable;
|
||||
/// <summary> 鼠标位置 </summary>
|
||||
private Vector2 mousePosition;
|
||||
/// <summary> 偏移位置 </summary>
|
||||
private Vector2 offsetPosition;
|
||||
/// <summary> 物品容器 </summary>
|
||||
private DragContainer container;
|
||||
/// <summary> 目标容器 </summary>
|
||||
private DragContainer targetContainer;
|
||||
|
||||
public Label Count => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
public VisualElement Preview => Q<VisualElement>("Preview");
|
||||
|
||||
public UIDragItem(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 = container.Count;
|
||||
DataItem item = container.Item;
|
||||
container.Exchange(targetContainer.Item, targetContainer.Count);
|
||||
targetContainer.Exchange(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(DragContainer container) {
|
||||
this.container = container;
|
||||
|
||||
isEnable = container.Item != null;
|
||||
element.EnableInClassList("document-page-hide", !isEnable);
|
||||
if (isEnable) { UpdatePreview(); }
|
||||
}
|
||||
/// <summary> 进入容器 </summary>
|
||||
public void EnterContainer(DragContainer container) {
|
||||
targetContainer = container;
|
||||
}
|
||||
/// <summary> 退出容器 </summary>
|
||||
public void ExitContainer(DragContainer 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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 项目拖拽
|
||||
/// </summary>
|
||||
public interface DragContainer {
|
||||
/// <summary> 数量 </summary>
|
||||
public int Count { get; }
|
||||
/// <summary> 物品 </summary>
|
||||
public DataItem Item { get; }
|
||||
/// <summary> 锚点 </summary>
|
||||
public VisualElement Anchor { get; }
|
||||
|
||||
/// <summary> 取消拖拽 </summary>
|
||||
public void Cancel();
|
||||
/// <summary> 交换物品 </summary>
|
||||
public void Exchange(DataItem item, int count);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58ee0d6bee6b6254a8871407bb3a0574
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// UI库存
|
||||
/// </summary>
|
||||
public class UIInventory : ModuleUIPanel {
|
||||
|
||||
public Inventory inventory;
|
||||
public ModuleUIItems<UIItem, InventorySlot> items;
|
||||
|
||||
public VisualElement Container => Q<VisualElement>("Container");
|
||||
|
||||
public UIInventory(VisualElement element, VisualTreeAsset templateAsset) : base(element) {
|
||||
items = new ModuleUIItems<UIItem, InventorySlot>(Container, templateAsset,
|
||||
(data, element) => new UIItem(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);
|
||||
}
|
||||
|
||||
/// <summary> UI项目 </summary>
|
||||
public class UIItem : ModuleUIItem<InventorySlot>, DragContainer {
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => value.count;
|
||||
public DataItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIItem(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.dragItem.Settings(this);
|
||||
Image.EnableInClassList("inventory-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
UIPopupManager.I.dragItem.EnterContainer(this);
|
||||
}
|
||||
private void MouseLeaveEvent(MouseLeaveEvent evt) {
|
||||
UIPopupManager.I.dragItem.ExitContainer(this);
|
||||
}
|
||||
private void ClickEvent(ClickEvent evt) {
|
||||
// Debug.Log("sss");
|
||||
}
|
||||
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("inventory-image-drag", false);
|
||||
}
|
||||
public void Exchange(DataItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
CountLabel.text = value.count.ToString();
|
||||
CountLabel.EnableInClassList("inventory-count-hide", value.count <= 1);
|
||||
Image.style.backgroundImage = new StyleBackground(value.Sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7c8f41e9e4833a4bb32d179036ebaf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user