blob: 771e1c116c2ed96c597c1c293c21e861086cad49 [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#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040023}
24
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;
32 xml_attribute<>* attr;
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 mAnimation = NULL;
35 mFrame = 1;
36 mFPS = 1;
37 mLoop = -1;
38 mRender = 1;
39 mUpdateCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020041 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040042
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020043 child = node->first_node("resource");
44 if (child)
45 {
46 attr = child->first_attribute("name");
47 if (attr)
48 mAnimation = (AnimationResource*) PageManager::FindResource(attr->value());
49 }
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 // Load the placement
52 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, NULL, NULL, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040053
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 child = node->first_node("speed");
55 if (child)
56 {
57 attr = child->first_attribute("fps");
58 if (attr)
59 mFPS = atoi(attr->value());
60 attr = child->first_attribute("render");
61 if (attr)
62 mRender = atoi(attr->value());
63 }
64 if (mFPS > 30) mFPS = 30;
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 child = node->first_node("loop");
67 if (child)
68 {
69 attr = child->first_attribute("frame");
70 if (attr)
71 mLoop = atoi(attr->value()) - 1;
72 attr = child->first_attribute("start");
73 if (attr)
74 mFrame = atoi(attr->value());
75 }
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 // Fetch the render sizes
78 if (mAnimation && mAnimation->GetResource())
79 {
80 mRenderW = gr_get_width(mAnimation->GetResource());
81 mRenderH = gr_get_height(mAnimation->GetResource());
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 // Adjust for placement
84 if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT)
85 {
86 if (mPlacement == CENTER)
87 mRenderX -= (mRenderW / 2);
88 else
89 mRenderX -= mRenderW;
90 }
91 if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT)
92 {
93 if (mPlacement == CENTER)
94 mRenderY -= (mRenderH / 2);
95 else
96 mRenderY -= mRenderH;
97 }
98 SetPlacement(TOP_LEFT);
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100}
101
102int GUIAnimation::Render(void)
103{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100104 if(!isConditionTrue())
105 return 0;
106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
110 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113int GUIAnimation::Update(void)
114{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100115 if(!isConditionTrue())
116 return 0;
117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 if (!mAnimation) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 // Handle the "end-of-animation" state
121 if (mLoop == -2) return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 // Determine if we need the next frame yet...
124 if (++mUpdateCount > 30 / mFPS)
125 {
126 mUpdateCount = 0;
127 if (++mFrame >= mAnimation->GetResourceCount())
128 {
129 if (mLoop < 0)
130 {
131 mFrame = mAnimation->GetResourceCount() - 1;
132 mLoop = -2;
133 }
134 else
135 mFrame = mLoop;
136 }
137 if (mRender == 2) return 2;
138 return (Render() == 0 ? 1 : -1);
139 }
140 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141}
142