add web pen pressure

This commit is contained in:
2019-10-10 20:57:16 +02:00
parent fd3eba69b8
commit e6e3488291

View File

@@ -64,6 +64,47 @@ std::string UtilityGetString(uintptr_t s)
return *reinterpret_cast<std::string*>(s);
}
kEventSource StringToType(const std::string& type)
{
if (type == "pen")
return kEventSource::Stylus;
else if (type == "touch")
return kEventSource::Touch;
return kEventSource::Mouse;
}
int ConvertButtonType(int js_button)
{
if (js_button == 0)
return 0;
else if (js_button == 2)
return 1;
return 0;
}
void CanvasOnPointerDown(std::string type, float x, float y, float f, int button)
{
app.ui_task_async([=]{
if (button == 0 || button == 2)
app.mouse_down(ConvertButtonType(button), x, y, f, StringToType(type), false);
});
}
void CanvasOnPointerUp(std::string type, float x, float y, int button)
{
app.ui_task_async([=]{
if (button == 0 || button == 2)
app.mouse_up(ConvertButtonType(button), x, y, StringToType(type), false);
});
}
void CanvasOnPointerMove(std::string type, float x, float y, float f)
{
app.ui_task_async([=]{
app.mouse_move(x, y, f, StringToType(type), false);
});
}
EMSCRIPTEN_BINDINGS(TaskCallback_bind) {
class_<TaskCallback<void(std::string)>>("TaskCallbackOpen");
class_<TaskCallback<void(bool)>>("TaskCallbackSave");
@@ -72,6 +113,9 @@ EMSCRIPTEN_BINDINGS(TaskCallback_bind) {
function("TaskCallback_open_call", &TaskCallback_open_call);
function("TaskCallback_open_delete", &TaskCallback_open_delete);
function("UtilityGetString", &UtilityGetString);
function("CanvasOnPointerDown", &CanvasOnPointerDown);
function("CanvasOnPointerUp", &CanvasOnPointerUp);
function("CanvasOnPointerMove", &CanvasOnPointerMove);
}
extern "C" {
@@ -118,6 +162,7 @@ int main()
wnd = glfwCreateWindow(1024, 768, "PanoPainter", nullptr, nullptr);
glfwMakeContextCurrent(wnd);
/*
glfwSetCursorPosCallback(wnd, [](GLFWwindow* wnd, double x, double y){
g_cursor_pos = glm::vec2(x, y);
app.ui_task_async([=]{
@@ -132,6 +177,7 @@ int main()
app.mouse_up(button, g_cursor_pos.x, g_cursor_pos.y, kEventSource::Mouse, false);
});
});
*/
glfwSetWindowSizeCallback(wnd, [](GLFWwindow* wnd, int width, int height){
app.ui_task_async([=]{
app.resize(width, height);
@@ -149,6 +195,22 @@ int main()
}, true);
});
EM_ASM (
Module.canvas.onpointermove = function(event) {
f = event.pointerType == "pen" ? event.pressure : 1.0;
Module.CanvasOnPointerMove(event.pointerType, event.layerX, event.layerY, f);
};
Module.canvas.onpointerdown = function(event) {
console.log(event);
f = event.pointerType == "pen" ? event.pressure : 1.0;
Module.CanvasOnPointerDown(event.pointerType, event.layerX, event.layerY, f, event.button);
};
Module.canvas.onpointerup = function(event) {
Module.CanvasOnPointerUp(event.pointerType, event.layerX, event.layerY, event.button);
};
Module.canvas.style.touchAction = 'none';
);
printf("GL version: %s\n", glGetString(GL_VERSION));
printf("GL vendor: %s\n", glGetString(GL_VENDOR));
printf("GL renderer: %s\n", glGetString(GL_RENDERER));