OSDN Git Service

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