/flash/src/playn/flash/FlashCanvasLayer.java

https://github.com/simensan/playn · Java · 230 lines · 109 code · 44 blank · 77 comment · 0 complexity · 174a95f110b7b8a36c92c81ba75cfb12 MD5 · raw file

  1. /*
  2. * Copyright 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package playn.flash;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. import playn.core.Asserts;
  19. import flash.display.BitmapData;
  20. import flash.gwt.FlashImport;
  21. import flash.display.Sprite;
  22. import playn.core.Canvas;
  23. import playn.core.CanvasLayer;
  24. /**
  25. *
  26. */
  27. public class FlashCanvasLayer extends FlashLayer implements CanvasLayer {
  28. private FlashCanvas canvas;
  29. public FlashCanvasLayer(int width, int height) {
  30. super((Sprite) CanvasElement.create(width, height).cast());
  31. canvas = new FlashCanvas(width, height, ((CanvasElement) display().cast()).getContext());
  32. }
  33. /* (non-Javadoc)
  34. * @see playn.core.CanvasLayer#canvas()
  35. */
  36. @Override
  37. public Canvas canvas() {
  38. // TODO Auto-generated method stub
  39. return canvas;
  40. }
  41. @FlashImport({"com.googlecode.flashcanvas.Canvas"})
  42. final static class CanvasElement extends JavaScriptObject {
  43. protected CanvasElement() {}
  44. public static native CanvasElement create() /*-{
  45. return new com.googlecode.flashcanvas.Canvas();
  46. }-*/;
  47. public static native CanvasElement create(int width, int height) /*-{
  48. return new com.googlecode.flashcanvas.Canvas(width, height);
  49. }-*/;
  50. public final native Context2d getContext() /*-{
  51. return this.getContext("2d");
  52. }-*/;
  53. }
  54. @FlashImport({"com.googlecode.flashcanvas.CanvasRenderingContext2D"})
  55. final static class Context2d extends JavaScriptObject {
  56. protected Context2d() {}
  57. public native void resize(int x, int y) /*-{
  58. this.resize(x,y);
  59. }-*/;
  60. public native void beginPath() /*-{
  61. this.beginPath();
  62. }-*/;
  63. public native void moveTo(double x, double y) /*-{
  64. this.moveTo(x, y);
  65. }-*/;
  66. public native void lineTo(double x, double y) /*-{
  67. this.lineTo(x, y);
  68. }-*/;
  69. public native void stroke() /*-{
  70. this.stroke();
  71. }-*/;
  72. public native void setStrokeStyle(String color) /*-{
  73. this.strokeStyle = color;
  74. }-*/;
  75. public native void setFillStyle(String color) /*-{
  76. this.fillStyle = color;
  77. }-*/;
  78. /**
  79. * @param bitmapData
  80. * @param x
  81. * @param y
  82. */
  83. public native void drawImage(BitmapData bitmapData, float x, float y) /*-{
  84. this._renderImage(bitmapData, [x, y]);
  85. }-*/;
  86. public native void drawImage(BitmapData bitmapData, float x, float y, float w, float h) /*-{
  87. this._renderImage(bitmapData, [x, y, w, h]);
  88. }-*/;
  89. public native void drawImage(BitmapData bitmapData, float x, float y, float w, float h,
  90. float sx, float sy, float sw, float sh) /*-{
  91. this._renderImage(bitmapData, [sx, sy, sw, sh, x, y, w, h]);
  92. }-*/;
  93. /**
  94. *
  95. */
  96. public native void restore() /*-{
  97. this.restore();
  98. }-*/;
  99. public native void save() /*-{
  100. this.save();
  101. }-*/;
  102. /**
  103. * @param radians
  104. */
  105. public native void rotate(float radians) /*-{
  106. // TODO Auto-generated method stub
  107. this.rotate(radians);
  108. }-*/;
  109. public native void scale(float sx, float sy) /*-{
  110. // TODO Auto-generated method stub
  111. this.scale(sx, sy);
  112. }-*/;
  113. public native void translate(float tx, float ty) /*-{
  114. // TODO Auto-generated method stub
  115. this.translate(sx, sy);
  116. }-*/;
  117. /**
  118. * @param m11
  119. * @param m12
  120. * @param m21
  121. * @param m22
  122. * @param dx
  123. * @param dy
  124. */
  125. public native void transform(float m11, float m12, float m21, float m22, float dx, float dy) /*-{
  126. // TODO Auto-generated method stub
  127. this.transform(m11, m12, m21, m22, dx, dy);
  128. }-*/;
  129. public native void setTransform(float m11, float m12, float m21, float m22, float dx, float dy) /*-{
  130. // TODO Auto-generated method stub
  131. this.setTransform(m11, m12, m21, m22, dx, dy);
  132. }-*/;
  133. public native void fillText(String text, float x, float y) /*-{
  134. this.fillText(text, x, y);
  135. }-*/;
  136. public native void fillRect(float x, float y, float w, float h) /*-{
  137. this.fillRect(x, y, w, h);
  138. }-*/;
  139. public native void strokeText(String text, float x, float y) /*-{
  140. this.strokeText(text, x, y);
  141. }-*/;
  142. public native void arcTo(double curX, double curY, double x, double y, double radius) /*-{
  143. this.arcTo(curX, curY, x, y, radius);
  144. }-*/;
  145. public native void quadraticCurveTo(
  146. double cpx, double cpy, double x, double y) /*-{
  147. this.quadraticCurveTo(cpx, cpy, x, y);
  148. }-*/;
  149. public native void close() /*-{
  150. this.close();
  151. }-*/;
  152. public native void fill() /*-{
  153. this.fill();
  154. }-*/;
  155. public native void strokeRect(float x, float y, float w, float h) /*-{
  156. this.strokeRect(x, y, w, h);
  157. }-*/;
  158. public native BitmapData bitmapData() /*-{
  159. return this.canvas.bitmapData;
  160. }-*/;
  161. public native void clearRect(int x, int y, int width, int height) /*-{
  162. this.clearRect(x, y, width, height);
  163. }-*/;
  164. public native void setLineWidth(float width) /*-{
  165. this.lineWidth = width;
  166. }-*/;
  167. }
  168. @Override
  169. public float width() {
  170. Asserts.checkNotNull(canvas, "Canvas must not be null");
  171. return canvas.width();
  172. }
  173. @Override
  174. public float height() {
  175. Asserts.checkNotNull(canvas, "Canvas must not be null");
  176. return canvas.height();
  177. }
  178. @Override
  179. public float scaledWidth() {
  180. return transform().scaleX() * width();
  181. }
  182. @Override
  183. public float scaledHeight() {
  184. return transform().scaleY() * height();
  185. }
  186. }