Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 29 | GUIAnimation::GUIAnimation(xml_node<>* node) : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 31 | xml_node<>* child; |
| 32 | xml_attribute<>* attr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 34 | mAnimation = NULL; |
| 35 | mFrame = 1; |
| 36 | mFPS = 1; |
| 37 | mLoop = -1; |
| 38 | mRender = 1; |
| 39 | mUpdateCount = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!node) return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 43 | child = node->first_node("resource"); |
| 44 | if (child) |
| 45 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame^] | 46 | mAnimation = LoadAttrAnimation(child, "name"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 47 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 49 | // Load the placement |
| 50 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, NULL, NULL, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | child = node->first_node("speed"); |
| 53 | if (child) |
| 54 | { |
| 55 | attr = child->first_attribute("fps"); |
| 56 | if (attr) |
| 57 | mFPS = atoi(attr->value()); |
| 58 | attr = child->first_attribute("render"); |
| 59 | if (attr) |
| 60 | mRender = atoi(attr->value()); |
| 61 | } |
| 62 | if (mFPS > 30) mFPS = 30; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 64 | child = node->first_node("loop"); |
| 65 | if (child) |
| 66 | { |
| 67 | attr = child->first_attribute("frame"); |
| 68 | if (attr) |
| 69 | mLoop = atoi(attr->value()) - 1; |
| 70 | attr = child->first_attribute("start"); |
| 71 | if (attr) |
| 72 | mFrame = atoi(attr->value()); |
| 73 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | // Fetch the render sizes |
| 76 | if (mAnimation && mAnimation->GetResource()) |
| 77 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame^] | 78 | mRenderW = mAnimation->GetWidth(); |
| 79 | mRenderH = mAnimation->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 81 | // Adjust for placement |
| 82 | if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT) |
| 83 | { |
| 84 | if (mPlacement == CENTER) |
| 85 | mRenderX -= (mRenderW / 2); |
| 86 | else |
| 87 | mRenderX -= mRenderW; |
| 88 | } |
| 89 | if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT) |
| 90 | { |
| 91 | if (mPlacement == CENTER) |
| 92 | mRenderY -= (mRenderH / 2); |
| 93 | else |
| 94 | mRenderY -= mRenderH; |
| 95 | } |
| 96 | SetPlacement(TOP_LEFT); |
| 97 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int GUIAnimation::Render(void) |
| 101 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 102 | if(!isConditionTrue()) |
| 103 | return 0; |
| 104 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 107 | gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 108 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int GUIAnimation::Update(void) |
| 112 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 113 | if(!isConditionTrue()) |
| 114 | return 0; |
| 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | if (!mAnimation) return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | // Handle the "end-of-animation" state |
| 119 | if (mLoop == -2) return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 121 | // Determine if we need the next frame yet... |
| 122 | if (++mUpdateCount > 30 / mFPS) |
| 123 | { |
| 124 | mUpdateCount = 0; |
| 125 | if (++mFrame >= mAnimation->GetResourceCount()) |
| 126 | { |
| 127 | if (mLoop < 0) |
| 128 | { |
| 129 | mFrame = mAnimation->GetResourceCount() - 1; |
| 130 | mLoop = -2; |
| 131 | } |
| 132 | else |
| 133 | mFrame = mLoop; |
| 134 | } |
| 135 | if (mRender == 2) return 2; |
| 136 | return (Render() == 0 ? 1 : -1); |
| 137 | } |
| 138 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 139 | } |
| 140 | |