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 | { |
| 46 | attr = child->first_attribute("name"); |
| 47 | if (attr) |
| 48 | mAnimation = (AnimationResource*) PageManager::FindResource(attr->value()); |
| 49 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 50 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 51 | // Load the placement |
| 52 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, NULL, NULL, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 54 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 83 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int GUIAnimation::Render(void) |
| 103 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 104 | if(!isConditionTrue()) |
| 105 | return 0; |
| 106 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 107 | if (!mAnimation || !mAnimation->GetResource(mFrame)) return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 109 | gr_blit(mAnimation->GetResource(mFrame), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 110 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int GUIAnimation::Update(void) |
| 114 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 115 | if(!isConditionTrue()) |
| 116 | return 0; |
| 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | if (!mAnimation) return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | // Handle the "end-of-animation" state |
| 121 | if (mLoop == -2) return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | } |
| 142 | |