Skip to content

Commit

Permalink
Use LiveSplit context menu instead of custom buttons in component ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Shotnex4 committed Jan 28, 2024
1 parent 2943b01 commit 0e063da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 102 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ However of course feel free to use it for your own runs that you do not want to
1. Download the dlls from the latest release on the [releases page](https://github.com/Shotnex4/LiveSplit.TimeAttackPause/releases) and put them in your LiveSplit/Components folder.
2. Add the component to your layout. (Right click → Edit Layout → Timer → TimeAttackPause)
3. Start your run and pause your timer at any time.
4. Click on the "Export" button in the component to save your splits to a json file.
4. Use Control → `Export current run` to save your splits to a json file.
5. Now you can close LiveSplit. Shut down your PC. Go to sleep. Whatever you want.
6. When you are ready to continue your run, open LiveSplit again and load your splits by clicking on the "Import" button in the component.
6. When you are ready to continue your run, open LiveSplit again and load your splits by using Control → `Import run`.

![example.png](doc/example.png)
106 changes: 15 additions & 91 deletions UI/Components/TimeAttackPauseComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
Expand All @@ -10,10 +11,6 @@ namespace LiveSplit.UI.Components
{
public class TimeAttackPauseComponent : ControlComponent
{
private readonly Button _exportButton;
private readonly Button _importButton;


private TimeAttackPauseSettings Settings { get; set; }

private ITimerModel Model { get; set; }
Expand All @@ -23,60 +20,29 @@ public class TimeAttackPauseComponent : ControlComponent

public override string ComponentName => "TimeAttackPause";

public override float HorizontalWidth => 50;
public override float MinimumWidth => 160;
public override float VerticalHeight => 40;
public override float MinimumHeight => 40;
public override float HorizontalWidth => 0;
public override float MinimumWidth => 0;
public override float VerticalHeight => 0;
public override float MinimumHeight => 0;

// This function is called when LiveSplit creates your component. This happens when the
// component is added to the layout, or when LiveSplit opens a layout with this component
// already added.
public TimeAttackPauseComponent(LiveSplitState state) : this(state, CreateFormControl(state))
public TimeAttackPauseComponent(LiveSplitState state) : this(state, CreateFormControl())
{
ContextMenuControls = new Dictionary<string, Action>();
ContextMenuControls.Add("Export current run", ExportCurrentRun);
ContextMenuControls.Add("Import run", ImportRun);
}

private static Control CreateFormControl(LiveSplitState state)
private static Control CreateFormControl()
{
// create a basic button to export the current run state
var exportButton = new Button
{
Name = "ExportButton",
Text = "Export",
Location = new Point(0, 0),
BackColor = Color.LightBlue,
Dock = DockStyle.Fill,
};

// create a new button that will be used to import a run state
var importButton = new Button
{
Name = "ImportButton",
Text = "Import",
Location = new Point(0, 0),
BackColor = Color.LightBlue,
Dock = DockStyle.Fill,
};

var tableLayoutContainer = new TableLayoutPanel
// not used anymore
return new FlowLayoutPanel
{
Name = "Container",
Size = new Size(100, 32),
ColumnCount = 2,
RowCount = 1,
AutoSize = false,
Size = new Size(0, 0),
Location = new Point(0, 0),
ColumnStyles =
{
new ColumnStyle(SizeType.Percent, 50F),
new ColumnStyle(SizeType.Percent, 50F),
}
};

// add the buttons to the container
tableLayoutContainer.Controls.Add(exportButton);
tableLayoutContainer.Controls.Add(importButton);

return tableLayoutContainer;
}

private TimeAttackPauseComponent(LiveSplitState state, Control formControl) : base(state, formControl,
Expand All @@ -85,21 +51,10 @@ private TimeAttackPauseComponent(LiveSplitState state, Control formControl) : ba
Settings = new TimeAttackPauseSettings();
Model = new TimerModel() { CurrentState = state };

_exportButton = formControl.Controls["ExportButton"] as Button;
_importButton = formControl.Controls["importButton"] as Button;

if (_exportButton == null || _importButton == null)
{
throw new Exception("Could not find the buttons in the form control.");
}

_exportButton.Click += OnExportButtonClick;
_importButton.Click += OnImportButtonClick;

CurrentState = state;
}

private void OnExportButtonClick(object sender, EventArgs e)
private void ExportCurrentRun()
{
if (CurrentState.CurrentPhase == TimerPhase.Running)
{
Expand All @@ -117,7 +72,7 @@ private void OnExportButtonClick(object sender, EventArgs e)
SplitsStateWriter.SaveSplitsState(CurrentState, saveFileDialog.FileName);
}

private void OnImportButtonClick(object sender, EventArgs e)
private void ImportRun()
{
// Displays a OpenFileDialog so the user can save the Run as json file
OpenFileDialog openFileDialog = new OpenFileDialog();
Expand All @@ -142,27 +97,6 @@ static void ErrorCallback(Form form, Exception ex)
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

private void DisposeIfError()
{
if (ErrorWithControl)
{
Dispose();
}
}

public override void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
base.DrawHorizontal(g, state, height, clipRegion);
DisposeIfError();
}

// We will be adding the ability to display the component across two rows in our settings menu.
public override void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
base.DrawVertical(g, state, width, clipRegion);
DisposeIfError();
}

public override Control GetSettingsControl(LayoutMode mode)
{
Settings.Mode = mode;
Expand All @@ -188,16 +122,6 @@ public override void Update(IInvalidator invalidator, LiveSplitState state, floa
CurrentState = state;
}

// This function is called when the component is removed from the layout, or when LiveSplit
// closes a layout with this component in it.
public override void Dispose()
{
base.Dispose();

_exportButton.Click -= OnExportButtonClick;
_importButton.Click -= OnImportButtonClick;
}

// I do not know what this is for.
public int GetSettingsHashCode() => Settings.GetSettingsHashCode();
}
Expand Down
15 changes: 6 additions & 9 deletions UI/Components/TimeAttackPauseFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using LiveSplit.Model;
using System;
using LiveSplit.Model;
using LiveSplit.UI.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiveSplit.TimeAttackPause.UI.Components
{
internal class TimeAttackPauseFactory: IComponentFactory
internal class TimeAttackPauseFactory : IComponentFactory
{
// The displayed name of the component in the Layout Editor.
public string ComponentName => "Time Attack Pause";

public string Description => "Adds the possibility to save your progress mid run to a file and then continue from this point on. Only use this for TA runs or unofficial runs that you do not submit to the leaderboards.";
public string Description =>
"Adds the possibility to save your progress mid run to a file and then continue from this point on. Only use this for TA runs or unofficial runs that you do not submit to the leaderboards.";

// The sub-menu this component will appear under when adding the component to the layout.
public ComponentCategory Category => ComponentCategory.Timer;
Expand All @@ -34,4 +31,4 @@ internal class TimeAttackPauseFactory: IComponentFactory

public Version Version => Version.Parse("1.0.0");
}
}
}
Binary file modified doc/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0e063da

Please sign in to comment.