/src/org/mt4j/components/visibleComponents/widgets/MTOverlayContainer.java

http://mt4j.googlecode.com/ · Java · 133 lines · 54 code · 17 blank · 62 comment · 8 complexity · 37c515d658e87798b0d6cd428ba33073 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.components.visibleComponents.widgets;
  19. import org.mt4j.MTApplication;
  20. import org.mt4j.components.MTComponent;
  21. import org.mt4j.components.StateChange;
  22. import org.mt4j.components.StateChangeEvent;
  23. import org.mt4j.components.StateChangeListener;
  24. import org.mt4j.components.interfaces.IMTController;
  25. import org.mt4j.util.camera.MTCamera;
  26. /**
  27. * This is a component that will try to always stay on top and will stay in place even
  28. * if the scene's camera is moved.
  29. * <br>Thus it can be used as a container for a GUI or a HUD (head up display).
  30. *
  31. * @author Christopher Ruff
  32. */
  33. public class MTOverlayContainer extends MTComponent {
  34. /** The app. */
  35. private MTApplication app;
  36. //TODO overlay layer priorites / layer numbers to sort them by priority
  37. /**
  38. * Instantiates a new mT overlay container.
  39. *
  40. * @param applet the applet
  41. */
  42. public MTOverlayContainer(MTApplication applet) {
  43. this(applet, "unnamed overlay container");
  44. }
  45. /**
  46. * Instantiates a new mT overlay container.
  47. *
  48. * @param app the app
  49. * @param name the name
  50. */
  51. public MTOverlayContainer(MTApplication app, String name) {
  52. super(app, name, new MTCamera(app));
  53. this.app = app;
  54. this.setDepthBufferDisabled(true);
  55. //Send overlay group to front again if it isnt - check each frame if its on front!
  56. this.setController(new IMTController() {
  57. public void update(long timeDelta) {
  58. putLastInParentList();
  59. }
  60. });
  61. //Always put last in parents children list if added to a parent,
  62. //so the chance is higher that this is really drawn ontop of other stuff
  63. this.addStateChangeListener(StateChange.ADDED_TO_PARENT, new StateChangeListener() {
  64. public void stateChanged(StateChangeEvent evt) {
  65. putLastInParentList();
  66. }
  67. });
  68. }
  69. private void putLastInParentList(){
  70. MTComponent parent = getParent();
  71. if (parent != null){
  72. int childCount = parent.getChildCount();
  73. if ( childCount > 0
  74. && !parent.getChildByIndex(childCount-1).equals(MTOverlayContainer.this)
  75. ){
  76. MTComponent lastChild = parent.getChildByIndex(childCount-1);
  77. if ( !(lastChild instanceof MTOverlayContainer)
  78. && !(lastChild.getName().equalsIgnoreCase("Cursor Trace group"))
  79. ){
  80. //last component in canvas child list is not a overlay container:
  81. MTOverlayContainer.this.app.invokeLater(new Runnable() {
  82. public void run(){
  83. MTComponent parent = getParent();
  84. if (parent != null){
  85. parent.removeChild(MTOverlayContainer.this);
  86. parent.addChild(MTOverlayContainer.this);
  87. }
  88. }
  89. });
  90. }else{
  91. //last component in canvas already is a different overlay container:
  92. // int insertionIndex = getInsertionIndex(parent);
  93. }
  94. }
  95. }
  96. }
  97. /*
  98. private int getInsertionIndex(MTComponent parent){
  99. int count = parent.getChildCount();
  100. for (int i = 1; i <= count; i++) {
  101. // int index = -i;
  102. int checkIndex = count-i;
  103. MTComponent child = parent.getChildByIndex(checkIndex);
  104. System.out.println("Check at: " + checkIndex + " ->" + child);
  105. if (child instanceof MTOverlayContainer){
  106. }else{
  107. System.out.println("Found insertion place for : " + this + " at: " + checkIndex);
  108. return checkIndex;
  109. }
  110. }
  111. return 0;
  112. }
  113. */
  114. }