Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ImageFrameAnimationComponent.c
Go to the documentation of this file.
1 class SCR_ImageFrameAnimationComponent : ScriptedWidgetComponent
8 {
9  // Attributes
10 
11  [Attribute("30", UIWidgets.EditBox, "Animation frames per second")]
12  protected float m_fFramesPerSecond;
13 
14  [Attribute("1", UIWidgets.EditBox, "Number of frames")]
15  protected int m_iNumFrames;
16 
17  [Attribute("0", UIWidgets.EditBox, "Start frame of an animation")]
18  protected int m_iStartFrameId;
19 
20  [Attribute("Frame_%1", UIWidgets.EditBox, "Format for image name withim imageset")]
21  protected string m_sImageNameFormat;
22 
23  [Attribute("-1", UIWidgets.EditBox, "Length of integer conversion of frame Id to string. See int.ToString().")]
24  protected int m_iIntToStringLen;
25 
26  [Attribute("", UIWidgets.ResourceNamePicker, "Image Set with frames", params: "imageset")]
27  protected ResourceName m_sImageSet;
28 
29  // Internal
30 
31  protected float m_fCounterMs;
32  protected float m_fMsPerFrame; // Milliseconds per frame
33  protected int m_iFrameId;
34 
35  protected ImageWidget m_wRoot;
36 
37  //------------------------------------------------------------------------------------------------
39 
40  // You can Start/Stop animation, but most likely it's not needed,
41  // Because it will terminate itself when widget is hidden,
42  // And it will get reactivated when widget is shown again.
43 
44  //------------------------------------------------------------------------------------------------
46  void Start()
47  {
48  if (!m_wRoot)
49  return;
50 
51  m_iFrameId = m_iStartFrameId;
52  ShowFrame(m_iStartFrameId);
53 
54  ScriptCallQueue queue = GetGame().GetCallqueue();
55  queue.Remove(OnEachFrame);
56  queue.CallLater(OnEachFrame, 0, true);
57  }
58 
59  //------------------------------------------------------------------------------------------------
61  void Stop()
62  {
63  GetGame().GetCallqueue().Remove(OnEachFrame);
64  }
65 
66  //------------------------------------------------------------------------------------------------
67  override protected void HandlerAttached(Widget w)
68  {
69  m_wRoot = ImageWidget.Cast(w);
70 
71  if (!m_wRoot)
72  {
73  Print("SCR_ImageFrameAnimationComponent must be attached to image widget!", LogLevel.ERROR);
74  return;
75  }
76 
77  // Ensure safe value for input data
78  if (m_fFramesPerSecond <= 0.0)
79  {
80  Print("SCR_ImageFrameAnimationComponent: wrong m_fFramesPerSecond value", LogLevel.ERROR);
81  m_fFramesPerSecond = 1;
82  }
83  if (m_iNumFrames <= 0)
84  {
85  Print("SCR_ImageFrameAnimationComponent: wrong m_iNumFrames value", LogLevel.ERROR);
86  m_iNumFrames = 1;
87  }
88  if (m_iStartFrameId < 0)
89  {
90  Print("SCR_ImageFrameAnimationComponent: wrong m_iStartFrameId value", LogLevel.ERROR);
91  m_iStartFrameId = 0;
92  }
93 
94  // Calculate run time constants
95  m_fMsPerFrame = Math.Round(1000.0 / m_fFramesPerSecond);
96  m_fMsPerFrame = Math.Max(1.0, m_fMsPerFrame);
97 
98  if (w.IsVisible())
99  Start();
100  }
101 
102  //------------------------------------------------------------------------------------------------
103  override protected void HandlerDeattached(Widget w)
104  {
105  ArmaReforgerScripted game = GetGame();
106  if (game && game.GetCallqueue())
107  Stop();
108  }
109 
110  //------------------------------------------------------------------------------------------------
111  protected void OnEachFrame()
112  {
113  if (!m_wRoot.IsVisibleInHierarchy())
114  {
115  Stop();
116  return;
117  }
118 
119  // Increase counter until we have accumulated m_fMsPerFrame time
120  // Then skip some frames, at least one
121 
122  m_fCounterMs += Math.Min(ftime, 200.0); // Limit ftime within reasonable value
123 
124  float nFramesToSkip = Math.Ceil(m_fCounterMs / m_fMsPerFrame);
125 
126  if (nFramesToSkip >= 1.0)
127  {
128  m_fCounterMs -= nFramesToSkip * m_fMsPerFrame;
129  m_iFrameId += nFramesToSkip;
130  m_iFrameId = m_iFrameId % m_iNumFrames;
131 
132  ShowFrame(m_iFrameId);
133  }
134  }
135 
136  //------------------------------------------------------------------------------------------------
137  protected void ShowFrame(int id)
138  {
139  string imageName = string.Format(m_sImageNameFormat, id.ToString(m_iIntToStringLen));
140  m_wRoot.LoadImageFromSet(0, m_sImageSet, imageName);
141  }
142 
143  //------------------------------------------------------------------------------------------------
144  override bool OnUpdate(Widget w)
145  {
146  if (w != m_wRoot || !m_wRoot)
147  return true;
148 
149  bool nowVisible = m_wRoot.IsVisibleInHierarchy();
150 
151  if (nowVisible)
152  Start();
153  else
154  Stop();
155 
156  return true;
157  }
158 }
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_ImageFrameAnimationComponent
Definition: SCR_ImageFrameAnimationComponent.c:7
params
Configs ServerBrowser KickDialogs params
Definition: SCR_NotificationSenderComponent.c:24