Skip to content

Commit 7b8b675

Browse files
committed
Save Screen now works with gamepad.
1 parent 233c616 commit 7b8b675

File tree

3 files changed

+117
-30
lines changed

3 files changed

+117
-30
lines changed

blade-engine/src/com/bladecoder/engine/ui/LoadSaveScreen.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.badlogic.gdx.graphics.Texture;
3232
import com.badlogic.gdx.graphics.Texture.TextureFilter;
3333
import com.badlogic.gdx.graphics.g2d.TextureRegion;
34+
import com.badlogic.gdx.math.Vector2;
3435
import com.badlogic.gdx.scenes.scene2d.Actor;
3536
import com.badlogic.gdx.scenes.scene2d.InputEvent;
3637
import com.badlogic.gdx.scenes.scene2d.InputListener;
@@ -42,12 +43,14 @@
4243
import com.badlogic.gdx.scenes.scene2d.ui.Image;
4344
import com.badlogic.gdx.scenes.scene2d.ui.Label;
4445
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
46+
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
4547
import com.badlogic.gdx.scenes.scene2d.ui.Table;
4648
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
4749
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
4850
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
4951
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
5052
import com.badlogic.gdx.utils.Align;
53+
import com.badlogic.gdx.utils.SnapshotArray;
5154
import com.badlogic.gdx.utils.viewport.ScreenViewport;
5255
import com.bladecoder.engine.assets.EngineAssetManager;
5356
import com.bladecoder.engine.model.Text;
@@ -130,8 +133,6 @@ public void show() {
130133

131134
stage = new Stage(new ScreenViewport());
132135

133-
controller = new ScreenControllerHandler(ui, stage, stage.getViewport());
134-
135136
slotWidth = (int) (stage.getViewport().getWorldWidth() / (ROW_SLOTS + 1) - 2 * pad);
136137
slotHeight = slotWidth * stage.getViewport().getScreenHeight() / stage.getViewport().getScreenWidth();
137138

@@ -254,6 +255,64 @@ public boolean keyUp(InputEvent event, int keycode) {
254255
stage.addActor(pointer);
255256

256257
Gdx.input.setInputProcessor(stage);
258+
259+
controller = new ScreenControllerHandler(ui, stage, stage.getViewport()) {
260+
@Override
261+
protected void focusNext(PointerToNextType type) {
262+
SnapshotArray<Actor> content = ((Table) scroll.getActor()).getChildren();
263+
264+
// find content cursor
265+
int idx = -1;
266+
for (int i = 0; i < content.size; i++) {
267+
Vector2 inputPos = new Vector2(Gdx.input.getX(), Gdx.input.getY());
268+
content.get(i).screenToLocalCoordinates(inputPos);
269+
if (content.get(i).hit(inputPos.x, inputPos.y, false) != null) {
270+
idx = i;
271+
}
272+
}
273+
274+
// find the slot
275+
Button b = getButtonUnderCursor(stage);
276+
if (b != null && idx != -1) {
277+
int find = ((Table) content.get(idx)).getChildren()
278+
.indexOf(b.getParent() instanceof Stack ? b.getParent() : b, true);
279+
280+
if (type == PointerToNextType.LEFT) {
281+
if (find == 0) {
282+
EngineLogger.debug(">>>Previous page!!");
283+
if (idx > 0) {
284+
idx--;
285+
scroll.scrollToPage(idx);
286+
287+
find = ((Table) content.get(idx)).getChildren().size - 1;
288+
}
289+
} else if (find == -1) {
290+
find = 0;
291+
} else {
292+
find--;
293+
}
294+
} else if (type == PointerToNextType.RIGHT) {
295+
296+
if (find == ((Table) content.get(idx)).getChildren().size - 1) {
297+
EngineLogger.debug(">>>Next page!!");
298+
if (idx < content.size - 1) {
299+
idx++;
300+
scroll.scrollToPage(idx);
301+
find = 0;
302+
}
303+
} else if (find == -1) {
304+
find = 0;
305+
} else {
306+
find++;
307+
}
308+
}
309+
310+
cursorToActor(((Table) content.get(idx)).getChildren().get(find));
311+
} else {
312+
super.focusNext(type);
313+
}
314+
}
315+
};
257316
}
258317

259318
private boolean slotExists(String slot) {

blade-engine/src/com/bladecoder/engine/ui/PagedScrollPane.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,44 @@ public class PagedScrollPane extends ScrollPane {
1414

1515
private Table content;
1616

17-
public PagedScrollPane () {
17+
public PagedScrollPane() {
1818
super(null);
1919
setup();
2020
}
2121

22-
public PagedScrollPane (Skin skin) {
22+
public PagedScrollPane(Skin skin) {
2323
super(null, skin);
2424
setup();
2525
}
2626

27-
public PagedScrollPane (Skin skin, String styleName) {
27+
public PagedScrollPane(Skin skin, String styleName) {
2828
super(null, skin, styleName);
2929
setup();
3030
}
3131

32-
public PagedScrollPane (Actor widget, ScrollPaneStyle style) {
32+
public PagedScrollPane(Actor widget, ScrollPaneStyle style) {
3333
super(null, style);
3434
setup();
3535
}
3636

3737
private void setup() {
3838
content = new Table();
3939
content.defaults().space(50);
40-
super.setActor(content);
40+
super.setActor(content);
4141
}
4242

43-
public void addPages (Actor... pages) {
43+
public void addPages(Actor... pages) {
4444
for (Actor page : pages) {
4545
content.add(page).expandY().fillY();
4646
}
4747
}
4848

49-
public void addPage (Actor page) {
49+
public void addPage(Actor page) {
5050
content.add(page).expandY().fillY();
5151
}
5252

5353
@Override
54-
public void act (float delta) {
54+
public void act(float delta) {
5555
super.act(delta);
5656
if (wasPanDragFling && !isPanning() && !isDragging() && !isFlinging()) {
5757
wasPanDragFling = false;
@@ -62,9 +62,9 @@ public void act (float delta) {
6262
}
6363
}
6464
}
65-
65+
6666
@Override
67-
public void setWidth (float width) {
67+
public void setWidth(float width) {
6868
super.setWidth(width);
6969
if (content != null) {
7070
for (Cell<?> cell : content.getCells()) {
@@ -74,7 +74,7 @@ public void setWidth (float width) {
7474
}
7575
}
7676

77-
public void setPageSpacing (float pageSpacing) {
77+
public void setPageSpacing(float pageSpacing) {
7878
if (content != null) {
7979
content.defaults().space(pageSpacing);
8080
for (Cell<?> cell : content.getCells()) {
@@ -84,12 +84,13 @@ public void setPageSpacing (float pageSpacing) {
8484
}
8585
}
8686

87-
private void scrollToPage () {
87+
private void scrollToPage() {
8888
final float width = getWidth();
8989
final float scrollX = getScrollX();
9090
final float maxX = getMaxX();
9191

92-
if (scrollX >= maxX || scrollX <= 0) return;
92+
if (scrollX >= maxX || scrollX <= 0)
93+
return;
9394

9495
Array<Actor> pages = content.getChildren();
9596
float pageX = 0;
@@ -106,4 +107,29 @@ private void scrollToPage () {
106107
}
107108
}
108109

110+
public void scrollToPage(int page) {
111+
final float width = getWidth();
112+
final float scrollX = getScrollX();
113+
final float maxX = getMaxX();
114+
115+
// if (scrollX >= maxX || scrollX <= 0)
116+
// return;
117+
118+
Actor a = content.getChildren().get(page);
119+
float pageX = 0;
120+
float pageWidth = 0;
121+
122+
pageX = a.getX();
123+
pageWidth = a.getWidth();
124+
// if (scrollX < (pageX + pageWidth * 0.5)) {
125+
// return;
126+
// }
127+
128+
setScrollX(MathUtils.clamp(pageX - (width - pageWidth) / 2, 0, maxX));
129+
130+
updateVisualScroll();
131+
invalidate();
132+
layout();
133+
}
134+
109135
}

blade-engine/src/com/bladecoder/engine/ui/defaults/ScreenControllerHandler.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,33 @@ public int compare(Vector2 o1, Vector2 o2) {
216216

217217
int idx = 0;
218218

219-
float minD = Float.MAX_VALUE;
220219
Vector2 mPos = new Vector2(Gdx.input.getX(), Gdx.input.getY());
221220

222221
// get the nearest actor
223222
for (int i = 0; i < positions.size(); i++) {
224223
Vector2 actPos = positions.get(i);
225-
float d = actPos.dst(mPos);
226-
if (d < minD) {
227-
minD = d;
228-
idx = i;
229-
}
230-
}
231224

232-
EngineLogger.debug("Prev: " + positions.get(idx) + " IDX: " + idx + " mPos: " + mPos);
225+
if (type == PointerToNextType.RIGHT) {
226+
if ((int) actPos.x < (int) mPos.x)
227+
continue;
233228

234-
if ((type == PointerToNextType.RIGHT && (int) positions.get(idx).x < (int) mPos.x)
235-
|| (type == PointerToNextType.LEFT && (int) positions.get(idx).x > (int) mPos.x)
236-
|| (type == PointerToNextType.RIGHT && (int) positions.get(idx).x == (int) mPos.x
237-
&& positions.get(idx).y < mPos.y)
238-
|| (type == PointerToNextType.LEFT && (int) positions.get(idx).x == (int) mPos.x
239-
&& positions.get(idx).y > mPos.y))
240-
idx = (idx + 1) % positions.size();
229+
if ((int) actPos.x == (int) mPos.x && (int) actPos.y < (int) mPos.y)
230+
continue;
231+
} else {
232+
if ((int) actPos.x > (int) mPos.x)
233+
continue;
234+
235+
if ((int) actPos.x == (int) mPos.x && (int) actPos.y > (int) mPos.y)
236+
continue;
237+
}
238+
239+
idx = i;
240+
break;
241+
}
241242

242243
EngineLogger.debug("Selected: " + positions.get(idx) + " IDX: " + idx);
243244
Gdx.input.setCursorPosition((int) positions.get(idx).x, (int) positions.get(idx).y);
245+
244246
}
245247

246248
private void updateAxis(float delta) {

0 commit comments

Comments
 (0)