OSDN Git Service

W.I.P. VII: Cmd events mostly Protobufferized,
[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
15                 private ILogger Logger;
16
17                 private long dashTickHandle;
18
19                 public override string ToggleKeyCombinationCode {
20                         get
21                         {
22                                 return _automapControlPanelKey;
23                         }
24                 }
25
26                 private uint totalShards, voidShards, changesThisTick;
27                 private RunState lastState;
28
29
30                 public AutomapGUIDialog(ICoreClientAPI capi,AutomapSystem ams) : base(capi)
31         {
32
33                 Logger = capi.Logger;
34                 SetupDialog( );
35                 capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
36                 
37                 }
38
39                 //Event for GUI status display
40
41                 private void SetupDialog( )
42                 {
43                 ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle);
44                                         
45                 ElementBounds textBounds = ElementBounds.Fixed(0, 40, 500, 300);
46                 
47                 ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
48                 bgBounds.BothSizing = ElementSizing.FitToChildren;
49                 bgBounds.WithChildren(textBounds);
50
51                 ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 64, 5, 2);
52                 toggleBounds.fixedHeight = 24;
53                 toggleBounds.fixedWidth = 64;
54
55                 ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
56                 txtStatusBounds.fixedHeight = 16;
57                 txtStatusBounds.percentWidth = 1;
58
59
60                 this.SingleComposer = capi.Gui.CreateCompo("automapControlPanel", dialogBounds)
61                         .AddShadedDialogBG(bgBounds)
62                         .AddDialogTitleBar("Automap Controls", OnTitleBarCloseClicked)
63                         .AddStaticText("Configure Automap settings:", CairoFont.WhiteDetailText( ), textBounds)
64                         .AddToggleButton("Run", CairoFont.ButtonText( ), RunToggle, toggleBounds, "btnRun")
65                         .AddDynamicText("Idle.", CairoFont.WhiteSmallText( ).WithFontSize(12), EnumTextOrientation.Left, txtStatusBounds, _statusTextKey)
66                         .Compose( );
67
68                         //Controls for ALL Block & Entity Designators (Enable/Disable)
69                         //_automapSystem.BlockID_Designators
70                         //_automapSystem.Entity_Designators
71                         //Renderer selection
72                         //Message verbosity? Speed?
73
74                         //A Button to add POI - notes manually (when AM running)
75
76                 }
77
78                 private void OnTitleBarCloseClicked( )
79                 {
80                 TryClose( );
81                 }
82
83                 /// <summary>
84                 /// Toggle Automap from/to RUN state
85                 /// </summary>
86                 /// <returns>The toggle.</returns>
87                 /// <param name="toggle">Run.</param>
88                 internal void RunToggle(bool toggle)
89                 {
90                 Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
91                 var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
92                         statusText.SetNewText($"State: {(toggle? "Run": "Halt" )}, Total: {totalShards}, Nulls: {voidShards} " );
93
94                 CommandData cmd;
95
96                 if (toggle) {
97                 dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 6001);
98                 cmd = new CommandData(toggle ? RunState.Run : RunState.Stop, new bool[ ] { true, true, true, true, true });
99                 }
100                 else {
101                 capi.Event.UnregisterGameTickListener(dashTickHandle);
102                 cmd = new CommandData(toggle ? RunState.Run : RunState.Stop);
103                 }
104                                         
105                 capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
106                 }
107
108
109                 private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
110                 {
111                 Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg");
112                 StatusData realData = data as StatusData;
113                 totalShards = realData.TotalUpdates;
114                 voidShards = realData.VoidChunks;
115                 changesThisTick = realData.Delta;
116                 lastState = realData.CurrentState;
117                 }
118
119                 private void UpdateDashDisplay(float delay )
120                 {
121                 var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
122                 statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} ");
123
124
125                 }
126
127
128         }
129 }
130