implement eraser detection on windows

This commit is contained in:
2018-10-09 02:12:09 +02:00
parent dba5eef824
commit a6d0914bf8
11 changed files with 95 additions and 35 deletions

View File

@@ -314,7 +314,7 @@
<text id="title" text="Just a test message" font-face="arial" font-size="11"></text>
</border>
<border width="400" color="0 0 0 .9" pad="10" dir="col">
<text id="message" text="Longer description for the error or the message." font-face="arial" font-size="11"></text>
<text id="message" text-wrap-width="380" text="Longer description for the error or the message." font-face="arial" font-size="11"></text>
<node height="40" grow="1" dir="row" align="flex-end" justify="flex-end">
<button id="btn-ok" text="Ok" width="50" height="30" margin="0 10 0 0"/>
<button id="btn-cancel" text="Cancel" width="60" height="30"/>

View File

@@ -119,9 +119,9 @@ public:
void async_redraw();
void async_end();
void resize(float w, float h);
bool mouse_down(int button, float x, float y, float pressure, kEventSource source);
bool mouse_move(float x, float y, float pressure, kEventSource source);
bool mouse_up(int button, float x, float y, kEventSource source);
bool mouse_down(int button, float x, float y, float pressure, kEventSource source, bool eraser);
bool mouse_move(float x, float y, float pressure, kEventSource source, bool eraser);
bool mouse_up(int button, float x, float y, kEventSource source, bool eraser);
bool mouse_scroll(float x, float y, float delta);
bool mouse_cancel(int button);
bool gesture_start(const glm::vec2& p0, const glm::vec2& p1);

View File

@@ -407,7 +407,7 @@ void App::dialog_export()
#elif defined(__OSX__)
message_box("Export JPG", "Image exported to Pictures/PanoPainter folder");
#elif defined(_WIN32)
message_box("Export JPG", "Image exported to Images/PanoPainter folder");
message_box("Export JPG", "Image exported to " + work_path);
#endif
});
}

View File

@@ -151,7 +151,7 @@ void App::display_file(std::string path)
#endif
}
bool App::mouse_down(int button, float x, float y, float pressure, kEventSource source)
bool App::mouse_down(int button, float x, float y, float pressure, kEventSource source, bool eraser)
{
redraw = true;
MouseEvent e;
@@ -159,11 +159,12 @@ bool App::mouse_down(int button, float x, float y, float pressure, kEventSource
e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure;
e.m_source = source;
e.m_eraser = eraser;
auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update();
return ret == kEventResult::Consumed;
}
bool App::mouse_move(float x, float y, float pressure, kEventSource source)
bool App::mouse_move(float x, float y, float pressure, kEventSource source, bool eraser)
{
redraw = true;
MouseEvent e;
@@ -171,18 +172,20 @@ bool App::mouse_move(float x, float y, float pressure, kEventSource source)
e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure;
e.m_source = source;
e.m_eraser = eraser;
kEventResult ret = kEventResult::Available;
if (auto* main = layout[main_id])
ret = main->on_event(&e);
return ret == kEventResult::Consumed;
}
bool App::mouse_up(int button, float x, float y, kEventSource source)
bool App::mouse_up(int button, float x, float y, kEventSource source, bool eraser)
{
redraw = true;
MouseEvent e;
e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL;
e.m_pos = { x / zoom, y / zoom };
e.m_source = source;
e.m_eraser = eraser;
auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update();
return ret == kEventResult::Consumed;

View File

@@ -74,6 +74,10 @@ void CanvasModeBasicCamera::on_GestureEvent(GestureEvent* ge)
void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
m_touching = (me->m_source == kEventSource::Touch);
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:
@@ -162,8 +166,7 @@ void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
void CanvasModePen::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera)
{
#if !(defined(__IOS__) || defined(__ANDROID__))
//if (!m_dragging)
if (!m_touching)
{
auto pos = m_resizing ? m_size_pos_start : m_cur_pos;
if (App::I.keys[(int)kKey::KeyAlt] && !m_resizing)
@@ -179,7 +182,8 @@ void CanvasModePen::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const
glm::scale(glm::vec3(1, -1, 1)) *
ortho *
glm::translate(glm::vec3(pos, 0)) *
glm::scale(glm::vec3(canvas->m_current_brush.m_tip_size * 800.f * tip_scale))
glm::scale(glm::vec3(canvas->m_current_brush.m_tip_size * 800.f * tip_scale)) *
glm::eulerAngleZ(canvas->m_current_brush.m_tip_angle * (float)(M_PI * 2.0))
);
glEnable(GL_BLEND);
glActiveTexture(GL_TEXTURE0);
@@ -189,7 +193,6 @@ void CanvasModePen::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const
canvas->m_plane.draw_fill();
tex.unbind();
}
#endif
}
void CanvasModePen::leave()
@@ -216,6 +219,8 @@ void CanvasModePen::enter()
void CanvasModeLine::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:
@@ -273,6 +278,8 @@ void CanvasModeLine::init()
void CanvasModeCamera::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownR:
@@ -306,6 +313,8 @@ void CanvasModeCamera::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
void CanvasModeGrid::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:
@@ -412,6 +421,8 @@ void CanvasModeMaskFree::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
static glm::vec2 oldpos;
static glm::vec2 oldvec;
static float acc = 0.f;
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:
@@ -605,6 +616,8 @@ void CanvasModeMaskLine::enter()
void CanvasModeMaskLine::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:
@@ -707,6 +720,8 @@ void CanvasModeFill::leave()
void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
{
if (canvas->m_touch_lock && me->m_source == kEventSource::Touch)
return;
switch (me->m_type)
{
case kEventType::MouseDownL:

View File

@@ -37,6 +37,7 @@ public:
class CanvasModePen : public CanvasMode
{
bool m_touching = true;
bool m_dragging = false;
glm::vec2 m_pan_start;
glm::vec2 m_cur_pos;
@@ -50,6 +51,7 @@ class CanvasModePen : public CanvasMode
// resizing the tip
bool m_resizing = false;
public:
CanvasModePen() = default;
virtual void on_MouseEvent(MouseEvent* me, glm::vec2& loc) override;
virtual void on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera) override;
virtual void enter() override;

View File

@@ -131,6 +131,7 @@ public:
glm::vec2 m_pos;
float m_pressure = 0;
float m_scroll_delta = 0;
bool m_eraser = false;
kEventSource m_source = kEventSource::Mouse;
};

View File

@@ -29,6 +29,7 @@ std::thread::id gl_thread;
int gl_count = 0;
std::deque<std::packaged_task<void()>> tasklist;
std::mutex task_mutex;
float timer_stylus = 0;
//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString()
@@ -466,10 +467,6 @@ int main(int argc, char** argv)
auto y = unsigned{};
GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &x, &y);
App::I.zoom = (float)x / 96.f;
// SetWindowPos(hWnd, NULL, 0, 0,
// (clientRect.right - clientRect.left) * App::I.zoom,
// (clientRect.bottom - clientRect.top) * App::I.zoom,
// SWP_NOREPOSITION | SWP_NOOWNERZORDER);
App::I.init();
@@ -483,6 +480,11 @@ int main(int argc, char** argv)
App::I.redraw = true;
});
SetWindowPos(hWnd, NULL, 0, 0,
(float)(clientRect.right - clientRect.left) * App::I.zoom,
(float)(clientRect.bottom - clientRect.top) * App::I.zoom,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);
bool running = true;
std::mutex render_mutex;
std::condition_variable render_cv;
@@ -504,6 +506,7 @@ int main(int argc, char** argv)
one_sec += dt;
render_timer += dt;
frame_timer += dt;
timer_stylus += dt;
t0 = t1;
if (one_sec > 1.f)
@@ -531,6 +534,9 @@ int main(int argc, char** argv)
working_list.pop_front();
}
async_unlock();
//LOG("clear");
//WacomTablet::I.m_stylus = false;
//WacomTablet::I.m_eraser = false;
}
}
@@ -542,6 +548,12 @@ int main(int argc, char** argv)
App::I.redraw = true;
render_timer = 0;
}
if (timer_stylus > 0.1 && WacomTablet::I.m_stylus)
{
WacomTablet::I.m_stylus = false;
WacomTablet::I.m_eraser = false;
App::I.redraw = true;
}
if (App::I.redraw)
{
async_lock();
@@ -597,9 +609,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
static POINT lastPoint;
auto extra = GetMessageExtraInfo();
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
// if ((extra & 0xFFFFFF00) == 0xFF515700)
// LOG("source %s", extra & 0x80 ? "touch" : "pen");
switch (msg)
{
@@ -653,6 +664,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
// }
case WT_PACKET:
{
timer_stylus = 0;
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([=] {
WacomTablet::I.handle_message(hWnd, msg, wp, lp);
@@ -688,45 +700,60 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
case WM_MOUSEMOVE:
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([lp, pointer_source, p = WacomTablet::I.get_pressure()]{
App::I.mouse_move((float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), p, pointer_source);
tasklist.emplace_back([lp, extra, p = WacomTablet::I.get_pressure()]{
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
App::I.mouse_move((float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), p, pointer_source, WacomTablet::I.m_eraser);
});
}
break;
case WM_LBUTTONDOWN:
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([lp, pointer_source, hWnd, p = WacomTablet::I.get_pressure()]{
tasklist.emplace_back([lp, extra, hWnd, p = WacomTablet::I.get_pressure()]{
SetCapture(hWnd);
App::I.mouse_down(0, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), p, pointer_source);
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
App::I.mouse_down(0, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), p, pointer_source, WacomTablet::I.m_eraser);
});
}
break;
case WM_LBUTTONUP:
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([lp, pointer_source]{
tasklist.emplace_back([lp, extra]{
WacomTablet::I.reset_pressure();
ReleaseCapture();
App::I.mouse_up(0, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), pointer_source);
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
App::I.mouse_up(0, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), pointer_source, WacomTablet::I.m_eraser);
});
}
break;
case WM_RBUTTONDOWN:
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([lp, pointer_source, hWnd]{
tasklist.emplace_back([lp, extra, hWnd]{
SetCapture(hWnd);
App::I.mouse_down(1, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), 1.f, pointer_source);
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
App::I.mouse_down(1, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), 1.f, pointer_source, 0);
});
}
break;
case WM_RBUTTONUP:
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([lp, pointer_source]{
tasklist.emplace_back([lp, extra]{
ReleaseCapture();
App::I.mouse_up(1, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), pointer_source);
auto pointer_source = WacomTablet::I.m_stylus ? kEventSource::Stylus : kEventSource::Mouse;
if ((extra & 0xFFFFFF00) == 0xFF515700)
pointer_source = kEventSource::Touch;
App::I.mouse_up(1, (float)GET_X_LPARAM(lp), (float)GET_Y_LPARAM(lp), pointer_source, 0);
});
}
break;

View File

@@ -362,6 +362,7 @@ void NodeCanvas::handle_resize(glm::vec2 old_size, glm::vec2 new_size)
kEventResult NodeCanvas::handle_event(Event* e)
{
static bool stylus_eraser = false;
Node::handle_event(e);
MouseEvent* me = static_cast<MouseEvent*>(e);
KeyEvent* ke = static_cast<KeyEvent*>(e);
@@ -370,17 +371,23 @@ kEventResult NodeCanvas::handle_event(Event* e)
switch (e->m_type)
{
case kEventType::MouseMove:
if (stylus_eraser != me->m_eraser)
{
ui::Canvas::set_mode(me->m_eraser ?
ui::Canvas::kCanvasMode::Erase :
ui::Canvas::kCanvasMode::Draw);
stylus_eraser = me->m_eraser;
}
case kEventType::MouseScroll:
case kEventType::MouseDownL:
case kEventType::MouseUpL:
case kEventType::MouseDownR:
case kEventType::MouseUpR:
case kEventType::MouseMove:
case kEventType::MouseCancel:
m_canvas->m_cur_pos = loc;
if (!(m_canvas->m_touch_lock && me->m_source == kEventSource::Touch))
for (auto& mode : *m_canvas->m_mode)
mode->on_MouseEvent(me, loc);
for (auto& mode : *m_canvas->m_mode)
mode->on_MouseEvent(me, loc);
break;
case kEventType::KeyDown:
if (ke->m_key == kKey::KeyE)

View File

@@ -166,7 +166,7 @@ void WacomTablet::handle_message(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lPar
static POINT ptOld, ptNew;
static UINT prsOld, prsNew;
if (gpWTPacket((HCTX)lParam, wParam, &pkt))
if (gpWTPacket((HCTX)lParam, (UINT)wParam, &pkt))
{
if (HIWORD(pkt.pkButtons) == TBN_DOWN)
m_pen_down = true;
@@ -183,6 +183,9 @@ void WacomTablet::handle_message(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lPar
m_pen_pos = { pkt.pkX, pkt.pkX };
m_pen_pres = (float)pkt.pkNormalPressure / (float)TabletPressure.axMax;
m_stylus = true;
m_eraser = (pkt.pkStatus == 0x10);
//LOG("packet %x", pkt.pkStatus);
}
}

View File

@@ -2,7 +2,7 @@
#include "WinTab/msgpack.h"
#include "WinTab/wintab.h"
#define PACKETDATA (PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE)
#define PACKETDATA (PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE | PK_STATUS)
#define PACKETMODE PK_BUTTONS
#include "WinTab/pktdef.h"
#include "WinTab/Utils.h"
@@ -18,6 +18,8 @@ public:
bool m_pen_down = false;
int m_pen_idle = 0;
bool m_mouse_down = false;
bool m_stylus = false;
bool m_eraser = false;
HCTX TabletInit(HWND hWnd);