blob: cff4620c019188c856c350b9bd4f27edb0e49585 [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
Jason Sams86c87ed2009-09-18 13:55:55 -07008
9// Attraction to center values from page edge to page center.
10float g_AttractionTable[9];
11float g_FrictionTable[9];
12float g_PhysicsTableSize;
13
14float g_PosPage;
15float g_PosVelocity;
16float g_LastPositionX;
17int g_LastTouchDown;
18float g_DT;
19int g_LastTime;
Jason Sams86c87ed2009-09-18 13:55:55 -070020int g_PageCount;
Jason Samsfd22dac2009-09-20 17:24:16 -070021float g_Zoom;
22float g_ZoomTarget;
Joe Onorato93839052009-08-06 20:34:32 -070023
Joe Onorato43e7bcf2009-08-08 18:53:53 -070024// Drawing constants, should be parameters ======
Joe Onoratoefabe002009-08-28 09:38:18 -070025#define VIEW_ANGLE 1.28700222f
Joe Onorato43e7bcf2009-08-08 18:53:53 -070026
Joe Onorato26646342009-09-23 15:15:48 -070027int g_oneLastFrame = 1;
28int one_last_frame(int more) {
29 if (more) {
30 g_oneLastFrame = 1;
31 return 1;
32 } else {
33 if (g_oneLastFrame) {
34 g_oneLastFrame = 0;
35 return 1;
36 } else {
37 g_oneLastFrame = 1;
38 return 0;
39 }
40 }
41}
42
Jason Sams78aebd82009-09-15 13:06:59 -070043void init() {
Jason Sams86c87ed2009-09-18 13:55:55 -070044 g_AttractionTable[0] = 4.5f;
45 g_AttractionTable[1] = 4.5f;
46 g_AttractionTable[2] = 5.0f;
47 g_AttractionTable[3] = 4.0f;
48 g_AttractionTable[4] = -4.0f;
49 g_AttractionTable[5] = -5.0f;
50 g_AttractionTable[6] = -4.5f;
51 g_AttractionTable[7] = -4.5f;
52 g_AttractionTable[8] = -4.5f; // dup 7 to avoid a clamp later
53 g_FrictionTable[0] = 3.5f;
54 g_FrictionTable[1] = 3.6f;
55 g_FrictionTable[2] = 3.7f;
56 g_FrictionTable[3] = 3.8f;
57 g_FrictionTable[4] = 3.8f;
58 g_FrictionTable[5] = 3.7f;
59 g_FrictionTable[6] = 3.6f;
60 g_FrictionTable[7] = 3.5f;
61 g_FrictionTable[8] = 3.5f; // dup 7 to avoid a clamp later
62 g_PhysicsTableSize = 7;
63
64 g_PosVelocity = 0;
65 g_PosPage = 0;
66 g_LastTouchDown = 0;
67 g_LastPositionX = 0;
Jason Samsfd22dac2009-09-20 17:24:16 -070068 g_Zoom = 0;
69 g_ZoomTarget = 0;
Jason Sams86c87ed2009-09-18 13:55:55 -070070}
71
72void move() {
73 if (g_LastTouchDown) {
74 float dx = -(state->newPositionX - g_LastPositionX);
75 g_PosVelocity = 0;
76 g_PosPage += dx;
Jason Samsfd22dac2009-09-20 17:24:16 -070077
78 float pmin = -0.25f;
79 float pmax = (g_PageCount - 1) + 0.25f;
80 g_PosPage = clampf(g_PosPage, pmin, pmax);
Jason Sams86c87ed2009-09-18 13:55:55 -070081 }
82 g_LastTouchDown = state->newTouchDown;
83 g_LastPositionX = state->newPositionX;
Jason Sams86c87ed2009-09-18 13:55:55 -070084}
85
86void fling() {
87 g_LastTouchDown = 0;
88 g_PosVelocity = -state->flingVelocityX;
Jason Samsfd22dac2009-09-20 17:24:16 -070089 float av = fabsf(g_PosVelocity);
90 float minVel = 3f;
91
92 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
93
94 if (av < minVel && av > 0.2f) {
95 if (g_PosVelocity > 0) {
96 g_PosVelocity = minVel;
97 } else {
98 g_PosVelocity = -minVel;
99 }
100 }
101
Jason Sams86c87ed2009-09-18 13:55:55 -0700102 if (g_PosPage <= 0) {
103 g_PosVelocity = maxf(0, g_PosVelocity);
104 }
105 if (g_PosPage > (g_PageCount - 1)) {
Jason Samsfd22dac2009-09-20 17:24:16 -0700106 g_PosVelocity = minf(0, g_PosVelocity);
Jason Sams86c87ed2009-09-18 13:55:55 -0700107 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700108 //g_Zoom += (maxf(fabsf(g_PosVelocity), 3) - 3) / 2.f;
Jason Sams78aebd82009-09-15 13:06:59 -0700109}
110
Jason Samsfd22dac2009-09-20 17:24:16 -0700111
112void setZoomTarget() {
113 g_ZoomTarget = state->zoom;
114 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400115}
116
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700117int
118count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700119{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700120 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
121 int pages = iconCount / iconsPerPage;
122 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700123 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700124 }
Joe Onoratod769a632009-08-11 17:09:02 -0700125 return pages;
126}
127
Joe Onoratod769a632009-08-11 17:09:02 -0700128float
129modf(float x, float y)
130{
131 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700132}
133
Jason Sams86c87ed2009-09-18 13:55:55 -0700134void updatePos() {
135 if (g_LastTouchDown) {
136 return;
137 }
138
139 //debugF("g_PosPage", g_PosPage);
140 //debugF(" g_PosVelocity", g_PosVelocity);
141
142 float tablePosNorm = fracf(g_PosPage + 0.5f);
143 float tablePosF = tablePosNorm * g_PhysicsTableSize;
144 int tablePosI = tablePosF;
145 float tablePosFrac = tablePosF - tablePosI;
146 //debugF("tablePosNorm", tablePosNorm);
147 //debugF("tablePosF", tablePosF);
148 //debugF("tablePosI", tablePosI);
149 //debugF("tablePosFrac", tablePosFrac);
150
151 float accel = lerpf(g_AttractionTable[tablePosI],
152 g_AttractionTable[tablePosI + 1],
153 tablePosFrac) * g_DT;
154 float friction = lerpf(g_FrictionTable[tablePosI],
155 g_FrictionTable[tablePosI + 1],
156 tablePosFrac) * g_DT;
157 //debugF(" accel", accel);
158 //debugF(" friction", friction);
159
Jason Samsfd22dac2009-09-20 17:24:16 -0700160 // If our velocity is low OR acceleration is opposing it, apply it.
161 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
162 g_PosVelocity += accel;
163 }
164
Jason Sams86c87ed2009-09-18 13:55:55 -0700165 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
166 // Special get back to center and overcome friction physics.
167 float t = tablePosNorm - 0.5f;
168 if (fabsf(t) < (friction * g_DT)) {
169 // really close, just snap
170 g_PosPage = roundf(g_PosPage);
171 g_PosVelocity = 0;
172 } else {
173 if (t > 0) {
174 g_PosVelocity = -friction;
175 } else {
176 g_PosVelocity = friction;
177 }
178 }
179 } else {
180 // Normal physics
181 if (g_PosVelocity > 0) {
182 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700183 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700184 } else {
185 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700186 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700187 }
188 }
189 g_PosPage += g_PosVelocity * g_DT;
190
191 // Check for out of boundry conditions.
192 if (g_PosPage < 0 && g_PosVelocity < 0) {
193 float damp = 1.0 + (g_PosPage * 3);
194 damp = clampf(damp, 0.f, 0.9f);
195 g_PosVelocity *= damp;
196 }
197 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
198 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
199 damp = clampf(damp, 0.f, 0.9f);
200 g_PosVelocity *= damp;
201 }
202}
203
Joe Onorato0d1c5632009-08-28 15:57:18 -0700204float
205far_size(float sizeAt0)
206{
207 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
208}
209
Joe Onoratoefabe002009-08-28 09:38:18 -0700210void
Jason Samsfd22dac2009-09-20 17:24:16 -0700211draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700212{
213 int row;
214 int col;
215
Jason Sams86c87ed2009-09-18 13:55:55 -0700216 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700217
Joe Onoratoefabe002009-08-28 09:38:18 -0700218 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
219 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
220
221 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700222 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700223
Joe Onorato6665c0f2009-09-02 15:27:24 -0700224 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700225 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700226
Joe Onorato6665c0f2009-09-02 15:27:24 -0700227 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
228
Jason Sams78aebd82009-09-15 13:06:59 -0700229 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700230 float farLabelWidth = far_size(normalizedLabelWidth);
Jason Sams78aebd82009-09-15 13:06:59 -0700231 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
232 float labelTextureWidth = (float)params->bubbleWidth / params->bubbleBitmapWidth;
233 float labelTextureHeight = (float)params->bubbleHeight / params->bubbleBitmapHeight;
Joe Onoratoefabe002009-08-28 09:38:18 -0700234
235 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
236 float angle = centerAngle;
237 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
238
Jason Samsfd22dac2009-09-20 17:24:16 -0700239 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700240 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700241 float iconBottom = iconTop - farIconSize;
242
Joe Onorato0d1c5632009-08-28 15:57:18 -0700243 float labelTop = iconBottom - (.1 * farLabelHeight);
244 float labelBottom = labelTop - farLabelHeight;
245
Joe Onorato6665c0f2009-09-02 15:27:24 -0700246 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
247 float iconTextureBottom = iconTextureTop - farIconTextureSize;
248
Joe Onoratoefabe002009-08-28 09:38:18 -0700249 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
250 // icon
251 float sine = sinf(angle);
252 float cosine = cosf(angle);
253
Joe Onorato0d1c5632009-08-28 15:57:18 -0700254 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700255 float centerZ = cosine * RADIUS / scale;
256
257 if (scale > 1.f) {
258 centerX *= scale;
259 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700260
Joe Onorato6665c0f2009-09-02 15:27:24 -0700261 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
262 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
263 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
264 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
265
Jason Samsfd22dac2009-09-20 17:24:16 -0700266 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700267 if (state->selectedIconIndex == icon) {
268 bindTexture(NAMED_PF, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700269 drawQuadTexCoords(
270 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
271 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
272 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
273 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400274 } else {
275 bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon));
276 drawQuadTexCoords(
277 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
278 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
279 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
280 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700281 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700282
Joe Onoratoefabe002009-08-28 09:38:18 -0700283 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700284 if (scale < 1.2f) {
285 float a = maxf(scale, 1.0f);
286 a = (1.2f - a) * 5;
287 color(1.0f, 1.0f, 1.0f, a);
288
Joe Onorato85a02a82009-09-08 12:34:22 -0700289 float labelLeftX = centerX - farLabelWidth * 0.5f;
290 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700291
Joe Onorato85a02a82009-09-08 12:34:22 -0700292 bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon));
293 drawQuadTexCoords(
294 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
295 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
296 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
297 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
298 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700299
Joe Onoratoefabe002009-08-28 09:38:18 -0700300 angle += columnGutterAngle + iconWidthAngle;
301 icon++;
302 }
303 }
304}
305
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700306int
Joe Onoratod769a632009-08-11 17:09:02 -0700307main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700308{
Jason Samsfd22dac2009-09-20 17:24:16 -0700309 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700310 int newTime = uptimeMillis();
311 g_DT = (newTime - g_LastTime) / 1000.f;
312 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700313
Joe Onorato26646342009-09-23 15:15:48 -0700314 //debugF("zoom", g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700315 if (g_Zoom != g_ZoomTarget) {
316 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
317 if (dz && (fabsf(dz) < 0.03f)) {
318 if (dz > 0) {
319 dz = 0.03f;
320 } else {
321 dz = -0.03f;
322 }
323 }
324 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
325 g_Zoom = g_ZoomTarget;
326 } else {
327 g_Zoom += dz;
328 }
Joe Onorato006b25f2009-09-03 11:38:43 -0700329 }
330
Jason Samsfd22dac2009-09-20 17:24:16 -0700331 // Set clear value to dim the background based on the zoom position.
332 if (g_Zoom < 0.8f) {
333 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
334 if (g_Zoom == 0) {
335 // Nothing else to do if fully zoomed out.
Joe Onorato26646342009-09-23 15:15:48 -0700336 g_PosPage = roundf(g_PosPage);
337 return one_last_frame(0);
Jason Samsfd22dac2009-09-20 17:24:16 -0700338 }
339 } else {
340 pfClearColor(0.0f, 0.0f, 0.0f, 0.80f);
341 }
342
343
344
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700345 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700346 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700347 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700348
Jason Sams86c87ed2009-09-18 13:55:55 -0700349 updatePos(0.1f);
350 state->readPosX = g_PosPage;
351 state->readVel = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700352
Jason Sams86c87ed2009-09-18 13:55:55 -0700353 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700354
Joe Onoratoc567acb2009-08-31 14:34:43 -0700355 // Draw the icons ========================================
356 bindProgramVertex(NAMED_PV);
Joe Onoratoefabe002009-08-28 09:38:18 -0700357 bindProgramFragment(NAMED_PF);
358 bindProgramFragmentStore(NAMED_PFS);
359
360 // Bug makes 1.0f alpha fail.
361 color(1.0f, 1.0f, 1.0f, 0.99f);
362
363 int lastIcon = iconCount-1;
364
Jason Sams86c87ed2009-09-18 13:55:55 -0700365 int page = g_PosPage;
366 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700367
Joe Onoratod769a632009-08-11 17:09:02 -0700368 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700369 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700370
Jason Samsfd22dac2009-09-20 17:24:16 -0700371 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700372
Jason Samsfd22dac2009-09-20 17:24:16 -0700373 float pageAngle = VIEW_ANGLE * 1.2f;
374 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
375 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700376
Joe Onorato6665c0f2009-09-02 15:27:24 -0700377
378 // Draw the scroll handle ========================================
379 /*
Joe Onoratoc567acb2009-08-31 14:34:43 -0700380 bindTexture(NAMED_PFText, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
381 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
382 float handleTop = 680;
383 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
384 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
385 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
386 */
Joe Onorato93839052009-08-06 20:34:32 -0700387
Jason Sams86c87ed2009-09-18 13:55:55 -0700388 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700389 // So we keep rendering until the bug is fixed.
Joe Onorato26646342009-09-23 15:15:48 -0700390 return one_last_frame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
Joe Onorato93839052009-08-06 20:34:32 -0700391}
392