PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/List.java

https://github.com/snovs065/libgdx
Java | 232 lines | 171 code | 33 blank | 28 comment | 33 complexity | 5e4b7dbd91d03695631d626fec0af491 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1, CC-BY-SA-3.0
  1. /*******************************************************************************
  2. * Copyright 2011 See AUTHORS file.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package com.badlogic.gdx.scenes.scene2d.ui;
  17. import com.badlogic.gdx.graphics.Color;
  18. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  19. import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
  20. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  21. import com.badlogic.gdx.math.Rectangle;
  22. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  23. import com.badlogic.gdx.scenes.scene2d.InputListener;
  24. import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
  25. import com.badlogic.gdx.scenes.scene2d.utils.Cullable;
  26. import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
  27. import com.badlogic.gdx.utils.GdxRuntimeException;
  28. import com.badlogic.gdx.utils.Pools;
  29. /** A list (aka list box) displays textual items and highlights the currently selected item.
  30. * <p>
  31. * {@link ChangeEvent} is fired when the list selection changes.
  32. * <p>
  33. * The preferred size of the list is determined by the text bounds of the items and the size of the {@link ListStyle#selection}.
  34. * @author mzechner */
  35. public class List extends Widget implements Cullable {
  36. private ListStyle style;
  37. private String[] items;
  38. private int selectedIndex;
  39. private Rectangle cullingArea;
  40. private float prefWidth, prefHeight;
  41. private float itemHeight;
  42. private float textOffsetX, textOffsetY;
  43. public List (Object[] items, Skin skin) {
  44. this(items, skin.get(ListStyle.class));
  45. }
  46. public List (Object[] items, Skin skin, String styleName) {
  47. this(items, skin.get(styleName, ListStyle.class));
  48. }
  49. public List (Object[] items, ListStyle style) {
  50. setStyle(style);
  51. setItems(items);
  52. setWidth(getPrefWidth());
  53. setHeight(getPrefHeight());
  54. addListener(new InputListener() {
  55. public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
  56. if (pointer == 0 && button != 0) return false;
  57. List.this.touchDown(y);
  58. return true;
  59. }
  60. });
  61. }
  62. void touchDown (float y) {
  63. int oldIndex = selectedIndex;
  64. selectedIndex = (int)((getHeight() - y) / itemHeight);
  65. selectedIndex = Math.max(0, selectedIndex);
  66. selectedIndex = Math.min(items.length - 1, selectedIndex);
  67. if (oldIndex != selectedIndex) {
  68. ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class);
  69. if (fire(changeEvent)) selectedIndex = oldIndex;
  70. Pools.free(changeEvent);
  71. }
  72. }
  73. public void setStyle (ListStyle style) {
  74. if (style == null) throw new IllegalArgumentException("style cannot be null.");
  75. this.style = style;
  76. if (items != null)
  77. setItems(items);
  78. else
  79. invalidateHierarchy();
  80. }
  81. /** Returns the list's style. Modifying the returned style may not have an effect until {@link #setStyle(ListStyle)} is called. */
  82. public ListStyle getStyle () {
  83. return style;
  84. }
  85. @Override
  86. public void draw (SpriteBatch batch, float parentAlpha) {
  87. BitmapFont font = style.font;
  88. Drawable selectedDrawable = style.selection;
  89. Color fontColorSelected = style.fontColorSelected;
  90. Color fontColorUnselected = style.fontColorUnselected;
  91. Color color = getColor();
  92. batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
  93. float x = getX();
  94. float y = getY();
  95. font.setColor(fontColorUnselected.r, fontColorUnselected.g, fontColorUnselected.b, fontColorUnselected.a * parentAlpha);
  96. float itemY = getHeight();
  97. for (int i = 0; i < items.length; i++) {
  98. if (cullingArea == null || (itemY - itemHeight <= cullingArea.y + cullingArea.height && itemY >= cullingArea.y)) {
  99. if (selectedIndex == i) {
  100. selectedDrawable.draw(batch, x, y + itemY - itemHeight, prefWidth, itemHeight);
  101. font.setColor(fontColorSelected.r, fontColorSelected.g, fontColorSelected.b, fontColorSelected.a * parentAlpha);
  102. }
  103. font.draw(batch, items[i], x + textOffsetX, y + itemY - textOffsetY);
  104. if (selectedIndex == i) {
  105. font.setColor(fontColorUnselected.r, fontColorUnselected.g, fontColorUnselected.b, fontColorUnselected.a
  106. * parentAlpha);
  107. }
  108. } else if (itemY < cullingArea.y) {
  109. break;
  110. }
  111. itemY -= itemHeight;
  112. }
  113. }
  114. /** @return The index of the currently selected item. The top item has an index of 0. */
  115. public int getSelectedIndex () {
  116. return selectedIndex;
  117. }
  118. public void setSelectedIndex (int index) {
  119. if (index < 0 || index >= items.length)
  120. throw new GdxRuntimeException("index must be >= 0 and < " + items.length + ": " + index);
  121. selectedIndex = index;
  122. }
  123. /** @return The text of the currently selected item or null if the list is empty. */
  124. public String getSelection () {
  125. if (items.length == 0) return null;
  126. return items[selectedIndex];
  127. }
  128. /** @return The index of the item that was selected, or -1. */
  129. public int setSelection (String item) {
  130. selectedIndex = -1;
  131. for (int i = 0, n = items.length; i < n; i++) {
  132. if (items[i].equals(item)) {
  133. selectedIndex = i;
  134. break;
  135. }
  136. }
  137. return selectedIndex;
  138. }
  139. public void setItems (Object[] objects) {
  140. if (objects == null) throw new IllegalArgumentException("items cannot be null.");
  141. if (!(objects instanceof String[])) {
  142. String[] strings = new String[objects.length];
  143. for (int i = 0, n = objects.length; i < n; i++)
  144. strings[i] = String.valueOf(objects[i]);
  145. items = strings;
  146. } else
  147. items = (String[])objects;
  148. selectedIndex = 0;
  149. final BitmapFont font = style.font;
  150. final Drawable selectedDrawable = style.selection;
  151. itemHeight = font.getCapHeight() - font.getDescent() * 2;
  152. itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight();
  153. textOffsetX = selectedDrawable.getLeftWidth();
  154. textOffsetY = selectedDrawable.getTopHeight() - font.getDescent();
  155. prefWidth = 0;
  156. for (int i = 0; i < items.length; i++) {
  157. TextBounds bounds = font.getBounds(items[i]);
  158. prefWidth = Math.max(bounds.width, prefWidth);
  159. }
  160. prefWidth += selectedDrawable.getLeftWidth() + selectedDrawable.getRightWidth();
  161. prefHeight = items.length * itemHeight;
  162. invalidateHierarchy();
  163. }
  164. public String[] getItems () {
  165. return items;
  166. }
  167. public float getPrefWidth () {
  168. return prefWidth;
  169. }
  170. public float getPrefHeight () {
  171. return prefHeight;
  172. }
  173. public void setCullingArea (Rectangle cullingArea) {
  174. this.cullingArea = cullingArea;
  175. }
  176. /** The style for a list, see {@link List}.
  177. * @author mzechner
  178. * @author Nathan Sweet */
  179. static public class ListStyle {
  180. public BitmapFont font;
  181. public Color fontColorSelected = new Color(1, 1, 1, 1);
  182. public Color fontColorUnselected = new Color(1, 1, 1, 1);
  183. public Drawable selection;
  184. public ListStyle () {
  185. }
  186. public ListStyle (BitmapFont font, Color fontColorSelected, Color fontColorUnselected, Drawable selection) {
  187. this.font = font;
  188. this.fontColorSelected.set(fontColorSelected);
  189. this.fontColorUnselected.set(fontColorUnselected);
  190. this.selection = selection;
  191. }
  192. public ListStyle (ListStyle style) {
  193. this.font = style.font;
  194. this.fontColorSelected.set(style.fontColorSelected);
  195. this.fontColorUnselected.set(style.fontColorUnselected);
  196. this.selection = style.selection;
  197. }
  198. }
  199. }