-
Notifications
You must be signed in to change notification settings - Fork 324
/
FLCPlayer.cs
122 lines (103 loc) · 3.73 KB
/
FLCPlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Lypyl (Lypyl@dfworkshop.net), Gavin Clayton (interkarma@dfworkshop.net)
// Contributors: Numidium
//
// Notes:
//
using UnityEngine;
using System.IO;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Utility;
namespace DaggerfallWorkshop.Game.UserInterface
{
public class FLCPlayer : Panel
{
FlcFile flcFile = new FlcFile();
Texture2D flcTexture = null;
float nextFrameTime;
bool isPlaying = false;
byte tRed = 0;
byte tGreen = 0;
byte tBlue = 0;
public bool Loop { get; set; }
public bool TransparencyEnabled { get; set; }
public FlcFile FLCFile { get { return flcFile; } }
public FLCPlayer()
{
Loop = true;
BackgroundTextureLayout = BackgroundLayout.ScaleToFit;
BackgroundColor = Color.black;
TransparencyEnabled = false;
}
public void Load(string filename)
{
flcFile.Transparency = TransparencyEnabled;
flcFile.TransparentRed = tRed;
flcFile.TransparentGreen = tGreen;
flcFile.TransparentBlue = tBlue;
// Seek from loose files Movies or Arena2 path
string moviePath = Path.Combine(Application.streamingAssetsPath, "Movies");
string path = Path.Combine(moviePath, filename);
if (!File.Exists(path))
path = Path.Combine(DaggerfallUnity.Instance.Arena2Path, filename);
if (!flcFile.Load(path))
return;
flcTexture = TextureReader.CreateFromSolidColor(flcFile.Header.Width, flcFile.Header.Height, (TransparencyEnabled ? Color.clear : Color.black), false, false);
flcTexture.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode;
}
public void Start()
{
if (flcFile != null && flcFile.ReadyToPlay)
{
flcFile.CurrentFrame = 0;
nextFrameTime = Time.realtimeSinceStartup + flcFile.FrameDelay;
BackgroundTexture = flcTexture;
isPlaying = true;
}
}
public void Stop()
{
if (flcFile != null)
{
isPlaying = false;
BackgroundTexture = null;
}
}
public void SetTransparentColor(byte r, byte g, byte b)
{
tRed = r;
tGreen = g;
tBlue = b;
}
public override void Update()
{
base.Update();
// Stop playing if we reach final frame and not looping
if (isPlaying && !Loop && flcFile.CurrentFrame >= flcFile.Header.NumOfFrames)
{
isPlaying = false;
RaiseAnimEnd();
}
if (flcFile == null || !flcTexture || !flcFile.ReadyToPlay || !isPlaying)
return;
if (Time.realtimeSinceStartup < nextFrameTime)
return;
else
nextFrameTime = Time.realtimeSinceStartup + flcFile.FrameDelay;
flcTexture.SetPixels32(flcFile.FrameBuffer);
flcTexture.Apply(false);
flcFile.BufferNextFrame();
}
public delegate void OnAnimEndHandler(FLCPlayer player);
public event OnAnimEndHandler OnAnimEnd;
void RaiseAnimEnd()
{
if (OnAnimEnd != null)
OnAnimEnd(this);
}
}
}