aboutsummaryrefslogtreecommitdiffstats
path: root/DemoApplication/main.qml
blob: a242e472f20c57ef34348ce02ab3623a990f693a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import QtQuick 2.9
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import QtGamepad 1.0

import CursorNavigation 1.0
import controls 1.0
import "pages"

ApplicationWindow {
    id: window
    width: 800
    height: 600
    visible: true
    title: qsTr("Cursor Navigation Demo Application")

    MessageDialog {
        id: dialog
        visible: false
        x: window.x+window.width/2-dialog.width/2
        y: window.y+window.height/2-dialog.height/2
    }

    Shortcut {
        id: dialogSC
        sequence: "p"
        onActivated: {
            dialog.visible = true;
        }
    }

    /* there are alternative ways to implement a tabbar. the traditional tabbar has
     * problems with CursorNavigation: when trying to navigate the cursor on it
     * right after launching the application, the cursor will not be passed onto
     * the children. However, if clicked first, the navigation works. Another
     * workaround is to force the active focus on one of the tabs.
     * The listview approach works right out of the box
     */

    header:/* ListView {
        id: tabBar
        orientation: ListView.Horizontal
        width: window.width
        height: 40
        CursorNavigation.acceptsCursor: true

        delegate: Rectangle {
            CursorNavigation.acceptsCursor: true
            CursorNavigation.onHasCursorChanged: {
                if (CursorNavigation.hasCursor)
                    tabBar.currentIndex = index
            }
            width: tabBar.width/tabBar.count
            height: 40
            border.width: 2
            border.color: CursorNavigation.hasCursor ? "red" : "transparent"

            Text { anchors.centerIn: parent; text: pageName}
        }

        model: ListModel {
            ListElement {
                pageName: "Page 1"
            }
            ListElement {
                pageName: "Page 2"
            }
            ListElement {
                pageName: "Page 3"
            }
            ListElement {
                pageName: "Page 4"
            }
            ListElement {
                pageName: "Page 5"
            }
            ListElement {
                pageName: "Map"
            }
        }

    }*/

    TabBar {
        id: tabBar
        width: window.width
        CursorNavigation.acceptsCursor: true

        CNTabButton {
            id: defaultButton
            text: qsTr("Basics and scopes")
            focus: true
        }
        CNTabButton {
            text: qsTr("Lists etc")
        }
        CNTabButton {
            text: qsTr("Flip buttons")
        }
        CNTabButton {
            text: qsTr("Map")
        }
        CNTabButton {
            text: qsTr("Redirects")
        }
    }

    contentData: Item {
        id: contentItem
        anchors.fill: parent
        StackLayout {
            anchors.fill: parent
            currentIndex: tabBar.currentIndex
            Basics { }
            FlipButtons { }
            Lists { }
            MapView { }
            Redirects { }
        }

        CursorNavigation.acceptsCursor: false

        Timer {
            id: cooldownTimer
            interval: 500
            repeat: false
        }

        Rectangle {
            id: pointerRect
            border.color: "orange"
            border.width: 1
            visible: false
            color: "transparent"
        }

        //use gamepad as another source of input. use analog stick input for magnitude information
        Gamepad {
            deviceId: GamepadManager.connectedGamepads.length > 0 ? GamepadManager.connectedGamepads[0] : -1

            function handleMove() {
                var v = Qt.vector2d(axisLeftX, axisLeftY);
                if (v.length() >= 0.99 && !cooldownTimer.running) {
                    //console.log("handle joystick move, v=" + v)
                    contentItem.CursorNavigation.move(Qt.vector2d(axisLeftX, axisLeftY), 10);
                    cooldownTimer.start();
                } else if (v.length() >= 0.1) {
                    contentItem.CursorNavigation.setMagnitude(v);
                    /*var item = parent.CursorNavigation.find(v, 10)
                    //cooldownTimer.start()
                    if (item != undefined) {
                        pointerRect.x = item.x
                        pointerRect.y = item.y
                        pointerRect.width = item.width
                        pointerRect.height = item.height
                        pointerRect.visible = true
                    }*/
                } else {
                    contentItem.CursorNavigation.setMagnitude(0,0);
                    pointerRect.visible = false;
                }
            }

            onAxisLeftXChanged: { handleMove(); }
            onAxisLeftYChanged: { handleMove(); }
            onButtonAChanged: { if (buttonA) { contentItem.CursorNavigation.activate(); } }
            onButtonBChanged: { if (buttonB) { contentItem.CursorNavigation.activate(); } }
        }
    }

    //a trick that ensures the cursor can be moved on the tabbar without needing to click teh tabbar first
    Component.onCompleted: {
        defaultButton.forceActiveFocus();
    }
}