PageRenderTime 67ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ch/usi/inf/saiv/railwayempire/gui/elements/AbstractPanel.java

https://bitbucket.org/aurecchia/railwayexpress
Java | 266 lines | 135 code | 29 blank | 102 comment | 24 complexity | 58dad094f4edb6366cf757d602a50004 MD5 | raw file
  1. package ch.usi.inf.saiv.railwayempire.gui.elements;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.newdawn.slick.Color;
  5. import org.newdawn.slick.Graphics;
  6. import org.newdawn.slick.Image;
  7. import org.newdawn.slick.geom.Shape;
  8. import org.newdawn.slick.gui.GUIContext;
  9. import ch.usi.inf.saiv.railwayempire.utilities.SystemConstants;
  10. /**
  11. * Class representing a panel which holds some children.
  12. *
  13. * Main responsibility is to hold a list of children gui components and to pass events to the children: execute render()
  14. * on children when render() is called, execute the mouse event methods on the right child, and so on.
  15. *
  16. * Another responsibility is to hold a background image or color and render it before the children.
  17. */
  18. public abstract class AbstractPanel extends AbstractGuiComponent {
  19. /**
  20. * List of children components.
  21. */
  22. private final List<IGuiComponent> childrenComponents;
  23. /**
  24. * Background Image of this component. NULL if not applicable.
  25. *
  26. * If both an image and a color are set, the image is drawn on top.
  27. */
  28. private Image backgroundImage;
  29. /**
  30. * Background Color of this component. NULL if not applicable.
  31. *
  32. * If both an image and a color are set, the image is drawn on top.
  33. */
  34. private Color backgroundColor;
  35. /**
  36. * Constructor.
  37. *
  38. * @param newShape
  39. * a rectangle representing the panel shape.
  40. */
  41. protected AbstractPanel(final Shape newShape) {
  42. super(newShape);
  43. this.childrenComponents = new ArrayList<IGuiComponent>();
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. @Override
  49. public void render(final GUIContext container, final Graphics graphics) {
  50. if (this.backgroundColor != SystemConstants.NULL) {
  51. final Color previousColor = graphics.getColor();
  52. graphics.setColor(this.backgroundColor);
  53. graphics.fill(this.getShape());
  54. graphics.setColor(previousColor);
  55. }
  56. if (this.backgroundImage != SystemConstants.NULL) {
  57. graphics.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight(), this.backgroundImage, 0, 0);
  58. }
  59. for (final IGuiComponent component : this.childrenComponents) {
  60. component.render(container, graphics);
  61. }
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. @Override
  67. public void mouseClicked(final int button, final int coordX, final int coordY, final int clickCount) {
  68. for (final IGuiComponent component : this.childrenComponents) {
  69. if (component.contains(coordX, coordY)) {
  70. component.mouseClicked(button, coordX, coordY, clickCount);
  71. }
  72. }
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. @Override
  78. public void mousePressed(final int button, final int coordX, final int coordY) {
  79. for (final IGuiComponent component : this.childrenComponents) {
  80. if (component.contains(coordX, coordY)) {
  81. component.mousePressed(button, coordX, coordY);
  82. }
  83. }
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. @Override
  89. public void mouseReleased(final int button, final int coordX, final int coordY) {
  90. for (final IGuiComponent component : this.childrenComponents) {
  91. if (component.contains(coordX, coordY)) {
  92. component.mouseReleased(button, coordX, coordY);
  93. }
  94. }
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. @Override
  100. public void mouseDragged(final int oldX, final int oldY, final int newX, final int newY) {
  101. for (final IGuiComponent component : this.childrenComponents) {
  102. if (component.contains(newX, newY) || component.contains(oldX, oldY)) {
  103. component.mouseDragged(oldX, oldY, newX, newY);
  104. }
  105. }
  106. }
  107. /**
  108. * {@inheritDoc}
  109. */
  110. @Override
  111. public void mouseMoved(final int oldX, final int oldY, final int newX, final int newY) {
  112. for (final IGuiComponent component : this.childrenComponents) {
  113. if (component.contains(newX, newY) || component.contains(oldX, oldY)) {
  114. component.mouseMoved(oldX, oldY, newX, newY);
  115. }
  116. }
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. @Override
  122. public void mouseWheelMoved(final int charge, final int coordX, final int coordY) {
  123. for (final IGuiComponent component : this.childrenComponents) {
  124. if (component.contains(coordX, coordY)) {
  125. component.mouseWheelMoved(charge, coordX, coordY);
  126. }
  127. }
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. @Override
  133. public void keyPressed(final int key, final char character) {
  134. for (final IGuiComponent component : this.childrenComponents) {
  135. component.keyPressed(key, character);
  136. }
  137. }
  138. /**
  139. *
  140. * {@inheritDoc}
  141. */
  142. @Override
  143. public void setLocation(final int newPosX, final int newPosY) {
  144. final int deltaX = this.getX() - newPosX;
  145. final int deltaY = this.getY() - newPosY;
  146. for (final IGuiComponent child : this.getChildrenComponents()) {
  147. child.setLocation(child.getX() - deltaX, child.getY() - deltaY);
  148. }
  149. super.setLocation(newPosX, newPosY);
  150. }
  151. /**
  152. *
  153. * {@inheritDoc}
  154. */
  155. @Override
  156. public void setX(final int newX) {
  157. final int delta = this.getX() - newX;
  158. for (final IGuiComponent child : this.getChildrenComponents()) {
  159. child.setX(child.getX() - delta);
  160. }
  161. super.setX(newX);
  162. }
  163. /**
  164. *
  165. * {@inheritDoc}
  166. */
  167. @Override
  168. public void setY(final int newY) {
  169. final int delta = this.getY() - newY;
  170. for (final IGuiComponent child : this.getChildrenComponents()) {
  171. child.setY(child.getY() - delta);
  172. }
  173. super.setY(newY);
  174. }
  175. /**
  176. * Set the background image.
  177. *
  178. * @param background
  179. * background image.
  180. */
  181. public final void setBackground(final Image background) {
  182. this.backgroundImage = background;
  183. }
  184. /**
  185. * Set the background color.
  186. *
  187. * @param background
  188. * background color.
  189. */
  190. public void setBackground(final Color background) {
  191. this.backgroundColor = background;
  192. }
  193. /**
  194. * Get the background color.
  195. *
  196. * @return The color of the background or <code>null</code> if it is not defined.
  197. */
  198. public Color getBackgroundColor() {
  199. return this.backgroundColor;
  200. }
  201. /**
  202. * Get the background image.
  203. *
  204. * @return The image of the background or <code>null</code> if it is not defined.
  205. */
  206. public Image getBackgroundImage() {
  207. return this.backgroundImage;
  208. }
  209. /**
  210. * Add a new children component.
  211. *
  212. * @param component
  213. * new children component.
  214. */
  215. public void add(final IGuiComponent component) {
  216. if (!this.childrenComponents.contains(component)) {
  217. this.childrenComponents.add(component);
  218. }
  219. }
  220. /**
  221. * Remove a children component.
  222. *
  223. * @param component
  224. * children component to remove.
  225. */
  226. public void remove(final IGuiComponent component) {
  227. this.childrenComponents.remove(component);
  228. }
  229. /**
  230. * Get the list of children components.
  231. *
  232. * @return list of children components.
  233. */
  234. public final List<IGuiComponent> getChildrenComponents() {
  235. return this.childrenComponents;
  236. }
  237. }