Joystick 虚拟操纵杆
程序员文章站
2022-05-15 09:54:41
...
Joystick 虚拟操纵杆
多点触控
–输出
4象限坐标输出 标准单位笛卡尔系,控件中心(0,0)
xAxis: 取值范围 [-1,1], 左 值<0, 右 值>0
yAxis: 取值范围 [-1,1], 下 值<0, 上 值>0
效果图 :
import QtQuick 2.5
Rectangle {
id: _joyRoot
width: dishRadius*2
height: dishRadius*2
radius: dishRadius
color: "transparent"
border.color: "#c6c6c6"
border.width: 2
//--输出
/* -- 4象限坐标输出 标准单位笛卡尔系,控件中心(0,0)
* xAxis: 取值范围 [-1,1], 左 值<0, 右 值>0
* yAxis: 取值范围 [-1,1], 下 值<0, 上 值>0
*/
property real xAxis: 0
property real yAxis: 0
//--控件位置控制
property real xPositionDelta: 0 ///< 用于设置手柄中心到触摸点 parent.leftMargin: xPositionDelta
property real yPositionDelta: 0 ///< 用于设置手柄中心到触摸点 parent.bottomMargin: -yPositionDelta
property bool _processTouchPoints: false //处理触摸点
//--手柄控制
property real _centerXY: width / 2 //中间坐标值
property real stickPositionX: _centerXY //操纵杆中心x坐标
property real stickPositionY: _centerXY //操纵杆中心y坐标
property int thumbRadius:60 //手柄半径
property int dishRadius:300 //底盘半径
property bool lightColors: true /// 启用浅色背景图片
//-- 调试
onXAxisChanged: {
console.log(xAxis,yAxis)
}
onYAxisChanged: {
console.log(xAxis,yAxis)
}
// x轴输出处理
onStickPositionXChanged: {
var xAxisPercent = stickPositionX / width
xAxis = 2*xAxisPercent - 1
}
// y轴输出处理
onStickPositionYChanged: {
var yAxisPercent = stickPositionY / width
yAxis = 1 - 2*yAxisPercent
}
//居中手柄
function reCenter()
{
_processTouchPoints = false
xPositionDelta = 0
yPositionDelta = 0
stickPositionX = _centerXY
stickPositionY = _centerXY
}
//手柄按下处理
function thumbDown(touchPoints)
{
//求出触点位置到 背景中心 相对坐标
xPositionDelta = touchPoints[0].x - _centerXY
yPositionDelta = touchPoints[0].y - _centerXY
// 等待,直到我们将控制移动到正确的位置,然后再处理触点
_processTouchPoints = true
}
Image {//-- 背景圆盘
anchors.fill: parent
source: lightColors ? "qrc:/JoystickBezel.png" : "qrc:/JoystickBezelLight.png"
mipmap: true
smooth: true
}
Rectangle {//-- 中间环(仅仅是显示效果)
anchors.margins: parent.width / 4
anchors.fill: parent
radius: width / 2
border.color: "#c6c6c6"
border.width: 2
color: "#c0c0c0"
gradient: Gradient {
GradientStop { position: 0.0; color: "#eeeeee" }
GradientStop { position: 0.5; color: "#555555" }
GradientStop { position: 1.0; color: "#eeeeee" }
}
}
Rectangle {//--thumb 手柄
width: thumbRadius*2
height: thumbRadius*2
radius: thumbRadius
color: "#111111"
x: stickPositionX - thumbRadius
y: stickPositionY - thumbRadius
gradient: Gradient {
GradientStop { position: 0.0; color: "#aaaaaa" }
GradientStop { position: 0.3; color: "#666666" }
GradientStop { position: 1.0; color: "#111111" }
}
}
Connections {//--移动操纵杆 设置操纵杆中心坐标,以及限制范围
target: touchPoint
onXChanged: {//--
if (_processTouchPoints) {
_joyRoot.stickPositionX = Math.max(Math.min(touchPoint.x, _joyRoot.width), 0)
}
}
onYChanged: {//--
if (_processTouchPoints) {
_joyRoot.stickPositionY = Math.max(Math.min(touchPoint.y, _joyRoot.height), 0)
}
}
}
MultiPointTouchArea {//--多点触控
anchors.fill: parent
minimumTouchPoints: 1
maximumTouchPoints: 1
touchPoints: [ TouchPoint { id: touchPoint } ]
onPressed: _joyRoot.thumbDown(touchPoints)
onReleased: _joyRoot.reCenter()
}
}
推荐阅读
-
装修装潢精髓 博客分类: Other 家居建材虚拟机生活360项目管理
-
转200条装修小常识 家居建材虚拟机项目管理生活音乐
-
python安装虚拟环境 博客分类: Python PythonVirtualenv
-
nova 建虚拟机 博客分类: openstacknova 虚拟机Novaneutron
-
dashboard增加删除虚拟机按钮 博客分类: openstack
-
使用vagrant和virtualbox虚拟环境
-
Linux vps搭建Apache服务,设置虚拟目录 博客分类: 服务器 vps linux apache
-
【环境】jupyter 虚拟环境搭建
-
IPVS虚拟服务
-
Hyper-v 虚拟机管理服务无法启动 博客分类: Hyper-v Hyper-v虚拟机管理服务无法启动