Android - Make Joystick appear always when possible (not in radar mode and when not hidden by some windows)

This commit is contained in:
Cristian Luis Duarte 2018-02-28 15:55:50 -03:00
parent 428f4b7b9d
commit 55d52f92df
10 changed files with 55 additions and 15 deletions

View file

@ -40,12 +40,6 @@ Item {
var keys = Object.keys(properties).forEach(function (key) {
button[key] = properties[key];
});
button.entered.connect(function() {
Controller.setVPadEnabled(false);
});
button.exited.connect(function() {
Controller.setVPadEnabled(true);
});
return button;
} else if( component.status == Component.Error) {
console.log("Load button errors " + component.errorString());

View file

@ -89,6 +89,11 @@ void ControllerScriptingInterface::setVPadEnabled(const bool enable) {
virtualPadManager.enable(enable);
}
void ControllerScriptingInterface::setVPadHidden(const bool hidden) {
auto& virtualPadManager = VirtualPad::Manager::instance();
virtualPadManager.hide(hidden);
}
void ControllerScriptingInterface::emitKeyPressEvent(QKeyEvent* event) { emit keyPressEvent(KeyEvent(*event)); }
void ControllerScriptingInterface::emitKeyReleaseEvent(QKeyEvent* event) { emit keyReleaseEvent(KeyEvent(*event)); }

View file

@ -65,6 +65,7 @@ public slots:
virtual glm::vec2 getViewportDimensions() const;
virtual QVariant getRecommendedHUDRect() const;
virtual void setVPadHidden(bool hidden); // Call it when a window should hide it
virtual void setVPadEnabled(bool enable);
signals:

View file

@ -96,7 +96,7 @@ bool Basic2DWindowOpenGLDisplayPlugin::internalActivate() {
void Basic2DWindowOpenGLDisplayPlugin::compositeExtra() {
auto& virtualPadManager = VirtualPad::Manager::instance();
if(virtualPadManager.getLeftVirtualPad()->isBeingTouched()) {
if(virtualPadManager.getLeftVirtualPad()->isShown()) {
// render stick base
auto stickBaseTransform = DependencyManager::get<CompositorHelper>()->getPoint2DTransform(virtualPadManager.getLeftVirtualPad()->getFirstTouch(),
_virtualPadPixelSize, _virtualPadPixelSize);

View file

@ -39,19 +39,28 @@ bool TouchscreenVirtualPadDevice::isSupported() const {
return false;
}
void TouchscreenVirtualPadDevice::initFromEvent(const QTouchEvent* event) {
QScreen* eventScreen = event->window()->screen();
void TouchscreenVirtualPadDevice::init() {
_fixedPosition = true; // This should be config
QScreen* eventScreen = qApp->primaryScreen();
if (_screenDPI != eventScreen->physicalDotsPerInch()) {
_screenWidthCenter = eventScreen->size().width() / 2;
_screenDPIScale.x = (float)eventScreen->physicalDotsPerInchX();
_screenDPIScale.y = (float)eventScreen->physicalDotsPerInchY();
_screenDPI = eventScreen->physicalDotsPerInch();
_fixedPosition = true; // This should be config
_fixedRadius = _screenDPI * 256 / 534;
qreal margin = _screenDPI * 59 / 534; // 59px is for our 'base' of 534dpi (Pixel XL or Huawei Mate 9 Pro)
_fixedCenterPosition = glm::vec2( _fixedRadius + margin, eventScreen->size().height() - margin - _fixedRadius );
}
if (_fixedPosition) {
_firstTouchLeftPoint = _fixedCenterPosition;
auto& virtualPadManager = VirtualPad::Manager::instance();
virtualPadManager.getLeftVirtualPad()->setShown(virtualPadManager.isEnabled() && !virtualPadManager.isHidden()); // Show whenever it's enabled
virtualPadManager.getLeftVirtualPad()->setFirstTouch(_firstTouchLeftPoint);
}
}
float clip(float n, float lower, float upper) {
@ -82,14 +91,21 @@ void TouchscreenVirtualPadDevice::pluginUpdate(float deltaTime, const controller
/* Shared variables for stick rendering (clipped to the stick radius)*/
// Prevent this for being done when not in first person view
virtualPadManager.getLeftVirtualPad()->setBeingTouched(true);
virtualPadManager.getLeftVirtualPad()->setFirstTouch(_firstTouchLeftPoint);
virtualPadManager.getLeftVirtualPad()->setCurrentTouch(
glm::vec2(clip(_currentTouchLeftPoint.x, -STICK_RADIUS_INCHES * _screenDPIScale.x + _firstTouchLeftPoint.x, STICK_RADIUS_INCHES * _screenDPIScale.x + _firstTouchLeftPoint.x),
clip(_currentTouchLeftPoint.y, -STICK_RADIUS_INCHES * _screenDPIScale.y + _firstTouchLeftPoint.y, STICK_RADIUS_INCHES * _screenDPIScale.y + _firstTouchLeftPoint.y))
);
virtualPadManager.getLeftVirtualPad()->setBeingTouched(true);
virtualPadManager.getLeftVirtualPad()->setShown(true); // If touched, show in any mode (fixed joystick position or non-fixed)
} else {
virtualPadManager.getLeftVirtualPad()->setBeingTouched(false);
if (_fixedPosition) {
virtualPadManager.getLeftVirtualPad()->setCurrentTouch(_fixedCenterPosition); // reset to the center
virtualPadManager.getLeftVirtualPad()->setShown(virtualPadManager.isEnabled() && !virtualPadManager.isHidden()); // Show whenever it's enabled
} else {
virtualPadManager.getLeftVirtualPad()->setShown(false);
}
}
if (_validTouchRight) {
@ -149,12 +165,13 @@ void TouchscreenVirtualPadDevice::touchBeginEvent(const QTouchEvent* event) {
return;
}
KeyboardMouseDevice::enableTouch(false);
initFromEvent(event);
}
void TouchscreenVirtualPadDevice::touchEndEvent(const QTouchEvent* event) {
auto& virtualPadManager = VirtualPad::Manager::instance();
if (!virtualPadManager.isEnabled()) {
touchLeftEnd();
touchRightEnd();
return;
}
// touch end here is a big reset -> resets both pads

View file

@ -26,6 +26,7 @@ Q_OBJECT
public:
// Plugin functions
virtual void init() override;
virtual bool isSupported() const override;
virtual const QString getName() const override { return NAME; }
@ -88,7 +89,6 @@ protected:
// just for debug
private:
void debugPoints(const QTouchEvent* event, QString who);
void initFromEvent(const QTouchEvent* event);
};

View file

@ -51,8 +51,24 @@ namespace VirtualPad {
return _enabled;
}
void Manager::hide(bool hidden) {
_hidden = hidden;
}
bool Manager::isHidden() {
return _hidden;
}
Instance* Manager::getLeftVirtualPad() {
return &_leftVPadInstance;
}
bool Instance::isShown() {
return _shown;
}
void Instance::setShown(bool show) {
_shown = show;
}
}

View file

@ -21,10 +21,13 @@ namespace VirtualPad {
virtual glm::vec2 getFirstTouch();
virtual void setCurrentTouch(glm::vec2 point);
virtual glm::vec2 getCurrentTouch();
virtual bool isShown();
virtual void setShown(bool show);
private:
bool _isBeingTouched;
glm::vec2 _firstTouch;
glm::vec2 _currentTouch;
bool _shown;
};
class Manager : public QObject, public Dependency {
@ -37,9 +40,12 @@ namespace VirtualPad {
Instance* getLeftVirtualPad();
bool isEnabled();
void enable(bool enable);
bool isHidden();
void hide(bool hide);
private:
Instance _leftVPadInstance;
bool _enabled;
bool _hidden;
};
}

View file

@ -52,7 +52,7 @@ module.exports = {
});
},
show: function() {
Controller.setVPadEnabled(false);
Controller.setVPadHidden(true);
if (window) {
window.fromQml.connect(fromQml);
window.setVisible(true);
@ -60,7 +60,7 @@ module.exports = {
}
},
hide: function() {
Controller.setVPadEnabled(true);
Controller.setVPadHidden(false);
if (window) {
window.fromQml.disconnect(fromQml);
window.setVisible(false);

View file

@ -14,6 +14,7 @@
function init() {
Controller.setVPadEnabled(true);
Controller.setVPadHidden(false);
}
init();