Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, The Android Open Source Project |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 25 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 26 | #include "AnimationPlugin.h" |
| 27 | |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 28 | #include <math.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | extern NPNetscapeFuncs* browser; |
| 32 | extern ANPLogInterfaceV0 gLogI; |
| 33 | extern ANPCanvasInterfaceV0 gCanvasI; |
| 34 | extern ANPPaintInterfaceV0 gPaintI; |
| 35 | extern ANPPathInterfaceV0 gPathI; |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 36 | extern ANPWindowInterfaceV0 gWindowI; |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 37 | |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 38 | static uint16 rnd16(float x, int inset) { |
| 39 | int ix = (int)roundf(x) + inset; |
| 40 | if (ix < 0) { |
| 41 | ix = 0; |
| 42 | } |
| 43 | return static_cast<uint16>(ix); |
| 44 | } |
| 45 | |
| 46 | static void inval(NPP instance, const ANPRectF& r, bool doAA) { |
| 47 | const int inset = doAA ? -1 : 0; |
| 48 | |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 49 | NPRect inval; |
| 50 | inval.left = rnd16(r.left, inset); |
| 51 | inval.top = rnd16(r.top, inset); |
| 52 | inval.right = rnd16(r.right, -inset); |
| 53 | inval.bottom = rnd16(r.bottom, -inset); |
| 54 | browser->invalidaterect(instance, &inval); |
| 55 | } |
| 56 | |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 57 | static void bounce(float* x, float* dx, const float max) { |
| 58 | *x += *dx; |
| 59 | if (*x < 0) { |
| 60 | *x = 0; |
| 61 | if (*dx < 0) { |
| 62 | *dx = -*dx; |
| 63 | } |
| 64 | } else if (*x > max) { |
| 65 | *x = max; |
| 66 | if (*dx > 0) { |
| 67 | *dx = -*dx; |
| 68 | } |
| 69 | } |
| 70 | } |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | BallAnimation::BallAnimation(NPP inst) : SubPlugin(inst) { |
| 74 | m_x = m_y = 0; |
| 75 | m_dx = 7 * SCALE; |
| 76 | m_dy = 5 * SCALE; |
| 77 | |
| 78 | memset(&m_oval, 0, sizeof(m_oval)); |
| 79 | |
| 80 | m_paint = gPaintI.newPaint(); |
| 81 | gPaintI.setFlags(m_paint, gPaintI.getFlags(m_paint) | kAntiAlias_ANPPaintFlag); |
| 82 | gPaintI.setColor(m_paint, 0xFFFF0000); |
| 83 | |
| 84 | //register for touch events |
| 85 | ANPEventFlags flags = kTouch_ANPEventFlag; |
| 86 | NPError err = browser->setvalue(inst, kAcceptEvents_ANPSetValue, &flags); |
| 87 | if (err != NPERR_NO_ERROR) { |
| 88 | gLogI.log(inst, kError_ANPLogType, "Error selecting input events."); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | BallAnimation::~BallAnimation() { |
| 93 | gPaintI.deletePaint(m_paint); |
| 94 | } |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 95 | |
Derek Sollenberger | 21f3991 | 2009-07-09 09:19:39 -0400 | [diff] [blame] | 96 | bool BallAnimation::supportsDrawingModel(ANPDrawingModel model) { |
| 97 | return (model == kBitmap_ANPDrawingModel); |
| 98 | } |
| 99 | |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 100 | void BallAnimation::drawPlugin(const ANPBitmap& bitmap, const ANPRectI& clip) { |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 101 | |
| 102 | // create a canvas |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 103 | ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); |
| 104 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 105 | // clip the canvas |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 106 | ANPRectF clipR; |
| 107 | clipR.left = clip.left; |
| 108 | clipR.top = clip.top; |
| 109 | clipR.right = clip.right; |
| 110 | clipR.bottom = clip.bottom; |
| 111 | gCanvasI.clipRect(canvas, &clipR); |
| 112 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 113 | // setup variables |
| 114 | PluginObject *obj = (PluginObject*) inst()->pdata; |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 115 | const float OW = 20; |
| 116 | const float OH = 20; |
| 117 | const int W = obj->window->width; |
| 118 | const int H = obj->window->height; |
| 119 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 120 | // paint the canvas (using the path API) |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 121 | gCanvasI.drawColor(canvas, 0xFFFFFFFF); |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 122 | { |
| 123 | ANPPath* path = gPathI.newPath(); |
| 124 | |
| 125 | float cx = W * 0.5f; |
| 126 | float cy = H * 0.5f; |
| 127 | gPathI.moveTo(path, 0, 0); |
| 128 | gPathI.quadTo(path, cx, cy, W, 0); |
| 129 | gPathI.quadTo(path, cx, cy, W, H); |
| 130 | gPathI.quadTo(path, cx, cy, 0, H); |
| 131 | gPathI.quadTo(path, cx, cy, 0, 0); |
| 132 | |
| 133 | gPaintI.setColor(m_paint, 0xFF0000FF); |
| 134 | gCanvasI.drawPath(canvas, path, m_paint); |
| 135 | |
| 136 | ANPRectF bounds; |
| 137 | memset(&bounds, 0, sizeof(bounds)); |
| 138 | gPathI.getBounds(path, &bounds); |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 139 | gPathI.deletePath(path); |
| 140 | } |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 141 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 142 | // draw the oval |
| 143 | inval(inst(), m_oval, true); // inval the old |
| 144 | m_oval.left = m_x; |
| 145 | m_oval.top = m_y; |
| 146 | m_oval.right = m_x + OW; |
| 147 | m_oval.bottom = m_y + OH; |
| 148 | inval(inst(), m_oval, true); // inval the new |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 149 | gPaintI.setColor(m_paint, 0xFFFF0000); |
| 150 | gCanvasI.drawOval(canvas, &m_oval, m_paint); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 151 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 152 | // update the coordinates of the oval |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 153 | bounce(&m_x, &m_dx, obj->window->width - OW); |
| 154 | bounce(&m_y, &m_dy, obj->window->height - OH); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 155 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame^] | 156 | // delete the canvas |
| 157 | gCanvasI.deleteCanvas(canvas); |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Derek Sollenberger | c654bce | 2009-07-20 10:03:58 -0400 | [diff] [blame] | 160 | void BallAnimation::showEntirePluginOnScreen() { |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 161 | NPP instance = this->inst(); |
| 162 | PluginObject *obj = (PluginObject*) instance->pdata; |
| 163 | NPWindow *window = obj->window; |
| 164 | |
Derek Sollenberger | c654bce | 2009-07-20 10:03:58 -0400 | [diff] [blame] | 165 | ANPRectI visibleRects[1]; |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 166 | |
Derek Sollenberger | c654bce | 2009-07-20 10:03:58 -0400 | [diff] [blame] | 167 | visibleRects[0].left = 0; |
| 168 | visibleRects[0].top = 0; |
| 169 | visibleRects[0].right = window->width; |
| 170 | visibleRects[0].bottom = window->height; |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 171 | |
Derek Sollenberger | c654bce | 2009-07-20 10:03:58 -0400 | [diff] [blame] | 172 | gWindowI.setVisibleRects(instance, visibleRects, 1); |
| 173 | gWindowI.clearVisibleRects(instance); |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 176 | int16 BallAnimation::handleEvent(const ANPEvent* evt) { |
| 177 | NPP instance = this->inst(); |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 178 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 179 | switch (evt->eventType) { |
| 180 | case kDraw_ANPEventType: |
| 181 | switch (evt->data.draw.model) { |
| 182 | case kBitmap_ANPDrawingModel: |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 183 | drawPlugin(evt->data.draw.data.bitmap, evt->data.draw.clip); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 184 | return 1; |
| 185 | default: |
| 186 | break; // unknown drawing model |
| 187 | } |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 188 | case kTouch_ANPEventType: |
| 189 | if (kDown_ANPTouchAction == evt->data.touch.action) { |
Derek Sollenberger | c654bce | 2009-07-20 10:03:58 -0400 | [diff] [blame] | 190 | showEntirePluginOnScreen(); |
Derek Sollenberger | 5b011e3 | 2009-06-22 11:39:40 -0400 | [diff] [blame] | 191 | } |
| 192 | return 1; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 193 | default: |
| 194 | break; |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 195 | } |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 196 | return 0; // unknown or unhandled event |
Grace Kloba | fbe47c0 | 2009-05-14 17:31:45 -0700 | [diff] [blame] | 197 | } |