blob: 8d09127241958e9bfa607afccdad63ab9042d2b9 [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
Jason Sams78aebd82009-09-15 13:06:59 -070027void init() {
Jason Sams86c87ed2009-09-18 13:55:55 -070028 g_AttractionTable[0] = 4.5f;
29 g_AttractionTable[1] = 4.5f;
30 g_AttractionTable[2] = 5.0f;
31 g_AttractionTable[3] = 4.0f;
32 g_AttractionTable[4] = -4.0f;
33 g_AttractionTable[5] = -5.0f;
34 g_AttractionTable[6] = -4.5f;
35 g_AttractionTable[7] = -4.5f;
36 g_AttractionTable[8] = -4.5f; // dup 7 to avoid a clamp later
37 g_FrictionTable[0] = 3.5f;
38 g_FrictionTable[1] = 3.6f;
39 g_FrictionTable[2] = 3.7f;
40 g_FrictionTable[3] = 3.8f;
41 g_FrictionTable[4] = 3.8f;
42 g_FrictionTable[5] = 3.7f;
43 g_FrictionTable[6] = 3.6f;
44 g_FrictionTable[7] = 3.5f;
45 g_FrictionTable[8] = 3.5f; // dup 7 to avoid a clamp later
46 g_PhysicsTableSize = 7;
47
48 g_PosVelocity = 0;
49 g_PosPage = 0;
50 g_LastTouchDown = 0;
51 g_LastPositionX = 0;
Jason Samsfd22dac2009-09-20 17:24:16 -070052 g_Zoom = 0;
53 g_ZoomTarget = 0;
Jason Sams86c87ed2009-09-18 13:55:55 -070054}
55
56void move() {
57 if (g_LastTouchDown) {
58 float dx = -(state->newPositionX - g_LastPositionX);
59 g_PosVelocity = 0;
60 g_PosPage += dx;
Jason Samsfd22dac2009-09-20 17:24:16 -070061
62 float pmin = -0.25f;
63 float pmax = (g_PageCount - 1) + 0.25f;
64 g_PosPage = clampf(g_PosPage, pmin, pmax);
Jason Sams86c87ed2009-09-18 13:55:55 -070065 }
66 g_LastTouchDown = state->newTouchDown;
67 g_LastPositionX = state->newPositionX;
Jason Sams86c87ed2009-09-18 13:55:55 -070068}
69
70void fling() {
71 g_LastTouchDown = 0;
72 g_PosVelocity = -state->flingVelocityX;
Jason Samsfd22dac2009-09-20 17:24:16 -070073 float av = fabsf(g_PosVelocity);
74 float minVel = 3f;
75
76 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
77
78 if (av < minVel && av > 0.2f) {
79 if (g_PosVelocity > 0) {
80 g_PosVelocity = minVel;
81 } else {
82 g_PosVelocity = -minVel;
83 }
84 }
85
Jason Sams86c87ed2009-09-18 13:55:55 -070086 if (g_PosPage <= 0) {
87 g_PosVelocity = maxf(0, g_PosVelocity);
88 }
89 if (g_PosPage > (g_PageCount - 1)) {
Jason Samsfd22dac2009-09-20 17:24:16 -070090 g_PosVelocity = minf(0, g_PosVelocity);
Jason Sams86c87ed2009-09-18 13:55:55 -070091 }
Jason Samsfd22dac2009-09-20 17:24:16 -070092 //g_Zoom += (maxf(fabsf(g_PosVelocity), 3) - 3) / 2.f;
Jason Sams78aebd82009-09-15 13:06:59 -070093}
94
Joe Onorato360d0352009-09-28 14:37:53 -040095void touchUp() {
96 g_LastTouchDown = 0;
97}
98
Jason Samsfd22dac2009-09-20 17:24:16 -070099void setZoomTarget() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700100 g_ZoomTarget = state->zoomTarget;
Jason Samsfd22dac2009-09-20 17:24:16 -0700101 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400102}
103
Joe Onorato7bb17492009-09-24 17:51:01 -0700104void setZoom() {
105 readback->zoom = g_Zoom = g_ZoomTarget = state->zoom;
106 //debugF("zoom", g_ZoomTarget);
107}
108
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700109int
110count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700111{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700112 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
113 int pages = iconCount / iconsPerPage;
114 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700115 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700116 }
Joe Onoratod769a632009-08-11 17:09:02 -0700117 return pages;
118}
119
Joe Onoratod769a632009-08-11 17:09:02 -0700120float
121modf(float x, float y)
122{
123 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700124}
125
Jason Sams86c87ed2009-09-18 13:55:55 -0700126void updatePos() {
127 if (g_LastTouchDown) {
128 return;
129 }
130
131 //debugF("g_PosPage", g_PosPage);
132 //debugF(" g_PosVelocity", g_PosVelocity);
133
134 float tablePosNorm = fracf(g_PosPage + 0.5f);
135 float tablePosF = tablePosNorm * g_PhysicsTableSize;
136 int tablePosI = tablePosF;
137 float tablePosFrac = tablePosF - tablePosI;
138 //debugF("tablePosNorm", tablePosNorm);
139 //debugF("tablePosF", tablePosF);
140 //debugF("tablePosI", tablePosI);
141 //debugF("tablePosFrac", tablePosFrac);
142
143 float accel = lerpf(g_AttractionTable[tablePosI],
144 g_AttractionTable[tablePosI + 1],
145 tablePosFrac) * g_DT;
146 float friction = lerpf(g_FrictionTable[tablePosI],
147 g_FrictionTable[tablePosI + 1],
148 tablePosFrac) * g_DT;
149 //debugF(" accel", accel);
150 //debugF(" friction", friction);
151
Jason Samsfd22dac2009-09-20 17:24:16 -0700152 // If our velocity is low OR acceleration is opposing it, apply it.
153 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
154 g_PosVelocity += accel;
155 }
156
Jason Sams86c87ed2009-09-18 13:55:55 -0700157 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
158 // Special get back to center and overcome friction physics.
159 float t = tablePosNorm - 0.5f;
160 if (fabsf(t) < (friction * g_DT)) {
161 // really close, just snap
162 g_PosPage = roundf(g_PosPage);
163 g_PosVelocity = 0;
164 } else {
165 if (t > 0) {
166 g_PosVelocity = -friction;
167 } else {
168 g_PosVelocity = friction;
169 }
170 }
171 } else {
172 // Normal physics
173 if (g_PosVelocity > 0) {
174 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700175 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700176 } else {
177 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700178 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700179 }
180 }
181 g_PosPage += g_PosVelocity * g_DT;
182
183 // Check for out of boundry conditions.
184 if (g_PosPage < 0 && g_PosVelocity < 0) {
185 float damp = 1.0 + (g_PosPage * 3);
186 damp = clampf(damp, 0.f, 0.9f);
187 g_PosVelocity *= damp;
188 }
189 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
190 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
191 damp = clampf(damp, 0.f, 0.9f);
192 g_PosVelocity *= damp;
193 }
194}
195
Joe Onorato0d1c5632009-08-28 15:57:18 -0700196float
197far_size(float sizeAt0)
198{
199 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
200}
201
Joe Onoratoefabe002009-08-28 09:38:18 -0700202void
Jason Samsfd22dac2009-09-20 17:24:16 -0700203draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700204{
205 int row;
206 int col;
207
Jason Sams86c87ed2009-09-18 13:55:55 -0700208 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700209
Joe Onoratoefabe002009-08-28 09:38:18 -0700210 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
211 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
212
213 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700214 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700215
Joe Onorato6665c0f2009-09-02 15:27:24 -0700216 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700217 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700218
Joe Onorato6665c0f2009-09-02 15:27:24 -0700219 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
220
Jason Sams78aebd82009-09-15 13:06:59 -0700221 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700222 float farLabelWidth = far_size(normalizedLabelWidth);
Jason Sams78aebd82009-09-15 13:06:59 -0700223 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
224 float labelTextureWidth = (float)params->bubbleWidth / params->bubbleBitmapWidth;
225 float labelTextureHeight = (float)params->bubbleHeight / params->bubbleBitmapHeight;
Joe Onoratoefabe002009-08-28 09:38:18 -0700226
227 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
228 float angle = centerAngle;
229 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
230
Jason Samsfd22dac2009-09-20 17:24:16 -0700231 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700232 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700233 float iconBottom = iconTop - farIconSize;
234
Joe Onorato0d1c5632009-08-28 15:57:18 -0700235 float labelTop = iconBottom - (.1 * farLabelHeight);
236 float labelBottom = labelTop - farLabelHeight;
237
Joe Onorato6665c0f2009-09-02 15:27:24 -0700238 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
239 float iconTextureBottom = iconTextureTop - farIconTextureSize;
240
Joe Onoratoefabe002009-08-28 09:38:18 -0700241 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
242 // icon
243 float sine = sinf(angle);
244 float cosine = cosf(angle);
245
Joe Onorato0d1c5632009-08-28 15:57:18 -0700246 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700247 float centerZ = cosine * RADIUS / scale;
248
249 if (scale > 1.f) {
250 centerX *= scale;
251 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700252
Joe Onorato6665c0f2009-09-02 15:27:24 -0700253 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
254 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
255 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
256 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
257
Jason Samsfd22dac2009-09-20 17:24:16 -0700258 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700259 if (state->selectedIconIndex == icon) {
260 bindTexture(NAMED_PF, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700261 drawQuadTexCoords(
262 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
263 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
264 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
265 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400266 } else {
267 bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon));
268 drawQuadTexCoords(
269 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
270 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
271 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
272 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700273 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700274
Joe Onoratoefabe002009-08-28 09:38:18 -0700275 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700276 if (scale < 1.2f) {
277 float a = maxf(scale, 1.0f);
278 a = (1.2f - a) * 5;
279 color(1.0f, 1.0f, 1.0f, a);
280
Joe Onorato85a02a82009-09-08 12:34:22 -0700281 float labelLeftX = centerX - farLabelWidth * 0.5f;
282 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700283
Joe Onorato85a02a82009-09-08 12:34:22 -0700284 bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon));
285 drawQuadTexCoords(
286 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
287 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
288 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
289 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
290 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700291
Joe Onoratoefabe002009-08-28 09:38:18 -0700292 angle += columnGutterAngle + iconWidthAngle;
293 icon++;
294 }
295 }
296}
297
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700298int
Joe Onoratod769a632009-08-11 17:09:02 -0700299main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700300{
Jason Samsfd22dac2009-09-20 17:24:16 -0700301 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700302 int newTime = uptimeMillis();
303 g_DT = (newTime - g_LastTime) / 1000.f;
304 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700305
Joe Onorato26646342009-09-23 15:15:48 -0700306 //debugF("zoom", g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700307 if (g_Zoom != g_ZoomTarget) {
308 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
309 if (dz && (fabsf(dz) < 0.03f)) {
310 if (dz > 0) {
311 dz = 0.03f;
312 } else {
313 dz = -0.03f;
314 }
315 }
316 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
317 g_Zoom = g_ZoomTarget;
318 } else {
319 g_Zoom += dz;
320 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700321 readback->zoom = g_Zoom;
Joe Onorato006b25f2009-09-03 11:38:43 -0700322 }
323
Jason Samsfd22dac2009-09-20 17:24:16 -0700324 // Set clear value to dim the background based on the zoom position.
Joe Onorato7bb17492009-09-24 17:51:01 -0700325 if (g_Zoom < 0.001f) {
326 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Joe Onorato360d0352009-09-28 14:37:53 -0400327 // When we're zoomed out and not tracking motion events, reset the pos to 0.
328 if (!g_LastTouchDown) {
329 g_PosPage = 0;
330 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700331 return 1; // 0;
332 } else if (g_Zoom < 0.8f) {
Jason Samsfd22dac2009-09-20 17:24:16 -0700333 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700334 } else {
335 pfClearColor(0.0f, 0.0f, 0.0f, 0.80f);
336 }
337
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700338 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700339 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700340 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700341
Jason Sams86c87ed2009-09-18 13:55:55 -0700342 updatePos(0.1f);
Joe Onorato7bb17492009-09-24 17:51:01 -0700343 readback->posX = g_PosPage;
344 readback->velocity = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700345
Jason Sams86c87ed2009-09-18 13:55:55 -0700346 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700347
Joe Onoratoc567acb2009-08-31 14:34:43 -0700348 // Draw the icons ========================================
349 bindProgramVertex(NAMED_PV);
Joe Onoratoefabe002009-08-28 09:38:18 -0700350 bindProgramFragment(NAMED_PF);
351 bindProgramFragmentStore(NAMED_PFS);
352
353 // Bug makes 1.0f alpha fail.
354 color(1.0f, 1.0f, 1.0f, 0.99f);
355
356 int lastIcon = iconCount-1;
357
Jason Sams86c87ed2009-09-18 13:55:55 -0700358 int page = g_PosPage;
359 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700360
Joe Onoratod769a632009-08-11 17:09:02 -0700361 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700362 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700363
Jason Samsfd22dac2009-09-20 17:24:16 -0700364 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700365
Jason Samsfd22dac2009-09-20 17:24:16 -0700366 float pageAngle = VIEW_ANGLE * 1.2f;
367 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
368 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700369
Joe Onorato56848b02009-09-25 13:59:59 -0700370 // Draw the border lines for debugging ========================================
371 /*
372 bindProgramVertex(NAMED_PVOrtho);
373 bindProgramFragment(NAMED_PFOrtho);
374 bindProgramFragmentStore(NAMED_PFSText);
375
376 color(1.0f, 1.0f, 0.0f, 0.99f);
377 int i;
378 for (i=0; i<ROWS_PER_PAGE+1; i++) {
379 int y = loadI32(ALLOC_Y_BORDERS, i);
380 drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f);
381 }
382 for (i=0; i<COLUMNS_PER_PAGE+1; i++) {
383 int x = loadI32(ALLOC_X_BORDERS, i);
384 drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f);
385 }
386 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700387
388 // Draw the scroll handle ========================================
389 /*
Joe Onorato56848b02009-09-25 13:59:59 -0700390 bindTexture(NAMED_PFOrtho, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
Joe Onoratoc567acb2009-08-31 14:34:43 -0700391 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
392 float handleTop = 680;
393 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
394 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
395 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
396 */
Joe Onorato93839052009-08-06 20:34:32 -0700397
Jason Sams86c87ed2009-09-18 13:55:55 -0700398 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700399 // So we keep rendering until the bug is fixed.
Joe Onorato7bb17492009-09-24 17:51:01 -0700400 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
Joe Onorato93839052009-08-06 20:34:32 -0700401}
402