1212import com .badlogic .gdx .scenes .scene2d .Touchable ;
1313import com .badlogic .gdx .scenes .scene2d .ui .Skin ;
1414import com .badlogic .gdx .scenes .scene2d .utils .ChangeListener ;
15+ import com .badlogic .gdx .scenes .scene2d .utils .ChangeListener .ChangeEvent ;
1516import com .badlogic .gdx .scenes .scene2d .utils .ClickListener ;
1617import com .badlogic .gdx .utils .Pools ;
1718
@@ -32,27 +33,32 @@ public class PieMenu extends PieWidget {
3233 */
3334 private InputListener pieMenuListener ;
3435
36+ /**
37+ * Used to denote a non-selected index, or canceled selection, for example.
38+ */
39+ public static final int NO_SELECTION = -1 ;
40+
3541 /**
3642 * The index that is used as a fallback value whenever a processed
3743 * user-input does not map to a valid child index value.<br>
3844 * This value can be negative, if you want nothing to be the default.
3945 */
40- private int defaultIndex = - 1 ;
46+ private int defaultIndex = NO_SELECTION ;
4147
4248 /**
4349 * Index of the currently selected item.
4450 */
45- private int selectedIndex = defaultIndex ;
51+ private int selectedIndex = NO_SELECTION ;
4652
4753 /**
4854 * Index of the currently highlighted item.
4955 */
50- private int highlightedIndex = defaultIndex ;
56+ private int highlightedIndex = NO_SELECTION ;
5157
5258 /**
5359 * Index of the currently highlighted item.
5460 */
55- private int hoveredIndex = - 1 ;
61+ private int hoveredIndex = NO_SELECTION ;
5662
5763 /**
5864 * Determines whether or not selection should only happen if the mouse is
@@ -62,12 +68,13 @@ public class PieMenu extends PieWidget {
6268
6369
6470 /**
65- * Determines whether or not releasing a click within the inner-radius
66- * should cancel the selection.<br>
67- * If {@code true}, a click released in the middle will trigger a selection
68- * of the {@link #defaultIndex}.<br>
69- * Only applies for the case where you have activated the
70- * {@link #infiniteSelectionRange} flag.
71+ * Determines whether or not releasing a click within the {@link #innerRadiusPercent
72+ * inner-radius} should "cancel" the selection.
73+ * If {@code true}, a click released in the middle will trigger a selection of the
74+ * {@link #defaultIndex}.<br>
75+ * As a corollary, this means that the {@link ChangeListener#changed(ChangeEvent, Actor)}
76+ * will still be called, no matter the value given to this variable.<br>
77+ * Only applies for the case where you have activated the {@link #infiniteSelectionRange} flag.
7178 */
7279 private boolean middleCancel = false ;
7380
@@ -369,7 +376,7 @@ public void resetSelection() {
369376 * (if you had set it up).
370377 */
371378 public void resetHover () {
372- hoveredIndex = - 1 ;
379+ hoveredIndex = NO_SELECTION ;
373380 }
374381
375382 @ Override
@@ -550,7 +557,7 @@ public PieMenuStyle() {
550557 *
551558 * @param style a Style to copy the parameters from.
552559 */
553- public PieMenuStyle (PieMenu . PieMenuStyle style ) {
560+ public PieMenuStyle (PieMenuStyle style ) {
554561 super (style );
555562 this .selectedColor = new Color (style .selectedColor );
556563 this .downColor = new Color (style .downColor );
@@ -589,8 +596,12 @@ public void selectIndex(int newIndex) {
589596
590597 selectedIndex = newIndex ;
591598 highlightedIndex = newIndex ;
592- if (newIndex != oldHighlightedIndex )
593- fire (new PieMenuHighlightChangeEvent (newIndex ));
599+ if (newIndex != oldHighlightedIndex ) {
600+ PieMenuHighlightChangeEvent highlightChangeEvent = Pools .obtain (PieMenuHighlightChangeEvent .class );
601+ highlightChangeEvent .newIndex = newIndex ;
602+ fire (highlightChangeEvent );
603+ Pools .free (highlightChangeEvent );
604+ }
594605
595606 ChangeListener .ChangeEvent changeEvent = Pools .obtain (ChangeListener .ChangeEvent .class );
596607 if (fire (changeEvent )) {
@@ -630,7 +641,10 @@ public void highlightIndex(int newIndex) {
630641 if (newIndex != highlightedIndex ) {
631642 highlightedIndex = newIndex ;
632643 resetHover ();
633- fire (new PieMenuHighlightChangeEvent (newIndex ));
644+ PieMenuHighlightChangeEvent highlightChangeEvent = Pools .obtain (PieMenuHighlightChangeEvent .class );
645+ highlightChangeEvent .newIndex = newIndex ;
646+ fire (highlightChangeEvent );
647+ Pools .free (highlightChangeEvent );
634648 }
635649 }
636650
@@ -665,7 +679,10 @@ public void hoverIndex(int newIndex) {
665679 newIndex = mapIndex (newIndex );
666680 if (newIndex != hoveredIndex ) {
667681 hoveredIndex = newIndex ;
668- fire (new PieMenuHoverChangeEvent (newIndex ));
682+ PieMenuHoverChangeEvent hoverChangeEvent = Pools .obtain (PieMenuHoverChangeEvent .class );
683+ hoverChangeEvent .newIndex = newIndex ;
684+ fire (hoverChangeEvent );
685+ Pools .free (hoverChangeEvent );
669686 }
670687 }
671688
@@ -780,7 +797,7 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
780797
781798 /* Reset the hover only when the mouse exits the PieMenu. */
782799 if (toActor != pieMenu && (toActor == null || !(toActor .isDescendantOf (pieMenu ))))
783- pieMenu .hoverIndex (- 1 );
800+ pieMenu .hoverIndex (NO_SELECTION );
784801 super .exit (event , x , y , pointer , toActor );
785802 }
786803 }
@@ -789,16 +806,20 @@ public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
789806 private static class PieMenuHighlightChangeEvent extends Event {
790807 private int newIndex ;
791808
792- public PieMenuHighlightChangeEvent (int newIndex ) {
793- this .newIndex = newIndex ;
809+ @ Override
810+ public void reset () {
811+ super .reset ();
812+ newIndex = NO_SELECTION ;
794813 }
795814 }
796815
797816 private static class PieMenuHoverChangeEvent extends Event {
798817 private int newIndex ;
799818
800- public PieMenuHoverChangeEvent (int newIndex ) {
801- this .newIndex = newIndex ;
819+ @ Override
820+ public void reset () {
821+ super .reset ();
822+ newIndex = NO_SELECTION ;
802823 }
803824 }
804825
@@ -920,20 +941,26 @@ public void setInfiniteSelectionRange(boolean infiniteSelectionRange) {
920941 }
921942
922943 /**
923- * Determines whether or not releasing a click within the inner-radius
924- * should cancel the selection.
925- * If {@code true} a release in the middle, even if {@link #infiniteSelectionRange}
926- * is set to {@code true}, will trigger a selection of the {@link #defaultIndex}.
944+ * Determines whether or not releasing a click within the {@link #innerRadiusPercent
945+ * inner-radius} should "cancel" the selection.
946+ * If {@code true}, a click released in the middle will trigger a selection of the
947+ * {@link #defaultIndex}.<br>
948+ * As a corollary, this means that the {@link ChangeListener#changed(ChangeEvent, Actor)}
949+ * will still be called, no matter the value given to this variable.<br>
950+ * Only applies for the case where you have activated the {@link #infiniteSelectionRange} flag.
927951 */
928952 public boolean isMiddleCancel () {
929953 return middleCancel ;
930954 }
931955
932956 /**
933- * Determines whether or not releasing a click within the inner-radius
934- * should cancel the selection.
935- * If {@code true} a release in the middle, even if {@link #infiniteSelectionRange}
936- * is set to {@code true}, will trigger a selection of the {@link #defaultIndex}.
957+ * Determines whether or not releasing a click within the {@link #innerRadiusPercent
958+ * inner-radius} should "cancel" the selection.
959+ * If {@code true}, a click released in the middle will trigger a selection of the
960+ * {@link #defaultIndex}.<br>
961+ * As a corollary, this means that the {@link ChangeListener#changed(ChangeEvent, Actor)}
962+ * will still be called, no matter the value given to this variable.<br>
963+ * Only applies for the case where you have activated the {@link #infiniteSelectionRange} flag.
937964 */
938965 public void setMiddleCancel (boolean middleCancel ) {
939966 this .middleCancel = middleCancel ;
0 commit comments