C

Qt Quick Ultralite swipegame Demo

/****************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
import QtQuick 2.0 import StyleModule 1.0 /* This component provides vertical/horizontal gesture recognition. Set the direction property to define which swipe direction should be checked. */ Item { id: root // NOTE: overwrite the Style and Globals value assignments if you want to use the component outside of this project // determines the size of the internal MouseArea. property int areaSize: Globals.useSmallTouchAreas ? Style.swipeAreaSizeSmall : Style.swipeAreaSizeDefault // determines the required distance in pixels for a gesture to be recognized as a swipe property int threshold: Style.swipeThreshold // the image used for visual feedback. Set to empty string if you want to try this component outside of this project property string highlightSource: "mask_circle_highlight_small_right.svg" // can be activated in the config view as a guide for users property bool showTouchArea: Globals.showTouchAreas // determines the swipe direction this area will detect property int direction: DirectionalSwipeArea.Direction.Left enum Direction { Left, Right, Up, Down } signal triggered() implicitWidth: Style.appSize implicitHeight: Style.appSize states: [ State { name: "Left" when: direction === DirectionalSwipeArea.Direction.Left PropertyChanges { target: mouseArea anchors { leftMargin: width - root.areaSize rightMargin: 0 topMargin: 0 bottomMargin: 0 } } }, State { name: "Right" when: direction === DirectionalSwipeArea.Direction.Right PropertyChanges { target: mouseArea anchors { leftMargin: 0 rightMargin: width - root.areaSize topMargin: 0 bottomMargin: 0 } } }, State { name: "Up" when: direction === DirectionalSwipeArea.Direction.Up PropertyChanges { target: mouseArea anchors { leftMargin: 0 rightMargin: 0 topMargin: height - root.areaSize bottomMargin: 0 } } }, State { name: "Down" when: direction === DirectionalSwipeArea.Direction.Down PropertyChanges { target: mouseArea anchors { leftMargin: 0 rightMargin: 0 topMargin: 0 bottomMargin: height - root.areaSize } } } ] QtObject { id: internal property int startX: 0 property int startY: 0 property bool triggered: false } MouseArea { id: mouseArea anchors.fill: parent onPressed: { internal.startX = mouseX internal.startY = mouseY internal.triggered = false } onPositionChanged: { if (internal.triggered) { return } switch (direction) { case DirectionalSwipeArea.Direction.Left: if (internal.startX - mouseX > root.threshold) { triggered(); internal.triggered = true } break case DirectionalSwipeArea.Direction.Right: if (mouseX - internal.startX > root.threshold) { triggered(); internal.triggered = true } break case DirectionalSwipeArea.Direction.Up: if (internal.startY - mouseY > root.threshold) { triggered(); internal.triggered = true } break case DirectionalSwipeArea.Direction.Down: if (mouseY - internal.startY > root.threshold) { triggered(); internal.triggered = true } break } } // can be activated in the config view as a guide for users Rectangle { id: areaVisualizer anchors.fill: parent color: "magenta" opacity: 0.4 visible: root.showTouchArea } } Image { id: highlight anchors.fill: parent source: root.highlightSource rotation: { switch (direction) { case DirectionalSwipeArea.Direction.Right: 180; break case DirectionalSwipeArea.Direction.Left: 0; break case DirectionalSwipeArea.Direction.Up: 90; break case DirectionalSwipeArea.Direction.Down: -90; break default: 0 } } visible: mouseArea.pressed } }