OSDN Git Service

List: added ability to select 'nothing', and ability to disable item selection ...
authorSG57 <jordansg57@gmail.com>
Sat, 1 Jun 2013 06:40:01 +0000 (00:40 -0600)
committerSG57 <jordansg57@gmail.com>
Sat, 1 Jun 2013 06:40:01 +0000 (00:40 -0600)
This adds functionality to the List Actor so it may behave like that of a ListView akin to Android's.  This is backwards compatible.

This is essential in situations where you may not always want to have an item selected (like a touch-based file browser), or where you may not want items even selectable at all (like during a tutorial).

The bug/defect fix was if you look in the old setSelection(String name) method, you'll see it sets the selectedIndex to -1, then tries to find an item with that name.  When it doesn't, it leaves the selectedIndex at -1 which essentially sets it to nothing.

*Note*
When the List is NOT selectable, it will no longer eat its touch event meaning they will still propagate down.

I'm not 100% sure if that is the behavior you want versus the contrary where it eats the touch event despite not doing anything.  Regardless, I have a note of this behavior change in the setSelectable comment.

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

index 0b507c4..633ef5d 100644 (file)
@@ -43,6 +43,7 @@ public class List extends Widget implements Cullable {
        private float prefWidth, prefHeight;\r
        private float itemHeight;\r
        private float textOffsetX, textOffsetY;\r
+       private boolean selectable;\r
 \r
        public List (Object[] items, Skin skin) {\r
                this(items, skin.get(ListStyle.class));\r
@@ -57,15 +58,33 @@ public class List extends Widget implements Cullable {
                setItems(items);\r
                setWidth(getPrefWidth());\r
                setHeight(getPrefHeight());\r
+               setSelectable(true);\r
 \r
                addListener(new InputListener() {\r
                        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {\r
                                if (pointer == 0 && button != 0) return false;\r
+                               if (!List.this.isSelectable()) return false; // don't eat touch event when NOT selectable\r
                                List.this.touchDown(y);\r
                                return true;\r
                        }\r
                });\r
        }\r
+       \r
+       /**\r
+        * Sets whether or not this List's items are selectable.\r
+        *\r
+        * *NOTE*\r
+        * If the List is NOT selectable, it will NOT eat its touch events meaning they will still propagate down.\r
+        * Please be aware of this behavior change.\r
+        */\r
+       public void setSelectable(boolean s) {\r
+               selectable = s;\r
+       }\r
+       \r
+       /** @return Whether or not the List's items are selectable. */\r
+       public boolean isSelectable() {\r
+               return selectable;\r
+       }\r
 \r
        void touchDown (float y) {\r
                int oldIndex = selectedIndex;\r
@@ -126,7 +145,7 @@ public class List extends Widget implements Cullable {
                }\r
        }\r
 \r
-       /** @return The index of the currently selected item. The top item has an index of 0. */\r
+       /** @return The index of the currently selected item. The top item has an index of 0. Nothing selected has an index of -1. */\r
        public int getSelectedIndex () {\r
                return selectedIndex;\r
        }\r
@@ -137,15 +156,19 @@ public class List extends Widget implements Cullable {
                selectedIndex = index;\r
        }\r
 \r
-       /** @return The text of the currently selected item or null if the list is empty. */\r
+       /** @return The text of the currently selected item, else null if the list is empty or nothing is selected. */\r
        public String getSelection () {\r
-               if (items.length == 0) return null;\r
+               if (items.length == 0 || isNothingSelected()) return null;\r
                return items[selectedIndex];\r
        }\r
 \r
-       /** @return The index of the item that was selected, or -1. */\r
+       /**\r
+        * Sets the selection to the item if found, else sets the selection to nothing.\r
+        * @return The new index of the list selection\r
+        */\r
        public int setSelection (String item) {\r
-               selectedIndex = -1;\r
+               setSelectionToNothing();\r
+               \r
                for (int i = 0, n = items.length; i < n; i++) {\r
                        if (items[i].equals(item)) {\r
                                selectedIndex = i;\r
@@ -155,6 +178,17 @@ public class List extends Widget implements Cullable {
                return selectedIndex;\r
        }\r
 \r
+       /** Sets the list item selection to nothing with selection index value -1. */\r
+       public void setSelectionToNothing() {\r
+               selectedIndex = -1;\r
+       }\r
+       \r
+       /** @return Whether or not the list has an item selected. */\r
+       public boolean isNothingSelected() {\r
+               return selectedIndex == -1;\r
+       }\r
+       \r
+\r
        public void setItems (Object[] objects) {\r
                if (objects == null) throw new IllegalArgumentException("items cannot be null.");\r
 \r