diff --git a/applicationQT2048/GamePage.qml b/applicationQT2048/GamePage.qml index 5609d8daa1b345ee44e109e83335189aac274933..04bce111907c30c952aeabc3b3af8b3c97155fd9 100644 --- a/applicationQT2048/GamePage.qml +++ b/applicationQT2048/GamePage.qml @@ -1,230 +1,243 @@ import QtQuick import QtQuick.Controls -Rectangle { - width: 400 - height: 600 - color: "#FAF8EF" +FocusScope { // Permet de capter le focus pour les raccourcis clavier + id: gamePage + focus: true // Active le focus pour capter les événements clavier + Rectangle { + width: 400 + height: 600 + color: "#FAF8EF" - Text { + Component.onCompleted: gamePage.forceActiveFocus() // Assure que la page capte bien les entrées clavier - text: "Fusionnez les nombres pour atteindre 2048!" - font.bold: true - color:"grey" - anchors.horizontalCenter: parent.horizontalCenter - y:145 - } + // Gestion des raccourcis clavier + Shortcut { + sequence: "Left" + onActivated: gameManager.moveLeft() + } - Rectangle{ - width:105;height:105 - color:"orange" - x:35 - y:10 - radius:4 + Shortcut { + sequence: "Right" + onActivated: gameManager.moveRight() + } - Text { + Shortcut { + sequence: "Up" + onActivated: gameManager.moveUp() + } + + Shortcut { + sequence: "Down" + onActivated: gameManager.moveDown() + } - text:"2048" - color: "white" - font.bold: true - font.pixelSize: 30 - anchors.centerIn: parent - } - } - Rectangle{ - color:"grey" - width: 90; height: 90 - x:270 - y:10 - radius:5 - Text{ - text: "MEILLEUR" - color:"lightgrey" - font.pixelSize: 14 + Text { + + text: "Fusionnez les nombres pour atteindre 2048!" font.bold: true + color:"grey" anchors.horizontalCenter: parent.horizontalCenter + y:145 + } + + Rectangle{ + width:105;height:105 + color:"orange" + x:35 y:10 + radius:4 + + Text { + text:"2048" + color: "white" + font.bold: true + font.pixelSize: 30 + anchors.centerIn: parent + } } - Text{ - id: bestScore - text:"0" - color:"white" - font.bold: true - font.pixelSize: 25 - anchors.centerIn:parent + Rectangle{ + color:"grey" + width: 90; height: 90 + x:270 + y:10 + radius:5 - } - } + Text{ + text: "MEILLEUR" + color:"lightgrey" + font.pixelSize: 14 + font.bold: true + anchors.horizontalCenter: parent.horizontalCenter + y:10 - Rectangle{ + } - color:"grey" - width: 90; height: 90 - x:170 - y:10 - radius:5 + Text{ + id: bestScore + text:"0" + color:"white" + font.bold: true + font.pixelSize: 25 + anchors.centerIn:parent - Text{ - text: "SCORE" - color:"lightgrey" - font.pixelSize: 14 - font.bold: true - anchors.horizontalCenter: parent.horizontalCenter - y:10 + } } - Text{ - id: score - text:"0" - color:"white" - font.bold: true - font.pixelSize: 25 - anchors.centerIn:parent + Rectangle{ + color:"grey" + width: 90; height: 90 + x:170 + y:10 + radius:5 - } - } + Text{ + text: "SCORE" + color:"lightgrey" + font.pixelSize: 14 + font.bold: true + anchors.horizontalCenter: parent.horizontalCenter + y:10 - Rectangle { - id: buttonNew - color:"#F65E3B" - width: 90; height: 30 - x:170 - y:105 - radius:5 - - Text{ - id: buttonLabel - anchors.centerIn: parent - text: "NOUVEAU " - color:"white" - font.bold: true - } + } + + Text{ + id: score + text:"0" + color:"white" + font.bold: true + font.pixelSize: 25 + anchors.centerIn:parent - MouseArea{ - id: buttonMouseArea - anchors.fill: parent - onClicked: gameManager.restartGame() + } } - } - Rectangle { - id: undo - color: "#F65E3B" - width: 90; height: 30 - x:270 - y:105 - radius:5 + Rectangle { + id: buttonNew + color:"#F65E3B" + width: 90; height: 30 + x:170 + y:105 + radius:5 + Text{ + id: buttonLabel + anchors.centerIn: parent + text: "NOUVEAU " + color:"white" + font.bold: true + } - Text{ + MouseArea{ + id: buttonMouseArea - anchors.centerIn: parent - text: "ANNULER " - color:"white" - font.bold: true + anchors.fill: parent + onClicked: gameManager.restartGame() + } } - MouseArea{ - id: undoMouseArea + Rectangle { + id: undo + color: "#F65E3B" + width: 90; height: 30 + x:270 + y:105 + radius:5 - anchors.fill: parent - onClicked: gameManager.undo() - } - } - Rectangle{ - width:350; height:350 - anchors.horizontalCenter: parent.horizontalCenter - y:170 - color:"grey" - radius: 5 - - - Grid { - id: gameGrid - columns: 4 - rows: 4 - anchors.centerIn: parent - spacing: 8 - padding:8 - - - Repeater { - model: gameManager.gridValues - Rectangle { - width: 78 - height: 78 - color: modelData == 2 ? "#EEE4DA" : - modelData == 4 ? "#EDE0C8" : - modelData == 8 ? "#F2B179" : - modelData == 16 ? "#F59563" : - modelData == 32 ? "#F67C5F" : - modelData == 64 ? "#F65E3B" : - modelData == 128 ? "#EDCF72" : - modelData == 256 ? "#EDCC61" : - modelData == 512 ? "#EDC850" : - modelData == 1024 ? "#EDC53F" : - modelData == 2048 ? "#EDC22E" : "#BBADA0" - radius: 5 - Text { - text: modelData > 0 ? modelData : "" - anchors.centerIn: parent - font.bold: true - font.pixelSize: 30 - color: "grey" - } - } + Text{ + + anchors.centerIn: parent + text: "ANNULER " + color:"white" + font.bold: true } - } - } - Rectangle { - id: menu - color:"#F65E3B" - width: 180; height: 30 - anchors.horizontalCenter: parent.horizontalCenter - y:540 - radius:5 - - Text{ - anchors.centerIn: parent - text: "MENU " - color:"white" - font.bold: true - } + MouseArea{ + id: undoMouseArea - MouseArea{ - anchors.fill: parent - onClicked: stackView.push("MenuPage.qml") + anchors.fill: parent + onClicked: gameManager.undo() + } } - } + Rectangle{ + width:350; height:350 + anchors.horizontalCenter: parent.horizontalCenter + y:170 + color:"grey" + radius: 5 - Item{ - id: keyHandler - focus: true - - Component.onCompleted: keyHandler.forceActiveFocus() // Assure que l'élément a le focus - Keys.onPressed: { - if (event.key === Qt.Key_Left) gameManager.moveLeft(); - else if (event.key === Qt.Key_Right) gameManager.moveRight(); - else if (event.key === Qt.Key_Up) gameManager.moveUp(); - else if (event.key === Qt.Key_Down) gameManager.moveDown(); + Grid { + id: gameGrid + columns: 4 + rows: 4 + anchors.centerIn: parent + spacing: 8 + padding:8 + + + Repeater { + model: gameManager.gridValues + Rectangle { + width: 78 + height: 78 + color: modelData == 2 ? "#EEE4DA" : + modelData == 4 ? "#EDE0C8" : + modelData == 8 ? "#F2B179" : + modelData == 16 ? "#F59563" : + modelData == 32 ? "#F67C5F" : + modelData == 64 ? "#F65E3B" : + modelData == 128 ? "#EDCF72" : + modelData == 256 ? "#EDCC61" : + modelData == 512 ? "#EDC850" : + modelData == 1024 ? "#EDC53F" : + modelData == 2048 ? "#EDC22E" : "#BBADA0" + radius: 5 + Text { + text: modelData > 0 ? modelData : "" + anchors.centerIn: parent + font.bold: true + font.pixelSize: 30 + color: "grey" + } + } + } + } } - } + Rectangle { + id: menu + color:"#F65E3B" + width: 180; height: 30 + anchors.horizontalCenter: parent.horizontalCenter + y:540 + radius:5 + Text{ + anchors.centerIn: parent + text: "MENU " + color:"white" + font.bold: true + } + + MouseArea{ + anchors.fill: parent + onClicked: stackView.push("MenuPage.qml") + } + } + } } diff --git a/applicationQT2048/Main.qml b/applicationQT2048/Main.qml index 1a9bf686e14c525f5697a196f613ee1b9bda31df..6dd408d16d7f0cec43c949907d0e76ed8d6e0045 100644 --- a/applicationQT2048/Main.qml +++ b/applicationQT2048/Main.qml @@ -10,6 +10,6 @@ ApplicationWindow { StackView { id: stackView anchors.fill: parent - initialItem: "GamePage.qml" // Charge la page du jeu + initialItem: "GamePage.qml" // Utilisez Qt.resolvedUrl pour éviter les problèmes de chemin } } diff --git a/applicationQT2048/main.cpp b/applicationQT2048/main.cpp index b1fb703e27c8568e53b3df27b8efeb7e5be1fa8e..17deb6c6a7572d43ffa1fd23d3e0cf1d532297d2 100644 --- a/applicationQT2048/main.cpp +++ b/applicationQT2048/main.cpp @@ -16,6 +16,12 @@ int main(int argc, char *argv[]) Qt::QueuedConnection); engine.rootContext()->setContextProperty("gameManager", &gameManager); engine.loadFromModule("applicationQT2048", "Main"); + + // Force le focus sur la fenêtre principale pour gérer les événements clavier + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [](QObject *obj, const QUrl &) { + if (obj) obj->setProperty("focus", true); + }); + return app.exec(); }