blob: 59862bca42e1b9f8ca0a8e7e35314fc184ea8405 [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
Jason Samsfd22dac2009-09-20 17:24:16 -070095void setZoomTarget() {
Joe Onorato7bb17492009-09-24 17:51:01 -070096 g_ZoomTarget = state->zoomTarget;
Jason Samsfd22dac2009-09-20 17:24:16 -070097 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -040098}
99
Joe Onorato7bb17492009-09-24 17:51:01 -0700100void setZoom() {
101 readback->zoom = g_Zoom = g_ZoomTarget = state->zoom;
102 //debugF("zoom", g_ZoomTarget);
103}
104
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700105int
106count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700107{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700108 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
109 int pages = iconCount / iconsPerPage;
110 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700111 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700112 }
Joe Onoratod769a632009-08-11 17:09:02 -0700113 return pages;
114}
115
Joe Onoratod769a632009-08-11 17:09:02 -0700116float
117modf(float x, float y)
118{
119 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700120}
121
Jason Sams86c87ed2009-09-18 13:55:55 -0700122void updatePos() {
123 if (g_LastTouchDown) {
124 return;
125 }
126
127 //debugF("g_PosPage", g_PosPage);
128 //debugF(" g_PosVelocity", g_PosVelocity);
129
130 float tablePosNorm = fracf(g_PosPage + 0.5f);
131 float tablePosF = tablePosNorm * g_PhysicsTableSize;
132 int tablePosI = tablePosF;
133 float tablePosFrac = tablePosF - tablePosI;
134 //debugF("tablePosNorm", tablePosNorm);
135 //debugF("tablePosF", tablePosF);
136 //debugF("tablePosI", tablePosI);
137 //debugF("tablePosFrac", tablePosFrac);
138
139 float accel = lerpf(g_AttractionTable[tablePosI],
140 g_AttractionTable[tablePosI + 1],
141 tablePosFrac) * g_DT;
142 float friction = lerpf(g_FrictionTable[tablePosI],
143 g_FrictionTable[tablePosI + 1],
144 tablePosFrac) * g_DT;
145 //debugF(" accel", accel);
146 //debugF(" friction", friction);
147
Jason Samsfd22dac2009-09-20 17:24:16 -0700148 // If our velocity is low OR acceleration is opposing it, apply it.
149 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
150 g_PosVelocity += accel;
151 }
152
Jason Sams86c87ed2009-09-18 13:55:55 -0700153 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
154 // Special get back to center and overcome friction physics.
155 float t = tablePosNorm - 0.5f;
156 if (fabsf(t) < (friction * g_DT)) {
157 // really close, just snap
158 g_PosPage = roundf(g_PosPage);
159 g_PosVelocity = 0;
160 } else {
161 if (t > 0) {
162 g_PosVelocity = -friction;
163 } else {
164 g_PosVelocity = friction;
165 }
166 }
167 } else {
168 // Normal physics
169 if (g_PosVelocity > 0) {
170 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700171 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700172 } else {
173 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700174 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700175 }
176 }
177 g_PosPage += g_PosVelocity * g_DT;
178
179 // Check for out of boundry conditions.
180 if (g_PosPage < 0 && g_PosVelocity < 0) {
181 float damp = 1.0 + (g_PosPage * 3);
182 damp = clampf(damp, 0.f, 0.9f);
183 g_PosVelocity *= damp;
184 }
185 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
186 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
187 damp = clampf(damp, 0.f, 0.9f);
188 g_PosVelocity *= damp;
189 }
190}
191
Joe Onorato0d1c5632009-08-28 15:57:18 -0700192float
193far_size(float sizeAt0)
194{
195 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
196}
197
Joe Onoratoefabe002009-08-28 09:38:18 -0700198void
Jason Samsfd22dac2009-09-20 17:24:16 -0700199draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700200{
201 int row;
202 int col;
203
Jason Sams86c87ed2009-09-18 13:55:55 -0700204 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700205
Joe Onoratoefabe002009-08-28 09:38:18 -0700206 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
207 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
208
209 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700210 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700211
Joe Onorato6665c0f2009-09-02 15:27:24 -0700212 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700213 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700214
Joe Onorato6665c0f2009-09-02 15:27:24 -0700215 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
216
Jason Sams78aebd82009-09-15 13:06:59 -0700217 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700218 float farLabelWidth = far_size(normalizedLabelWidth);
Jason Sams78aebd82009-09-15 13:06:59 -0700219 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
220 float labelTextureWidth = (float)params->bubbleWidth / params->bubbleBitmapWidth;
221 float labelTextureHeight = (float)params->bubbleHeight / params->bubbleBitmapHeight;
Joe Onoratoefabe002009-08-28 09:38:18 -0700222
223 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
224 float angle = centerAngle;
225 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
226
Jason Samsfd22dac2009-09-20 17:24:16 -0700227 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700228 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700229 float iconBottom = iconTop - farIconSize;
230
Joe Onorato0d1c5632009-08-28 15:57:18 -0700231 float labelTop = iconBottom - (.1 * farLabelHeight);
232 float labelBottom = labelTop - farLabelHeight;
233
Joe Onorato6665c0f2009-09-02 15:27:24 -0700234 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
235 float iconTextureBottom = iconTextureTop - farIconTextureSize;
236
Joe Onoratoefabe002009-08-28 09:38:18 -0700237 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
238 // icon
239 float sine = sinf(angle);
240 float cosine = cosf(angle);
241
Joe Onorato0d1c5632009-08-28 15:57:18 -0700242 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700243 float centerZ = cosine * RADIUS / scale;
244
245 if (scale > 1.f) {
246 centerX *= scale;
247 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700248
Joe Onorato6665c0f2009-09-02 15:27:24 -0700249 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
250 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
251 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
252 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
253
Jason Samsfd22dac2009-09-20 17:24:16 -0700254 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700255 if (state->selectedIconIndex == icon) {
256 bindTexture(NAMED_PF, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700257 drawQuadTexCoords(
258 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
259 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
260 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
261 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400262 } else {
263 bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon));
264 drawQuadTexCoords(
265 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
266 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
267 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
268 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700269 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700270
Joe Onoratoefabe002009-08-28 09:38:18 -0700271 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700272 if (scale < 1.2f) {
273 float a = maxf(scale, 1.0f);
274 a = (1.2f - a) * 5;
275 color(1.0f, 1.0f, 1.0f, a);
276
Joe Onorato85a02a82009-09-08 12:34:22 -0700277 float labelLeftX = centerX - farLabelWidth * 0.5f;
278 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700279
Joe Onorato85a02a82009-09-08 12:34:22 -0700280 bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon));
281 drawQuadTexCoords(
282 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
283 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
284 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
285 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
286 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700287
Joe Onoratoefabe002009-08-28 09:38:18 -0700288 angle += columnGutterAngle + iconWidthAngle;
289 icon++;
290 }
291 }
292}
293
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700294int
Joe Onoratod769a632009-08-11 17:09:02 -0700295main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700296{
Jason Samsfd22dac2009-09-20 17:24:16 -0700297 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700298 int newTime = uptimeMillis();
299 g_DT = (newTime - g_LastTime) / 1000.f;
300 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700301
Joe Onorato26646342009-09-23 15:15:48 -0700302 //debugF("zoom", g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700303 if (g_Zoom != g_ZoomTarget) {
304 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
305 if (dz && (fabsf(dz) < 0.03f)) {
306 if (dz > 0) {
307 dz = 0.03f;
308 } else {
309 dz = -0.03f;
310 }
311 }
312 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
313 g_Zoom = g_ZoomTarget;
314 } else {
315 g_Zoom += dz;
316 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700317 readback->zoom = g_Zoom;
Joe Onorato006b25f2009-09-03 11:38:43 -0700318 }
319
Jason Samsfd22dac2009-09-20 17:24:16 -0700320 // Set clear value to dim the background based on the zoom position.
Joe Onorato7bb17492009-09-24 17:51:01 -0700321 if (g_Zoom < 0.001f) {
322 pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
323 // Nothing else to do if fully zoomed out.
324 g_PosPage = roundf(g_PosPage);
325 return 1; // 0;
326 } else if (g_Zoom < 0.8f) {
Jason Samsfd22dac2009-09-20 17:24:16 -0700327 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
Jason Samsfd22dac2009-09-20 17:24:16 -0700328 } else {
329 pfClearColor(0.0f, 0.0f, 0.0f, 0.80f);
330 }
331
332
333
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700334 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700335 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700336 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700337
Jason Sams86c87ed2009-09-18 13:55:55 -0700338 updatePos(0.1f);
Joe Onorato7bb17492009-09-24 17:51:01 -0700339 readback->posX = g_PosPage;
340 readback->velocity = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700341
Jason Sams86c87ed2009-09-18 13:55:55 -0700342 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700343
Joe Onoratoc567acb2009-08-31 14:34:43 -0700344 // Draw the icons ========================================
345 bindProgramVertex(NAMED_PV);
Joe Onoratoefabe002009-08-28 09:38:18 -0700346 bindProgramFragment(NAMED_PF);
347 bindProgramFragmentStore(NAMED_PFS);
348
349 // Bug makes 1.0f alpha fail.
350 color(1.0f, 1.0f, 1.0f, 0.99f);
351
352 int lastIcon = iconCount-1;
353
Jason Sams86c87ed2009-09-18 13:55:55 -0700354 int page = g_PosPage;
355 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700356
Joe Onoratod769a632009-08-11 17:09:02 -0700357 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700358 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700359
Jason Samsfd22dac2009-09-20 17:24:16 -0700360 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700361
Jason Samsfd22dac2009-09-20 17:24:16 -0700362 float pageAngle = VIEW_ANGLE * 1.2f;
363 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
364 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700365
Joe Onorato6665c0f2009-09-02 15:27:24 -0700366
367 // Draw the scroll handle ========================================
368 /*
Joe Onoratoc567acb2009-08-31 14:34:43 -0700369 bindTexture(NAMED_PFText, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
370 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
371 float handleTop = 680;
372 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
373 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
374 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
375 */
Joe Onorato93839052009-08-06 20:34:32 -0700376
Jason Sams86c87ed2009-09-18 13:55:55 -0700377 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700378 // So we keep rendering until the bug is fixed.
Joe Onorato7bb17492009-09-24 17:51:01 -0700379 return 1; //(g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != g_ZoomTarget);
Joe Onorato93839052009-08-06 20:34:32 -0700380}
381