Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch event to mouse event passthrough in EmscriptenApplication #532

Open
werto87 opened this issue Aug 11, 2021 · 3 comments
Open

Touch event to mouse event passthrough in EmscriptenApplication #532

werto87 opened this issue Aug 11, 2021 · 3 comments
Projects
Milestone

Comments

@werto87
Copy link

werto87 commented Aug 11, 2021

I want to be able to allow touch drag to scroll in my imgui gui. While investigating the problem I made a small example based on magnum example which uses drag to rotate a cube.
I modified this example so it works with emscripten, compiled it with emscripten for wasm and put it online. Drag with mouse works but on mobile it does not work. How to enable touch drag?
Modified example:

/*
    This file is part of Magnum.

    Original authors — credit is appreciated but not required:

        2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021
             — Vladimír Vondruš <mosra@centrum.cz>

    This is free and unencumbered software released into the public domain.

    Anyone is free to copy, modify, publish, use, compile, sell, or distribute
    this software, either in source code form or as a compiled binary, for any
    purpose, commercial or non-commercial, and by any means.

    In jurisdictions that recognize copyright laws, the author or authors of
    this software dedicate any and all copyright interest in the software to
    the public domain. We make this dedication for the benefit of the public
    at large and to the detriment of our heirs and successors. We intend this
    dedication to be an overt act of relinquishment in perpetuity of all
    present and future rights to this software under copyright law.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Matrix4.h>
#include <Magnum/MeshTools/CompressIndices.h>
#include <Magnum/MeshTools/Interleave.h>
#include <Magnum/Platform/EmscriptenApplication.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Primitives/Cube.h>
#include <Magnum/Shaders/PhongGL.h>
#include <Magnum/Trade/MeshData.h>
namespace Magnum
{
namespace Examples
{

using namespace Magnum::Math::Literals;

class PrimitivesExample : public Platform::Application
{
public:
  explicit PrimitivesExample (const Arguments &arguments);

private:
  void drawEvent () override;
  void mousePressEvent (MouseEvent &event) override;
  void mouseReleaseEvent (MouseEvent &event) override;
  void mouseMoveEvent (MouseMoveEvent &event) override;

  GL::Mesh _mesh;
  Shaders::PhongGL _shader;

  Matrix4 _transformation, _projection;
  Color3 _color;
};

PrimitivesExample::PrimitivesExample (const Arguments &arguments) : Platform::Application{ arguments, Configuration{}.setTitle ("Magnum Primitives Example") }
{
  GL::Renderer::enable (GL::Renderer::Feature::DepthTest);
  GL::Renderer::enable (GL::Renderer::Feature::FaceCulling);
  Trade::MeshData cube = Primitives::cubeSolid ();

  GL::Buffer vertices{ GL::Buffer::TargetHint::Array };
  vertices.setData (MeshTools::interleave (cube.positions3DAsArray (), cube.normalsAsArray ()));

  std::pair<Containers::Array<char>, MeshIndexType> compressed = MeshTools::compressIndices (cube.indicesAsArray ());
  GL::Buffer indices{ GL::Buffer::TargetHint::ElementArray };
  indices.setData (compressed.first);

  _mesh.setPrimitive (cube.primitive ()).setCount (cube.indexCount ()).addVertexBuffer (std::move (vertices), 0, Shaders::PhongGL::Position{}, Shaders::PhongGL::Normal{}).setIndexBuffer (std::move (indices), 0, compressed.second);

  _transformation = Matrix4::rotationX (30.0_degf) * Matrix4::rotationY (40.0_degf);
  _projection = Matrix4::perspectiveProjection (35.0_degf, Vector2{ windowSize () }.aspectRatio (), 0.01f, 100.0f) * Matrix4::translation (Vector3::zAxis (-10.0f));
  _color = Color3::fromHsv ({ 35.0_degf, 1.0f, 1.0f });
}

void
PrimitivesExample::drawEvent ()
{
  GL::defaultFramebuffer.clear (GL::FramebufferClear::Color | GL::FramebufferClear::Depth);

  _shader.setLightPositions ({ { 1.4f, 1.0f, 0.75f, 0.0f } }).setDiffuseColor (_color).setAmbientColor (Color3::fromHsv ({ _color.hue (), 1.0f, 0.3f })).setTransformationMatrix (_transformation).setNormalMatrix (_transformation.normalMatrix ()).setProjectionMatrix (_projection).draw (_mesh);

  swapBuffers ();
}

void
PrimitivesExample::mousePressEvent (MouseEvent &event)
{
  if (event.button () != MouseEvent::Button::Left) return;

  event.setAccepted ();
}

void
PrimitivesExample::mouseReleaseEvent (MouseEvent &event)
{
  _color = Color3::fromHsv ({ _color.hue () + 50.0_degf, 1.0f, 1.0f });

  event.setAccepted ();
  redraw ();
}

void
PrimitivesExample::mouseMoveEvent (MouseMoveEvent &event)
{
  if (!(event.buttons () & MouseMoveEvent::Button::Left)) return;

  Vector2 delta = 3.0f * Vector2{ event.relativePosition () } / Vector2{ windowSize () };

  _transformation = Matrix4::rotationX (Rad{ delta.y () }) * _transformation * Matrix4::rotationY (Rad{ delta.x () });

  event.setAccepted ();
  redraw ();
}

}
}

MAGNUM_EMSCRIPTENAPPLICATION_MAIN (Magnum::Examples::PrimitivesExample)

Basically I added

#include <Magnum/Platform/EmscriptenApplication.h>

Replaced old main with

MAGNUM_EMSCRIPTENAPPLICATION_MAIN (Magnum::Examples::PrimitivesExample)

and changed GL::Buffer vertices to work with emscripten

GL::Buffer vertices{ GL::Buffer::TargetHint::Array };
GL::Buffer indices{ GL::Buffer::TargetHint::ElementArray };
@werto87 werto87 changed the title Touch drag event How to enable touch drag? Aug 11, 2021
@werto87
Copy link
Author

werto87 commented Aug 15, 2021

you seem to have this on the todo list so I close this isue.

EmscriptenApplication doesn't report touch drag as move events (Sdl2Application did, I think)

@werto87 werto87 closed this as completed Aug 15, 2021
@zaichenhu
Copy link

well, what's the solution of this problem, I have the same trouble...

@mosra mosra added this to the 2022.0a milestone Aug 29, 2022
@mosra mosra added this to TODO in Platforms via automation Aug 29, 2022
@mosra mosra changed the title How to enable touch drag? Touch event to mouse event passthrough in EmscriptenApplication Aug 29, 2022
@mosra
Copy link
Owner

mosra commented Aug 29, 2022

Reopening until this is actually implemented. For the record, the workaround is to use Sdl2Application instead of EmscriptenApplication on the web as well.

@mosra mosra reopened this Aug 29, 2022
Platforms automation moved this from TODO to In progress Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Platforms
  
In progress
Development

No branches or pull requests

3 participants