blob: 8c922786de678d066953a175306ac0cf5e9e536e [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
29GUIAnimation::GUIAnimation(xml_node<>* node)
30{
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 Bocekfafb0c52013-07-25 22:53:02 +0200104 if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
107 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400108}
109
110int GUIAnimation::Update(void)
111{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 if (!mAnimation) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 // Handle the "end-of-animation" state
115 if (mLoop == -2) return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 // Determine if we need the next frame yet...
118 if (++mUpdateCount > 30 / mFPS)
119 {
120 mUpdateCount = 0;
121 if (++mFrame >= mAnimation->GetResourceCount())
122 {
123 if (mLoop < 0)
124 {
125 mFrame = mAnimation->GetResourceCount() - 1;
126 mLoop = -2;
127 }
128 else
129 mFrame = mLoop;
130 }
131 if (mRender == 2) return 2;
132 return (Render() == 0 ? 1 : -1);
133 }
134 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135}
136