blob: eb019eec888acdc5b22497773a1746afd70131f4 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001#pragma version(1)
2#pragma stateVertex(PV)
Jason Samscd689e12009-09-29 15:28:22 -07003#pragma stateFragment(PFTexLinear)
4#pragma stateStore(PSIcons)
Joe Onorato93839052009-08-06 20:34:32 -07005
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 Sams28870b72009-09-30 17:32:05 -070068 //debugF("Move P", g_PosPage);
Jason Sams86c87ed2009-09-18 13:55:55 -070069}
70
71void fling() {
72 g_LastTouchDown = 0;
73 g_PosVelocity = -state->flingVelocityX;
Jason Samsfd22dac2009-09-20 17:24:16 -070074 float av = fabsf(g_PosVelocity);
75 float minVel = 3f;
76
77 minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
78
79 if (av < minVel && av > 0.2f) {
80 if (g_PosVelocity > 0) {
81 g_PosVelocity = minVel;
82 } else {
83 g_PosVelocity = -minVel;
84 }
85 }
86
Jason Sams86c87ed2009-09-18 13:55:55 -070087 if (g_PosPage <= 0) {
88 g_PosVelocity = maxf(0, g_PosVelocity);
89 }
90 if (g_PosPage > (g_PageCount - 1)) {
Jason Samsfd22dac2009-09-20 17:24:16 -070091 g_PosVelocity = minf(0, g_PosVelocity);
Jason Sams86c87ed2009-09-18 13:55:55 -070092 }
Jason Sams28870b72009-09-30 17:32:05 -070093 //debugF("fling v", g_PosVelocity);
Jason Sams78aebd82009-09-15 13:06:59 -070094}
95
Joe Onorato360d0352009-09-28 14:37:53 -040096void touchUp() {
97 g_LastTouchDown = 0;
98}
99
Jason Samsfd22dac2009-09-20 17:24:16 -0700100void setZoomTarget() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700101 g_ZoomTarget = state->zoomTarget;
Jason Samsfd22dac2009-09-20 17:24:16 -0700102 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400103}
104
Joe Onorato7bb17492009-09-24 17:51:01 -0700105void setZoom() {
106 readback->zoom = g_Zoom = g_ZoomTarget = state->zoom;
107 //debugF("zoom", g_ZoomTarget);
108}
109
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700110int
111count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700112{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700113 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
114 int pages = iconCount / iconsPerPage;
115 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700116 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700117 }
Joe Onoratod769a632009-08-11 17:09:02 -0700118 return pages;
119}
120
Joe Onoratod769a632009-08-11 17:09:02 -0700121float
122modf(float x, float y)
123{
124 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700125}
126
Jason Sams86c87ed2009-09-18 13:55:55 -0700127void updatePos() {
128 if (g_LastTouchDown) {
129 return;
130 }
131
Jason Sams86c87ed2009-09-18 13:55:55 -0700132 float tablePosNorm = fracf(g_PosPage + 0.5f);
133 float tablePosF = tablePosNorm * g_PhysicsTableSize;
134 int tablePosI = tablePosF;
135 float tablePosFrac = tablePosF - tablePosI;
Jason Sams86c87ed2009-09-18 13:55:55 -0700136 float accel = lerpf(g_AttractionTable[tablePosI],
137 g_AttractionTable[tablePosI + 1],
138 tablePosFrac) * g_DT;
139 float friction = lerpf(g_FrictionTable[tablePosI],
140 g_FrictionTable[tablePosI + 1],
141 tablePosFrac) * g_DT;
142 //debugF(" accel", accel);
143 //debugF(" friction", friction);
144
Jason Samsfd22dac2009-09-20 17:24:16 -0700145 // If our velocity is low OR acceleration is opposing it, apply it.
146 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
147 g_PosVelocity += accel;
148 }
149
Jason Sams86c87ed2009-09-18 13:55:55 -0700150 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
151 // Special get back to center and overcome friction physics.
152 float t = tablePosNorm - 0.5f;
153 if (fabsf(t) < (friction * g_DT)) {
154 // really close, just snap
155 g_PosPage = roundf(g_PosPage);
156 g_PosVelocity = 0;
157 } else {
158 if (t > 0) {
159 g_PosVelocity = -friction;
160 } else {
161 g_PosVelocity = friction;
162 }
163 }
164 } else {
165 // Normal physics
166 if (g_PosVelocity > 0) {
167 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700168 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700169 } else {
170 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700171 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700172 }
173 }
174 g_PosPage += g_PosVelocity * g_DT;
175
176 // Check for out of boundry conditions.
177 if (g_PosPage < 0 && g_PosVelocity < 0) {
178 float damp = 1.0 + (g_PosPage * 3);
179 damp = clampf(damp, 0.f, 0.9f);
180 g_PosVelocity *= damp;
181 }
182 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
183 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
184 damp = clampf(damp, 0.f, 0.9f);
185 g_PosVelocity *= damp;
186 }
187}
188
Joe Onorato0d1c5632009-08-28 15:57:18 -0700189float
190far_size(float sizeAt0)
191{
192 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
193}
194
Joe Onoratoefabe002009-08-28 09:38:18 -0700195void
Jason Samsfd22dac2009-09-20 17:24:16 -0700196draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700197{
198 int row;
199 int col;
200
Jason Sams86c87ed2009-09-18 13:55:55 -0700201 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700202
Joe Onoratoefabe002009-08-28 09:38:18 -0700203 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
204 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
205
206 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700207 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700208
Joe Onorato6665c0f2009-09-02 15:27:24 -0700209 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700210 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700211
Joe Onorato6665c0f2009-09-02 15:27:24 -0700212 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
213
Jason Sams78aebd82009-09-15 13:06:59 -0700214 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Jason Sams78aebd82009-09-15 13:06:59 -0700215 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
Joe Onoratoefabe002009-08-28 09:38:18 -0700216
217 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
218 float angle = centerAngle;
219 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
220
Jason Samsfd22dac2009-09-20 17:24:16 -0700221 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700222 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700223 float iconBottom = iconTop - farIconSize;
224
Jason Sams28870b72009-09-30 17:32:05 -0700225 float labelY = iconBottom - farLabelHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700226 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
227 float iconTextureBottom = iconTextureTop - farIconTextureSize;
228
Joe Onoratoefabe002009-08-28 09:38:18 -0700229 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
230 // icon
231 float sine = sinf(angle);
232 float cosine = cosf(angle);
233
Joe Onorato0d1c5632009-08-28 15:57:18 -0700234 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700235 float centerZ = cosine * RADIUS / scale;
236
237 if (scale > 1.f) {
238 centerX *= scale;
239 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700240
Jason Sams28870b72009-09-30 17:32:05 -0700241 float iconLeftX = centerX - (/*cosine * */ farIconTextureSize * .5);
242 float iconRightX = centerX + (/*cosine * */ farIconTextureSize * .5);
243 float iconLeftZ = centerZ;// + (sine * farIconTextureSize * .5);
244 float iconRightZ = centerZ;// - (sine * farIconTextureSize * .5);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700245
Jason Samsfd22dac2009-09-20 17:24:16 -0700246 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700247 if (state->selectedIconIndex == icon) {
Jason Samscd689e12009-09-29 15:28:22 -0700248 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700249 drawQuadTexCoords(
250 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
251 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
252 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
253 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400254 } else {
Jason Samscd689e12009-09-29 15:28:22 -0700255 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, icon));
Joe Onorato1291a8c2009-09-15 15:07:25 -0400256 drawQuadTexCoords(
257 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
258 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
259 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
260 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700261 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700262
Joe Onoratoefabe002009-08-28 09:38:18 -0700263 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700264 if (scale < 1.2f) {
Jason Sams28870b72009-09-30 17:32:05 -0700265 float a = (1.2f - maxf(scale, 1.0f)) * 5;
Jason Samsfd22dac2009-09-20 17:24:16 -0700266 color(1.0f, 1.0f, 1.0f, a);
Jason Samscd689e12009-09-29 15:28:22 -0700267 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, icon));
Jason Sams28870b72009-09-30 17:32:05 -0700268 drawSprite(centerX, labelY, centerZ,
269 params->bubbleBitmapWidth, params->bubbleBitmapHeight);
Joe Onorato85a02a82009-09-08 12:34:22 -0700270 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700271
Joe Onoratoefabe002009-08-28 09:38:18 -0700272 angle += columnGutterAngle + iconWidthAngle;
273 icon++;
274 }
275 }
276}
277
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700278int
Joe Onoratod769a632009-08-11 17:09:02 -0700279main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700280{
Jason Samsfd22dac2009-09-20 17:24:16 -0700281 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700282 int newTime = uptimeMillis();
283 g_DT = (newTime - g_LastTime) / 1000.f;
284 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700285
Joe Onorato26646342009-09-23 15:15:48 -0700286 //debugF("zoom", g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700287 if (g_Zoom != g_ZoomTarget) {
288 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
289 if (dz && (fabsf(dz) < 0.03f)) {
290 if (dz > 0) {
291 dz = 0.03f;
292 } else {
293 dz = -0.03f;
294 }
295 }
296 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
297 g_Zoom = g_ZoomTarget;
298 } else {
299 g_Zoom += dz;
300 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700301 readback->zoom = g_Zoom;
Joe Onorato006b25f2009-09-03 11:38:43 -0700302 }
303
Jason Samsfd22dac2009-09-20 17:24:16 -0700304 // Set clear value to dim the background based on the zoom position.
Joe Onorato7bb17492009-09-24 17:51:01 -0700305 if (g_Zoom < 0.001f) {
306 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Joe Onorato360d0352009-09-28 14:37:53 -0400307 // When we're zoomed out and not tracking motion events, reset the pos to 0.
308 if (!g_LastTouchDown) {
309 g_PosPage = 0;
310 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700311 return 1; // 0;
Jason Sams28870b72009-09-30 17:32:05 -0700312 } else if (g_Zoom < 0.85f) {
Jason Samsfd22dac2009-09-20 17:24:16 -0700313 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700314 } else {
Jason Sams28870b72009-09-30 17:32:05 -0700315 pfClearColor(0.0f, 0.0f, 0.0f, 0.85f);
Jason Samsfd22dac2009-09-20 17:24:16 -0700316 }
317
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700318 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700319 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700320 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700321
Jason Sams86c87ed2009-09-18 13:55:55 -0700322 updatePos(0.1f);
Joe Onorato7bb17492009-09-24 17:51:01 -0700323 readback->posX = g_PosPage;
324 readback->velocity = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700325
Jason Sams86c87ed2009-09-18 13:55:55 -0700326 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700327
Joe Onoratoc567acb2009-08-31 14:34:43 -0700328 // Draw the icons ========================================
Joe Onoratoefabe002009-08-28 09:38:18 -0700329
330 // Bug makes 1.0f alpha fail.
331 color(1.0f, 1.0f, 1.0f, 0.99f);
332
333 int lastIcon = iconCount-1;
334
Jason Sams86c87ed2009-09-18 13:55:55 -0700335 int page = g_PosPage;
336 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700337
Joe Onoratod769a632009-08-11 17:09:02 -0700338 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700339 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700340
Jason Samsfd22dac2009-09-20 17:24:16 -0700341 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700342
Jason Samsfd22dac2009-09-20 17:24:16 -0700343 float pageAngle = VIEW_ANGLE * 1.2f;
344 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
345 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700346
Joe Onorato56848b02009-09-25 13:59:59 -0700347 // Draw the border lines for debugging ========================================
348 /*
349 bindProgramVertex(NAMED_PVOrtho);
350 bindProgramFragment(NAMED_PFOrtho);
351 bindProgramFragmentStore(NAMED_PFSText);
352
353 color(1.0f, 1.0f, 0.0f, 0.99f);
354 int i;
355 for (i=0; i<ROWS_PER_PAGE+1; i++) {
356 int y = loadI32(ALLOC_Y_BORDERS, i);
357 drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f);
358 }
359 for (i=0; i<COLUMNS_PER_PAGE+1; i++) {
360 int x = loadI32(ALLOC_X_BORDERS, i);
361 drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f);
362 }
363 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700364
365 // Draw the scroll handle ========================================
366 /*
Joe Onorato56848b02009-09-25 13:59:59 -0700367 bindTexture(NAMED_PFOrtho, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
Joe Onoratoc567acb2009-08-31 14:34:43 -0700368 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
369 float handleTop = 680;
370 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
371 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
372 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
373 */
Joe Onorato93839052009-08-06 20:34:32 -0700374
Jason Sams86c87ed2009-09-18 13:55:55 -0700375 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700376 // So we keep rendering until the bug is fixed.
Joe Onorato7bb17492009-09-24 17:51:01 -0700377 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
Joe Onorato93839052009-08-06 20:34:32 -0700378}
379