Skip to content

Commit 1358733

Browse files
committed
Changed hit.
1 parent 65ff73b commit 1358733

File tree

9 files changed

+226
-111
lines changed

9 files changed

+226
-111
lines changed

CHANGES

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
- `getMaxRadius` has been renamed to `getCurrentRadius`.
66
- `minRadius` has been renamed to `preferredRadius`.
77
- Introduced a new `individuals` demo (named "RadialButtons") to showcase the new `RadialGroup` class.
8-
- Hotfix for a `RadialGroup` containing other `PieMenu`.
9-
- Hotfix on `hit` for when a contained Actor extends beyond the limits of the widget itself.
10-
- Hotfix on `hit` for the `middleCancel` flag of `PieMenu`.
8+
- Changed the way the scene2d `hit` method works with the widget.
9+
- Fixed the `hit` for when a contained Actor extends beyond the limits of the widget itself.
10+
- Fixed `RadialGroup` containing other `PieMenu`.
11+
- Fixed non-regular width and height values on Animated widgets.
12+
- Fixed `centerOnActor` method used on an Actor that was contained within a `Group`.
13+
todo: should PieWidget `hit` return `this` when hitting a background ? (define "isBackgroundHit()" method for custom approach without duplicating `hit`)
1114

1215
[3.1.0]
13-
- Hotfix for "floating" `Group` that contained `RadialGroup`.
16+
- Fixed the "floating" `Group` that contained `RadialGroup`.
1417
- More efficient evaluation for the reset of the hover on mouse-exit.
1518
- The `background` image can now be a `Drawable` as well (but keep in mind that `Drawable` cannot be rotated).
1619

src/main/java/com/payne/games/piemenu/AnimatedPieMenu.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.badlogic.gdx.scenes.scene2d.Actor;
88
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
99
import com.badlogic.gdx.utils.Align;
10+
1011
import java.util.HashMap;
1112

1213

@@ -286,7 +287,7 @@ public void layout() {
286287
vector2.set(dist, 0);
287288
vector2.rotate(degreesPerChild*(i + half) + startDegreesOffset);
288289
modifyActor(actor, degreesPerChild, dist); // overridden by user
289-
actor.setPosition(vector2.x+ getCurrentRadius(), vector2.y+ getCurrentRadius(), Align.center);
290+
actor.setPosition(vector2.x + getWidth()/2, vector2.y + getHeight()/2, Align.center);
290291

291292
/* Updating alpha (fade-in animation). */
292293
if(isCurrentlyAnimated()) {

src/main/java/com/payne/games/piemenu/AnimatedPieWidget.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.badlogic.gdx.scenes.scene2d.Actor;
88
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
99
import com.badlogic.gdx.utils.Align;
10+
1011
import java.util.HashMap;
1112

1213

@@ -286,7 +287,7 @@ public void layout() {
286287
vector2.set(dist, 0);
287288
vector2.rotate(degreesPerChild*(i + half) + startDegreesOffset);
288289
modifyActor(actor, degreesPerChild, dist); // overridden by user
289-
actor.setPosition(vector2.x+ getCurrentRadius(), vector2.y+ getCurrentRadius(), Align.center);
290+
actor.setPosition(vector2.x + getWidth()/2, vector2.y + getHeight()/2, Align.center);
290291

291292
/* Updating alpha (fade-in animation). */
292293
if(isCurrentlyAnimated()) {

src/main/java/com/payne/games/piemenu/PieMenu.java

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
import com.badlogic.gdx.graphics.Color;
55
import com.badlogic.gdx.graphics.g2d.TextureRegion;
66
import com.badlogic.gdx.math.Vector2;
7-
import com.badlogic.gdx.scenes.scene2d.Actor;
8-
import com.badlogic.gdx.scenes.scene2d.Event;
9-
import com.badlogic.gdx.scenes.scene2d.EventListener;
10-
import com.badlogic.gdx.scenes.scene2d.InputEvent;
11-
import com.badlogic.gdx.scenes.scene2d.InputListener;
12-
import com.badlogic.gdx.scenes.scene2d.Touchable;
7+
import com.badlogic.gdx.scenes.scene2d.*;
138
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
149
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
1510
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
@@ -18,7 +13,8 @@
1813

1914
/**
2015
* A PieMenu reuses the {@link PieWidget}'s functionalities to provide a way
21-
* to interact with the contained Actors through the "hit-box" of the slices.
16+
* to interact with the contained Actors through the "hit-box" of the slices.<br/>
17+
* Basically, each slice ends up acting like a button.
2218
*
2319
* @author Jérémi Grenier-Berthiaume (aka "payne")
2420
*/
@@ -388,21 +384,6 @@ public int findChildIndexAtStage(float x, float y) {
388384
: getAmountOfChildren(); // "getAmountOfChildren" is equivalent to "invalid"
389385
}
390386

391-
/**
392-
* Checks whether or not the input coordinate is within (exclusively)
393-
* the inner-radius.
394-
*
395-
* @param x x-coordinate relative to the center of the widget's
396-
* @param y y-coordinate relative to the center of the widget's
397-
* @return 'true' only if the coordinates fall within the widget's
398-
* inner radius.
399-
*/
400-
public boolean isWithinInnerRadius(float x, float y) {
401-
float distance = pow2(x) + pow2(y);
402-
float innerRadSquared = pow2(getInnerRadiusLength());
403-
return distance < innerRadSquared;
404-
}
405-
406387
/**
407388
* Used to transform an index into a known range: it'll either remain itself
408389
* if it was designating a valid child index, else it becomes the
@@ -415,43 +396,6 @@ public int mapIndex(int index) {
415396
return isValidIndex(index) ? index : defaultIndex;
416397
}
417398

418-
/**
419-
* @param x x-coordinate relative to the origin (bottom left) of the widget
420-
* @param y y-coordinate relative to the origin (bottom left) of the widget
421-
* @param touchable if {@code true}, hit detection will respect the
422-
* {@link #setTouchable(Touchable) touchability}.
423-
* @return deepest child's hit at (x,y). Else, the widget itself if it's
424-
* the background. Else {@code null} if {@link #middleCancel} is
425-
* {@code true} and within the {@link #getInnerRadiusLength()}.
426-
* Else the widget itself if with infinite range. Else {@code null}.
427-
*/
428-
@Override
429-
public Actor hit(float x, float y, boolean touchable) {
430-
if (touchable && getTouchable() == Touchable.disabled) return null;
431-
if (!isVisible()) return null;
432-
433-
localToStageCoordinates(vector2.set(x,y));
434-
int childIndex = findChildIndexAtStage(vector2.x,vector2.y);
435-
if (isValidIndex(childIndex)) {
436-
if(middleCancel && isWithinInnerRadius(x + getWidth()/2, y + getHeight()/2))
437-
return null;
438-
Actor child = getChildren().get(childIndex);
439-
if(child.getTouchable() == Touchable.disabled)
440-
return this;
441-
child.parentToLocalCoordinates(vector2.set(x,y));
442-
Actor hit = child.hit(vector2.x, vector2.y, touchable);
443-
if(hit != null)
444-
return hit;
445-
else
446-
return this;
447-
}
448-
449-
if(infiniteSelectionRange)
450-
return this;
451-
452-
return null;
453-
}
454-
455399
@Override
456400
public Color getColor(int index) {
457401
if(style.hoverSelectedColor != null

src/main/java/com/payne/games/piemenu/PieWidget.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
import com.badlogic.gdx.utils.Align;
1313
import space.earlygrey.shapedrawer.ShapeDrawer;
1414

15+
1516
/**
1617
* A PieWidget aims at providing the user with a simple way to lay out
1718
* the contained Actors in a circular fashion. It uses a
18-
* {@link ShapeDrawer} to draw certain elements, when necessary.
19-
* Basically, it uses a Style (compared to the {@link RadialGroup}).
19+
* {@link ShapeDrawer} to draw certain elements, when necessary.<br/>
20+
* Basically, if you are not finding yourself happy with the results
21+
* coming from using a {@link PieWidgetStyle}, then you might want to
22+
* consider using a simple {@link RadialGroup}.
2023
*
2124
* @author Jérémi Grenier-Berthiaume (aka "payne")
2225
*/
@@ -404,13 +407,16 @@ protected void drawWithShapeDrawer(Batch batch, float parentAlpha, float degrees
404407
float restoreAlpha = bc.a;
405408
batch.setColor(bc.r, bc.g, bc.b, bc.a * globalAlphaMultiplier);
406409
if(style.background instanceof TransformDrawable) {
410+
// todo: can "getX()" screw up because relative to parent? (TO TEST)
407411
((TransformDrawable)(style.background)).draw(batch,
408-
getX(Align.center) - getCurrentRadius(), getY(Align.center) - getCurrentRadius(),
412+
getX(Align.center) - getCurrentRadius(),
413+
getY(Align.center) - getCurrentRadius(),
409414
getCurrentRadius(), getCurrentRadius(),
410415
getCurrentDiameter(), getCurrentDiameter(),
411416
getScaleX(), getScaleY(),
412417
getRotation());
413418
} else {
419+
// todo: can "getX()" screw up because relative to parent? (TO TEST)
414420
style.background.draw(batch,
415421
getX(Align.center) - getCurrentRadius(),
416422
getY(Align.center) - getCurrentRadius(),
@@ -449,6 +455,20 @@ protected void drawChildSeparator(Vector2 vector2, float drawnRadianAngle) {
449455
}
450456
}
451457

458+
/**
459+
* To get the coordinates of a point along a line traced from the center,
460+
* along the designated angle, at the designated distance from the center.
461+
*
462+
* @param center center point of the Circle
463+
* @param radius how far from the center the desired point is
464+
* @param radian the angle along which the line is calculated
465+
* @return the point associated with those parameters
466+
*/
467+
public Vector2 pointAtAngle(Vector2 output, Vector2 center, float radius, float radian) {
468+
output.set(center.x + radius * MathUtils.cos(radian), center.y + radius * MathUtils.sin(radian));
469+
return output;
470+
}
471+
452472
/**
453473
* Determines the color of the slice in which resides the Actor designated
454474
* by the {@code index} parameter. By default, the colors come from the way
@@ -501,7 +521,19 @@ protected void drawChildCircumference(Vector2 vector2, float startAngle, float r
501521
sd.arc(vector2.x, vector2.y, radius, startAngle, radian, style.circumferenceWidth);
502522
}
503523
}
504-
/*
524+
525+
526+
527+
528+
529+
530+
531+
532+
533+
534+
535+
536+
/*
505537
=================================== STYLE ==================================
506538
*/
507539

src/main/java/com/payne/games/piemenu/RadialGroup.java

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,6 @@ protected void updateOrigin() {
340340
setOrigin(getWidth()/2, getHeight()/2); // to support rotations around the center
341341
}
342342

343-
344-
/**
345-
* To get the coordinates of a point along a line traced from the center,
346-
* along the designated angle, at the designated distance from the center.
347-
*
348-
* @param center center point of the Circle
349-
* @param radius how far from the center the desired point is
350-
* @param radian the angle along which the line is calculated
351-
* @return the point associated with those parameters
352-
*/
353-
public Vector2 pointAtAngle(Vector2 output, Vector2 center, float radius, float radian) {
354-
output.set(center.x + radius * MathUtils.cos(radian), center.y + radius * MathUtils.sin(radian));
355-
return output;
356-
}
357-
358343
/**
359344
* @param x x-coordinate relative to the origin (bottom left) of the widget
360345
* @param y y-coordinate relative to the origin (bottom left) of the widget
@@ -368,23 +353,41 @@ public Actor hit(float x, float y, boolean touchable) {
368353
if (touchable && getTouchable() == Touchable.disabled) return null;
369354
if (!isVisible()) return null;
370355

371-
localToStageCoordinates(vector2.set(x,y));
372-
int childIndex = findChildIndexAtStage(vector2.x,vector2.y);
373-
if (isValidIndex(childIndex)) {
374-
Actor child = getChildren().get(childIndex);
356+
/* Checking all children first to catch the ones extending beyond the widget. */
357+
for (Actor child : getChildren()) {
375358
if(child.getTouchable() == Touchable.disabled)
376-
return this;
359+
continue;
377360
child.parentToLocalCoordinates(vector2.set(x,y));
378361
Actor hit = child.hit(vector2.x, vector2.y, touchable);
379-
if(hit != null)
362+
if(hit != null) {
363+
// System.out.println("hit");
380364
return hit;
381-
else
382-
return this;
365+
}
366+
}
367+
368+
/* Then we want to consider the widget's boundaries itself. */
369+
localToStageCoordinates(vector2.set(x,y));
370+
int childIndex = findChildIndexAtStage(vector2.x,vector2.y);
371+
if(isValidIndex(childIndex)) {
372+
// System.out.println("this valid");
373+
return this;
383374
}
384375

376+
// // todo: shouldn't return `null` if hitting background
377+
// if(isBackgroundHit(x,y)) {
378+
// System.out.println("this background");
379+
// return this;
380+
// }
381+
382+
// System.out.println("null");
385383
return null;
386384
}
387385

386+
// public boolean isBackgroundHit(float x, float y) {
387+
// stageToLocalCoordinates(vector2.set(x,y));
388+
// return isWithinInnerRadius(vector2.x - getWidth()/2, vector2.y - getHeight()/2);
389+
// }
390+
388391
/**
389392
* Given a coordinate, find the index of the child (if any).
390393
*
@@ -429,7 +432,7 @@ public float angleAtStage(float x, float y) {
429432
return normalizeAngle(
430433
MathUtils.radiansToDegrees
431434
* MathUtils.atan2(y - vector2.y, x - vector2.x)
432-
- getTotalRotation() - startDegreesOffset
435+
- getTotalRotation() - startDegreesOffset
433436
);
434437
}
435438

@@ -461,14 +464,26 @@ public float normalizeAngle(float angle) {
461464
return ((angle % 360 + 360) % 360);
462465
}
463466

467+
/**
468+
* Given a child index, find whether or not it would be a valid candidate to
469+
* highlight or select.
470+
*
471+
* @param index an integer that would usually be the output of
472+
* {@link #findChildIndexAtStage(float, float)}.
473+
* @return {@code true} only if the index is linked to a valid child sector.
474+
*/
475+
public boolean isValidIndex(int index) {
476+
return !(index <= -1 || index >= getAmountOfChildren());
477+
}
478+
464479
/**
465480
* Checks whether or not the input coordinate is in between (inclusively)
466481
* the inner-radius and the current radius of the widget (which can
467482
* be bigger than {@link #preferredRadius} if you use {@link #setFillParent(boolean)},
468483
* for example).
469484
*
470-
* @param x x-coordinate relative to the center of the widget's
471-
* @param y y-coordinate relative to the center of the widget's
485+
* @param x x-coordinate relative to the center of the widget.
486+
* @param y y-coordinate relative to the center of the widget.
472487
* @return 'true' only if the coordinates fall within the widget's radii.
473488
*/
474489
public boolean isWithinRadii(float x, float y) {
@@ -478,6 +493,21 @@ public boolean isWithinRadii(float x, float y) {
478493
return distance >= innerRadSquared && distance <= radSquared;
479494
}
480495

496+
/**
497+
* Checks whether or not the input coordinate is within (exclusively)
498+
* the inner-radius.
499+
*
500+
* @param x x-coordinate relative to the center of the widget.
501+
* @param y y-coordinate relative to the center of the widget.
502+
* @return 'true' only if the coordinates fall within the widget's
503+
* inner radius.
504+
*/
505+
public boolean isWithinInnerRadius(float x, float y) {
506+
float distance = pow2(x) + pow2(y);
507+
float innerRadSquared = pow2(getInnerRadiusLength());
508+
return distance < innerRadSquared;
509+
}
510+
481511
/**
482512
* Returns the input to the power of 2.
483513
*
@@ -511,20 +541,8 @@ public void centerOnScreen() {
511541
public void centerOnActor(Actor actor) {
512542
if(actor == null)
513543
return;
514-
// todo: possibly needs to be fixed for Contained Actors ?
515-
setPosition(actor.getX(Align.center), actor.getY(Align.center), Align.center);
516-
}
517-
518-
/**
519-
* Given a child index, find whether or not it would be a valid candidate to
520-
* highlight or select.
521-
*
522-
* @param index an integer that would usually be the output of
523-
* {@link #findChildIndexAtStage(float, float)}.
524-
* @return {@code true} only if the index is linked to a valid child sector.
525-
*/
526-
public boolean isValidIndex(int index) {
527-
return !(index <= -1 || index >= getAmountOfChildren());
544+
actor.localToStageCoordinates(vector2.set(actor.getWidth()/2, actor.getHeight()/2));
545+
setPosition(vector2.x, vector2.y, Align.center);
528546
}
529547

530548

0 commit comments

Comments
 (0)