OSDN Git Service

Fixes for Automatic startup
[automap/automap.git] / Automap / Subsystems / AutomapGUIDialog.cs
index 1692997..72471f0 100644 (file)
@@ -11,73 +11,115 @@ namespace Automap
        {
                public const string _automapControlPanelKey = "automapControlPanelKey";
                private const string _statusTextKey = @"txtStatus";
+               private const string _noteTextKey = @"edtNote";
+               private const string _swAutostart = @"swAutostart";
+               private const string _btnRunKey = @"btnRun";
+               private const string _btnSnapKey = @"btnSnap";
 
                private ILogger Logger;
-
+               private PersistedConfiguration configuration;
                private long dashTickHandle;
 
-               public override string ToggleKeyCombinationCode {
-                       get
-                       {
+               public override string ToggleKeyCombinationCode
+               {
+                       get {
                                return _automapControlPanelKey;
                        }
                }
 
                private uint totalShards, voidShards, changesThisTick;
-               private RunState lastState;
+               private CommandType lastState;
 
 
-               public AutomapGUIDialog(ICoreClientAPI capi,AutomapSystem ams) : base(capi)
-       {
+               public AutomapGUIDialog(ICoreClientAPI capi, AutomapSystem ams, PersistedConfiguration cfg) : base(capi)
+               {
+                       configuration = cfg;
+                       Logger = capi.Logger;
+                       SetupDialog();
+                       capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
+               }
 
-               Logger = capi.Logger;
-               SetupDialog( );
-               capi.Event.RegisterEventBusListener(AutomapStatusMsg, 1.0D, AutomapSystem.AutomapStatusEventKey);
-               
+               public AutomapGUIDialog(ICoreClientAPI capi) : base(capi)
+               {                       
                }
 
                //Event for GUI status display
 
-               private void SetupDialog( )
+               private void SetupDialog()
                {
-               ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle);
-                                       
-               ElementBounds textBounds = ElementBounds.Fixed(0, 40, 500, 300);
-               
-               ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
-               bgBounds.BothSizing = ElementSizing.FitToChildren;
-               bgBounds.WithChildren(textBounds);
+                       ElementBounds dialogBounds = ElementStdBounds.AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle);
+
+                       ElementBounds textBounds = ElementBounds.Fixed(0, 40, 500, 300);
+
+                       ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
+                       bgBounds.BothSizing = ElementSizing.FitToChildren;
+                       bgBounds.WithChildren(textBounds);
 
-               ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(3, 64, 5, 2);
-               toggleBounds.fixedHeight = 24;
-               toggleBounds.fixedWidth = 64;
+                       ElementBounds toggleBounds = textBounds.CopyOffsetedSibling(0, 72, 5, 2);
+                       toggleBounds.fixedHeight = 24;
+                       toggleBounds.fixedWidth = 64;
 
-               ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
-               txtStatusBounds.fixedHeight = 16;
-               txtStatusBounds.percentWidth = 1;
+                       ElementBounds autostartBounds = toggleBounds.RightCopy(66, 0, 1, 1);
+                       autostartBounds.fixedHeight = 24;
+                       autostartBounds.fixedWidth = 80;
 
+                       ElementBounds txtStatusBounds = textBounds.CopyOffsetedSibling(0, 26, 2, 4);
+                       txtStatusBounds.fixedHeight = 16;
+                       txtStatusBounds.percentWidth = 1;
 
-               this.SingleComposer = capi.Gui.CreateCompo("automapControlPanel", dialogBounds)
-                       .AddShadedDialogBG(bgBounds)
-                       .AddDialogTitleBar("Automap Controls", OnTitleBarCloseClicked)
-                       .AddStaticText("Configure Automap settings:", CairoFont.WhiteDetailText( ), textBounds)
-                       .AddToggleButton("Run", CairoFont.ButtonText( ), RunToggle, toggleBounds, "btnRun")
-                       .AddDynamicText("Idle.", CairoFont.WhiteSmallText( ).WithFontSize(12), EnumTextOrientation.Left, txtStatusBounds, _statusTextKey)
-                       .Compose( );
+                       ElementBounds btnNoteArea = textBounds.CopyOffsetedSibling(0, 42, 2, 5);
+                       btnNoteArea.fixedHeight = 24;
+                       btnNoteArea.fixedWidth = 20;
 
-                       //Controls for ALL Block & Entity Designators (Enable/Disable)
+                       ElementBounds txtNoteArea = btnNoteArea.CopyOffsetedSibling(64, 0, 1, 6);
+                       txtNoteArea.fixedHeight = 24;
+                       txtNoteArea.fixedWidth = 256;
+
+                       ElementBounds btnSnapshotArea = btnNoteArea.CopyOffsetedSibling(0, 64, 2, 5);
+                       btnNoteArea.fixedHeight = 24;
+                       btnNoteArea.fixedWidth = 28;
+
+
+                       this.SingleComposer = capi.Gui.CreateCompo("automapControlPanel", dialogBounds)
+                               .AddShadedDialogBG(bgBounds)
+                               .AddDialogTitleBar("Automap Controls", OnTitleBarCloseClicked)
+                               .AddStaticText("Configure Automap settings:", CairoFont.WhiteDetailText(), textBounds)
+                               .AddToggleButton("Run", CairoFont.ButtonText(), RunToggle, toggleBounds, _btnRunKey)
+                               .AddSwitch(AutostartChange,autostartBounds,_swAutostart)
+                               .AddStaticText("Autostart",CairoFont.WhiteDetailText(),autostartBounds.RightCopy(16))
+                               .AddDynamicText("Idle.", CairoFont.WhiteSmallText().WithFontSize(12), EnumTextOrientation.Left, txtStatusBounds, _statusTextKey)
+                               .AddTextInput(txtNoteArea, null, CairoFont.WhiteMediumText().WithFontSize(16), _noteTextKey)
+                               .AddButton("Note:", CreateNote, btnNoteArea, CairoFont.ButtonText())
+                               .AddButton("Snapshot!", TriggerSnapshot, btnSnapshotArea, CairoFont.ButtonText( ), key: _btnSnapKey)
+                               .Compose();
+
+                       //Controls for ALL Block & Entity Designators (Enable/Disable) <-- block edits while in 'Run' state
                        //_automapSystem.BlockID_Designators
                        //_automapSystem.Entity_Designators
                        //Renderer selection
                        //Message verbosity? Speed?
+                       var swAutostart = this.SingleComposer.GetSwitch(_swAutostart);
+                       swAutostart.On = this.configuration.Autostart;
 
-                       //A Button to add POI - notes manually (when AM running)
+               }
 
+               private void OnTitleBarCloseClicked()
+               {
+                       TryClose();
                }
 
-               private void OnTitleBarCloseClicked( )
+               public override void OnGuiOpened( )
                {
-               TryClose( );
+               base.OnGuiOpened( );
+               UpdateDashDisplay(0f);
+               if (dashTickHandle == 0L)  dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 1000);
+               }
+
+               public override void OnGuiClosed( )
+               {
+               base.OnGuiClosed( );
+
+               if (dashTickHandle != 0L) capi.Event.UnregisterGameTickListener(dashTickHandle);
                }
 
                /// <summary>
@@ -87,41 +129,75 @@ namespace Automap
                /// <param name="toggle">Run.</param>
                internal void RunToggle(bool toggle)
                {
-               Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
-               var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
-               statusText.SetNewText($"State: {(toggle? "Run": "Halt" )}, Total: ?, Nulls: 0 " );
+                       Logger.VerboseDebug("Dialog Changed; [ Automap Enabled: {0} ]", toggle);
 
-               if (toggle) {
-               dashTickHandle = capi.Event.RegisterGameTickListener(UpdateDashDisplay, 6001);
+                       UpdateDashDisplay(0f);
+                                                
+                       CommandData cmd = new CommandData(toggle ? CommandType.Run : CommandType.Stop);
+
+                       capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
                }
-               else {
-               capi.Event.UnregisterGameTickListener(dashTickHandle);
+
+               private bool CreateNote()
+               {
+                       var noteCmd = new CommandData(CommandType.Notation);
+                       var txtNote = this.SingleComposer.GetTextInput(_noteTextKey);
+                       if (!String.IsNullOrWhiteSpace(txtNote.GetText()))
+                       {
+                               noteCmd.Notation = txtNote.GetText().Replace("\n", " ");
+                               txtNote.SetValue(string.Empty);
+
+                               capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, noteCmd);
+                       }
+                       return true;//FINDOUT: What does this DO?
                }
 
-               CommandData cmd = new CommandData(toggle ? RunState.Run : RunState.Stop);
-               capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, cmd);
+               private bool TriggerSnapshot( )
+               {
+               var snappyCmd = new CommandData(CommandType.Snapshot);
+               
+               capi.Event.PushEvent(AutomapSystem.AutomapCommandEventKey, snappyCmd);
+               
+               return true;//FINDOUT: What does this DO?
                }
 
+               private void AutostartChange(bool startValue)
+               {
+               configuration.Autostart = startValue;           
+               }
 
                private void AutomapStatusMsg(string eventName, ref EnumHandling handling, IAttribute data)
                {
-               Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg");
-               StatusData realData = data as StatusData;
-               totalShards = realData.TotalUpdates;
-               voidShards = realData.VoidChunks;
-               changesThisTick = realData.Delta;
-               lastState = realData.CurrentState;
+                       Logger.VerboseDebug("MsgBus RX: AutomapStatusMsg");
+                       StatusData realData = data as StatusData;
+                       totalShards = realData.TotalUpdates;
+                       voidShards = realData.VoidChunks;
+                       changesThisTick = realData.Delta;
+                       lastState = realData.CurrentState;
                }
 
-               private void UpdateDashDisplay(float delay )
+               private void UpdateDashDisplay(float delay)
                {
-               var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
-               statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: {changesThisTick} Nulls: {voidShards} ");
+                       var statusText = this.SingleComposer.GetDynamicText(_statusTextKey);
 
+                       var btnRun = this.SingleComposer.GetToggleButton(_btnRunKey);
 
+                       if (lastState == CommandType.Run) {
+                       statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Delta: ±{changesThisTick} Nulls: {voidShards} ");
+                       
+                       if (!btnRun.On) btnRun.SetValue(true);
+                                                       
+                       }
+                       else if (lastState == CommandType.Stop && btnRun.Enabled) {
+                       statusText.SetNewText($"State: {lastState}, Total: {totalShards}, Nulls: {voidShards} ");                       
+                       
+                       if (btnRun.On) btnRun.SetValue(false);
+                                                       
+                       }
                }
 
 
+
        }
 }