OSDN Git Service

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