Added Play/Pause and timer control
This commit is contained in:
Родитель
6aa0230117
Коммит
6778ee0878
|
@ -1,6 +1,6 @@
|
|||
using System.Diagnostics;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Foundation;
|
||||
using System.Threading.Tasks;
|
||||
using WebKit;
|
||||
|
||||
namespace VS4Mac.LottiePlayer.Controls
|
||||
|
@ -9,6 +9,8 @@ namespace VS4Mac.LottiePlayer.Controls
|
|||
{
|
||||
const string HTML_RESOURCE = "LottiePlayer";
|
||||
|
||||
public event EventHandler DurationChanged;
|
||||
|
||||
public LottiePlayer()
|
||||
{
|
||||
NavigationDelegate = new LottiePlayerNavigationDelegate();
|
||||
|
@ -20,7 +22,9 @@ namespace VS4Mac.LottiePlayer.Controls
|
|||
|
||||
public bool Initialized { get; set; }
|
||||
|
||||
public void SetData(string data)
|
||||
public double Duration { get; internal set; }
|
||||
|
||||
public async Task SetDataAsync(string data)
|
||||
{
|
||||
Animation = data;
|
||||
|
||||
|
@ -28,18 +32,37 @@ namespace VS4Mac.LottiePlayer.Controls
|
|||
{
|
||||
var html = CreateHtml(Animation);
|
||||
InitialNavigation = LoadHtmlString(html, null);
|
||||
await Task.Delay(500);
|
||||
LoadAnimationDuration();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
EvaluateJavaScript($"lottie.play();", null);
|
||||
EvaluateJavaScript($"window.animation.play();", null);
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
EvaluateJavaScript($"lottie.pause();", null);
|
||||
EvaluateJavaScript($"window.animation.pause();", null);
|
||||
LoadAnimationDuration();
|
||||
}
|
||||
|
||||
public void GoToAndStop(double value)
|
||||
{
|
||||
Pause();
|
||||
EvaluateJavaScript($"window.animation.goToAndStop({value}, true);", null);
|
||||
}
|
||||
|
||||
void LoadAnimationDuration()
|
||||
{
|
||||
EvaluateJavaScript($"getAnimationDuration();", (result, error) =>
|
||||
{
|
||||
var durationString = result.ToString();
|
||||
Duration = Convert.ToDouble(durationString);
|
||||
DurationChanged?.Invoke(this, null);
|
||||
});
|
||||
}
|
||||
|
||||
static string CreateHtml(string data)
|
||||
|
@ -58,7 +81,7 @@ namespace VS4Mac.LottiePlayer.Controls
|
|||
|
||||
public class LottiePlayerNavigationDelegate : WKNavigationDelegate
|
||||
{
|
||||
public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
|
||||
public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
|
||||
{
|
||||
var lottiePlayer = webView as LottiePlayer;
|
||||
|
||||
|
@ -66,7 +89,7 @@ namespace VS4Mac.LottiePlayer.Controls
|
|||
{
|
||||
lottiePlayer.InitialNavigation = null;
|
||||
lottiePlayer.Initialized = true;
|
||||
lottiePlayer.SetData(lottiePlayer.Animation);
|
||||
await lottiePlayer.SetDataAsync(lottiePlayer.Animation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,20 @@
|
|||
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.4.4/lottie.js" type="text/javascript"></script>
|
||||
<script type="text/javascript">
|
||||
var animationData = '{REPLACE_WITH_ANIMATION}';
|
||||
window.onload = function() {
|
||||
window.animation = bodymovin.loadAnimation({
|
||||
container: document.getElementById('player'),
|
||||
renderer: 'svg',
|
||||
loop: true,
|
||||
autoplay: true,
|
||||
animationData: JSON.parse(animationData)
|
||||
})
|
||||
};
|
||||
|
||||
var animation = bodymovin.loadAnimation({
|
||||
container: document.getElementById('player'),
|
||||
renderer: 'svg',
|
||||
loop: true,
|
||||
autoplay: true,
|
||||
animationData: JSON.parse(animationData)
|
||||
})
|
||||
function getAnimationDuration()
|
||||
{
|
||||
return window.animation.getDuration(true);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
// attributes that are applied to this project.
|
||||
// Project-level suppressions either have no target or are given
|
||||
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
||||
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Potential Code Quality Issues", "RECS0165:Asynchronous methods should return a Task instead of void", Justification = "<Pending>", Scope = "member", Target = "~M:VS4Mac.LottiePlayer.Controls.LottiePlayerNavigationDelegate.DidFinishNavigation(WebKit.WKWebView,WebKit.WKNavigation)")]
|
||||
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Potential Code Quality Issues", "RECS0165:Asynchronous methods should return a Task instead of void", Justification = "<Pending>", Scope = "member", Target = "~M:VS4Mac.LottiePlayer.Views.LottiePlayerDialog.SetController(VS4Mac.LottiePlayer.Controllers.Base.IController)")]
|
||||
|
|
@ -29,9 +29,4 @@
|
|||
<None Remove="Images\pause.png" />
|
||||
<None Remove="Images\play.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\" />
|
||||
<Folder Include="Views\Base\" />
|
||||
<Folder Include="Controllers\Base\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,4 +1,5 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MonoDevelop.Ide;
|
||||
using VS4Mac.LottiePlayer.Controllers;
|
||||
using VS4Mac.LottiePlayer.Controllers.Base;
|
||||
|
@ -18,6 +19,7 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
Controls.LottiePlayer _lottiePlayer;
|
||||
HBox _controlBox;
|
||||
Button _playButton;
|
||||
Slider _timeSlider;
|
||||
HBox _buttonBox;
|
||||
Button _closeButton;
|
||||
|
||||
|
@ -39,17 +41,26 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
};
|
||||
|
||||
_lottiePlayer = new Controls.LottiePlayer();
|
||||
_controlBox = new HBox();
|
||||
|
||||
_controlBox = new HBox
|
||||
{
|
||||
Margin = new WidgetSpacing(0, 6, 0, 6)
|
||||
};
|
||||
|
||||
_playButton = new Button
|
||||
{
|
||||
BackgroundColor = MonoDevelop.Ide.Gui.Styles.BackgroundColor,
|
||||
HorizontalPlacement = WidgetPlacement.Center,
|
||||
Image = ImageService.GetIcon("lottie-pause", Gtk.IconSize.Button),
|
||||
ImagePosition = ContentPosition.Center,
|
||||
Style = ButtonStyle.Borderless
|
||||
};
|
||||
|
||||
_timeSlider = new HSlider
|
||||
{
|
||||
MinimumValue = 0,
|
||||
Visible = false
|
||||
};
|
||||
|
||||
_buttonBox = new HBox();
|
||||
_closeButton = new Button("Close");
|
||||
}
|
||||
|
@ -63,7 +74,8 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
|
||||
var xwtLottiePlayer = Toolkit.CurrentEngine.WrapWidget(_lottiePlayer);
|
||||
|
||||
_controlBox.PackStart(_playButton, true);
|
||||
_controlBox.PackStart(_playButton);
|
||||
_controlBox.PackStart(_timeSlider, true);
|
||||
|
||||
_buttonBox.PackEnd(_closeButton);
|
||||
|
||||
|
@ -79,6 +91,8 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
|
||||
void AttachEvents()
|
||||
{
|
||||
_lottiePlayer.DurationChanged += OnLottiePlayerDurationChanged;
|
||||
_timeSlider.ValueChanged += OnTimeSliderValueChanged;
|
||||
_playButton.Clicked += OnPlayButtonClicked;
|
||||
_closeButton.Clicked += OnCloseButtonClicked;
|
||||
}
|
||||
|
@ -87,22 +101,41 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
{
|
||||
base.Dispose(disposing);
|
||||
|
||||
_lottiePlayer.DurationChanged -= OnLottiePlayerDurationChanged;
|
||||
_timeSlider.ValueChanged -= OnTimeSliderValueChanged;
|
||||
_playButton.Clicked -= OnPlayButtonClicked;
|
||||
_closeButton.Clicked -= OnCloseButtonClicked;
|
||||
}
|
||||
|
||||
public void SetController(IController controller)
|
||||
public async void SetController(IController controller)
|
||||
{
|
||||
_controller = (LottiePlayerController)controller;
|
||||
|
||||
LoadAnimation();
|
||||
await LoadAnimationAsync();
|
||||
}
|
||||
|
||||
void LoadAnimation()
|
||||
async Task LoadAnimationAsync()
|
||||
{
|
||||
var animationText = File.ReadAllText(_controller.ProjectFile.FilePath);
|
||||
|
||||
_lottiePlayer.SetData(animationText);
|
||||
await _lottiePlayer.SetDataAsync(animationText);
|
||||
}
|
||||
|
||||
void UpdateSliderData()
|
||||
{
|
||||
_timeSlider.MaximumValue = _lottiePlayer.Duration;
|
||||
_timeSlider.StepIncrement = 0.1;
|
||||
}
|
||||
|
||||
void OnLottiePlayerDurationChanged(object sender, System.EventArgs e)
|
||||
{
|
||||
UpdateSliderData();
|
||||
}
|
||||
|
||||
void OnTimeSliderValueChanged(object sender, System.EventArgs e)
|
||||
{
|
||||
var timeValue = _timeSlider.Value;
|
||||
_lottiePlayer.GoToAndStop(timeValue);
|
||||
}
|
||||
|
||||
void OnPlayButtonClicked(object sender, System.EventArgs e)
|
||||
|
@ -122,12 +155,14 @@ namespace VS4Mac.LottiePlayer.Views
|
|||
void Pause()
|
||||
{
|
||||
_playButton.Image = ImageService.GetIcon("lottie-pause", Gtk.IconSize.Button);
|
||||
_timeSlider.Visible = false;
|
||||
_lottiePlayer.Play();
|
||||
}
|
||||
|
||||
void Play()
|
||||
{
|
||||
_playButton.Image = ImageService.GetIcon("lottie-play", Gtk.IconSize.Button);
|
||||
_timeSlider.Visible = true;
|
||||
_lottiePlayer.Pause();
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче