blob: 58623730e399c4dcb002dae23e0514cc07b32201 [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 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 Sams78aebd82009-09-15 13:06:59 -070092}
93
Joe Onorato360d0352009-09-28 14:37:53 -040094void touchUp() {
95 g_LastTouchDown = 0;
96}
97
Jason Samsfd22dac2009-09-20 17:24:16 -070098void setZoomTarget() {
Joe Onorato7bb17492009-09-24 17:51:01 -070099 g_ZoomTarget = state->zoomTarget;
Jason Samsfd22dac2009-09-20 17:24:16 -0700100 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400101}
102
Joe Onorato7bb17492009-09-24 17:51:01 -0700103void setZoom() {
104 readback->zoom = g_Zoom = g_ZoomTarget = state->zoom;
105 //debugF("zoom", g_ZoomTarget);
106}
107
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700108int
109count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700110{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700111 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
112 int pages = iconCount / iconsPerPage;
113 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700114 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700115 }
Joe Onoratod769a632009-08-11 17:09:02 -0700116 return pages;
117}
118
Joe Onoratod769a632009-08-11 17:09:02 -0700119float
120modf(float x, float y)
121{
122 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700123}
124
Jason Sams86c87ed2009-09-18 13:55:55 -0700125void updatePos() {
126 if (g_LastTouchDown) {
127 return;
128 }
129
Jason Sams86c87ed2009-09-18 13:55:55 -0700130 float tablePosNorm = fracf(g_PosPage + 0.5f);
131 float tablePosF = tablePosNorm * g_PhysicsTableSize;
132 int tablePosI = tablePosF;
133 float tablePosFrac = tablePosF - tablePosI;
Jason Sams86c87ed2009-09-18 13:55:55 -0700134 float accel = lerpf(g_AttractionTable[tablePosI],
135 g_AttractionTable[tablePosI + 1],
136 tablePosFrac) * g_DT;
137 float friction = lerpf(g_FrictionTable[tablePosI],
138 g_FrictionTable[tablePosI + 1],
139 tablePosFrac) * g_DT;
140 //debugF(" accel", accel);
141 //debugF(" friction", friction);
142
Jason Samsfd22dac2009-09-20 17:24:16 -0700143 // If our velocity is low OR acceleration is opposing it, apply it.
144 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
145 g_PosVelocity += accel;
146 }
147
Jason Sams86c87ed2009-09-18 13:55:55 -0700148 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
149 // Special get back to center and overcome friction physics.
150 float t = tablePosNorm - 0.5f;
151 if (fabsf(t) < (friction * g_DT)) {
152 // really close, just snap
153 g_PosPage = roundf(g_PosPage);
154 g_PosVelocity = 0;
155 } else {
156 if (t > 0) {
157 g_PosVelocity = -friction;
158 } else {
159 g_PosVelocity = friction;
160 }
161 }
162 } else {
163 // Normal physics
164 if (g_PosVelocity > 0) {
165 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700166 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700167 } else {
168 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700169 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700170 }
171 }
172 g_PosPage += g_PosVelocity * g_DT;
173
174 // Check for out of boundry conditions.
175 if (g_PosPage < 0 && g_PosVelocity < 0) {
176 float damp = 1.0 + (g_PosPage * 3);
177 damp = clampf(damp, 0.f, 0.9f);
178 g_PosVelocity *= damp;
179 }
180 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
181 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
182 damp = clampf(damp, 0.f, 0.9f);
183 g_PosVelocity *= damp;
184 }
185}
186
Joe Onorato0d1c5632009-08-28 15:57:18 -0700187float
188far_size(float sizeAt0)
189{
190 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
191}
192
Joe Onoratoefabe002009-08-28 09:38:18 -0700193void
Jason Samsfd22dac2009-09-20 17:24:16 -0700194draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700195{
196 int row;
197 int col;
198
Jason Sams86c87ed2009-09-18 13:55:55 -0700199 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700200
Joe Onoratoefabe002009-08-28 09:38:18 -0700201 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
202 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
203
204 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700205 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700206
Joe Onorato6665c0f2009-09-02 15:27:24 -0700207 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700208 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700209
Joe Onorato6665c0f2009-09-02 15:27:24 -0700210 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
211
Jason Sams78aebd82009-09-15 13:06:59 -0700212 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700213 float farLabelWidth = far_size(normalizedLabelWidth);
Jason Sams78aebd82009-09-15 13:06:59 -0700214 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
215 float labelTextureWidth = (float)params->bubbleWidth / params->bubbleBitmapWidth;
216 float labelTextureHeight = (float)params->bubbleHeight / params->bubbleBitmapHeight;
Joe Onoratoefabe002009-08-28 09:38:18 -0700217
218 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
219 float angle = centerAngle;
220 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
221
Jason Samsfd22dac2009-09-20 17:24:16 -0700222 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700223 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700224 float iconBottom = iconTop - farIconSize;
225
Joe Onorato0d1c5632009-08-28 15:57:18 -0700226 float labelTop = iconBottom - (.1 * farLabelHeight);
227 float labelBottom = labelTop - farLabelHeight;
228
Joe Onorato6665c0f2009-09-02 15:27:24 -0700229 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
230 float iconTextureBottom = iconTextureTop - farIconTextureSize;
231
Joe Onoratoefabe002009-08-28 09:38:18 -0700232 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
233 // icon
234 float sine = sinf(angle);
235 float cosine = cosf(angle);
236
Joe Onorato0d1c5632009-08-28 15:57:18 -0700237 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700238 float centerZ = cosine * RADIUS / scale;
239
240 if (scale > 1.f) {
241 centerX *= scale;
242 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700243
Joe Onorato6665c0f2009-09-02 15:27:24 -0700244 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
245 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
246 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
247 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
248
Jason Samsfd22dac2009-09-20 17:24:16 -0700249 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700250 if (state->selectedIconIndex == icon) {
Jason Samscd689e12009-09-29 15:28:22 -0700251 bindTexture(NAMED_PFTexLinear, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700252 drawQuadTexCoords(
253 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
254 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
255 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
256 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400257 } else {
Jason Samscd689e12009-09-29 15:28:22 -0700258 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_ICON_IDS, icon));
Joe Onorato1291a8c2009-09-15 15:07:25 -0400259 drawQuadTexCoords(
260 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
261 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
262 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
263 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700264 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700265
Joe Onoratoefabe002009-08-28 09:38:18 -0700266 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700267 if (scale < 1.2f) {
268 float a = maxf(scale, 1.0f);
269 a = (1.2f - a) * 5;
270 color(1.0f, 1.0f, 1.0f, a);
271
Joe Onorato85a02a82009-09-08 12:34:22 -0700272 float labelLeftX = centerX - farLabelWidth * 0.5f;
273 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700274
Jason Samscd689e12009-09-29 15:28:22 -0700275 bindTexture(NAMED_PFTexLinear, 0, loadI32(ALLOC_LABEL_IDS, icon));
Joe Onorato85a02a82009-09-08 12:34:22 -0700276 drawQuadTexCoords(
277 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
278 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
279 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
280 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
281 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700282
Joe Onoratoefabe002009-08-28 09:38:18 -0700283 angle += columnGutterAngle + iconWidthAngle;
284 icon++;
285 }
286 }
287}
288
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700289int
Joe Onoratod769a632009-08-11 17:09:02 -0700290main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700291{
Jason Samsfd22dac2009-09-20 17:24:16 -0700292 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700293 int newTime = uptimeMillis();
294 g_DT = (newTime - g_LastTime) / 1000.f;
295 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700296
Joe Onorato26646342009-09-23 15:15:48 -0700297 //debugF("zoom", g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700298 if (g_Zoom != g_ZoomTarget) {
299 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
300 if (dz && (fabsf(dz) < 0.03f)) {
301 if (dz > 0) {
302 dz = 0.03f;
303 } else {
304 dz = -0.03f;
305 }
306 }
307 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
308 g_Zoom = g_ZoomTarget;
309 } else {
310 g_Zoom += dz;
311 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700312 readback->zoom = g_Zoom;
Joe Onorato006b25f2009-09-03 11:38:43 -0700313 }
314
Jason Samsfd22dac2009-09-20 17:24:16 -0700315 // Set clear value to dim the background based on the zoom position.
Joe Onorato7bb17492009-09-24 17:51:01 -0700316 if (g_Zoom < 0.001f) {
317 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Joe Onorato360d0352009-09-28 14:37:53 -0400318 // When we're zoomed out and not tracking motion events, reset the pos to 0.
319 if (!g_LastTouchDown) {
320 g_PosPage = 0;
321 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700322 return 1; // 0;
323 } else if (g_Zoom < 0.8f) {
Jason Samsfd22dac2009-09-20 17:24:16 -0700324 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700325 } else {
326 pfClearColor(0.0f, 0.0f, 0.0f, 0.80f);
327 }
328
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700329 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700330 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700331 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700332
Jason Sams86c87ed2009-09-18 13:55:55 -0700333 updatePos(0.1f);
Joe Onorato7bb17492009-09-24 17:51:01 -0700334 readback->posX = g_PosPage;
335 readback->velocity = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700336
Jason Sams86c87ed2009-09-18 13:55:55 -0700337 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700338
Joe Onoratoc567acb2009-08-31 14:34:43 -0700339 // Draw the icons ========================================
Joe Onoratoefabe002009-08-28 09:38:18 -0700340
341 // Bug makes 1.0f alpha fail.
342 color(1.0f, 1.0f, 1.0f, 0.99f);
343
344 int lastIcon = iconCount-1;
345
Jason Sams86c87ed2009-09-18 13:55:55 -0700346 int page = g_PosPage;
347 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700348
Joe Onoratod769a632009-08-11 17:09:02 -0700349 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700350 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700351
Jason Samsfd22dac2009-09-20 17:24:16 -0700352 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700353
Jason Samsfd22dac2009-09-20 17:24:16 -0700354 float pageAngle = VIEW_ANGLE * 1.2f;
355 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
356 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700357
Joe Onorato56848b02009-09-25 13:59:59 -0700358 // Draw the border lines for debugging ========================================
359 /*
360 bindProgramVertex(NAMED_PVOrtho);
361 bindProgramFragment(NAMED_PFOrtho);
362 bindProgramFragmentStore(NAMED_PFSText);
363
364 color(1.0f, 1.0f, 0.0f, 0.99f);
365 int i;
366 for (i=0; i<ROWS_PER_PAGE+1; i++) {
367 int y = loadI32(ALLOC_Y_BORDERS, i);
368 drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f);
369 }
370 for (i=0; i<COLUMNS_PER_PAGE+1; i++) {
371 int x = loadI32(ALLOC_X_BORDERS, i);
372 drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f);
373 }
374 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700375
376 // Draw the scroll handle ========================================
377 /*
Joe Onorato56848b02009-09-25 13:59:59 -0700378 bindTexture(NAMED_PFOrtho, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
Joe Onoratoc567acb2009-08-31 14:34:43 -0700379 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
380 float handleTop = 680;
381 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
382 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
383 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
384 */
Joe Onorato93839052009-08-06 20:34:32 -0700385
Jason Sams86c87ed2009-09-18 13:55:55 -0700386 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700387 // So we keep rendering until the bug is fixed.
Joe Onorato7bb17492009-09-24 17:51:01 -0700388 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
Joe Onorato93839052009-08-06 20:34:32 -0700389}
390