Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.gallery3d.ui; |
| 18 | |
Owen Lin | 2b3ee0e | 2012-03-14 17:27:24 +0800 | [diff] [blame^] | 19 | import android.util.SparseArray; |
| 20 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 21 | import com.android.gallery3d.app.GalleryActivity; |
| 22 | import com.android.gallery3d.common.Utils; |
| 23 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 24 | import java.util.WeakHashMap; |
| 25 | |
| 26 | public class PositionRepository { |
| 27 | private static final WeakHashMap<GalleryActivity, PositionRepository> |
| 28 | sMap = new WeakHashMap<GalleryActivity, PositionRepository>(); |
| 29 | |
| 30 | public static class Position implements Cloneable { |
| 31 | public float x; |
| 32 | public float y; |
| 33 | public float z; |
| 34 | public float theta; |
| 35 | public float alpha; |
| 36 | |
| 37 | public Position() { |
| 38 | } |
| 39 | |
| 40 | public Position(float x, float y, float z) { |
| 41 | this(x, y, z, 0f, 1f); |
| 42 | } |
| 43 | |
| 44 | public Position(float x, float y, float z, float ftheta, float alpha) { |
| 45 | this.x = x; |
| 46 | this.y = y; |
| 47 | this.z = z; |
| 48 | this.theta = ftheta; |
| 49 | this.alpha = alpha; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Position clone() { |
| 54 | try { |
| 55 | return (Position) super.clone(); |
| 56 | } catch (CloneNotSupportedException e) { |
| 57 | throw new AssertionError(); // we do support clone. |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public void set(Position another) { |
| 62 | x = another.x; |
| 63 | y = another.y; |
| 64 | z = another.z; |
| 65 | theta = another.theta; |
| 66 | alpha = another.alpha; |
| 67 | } |
| 68 | |
| 69 | public void set(float x, float y, float z, float ftheta, float alpha) { |
| 70 | this.x = x; |
| 71 | this.y = y; |
| 72 | this.z = z; |
| 73 | this.theta = ftheta; |
| 74 | this.alpha = alpha; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object object) { |
| 79 | if (!(object instanceof Position)) return false; |
| 80 | Position position = (Position) object; |
| 81 | return x == position.x && y == position.y && z == position.z |
| 82 | && theta == position.theta |
| 83 | && alpha == position.alpha; |
| 84 | } |
| 85 | |
| 86 | public static void interpolate( |
| 87 | Position source, Position target, Position output, float progress) { |
| 88 | if (progress < 1f) { |
| 89 | output.set( |
| 90 | Utils.interpolateScale(source.x, target.x, progress), |
| 91 | Utils.interpolateScale(source.y, target.y, progress), |
| 92 | Utils.interpolateScale(source.z, target.z, progress), |
| 93 | Utils.interpolateAngle(source.theta, target.theta, progress), |
| 94 | Utils.interpolateScale(source.alpha, target.alpha, progress)); |
| 95 | } else { |
| 96 | output.set(target); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public static PositionRepository getInstance(GalleryActivity activity) { |
| 102 | PositionRepository repository = sMap.get(activity); |
| 103 | if (repository == null) { |
| 104 | repository = new PositionRepository(); |
| 105 | sMap.put(activity, repository); |
| 106 | } |
| 107 | return repository; |
| 108 | } |
| 109 | |
Chih-Chung Chang | 95018d1 | 2012-02-15 08:19:50 +0800 | [diff] [blame] | 110 | private SparseArray<Position> mData = new SparseArray<Position>(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 111 | private int mOffsetX; |
| 112 | private int mOffsetY; |
| 113 | private Position mTempPosition = new Position(); |
| 114 | |
Chih-Chung Chang | 95018d1 | 2012-02-15 08:19:50 +0800 | [diff] [blame] | 115 | public Position get(int identity) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 116 | Position position = mData.get(identity); |
| 117 | if (position == null) return null; |
| 118 | mTempPosition.set(position); |
| 119 | position = mTempPosition; |
| 120 | position.x -= mOffsetX; |
| 121 | position.y -= mOffsetY; |
| 122 | return position; |
| 123 | } |
| 124 | |
| 125 | public void setOffset(int offsetX, int offsetY) { |
| 126 | mOffsetX = offsetX; |
| 127 | mOffsetY = offsetY; |
| 128 | } |
| 129 | |
Chih-Chung Chang | 95018d1 | 2012-02-15 08:19:50 +0800 | [diff] [blame] | 130 | public void putPosition(int identity, Position position) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 131 | Position clone = position.clone(); |
| 132 | clone.x += mOffsetX; |
| 133 | clone.y += mOffsetY; |
| 134 | mData.put(identity, clone); |
| 135 | } |
| 136 | |
| 137 | public void clear() { |
| 138 | mData.clear(); |
| 139 | } |
| 140 | } |