Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import java.io.Writer; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.concurrent.Semaphore; |
| 22 | import java.lang.Float; |
| 23 | |
| 24 | import android.renderscript.RSSurfaceView; |
| 25 | import android.renderscript.RenderScript; |
| 26 | |
| 27 | import android.renderscript.RenderScript; |
| 28 | import android.renderscript.ProgramVertex; |
| 29 | import android.renderscript.Element; |
| 30 | import android.renderscript.Allocation; |
| 31 | import android.renderscript.Script; |
| 32 | import android.renderscript.ScriptC; |
| 33 | import android.renderscript.ProgramFragment; |
| 34 | import android.renderscript.ProgramStore; |
| 35 | import android.renderscript.Sampler; |
| 36 | |
| 37 | import android.content.Context; |
| 38 | import android.content.res.Resources; |
| 39 | import android.graphics.Bitmap; |
| 40 | import android.graphics.BitmapFactory; |
| 41 | import android.graphics.Canvas; |
| 42 | import android.graphics.Paint; |
| 43 | import android.graphics.Typeface; |
| 44 | import android.graphics.drawable.BitmapDrawable; |
| 45 | import android.graphics.drawable.Drawable; |
| 46 | import android.os.Handler; |
| 47 | import android.os.Message; |
| 48 | import android.util.AttributeSet; |
| 49 | import android.util.Log; |
| 50 | import android.view.Surface; |
| 51 | import android.view.SurfaceHolder; |
| 52 | import android.view.SurfaceView; |
| 53 | import android.view.KeyEvent; |
| 54 | import android.view.MotionEvent; |
| 55 | import android.graphics.PixelFormat; |
| 56 | |
| 57 | |
| 58 | public class AllAppsView extends RSSurfaceView { |
| 59 | public AllAppsView(Context context) { |
| 60 | super(context); |
| 61 | setFocusable(true); |
| 62 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
| 63 | } |
| 64 | |
| 65 | public AllAppsView(Context context, AttributeSet attrs) { |
| 66 | this(context); |
| 67 | } |
| 68 | |
| 69 | public AllAppsView(Context context, AttributeSet attrs, int defStyle) { |
| 70 | this(context); |
| 71 | } |
| 72 | |
| 73 | private RenderScript mRS; |
| 74 | private RolloRS mRender; |
| 75 | |
| 76 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 77 | super.surfaceChanged(holder, format, w, h); |
| 78 | |
| 79 | mRS = createRenderScript(); |
| 80 | mRender = new RolloRS(); |
| 81 | mRender.init(mRS, getResources(), w, h); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean onKeyDown(int keyCode, KeyEvent event) |
| 86 | { |
| 87 | // break point at here |
| 88 | // this method doesn't work when 'extends View' include 'extends ScrollView'. |
| 89 | return super.onKeyDown(keyCode, event); |
| 90 | } |
| 91 | |
| 92 | boolean mControlMode = false; |
| 93 | boolean mZoomMode = false; |
| 94 | boolean mFlingMode = false; |
| 95 | float mFlingX = 0; |
| 96 | float mFlingY = 0; |
| 97 | float mColumn = -1; |
| 98 | float mOldColumn; |
| 99 | float mZoom = 1; |
| 100 | |
| 101 | int mIconCount = 29; |
| 102 | int mRows = 4; |
| 103 | int mColumns = (mIconCount + mRows - 1) / mRows; |
| 104 | |
| 105 | float mMaxZoom = ((float)mColumns) / 3.f; |
| 106 | |
| 107 | |
| 108 | void setColumn(boolean clamp) |
| 109 | { |
| 110 | //Log.e("rs", " col = " + Float.toString(mColumn)); |
| 111 | float c = mColumn; |
| 112 | if(c > (mColumns -2)) { |
| 113 | c = (mColumns -2); |
| 114 | } |
| 115 | if(c < 0) { |
| 116 | c = 0; |
| 117 | } |
| 118 | mRender.setPosition(c); |
| 119 | if(clamp) { |
| 120 | mColumn = c; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void computeSelection(float x, float y) |
| 125 | { |
| 126 | float col = mColumn + (x - 0.5f) * 4 + 1.25f; |
| 127 | int iCol = (int)(col + 0.25f); |
| 128 | |
| 129 | float row = (y / 0.8f) * mRows; |
| 130 | int iRow = (int)(row - 0.5f); |
| 131 | |
| 132 | mRender.setSelected(iCol * mRows + iRow); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean onTouchEvent(MotionEvent ev) |
| 137 | { |
| 138 | boolean ret = true; |
| 139 | int act = ev.getAction(); |
| 140 | if (act == ev.ACTION_UP) { |
| 141 | ret = false; |
| 142 | } |
| 143 | |
| 144 | float nx = ev.getX() / getWidth(); |
| 145 | float ny = ev.getY() / getHeight(); |
| 146 | |
| 147 | //Log.e("rs", "width=" + Float.toString(getWidth())); |
| 148 | //Log.e("rs", "height=" + Float.toString(getHeight())); |
| 149 | |
| 150 | mRender.setTouch(ret); |
| 151 | |
| 152 | if((ny > 0.85f) || mControlMode) { |
| 153 | mFlingMode = false; |
| 154 | |
| 155 | // Projector control |
| 156 | if((nx > 0.2f) && (nx < 0.8f) || mControlMode) { |
| 157 | if(act != ev.ACTION_UP) { |
| 158 | float zoom = mMaxZoom; |
| 159 | if(mControlMode) { |
| 160 | if(!mZoomMode) { |
| 161 | zoom = 1.f; |
| 162 | } |
| 163 | float dx = nx - mFlingX; |
| 164 | |
| 165 | if((ny < 0.9) && mZoomMode) { |
| 166 | zoom = mMaxZoom - ((0.9f - ny) * 10.f); |
| 167 | if(zoom < 1) { |
| 168 | zoom = 1; |
| 169 | mZoomMode = false; |
| 170 | } |
| 171 | mOldColumn = mColumn; |
| 172 | } |
| 173 | mColumn += dx * 4;// * zoom; |
| 174 | if(zoom > 1.01f) { |
| 175 | mColumn += (mZoom - zoom) * (nx - 0.5f) * 4 * zoom; |
| 176 | } |
| 177 | } else { |
| 178 | mOldColumn = mColumn; |
| 179 | mColumn = ((float)mColumns) / 2; |
| 180 | mControlMode = true; |
| 181 | mZoomMode = true; |
| 182 | } |
| 183 | mZoom = zoom; |
| 184 | mFlingX = nx; |
| 185 | mRender.setZoom(zoom); |
| 186 | if(mZoom < 1.01f) { |
| 187 | computeSelection(nx, ny); |
| 188 | } |
| 189 | } else { |
| 190 | mControlMode = false; |
| 191 | mColumn = mOldColumn; |
| 192 | mRender.setZoom(1.f); |
| 193 | mRender.setSelected(-1); |
| 194 | } |
| 195 | } else { |
| 196 | // Do something with corners here.... |
| 197 | } |
| 198 | setColumn(true); |
| 199 | |
| 200 | } else { |
| 201 | // icon control |
| 202 | if(act != ev.ACTION_UP) { |
| 203 | if(mFlingMode) { |
| 204 | mColumn += (mFlingX - nx) * 4; |
| 205 | setColumn(true); |
| 206 | } |
| 207 | mFlingMode = true; |
| 208 | mFlingX = nx; |
| 209 | mFlingY = ny; |
| 210 | } else { |
| 211 | mFlingMode = false; |
| 212 | mColumn = (float)(java.lang.Math.floor(mColumn * 0.25f + 0.3f) * 4.f) + 1.f; |
| 213 | setColumn(true); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public boolean onTrackballEvent(MotionEvent ev) |
| 223 | { |
| 224 | float x = ev.getX(); |
| 225 | float y = ev.getY(); |
| 226 | //Float tx = new Float(x); |
| 227 | //Float ty = new Float(y); |
| 228 | //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString()); |
| 229 | |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | public class RolloRS { |
| 235 | //public static final int STATE_SELECTED_ID = 0; |
| 236 | public static final int STATE_DONE = 1; |
| 237 | //public static final int STATE_PRESSURE = 2; |
| 238 | public static final int STATE_ZOOM = 3; |
| 239 | //public static final int STATE_WARP = 4; |
| 240 | public static final int STATE_ORIENTATION = 5; |
| 241 | public static final int STATE_SELECTION = 6; |
| 242 | public static final int STATE_FIRST_VISIBLE = 7; |
| 243 | public static final int STATE_COUNT = 8; |
| 244 | public static final int STATE_TOUCH = 9; |
| 245 | |
| 246 | |
| 247 | public RolloRS() { |
| 248 | } |
| 249 | |
| 250 | public void init(RenderScript rs, Resources res, int width, int height) { |
| 251 | mRS = rs; |
| 252 | mRes = res; |
| 253 | mWidth = width; |
| 254 | mHeight = height; |
| 255 | initNamed(); |
| 256 | initRS(); |
| 257 | } |
| 258 | |
| 259 | public void setPosition(float column) { |
| 260 | mAllocStateBuf[STATE_FIRST_VISIBLE] = (int)(column * (-20)); |
| 261 | mAllocState.data(mAllocStateBuf); |
| 262 | } |
| 263 | |
| 264 | public void setTouch(boolean touch) { |
| 265 | mAllocStateBuf[STATE_TOUCH] = touch ? 1 : 0; |
| 266 | mAllocState.data(mAllocStateBuf); |
| 267 | } |
| 268 | |
| 269 | public void setZoom(float z) { |
| 270 | //Log.e("rs", "zoom " + Float.toString(z)); |
| 271 | |
| 272 | mAllocStateBuf[STATE_ZOOM] = (int)(z * 1000.f); |
| 273 | mAllocState.data(mAllocStateBuf); |
| 274 | } |
| 275 | |
| 276 | public void setSelected(int index) { |
| 277 | //Log.e("rs", "setSelected " + Integer.toString(index)); |
| 278 | |
| 279 | mAllocStateBuf[STATE_SELECTION] = index; |
| 280 | mAllocStateBuf[STATE_DONE] = 1; |
| 281 | mAllocState.data(mAllocStateBuf); |
| 282 | } |
| 283 | |
| 284 | private int mWidth; |
| 285 | private int mHeight; |
| 286 | |
| 287 | private Resources mRes; |
| 288 | private RenderScript mRS; |
| 289 | private Script mScript; |
| 290 | private Sampler mSampler; |
| 291 | private Sampler mSamplerText; |
| 292 | private ProgramStore mPSBackground; |
| 293 | private ProgramStore mPSText; |
| 294 | private ProgramFragment mPFImages; |
| 295 | private ProgramFragment mPFText; |
| 296 | private ProgramVertex mPV; |
| 297 | private ProgramVertex.MatrixAllocation mPVAlloc; |
| 298 | private ProgramVertex mPVOrtho; |
| 299 | private ProgramVertex.MatrixAllocation mPVOrthoAlloc; |
| 300 | private Allocation[] mIcons; |
| 301 | private Allocation[] mLabels; |
| 302 | |
| 303 | private int[] mAllocStateBuf; |
| 304 | private Allocation mAllocState; |
| 305 | |
| 306 | private int[] mAllocIconIDBuf; |
| 307 | private Allocation mAllocIconID; |
| 308 | |
| 309 | private int[] mAllocLabelIDBuf; |
| 310 | private Allocation mAllocLabelID; |
| 311 | |
| 312 | private int[] mAllocScratchBuf; |
| 313 | private Allocation mAllocScratch; |
| 314 | |
| 315 | private void initNamed() { |
| 316 | Sampler.Builder sb = new Sampler.Builder(mRS); |
| 317 | sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR); |
| 318 | sb.setMag(Sampler.Value.LINEAR); |
| 319 | sb.setWrapS(Sampler.Value.CLAMP); |
| 320 | sb.setWrapT(Sampler.Value.CLAMP); |
| 321 | mSampler = sb.create(); |
| 322 | |
| 323 | sb.setMin(Sampler.Value.NEAREST); |
| 324 | sb.setMag(Sampler.Value.NEAREST); |
| 325 | mSamplerText = sb.create(); |
| 326 | |
| 327 | |
| 328 | ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null); |
| 329 | bf.setTexEnable(true, 0); |
| 330 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
| 331 | mPFImages = bf.create(); |
| 332 | mPFImages.setName("PF"); |
| 333 | mPFImages.bindSampler(mSampler, 0); |
| 334 | |
| 335 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
| 336 | mPFText = bf.create(); |
| 337 | mPFText.setName("PFText"); |
| 338 | mPFText.bindSampler(mSamplerText, 0); |
| 339 | |
| 340 | ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); |
| 341 | bs.setDepthFunc(ProgramStore.DepthFunc.LESS); |
| 342 | bs.setDitherEnable(false); |
| 343 | bs.setDepthMask(true); |
| 344 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 345 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 346 | mPSBackground = bs.create(); |
| 347 | mPSBackground.setName("PFS"); |
| 348 | |
| 349 | bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); |
| 350 | bs.setDepthMask(false); |
| 351 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 352 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 353 | mPSText = bs.create(); |
| 354 | mPSText.setName("PFSText"); |
| 355 | |
| 356 | mPVAlloc = new ProgramVertex.MatrixAllocation(mRS); |
| 357 | mPVAlloc.setupProjectionNormalized(mWidth, mHeight); |
| 358 | |
| 359 | ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); |
| 360 | mPV = pvb.create(); |
| 361 | mPV.setName("PV"); |
| 362 | mPV.bindAllocation(mPVAlloc); |
| 363 | |
| 364 | mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS); |
| 365 | mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight); |
| 366 | |
| 367 | pvb.setTextureMatrixEnable(true); |
| 368 | mPVOrtho = pvb.create(); |
| 369 | mPVOrtho.setName("PVOrtho"); |
| 370 | mPVOrtho.bindAllocation(mPVOrthoAlloc); |
| 371 | |
| 372 | mRS.contextBindProgramVertex(mPV); |
| 373 | |
| 374 | mAllocScratchBuf = new int[32]; |
| 375 | mAllocScratch = Allocation.createSized(mRS, |
| 376 | Element.USER_I32, mAllocScratchBuf.length); |
| 377 | mAllocScratch.data(mAllocScratchBuf); |
| 378 | |
| 379 | Log.e("rs", "Done loading named"); |
| 380 | |
| 381 | |
| 382 | |
| 383 | { |
| 384 | mIcons = new Allocation[29]; |
| 385 | mAllocIconIDBuf = new int[mIcons.length]; |
| 386 | mAllocIconID = Allocation.createSized(mRS, |
| 387 | Element.USER_I32, mAllocIconIDBuf.length); |
| 388 | |
| 389 | mLabels = new Allocation[29]; |
| 390 | mAllocLabelIDBuf = new int[mLabels.length]; |
| 391 | mAllocLabelID = Allocation.createSized(mRS, |
| 392 | Element.USER_I32, mLabels.length); |
| 393 | |
| 394 | Element ie8888 = Element.RGBA_8888; |
| 395 | |
| 396 | for (int i=0; i<mIcons.length; i++) { |
| 397 | mIcons[i] = Allocation.createFromBitmapResource( |
| 398 | mRS, mRes, R.raw.maps, ie8888, true); |
| 399 | mLabels[i] = makeTextBitmap("Maps"); |
| 400 | } |
| 401 | |
| 402 | for(int ct=0; ct < mIcons.length; ct++) { |
| 403 | mIcons[ct].uploadToTexture(0); |
| 404 | mLabels[ct].uploadToTexture(0); |
| 405 | mAllocIconIDBuf[ct] = mIcons[ct].getID(); |
| 406 | mAllocLabelIDBuf[ct] = mLabels[ct].getID(); |
| 407 | } |
| 408 | mAllocIconID.data(mAllocIconIDBuf); |
| 409 | mAllocLabelID.data(mAllocLabelIDBuf); |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | Allocation makeTextBitmap(String t) { |
| 415 | Bitmap b = Bitmap.createBitmap(128, 32, Bitmap.Config.ARGB_8888); |
| 416 | Canvas c = new Canvas(b); |
| 417 | Paint p = new Paint(); |
| 418 | p.setTypeface(Typeface.DEFAULT_BOLD); |
| 419 | p.setTextSize(20); |
| 420 | p.setColor(0xffffffff); |
| 421 | c.drawText(t, 2, 26, p); |
| 422 | return Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | private void initRS() { |
| 427 | ScriptC.Builder sb = new ScriptC.Builder(mRS); |
| 428 | sb.setScript(mRes, R.raw.rollo); |
| 429 | //sb.setScript(mRes, R.raw.rollo2); |
| 430 | sb.setRoot(true); |
| 431 | mScript = sb.create(); |
| 432 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 433 | |
| 434 | mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0}; |
| 435 | mAllocState = Allocation.createSized(mRS, |
| 436 | Element.USER_I32, mAllocStateBuf.length); |
| 437 | mScript.bindAllocation(mAllocState, 0); |
| 438 | mScript.bindAllocation(mAllocIconID, 1); |
| 439 | mScript.bindAllocation(mAllocScratch, 2); |
| 440 | mScript.bindAllocation(mAllocLabelID, 3); |
| 441 | setPosition(0); |
| 442 | setZoom(1); |
| 443 | |
| 444 | //RenderScript.File f = mRS.fileOpen("/sdcard/test.a3d"); |
| 445 | |
| 446 | mRS.contextBindRootScript(mScript); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |