Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1 | #pragma version(1) |
| 2 | #pragma stateVertex(PV) |
| 3 | #pragma stateFragment(PF) |
| 4 | #pragma stateFragmentStore(PFS) |
| 5 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 6 | #define PI 3.14159f |
| 7 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 8 | // Variables from java ====== |
| 9 | |
| 10 | // Parameters ====== |
| 11 | #define PARAM_BUBBLE_WIDTH 0 |
| 12 | #define PARAM_BUBBLE_HEIGHT 1 |
| 13 | #define PARAM_BUBBLE_BITMAP_WIDTH 2 |
| 14 | #define PARAM_BUBBLE_BITMAP_HEIGHT 3 |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 15 | #define PARAM_SCROLL_HANDLE_ID 4 |
| 16 | #define PARAM_SCROLL_HANDLE_TEX_WIDTH 5 |
| 17 | #define PARAM_SCROLL_HANDLE_TEX_HEIGHT 6 |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 19 | // State ====== |
| 20 | #define STATE_ICON_COUNT 0 |
| 21 | #define STATE_SCROLL_X 1 |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 22 | #define STATE_FLING_TIME 2 |
| 23 | #define STATE_FLING_VELOCITY_X 3 |
| 24 | #define STATE_ADJUSTED_DECELERATION 4 |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 25 | |
| 26 | /* with fling offset applied */ |
| 27 | #define STATE_CURRENT_SCROLL_X 5 |
| 28 | |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 29 | #define STATE_FLING_DURATION 6 |
| 30 | #define STATE_FLING_END_POS 7 |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 31 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 32 | #define STATE_START_SCROLL_X 8 |
| 33 | #define STATE_SELECTED_ICON_INDEX 9 |
| 34 | #define STATE_SELECTED_ICON_TEXTURE 10 |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 35 | |
Joe Onorato | 006b25f | 2009-09-03 11:38:43 -0700 | [diff] [blame] | 36 | #define STATE_VISIBLE 11 |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 37 | #define STATE_ZOOM 12 |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 38 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 39 | // Drawing constants, should be parameters ====== |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 40 | #define VIEW_ANGLE 1.28700222f |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 41 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 42 | int g_lastFrameTime = 0; |
| 43 | void print_frame_rate() |
| 44 | { |
| 45 | int now = uptimeMillis(); |
| 46 | if (g_lastFrameTime != 0) { |
| 47 | debugI32("frame_rate", 1000/(now-g_lastFrameTime)); |
| 48 | } |
| 49 | g_lastFrameTime = now; |
| 50 | } |
| 51 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 52 | int |
| 53 | count_pages(int iconCount) |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 54 | { |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 55 | int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE; |
| 56 | int pages = iconCount / iconsPerPage; |
| 57 | if (pages*iconsPerPage != iconCount) { |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 58 | pages++; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 59 | } |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 60 | return pages; |
| 61 | } |
| 62 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 63 | float |
| 64 | modf(float x, float y) |
| 65 | { |
| 66 | return x-(y*floorf(x/y)); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 69 | float |
| 70 | far_size(float sizeAt0) |
| 71 | { |
| 72 | return sizeAt0 * (RADIUS+2) / 2; // -2 is the camera z=(z-camZ)/z |
| 73 | } |
| 74 | |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 75 | void |
| 76 | draw_page(int icon, int lastIcon, float centerAngle) |
| 77 | { |
| 78 | int row; |
| 79 | int col; |
| 80 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 81 | float scale = 1.0f - loadI32(ALLOC_STATE, STATE_ZOOM)/100000.0f; |
| 82 | |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 83 | float iconTextureWidth = ICON_WIDTH_PX / (float)ICON_TEXTURE_WIDTH_PX; |
| 84 | float iconTextureHeight = ICON_HEIGHT_PX / (float)ICON_TEXTURE_HEIGHT_PX; |
| 85 | |
| 86 | float iconWidthAngle = VIEW_ANGLE * ICON_WIDTH_PX / SCREEN_WIDTH_PX; |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 87 | float columnGutterAngle = iconWidthAngle * 0.70f; |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 88 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 89 | float farIconSize = FAR_ICON_SIZE; |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 90 | float iconGutterHeight = farIconSize * 1.1f; |
| 91 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 92 | float farIconTextureSize = far_size(2 * ICON_TEXTURE_WIDTH_PX / (float)SCREEN_WIDTH_PX); |
| 93 | |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 94 | float labelWidthPx = loadI32(ALLOC_PARAMS, PARAM_BUBBLE_WIDTH); |
| 95 | float labelHeightPx = loadI32(ALLOC_PARAMS, PARAM_BUBBLE_HEIGHT); |
| 96 | |
| 97 | float normalizedLabelWidth = 2 * labelWidthPx / (float)SCREEN_WIDTH_PX; |
| 98 | float farLabelWidth = far_size(normalizedLabelWidth); |
| 99 | float farLabelHeight = far_size(labelHeightPx * (normalizedLabelWidth / labelWidthPx)); |
| 100 | float labelTextureWidth = labelWidthPx / loadI32(ALLOC_PARAMS, PARAM_BUBBLE_BITMAP_WIDTH); |
| 101 | float labelTextureHeight = labelHeightPx / loadI32(ALLOC_PARAMS, PARAM_BUBBLE_BITMAP_HEIGHT); |
| 102 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 103 | int selectedIconIndex = loadI32(ALLOC_STATE, STATE_SELECTED_ICON_INDEX); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 104 | |
| 105 | for (row=0; row<ROWS_PER_PAGE && icon<=lastIcon; row++) { |
| 106 | float angle = centerAngle; |
| 107 | angle -= (columnGutterAngle + iconWidthAngle) * 1.5f; |
| 108 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 109 | float iconTop = (farIconSize + iconGutterHeight) * (2.0f + ICON_TOP_OFFSET) |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 110 | - row * (farIconSize + iconGutterHeight); |
Joe Onorato | d40eec3 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 111 | iconTop -= 6 * scale; // make the zoom point below center |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 112 | float iconBottom = iconTop - farIconSize; |
| 113 | |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 114 | float labelTop = iconBottom - (.1 * farLabelHeight); |
| 115 | float labelBottom = labelTop - farLabelHeight; |
| 116 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 117 | float iconTextureTop = iconTop + (0.5f * (farIconTextureSize - farIconSize)); |
| 118 | float iconTextureBottom = iconTextureTop - farIconTextureSize; |
| 119 | |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 120 | for (col=0; col<COLUMNS_PER_PAGE && icon<=lastIcon; col++) { |
| 121 | // icon |
| 122 | float sine = sinf(angle); |
| 123 | float cosine = cosf(angle); |
| 124 | |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 125 | float centerX = sine * RADIUS; |
| 126 | float centerZ = cosine * RADIUS; |
Joe Onorato | d40eec3 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 127 | centerZ -= ((RADIUS+2+1)*scale); // 2 is camera loc, 1 put it slightly behind that. |
Joe Onorato | 0d1c563 | 2009-08-28 15:57:18 -0700 | [diff] [blame] | 128 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 129 | float iconLeftX = centerX - (cosine * farIconTextureSize * .5); |
| 130 | float iconRightX = centerX + (cosine * farIconTextureSize * .5); |
| 131 | float iconLeftZ = centerZ + (sine * farIconTextureSize * .5); |
| 132 | float iconRightZ = centerZ - (sine * farIconTextureSize * .5); |
| 133 | |
| 134 | if (selectedIconIndex == icon) { |
| 135 | bindTexture(NAMED_PF, 0, loadI32(ALLOC_STATE, STATE_SELECTED_ICON_TEXTURE)); |
| 136 | drawQuadTexCoords( |
| 137 | iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f, |
| 138 | iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f, |
| 139 | iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f, |
| 140 | iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f); |
| 141 | } |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 142 | |
| 143 | bindTexture(NAMED_PF, 0, loadI32(ALLOC_ICON_IDS, icon)); |
| 144 | drawQuadTexCoords( |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 145 | iconLeftX, iconTextureTop, iconLeftZ, 0.0f, 0.0f, |
| 146 | iconRightX, iconTextureTop, iconRightZ, 1.0f, 0.0f, |
| 147 | iconRightX, iconTextureBottom, iconRightZ, 1.0f, 1.0f, |
| 148 | iconLeftX, iconTextureBottom, iconLeftZ, 0.0f, 1.0f); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 149 | |
| 150 | // label |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 151 | if (scale <= 0.1f) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 152 | float labelLeftX = centerX - farLabelWidth * 0.5f; |
| 153 | float labelRightX = centerX + farLabelWidth * 0.5f; |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 154 | |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 155 | bindTexture(NAMED_PF, 0, loadI32(ALLOC_LABEL_IDS, icon)); |
| 156 | drawQuadTexCoords( |
| 157 | labelLeftX, labelTop, centerZ, 0.0f, 0.0f, |
| 158 | labelRightX, labelTop, centerZ, labelTextureWidth, 0.0f, |
| 159 | labelRightX, labelBottom, centerZ, labelTextureWidth, labelTextureHeight, |
| 160 | labelLeftX, labelBottom, centerZ, 0.0f, labelTextureHeight); |
| 161 | } |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 162 | |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 163 | angle += columnGutterAngle + iconWidthAngle; |
| 164 | icon++; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 169 | int |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 170 | main(int launchID) |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 171 | { |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 172 | // Clear to transparent |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 173 | pfClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 174 | |
Joe Onorato | 006b25f | 2009-09-03 11:38:43 -0700 | [diff] [blame] | 175 | // If we're not supposed to be showing, don't do anything. |
| 176 | if (!loadI32(ALLOC_STATE, STATE_VISIBLE)) { |
| 177 | return 0; |
| 178 | } |
| 179 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 180 | // icons & labels |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 181 | int iconCount = loadI32(ALLOC_STATE, STATE_ICON_COUNT); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 182 | int pageCount = count_pages(iconCount); |
| 183 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 184 | float scrollXPx = loadI32(ALLOC_STATE, STATE_SCROLL_X); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 185 | float maxScrollXPx = -(pageCount-1) * SCREEN_WIDTH_PX; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 186 | int done = 0; |
| 187 | |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 188 | // Clamp -- because java doesn't know how big the icons are |
| 189 | if (scrollXPx > 0) { |
| 190 | scrollXPx = 0; |
| 191 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 192 | if (scrollXPx < maxScrollXPx) { |
| 193 | scrollXPx = maxScrollXPx; |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 196 | // If we've been given a velocity, start a fling |
| 197 | float flingVelocityPxMs = loadI32(ALLOC_STATE, STATE_FLING_VELOCITY_X); |
| 198 | if (flingVelocityPxMs != 0) { |
| 199 | // how many screens will this velocity do? TODO: use long |
| 200 | // G * ppi * friction // why G? // friction = 0.015 |
| 201 | float deceleration = loadF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION); |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 202 | float flingDurationMs; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 203 | if (deceleration == 0) { |
| 204 | // On the first frame, calculate which animation we're going to do. If it's |
| 205 | // going to end up less than halfway into a page, we'll bounce back the previous |
| 206 | // page. Otherwise, we'll adjust the deceleration so it just makes it to the |
| 207 | // page boundary. |
| 208 | if (flingVelocityPxMs > 0) { |
| 209 | deceleration = -1000; |
| 210 | } else { |
| 211 | deceleration = 1000; |
| 212 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 213 | // minimum velocity |
| 214 | if (flingVelocityPxMs < 0) { |
| 215 | if (flingVelocityPxMs > -500) { |
| 216 | flingVelocityPxMs = -500; |
| 217 | } |
| 218 | } else { |
| 219 | if (flingVelocityPxMs < 500) { |
| 220 | flingVelocityPxMs = 500; |
| 221 | } |
| 222 | } |
| 223 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 224 | // v' = v + at --> t = -v / a |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 225 | // x' = x + vt + .5 a t^2 |
| 226 | flingDurationMs = - flingVelocityPxMs / deceleration; |
| 227 | float endPos = scrollXPx + (flingVelocityPxMs*flingDurationMs) |
| 228 | + ((deceleration*flingDurationMs*flingDurationMs)/2); |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 229 | |
| 230 | if (endPos > 0) { |
| 231 | endPos = 0; |
| 232 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 233 | if (endPos < maxScrollXPx) { |
| 234 | endPos = maxScrollXPx; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 235 | } |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 236 | float scrollOnPage = modf(endPos, SCREEN_WIDTH_PX); |
| 237 | int endPage = -endPos/SCREEN_WIDTH_PX; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 238 | |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 239 | if (flingVelocityPxMs < 0) { |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 240 | if (scrollOnPage < (SCREEN_WIDTH_PX/2)) { |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 241 | // adjust the deceleration so we align on the page boundary |
| 242 | // a = 2(x-x0-v0t)/t^2 |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 243 | endPos = -(endPage+1) * SCREEN_WIDTH_PX; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 244 | debugI32("endPos case 1", endPos); |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 245 | } else { |
| 246 | // TODO: bounce |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 247 | endPos = -(endPage+1) * SCREEN_WIDTH_PX; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 248 | debugI32("endPos case 2", endPos); |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 249 | } |
| 250 | } else { |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 251 | if (scrollOnPage >= (SCREEN_WIDTH_PX/2)) { |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 252 | // adjust the deceleration so we align on the page boundary |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 253 | endPos = -endPage * SCREEN_WIDTH_PX; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 254 | debugI32("endPos case 3", endPos); |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 255 | } else { |
| 256 | // TODO: bounce |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 257 | endPos = -endPage * SCREEN_WIDTH_PX; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 258 | debugI32("endPos case 4", endPos); |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | // v = v0 + at --> (v - v0) / t |
| 262 | deceleration = 2*(endPos-scrollXPx-(flingVelocityPxMs*flingDurationMs)) |
| 263 | / (flingDurationMs*flingDurationMs); |
| 264 | endPos = scrollXPx + (flingVelocityPxMs*flingDurationMs) |
| 265 | + ((deceleration*flingDurationMs*flingDurationMs)/2); |
| 266 | |
| 267 | storeF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION, deceleration); |
| 268 | storeF(ALLOC_STATE, STATE_FLING_DURATION, flingDurationMs); |
| 269 | storeF(ALLOC_STATE, STATE_FLING_END_POS, endPos); |
| 270 | } else { |
| 271 | flingDurationMs = loadF(ALLOC_STATE, STATE_FLING_DURATION); |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // adjust the deceleration so we always hit a page boundary |
| 275 | |
| 276 | int flingTime = loadI32(ALLOC_STATE, STATE_FLING_TIME); |
| 277 | int now = uptimeMillis(); |
| 278 | float elapsedTime = (now - flingTime) / 1000.0f; |
| 279 | int animEndTime = -flingVelocityPxMs / deceleration; |
| 280 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 281 | int flingOffsetPx = (flingVelocityPxMs * elapsedTime) |
| 282 | + (deceleration * elapsedTime * elapsedTime / 2.0f); |
| 283 | scrollXPx += flingOffsetPx; |
| 284 | |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 285 | if (elapsedTime > flingDurationMs) { |
| 286 | scrollXPx = loadF(ALLOC_STATE, STATE_FLING_END_POS); |
| 287 | done = 1; |
| 288 | } |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 289 | } else { |
| 290 | done = 1; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 293 | // Clamp |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 294 | if (scrollXPx > 0) { |
| 295 | scrollXPx = 0; |
| 296 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 297 | if (scrollXPx < maxScrollXPx) { |
| 298 | scrollXPx = maxScrollXPx; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | storeI32(ALLOC_STATE, STATE_CURRENT_SCROLL_X, scrollXPx); |
| 302 | if (done) { |
| 303 | storeI32(ALLOC_STATE, STATE_SCROLL_X, scrollXPx); |
| 304 | storeI32(ALLOC_STATE, STATE_FLING_VELOCITY_X, 0); |
| 305 | storeF(ALLOC_STATE, STATE_ADJUSTED_DECELERATION, 0); |
| 306 | } |
| 307 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 308 | // Draw the icons ======================================== |
| 309 | bindProgramVertex(NAMED_PV); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 310 | bindProgramFragment(NAMED_PF); |
| 311 | bindProgramFragmentStore(NAMED_PFS); |
| 312 | |
| 313 | // Bug makes 1.0f alpha fail. |
| 314 | color(1.0f, 1.0f, 1.0f, 0.99f); |
| 315 | |
| 316 | int lastIcon = iconCount-1; |
| 317 | |
| 318 | float currentPage = -scrollXPx / (float)SCREEN_WIDTH_PX; |
| 319 | int page = currentPage; |
| 320 | float currentPagePosition = currentPage - page; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 321 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 322 | int iconsPerPage = COLUMNS_PER_PAGE * ROWS_PER_PAGE; |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 323 | int icon = clamp(iconsPerPage * page, 0, lastIcon); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 324 | |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 325 | draw_page(icon, lastIcon, -VIEW_ANGLE*currentPagePosition); |
| 326 | draw_page(icon+iconsPerPage, lastIcon, (-VIEW_ANGLE*currentPagePosition)+VIEW_ANGLE); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 327 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 328 | // Draw the border lines for debugging ======================================== |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 329 | /* |
| 330 | bindProgramVertex(NAMED_PVOrtho); |
| 331 | bindProgramFragment(NAMED_PFText); |
| 332 | bindProgramFragmentStore(NAMED_PFSText); |
| 333 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 334 | color(1.0f, 1.0f, 0.0f, 0.99f); |
| 335 | int i; |
| 336 | for (i=0; i<ROWS_PER_PAGE+1; i++) { |
| 337 | int y = loadI32(ALLOC_Y_BORDERS, i); |
| 338 | drawRect(0, y, SCREEN_WIDTH_PX, y+1, 0.0f); |
| 339 | } |
| 340 | for (i=0; i<COLUMNS_PER_PAGE+1; i++) { |
| 341 | int x = loadI32(ALLOC_X_BORDERS, i); |
| 342 | drawRect(x, 0, x+1, SCREEN_HEIGHT_PX, 0.0f); |
| 343 | } |
| 344 | */ |
| 345 | |
| 346 | // Draw the scroll handle ======================================== |
| 347 | /* |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 348 | bindTexture(NAMED_PFText, 0, loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_ID)); |
| 349 | float handleLeft = 40 + (320 * (scrollXPx/(float)(maxScrollXPx))); |
| 350 | float handleTop = 680; |
| 351 | float handleWidth = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_WIDTH); |
| 352 | float handleHeight = loadI32(ALLOC_PARAMS, PARAM_SCROLL_HANDLE_TEX_HEIGHT); |
| 353 | drawRect(handleLeft, handleTop, handleLeft+handleWidth, handleTop+handleHeight, 0.0f); |
| 354 | */ |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 355 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 356 | print_frame_rate(); |
| 357 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 358 | return !done; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 359 | } |
| 360 | |