Commit 72484c3a authored by Georgiy Yankovskiy's avatar Georgiy Yankovskiy

Gamepad back button, autofocus to play button for GameInfoScene.qml

parent d85cd652
...@@ -29,6 +29,7 @@ class App(QtCore.QObject): ...@@ -29,6 +29,7 @@ class App(QtCore.QObject):
gamepad_clicked_LB = Signal(bool, name="gamepadClickedLB") gamepad_clicked_LB = Signal(bool, name="gamepadClickedLB")
gamepad_clicked_RB = Signal(bool, name="gamepadClickedRB") gamepad_clicked_RB = Signal(bool, name="gamepadClickedRB")
gamepad_clicked_apply = Signal(bool, name="gamepadClickedApply") gamepad_clicked_apply = Signal(bool, name="gamepadClickedApply")
gamepad_clicked_back = Signal(bool, name="gamepadClickedBack")
gamepad_axis_left = Signal(bool, name="gamepadAxisLeft") gamepad_axis_left = Signal(bool, name="gamepadAxisLeft")
gamepad_axis_right = Signal(bool, name="gamepadAxisRight") gamepad_axis_right = Signal(bool, name="gamepadAxisRight")
...@@ -46,6 +47,7 @@ class App(QtCore.QObject): ...@@ -46,6 +47,7 @@ class App(QtCore.QObject):
self.gamepad.apply_clicked = lambda: self.gamepad_clicked_apply.emit(True) self.gamepad.apply_clicked = lambda: self.gamepad_clicked_apply.emit(True)
self.gamepad.l_clicked = lambda: self.gamepad_axis_left.emit(True) self.gamepad.l_clicked = lambda: self.gamepad_axis_left.emit(True)
self.gamepad.r_clicked = lambda: self.gamepad_axis_right.emit(True) self.gamepad.r_clicked = lambda: self.gamepad_axis_right.emit(True)
self.gamepad.back_clicked = lambda: self.gamepad_clicked_back.emit(True)
self.setup() self.setup()
......
...@@ -12,20 +12,26 @@ class Gamepad: ...@@ -12,20 +12,26 @@ class Gamepad:
LEFT_RIGHT_AXIS = 0 LEFT_RIGHT_AXIS = 0
LEFT_RIGHT_AXIS_SENSITIVITY = 0.7 LEFT_RIGHT_AXIS_SENSITIVITY = 0.7
APPLY_BUTTON = 0 APPLY_BUTTON = 0
BACK_BUTTON = 1
def __init__(self): def __init__(self):
self.joystick: Union[Joystick, None] = None self.joystick: Union[Joystick, None] = None
self.terminated: bool = False self.terminated: bool = False
self.last_rb_clicked: bool = False self.last_rb_clicked: bool = False
self.last_lb_clicked: bool = False self.last_lb_clicked: bool = False
self.last_apply_clicked: bool = False self.last_apply_clicked: bool = False
self.last_left_clicked: bool = False self.last_left_clicked: bool = False
self.last_right_clicked: bool = False self.last_right_clicked: bool = False
self.last_back_clicked: bool = False
self.lb_clicked: () = lambda: None self.lb_clicked: () = lambda: None
self.rb_clicked: () = lambda: None self.rb_clicked: () = lambda: None
self.l_clicked: () = lambda: None self.l_clicked: () = lambda: None
self.r_clicked: () = lambda: None self.r_clicked: () = lambda: None
self.back_clicked: () = lambda: None
self.apply_clicked: () = lambda: None self.apply_clicked: () = lambda: None
self.thread: Union[threading.Thread, None] = None self.thread: Union[threading.Thread, None] = None
pygame.init() pygame.init()
...@@ -58,6 +64,7 @@ class Gamepad: ...@@ -58,6 +64,7 @@ class Gamepad:
rb_button = self.joystick.get_button(self.RB_BUTTON) rb_button = self.joystick.get_button(self.RB_BUTTON)
apply_button = self.joystick.get_button(self.APPLY_BUTTON) apply_button = self.joystick.get_button(self.APPLY_BUTTON)
left_right_axis = self.joystick.get_axis(self.LEFT_RIGHT_AXIS) left_right_axis = self.joystick.get_axis(self.LEFT_RIGHT_AXIS)
back_button = self.joystick.get_button(self.BACK_BUTTON)
# LB # LB
...@@ -103,6 +110,14 @@ class Gamepad: ...@@ -103,6 +110,14 @@ class Gamepad:
if (not left_right_axis >= self.LEFT_RIGHT_AXIS_SENSITIVITY) and self.last_right_clicked: if (not left_right_axis >= self.LEFT_RIGHT_AXIS_SENSITIVITY) and self.last_right_clicked:
self.last_right_clicked = not self.last_right_clicked self.last_right_clicked = not self.last_right_clicked
# BACK
if back_button and not self.last_back_clicked:
self.last_back_clicked = not self.last_back_clicked
self.back_clicked()
if not back_button and self.last_back_clicked:
self.last_back_clicked = not self.last_back_clicked
# print(f"Button {self.LB_BUTTON}: {lb_button}") # print(f"Button {self.LB_BUTTON}: {lb_button}")
# print(f"Button {self.RB_BUTTON}: {rb_button}") # print(f"Button {self.RB_BUTTON}: {rb_button}")
......
...@@ -7,32 +7,32 @@ C.Button { ...@@ -7,32 +7,32 @@ C.Button {
font.bold: true font.bold: true
//font.letterSpacing: font.pixelSize / 100 * 30 //font.letterSpacing: font.pixelSize / 100 * 30
// font.pointSize: Math.max(parent.width / 100,10) font.pointSize: Math.max(parent.width / 100,10)
// contentItem: Text { contentItem: Text {
// text: control.text text: control.text
// font: control.font font: control.font
// //fontSizeMode: Text.Fit; minimumPixelSize: 10; //fontSizeMode: Text.Fit; minimumPixelSize: 10;
// opacity: enabled ? 1.0 : 0.3 opacity: enabled ? 1.0 : 0.3
// color: control.down ? "Black" : (control.activeFocus ? "#333333" : "#ffffff") color: control.down ? "#000000" : (control.activeFocus ? "#333333" : "#ffffff")
// horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
// verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
// elide: Text.ElideRight elide: Text.ElideRight
// leftPadding: font.pointSize / 100 * 20 leftPadding: font.pointSize / 100 * 20
// rightPadding: font.pointSize / 100 * 20 rightPadding: font.pointSize / 100 * 20
// topPadding: font.pointSize / 100 * 0 topPadding: font.pointSize / 100 * 0
// bottomPadding: font.pointSize / 100 * 0 bottomPadding: font.pointSize / 100 * 0
// } }
// background: Rectangle { background: Rectangle {
// //implicitWidth: 100 //implicitWidth: 100
// //implicitHeight: 40 //implicitHeight: 40
// opacity: enabled ? 1 : 0.3 opacity: enabled ? 1 : 0.3
// color: control.down ? "#000000" : (control.activeFocus ? "#ffffff" : "#00000000") color: control.down ? "#aaaaaa" : (control.activeFocus ? "#aaaaaa" : "#000000")
// border.width: 0 border.width: 0
// radius: 16 radius: 16
// } }
} }
...@@ -218,8 +218,9 @@ Rectangle { ...@@ -218,8 +218,9 @@ Rectangle {
} }
function onGamepadClickedApply(done){ function onGamepadClickedApply(done){
if(!visible) return; if(!visible) return;
console.log("onGamepadClickedApply", tabs.focusedItems);
let c = gamesGrid.children; let c = gamesGrid.children;
c[tabs.focusedItems].clicked(); c[tabs.focusedItems].press();
} }
} }
......
...@@ -17,16 +17,21 @@ C.Button { ...@@ -17,16 +17,21 @@ C.Button {
id: hoverArea id: hoverArea
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
acceptedButtons: "AllButtons"
onClicked: function(){ onClicked: function(){
// console.log(game.title); if(!game.visible) return;
gameInfoScene.title = game.gameTitle; game.press();
gameInfoScene.icon = game.gameIcon;
gameInfoScene.exec = game.gameExec;
window.scene = SceneConstants.gameInfoScene;
} }
} }
function press(){
if(!visible) return;
console.log(game.title);
gameInfoScene.title = game.gameTitle;
gameInfoScene.icon = game.gameIcon;
gameInfoScene.exec = game.gameExec;
window.scene = SceneConstants.gameInfoScene;
}
......
...@@ -9,6 +9,17 @@ Rectangle { ...@@ -9,6 +9,17 @@ Rectangle {
property string icon: "" property string icon: ""
property string exec: "" property string exec: ""
onVisibleChanged: function(){
// if(visible){
if(!visible) return;
container.setItemsFocus(1);
// }
}
Keys.onEscapePressed: function(){
if(!visible) return;
back.clicked();
}
id: container id: container
x: 0 x: 0
...@@ -67,6 +78,7 @@ Rectangle { ...@@ -67,6 +78,7 @@ Rectangle {
} }
Button { Button {
id: runGameButton
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
anchors.leftMargin: 142 anchors.leftMargin: 142
...@@ -82,20 +94,35 @@ Rectangle { ...@@ -82,20 +94,35 @@ Rectangle {
// LOGIC // LOGIC
property int focusedItems: 0; property int focusedItems: 0;
function focusElements(){
return [
back,
runGameButton
];
}
function applyItemsFocus(inc){ function applyItemsFocus(inc){
let c = children; let c = focusElements();
let i = focusedItems;
focusedItems += inc; i += inc;
if(focusedItems >= c.length) if(i >= c.length)
focusedItems = 0; i = 0;
if(focusedItems < 0) if(i < 0)
focusedItems = c.length - 1; i = c.length - 1;
c[focusedItems].forceActiveFocus(); setItemsFocus(i);
// c[focusedItems].clicked(); // c[focusedItems].clicked();
} }
function setItemsFocus(i){
let c = focusElements();
focusedItems = i;
c[i].forceActiveFocus();
}
Connections { Connections {
target: core_app target: core_app
function onGamepadAxisLeft(done){ function onGamepadAxisLeft(done){
...@@ -108,9 +135,13 @@ Rectangle { ...@@ -108,9 +135,13 @@ Rectangle {
} }
function onGamepadClickedApply(done){ function onGamepadClickedApply(done){
if(!visible) return; if(!visible) return;
let c = container.children; let c = focusElements();
c[container.focusedItems].clicked(); c[container.focusedItems].clicked();
} }
function onGamepadClickedBack(done){
if(!visible) return;
back.clicked();
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment