JD2022-TU1/main/tools/thirdparty/Vegas/VegasToTempoBlasterPlugin/VegasEntryPoint.cs

210 lines
6.6 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Threading;
using DccToolToTempoBlasterPlugin;
using Sony.Vegas;
using System;
using System.Collections;
using System.Windows.Forms;
using UAF.Remoting;
using VARDESC = System.Runtime.InteropServices.ComTypes.VARDESC;
namespace VegasToTempoBlasterPlugin
{
public class VegasModuleEntry : ICustomCommandModule
{
public const string DisplayName = @"TempoBlaster";
public const string ExtensionName = @"TempoBlasterExtension";
public const string CommandName = @"TempoBlasterMenu";
private Vegas _vegas;
private readonly DccToTempoBlasterPlugin _plugin;
private CommunityMashupPluginHandler _communityMashupPluginHandler;
private bool _isInside;
private bool _isShowMessage;
private ProjectUsefulInfo _audioInfo = null;
public VegasModuleEntry()
{
//MessageBox.Show("Attach to process now!");
_plugin = new DccToTempoBlasterPlugin();
}
public void InitializeModule ( Vegas vegas )
{
_vegas = vegas;
_communityMashupPluginHandler = new CommunityMashupPluginHandler(_plugin, vegas);
var timer = new System.Windows.Forms.Timer {Interval = 15};
timer.Tick += this.Tick;
timer.Start();
}
private void Tick(object sender, EventArgs e)
{
// Be careful, we are ticked here from timer, and we don't want to have
// re-entrant situation
if (_isInside)
{
return;
}
try
{
_isInside = true;
Tick();
}
finally
{
_isInside = false;
}
}
private void TickWavePath()
{
var wavePath = _plugin.WavePath;
using (wavePath.ClearValueChanged())
{
if (wavePath.ValueChanged)
{
_audioInfo = new ProjectUsefulInfo(_vegas, wavePath.Value);
_isShowMessage = true;
}
}
if (null != _audioInfo)
{
_audioInfo.Update();
if (_audioInfo.HasAllInfo)
{
var audioStartTime = TimeSpan.FromMilliseconds(_audioInfo.Event.Start.ToMilliseconds());
_plugin.UpdateAudioStartTime(audioStartTime);
}
else
{
if (_isShowMessage)
{
MessageBox.Show(
string.Format("The current vegas composition does not refer the wave '{0}'.\nThe synchronization with the TempoBlaster will assume the wave starts at the beginning of the composition.",
_audioInfo.TakeName), "TempoBlaster synchronization warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
_isShowMessage = false;
}
}
}
}
private void TickCurrentTime()
{
var transport = _vegas.Transport;
var isAudioPlaying = transport.IsPlaying;
var cursorPosition = transport.PlayCursorPosition;
var isPlaying = _plugin.IsPlaying;
var currentTime = _plugin.CurrentTime;
// IMPORTANT:
// When Vegas is remotely pilot, ensure it is in foreground and
// has focus.
// If not Vegas 'Play'/'Stop'/'Cursor position update' may fail.
using (isPlaying.ValueChanged || currentTime.ValueChanged ? TakeFocus() : null)
{
using (isPlaying.ClearValueChanged())
{
if (isPlaying.ValueChanged)
{
if (isPlaying)
{
transport.Play();
isAudioPlaying = true;
}
else
{
transport.Stop();
isAudioPlaying = false;
}
}
}
if (currentTime.ValueChanged)
{
var wasPlaying = transport.IsPlaying;
if (wasPlaying)
{
transport.Stop();
}
cursorPosition = Timecode.FromMilliseconds(currentTime.Value.TotalMilliseconds);
transport.CursorPosition = cursorPosition;
transport.ViewCursor(true);
if (wasPlaying)
{
transport.Play();
}
}
_plugin.UpdatePlayingState(isAudioPlaying, TimeSpan.FromMilliseconds(cursorPosition.ToMilliseconds()));
}
}
private IDisposable TakeFocus()
{
var currentForegroundWindow = User32.GetForegroundWindow();
var hWnd = _vegas.MainWindow.Handle;
User32.SetForegroundWindow(hWnd);
User32.ShowWindow(hWnd, (int)SwMsgs.SW_MAXIMIZE);
return new DisposableAction(() => User32.SetForegroundWindow(currentForegroundWindow));
}
private void Tick()
{
TickWavePath();
TickCurrentTime();
_communityMashupPluginHandler.Tick();
}
public ICollection GetCustomCommands()
{
var markerCommand = new CustomCommand(CommandCategory.Edit, "Add marker in TempoBlaster")
{
CanAddToKeybindings = true,
CanAddToToolbar = false,
CanAddToMenu = false
};
markerCommand.Invoked += this.AddMarker;
return new CustomCommand[] { markerCommand };
}
private void AddMarker(object sender, EventArgs e)
{
var markerPosition = TimeSpan.FromMilliseconds(_vegas.Transport.PlayCursorPosition.ToMilliseconds());
_plugin.AddMarker(markerPosition);
}
private class DisposableAction : IDisposable
{
private readonly Action _disposeAction;
public DisposableAction(Action disposeAction)
{
_disposeAction = disposeAction;
}
public void Dispose()
{
_disposeAction();
}
}
}
}