OSDN Git Service

Working (W.I.P.) snapshots!
[automap/automap.git] / Automap / Subsystems / AutomapGUIDialog.cs
1 using System;
2
3
4 using Vintagestory.API.Client;
5 using Vintagestory.API.Common;
6 using Vintagestory.API.Datastructures;
7
8 namespace Automap
9 {
10         public class AutomapGUIDialog : GuiDialog
11         {
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
18                 private ILogger Logger;
19                 private PersistedConfiguration configuration;
20                 private long dashTickHandle;
21
22                 public override string ToggleKeyCombinationCode
23                 {
24                         get {
25                                 return _automapControlPanelKey;
26                         }
27                 }
28
29                 private uint totalShards, voidShards, changesThisTick;
30                 private CommandType lastState;
31
32
33                 public AutomapGUIDialog(ICoreClientAPI capi, AutomapSystem ams, PersistedConfiguration cfg) : base(capi)
34                 {
35
36                         Logger = capi.Logger;
37                         SetupDialog();
38                         capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
39                         configuration = cfg;
40                 }
41
42                 public AutomapGUIDialog(ICoreClientAPI capi) : base(capi)
43                 {                       
44                 }
45
46                 //Event for GUI status display
47
48                 private void SetupDialog()
49                 {
50                         ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle);
51
52                         ElementBounds textBounds = ElementBounds.Fixed(0, 40, 500, 300);
53
54                         ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
55                         bgBounds.BothSizing = ElementSizing.FitToChildren;
56                         bgBounds.WithChildren(textBounds);
57
58                         ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(0, 72, 5, 2);
59                         toggleBounds.fixedHeight = 24;
60                         toggleBounds.fixedWidth = 64;
61
62                         ElementBounds autostartBounds = toggleBounds.RightCopy(66, 0, 1, 1);
63                         autostartBounds.fixedHeight = 24;
64                         autostartBounds.fixedWidth = 80;
65
66                         ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
67                         txtStatusBounds.fixedHeight = 16;
68                         txtStatusBounds.percentWidth = 1;
69
70                         ElementBounds btnNoteArea = textBounds.CopyOffsetedSibling(0, 42, 2, 5);
71                         btnNoteArea.fixedHeight = 24;
72                         btnNoteArea.fixedWidth = 20;
73
74                         ElementBounds txtNoteArea = btnNoteArea.CopyOffsetedSibling(64, 0, 1, 6);
75                         txtNoteArea.fixedHeight = 24;
76                         txtNoteArea.fixedWidth = 256;
77
78                         ElementBounds btnSnapshotArea = btnNoteArea.CopyOffsetedSibling(0, 64, 2, 5);
79                         btnNoteArea.fixedHeight = 24;
80                         btnNoteArea.fixedWidth = 20;
81
82
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( ))
94                                 .Compose();
95
96                         //Controls for ALL Block & Entity Designators (Enable/Disable) <-- block edits while in 'Run' state
97                         //_automapSystem.BlockID_Designators
98                         //_automapSystem.Entity_Designators
99                         //Renderer selection
100                         //Message verbosity? Speed?
101
102
103
104                 }
105
106                 private void OnTitleBarCloseClicked()
107                 {
108                         TryClose();
109                 }
110
111                 public override void OnGuiOpened( )
112                 {
113                 base.OnGuiOpened( );
114                 UpdateDashDisplay(0f);
115                 if (dashTickHandle == 0L)  dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 1000);
116                 }
117
118                 public override void OnGuiClosed( )
119                 {
120                 base.OnGuiClosed( );
121
122                 if (dashTickHandle != 0L) capi.Event.UnregisterGameTickListener(dashTickHandle);
123                 }
124
125                 /// <summary>
126                 /// Toggle Automap from/to RUN state
127                 /// </summary>
128                 /// <returns>The toggle.</returns>
129                 /// <param name="toggle">Run.</param>
130                 internal void RunToggle(bool toggle)
131                 {
132                         Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
133
134                         UpdateDashDisplay(0f);
135                                                  
136                         CommandData cmd = new CommandData(toggle ? CommandType.Run : CommandType.Stop);
137
138                         capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
139                 }
140
141                 private bool CreateNote()
142                 {
143                         var noteCmd = new CommandData(CommandType.Notation);
144                         var txtNote = this.SingleComposer.GetTextInput(_noteTextKey);
145                         if (!String.IsNullOrWhiteSpace(txtNote.GetText()))
146                         {
147                                 noteCmd.Notation = txtNote.GetText();
148                                 txtNote.SetValue(string.Empty);
149
150                                 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, noteCmd);
151                         }
152                         return true;//FINDOUT: What does this DO?
153                 }
154
155                 private bool TriggerSnapshot( )
156                 {
157                 var snappyCmd = new CommandData(CommandType.Snapshot);
158                 
159                 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, snappyCmd);
160                 
161                 return true;//FINDOUT: What does this DO?
162                 }
163
164                 private void AutostartChange(bool startValue)
165                 {
166                 configuration.Autostart = startValue;           
167                 }
168
169                 private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
170                 {
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;
177                 }
178
179                 private void UpdateDashDisplay(float delay)
180                 {
181                         var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
182
183                         var btnRun = this.SingleComposer.GetToggleButton(_btnRunKey);
184
185                         if (lastState == CommandType.Run) {
186                         statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: ±{changesThisTick} Nulls: {voidShards} ");
187                         
188                         if (!btnRun.On) btnRun.SetValue(true);
189
190                         LockGUIElements(true);  
191                         }
192                         else if (lastState == CommandType.Stop && btnRun.Enabled) {
193                         statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Nulls: {voidShards} ");                       
194                         
195                         if (btnRun.On) btnRun.SetValue(false);
196
197                         LockGUIElements(true);
198                         }
199                 }
200
201                 private void LockGUIElements(bool state)
202                 {
203                 var swAutostart = this.SingleComposer.GetSwitch(_swAutostart);
204
205                 swAutostart.Enabled = state;
206
207                 }
208
209         }
210 }
211