blob: 72e2fe905f1bc7dfb39e45bd7d9bd07ea6116347 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// button.cpp - GUIButton 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" {
21#include "../common.h"
22#include "../minuitwrp/minui.h"
23#include "../recovery_ui.h"
24}
25
26#include "rapidxml.hpp"
27#include "objects.hpp"
28
29GUIButton::GUIButton(xml_node<>* node)
30 : Conditional(node)
31{
32 xml_attribute<>* attr;
33 xml_node<>* child;
34
35 mButtonImg = NULL;
36 mButtonIcon = NULL;
37 mButtonLabel = NULL;
38 mAction = NULL;
39 mRendered = false;
40
41 if (!node) return;
42
43 // Three of the four can be loaded directly from the node
44 mButtonImg = new GUIImage(node);
45 mButtonLabel = new GUIText(node);
46 mAction = new GUIAction(node);
47
48 if (mButtonImg->Render() < 0)
49 {
50 LOGE("Unable to locate button image\n");
51 delete mButtonImg;
52 mButtonImg = NULL;
53 }
54 if (mButtonLabel->Render() < 0)
55 {
56 delete mButtonLabel;
57 mButtonLabel = NULL;
58 }
59
60 // The icon is a special case
61 child = node->first_node("icon");
62 if (child)
63 {
64 attr = child->first_attribute("resource");
65 if (attr)
66 mButtonIcon = PageManager::FindResource(attr->value());
67 }
68
69 int x, y, w, h;
70 if (mButtonImg) mButtonImg->GetRenderPos(x, y, w, h);
71 SetRenderPos(x, y, w, h);
72 return;
73}
74
75GUIButton::~GUIButton()
76{
77 if (mButtonImg) delete mButtonImg;
78 if (mButtonLabel) delete mButtonLabel;
79 if (mAction) delete mAction;
80 if (mButtonIcon) delete mButtonIcon;
81}
82
83int GUIButton::Render(void)
84{
85 if (!isConditionTrue())
86 {
87 mRendered = false;
88 return 0;
89 }
90
91 int ret = 0;
92
93 if (mButtonImg) ret = mButtonImg->Render();
94 if (ret < 0) return ret;
95 if (mButtonIcon && mButtonIcon->GetResource())
96 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
97 if (mButtonLabel) ret = mButtonLabel->Render();
98 if (ret < 0) return ret;
99 mRendered = true;
100 return ret;
101}
102
103int GUIButton::Update(void)
104{
105 if (!isConditionTrue()) return (mRendered ? 2 : 0);
106 if (!mRendered) return 2;
107
108 int ret = 0, ret2 = 0;
109
110 if (mButtonImg) ret = mButtonImg->Update();
111 if (ret < 0) return ret;
112
113 if (ret == 0)
114 {
115 if (mButtonLabel) ret2 = mButtonLabel->Update();
116 if (ret2 < 0) return ret2;
117 if (ret2 > ret) ret = ret2;
118 }
119 else if (ret == 1)
120 {
121 // The button re-rendered, so everyone else is a render
122 if (mButtonIcon && mButtonIcon->GetResource())
123 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
124 if (mButtonLabel) ret = mButtonLabel->Render();
125 if (ret < 0) return ret;
126 ret = 1;
127 }
128 else
129 {
130 // Aparently, the button needs a background update
131 ret = 2;
132 }
133 return ret;
134}
135
136int GUIButton::SetRenderPos(int x, int y, int w, int h)
137{
138 mRenderX = x;
139 mRenderY = y;
140 if (w || h)
141 {
142 mRenderW = w;
143 mRenderH = h;
144 }
145
146 mIconW = 0; mIconH = 0;
147 if (mButtonIcon && mButtonIcon->GetResource())
148 {
149 mIconW = gr_get_width(mButtonIcon->GetResource());
150 mIconH = gr_get_height(mButtonIcon->GetResource());
151 }
152
153 mTextH = 0;
154 mTextW = 0;
155 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
156 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
157 if (mTextW)
158 {
159 // As a special case, we'll allow large text which automatically moves it to the right.
160 if (mTextW > mRenderW)
161 {
162 mTextX = mRenderW + mRenderX + 5;
163 mRenderW += mTextW + 5;
164 }
165 else
166 {
167 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
168 }
169 }
170
171 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
172 {
173 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
174 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
175 }
176 else
177 {
178 int divisor = mRenderH - (mIconH + mTextH);
179 mIconY = mRenderY + (divisor / 3);
180 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
181 }
182
183 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
184 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
185 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
186 return 0;
187}
188
189int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
190{
191 if (!isConditionTrue()) return -1;
192 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
193}
194