C

Qt Quick Ultralite swipe_game Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

import QtQuick 2.0
import StyleModule 1.0

/*
    This component is the root element of the SwipeGame demo.
    It doesn't have a width and height set so that Qt for MCU automatically resizes it.
    On MCUs it should be resized to the display size and on desktop to some default size.
    It passes the size to the Style.qml for UI scaling. In addition it controls the
    visibility of the configuration and game parts of the SwipeGame.
    The whole demo is meant to be displayed on a circular display. On a rectangular
    display the shorter dimension will be used as radius for the circular elements which
    be positioned at the center of the screen.
*/

Item {
    id: root

    onWidthChanged: resizeTimer.restart()
    onHeightChanged: resizeTimer.restart()

    // rectangular background required for rectangular displays
    Rectangle {
        anchors.fill: parent
        color: Style.colorAppBackground
    }

    ConfigContainer {
        id: configContainer

        anchors.centerIn: parent

        onStartRequested: {
            Globals.initGameData()
            visible = false
            gameContainer.visible = true
        }
    }

    GameContainer {
        id: gameContainer

        anchors.centerIn: parent
        visible: false

        onGameAborted: {
            if (Globals.fails === Globals.tries) {
                // if the game was played to the end, add a highscore
                configContainer.addHighscore()
            }

            visible = false
            configContainer.visible = true
        }
    }

    // use timer to avoid multiple resizes when width/height change quickly
    Timer {
        id: resizeTimer

        interval: 40

        onTriggered: {
            Style.appSize = Math.min(root.width, root.height)
        }
    }
}