blob: 5dcb16c66ee454329a2258daae1fd25bcbc0b5ae [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 -070095
96void setZoomTarget() {
97 g_ZoomTarget = state->zoom;
98 //debugF("zoom target", g_ZoomTarget);
Joe Onoratofb0ca672009-09-14 17:55:46 -040099}
100
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700101int
102count_pages(int iconCount)
Joe Onorato93839052009-08-06 20:34:32 -0700103{
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700104 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
105 int pages = iconCount / iconsPerPage;
106 if (pages*iconsPerPage != iconCount) {
Joe Onoratod769a632009-08-11 17:09:02 -0700107 pages++;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700108 }
Joe Onoratod769a632009-08-11 17:09:02 -0700109 return pages;
110}
111
Joe Onoratod769a632009-08-11 17:09:02 -0700112float
113modf(float x, float y)
114{
115 return x-(y*floorf(x/y));
Joe Onorato93839052009-08-06 20:34:32 -0700116}
117
Jason Sams86c87ed2009-09-18 13:55:55 -0700118void updatePos() {
119 if (g_LastTouchDown) {
120 return;
121 }
122
123 //debugF("g_PosPage", g_PosPage);
124 //debugF(" g_PosVelocity", g_PosVelocity);
125
126 float tablePosNorm = fracf(g_PosPage + 0.5f);
127 float tablePosF = tablePosNorm * g_PhysicsTableSize;
128 int tablePosI = tablePosF;
129 float tablePosFrac = tablePosF - tablePosI;
130 //debugF("tablePosNorm", tablePosNorm);
131 //debugF("tablePosF", tablePosF);
132 //debugF("tablePosI", tablePosI);
133 //debugF("tablePosFrac", tablePosFrac);
134
135 float accel = lerpf(g_AttractionTable[tablePosI],
136 g_AttractionTable[tablePosI + 1],
137 tablePosFrac) * g_DT;
138 float friction = lerpf(g_FrictionTable[tablePosI],
139 g_FrictionTable[tablePosI + 1],
140 tablePosFrac) * g_DT;
141 //debugF(" accel", accel);
142 //debugF(" friction", friction);
143
Jason Samsfd22dac2009-09-20 17:24:16 -0700144 // If our velocity is low OR acceleration is opposing it, apply it.
145 if (fabsf(g_PosVelocity) < 0.5f || (g_PosVelocity * accel) < 0) {
146 g_PosVelocity += accel;
147 }
148
Jason Sams86c87ed2009-09-18 13:55:55 -0700149 if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
150 // Special get back to center and overcome friction physics.
151 float t = tablePosNorm - 0.5f;
152 if (fabsf(t) < (friction * g_DT)) {
153 // really close, just snap
154 g_PosPage = roundf(g_PosPage);
155 g_PosVelocity = 0;
156 } else {
157 if (t > 0) {
158 g_PosVelocity = -friction;
159 } else {
160 g_PosVelocity = friction;
161 }
162 }
163 } else {
164 // Normal physics
165 if (g_PosVelocity > 0) {
166 g_PosVelocity -= friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700167 g_PosVelocity = maxf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700168 } else {
169 g_PosVelocity += friction;
Jason Samsfd22dac2009-09-20 17:24:16 -0700170 g_PosVelocity = minf(g_PosVelocity, 0);
Jason Sams86c87ed2009-09-18 13:55:55 -0700171 }
172 }
173 g_PosPage += g_PosVelocity * g_DT;
174
175 // Check for out of boundry conditions.
176 if (g_PosPage < 0 && g_PosVelocity < 0) {
177 float damp = 1.0 + (g_PosPage * 3);
178 damp = clampf(damp, 0.f, 0.9f);
179 g_PosVelocity *= damp;
180 }
181 if (g_PosPage > (g_PageCount-1) && g_PosVelocity > 0) {
182 float damp = 1.0 - ((g_PosPage - g_PageCount + 1) * 3);
183 damp = clampf(damp, 0.f, 0.9f);
184 g_PosVelocity *= damp;
185 }
186}
187
Joe Onorato0d1c5632009-08-28 15:57:18 -0700188float
189far_size(float sizeAt0)
190{
191 return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z
192}
193
Joe Onoratoefabe002009-08-28 09:38:18 -0700194void
Jason Samsfd22dac2009-09-20 17:24:16 -0700195draw_page(int icon, int lastIcon, float centerAngle, float scale)
Joe Onoratoefabe002009-08-28 09:38:18 -0700196{
197 int row;
198 int col;
199
Jason Sams86c87ed2009-09-18 13:55:55 -0700200 //debugF("center angle", centerAngle);
Joe Onorato85a02a82009-09-08 12:34:22 -0700201
Joe Onoratoefabe002009-08-28 09:38:18 -0700202 float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX;
203 float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX;
204
205 float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX;
Jason Samsfd22dac2009-09-20 17:24:16 -0700206 float columnGutterAngle = iconWidthAngle * 0.9f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700207
Joe Onorato6665c0f2009-09-02 15:27:24 -0700208 float farIconSize = FAR_ICON_SIZE;
Jason Samsfd22dac2009-09-20 17:24:16 -0700209 float iconGutterHeight = farIconSize * 1.3f;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700210
Joe Onorato6665c0f2009-09-02 15:27:24 -0700211 float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX);
212
Jason Sams78aebd82009-09-15 13:06:59 -0700213 float normalizedLabelWidth = 2 * params->bubbleWidth / (float)SCREEN_WIDTH_PX;
Joe Onorato0d1c5632009-08-28 15:57:18 -0700214 float farLabelWidth = far_size(normalizedLabelWidth);
Jason Sams78aebd82009-09-15 13:06:59 -0700215 float farLabelHeight = far_size(params->bubbleHeight * (normalizedLabelWidth / params->bubbleWidth));
216 float labelTextureWidth = (float)params->bubbleWidth / params->bubbleBitmapWidth;
217 float labelTextureHeight = (float)params->bubbleHeight / params->bubbleBitmapHeight;
Joe Onoratoefabe002009-08-28 09:38:18 -0700218
219 for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) {
220 float angle = centerAngle;
221 angle -= (columnGutterAngle + iconWidthAngle) * 1.5f;
222
Jason Samsfd22dac2009-09-20 17:24:16 -0700223 float iconTop = (farIconSize + iconGutterHeight) * (1.85f + ICON_TOP_OFFSET)
Joe Onorato0d1c5632009-08-28 15:57:18 -0700224 - row * (farIconSize + iconGutterHeight);
Joe Onoratoefabe002009-08-28 09:38:18 -0700225 float iconBottom = iconTop - farIconSize;
226
Joe Onorato0d1c5632009-08-28 15:57:18 -0700227 float labelTop = iconBottom - (.1 * farLabelHeight);
228 float labelBottom = labelTop - farLabelHeight;
229
Joe Onorato6665c0f2009-09-02 15:27:24 -0700230 float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize));
231 float iconTextureBottom = iconTextureTop - farIconTextureSize;
232
Joe Onoratoefabe002009-08-28 09:38:18 -0700233 for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) {
234 // icon
235 float sine = sinf(angle);
236 float cosine = cosf(angle);
237
Joe Onorato0d1c5632009-08-28 15:57:18 -0700238 float centerX = sine * RADIUS;
Jason Samsfd22dac2009-09-20 17:24:16 -0700239 float centerZ = cosine * RADIUS / scale;
240
241 if (scale > 1.f) {
242 centerX *= scale;
243 }
Joe Onorato0d1c5632009-08-28 15:57:18 -0700244
Joe Onorato6665c0f2009-09-02 15:27:24 -0700245 float iconLeftX = centerX - (cosine * farIconTextureSize * .5);
246 float iconRightX = centerX + (cosine * farIconTextureSize * .5);
247 float iconLeftZ = centerZ + (sine * farIconTextureSize * .5);
248 float iconRightZ = centerZ - (sine * farIconTextureSize * .5);
249
Jason Samsfd22dac2009-09-20 17:24:16 -0700250 color(1.0f, 1.0f, 1.0f, 0.99f);
Jason Sams78aebd82009-09-15 13:06:59 -0700251 if (state->selectedIconIndex == icon) {
252 bindTexture(NAMED_PF, 0, state->selectedIconTexture);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700253 drawQuadTexCoords(
254 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
255 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
256 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
257 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400258 } else {
259 bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon));
260 drawQuadTexCoords(
261 iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f,
262 iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f,
263 iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f,
264 iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700265 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700266
Joe Onoratoefabe002009-08-28 09:38:18 -0700267 // label
Jason Samsfd22dac2009-09-20 17:24:16 -0700268 if (scale < 1.2f) {
269 float a = maxf(scale, 1.0f);
270 a = (1.2f - a) * 5;
271 color(1.0f, 1.0f, 1.0f, a);
272
Joe Onorato85a02a82009-09-08 12:34:22 -0700273 float labelLeftX = centerX - farLabelWidth * 0.5f;
274 float labelRightX = centerX + farLabelWidth * 0.5f;
Joe Onoratoefabe002009-08-28 09:38:18 -0700275
Joe Onorato85a02a82009-09-08 12:34:22 -0700276 bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon));
277 drawQuadTexCoords(
278 labelLeftX, labelTop, centerZ, 0.0f, 0.0f,
279 labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f,
280 labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight,
281 labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight);
282 }
Joe Onoratoefabe002009-08-28 09:38:18 -0700283
Joe Onoratoefabe002009-08-28 09:38:18 -0700284 angle += columnGutterAngle + iconWidthAngle;
285 icon++;
286 }
287 }
288}
289
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700290int
Joe Onoratod769a632009-08-11 17:09:02 -0700291main(int launchID)
Joe Onorato93839052009-08-06 20:34:32 -0700292{
Jason Samsfd22dac2009-09-20 17:24:16 -0700293 // Compute dt in seconds.
Jason Sams86c87ed2009-09-18 13:55:55 -0700294 int newTime = uptimeMillis();
295 g_DT = (newTime - g_LastTime) / 1000.f;
296 g_LastTime = newTime;
Jason Sams86c87ed2009-09-18 13:55:55 -0700297
Jason Samsfd22dac2009-09-20 17:24:16 -0700298 debugF("zoom", g_Zoom);
299 if (g_Zoom != g_ZoomTarget) {
300 float dz = (g_ZoomTarget - g_Zoom) * g_DT * 5;
301 if (dz && (fabsf(dz) < 0.03f)) {
302 if (dz > 0) {
303 dz = 0.03f;
304 } else {
305 dz = -0.03f;
306 }
307 }
308 if (fabsf(g_Zoom - g_ZoomTarget) < fabsf(dz)) {
309 g_Zoom = g_ZoomTarget;
310 } else {
311 g_Zoom += dz;
312 }
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.
316 if (g_Zoom < 0.8f) {
317 pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
318 if (g_Zoom == 0) {
319 // Nothing else to do if fully zoomed out.
320 return 1;
321 }
322 } else {
323 pfClearColor(0.0f, 0.0f, 0.0f, 0.80f);
324 }
325
326
327
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700328 // icons & labels
Jason Sams78aebd82009-09-15 13:06:59 -0700329 int iconCount = state->iconCount;
Jason Sams86c87ed2009-09-18 13:55:55 -0700330 g_PageCount = count_pages(iconCount);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700331
Jason Sams86c87ed2009-09-18 13:55:55 -0700332 updatePos(0.1f);
333 state->readPosX = g_PosPage;
334 state->readVel = g_PosVelocity;
Joe Onoratod769a632009-08-11 17:09:02 -0700335
Jason Sams86c87ed2009-09-18 13:55:55 -0700336 //debugF(" draw g_PosPage", g_PosPage);
Joe Onoratod769a632009-08-11 17:09:02 -0700337
Joe Onoratoc567acb2009-08-31 14:34:43 -0700338 // Draw the icons ========================================
339 bindProgramVertex(NAMED_PV);
Joe Onoratoefabe002009-08-28 09:38:18 -0700340 bindProgramFragment(NAMED_PF);
341 bindProgramFragmentStore(NAMED_PFS);
342
343 // Bug makes 1.0f alpha fail.
344 color(1.0f, 1.0f, 1.0f, 0.99f);
345
346 int lastIcon = iconCount-1;
347
Jason Sams86c87ed2009-09-18 13:55:55 -0700348 int page = g_PosPage;
349 float currentPagePosition = g_PosPage - page;
Joe Onoratod769a632009-08-11 17:09:02 -0700350
Joe Onoratod769a632009-08-11 17:09:02 -0700351 int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE;
Joe Onoratoefabe002009-08-28 09:38:18 -0700352 int icon = clamp(iconsPerPage * page, 0, lastIcon);
Joe Onorato93839052009-08-06 20:34:32 -0700353
Jason Samsfd22dac2009-09-20 17:24:16 -0700354 float scale = (1 / g_Zoom);
Jason Sams78aebd82009-09-15 13:06:59 -0700355
Jason Samsfd22dac2009-09-20 17:24:16 -0700356 float pageAngle = VIEW_ANGLE * 1.2f;
357 draw_page(icon, lastIcon, -pageAngle*currentPagePosition, scale);
358 draw_page(icon+iconsPerPage, lastIcon, (-pageAngle*currentPagePosition)+pageAngle, scale);
Jason Sams86c87ed2009-09-18 13:55:55 -0700359
Joe Onorato6665c0f2009-09-02 15:27:24 -0700360
361 // Draw the scroll handle ========================================
362 /*
Joe Onoratoc567acb2009-08-31 14:34:43 -0700363 bindTexture(NAMED_PFText, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID));
364 float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx)));
365 float handleTop = 680;
366 float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH);
367 float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT);
368 drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f);
369 */
Joe Onorato93839052009-08-06 20:34:32 -0700370
Jason Sams86c87ed2009-09-18 13:55:55 -0700371 // Bug workaround where the last frame is not always displayed
Jason Samsfd22dac2009-09-20 17:24:16 -0700372 // So we keep rendering until the bug is fixed.
373 return 1;//(g_PosVelocity != 0) || fracf(g_PosPage);
Joe Onorato93839052009-08-06 20:34:32 -0700374}
375