Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 1 | #pragma version(1) |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 2 | |
| 3 | #include "../../../../../frameworks/base/libs/rs/scriptc/rs_types.rsh" |
| 4 | #include "../../../../../frameworks/base/libs/rs/scriptc/rs_math.rsh" |
| 5 | #include "../../../../../frameworks/base/libs/rs/scriptc/rs_graphics.rsh" |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 6 | |
| 7 | #define PI 3.14159f |
| 8 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 9 | // Constants from Java |
| 10 | int COLUMNS_PER_PAGE_PORTRAIT; |
| 11 | int ROWS_PER_PAGE_PORTRAIT; |
| 12 | int COLUMNS_PER_PAGE_LANDSCAPE; |
| 13 | int ROWS_PER_PAGE_LANDSCAPE; |
| 14 | |
| 15 | float gNewPositionX; |
| 16 | int gNewTouchDown; |
| 17 | float gFlingVelocity; |
| 18 | int gIconCount; |
| 19 | int gSelectedIconIndex; |
| 20 | rs_allocation gSelectedIconTexture; |
| 21 | float gZoomTarget; |
| 22 | rs_allocation gHomeButton; |
| 23 | float gTargetPos; |
| 24 | |
| 25 | rs_program_fragment gPFTexNearest; |
| 26 | rs_program_fragment gPFTexMip; |
| 27 | rs_program_fragment gPFTexMipAlpha; |
| 28 | rs_program_vertex gPVCurve; |
| 29 | rs_program_store gPS; |
| 30 | rs_mesh gSMCell; |
| 31 | |
| 32 | rs_allocation *gIconIDs; |
| 33 | rs_allocation *gLabelIDs; |
| 34 | |
| 35 | typedef struct VpConsts_s { |
| 36 | float4 Position; |
| 37 | float4 ScaleOffset; |
| 38 | float2 BendPos; |
| 39 | float2 ImgSize; |
| 40 | } VpConsts_t; |
| 41 | VpConsts_t *vpConstants; |
| 42 | |
| 43 | |
| 44 | #pragma rs export_var(COLUMNS_PER_PAGE_PORTRAIT, ROWS_PER_PAGE_PORTRAIT, COLUMNS_PER_PAGE_LANDSCAPE, ROWS_PER_PAGE_LANDSCAPE, gNewPositionX, gNewTouchDown, gFlingVelocity, gIconCount, gSelectedIconIndex, gSelectedIconTexture, gZoomTarget, gHomeButton, gTargetPos, gPFTexNearest, gPFTexMip, gPFTexMipAlpha, gPVCurve, gPS, gSMCell, gIconIDs, gLabelIDs, vpConstants) |
| 45 | #pragma rs export_func(resetHWWar, move, moveTo, setZoom, fling) |
| 46 | |
| 47 | |
| 48 | void debugAll() |
| 49 | { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 52 | |
| 53 | // Attraction to center values from page edge to page center. |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 54 | static float g_AttractionTable[9] = {20.f, 20.f, 20.f, 10.f, -10.f, -20.f, -20.f, -20.f, -20.f}; |
| 55 | static float g_FrictionTable[9] = {10.f, 10.f, 11.f, 15.f, 15.f, 11.f, 10.f, 10.f, 10.f}; |
| 56 | static float g_PhysicsTableSize = 7; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 57 | |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 58 | static float g_PosPage = 0.f; |
| 59 | static float g_PosVelocity = 0.f; |
| 60 | static float g_LastPositionX = 0.f; |
| 61 | static int g_LastTouchDown = 0; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 62 | static float g_DT; |
| 63 | static int g_LastTime; |
| 64 | static int g_PosMax; |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 65 | static float g_Zoom = 0.f; |
| 66 | static float g_Animation = 1.f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 67 | static float g_OldPosPage; |
| 68 | static float g_OldPosVelocity; |
| 69 | static float g_OldZoom; |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 70 | static float g_MoveToTotalTime = 0.2f; |
| 71 | static float g_MoveToTime = 0.f; |
| 72 | static float g_MoveToOldPos = 0.f; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 73 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 74 | static int g_Cols; |
| 75 | static int g_Rows; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 76 | |
| 77 | // Drawing constants, should be parameters ====== |
| 78 | #define VIEW_ANGLE 1.28700222f |
| 79 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 80 | static int g_DrawLastFrame; |
| 81 | static int lastFrame(int draw) { |
Jason Sams | 5c1417a | 2010-05-18 17:06:04 -0700 | [diff] [blame] | 82 | // We draw one extra frame to work around the last frame post bug. |
| 83 | // We also need to track if we drew the last frame to deal with large DT |
| 84 | // in the physics. |
| 85 | int ret = g_DrawLastFrame | draw; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 86 | g_DrawLastFrame = draw; |
Jason Sams | 5c1417a | 2010-05-18 17:06:04 -0700 | [diff] [blame] | 87 | return ret;//draw; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 90 | static void updateReadback() { |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 91 | if ((g_OldPosPage != g_PosPage) || |
| 92 | (g_OldPosVelocity != g_PosVelocity) || |
| 93 | (g_OldZoom != g_Zoom)) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 94 | |
| 95 | g_OldPosPage = g_PosPage; |
| 96 | g_OldPosVelocity = g_PosVelocity; |
| 97 | g_OldZoom = g_Zoom; |
| 98 | |
| 99 | int i[3]; |
| 100 | i[0] = g_PosPage * (1 << 16); |
| 101 | i[1] = g_PosVelocity * (1 << 16); |
| 102 | i[2] = g_OldZoom * (1 << 16); |
| 103 | sendToClient(&i[0], 1, 12, 1); |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 104 | } |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 107 | void setColor(float r, float g, float b, float a) { |
Jason Sams | 41b61c8 | 2009-10-15 15:40:54 -0700 | [diff] [blame] | 108 | } |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 109 | |
| 110 | void init() { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 111 | //debugPi(99, 10); |
Jason Sams | 41b61c8 | 2009-10-15 15:40:54 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void resetHWWar() { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void move() { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 118 | //debugPi(99, 8); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 119 | if (g_LastTouchDown) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 120 | float dx = -(gNewPositionX - g_LastPositionX); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 121 | g_PosVelocity = 0; |
Jason Sams | 6ec11bc | 2010-01-19 17:56:52 -0800 | [diff] [blame] | 122 | g_PosPage += dx * 5.2f; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 123 | |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 124 | float pmin = -0.49f; |
| 125 | float pmax = g_PosMax + 0.49f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 126 | g_PosPage = clamp(g_PosPage, pmin, pmax); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 127 | } |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 128 | g_LastTouchDown = gNewTouchDown; |
| 129 | g_LastPositionX = gNewPositionX; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 130 | g_MoveToTime = 0; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 131 | //debugF("Move P", g_PosPage); |
| 132 | } |
| 133 | |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 134 | void moveTo() { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 135 | //debugPi(99, 7); |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 136 | g_MoveToTime = g_MoveToTotalTime; |
| 137 | g_PosVelocity = 0; |
| 138 | g_MoveToOldPos = g_PosPage; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 139 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 140 | // debugF("======= moveTo", state->targetPos); |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 143 | void setZoom() { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 144 | //debugPi(99, 6); |
| 145 | g_Zoom = gZoomTarget; |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 146 | g_DrawLastFrame = 1; |
| 147 | updateReadback(); |
| 148 | } |
| 149 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 150 | void fling() { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 151 | //debugPi(99, 5); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 152 | g_LastTouchDown = 0; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 153 | g_PosVelocity = -gFlingVelocity * 4; |
| 154 | float av = fabs(g_PosVelocity); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 155 | float minVel = 3.5f; |
| 156 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 157 | minVel *= 1.f - (fabs(frac(g_PosPage + 0.5f) - 0.5f) * 0.45f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 158 | |
| 159 | if (av < minVel && av > 0.2f) { |
| 160 | if (g_PosVelocity > 0) { |
| 161 | g_PosVelocity = minVel; |
| 162 | } else { |
| 163 | g_PosVelocity = -minVel; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (g_PosPage <= 0) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 168 | g_PosVelocity = max(0.f, g_PosVelocity); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 169 | } |
| 170 | if (g_PosPage > g_PosMax) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 171 | g_PosVelocity = min(0.f, g_PosVelocity); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 175 | // Interpolates values in the range 0..1 to a curve that eases in |
| 176 | // and out. |
| 177 | static float getInterpolation(float input) { |
| 178 | //debugPi(99, 4); |
| 179 | return (cos((input + 1) * PI) * 0.5f) + 0.5f; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 182 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 183 | static void updatePos() { |
| 184 | //debugPi(99, 3); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 185 | if (g_LastTouchDown) { |
| 186 | return; |
| 187 | } |
| 188 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 189 | float tablePosNorm = frac(g_PosPage + 0.5f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 190 | float tablePosF = tablePosNorm * g_PhysicsTableSize; |
| 191 | int tablePosI = tablePosF; |
| 192 | float tablePosFrac = tablePosF - tablePosI; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 193 | float accel = mix(g_AttractionTable[tablePosI], |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 194 | g_AttractionTable[tablePosI + 1], |
| 195 | tablePosFrac) * g_DT; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 196 | float friction = mix(g_FrictionTable[tablePosI], |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 197 | g_FrictionTable[tablePosI + 1], |
| 198 | tablePosFrac) * g_DT; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 199 | |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 200 | if (g_MoveToTime) { |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 201 | // New position is old posiition + (total distance) * (interpolated time) |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 202 | g_PosPage = g_MoveToOldPos + (gTargetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime); |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 203 | g_MoveToTime -= g_DT; |
| 204 | if (g_MoveToTime <= 0) { |
| 205 | g_MoveToTime = 0; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 206 | g_PosPage = gTargetPos; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 207 | } |
| 208 | return; |
| 209 | } |
| 210 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 211 | // If our velocity is low OR acceleration is opposing it, apply it. |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 212 | if (fabs(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 213 | g_PosVelocity += accel; |
| 214 | } |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 215 | //debugF("g_PosPage", g_PosPage); |
| 216 | //debugF(" g_PosVelocity", g_PosVelocity); |
| 217 | //debugF(" friction", friction); |
| 218 | //debugF(" accel", accel); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 219 | |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 220 | // Normal physics |
| 221 | if (g_PosVelocity > 0) { |
| 222 | g_PosVelocity -= friction; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 223 | g_PosVelocity = max(g_PosVelocity, 0.f); |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 224 | } else { |
| 225 | g_PosVelocity += friction; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 226 | g_PosVelocity = min(g_PosVelocity, 0.f); |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 229 | if ((friction > fabs(g_PosVelocity)) && (friction > fabs(accel))) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 230 | // Special get back to center and overcome friction physics. |
| 231 | float t = tablePosNorm - 0.5f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 232 | if (fabs(t) < (friction * g_DT)) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 233 | // really close, just snap |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 234 | g_PosPage = round(g_PosPage); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 235 | g_PosVelocity = 0; |
| 236 | } else { |
| 237 | if (t > 0) { |
| 238 | g_PosVelocity = -friction; |
| 239 | } else { |
| 240 | g_PosVelocity = friction; |
| 241 | } |
| 242 | } |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 243 | } |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 244 | |
| 245 | // Check for out of boundry conditions. |
| 246 | if (g_PosPage < 0 && g_PosVelocity < 0) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 247 | float damp = 1.0f + (g_PosPage * 4); |
| 248 | damp = clamp(damp, 0.f, 0.9f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 249 | g_PosVelocity *= damp; |
| 250 | } |
| 251 | if (g_PosPage > g_PosMax && g_PosVelocity > 0) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 252 | float damp = 1.0f - ((g_PosPage - g_PosMax) * 4); |
| 253 | damp = clamp(damp, 0.f, 0.9f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 254 | g_PosVelocity *= damp; |
| 255 | } |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 256 | |
| 257 | g_PosPage += g_PosVelocity * g_DT; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 258 | g_PosPage = clamp(g_PosPage, -0.49f, g_PosMax + 0.49f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 261 | static void |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 262 | draw_home_button() |
| 263 | { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 264 | //debugPi(99, 2); |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 265 | color(1.0f, 1.0f, 1.0f, 1.0f); |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 266 | bindTexture(gPFTexNearest, 0, gHomeButton); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 267 | |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 268 | float w = getWidth(); |
| 269 | float h = getHeight(); |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 270 | float tw = allocGetDimX(gHomeButton); |
| 271 | float th = allocGetDimY(gHomeButton); |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 272 | |
| 273 | float x; |
| 274 | float y; |
| 275 | if (getWidth() > getHeight()) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 276 | x = w - (tw * (1 - g_Animation)) + 20; |
| 277 | y = (h - th) * 0.5f; |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 278 | } else { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 279 | x = (w - tw) / 2; |
| 280 | y = -g_Animation * th; |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 281 | y -= 30; // move the house to the edge of the screen as it doesn't fill the texture. |
| 282 | } |
| 283 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 284 | drawSpriteScreenspace(x, y, 0, tw, th); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 287 | static void drawFrontGrid(float rowOffset, float p) |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 288 | { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 289 | //debugPi(99, 1); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 290 | float h = getHeight(); |
| 291 | float w = getWidth(); |
| 292 | |
| 293 | int intRowOffset = rowOffset; |
| 294 | float rowFrac = rowOffset - intRowOffset; |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 295 | float colWidth = 120.f;//getWidth() / 4; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 296 | float rowHeight = colWidth + 25.f; |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 297 | float yoff = 0.5f * h + 1.5f * rowHeight; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 298 | |
| 299 | int row, col; |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 300 | int colCount = 4; |
| 301 | if (w > h) { |
| 302 | colCount = 6; |
| 303 | rowHeight -= 12.f; |
| 304 | yoff = 0.47f * h + 1.0f * rowHeight; |
| 305 | } |
| 306 | |
| 307 | int iconNum = (intRowOffset - 5) * colCount; |
| 308 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 309 | bindProgramVertex(gPVCurve); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 310 | |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 311 | vpConstants->Position.z = p; |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 312 | |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 313 | color(1.0f, 1.0f, 1.0f, 1.0f); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 314 | for (row = -5; row < 15; row++) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 315 | float y = yoff - ((-rowFrac + row) * rowHeight); |
| 316 | |
Jason Sams | 7651248 | 2010-02-04 16:31:35 -0800 | [diff] [blame] | 317 | for (col=0; col < colCount; col++) { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 318 | if (iconNum >= gIconCount) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (iconNum >= 0) { |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 323 | float x = colWidth * col + (colWidth / 2); |
Jason Sams | 6b08ffe | 2010-02-22 15:41:30 -0800 | [diff] [blame] | 324 | vpConstants->Position.x = x + 0.2f; |
Jason Sams | 1a94ee3 | 2010-01-20 13:34:30 -0800 | [diff] [blame] | 325 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 326 | if (gSelectedIconIndex == iconNum && !p && gSelectedIconTexture) { |
| 327 | bindProgramFragment(gPFTexNearest); |
| 328 | bindTexture(gPFTexNearest, 0, gSelectedIconTexture); |
| 329 | vpConstants->ImgSize.x = allocGetDimX(gSelectedIconTexture); |
| 330 | vpConstants->ImgSize.y = allocGetDimY(gSelectedIconTexture); |
| 331 | vpConstants->Position.y = y - (allocGetDimY(gSelectedIconTexture) - allocGetDimY(gIconIDs[iconNum])) * 0.5f; |
| 332 | drawSimpleMesh(gSMCell); |
Jason Sams | 37f262d | 2010-01-20 11:19:51 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 335 | bindProgramFragment(gPFTexMip); |
| 336 | vpConstants->ImgSize.x = allocGetDimX(gIconIDs[iconNum]); |
| 337 | vpConstants->ImgSize.y = allocGetDimY(gIconIDs[iconNum]); |
Jason Sams | 6b08ffe | 2010-02-22 15:41:30 -0800 | [diff] [blame] | 338 | vpConstants->Position.y = y - 0.2f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 339 | bindTexture(gPFTexMip, 0, gIconIDs[iconNum]); |
| 340 | drawSimpleMesh(gSMCell); |
Joe Onorato | 742d7fc | 2009-10-15 19:48:16 -0700 | [diff] [blame] | 341 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 342 | bindProgramFragment(gPFTexMipAlpha); |
| 343 | vpConstants->ImgSize.x = allocGetDimX(gLabelIDs[iconNum]); |
| 344 | vpConstants->ImgSize.y = allocGetDimY(gLabelIDs[iconNum]); |
Jason Sams | 6b08ffe | 2010-02-22 15:41:30 -0800 | [diff] [blame] | 345 | vpConstants->Position.y = y - 64.f - 0.2f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 346 | bindTexture(gPFTexMipAlpha, 0, gLabelIDs[iconNum]); |
| 347 | drawSimpleMesh(gSMCell); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 348 | } |
| 349 | iconNum++; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 354 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 355 | int root() |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 356 | { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 357 | //debugAll(); |
| 358 | bindProgramStore(gPS); |
| 359 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 360 | // Compute dt in seconds. |
| 361 | int newTime = uptimeMillis(); |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 362 | g_DT = (newTime - g_LastTime) * 0.001f; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 363 | g_LastTime = newTime; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 364 | |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 365 | if (!g_DrawLastFrame) { |
| 366 | // If we stopped rendering we cannot use DT. |
| 367 | // assume 30fps in this case. |
| 368 | g_DT = 0.033f; |
| 369 | } |
Jason Sams | b52dfa0 | 2009-10-14 20:16:14 -0700 | [diff] [blame] | 370 | // physics may break if DT is large. |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 371 | g_DT = min(g_DT, 0.2f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 372 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 373 | if (g_Zoom != gZoomTarget) { |
Jason Sams | 6471c8b | 2010-01-20 14:15:22 -0800 | [diff] [blame] | 374 | float dz = g_DT * 1.7f; |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 375 | if (gZoomTarget < 0.5f) { |
Jason Sams | 6471c8b | 2010-01-20 14:15:22 -0800 | [diff] [blame] | 376 | dz = -dz; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 377 | } |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 378 | if (fabs(g_Zoom - gZoomTarget) < fabs(dz)) { |
| 379 | g_Zoom = gZoomTarget; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 380 | } else { |
| 381 | g_Zoom += dz; |
| 382 | } |
| 383 | updateReadback(); |
| 384 | } |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 385 | g_Animation = pow(1.f - g_Zoom, 3.f); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 386 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 387 | //debugPf(100, g_Zoom); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 388 | // Set clear value to dim the background based on the zoom position. |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 389 | if ((g_Zoom < 0.001f) && (gZoomTarget < 0.001f)) { |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 390 | pfClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 391 | // When we're zoomed out and not tracking motion events, reset the pos to 0. |
| 392 | if (!g_LastTouchDown) { |
| 393 | g_PosPage = 0; |
| 394 | } |
| 395 | return lastFrame(0); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 396 | } else { |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 397 | //debugPf(101, g_Zoom); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 398 | pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom); |
| 399 | } |
| 400 | |
| 401 | // icons & labels |
Jason Sams | cc90349 | 2010-03-12 12:46:04 -0800 | [diff] [blame] | 402 | if (getWidth() > getHeight()) { |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 403 | g_Cols = COLUMNS_PER_PAGE_LANDSCAPE; |
| 404 | g_Rows = ROWS_PER_PAGE_LANDSCAPE; |
Joe Onorato | 20e7a56 | 2010-03-18 17:12:52 -0700 | [diff] [blame] | 405 | } else { |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 406 | g_Cols = COLUMNS_PER_PAGE_PORTRAIT; |
| 407 | g_Rows = ROWS_PER_PAGE_PORTRAIT; |
Jason Sams | cc90349 | 2010-03-12 12:46:04 -0800 | [diff] [blame] | 408 | } |
Jason Sams | ad1bdf0 | 2010-05-14 17:54:50 -0700 | [diff] [blame] | 409 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 410 | g_PosMax = ((gIconCount + (g_Cols-1)) / g_Cols) - g_Rows; |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 411 | if (g_PosMax < 0) g_PosMax = 0; |
| 412 | |
Jason Sams | cc90349 | 2010-03-12 12:46:04 -0800 | [diff] [blame] | 413 | updatePos(); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 414 | updateReadback(); |
| 415 | |
| 416 | //debugF(" draw g_PosPage", g_PosPage); |
| 417 | |
| 418 | // Draw the icons ======================================== |
Jason Sams | 6471c8b | 2010-01-20 14:15:22 -0800 | [diff] [blame] | 419 | drawFrontGrid(g_PosPage, g_Animation); |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 420 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 421 | bindProgramFragment(gPFTexNearest); |
Jason Sams | b52dfa0 | 2009-10-14 20:16:14 -0700 | [diff] [blame] | 422 | draw_home_button(); |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 423 | return lastFrame((g_PosVelocity != 0) || frac(g_PosPage) || g_Zoom != gZoomTarget || (g_MoveToTime != 0)); |
Jason Sams | 0f505e5 | 2009-10-13 17:18:35 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Jason Sams | 14f67ed | 2010-05-11 14:02:43 -0700 | [diff] [blame] | 426 | |