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