Advertisement
prajna-pranab

MEFlickArea.qml

Dec 12th, 2023 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 2.35 KB | None | 0 0
  1. /*
  2.  * Copyright 2015 Canonical Ltd.
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; version 3.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  */
  16.  
  17. import QtQuick 2.4
  18.  
  19. MultiPointTouchArea {
  20.     id: root
  21.  
  22.     /// Is true while the area is touched, and the finger did not yet lift
  23.     property bool pressed: false
  24.  
  25.     /// Cancels the current pressed state of the mouse are
  26.     function cancelPress() {
  27.         pressed = false;
  28.     }
  29.  
  30.     onPressed: {
  31.         index = 0;
  32.         pressed = true;
  33.         rad = 0
  34.     }
  35.  
  36.     onReleased: {
  37.         pressed = false;
  38.     }
  39.  
  40.     property int index: 0   // 0:center, 1:west, 2:northwest, 3:north, 4:northeast, 5:east, 6:southeast, 7:south, 8:southwest
  41.     property int old_index: 0
  42.     property real posX: point.x - parent.width / 2
  43.     property real posY: point.y - parent.height / 2
  44.     property real rad: 0
  45.     property real heading: 0
  46.     property real direction: -157.5
  47.  
  48.     touchPoints: [
  49.         TouchPoint { id: point }
  50.     ]
  51.  
  52.     onUpdated: {
  53.         rad = Math.atan2(posY, posX)
  54.         // if the swipe is short it's the center of the key
  55.         if ((posX * posX + posY * posY) < (0.5 * parent.height * 0.5 * parent.height)) {
  56.             index = 0
  57.         } else {
  58.             // calculate swipe angle; -180 < angle < 180 deg
  59.             heading = rad * 180/Math.PI
  60.             index = 1 // If the heading is less than -157.5 deg or greater than 157.5 deg the direction is West            
  61.             if (Math.abs(heading) < 157.5) {
  62.                 direction = -157.5
  63.                 while (direction < heading) {
  64.                     index++
  65.                     direction += 45
  66.                 }
  67.             }
  68.         }
  69.         if (old_index != index) {
  70.             old_index = index
  71.  
  72.             if (maliit_input_method.useHapticFeedback)
  73.                 pressEffect.start();
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement