blob: 37f0d08721aab229e358e5d8ddce0f433ada2692 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// animation.cpp - GUIAnimation object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/reboot.h>
9#include <sys/stat.h>
10#include <sys/time.h>
11#include <sys/mman.h>
12#include <sys/types.h>
13#include <sys/ioctl.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
17
18#include <string>
19
20extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000021#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040022}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010023#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
25#include "rapidxml.hpp"
26#include "objects.hpp"
27
28
Vojtech Bocekede51c52014-02-07 23:58:09 +010029GUIAnimation::GUIAnimation(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040030{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020033 mAnimation = NULL;
34 mFrame = 1;
35 mFPS = 1;
36 mLoop = -1;
37 mRender = 1;
38 mUpdateCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Ethan Yonker21ff02a2015-02-18 14:35:00 -060042 mAnimation = LoadAttrAnimation(FindNode(node, "resource"), "name");
Dees_Troy51a0e822012-09-05 15:24:24 -040043
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020044 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060045 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, NULL, NULL, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040046
Ethan Yonker21ff02a2015-02-18 14:35:00 -060047 child = FindNode(node, "speed");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 if (child)
49 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -060050 mFPS = LoadAttrInt(child, "fps", mFPS);
51 mRender = LoadAttrInt(child, "render", mRender);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 }
53 if (mFPS > 30) mFPS = 30;
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Ethan Yonker21ff02a2015-02-18 14:35:00 -060055 child = FindNode(node, "loop");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020056 if (child)
57 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -060058 xml_attribute<>* attr = child->first_attribute("frame");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (attr)
60 mLoop = atoi(attr->value()) - 1;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060061 mFrame = LoadAttrInt(child, "start", mFrame);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 }
Dees_Troy51a0e822012-09-05 15:24:24 -040063
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020064 // Fetch the render sizes
65 if (mAnimation && mAnimation->GetResource())
66 {
thatf6ed8fc2015-02-14 20:23:16 +010067 mRenderW = mAnimation->GetWidth();
68 mRenderH = mAnimation->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040069
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 // Adjust for placement
71 if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT)
72 {
73 if (mPlacement == CENTER)
74 mRenderX -= (mRenderW / 2);
75 else
76 mRenderX -= mRenderW;
77 }
78 if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT)
79 {
80 if (mPlacement == CENTER)
81 mRenderY -= (mRenderH / 2);
82 else
83 mRenderY -= mRenderH;
84 }
85 SetPlacement(TOP_LEFT);
86 }
Dees_Troy51a0e822012-09-05 15:24:24 -040087}
88
89int GUIAnimation::Render(void)
90{
Matt Mowera8a89d12016-12-30 18:10:37 -060091 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +010092 return 0;
93
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
97 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040098}
99
100int GUIAnimation::Update(void)
101{
Matt Mowera8a89d12016-12-30 18:10:37 -0600102 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100103 return 0;
104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 if (!mAnimation) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 // Handle the "end-of-animation" state
108 if (mLoop == -2) return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 // Determine if we need the next frame yet...
111 if (++mUpdateCount > 30 / mFPS)
112 {
113 mUpdateCount = 0;
114 if (++mFrame >= mAnimation->GetResourceCount())
115 {
116 if (mLoop < 0)
117 {
118 mFrame = mAnimation->GetResourceCount() - 1;
119 mLoop = -2;
120 }
121 else
122 mFrame = mLoop;
123 }
124 if (mRender == 2) return 2;
125 return (Render() == 0 ? 1 : -1);
126 }
127 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129