blob: dc1199e410a1327e00e77ec9ea4ef58304086a7c [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001#pragma version(1)
2#pragma stateVertex(PV)
3#pragma stateFragment(PF)
4#pragma stateFragmentStore(PFS)
5
Joe Onorato43e7bcf2009-08-08 18:53:53 -07006#define PI 3.14159f
7
Joe Onorato43e7bcf2009-08-08 18:53:53 -07008// Variables from java ======
9
10// Parameters ======
11#define PARAM_BUBBLE_WIDTH 0
12#define PARAM_BUBBLE_HEIGHT 1
13#define PARAM_BUBBLE_BITMAP_WIDTH 2
14#define PARAM_BUBBLE_BITMAP_HEIGHT 3
Joe Onoratoc567acb2009-08-31 14:34:43 -070015#define PARAM_SCROLL_HANDLE_ID 4
16#define PARAM_SCROLL_HANDLE_TEX_WIDTH 5
17#define PARAM_SCROLL_HANDLE_TEX_HEIGHT 6
Joe Onorato93839052009-08-06 20:34:32 -070018
Joe Onorato1feb3a82009-08-08 22:32:00 -070019// State ======
20#define STATE_ICON_COUNT 0
21#define STATE_SCROLL_X 1
Joe Onoratod769a632009-08-11 17:09:02 -070022#define STATE_FLING_TIME 2
23#define STATE_FLING_VELOCITY_X 3
24#define STATE_ADJUSTED_DECELERATION 4
Joe Onorato9c1289c2009-08-17 11:03:03 -040025
26/* with fling offset applied */
27#define STATE_CURRENT_SCROLL_X 5
28
Joe Onoratoeb2c02e2009-08-12 21:40:52 -070029#define STATE_FLING_DURATION 6
30#define STATE_FLING_END_POS 7
Joe Onorato93839052009-08-06 20:34:32 -070031
Joe Onorato6665c0f2009-09-02 15:27:24 -070032#define STATE_START_SCROLL_X 8
33#define STATE_SELECTED_ICON_INDEX 9
34#define STATE_SELECTED_ICON_TEXTURE 10
Joe Onoratoc567acb2009-08-31 14:34:43 -070035
Joe Onorato006b25f2009-09-03 11:38:43 -070036#define STATE_VISIBLE 11
Joe Onorato85a02a82009-09-08 12:34:22 -070037#define STATE_ZOOM 12
Joe Onorato93839052009-08-06 20:34:32 -070038
Joe Onorato43e7bcf2009-08-08 18:53:53 -070039// Drawing constants, should be parameters ======
Joe Onoratoefabe002009-08-28 09:38:18 -070040#define VIEW_ANGLE 1.28700222f
Joe Onorato43e7bcf2009-08-08 18:53:53 -070041
42int
43count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -070044{
Joe Onorato43e7bcf2009-08-08 18:53:53 -070045 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
46 int pages = iconCount / iconsPerPage;
47 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -070048 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -070049 }
Joe Onoratod769a632009-08-11 17:09:02 -070050 return pages;
51}
52
Joe Onoratod769a632009-08-11 17:09:02 -070053float
54modf(float x, float y)
55{
56 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -070057}
58
Joe Onorato0d1c5632009-08-28 15:57:18 -070059float
60far_size(float sizeAt0)
61{
62 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
63}
64
Joe Onoratoefabe002009-08-28 09:38:18 -070065void
66draw_page(int icon, int lastIcon, float centerAngle)
67{
68 int row;
69 int col;
70
Joe Onorato85a02a82009-09-08 12:34:22 -070071 float scale = 1.0f - loadI32(ALLOC_STATE, STATE_ZOOM)/100000.0f;
72
Joe Onoratoefabe002009-08-28 09:38:18 -070073 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
74 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
75
76 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -070077 float columnGutterAngle = iconWidthAngle * 0.70f;
Joe Onoratoefabe002009-08-28 09:38:18 -070078
Joe Onorato6665c0f2009-09-02 15:27:24 -070079 float farIconSize = FAR_ICON_SIZE;
Joe Onorato0d1c5632009-08-28 15:57:18 -070080 float iconGutterHeight = farIconSize * 1.1f;
81
Joe Onorato6665c0f2009-09-02 15:27:24 -070082 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
83
Joe Onorato0d1c5632009-08-28 15:57:18 -070084 float labelWidthPx = loadI32(ALLOC_PARAMS, PARAM_BUBBLE_WIDTH);
85 float labelHeightPx = loadI32(ALLOC_PARAMS, PARAM_BUBBLE_HEIGHT);
86
87 float normalizedLabelWidth = 2 * labelWidthPx / (float)SCREEN_WIDTH_PX;
88 float farLabelWidth = far_size(normalizedLabelWidth);
89 float farLabelHeight = far_size(labelHeightPx * (normalizedLabelWidth / labelWidthPx));
90 float labelTextureWidth = labelWidthPx / loadI32(ALLOC_PARAMS, PARAM_BUBBLE_BITMAP_WIDTH);
91 float labelTextureHeight = labelHeightPx / loadI32(ALLOC_PARAMS, PARAM_BUBBLE_BITMAP_HEIGHT);
92
Joe Onorato6665c0f2009-09-02 15:27:24 -070093 int selectedIconIndex = loadI32(ALLOC_STATE, STATE_SELECTED_ICON_INDEX);
Joe Onoratoefabe002009-08-28 09:38:18 -070094
95 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
96 float angle = centerAngle;
97 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
98
Joe Onorato6665c0f2009-09-02 15:27:24 -070099 float iconTop = (farIconSize + iconGutterHeight) * (2.0f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700100 - row * (farIconSize + iconGutterHeight);
Joe Onoratod40eec32009-09-08 12:34:22 -0700101 iconTop -= 6 * scale; // make the zoom point below center
Joe Onoratoefabe002009-08-28 09:38:18 -0700102 float iconBottom = iconTop - farIconSize;
103
Joe Onorato0d1c5632009-08-28 15:57:18 -0700104 float labelTop = iconBottom - (.1 * farLabelHeight);
105 float labelBottom = labelTop - farLabelHeight;
106
Joe Onorato6665c0f2009-09-02 15:27:24 -0700107 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
108 float iconTextureBottom = iconTextureTop - farIconTextureSize;
109
Joe Onoratoefabe002009-08-28 09:38:18 -0700110 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
111 // icon
112 float sine = sinf(angle);
113 float cosine = cosf(angle);
114
Joe Onorato0d1c5632009-08-28 15:57:18 -0700115 float centerX = sine * RADIUS;
116 float centerZ = cosine * RADIUS;
Joe Onoratod40eec32009-09-08 12:34:22 -0700117 centerZ -= ((RADIUS+2+1)*scale); // 2 is camera loc, 1 put it slightly behind that.
Joe Onorato0d1c5632009-08-28 15:57:18 -0700118
Joe Onorato6665c0f2009-09-02 15:27:24 -0700119 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
120 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
121 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
122 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
123
124 if (selectedIconIndex == icon) {
125 bindTexture(NAMED_PF, 0, loadI32(ALLOC_STATE, STATE_SELECTED_ICON_TEXTURE));
126 drawQuadTexCoords(
127 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
128 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
129 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
130 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
131 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700132
133 bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon));
134 drawQuadTexCoords(
Joe Onorato6665c0f2009-09-02 15:27:24 -0700135 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
136 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
137 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
138 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onoratoefabe002009-08-28 09:38:18 -0700139
140 // label
Joe Onorato85a02a82009-09-08 12:34:22 -0700141 if (scale <= 1.04f) {
142 float labelLeftX = centerX - farLabelWidth * 0.5f;
143 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700144
Joe Onorato85a02a82009-09-08 12:34:22 -0700145 bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon));
146 drawQuadTexCoords(
147 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
148 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
149 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
150 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
151 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700152
Joe Onoratoefabe002009-08-28 09:38:18 -0700153 angle += columnGutterAngle + iconWidthAngle;
154 icon++;
155 }
156 }
157}
158
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700159int
Joe Onoratod769a632009-08-11 17:09:02 -0700160main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700161{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700162 // Clear to transparent
Joe Onorato93839052009-08-06 20:34:32 -0700163 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Joe Onorato93839052009-08-06 20:34:32 -0700164
Joe Onorato006b25f2009-09-03 11:38:43 -0700165 // If we're not supposed to be showing, don't do anything.
166 if (!loadI32(ALLOC_STATE, STATE_VISIBLE)) {
167 return 0;
168 }
169
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700170 // icons & labels
Joe Onorato1feb3a82009-08-08 22:32:00 -0700171 int iconCount = loadI32(ALLOC_STATE, STATE_ICON_COUNT);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700172 int pageCount = count_pages(iconCount);
173
Joe Onoratod769a632009-08-11 17:09:02 -0700174 float scrollXPx = loadI32(ALLOC_STATE, STATE_SCROLL_X);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700175 float maxScrollXPx = -(pageCount-1) * SCREEN_WIDTH_PX;
Joe Onoratod769a632009-08-11 17:09:02 -0700176 int done = 0;
177
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700178 // Clamp -- because java doesn't know how big the icons are
179 if (scrollXPx > 0) {
180 scrollXPx = 0;
181 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700182 if (scrollXPx < maxScrollXPx) {
183 scrollXPx = maxScrollXPx;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700184 }
185
Joe Onoratod769a632009-08-11 17:09:02 -0700186 // If we've been given a velocity, start a fling
187 float flingVelocityPxMs = loadI32(ALLOC_STATE, STATE_FLING_VELOCITY_X);
188 if (flingVelocityPxMs != 0) {
189 // how many screens will this velocity do? TODO: use long
190 // G * ppi * friction // why G? // friction = 0.015
191 float deceleration = loadF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION);
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700192 float flingDurationMs;
Joe Onoratod769a632009-08-11 17:09:02 -0700193 if (deceleration == 0) {
194 // On the first frame, calculate which animation we're going to do. If it's
195 // going to end up less than halfway into a page, we'll bounce back the previous
196 // page. Otherwise, we'll adjust the deceleration so it just makes it to the
197 // page boundary.
198 if (flingVelocityPxMs > 0) {
199 deceleration = -1000;
200 } else {
201 deceleration = 1000;
202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 // minimum velocity
204 if (flingVelocityPxMs < 0) {
205 if (flingVelocityPxMs > -500) {
206 flingVelocityPxMs = -500;
207 }
208 } else {
209 if (flingVelocityPxMs < 500) {
210 flingVelocityPxMs = 500;
211 }
212 }
213
Joe Onoratod769a632009-08-11 17:09:02 -0700214 // v' = v + at --> t = -v / a
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700215 // x' = x + vt + .5 a t^2
216 flingDurationMs = - flingVelocityPxMs / deceleration;
217 float endPos = scrollXPx + (flingVelocityPxMs*flingDurationMs)
218 + ((deceleration*flingDurationMs*flingDurationMs)/2);
Joe Onoratod769a632009-08-11 17:09:02 -0700219
220 if (endPos > 0) {
221 endPos = 0;
222 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700223 if (endPos < maxScrollXPx) {
224 endPos = maxScrollXPx;
Joe Onoratod769a632009-08-11 17:09:02 -0700225 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700226 float scrollOnPage = modf(endPos, SCREEN_WIDTH_PX);
227 int endPage = -endPos/SCREEN_WIDTH_PX;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700229 if (flingVelocityPxMs < 0) {
Joe Onoratoefabe002009-08-28 09:38:18 -0700230 if (scrollOnPage < (SCREEN_WIDTH_PX/2)) {
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700231 // adjust the deceleration so we align on the page boundary
232 // a = 2(x-x0-v0t)/t^2
Joe Onoratoefabe002009-08-28 09:38:18 -0700233 endPos = -(endPage+1) * SCREEN_WIDTH_PX;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 debugI32("endPos case 1", endPos);
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700235 } else {
236 // TODO: bounce
Joe Onoratoefabe002009-08-28 09:38:18 -0700237 endPos = -(endPage+1) * SCREEN_WIDTH_PX;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238 debugI32("endPos case 2", endPos);
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700239 }
240 } else {
Joe Onoratoefabe002009-08-28 09:38:18 -0700241 if (scrollOnPage >= (SCREEN_WIDTH_PX/2)) {
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700242 // adjust the deceleration so we align on the page boundary
Joe Onoratoefabe002009-08-28 09:38:18 -0700243 endPos = -endPage * SCREEN_WIDTH_PX;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 debugI32("endPos case 3", endPos);
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700245 } else {
246 // TODO: bounce
Joe Onoratoefabe002009-08-28 09:38:18 -0700247 endPos = -endPage * SCREEN_WIDTH_PX;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400248 debugI32("endPos case 4", endPos);
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700249 }
250 }
251 // v = v0 + at --> (v - v0) / t
252 deceleration = 2*(endPos-scrollXPx-(flingVelocityPxMs*flingDurationMs))
253 / (flingDurationMs*flingDurationMs);
254 endPos = scrollXPx + (flingVelocityPxMs*flingDurationMs)
255 + ((deceleration*flingDurationMs*flingDurationMs)/2);
256
257 storeF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION, deceleration);
258 storeF(ALLOC_STATE, STATE_FLING_DURATION, flingDurationMs);
259 storeF(ALLOC_STATE, STATE_FLING_END_POS, endPos);
260 } else {
261 flingDurationMs = loadF(ALLOC_STATE, STATE_FLING_DURATION);
Joe Onoratod769a632009-08-11 17:09:02 -0700262 }
263
264 // adjust the deceleration so we always hit a page boundary
265
266 int flingTime = loadI32(ALLOC_STATE, STATE_FLING_TIME);
267 int now = uptimeMillis();
268 float elapsedTime = (now - flingTime) / 1000.0f;
269 int animEndTime = -flingVelocityPxMs / deceleration;
270
Joe Onoratod769a632009-08-11 17:09:02 -0700271 int flingOffsetPx = (flingVelocityPxMs * elapsedTime)
272 + (deceleration * elapsedTime * elapsedTime / 2.0f);
273 scrollXPx += flingOffsetPx;
274
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700275 if (elapsedTime > flingDurationMs) {
276 scrollXPx = loadF(ALLOC_STATE, STATE_FLING_END_POS);
277 done = 1;
278 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700279 } else {
280 done = 1;
Joe Onoratod769a632009-08-11 17:09:02 -0700281 }
282
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700283 // Clamp
Joe Onoratod769a632009-08-11 17:09:02 -0700284 if (scrollXPx > 0) {
285 scrollXPx = 0;
286 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700287 if (scrollXPx < maxScrollXPx) {
288 scrollXPx = maxScrollXPx;
Joe Onoratod769a632009-08-11 17:09:02 -0700289 }
290
291 storeI32(ALLOC_STATE, STATE_CURRENT_SCROLL_X, scrollXPx);
292 if (done) {
293 storeI32(ALLOC_STATE, STATE_SCROLL_X, scrollXPx);
294 storeI32(ALLOC_STATE, STATE_FLING_VELOCITY_X, 0);
295 storeF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION, 0);
296 }
297
Joe Onoratoc567acb2009-08-31 14:34:43 -0700298 // Draw the icons ========================================
299 bindProgramVertex(NAMED_PV);
Joe Onoratoefabe002009-08-28 09:38:18 -0700300 bindProgramFragment(NAMED_PF);
301 bindProgramFragmentStore(NAMED_PFS);
302
303 // Bug makes 1.0f alpha fail.
304 color(1.0f, 1.0f, 1.0f, 0.99f);
305
306 int lastIcon = iconCount-1;
307
308 float currentPage = -scrollXPx / (float)SCREEN_WIDTH_PX;
309 int page = currentPage;
310 float currentPagePosition = currentPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700311
Joe Onoratod769a632009-08-11 17:09:02 -0700312 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700313 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700314
Joe Onoratoefabe002009-08-28 09:38:18 -0700315 draw_page(icon, lastIcon, -VIEW_ANGLE*currentPagePosition);
316 draw_page(icon+iconsPerPage, lastIcon, (-VIEW_ANGLE*currentPagePosition)+VIEW_ANGLE);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700317
Joe Onorato6665c0f2009-09-02 15:27:24 -0700318 // Draw the border lines for debugging ========================================
Joe Onoratoc567acb2009-08-31 14:34:43 -0700319 /*
320 bindProgramVertex(NAMED_PVOrtho);
321 bindProgramFragment(NAMED_PFText);
322 bindProgramFragmentStore(NAMED_PFSText);
323
Joe Onorato6665c0f2009-09-02 15:27:24 -0700324 color(1.0f, 1.0f, 0.0f, 0.99f);
325 int i;
326 for (i=0; i<ROWS_PER_PAGE+1; i++) {
327 int y = loadI32(ALLOC_Y_BORDERS, i);
328 drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f);
329 }
330 for (i=0; i<COLUMNS_PER_PAGE+1; i++) {
331 int x = loadI32(ALLOC_X_BORDERS, i);
332 drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f);
333 }
334 */
335
336 // Draw the scroll handle ========================================
337 /*
Joe Onoratoc567acb2009-08-31 14:34:43 -0700338 bindTexture(NAMED_PFText, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
339 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
340 float handleTop = 680;
341 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
342 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
343 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
344 */
Joe Onorato93839052009-08-06 20:34:32 -0700345
Joe Onoratod769a632009-08-11 17:09:02 -0700346 return !done;
Joe Onorato93839052009-08-06 20:34:32 -0700347}
348