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