Skip to content

Commit

Permalink
Merge pull request agateau#450 from agateau/fix-select-track-nav
Browse files Browse the repository at this point in the history
fix select track nav
  • Loading branch information
agateau authored Aug 8, 2024
2 parents ce27fe6 + 8c78a9e commit 29fea66
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
9 changes: 6 additions & 3 deletions android/assets/screens/selecttrack.gdxui
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
<AnchorGroup id="root" gridSize="20">
<Label id="titleLabel" style="title" topCenter="root.topCenter 0 -1g">Select Track</Label>

<Menu id="menu" topCenter="titleLabel.bottomCenter 0 0" width="700" height="400"/>
<Image tiled="true" atlas="ui" name="garage-ground" topCenter="titleLabel.bottomCenter 0 -0.2"
width="960px" height="178px"/>

<Menu id="menu" topCenter="titleLabel.bottomCenter 0 -0.5" width="700" height="400"/>

<Label id="trackNameLabel" topCenter="menu.bottomCenter 0 0" align="center"/>
<Label id="unlockHintLabel" topCenter="$prev.bottomCenter 0 -1" align="center" style="small"/>

<Label id="lapRecordsLabel" bottomRight="root.bottomCenter -1.5g 4.5g" width="300" align="topCenter" style="small">Best Lap</Label>
<Label id="lapRecordsLabel" bottomRight="root.bottomCenter -1.5g 3.9g" width="300" align="topCenter" style="small">Best Lap</Label>
<Table id="lapRecordsTable" topLeft="$prev.bottomLeft 0 0" width="300"/>

<Label id="totalRecordsLabel" bottomLeft="root.bottomCenter 1.5g 4.5g" width="300" align="topCenter" style="small">Best Total</Label>
<Label id="totalRecordsLabel" bottomLeft="root.bottomCenter 1.5g 3.9g" width="300" align="topCenter" style="small">Best Total</Label>
<Table id="totalRecordsTable" topLeft="$prev.bottomLeft 0 0" width="300"/>
</AnchorGroup>
</gdxui>
1 change: 0 additions & 1 deletion android/assets/ui/uiskin.gdxjson
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
up: rectbutton,
down: rectbutton-down,
disabled: rectbutton-disabled,
unpressedOffsetY: 2,
pressedOffsetY: -2
}
},
Expand Down
30 changes: 26 additions & 4 deletions core/src/com/agateau/pixelwheels/screens/SelectTrackScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.agateau.ui.menu.CornerMenuButton;
import com.agateau.ui.menu.GridMenuItem;
import com.agateau.ui.menu.Menu;
import com.agateau.ui.menu.MenuItemListener;
import com.agateau.ui.uibuilder.UiBuilder;
import com.agateau.utils.FileUtils;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
Expand Down Expand Up @@ -192,11 +193,33 @@ private void createChampionshipSelector(Menu menu, Track currentTrack) {
mChampionshipSelector.setSelectionListener(
new GridMenuItem.SelectionListener<Championship>() {
@Override
public void currentChanged(Championship item, int index) {}
public void currentChanged(Championship item, int index) {
mChampionshipSelector.setSelected(item);
mTrackSelector.setCurrentChampionship(item);

// Unselect the current track, forcing GridMenuItem to set the first item as
// the current one.
mTrackSelector.setSelected(null);

// Force update of track: because if mTrackSelector current index has not
// changed then currentChanged() has not been called
updateTrackDetails(mTrackSelector.getCurrent());

updateNextButton();
}

@Override
public void selectionConfirmed() {
mTrackSelector.setCurrentChampionship(mChampionshipSelector.getSelected());
public void selectionConfirmed() {}
});

// Move the focus to the track selector when championship selector is triggered. Do not do
// this with SelectionListener.selectionConfirmed() because the championship selector
// selects its current item every time the current changes, so it would move the focus when
// changing the current championship with the keyboard.
mChampionshipSelector.setListener(
new MenuItemListener() {
@Override
public void triggered() {
menu.setCurrentItem(mTrackSelector);
}
});
Expand All @@ -211,7 +234,6 @@ private void createTrackSelector(Menu menu, Track currentTrack) {
mTrackSelector.init(assets, mGame.getRewardManager(), currentTrack.getChampionship());
mTrackSelector.setCurrent(currentTrack);
menu.addItem(mTrackSelector);
menu.setCurrentItem(mTrackSelector);

mTrackSelector.setSelectionListener(
new GridMenuItem.SelectionListener<Track>() {
Expand Down
7 changes: 4 additions & 3 deletions core/src/com/agateau/ui/anchor/PositionRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public Actor getTarget() {

@Override
public void apply() {
// Compute reference position
Vector2 referencePos =
// Compute reference position.
// referenceOffset is the offset relative to the bottom-left corner of reference.
Vector2 referenceOffset =
new Vector2(
reference.getWidth() * referenceAnchor.hPercent,
reference.getHeight() * referenceAnchor.vPercent);

Vector2 stagePos = reference.localToStageCoordinates(referencePos);
Vector2 stagePos = reference.localToStageCoordinates(referenceOffset);

// Apply space
stagePos.add(hSpace, vSpace);
Expand Down
9 changes: 9 additions & 0 deletions core/src/com/agateau/ui/menu/FocusIndicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public void draw(Batch batch, float x, float y, float width, float height) {

float alpha = mAlpha * (1 - BLINK_DEPTH / 2 + BLINK_DEPTH / 2 * k);

drawIndicator(batch, x, y, width, height, alpha);
}

/**
* Helper method to draw the indicator independently of the current value of mAlpha Used by
* other UI components
*/
public void drawIndicator(
Batch batch, float x, float y, float width, float height, float alpha) {
mOldBatchColor.set(batch.getColor());
batch.setColor(alpha, alpha, alpha, alpha);

Expand Down
24 changes: 22 additions & 2 deletions core/src/com/agateau/ui/menu/GridMenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
/** A MenuItem to display a grid of custom elements */
public class GridMenuItem<T> extends Widget implements MenuItem {
public static final int INVALID_INDEX = -1;
private static final float UNFOCUSED_CURRENT_INDICATOR_ALPHA = 0.7f;
private final Menu mMenu;
private Array<T> mItems;
private ItemRenderer<T> mRenderer;
Expand Down Expand Up @@ -184,6 +185,9 @@ public boolean goDown() {
}

public void setCurrentIndex(int currentIndex) {
if (mCurrentIndex == currentIndex) {
return;
}
if (mCurrentIndex != INVALID_INDEX) {
mFocusIndicators.get(mCurrentIndex).setFocused(false);
}
Expand Down Expand Up @@ -293,6 +297,10 @@ public void addCursor() {
mCursors.add(new Cursor(mCursors.size));
}

public void setListener(MenuItemListener listener) {
setListener(0, listener);
}

public void setListener(int idx, MenuItemListener listener) {
mCursors.get(idx).mMenuItemListener = listener;
}
Expand Down Expand Up @@ -329,10 +337,13 @@ public void setSelectionListener(int idx, SelectionListener<T> selectionListener
mCursors.get(idx).mSelectionListener = selectionListener;
}

public void setCurrentAndSelect(int cursorIdx, T item, boolean select) {
private void setCurrentAndSelect(int cursorIdx, T item, boolean select) {
Cursor cursor = mCursors.get(cursorIdx);
if (item == null) {
cursor.setCurrentIndex(0);
if (select) {
cursor.setSelectedIndex(INVALID_INDEX);
}
return;
}
int index = mItems.indexOf(item, true);
Expand All @@ -341,7 +352,7 @@ public void setCurrentAndSelect(int cursorIdx, T item, boolean select) {
return;
}
cursor.setCurrentIndex(index);
if (PlatformUtils.isTouchUi() || select) {
if (select) {
cursor.setSelectedIndex(index);
}
}
Expand Down Expand Up @@ -484,6 +495,15 @@ public void draw(Batch batch, float parentAlpha) {
getY() + y + rect.y,
rect.width,
rect.height);
} else if (idx == cursor.mCurrentIndex) {
FocusIndicator focusIndicator = cursor.mFocusIndicators.get(idx);
focusIndicator.drawIndicator(
batch,
getX() + x + rect.x,
getY() + y + rect.y,
rect.width,
rect.height,
UNFOCUSED_CURRENT_INDICATOR_ALPHA);
}

if (idx == cursor.mSelectedIndex) {
Expand Down

0 comments on commit 29fea66

Please sign in to comment.