allapps.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #pragma version(1)
  2. #pragma stateVertex(PV)
  3. #pragma stateFragment(PFTexNearest)
  4. #pragma stateStore(PSIcons)
  5. #define PI 3.14159f
  6. int g_SpecialHWWar;
  7. // Attraction to center values from page edge to page center.
  8. float g_AttractionTable[9];
  9. float g_FrictionTable[9];
  10. float g_PhysicsTableSize;
  11. float g_PosPage;
  12. float g_PosVelocity;
  13. float g_LastPositionX;
  14. int g_LastTouchDown;
  15. float g_DT;
  16. int g_LastTime;
  17. int g_PosMax;
  18. float g_Zoom;
  19. float g_Animation;
  20. float g_OldPosPage;
  21. float g_OldPosVelocity;
  22. float g_OldZoom;
  23. float g_MoveToTotalTime;
  24. float g_MoveToTime;
  25. float g_MoveToOldPos;
  26. int g_Cols;
  27. int g_Rows;
  28. // Drawing constants, should be parameters ======
  29. #define VIEW_ANGLE 1.28700222f
  30. int g_DrawLastFrame;
  31. int lastFrame(int draw) {
  32. // We draw one extra frame to work around the last frame post bug.
  33. // We also need to track if we drew the last frame to deal with large DT
  34. // in the physics.
  35. int ret = g_DrawLastFrame | draw;
  36. g_DrawLastFrame = draw;
  37. return ret; // should return draw instead.
  38. }
  39. void updateReadback() {
  40. if ((g_OldPosPage != g_PosPage) ||
  41. (g_OldPosVelocity != g_PosVelocity) ||
  42. (g_OldZoom != g_Zoom)) {
  43. g_OldPosPage = g_PosPage;
  44. g_OldPosVelocity = g_PosVelocity;
  45. g_OldZoom = g_Zoom;
  46. int i[3];
  47. i[0] = g_PosPage * (1 << 16);
  48. i[1] = g_PosVelocity * (1 << 16);
  49. i[2] = g_OldZoom * (1 << 16);
  50. sendToClient(&i[0], 1, 12, 1);
  51. }
  52. }
  53. void setColor(float r, float g, float b, float a) {
  54. if (g_SpecialHWWar) {
  55. color(0, 0, 0, 0.001f);
  56. } else {
  57. color(r, g, b, a);
  58. }
  59. }
  60. void init() {
  61. g_AttractionTable[0] = 20.0f;
  62. g_AttractionTable[1] = 20.0f;
  63. g_AttractionTable[2] = 20.0f;
  64. g_AttractionTable[3] = 10.0f;
  65. g_AttractionTable[4] = -10.0f;
  66. g_AttractionTable[5] = -20.0f;
  67. g_AttractionTable[6] = -20.0f;
  68. g_AttractionTable[7] = -20.0f;
  69. g_AttractionTable[8] = -20.0f; // dup 7 to avoid a clamp later
  70. g_FrictionTable[0] = 10.0f;
  71. g_FrictionTable[1] = 10.0f;
  72. g_FrictionTable[2] = 11.0f;
  73. g_FrictionTable[3] = 15.0f;
  74. g_FrictionTable[4] = 15.0f;
  75. g_FrictionTable[5] = 11.0f;
  76. g_FrictionTable[6] = 10.0f;
  77. g_FrictionTable[7] = 10.0f;
  78. g_FrictionTable[8] = 10.0f; // dup 7 to avoid a clamp later
  79. g_PhysicsTableSize = 7;
  80. g_PosVelocity = 0;
  81. g_PosPage = 0;
  82. g_LastTouchDown = 0;
  83. g_LastPositionX = 0;
  84. g_Zoom = 0;
  85. g_Animation = 1.f;
  86. g_SpecialHWWar = 1;
  87. g_MoveToTime = 0;
  88. g_MoveToOldPos = 0;
  89. g_MoveToTotalTime = 0.2f; // Duration of scrolling 1 line
  90. }
  91. void resetHWWar() {
  92. g_SpecialHWWar = 1;
  93. }
  94. void move() {
  95. if (g_LastTouchDown) {
  96. float dx = -(state->newPositionX - g_LastPositionX);
  97. g_PosVelocity = 0;
  98. g_PosPage += dx * 5.2f;
  99. float pmin = -0.49f;
  100. float pmax = g_PosMax + 0.49f;
  101. g_PosPage = clampf(g_PosPage, pmin, pmax);
  102. }
  103. g_LastTouchDown = state->newTouchDown;
  104. g_LastPositionX = state->newPositionX;
  105. g_MoveToTime = 0;
  106. //debugF("Move P", g_PosPage);
  107. }
  108. void moveTo() {
  109. g_MoveToTime = g_MoveToTotalTime;
  110. g_PosVelocity = 0;
  111. g_MoveToOldPos = g_PosPage;
  112. // debugF("======= moveTo", state->targetPos);
  113. }
  114. void setZoom() {
  115. g_Zoom = state->zoomTarget;
  116. g_DrawLastFrame = 1;
  117. updateReadback();
  118. }
  119. void fling() {
  120. g_LastTouchDown = 0;
  121. g_PosVelocity = -state->flingVelocity * 4;
  122. float av = fabsf(g_PosVelocity);
  123. float minVel = 3.5f;
  124. minVel *= 1.f - (fabsf(fracf(g_PosPage + 0.5f) - 0.5f) * 0.45f);
  125. if (av < minVel && av > 0.2f) {
  126. if (g_PosVelocity > 0) {
  127. g_PosVelocity = minVel;
  128. } else {
  129. g_PosVelocity = -minVel;
  130. }
  131. }
  132. if (g_PosPage <= 0) {
  133. g_PosVelocity = maxf(0, g_PosVelocity);
  134. }
  135. if (g_PosPage > g_PosMax) {
  136. g_PosVelocity = minf(0, g_PosVelocity);
  137. }
  138. }
  139. float
  140. modf(float x, float y)
  141. {
  142. return x-(y*floorf(x/y));
  143. }
  144. /*
  145. * Interpolates values in the range 0..1 to a curve that eases in
  146. * and out.
  147. */
  148. float
  149. getInterpolation(float input) {
  150. return (cosf((input + 1) * PI) / 2.0f) + 0.5f;
  151. }
  152. void updatePos() {
  153. if (g_LastTouchDown) {
  154. return;
  155. }
  156. float tablePosNorm = fracf(g_PosPage + 0.5f);
  157. float tablePosF = tablePosNorm * g_PhysicsTableSize;
  158. int tablePosI = tablePosF;
  159. float tablePosFrac = tablePosF - tablePosI;
  160. float accel = lerpf(g_AttractionTable[tablePosI],
  161. g_AttractionTable[tablePosI + 1],
  162. tablePosFrac) * g_DT;
  163. float friction = lerpf(g_FrictionTable[tablePosI],
  164. g_FrictionTable[tablePosI + 1],
  165. tablePosFrac) * g_DT;
  166. if (g_MoveToTime) {
  167. // New position is old posiition + (total distance) * (interpolated time)
  168. g_PosPage = g_MoveToOldPos + (state->targetPos - g_MoveToOldPos) * getInterpolation((g_MoveToTotalTime - g_MoveToTime) / g_MoveToTotalTime);
  169. g_MoveToTime -= g_DT;
  170. if (g_MoveToTime <= 0) {
  171. g_MoveToTime = 0;
  172. g_PosPage = state->targetPos;
  173. }
  174. return;
  175. }
  176. // If our velocity is low OR acceleration is opposing it, apply it.
  177. if (fabsf(g_PosVelocity) < 4.0f || (g_PosVelocity * accel) < 0) {
  178. g_PosVelocity += accel;
  179. }
  180. //debugF("g_PosPage", g_PosPage);
  181. //debugF(" g_PosVelocity", g_PosVelocity);
  182. //debugF(" friction", friction);
  183. //debugF(" accel", accel);
  184. // Normal physics
  185. if (g_PosVelocity > 0) {
  186. g_PosVelocity -= friction;
  187. g_PosVelocity = maxf(g_PosVelocity, 0);
  188. } else {
  189. g_PosVelocity += friction;
  190. g_PosVelocity = minf(g_PosVelocity, 0);
  191. }
  192. if ((friction > fabsf(g_PosVelocity)) && (friction > fabsf(accel))) {
  193. // Special get back to center and overcome friction physics.
  194. float t = tablePosNorm - 0.5f;
  195. if (fabsf(t) < (friction * g_DT)) {
  196. // really close, just snap
  197. g_PosPage = roundf(g_PosPage);
  198. g_PosVelocity = 0;
  199. } else {
  200. if (t > 0) {
  201. g_PosVelocity = -friction;
  202. } else {
  203. g_PosVelocity = friction;
  204. }
  205. }
  206. }
  207. // Check for out of boundry conditions.
  208. if (g_PosPage < 0 && g_PosVelocity < 0) {
  209. float damp = 1.0 + (g_PosPage * 4);
  210. damp = clampf(damp, 0.f, 0.9f);
  211. g_PosVelocity *= damp;
  212. }
  213. if (g_PosPage > g_PosMax && g_PosVelocity > 0) {
  214. float damp = 1.0 - ((g_PosPage - g_PosMax) * 4);
  215. damp = clampf(damp, 0.f, 0.9f);
  216. g_PosVelocity *= damp;
  217. }
  218. g_PosPage += g_PosVelocity * g_DT;
  219. g_PosPage = clampf(g_PosPage, -0.49, g_PosMax + 0.49);
  220. }
  221. void
  222. draw_home_button()
  223. {
  224. setColor(1.0f, 1.0f, 1.0f, 1.0f);
  225. bindTexture(NAMED_PFTexNearest, 0, state->homeButtonId);
  226. float w = getWidth();
  227. float h = getHeight();
  228. float x;
  229. float y;
  230. if (getWidth() > getHeight()) {
  231. x = w - (params->homeButtonTextureWidth * (1 - g_Animation)) + 20;
  232. y = (h - params->homeButtonTextureHeight) * 0.5f;
  233. } else {
  234. x = (w - params->homeButtonTextureWidth) / 2;
  235. y = -g_Animation * params->homeButtonTextureHeight;
  236. y -= 30; // move the house to the edge of the screen as it doesn't fill the texture.
  237. }
  238. drawSpriteScreenspace(x, y, 0, params->homeButtonTextureWidth, params->homeButtonTextureHeight);
  239. }
  240. void drawFrontGrid(float rowOffset, float p)
  241. {
  242. float h = getHeight();
  243. float w = getWidth();
  244. int intRowOffset = rowOffset;
  245. float rowFrac = rowOffset - intRowOffset;
  246. float colWidth = 120.f;//getWidth() / 4;
  247. float rowHeight = colWidth + 25.f;
  248. float yoff = 0.5f * h + 1.5f * rowHeight;
  249. int row, col;
  250. int colCount = 4;
  251. if (w > h) {
  252. colCount = 6;
  253. rowHeight -= 12.f;
  254. yoff = 0.47f * h + 1.0f * rowHeight;
  255. }
  256. int iconNum = (intRowOffset - 5) * colCount;
  257. bindProgramVertex(NAMED_PVCurve);
  258. vpConstants->Position.z = p;
  259. setColor(1.0f, 1.0f, 1.0f, 1.0f);
  260. for (row = -5; row < 15; row++) {
  261. float y = yoff - ((-rowFrac + row) * rowHeight);
  262. for (col=0; col < colCount; col++) {
  263. if (iconNum >= state->iconCount) {
  264. return;
  265. }
  266. if (iconNum >= 0) {
  267. float x = colWidth * col + (colWidth / 2);
  268. vpConstants->Position.x = x + 0.2f;
  269. if (state->selectedIconIndex == iconNum && !p) {
  270. bindProgramFragment(NAMED_PFTexNearest);
  271. bindTexture(NAMED_PFTexNearest, 0, state->selectedIconTexture);
  272. vpConstants->ImgSize.x = SELECTION_TEXTURE_WIDTH_PX;
  273. vpConstants->ImgSize.y = SELECTION_TEXTURE_HEIGHT_PX;
  274. vpConstants->Position.y = y - (SELECTION_TEXTURE_HEIGHT_PX - ICON_TEXTURE_HEIGHT_PX) * 0.5f;
  275. drawSimpleMesh(NAMED_SMCell);
  276. }
  277. bindProgramFragment(NAMED_PFTexMip);
  278. vpConstants->ImgSize.x = ICON_TEXTURE_WIDTH_PX;
  279. vpConstants->ImgSize.y = ICON_TEXTURE_HEIGHT_PX;
  280. vpConstants->Position.y = y - 0.2f;
  281. bindTexture(NAMED_PFTexMip, 0, loadI32(ALLOC_ICON_IDS, iconNum));
  282. drawSimpleMesh(NAMED_SMCell);
  283. bindProgramFragment(NAMED_PFTexMipAlpha);
  284. vpConstants->ImgSize.x = 120.f;
  285. vpConstants->ImgSize.y = 64.f;
  286. vpConstants->Position.y = y - 64.f - 0.2f;
  287. bindTexture(NAMED_PFTexMipAlpha, 0, loadI32(ALLOC_LABEL_IDS, iconNum));
  288. drawSimpleMesh(NAMED_SMCell);
  289. }
  290. iconNum++;
  291. }
  292. }
  293. }
  294. int
  295. main(int launchID)
  296. {
  297. // Compute dt in seconds.
  298. int newTime = uptimeMillis();
  299. g_DT = (newTime - g_LastTime) / 1000.f;
  300. g_LastTime = newTime;
  301. if (!g_DrawLastFrame) {
  302. // If we stopped rendering we cannot use DT.
  303. // assume 30fps in this case.
  304. g_DT = 0.033f;
  305. }
  306. // physics may break if DT is large.
  307. g_DT = minf(g_DT, 0.2f);
  308. if (g_Zoom != state->zoomTarget) {
  309. float dz = g_DT * 1.7f;
  310. if (state->zoomTarget < 0.5f) {
  311. dz = -dz;
  312. }
  313. if (fabsf(g_Zoom - state->zoomTarget) < fabsf(dz)) {
  314. g_Zoom = state->zoomTarget;
  315. } else {
  316. g_Zoom += dz;
  317. }
  318. updateReadback();
  319. }
  320. g_Animation = powf(1-g_Zoom, 3);
  321. // Set clear value to dim the background based on the zoom position.
  322. if ((g_Zoom < 0.001f) && (state->zoomTarget < 0.001f) && !g_SpecialHWWar) {
  323. pfClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  324. // When we're zoomed out and not tracking motion events, reset the pos to 0.
  325. if (!g_LastTouchDown) {
  326. g_PosPage = 0;
  327. }
  328. return lastFrame(0);
  329. } else {
  330. pfClearColor(0.0f, 0.0f, 0.0f, g_Zoom);
  331. }
  332. // icons & labels
  333. int iconCount = state->iconCount;
  334. if (getWidth() > getHeight()) {
  335. g_Cols = 6;
  336. g_Rows = 3;
  337. } else {
  338. g_Cols = 4;
  339. g_Rows = 4;
  340. }
  341. g_PosMax = ((iconCount + (g_Cols-1)) / g_Cols) - g_Rows;
  342. if (g_PosMax < 0) g_PosMax = 0;
  343. updatePos();
  344. updateReadback();
  345. //debugF(" draw g_PosPage", g_PosPage);
  346. // Draw the icons ========================================
  347. drawFrontGrid(g_PosPage, g_Animation);
  348. bindProgramFragment(NAMED_PFTexNearest);
  349. draw_home_button();
  350. // This is a WAR to do a rendering pass without drawing during init to
  351. // force the driver to preload and compile its shaders.
  352. // Without this the first animation does not appear due to the time it
  353. // takes to init the driver state.
  354. if (g_SpecialHWWar) {
  355. g_SpecialHWWar = 0;
  356. return 1;
  357. }
  358. // Bug workaround where the last frame is not always displayed
  359. // So we keep rendering until the bug is fixed.
  360. return lastFrame((g_PosVelocity != 0) || fracf(g_PosPage) || g_Zoom != state->zoomTarget || (g_MoveToTime != 0));
  361. }