OSDN Git Service

SelectBox: added set max # items to display in dropdown
authorCord Rehn <jordansg57@gmail.com>
Sun, 2 Jun 2013 13:11:00 +0000 (07:11 -0600)
committerCord Rehn <jordansg57@gmail.com>
Sun, 2 Jun 2013 13:11:00 +0000 (07:11 -0600)
Added backwards compatible functionality for configuring the max # of items to display in the dropdown SelectList.

You set the maximum number of items you want displayed in the dropdown box until scrolling is required, or <= 0 to have it list them all, but should it reach the bottom/top of the screen then scrolling is required.

This is also a solution to an API defect. There was no previous easy way to modify the inner SelectList's prefHeight() to adjust the dropdown list's displayed height.

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

index 1cc4320..19fd3cd 100644 (file)
@@ -54,6 +54,7 @@ public class SelectBox extends Widget {
        SelectList list;\r
        private float prefWidth, prefHeight;\r
        private ClickListener clickListener;\r
+       private int maxDropdownCount;\r
 \r
        public SelectBox (Object[] items, Skin skin) {\r
                this(items, skin.get(SelectBoxStyle.class));\r
@@ -68,6 +69,7 @@ public class SelectBox extends Widget {
                setItems(items);\r
                setWidth(getPrefWidth());\r
                setHeight(getPrefHeight());\r
+               setMaxDropdownCount(0);\r
 \r
                addListener(clickListener = new ClickListener() {\r
                        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {\r
@@ -80,6 +82,19 @@ public class SelectBox extends Widget {
                });\r
        }\r
 \r
+       /**\r
+        * Set the max number of dropdown List items to display when the box is opened.\r
+        * @param num_items Max number of items to display, or <= 0 to display them all\r
+        */\r
+       public void setMaxDropdownCount(int num_items) {\r
+               maxDropdownCount = num_items;\r
+       }\r
+\r
+       /** @return Max number of dropdown List items to display when the box is opened, or <= 0 to display them all. */\r
+       public int getMaxDropdownCount() {\r
+               return maxDropdownCount;\r
+       }\r
+\r
        public void setStyle (SelectBoxStyle style) {\r
                if (style == null) throw new IllegalArgumentException("style cannot be null.");\r
                this.style = style;\r
@@ -255,7 +270,10 @@ public class SelectBox extends Widget {
                        list.setSelectedIndex(selectedIndex);\r
 \r
                        // Show the list above or below the select box, limited to the available height.\r
-                       float height = getPrefHeight();\r
+                       //float height = getPrefHeight(); // old method\r
+                       \r
+                       // set the available height to show the max desired dropdown items\r
+                       float height = list.getItemHeight() * ((getMaxDropdownCount() <= 0 ? list.getItems().length : Math.min(getMaxDropdownCount(), list.getItems().length)) + 1);\r
                        float heightBelow = tmpCoords.y;\r
                        float heightAbove = stage.getCamera().viewportHeight - tmpCoords.y - SelectBox.this.getHeight();\r
                        boolean below = true;\r