Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.gfx; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.GeckoAppShell; |
michael@0 | 8 | import org.mozilla.gecko.util.FloatUtils; |
michael@0 | 9 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 10 | |
michael@0 | 11 | import android.graphics.PointF; |
michael@0 | 12 | import android.graphics.Rect; |
michael@0 | 13 | import android.graphics.RectF; |
michael@0 | 14 | import android.view.SurfaceView; |
michael@0 | 15 | import android.view.View; |
michael@0 | 16 | import android.widget.AbsoluteLayout; |
michael@0 | 17 | |
michael@0 | 18 | public class PluginLayer extends TileLayer { |
michael@0 | 19 | private static final String LOGTAG = "PluginLayer"; |
michael@0 | 20 | |
michael@0 | 21 | private View mView; |
michael@0 | 22 | private SurfaceView mSurfaceView; |
michael@0 | 23 | private PluginLayoutParams mLayoutParams; |
michael@0 | 24 | private AbsoluteLayout mContainer; |
michael@0 | 25 | |
michael@0 | 26 | private boolean mDestroyed; |
michael@0 | 27 | private boolean mViewVisible; |
michael@0 | 28 | |
michael@0 | 29 | private RectF mLastViewport; |
michael@0 | 30 | private float mLastZoomFactor; |
michael@0 | 31 | |
michael@0 | 32 | private static final float TEXTURE_MAP[] = { |
michael@0 | 33 | 0.0f, 1.0f, // top left |
michael@0 | 34 | 0.0f, 0.0f, // bottom left |
michael@0 | 35 | 1.0f, 1.0f, // top right |
michael@0 | 36 | 1.0f, 0.0f, // bottom right |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | public PluginLayer(View view, RectF rect, int maxDimension) { |
michael@0 | 40 | super(new BufferedCairoImage(null, 0, 0, 0), TileLayer.PaintMode.NORMAL); |
michael@0 | 41 | |
michael@0 | 42 | mView = view; |
michael@0 | 43 | mContainer = GeckoAppShell.getGeckoInterface().getPluginContainer(); |
michael@0 | 44 | |
michael@0 | 45 | mView.setWillNotDraw(false); |
michael@0 | 46 | if (mView instanceof SurfaceView) { |
michael@0 | 47 | mSurfaceView = (SurfaceView)view; |
michael@0 | 48 | mSurfaceView.setZOrderOnTop(false); |
michael@0 | 49 | mSurfaceView.setZOrderMediaOverlay(true); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | mLayoutParams = new PluginLayoutParams(rect, maxDimension); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | public void setVisible(boolean visible) { |
michael@0 | 56 | if (visible) { |
michael@0 | 57 | showView(); |
michael@0 | 58 | } else { |
michael@0 | 59 | hideView(); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | private void hideView() { |
michael@0 | 64 | if (mViewVisible) { |
michael@0 | 65 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 66 | @Override |
michael@0 | 67 | public void run() { |
michael@0 | 68 | mView.setVisibility(View.GONE); |
michael@0 | 69 | mViewVisible = false; |
michael@0 | 70 | } |
michael@0 | 71 | }); |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | public void showView() { |
michael@0 | 76 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 77 | @Override |
michael@0 | 78 | public void run() { |
michael@0 | 79 | if (mContainer.indexOfChild(mView) < 0) { |
michael@0 | 80 | mContainer.addView(mView, mLayoutParams); |
michael@0 | 81 | } else { |
michael@0 | 82 | mContainer.updateViewLayout(mView, mLayoutParams); |
michael@0 | 83 | mView.setVisibility(View.VISIBLE); |
michael@0 | 84 | } |
michael@0 | 85 | mViewVisible = true; |
michael@0 | 86 | } |
michael@0 | 87 | }); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @Override |
michael@0 | 91 | public void destroy() { |
michael@0 | 92 | mDestroyed = true; |
michael@0 | 93 | |
michael@0 | 94 | mContainer.removeView(mView); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | public void reset(RectF rect) { |
michael@0 | 98 | mLayoutParams.reset(rect); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | @Override |
michael@0 | 102 | protected void performUpdates(RenderContext context) { |
michael@0 | 103 | if (mDestroyed) |
michael@0 | 104 | return; |
michael@0 | 105 | |
michael@0 | 106 | if (!RectUtils.fuzzyEquals(context.viewport, mLastViewport) || |
michael@0 | 107 | !FloatUtils.fuzzyEquals(context.zoomFactor, mLastZoomFactor)) { |
michael@0 | 108 | |
michael@0 | 109 | mLastZoomFactor = context.zoomFactor; |
michael@0 | 110 | mLastViewport = context.viewport; |
michael@0 | 111 | mLayoutParams.reposition(context.viewport, context.offset, context.zoomFactor); |
michael@0 | 112 | |
michael@0 | 113 | showView(); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | @Override |
michael@0 | 118 | public void draw(RenderContext context) { |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | class PluginLayoutParams extends AbsoluteLayout.LayoutParams |
michael@0 | 122 | { |
michael@0 | 123 | private static final String LOGTAG = "GeckoApp.PluginLayoutParams"; |
michael@0 | 124 | |
michael@0 | 125 | private RectF mRect; |
michael@0 | 126 | private int mMaxDimension; |
michael@0 | 127 | private float mLastResolution; |
michael@0 | 128 | |
michael@0 | 129 | public PluginLayoutParams(RectF rect, int maxDimension) { |
michael@0 | 130 | super(0, 0, 0, 0); |
michael@0 | 131 | |
michael@0 | 132 | mMaxDimension = maxDimension; |
michael@0 | 133 | reset(rect); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | private void clampToMaxSize() { |
michael@0 | 137 | if (width > mMaxDimension || height > mMaxDimension) { |
michael@0 | 138 | if (width > height) { |
michael@0 | 139 | height = Math.round(((float)height/(float)width) * mMaxDimension); |
michael@0 | 140 | width = mMaxDimension; |
michael@0 | 141 | } else { |
michael@0 | 142 | width = Math.round(((float)width/(float)height) * mMaxDimension); |
michael@0 | 143 | height = mMaxDimension; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | public void reset(RectF rect) { |
michael@0 | 149 | mRect = rect; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | public void reposition(RectF viewport, PointF offset, float zoomFactor) { |
michael@0 | 153 | |
michael@0 | 154 | RectF scaled = RectUtils.scale(mRect, zoomFactor); |
michael@0 | 155 | scaled.offset(offset.x, offset.y); |
michael@0 | 156 | |
michael@0 | 157 | this.x = Math.round(scaled.left - viewport.left); |
michael@0 | 158 | this.y = Math.round(scaled.top - viewport.top); |
michael@0 | 159 | |
michael@0 | 160 | if (!FloatUtils.fuzzyEquals(mLastResolution, zoomFactor)) { |
michael@0 | 161 | width = Math.round(mRect.width() * zoomFactor); |
michael@0 | 162 | height = Math.round(mRect.height() * zoomFactor); |
michael@0 | 163 | mLastResolution = zoomFactor; |
michael@0 | 164 | |
michael@0 | 165 | clampToMaxSize(); |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | } |