Android - Virtual Pad - Refactor in Basic2D..Plugin code for multiple buttons

This commit is contained in:
Cristian Luis Duarte 2018-09-03 19:19:55 -03:00
parent 2e43912e92
commit 1891b0fd81
6 changed files with 107 additions and 108 deletions

View file

@ -74,55 +74,15 @@ void Basic2DWindowOpenGLDisplayPlugin::customizeContext() {
}
}
_virtualPadJumpBtnPixelSize = dpi * VirtualPad::Manager::JUMP_BTN_FULL_PIXELS / VirtualPad::Manager::DPI;
if (!_virtualPadJumpBtnTexture) {
auto iconPath = PathUtils::resourcesPath() + "images/fly.png";
auto image = QImage(iconPath);
if (image.format() != QImage::Format_ARGB32) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
if ((image.width() > 0) && (image.height() > 0)) {
image = image.scaled(_virtualPadJumpBtnPixelSize, _virtualPadJumpBtnPixelSize, Qt::KeepAspectRatio);
image = image.mirrored();
_virtualPadJumpBtnTexture = gpu::Texture::createStrict(
gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA),
image.width(), image.height(),
gpu::Texture::MAX_NUM_MIPS,
gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR));
_virtualPadJumpBtnTexture->setSource("virtualPad jump");
auto usage = gpu::Texture::Usage::Builder().withColor().withAlpha();
_virtualPadJumpBtnTexture->setUsage(usage.build());
_virtualPadJumpBtnTexture->setStoredMipFormat(gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA));
_virtualPadJumpBtnTexture->assignStoredMip(0, image.byteCount(), image.constBits());
_virtualPadJumpBtnTexture->setAutoGenerateMips(true);
}
}
_virtualPadRbBtnPixelSize = dpi * VirtualPad::Manager::JUMP_BTN_FULL_PIXELS / VirtualPad::Manager::DPI;
if (!_virtualPadRbBtnTexture) {
auto iconPath = PathUtils::resourcesPath() + "images/handshake.png";
auto image = QImage(iconPath);
if (image.format() != QImage::Format_ARGB32) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
if ((image.width() > 0) && (image.height() > 0)) {
image = image.scaled(_virtualPadRbBtnPixelSize, _virtualPadRbBtnPixelSize, Qt::KeepAspectRatio);
image = image.mirrored();
_virtualPadRbBtnTexture = gpu::Texture::createStrict(
gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA),
image.width(), image.height(),
gpu::Texture::MAX_NUM_MIPS,
gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR));
_virtualPadRbBtnTexture->setSource("virtualPad handshake");
auto usage = gpu::Texture::Usage::Builder().withColor().withAlpha();
_virtualPadRbBtnTexture->setUsage(usage.build());
_virtualPadRbBtnTexture->setStoredMipFormat(gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA));
_virtualPadRbBtnTexture->assignStoredMip(0, image.byteCount(), image.constBits());
_virtualPadRbBtnTexture->setAutoGenerateMips(true);
}
if (_virtualPadButtons.size() == 0) {
_virtualPadButtons.append(VirtualPadButton(
dpi * VirtualPad::Manager::JUMP_BTN_FULL_PIXELS / VirtualPad::Manager::DPI,
PathUtils::resourcesPath() + "images/fly.png",
VirtualPad::Manager::Button::JUMP));
_virtualPadButtons.append(VirtualPadButton(
dpi * VirtualPad::Manager::JUMP_BTN_FULL_PIXELS / VirtualPad::Manager::DPI,
PathUtils::resourcesPath() + "images/handshake.png",
VirtualPad::Manager::Button::HANDSHAKE));
}
#endif
Parent::customizeContext();
@ -158,10 +118,6 @@ void Basic2DWindowOpenGLDisplayPlugin::compositeExtra() {
_virtualPadPixelSize, _virtualPadPixelSize);
auto stickTransform = DependencyManager::get<CompositorHelper>()->getPoint2DTransform(virtualPadManager.getLeftVirtualPad()->getCurrentTouch(),
_virtualPadPixelSize, _virtualPadPixelSize);
auto jumpTransform = DependencyManager::get<CompositorHelper>()->getPoint2DTransform(virtualPadManager.getJumpButtonPosition(),
_virtualPadJumpBtnPixelSize, _virtualPadJumpBtnPixelSize);
auto rbTransform = DependencyManager::get<CompositorHelper>()->getPoint2DTransform(virtualPadManager.getRbButtonPosition(),
_virtualPadRbBtnPixelSize, _virtualPadRbBtnPixelSize);
render([&](gpu::Batch& batch) {
batch.enableStereo(false);
@ -178,13 +134,9 @@ void Basic2DWindowOpenGLDisplayPlugin::compositeExtra() {
batch.setModelTransform(stickTransform);
batch.draw(gpu::TRIANGLE_STRIP, 4);
batch.setResourceTexture(0, _virtualPadJumpBtnTexture);
batch.setModelTransform(jumpTransform);
batch.draw(gpu::TRIANGLE_STRIP, 4);
batch.setResourceTexture(0, _virtualPadRbBtnTexture);
batch.setModelTransform(rbTransform);
batch.draw(gpu::TRIANGLE_STRIP, 4);
foreach(VirtualPadButton virtualPadButton, _virtualPadButtons) {
virtualPadButton.draw(batch, virtualPadManager.getButtonPosition(virtualPadButton._button));
}
});
}
#endif
@ -209,3 +161,47 @@ bool Basic2DWindowOpenGLDisplayPlugin::isThrottled() const {
QScreen* Basic2DWindowOpenGLDisplayPlugin::getFullscreenTarget() {
return qApp->primaryScreen();
}
#if defined(Q_OS_ANDROID)
Basic2DWindowOpenGLDisplayPlugin::VirtualPadButton::VirtualPadButton(qreal pixelSize,
QString iconPath,
VirtualPad::Manager::Button button) :
_pixelSize { pixelSize },
_button { button }
{
if (!_texture) {
auto image = QImage(iconPath);
if (image.format() != QImage::Format_ARGB32) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
if ((image.width() > 0) && (image.height() > 0)) {
image = image.scaled(_pixelSize, _pixelSize, Qt::KeepAspectRatio);
image = image.mirrored();
_texture = gpu::Texture::createStrict(
gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA),
image.width(), image.height(),
gpu::Texture::MAX_NUM_MIPS,
gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR));
_texture->setSource(iconPath.toStdString());
auto usage = gpu::Texture::Usage::Builder().withColor().withAlpha();
_texture->setUsage(usage.build());
_texture->setStoredMipFormat(gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA));
_texture->assignStoredMip(0, image.byteCount(), image.constBits());
_texture->setAutoGenerateMips(true);
}
}
}
void Basic2DWindowOpenGLDisplayPlugin::VirtualPadButton::draw(gpu::Batch &batch,
glm::vec2 buttonPosition) {
auto transform = DependencyManager::get<CompositorHelper>()->getPoint2DTransform(
buttonPosition,
_pixelSize, _pixelSize);
batch.setResourceTexture(0, _texture);
batch.setModelTransform(transform);
batch.draw(gpu::TRIANGLE_STRIP, 4);
}
#endif

View file

@ -9,6 +9,10 @@
#include "OpenGLDisplayPlugin.h"
#if defined(Q_OS_ANDROID)
#include "VirtualPadManager.h"
#endif
const float TARGET_FRAMERATE_Basic2DWindowOpenGL = 60.0f;
class QScreen;
@ -52,5 +56,20 @@ private:
gpu::TexturePointer _virtualPadRbBtnTexture;
qreal _virtualPadRbBtnPixelSize;
class VirtualPadButton {
public:
VirtualPadButton() {}
VirtualPadButton(qreal pixelSize, QString iconPath, VirtualPad::Manager::Button button);
void draw(gpu::Batch& batch, glm::vec2 buttonPosition);
gpu::TexturePointer _texture;
qreal _pixelSize;
VirtualPad::Manager::Button _button;
};
QVector<VirtualPadButton> _virtualPadButtons;
#endif
};

View file

@ -66,8 +66,7 @@ void TouchscreenVirtualPadDevice::resize() {
_fixedRadius = _screenDPI * 0.5f * VirtualPad::Manager::BASE_DIAMETER_PIXELS / VirtualPad::Manager::DPI;
_fixedRadiusForCalc = _fixedRadius - _screenDPI * VirtualPad::Manager::STICK_RADIUS_PIXELS / VirtualPad::Manager::DPI;
_jumpButtonRadius = _screenDPI * VirtualPad::Manager::JUMP_BTN_TRIMMED_RADIUS_PIXELS / VirtualPad::Manager::DPI;
_rbButtonRadius = _jumpButtonRadius;
_buttonRadius = _screenDPI * VirtualPad::Manager::JUMP_BTN_TRIMMED_RADIUS_PIXELS / VirtualPad::Manager::DPI;
}
auto& virtualPadManager = VirtualPad::Manager::instance();
@ -90,18 +89,16 @@ void TouchscreenVirtualPadDevice::setupControlsPositions(VirtualPad::Manager& vi
float jumpBtnPixelSize = _screenDPI * VirtualPad::Manager::JUMP_BTN_FULL_PIXELS / VirtualPad::Manager::DPI;
float rightMargin = _screenDPI * VirtualPad::Manager::JUMP_BTN_RIGHT_MARGIN_PIXELS / VirtualPad::Manager::DPI;
float bottomMargin = _screenDPI * VirtualPad::Manager::JUMP_BTN_BOTTOM_MARGIN_PIXELS/ VirtualPad::Manager::DPI;
_jumpButtonPosition = glm::vec2( eventScreen->availableSize().width() - rightMargin - jumpBtnPixelSize, eventScreen->availableSize().height() - bottomMargin - _jumpButtonRadius - _extraBottomMargin);
// RB button (use same size as jump)
_rbButtonPosition = glm::vec2( eventScreen->availableSize().width() - rightMargin - jumpBtnPixelSize, eventScreen->availableSize().height() - 2 * bottomMargin - 3 * _rbButtonRadius - _extraBottomMargin);
glm::vec2 jumpButtonPosition = glm::vec2( eventScreen->availableSize().width() - rightMargin - jumpBtnPixelSize, eventScreen->availableSize().height() - bottomMargin - _buttonRadius - _extraBottomMargin);
glm::vec2 rbButtonPosition = glm::vec2( eventScreen->availableSize().width() - rightMargin - jumpBtnPixelSize, eventScreen->availableSize().height() - 2 * bottomMargin - 3 * _buttonRadius - _extraBottomMargin);
// Avoid generating buttons in portrait mode
if ( eventScreen->availableSize().width() > eventScreen->availableSize().height() && _buttonsManager.buttonsCount() == 0) {
_buttonsManager.addButton(TouchscreenButton(JUMP_BUTTON_PRESS, JUMP, _jumpButtonRadius, _jumpButtonPosition, _inputDevice ));
_buttonsManager.addButton(TouchscreenButton(RB, RB_BUTTON, _rbButtonRadius, _rbButtonPosition, _inputDevice ));
_buttonsManager.addButton(TouchscreenButton(JUMP_BUTTON_PRESS, JUMP, _buttonRadius, jumpButtonPosition, _inputDevice ));
_buttonsManager.addButton(TouchscreenButton(RB, RB_BUTTON, _buttonRadius, rbButtonPosition, _inputDevice ));
virtualPadManager.setJumpButtonPosition(_jumpButtonPosition);
virtualPadManager.setRbButtonPosition(_rbButtonPosition);
virtualPadManager.setButtonPosition(VirtualPad::Manager::Button::JUMP, jumpButtonPosition);
virtualPadManager.setButtonPosition(VirtualPad::Manager::Button::HANDSHAKE, rbButtonPosition);
}
}
@ -484,10 +481,10 @@ TouchscreenVirtualPadDevice::TouchscreenButton::TouchscreenButton(
TouchscreenVirtualPadDevice::TouchButtonChannel channelIn,
TouchscreenVirtualPadDevice::TouchType touchTypeIn, float buttonRadiusIn,
glm::vec2 buttonPositionIn, std::shared_ptr<InputDevice> inputDeviceIn) :
channel(channelIn),
touchType(touchTypeIn),
buttonRadius(buttonRadiusIn),
buttonPosition(buttonPositionIn),
buttonRadius(buttonRadiusIn),
touchType(touchTypeIn),
channel(channelIn),
_inputDevice(inputDeviceIn)
{
}
@ -514,8 +511,6 @@ void TouchscreenVirtualPadDevice::TouchscreenButton::touchEnd() {
}
bool TouchscreenVirtualPadDevice::TouchscreenButton::touchBeginIsValid(glm::vec2 touchPoint) {
qDebug() << "[HANDSHAKE] isValid " << (glm::distance2(touchPoint, buttonPosition) < buttonRadius * buttonRadius) <<
" dist2 " << glm::distance2(touchPoint, buttonPosition) << " vs " << buttonRadius * buttonRadius;
return glm::distance2(touchPoint, buttonPosition) < buttonRadius * buttonRadius;
}
@ -560,15 +555,7 @@ bool TouchscreenVirtualPadDevice::TouchscreenButtonsManager::findStartingTouchPo
for(int i = 0; i < buttons.size(); i++) {
TouchscreenButton &button = buttons[i];
qDebug() << "[HANDSHAKE] !button._found && button._candidatePointIdx == -1 " << (!button._found && button._candidatePointIdx == -1);
qDebug() << "[HANDSHAKE] button.touchBeginIsValid(thisPoint) " << (button.touchBeginIsValid(thisPoint));
qDebug() << "[HANDSHAKE] !globalUnusedTouches.count(thisPointId) " << (!globalUnusedTouches.count(thisPointId));
if (globalUnusedTouches.count(thisPointId)) {
qDebug() << "[HANDSHAKE] globalUnusedTouches[thisPointId] == button.touchType " << (globalUnusedTouches[thisPointId] == button.touchType);
}
if (!button._found && button._candidatePointIdx == -1 && button.touchBeginIsValid(thisPoint)) {
qDebug() << "[HANDSHAKE] CHECK AGAIN !globalUnusedTouches.count(thisPointId) " << (!globalUnusedTouches.count(thisPointId));
qDebug() << "[HANDSHAKE] CHECK AGAIN double-check !globalUnusedTouches.count(thisPointId) " << (!globalUnusedTouches.count(thisPointId));
if (!globalUnusedTouches.count(thisPointId) ) {
button._candidatePointIdx = thisPointIdx;
return true;

View file

@ -168,11 +168,7 @@ protected:
float _fixedRadiusForCalc;
int _extraBottomMargin {0};
glm::vec2 _jumpButtonPosition;
float _jumpButtonRadius;
glm::vec2 _rbButtonPosition;
float _rbButtonRadius;
float _buttonRadius;
TouchscreenButtonsManager _buttonsManager;

View file

@ -76,22 +76,6 @@ namespace VirtualPad {
_extraBottomMargin = margin;
}
glm::vec2 Manager::getJumpButtonPosition() {
return _jumpButtonPosition;
}
void Manager::setJumpButtonPosition(glm::vec2 point) {
_jumpButtonPosition = point;
}
glm::vec2 Manager::getRbButtonPosition() {
return _rbButtonPosition;
}
void Manager::setRbButtonPosition(glm::vec2 point) {
_rbButtonPosition = point;
}
void Manager::requestHapticFeedback(int duration) {
emit hapticFeedbackRequested(duration);
}
@ -100,6 +84,17 @@ namespace VirtualPad {
return &_leftVPadInstance;
}
glm::vec2 Manager::getButtonPosition(Manager::Button button) {
if (_buttonsPositions.count(button)) {
return _buttonsPositions.at(button);
}
return glm::vec2();
}
void Manager::setButtonPosition(Manager::Button button, glm::vec2 point) {
_buttonsPositions[button] = point;
}
bool Instance::isShown() {
return _shown;
}

View file

@ -44,10 +44,15 @@ namespace VirtualPad {
void hide(bool hide);
int extraBottomMargin();
void setExtraBottomMargin(int margin);
glm::vec2 getJumpButtonPosition();
void setJumpButtonPosition(glm::vec2 point);
glm::vec2 getRbButtonPosition();
void setRbButtonPosition(glm::vec2 point);
enum Button {
JUMP,
HANDSHAKE
};
glm::vec2 getButtonPosition(Button button);
void setButtonPosition(Button button, glm::vec2 point);
void requestHapticFeedback(int duration);
static const float DPI;
@ -69,6 +74,7 @@ namespace VirtualPad {
glm::vec2 _jumpButtonPosition;
glm::vec2 _rbButtonPosition;
int _extraBottomMargin {0};
std::map<Button, glm::vec2> _buttonsPositions;
};
}