using Godot; using System.Collections.Generic; namespace EGFramework.Examples.ModbusDebugTool{ public partial class ViewMenu : Node,IEGFramework { [Export] public PackedScene StorageItem = GD.Load("res://Example/ModbusDebugTool/Component/modbus_item.tscn"); public PopupMenu MenuAdd; public PopupMenu MenuSettings; public GridContainer ModbusItemContainer; public ViewEdit Edit; public EasyEvent OnModbusRTUGet { set; get; } = new EasyEvent(); public override void _Ready() { MenuAdd = this.GetNode("MenuBar/添加"); MenuSettings = this.GetNode("MenuBar/设置"); ModbusItemContainer = this.GetNode("Scroll/ModbusList"); Edit = this.GetNode("Edit"); Edit.Visible = false; this.EGEnabledProtocolTools(); DataModbus dataModbus = this.EGSave().GetObjectFromJson(); if (dataModbus == null) { dataModbus = new DataModbus(); } this.EGRegisterObject(dataModbus); RefreshSaveData(); this.EGRegisterMessageEvent((e,sender,ProtocolType)=>{ if(ProtocolType == ProtocolType.SerialPort){ this.AppendMessage("【接收-"+sender+"】 "+e.SourceData.ToStringByHex()); OnModbusRTUGet.Invoke(e); } }); this.EGOnMessage(); } public void OpenEdit(int AddMenuId){ GD.Print(MenuAdd.GetItemText(AddMenuId)); switch(MenuAdd.GetItemText(AddMenuId)){ case "保持寄存器": this.OnNewEdit(ModbusRegisterType.HoldingRegister); break; default: break; } } public void OpenSettings(int OtherId){ switch(MenuSettings.GetItemText(OtherId)){ case "查看报文": this.ViewMessage().Visible = true; break; case "设置": this.ViewSettings().Visible = true; break; default: break; } } public void ReadAll(int ReadMenuId){ GD.Print(MenuAdd.GetItemText(ReadMenuId)); } public void RefreshSaveData(){ ModbusItemContainer.ClearChildren(); if(this.EGGetObject() != null){ foreach(DataModbusItem item in this.EGGetObject().Items.Values){ ViewModbusItem viewItem = StorageItem.Instantiate(); ModbusItemContainer.AddChild(viewItem); viewItem.LoadData(item); } } } } }