修改框架
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using MuHua;
|
||||
|
||||
/// <summary>
|
||||
/// UI装备栏
|
||||
/// </summary>
|
||||
public class UIEquipment : ModuleUIPanel {
|
||||
|
||||
public Equipment equipment;
|
||||
/// <summary> 插槽字典 </summary>
|
||||
public Dictionary<string, UIEquipmentSlot> dictionary = new Dictionary<string, UIEquipmentSlot>();
|
||||
|
||||
public UIEquipment(VisualElement element) : base(element) {
|
||||
InitialSlot(1, SlotType.主手);
|
||||
InitialSlot(2, SlotType.副手);
|
||||
InitialSlot(3, SlotType.上衣);
|
||||
InitialSlot(4, SlotType.项链);
|
||||
|
||||
InitialSlot(4, SlotType.戒指1);
|
||||
InitialSlot(4, SlotType.戒指2);
|
||||
InitialSlot(4, SlotType.手镯1);
|
||||
InitialSlot(4, SlotType.手镯2);
|
||||
|
||||
InitialSlot(4, SlotType.头盔);
|
||||
InitialSlot(4, SlotType.手套);
|
||||
InitialSlot(4, SlotType.腰带);
|
||||
InitialSlot(4, SlotType.鞋子);
|
||||
}
|
||||
public void Dispose() {
|
||||
|
||||
}
|
||||
|
||||
public void Settings(Equipment equipment) {
|
||||
if (this.equipment != null) { Dispose(); }
|
||||
this.equipment = equipment;
|
||||
if (equipment == null) { return; }
|
||||
foreach (var item in dictionary) {
|
||||
if (!equipment.ContainsKey(item.Key)) { continue; }
|
||||
EquipmentSlot slot = equipment[item.Key];
|
||||
item.Value.Settings(slot);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitialSlot(int index, SlotType type) {
|
||||
VisualElement element = Q<VisualElement>($"EquipmentSlot{index}");
|
||||
UIEquipmentSlot uiSlot = new UIEquipmentSlot(element);
|
||||
dictionary.Add(type.ToString(), uiSlot);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// UI装备插槽
|
||||
/// </summary>
|
||||
public class UIEquipmentSlot : ModuleUIPanel, DragContainer {
|
||||
|
||||
public EquipmentSlot value;
|
||||
|
||||
public Label CountLabel => Q<Label>("Count");
|
||||
public VisualElement Image => Q<VisualElement>("Image");
|
||||
|
||||
public int Count => 1;
|
||||
public DataItem Item => value.item;
|
||||
public VisualElement Anchor => element;
|
||||
|
||||
public UIEquipmentSlot(VisualElement element) : base(element) {
|
||||
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("equipment-image-drag", true);
|
||||
}
|
||||
private void MouseOverEvent(MouseOverEvent evt) {
|
||||
if (!Verify()) { return; }
|
||||
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 Settings(EquipmentSlot value) {
|
||||
this.value = value;
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Settings(DataItem item, int count) {
|
||||
value.Settings(item, count);
|
||||
UpdatePreview();
|
||||
}
|
||||
public void Cancel() {
|
||||
Image.EnableInClassList("equipment-image-drag", false);
|
||||
}
|
||||
|
||||
private void UpdatePreview() {
|
||||
Sprite sprite = value == null || value.item == null ? null : value.item.sprite;
|
||||
Image.style.backgroundImage = new StyleBackground(sprite);
|
||||
}
|
||||
private bool Verify() {
|
||||
DragContainer container = UIPopupManager.I.dragItem.container;
|
||||
if (container == null) { return false; }
|
||||
if (container.Item is DataEquipment equipment) { return value.Verify(equipment); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -36,50 +36,3 @@ public class UIInventory : ModuleUIPanel {
|
||||
items.Create(inventory.slots);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// UI库存插槽
|
||||
/// </summary>
|
||||
public class UIInventorySlot : 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 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.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 Settings(DataItem 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);
|
||||
}
|
||||
}
|
||||
+8
-24
@@ -7,7 +7,7 @@ using MuHua;
|
||||
/// <summary>
|
||||
/// UI拖拽物品
|
||||
/// </summary>
|
||||
public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
public class UIInventoryDrag : ModuleUIPanel, UIControl {
|
||||
/// <summary> 画布 </summary>
|
||||
public VisualElement canvas;
|
||||
/// <summary> 是否启用 </summary>
|
||||
@@ -17,15 +17,15 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
/// <summary> 偏移位置 </summary>
|
||||
public Vector2 offsetPosition;
|
||||
/// <summary> 物品容器 </summary>
|
||||
public DragContainer container;
|
||||
public InventoryDrag container;
|
||||
/// <summary> 目标容器 </summary>
|
||||
public DragContainer targetContainer;
|
||||
public InventoryDrag 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) {
|
||||
public UIInventoryDrag(VisualElement element, VisualElement canvas) : base(element) {
|
||||
this.canvas = canvas;
|
||||
ModuleUI.AddControl(this);
|
||||
canvas.RegisterCallback<MouseUpEvent>(MouseUpEvent);
|
||||
@@ -39,7 +39,7 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
// 交换物品
|
||||
if (container == null || targetContainer == null) { return; }
|
||||
int count = targetContainer.Count;
|
||||
DataItem item = targetContainer.Item;
|
||||
InventoryItem item = targetContainer.Item;
|
||||
targetContainer.Settings(container.Item, container.Count);
|
||||
container.Settings(item, count);
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
}
|
||||
|
||||
/// <summary> 设置容器 </summary>
|
||||
public void Settings(DragContainer container) {
|
||||
public void Settings(InventoryDrag container) {
|
||||
this.container = container;
|
||||
|
||||
isEnable = container.Item != null;
|
||||
@@ -65,11 +65,11 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
if (isEnable) { UpdatePreview(); }
|
||||
}
|
||||
/// <summary> 进入容器 </summary>
|
||||
public void EnterContainer(DragContainer container) {
|
||||
public void EnterContainer(InventoryDrag container) {
|
||||
targetContainer = container;
|
||||
}
|
||||
/// <summary> 退出容器 </summary>
|
||||
public void ExitContainer(DragContainer container) {
|
||||
public void ExitContainer(InventoryDrag container) {
|
||||
if (targetContainer == container) { targetContainer = null; }
|
||||
}
|
||||
|
||||
@@ -81,19 +81,3 @@ public class UIDragItem : ModuleUIPanel, UIControl {
|
||||
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 Settings(DataItem item, int count);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bad4c66685a426345b174ec593f67f56
|
||||
guid: ef816982615753c4c9d3ebcf38d51598
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
Reference in New Issue
Block a user