/flash/src/playn/flash/FlashGroupLayer.java

https://github.com/simensan/playn
Java | 132 lines | 95 code | 22 blank | 15 comment | 2 complexity | 058a1c3b015a28ab245a4ee049f2f14b 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 flash.display.DisplayObject;
  18. import flash.display.DisplayObjectContainer;
  19. import flash.display.Sprite;
  20. import playn.core.Asserts;
  21. import playn.core.GroupLayer;
  22. import playn.core.GroupLayerImpl;
  23. import playn.core.Layer;
  24. import playn.core.ParentLayer;
  25. public class FlashGroupLayer extends FlashLayer implements GroupLayer, ParentLayer {
  26. private GroupLayerImpl<FlashLayer> impl = new GroupLayerImpl<FlashLayer>();
  27. FlashGroupLayer(DisplayObjectContainer container) {
  28. super(container);
  29. }
  30. FlashGroupLayer() {
  31. super(Sprite.create());
  32. }
  33. static FlashGroupLayer getRoot() {
  34. return new FlashGroupLayer(Sprite.getRootSprite());
  35. }
  36. @Override
  37. public Layer get(int index) {
  38. return impl.children.get(index);
  39. }
  40. @Override
  41. public void add(Layer layer) {
  42. int index = impl.add(this, (FlashLayer) layer);
  43. ((FlashLayer) layer).update();
  44. container().addChildAt(display(layer), index);
  45. }
  46. @Override @Deprecated
  47. public void add(int index, Layer layer) {
  48. impl.add(this, index, (FlashLayer) layer);
  49. ((FlashLayer) layer).update();
  50. container().addChildAt(display(layer), index);
  51. }
  52. @Override
  53. public void remove(Layer layer) {
  54. impl.remove(this, (FlashLayer) layer);
  55. container().removeChild(display(layer));
  56. }
  57. @Override @Deprecated
  58. public void remove(int index) {
  59. impl.remove(this, index);
  60. container().removeChildAt(index);
  61. }
  62. @Override
  63. public void clear() {
  64. impl.clear(this);
  65. while (container().getNumChildren() > 0) {
  66. container().removeChildAt(0);
  67. }
  68. }
  69. @Override
  70. public int size() {
  71. return impl.children.size();
  72. }
  73. @Override
  74. public void destroy() {
  75. super.destroy();
  76. impl.destroy(this);
  77. }
  78. @Override
  79. public void onAdd() {
  80. super.onAdd();
  81. impl.onAdd(this);
  82. }
  83. @Override
  84. public void onRemove() {
  85. super.onRemove();
  86. impl.onRemove(this);
  87. }
  88. @Override
  89. public void depthChanged(Layer layer, float oldDepth) {
  90. int index = impl.depthChanged(this, layer, oldDepth);
  91. DisplayObject child = ((FlashLayer)layer).display();
  92. container().removeChild(child);
  93. container().addChildAt(child, index);
  94. }
  95. protected void updateChildren() {
  96. for (Layer l : impl.children) {
  97. ((FlashLayer) l).update();
  98. }
  99. }
  100. private DisplayObjectContainer container() {
  101. return display().cast();
  102. }
  103. private FlashLayer flash(Layer layer) {
  104. Asserts.checkArgument(layer instanceof FlashLayer);
  105. return (FlashLayer) layer;
  106. }
  107. private DisplayObject display(Layer l) {
  108. return flash(l).display();
  109. }
  110. }