4 using Vintagestory.API.Client;
5 using Vintagestory.API.Common;
6 using Vintagestory.API.Datastructures;
10 public class AutomapGUIDialog : GuiDialog
12 public const string _automapControlPanelKey = "automapControlPanelKey";
13 private const string _statusTextKey = @"txtStatus";
14 private const string _noteTextKey = @"edtNote";
15 private const string _swAutostart = @"swAutostart";
16 private const string _btnRunKey = @"btnRun";
17 private const string _btnSnapKey = @"btnSnap";
19 private ILogger Logger;
20 private PersistedConfiguration configuration;
21 private long dashTickHandle;
23 public override string ToggleKeyCombinationCode
26 return _automapControlPanelKey;
30 private uint totalShards, voidShards, changesThisTick;
31 private CommandType lastState;
34 public AutomapGUIDialog(ICoreClientAPI capi, AutomapSystem ams, PersistedConfiguration cfg) : base(capi)
39 capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
42 public AutomapGUIDialog(ICoreClientAPI capi) : base(capi)
46 //Event for GUI status display
48 private void SetupDialog()
50 ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle);
52 ElementBounds textBounds = ElementBounds.Fixed(0, 40, 500, 300);
54 ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
55 bgBounds.BothSizing = ElementSizing.FitToChildren;
56 bgBounds.WithChildren(textBounds);
58 ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(0, 72, 5, 2);
59 toggleBounds.fixedHeight = 24;
60 toggleBounds.fixedWidth = 64;
62 ElementBounds autostartBounds = toggleBounds.RightCopy(66, 0, 1, 1);
63 autostartBounds.fixedHeight = 24;
64 autostartBounds.fixedWidth = 80;
66 ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
67 txtStatusBounds.fixedHeight = 16;
68 txtStatusBounds.percentWidth = 1;
70 ElementBounds btnNoteArea = textBounds.CopyOffsetedSibling(0, 42, 2, 5);
71 btnNoteArea.fixedHeight = 24;
72 btnNoteArea.fixedWidth = 20;
74 ElementBounds txtNoteArea = btnNoteArea.CopyOffsetedSibling(64, 0, 1, 6);
75 txtNoteArea.fixedHeight = 24;
76 txtNoteArea.fixedWidth = 256;
78 ElementBounds btnSnapshotArea = btnNoteArea.CopyOffsetedSibling(0, 64, 2, 5);
79 btnNoteArea.fixedHeight = 24;
80 btnNoteArea.fixedWidth = 28;
83 this.SingleComposer = capi.Gui.CreateCompo("automapControlPanel", dialogBounds)
84 .AddShadedDialogBG(bgBounds)
85 .AddDialogTitleBar("Automap Controls", OnTitleBarCloseClicked)
86 .AddStaticText("Configure Automap settings:", CairoFont.WhiteDetailText(), textBounds)
87 .AddToggleButton("Run", CairoFont.ButtonText(), RunToggle, toggleBounds, _btnRunKey)
88 .AddSwitch(AutostartChange,autostartBounds,_swAutostart)
89 .AddStaticText("Autostart",CairoFont.WhiteDetailText(),autostartBounds.RightCopy(16))
90 .AddDynamicText("Idle.", CairoFont.WhiteSmallText().WithFontSize(12), EnumTextOrientation.Left, txtStatusBounds, _statusTextKey)
91 .AddTextInput(txtNoteArea, null, CairoFont.WhiteMediumText().WithFontSize(16), _noteTextKey)
92 .AddButton("Note:", CreateNote, btnNoteArea, CairoFont.ButtonText())
93 .AddButton("Snapshot!", TriggerSnapshot, btnSnapshotArea, CairoFont.ButtonText( ), key: _btnSnapKey)
96 //Controls for ALL Block & Entity Designators (Enable/Disable) <-- block edits while in 'Run' state
97 //_automapSystem.BlockID_Designators
98 //_automapSystem.Entity_Designators
100 //Message verbosity? Speed?
101 var swAutostart = this.SingleComposer.GetSwitch(_swAutostart);
102 swAutostart.On = this.configuration.Autostart;
106 private void OnTitleBarCloseClicked()
111 public override void OnGuiOpened( )
114 UpdateDashDisplay(0f);
115 if (dashTickHandle == 0L) dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 1000);
118 public override void OnGuiClosed( )
122 if (dashTickHandle != 0L) capi.Event.UnregisterGameTickListener(dashTickHandle);
126 /// Toggle Automap from/to RUN state
128 /// <returns>The toggle.</returns>
129 /// <param name="toggle">Run.</param>
130 internal void RunToggle(bool toggle)
132 Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
134 UpdateDashDisplay(0f);
136 CommandData cmd = new CommandData(toggle ? CommandType.Run : CommandType.Stop);
138 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
141 private bool CreateNote()
143 var noteCmd = new CommandData(CommandType.Notation);
144 var txtNote = this.SingleComposer.GetTextInput(_noteTextKey);
145 if (!String.IsNullOrWhiteSpace(txtNote.GetText()))
147 noteCmd.Notation = txtNote.GetText().Replace("\n", " ");
148 txtNote.SetValue(string.Empty);
150 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, noteCmd);
152 return true;//FINDOUT: What does this DO?
155 private bool TriggerSnapshot( )
157 var snappyCmd = new CommandData(CommandType.Snapshot);
159 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, snappyCmd);
161 return true;//FINDOUT: What does this DO?
164 private void AutostartChange(bool startValue)
166 configuration.Autostart = startValue;
169 private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
171 Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg");
172 StatusData realData = data as StatusData;
173 totalShards = realData.TotalUpdates;
174 voidShards = realData.VoidChunks;
175 changesThisTick = realData.Delta;
176 lastState = realData.CurrentState;
179 private void UpdateDashDisplay(float delay)
181 var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
183 var btnRun = this.SingleComposer.GetToggleButton(_btnRunKey);
185 if (lastState == CommandType.Run) {
186 statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: ±{changesThisTick} Nulls: {voidShards} ");
188 if (!btnRun.On) btnRun.SetValue(true);
191 else if (lastState == CommandType.Stop && btnRun.Enabled) {
192 statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Nulls: {voidShards} ");
194 if (btnRun.On) btnRun.SetValue(false);