implement canvas touch lock

This commit is contained in:
2017-08-19 13:18:35 +01:00
parent ac3857b697
commit da4bfff69b
12 changed files with 56 additions and 27 deletions

View File

@@ -214,49 +214,49 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_down(0, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure); App::I.mouse_down(0, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)rightMouseDown:(NSEvent *)theEvent - (void)rightMouseDown:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_down(1, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure); App::I.mouse_down(1, mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)mouseUp:(NSEvent *)theEvent - (void)mouseUp:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_up(0, mouseLoc.x, App::I.height - mouseLoc.y - 1); App::I.mouse_up(0, mouseLoc.x, App::I.height - mouseLoc.y - 1, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)rightMouseUp:(NSEvent *)theEvent - (void)rightMouseUp:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_up(1, mouseLoc.x, App::I.height - mouseLoc.y - 1); App::I.mouse_up(1, mouseLoc.x, App::I.height - mouseLoc.y - 1, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)mouseMoved:(NSEvent *)theEvent - (void)mouseMoved:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure); App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
-(void)mouseDragged:(NSEvent *)theEvent -(void)mouseDragged:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure); App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)rightMouseDragged:(NSEvent *)theEvent - (void)rightMouseDragged:(NSEvent *)theEvent
{ {
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; auto mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure); App::I.mouse_move(mouseLoc.x, App::I.height - mouseLoc.y - 1, theEvent.pressure, kEventSource::Mouse);
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
- (void)scrollWheel:(NSEvent *)theEvent - (void)scrollWheel:(NSEvent *)theEvent

View File

@@ -115,7 +115,8 @@ glm::vec2 t_pos;
CGPoint touchLocation = [touch locationInView:self.view]; CGPoint touchLocation = [touch locationInView:self.view];
float scale = self.view.contentScaleFactor; float scale = self.view.contentScaleFactor;
App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force); kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force, source);
t_count = 1; t_count = 1;
t_pos = {touchLocation.x * scale, touchLocation.y * scale}; t_pos = {touchLocation.x * scale, touchLocation.y * scale};
} }
@@ -162,7 +163,8 @@ glm::vec2 t_pos;
} }
else else
{ {
App::I.mouse_move(touchLocation.x * scale, touchLocation.y * scale, force); kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
App::I.mouse_move(touchLocation.x * scale, touchLocation.y * scale, force, source);
} }
} }
t_pos = {tl0.x * scale, tl0.y * scale}; t_pos = {tl0.x * scale, tl0.y * scale};
@@ -173,10 +175,12 @@ glm::vec2 t_pos;
CGPoint touchLocation = [touch locationInView:self.view]; CGPoint touchLocation = [touch locationInView:self.view];
float scale = self.view.contentScaleFactor; float scale = self.view.contentScaleFactor;
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
if (t_count == 2) if (t_count == 2)
App::I.gesture_end(); App::I.gesture_end();
else else
App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale); App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source);
t_count = 0; t_count = 0;
t_pos = {touchLocation.x * scale, touchLocation.y * scale}; t_pos = {touchLocation.x * scale, touchLocation.y * scale};
} }

View File

@@ -454,6 +454,7 @@
<node dir="row" justify="center" grow="1"> <node dir="row" justify="center" grow="1">
<button id="btn-pen" width="50" height="100%" margin="0 0 0 0" text="Pen"/> <button id="btn-pen" width="50" height="100%" margin="0 0 0 0" text="Pen"/>
<button id="btn-pick" width="50" height="100%" margin="0 0 0 0" text="Pick"/> <button id="btn-pick" width="50" height="100%" margin="0 0 0 0" text="Pick"/>
<button id="btn-touchlock" width="50" height="100%" margin="0 0 0 0" text="Lock"/>
<button id="btn-erase" width="60" height="100%" margin="0 0 0 5" text="Erase"/> <button id="btn-erase" width="60" height="100%" margin="0 0 0 5" text="Erase"/>

View File

@@ -139,6 +139,8 @@ void App::update(float dt)
CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0]; CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0];
layout[main_id]->find<NodeButton>("btn-pick")->set_color( layout[main_id]->find<NodeButton>("btn-pick")->set_color(
mode->m_picking ? color_button_hlight : color_button_normal); mode->m_picking ? color_button_hlight : color_button_normal);
layout[main_id]->find<NodeButton>("btn-touchlock")->set_color(
canvas->m_canvas->m_touch_lock ? color_button_hlight : color_button_normal);
auto observer = [this](Node* n) auto observer = [this](Node* n)

View File

@@ -80,9 +80,9 @@ public:
void update_memory_usage(size_t bytes); void update_memory_usage(size_t bytes);
void update(float dt); void update(float dt);
void resize(float w, float h); void resize(float w, float h);
bool mouse_down(int button, float x, float y, float pressure); bool mouse_down(int button, float x, float y, float pressure, kEventSource source);
bool mouse_move(float x, float y, float pressure); bool mouse_move(float x, float y, float pressure, kEventSource source);
bool mouse_up(int button, float x, float y); bool mouse_up(int button, float x, float y, kEventSource source);
bool mouse_scroll(float x, float y, float delta); bool mouse_scroll(float x, float y, float delta);
bool mouse_cancel(int button); bool mouse_cancel(int button);
bool gesture_start(const glm::vec2& p0, const glm::vec2& p1); bool gesture_start(const glm::vec2& p0, const glm::vec2& p1);

View File

@@ -28,35 +28,38 @@ void App::hideKeyboard()
#endif #endif
} }
bool App::mouse_down(int button, float x, float y, float pressure) bool App::mouse_down(int button, float x, float y, float pressure, kEventSource source)
{ {
redraw = true; redraw = true;
MouseEvent e; MouseEvent e;
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL; e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x / zoom, y / zoom }; e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure; e.m_pressure = pressure;
e.m_source = source;
auto ret = layout[main_id]->on_event(&e); auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update(); layout[main_id]->update();
return ret == kEventResult::Consumed; return ret == kEventResult::Consumed;
} }
bool App::mouse_move(float x, float y, float pressure) bool App::mouse_move(float x, float y, float pressure, kEventSource source)
{ {
redraw = true; redraw = true;
MouseEvent e; MouseEvent e;
e.m_type = kEventType::MouseMove; e.m_type = kEventType::MouseMove;
e.m_pos = { x / zoom, y / zoom }; e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure; e.m_pressure = pressure;
e.m_source = source;
kEventResult ret = kEventResult::Available; kEventResult ret = kEventResult::Available;
if (auto* main = layout[main_id]) if (auto* main = layout[main_id])
ret = main->on_event(&e); ret = main->on_event(&e);
return ret == kEventResult::Consumed; return ret == kEventResult::Consumed;
} }
bool App::mouse_up(int button, float x, float y) bool App::mouse_up(int button, float x, float y, kEventSource source)
{ {
redraw = true; redraw = true;
MouseEvent e; MouseEvent e;
e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL; e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL;
e.m_pos = { x / zoom, y / zoom }; e.m_pos = { x / zoom, y / zoom };
e.m_source = source;
auto ret = layout[main_id]->on_event(&e); auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update(); layout[main_id]->update();
return ret == kEventResult::Consumed; return ret == kEventResult::Consumed;

View File

@@ -240,6 +240,12 @@ void App::init_toolbar_draw()
} }
}; };
} }
if (auto* button = layout[main_id]->find<NodeButton>("btn-touchlock"))
{
button->on_click = [this](Node*) {
canvas->m_canvas->m_touch_lock = !canvas->m_canvas->m_touch_lock;
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-erase")) if (auto* button = layout[main_id]->find<NodeButton>("btn-erase"))
{ {
button->on_click = [this](Node*) { button->on_click = [this](Node*) {

View File

@@ -228,7 +228,7 @@ void App::initShaders()
#else #else
" mediump vec4 bg = texture(tex_bg, uv2);\n" " mediump vec4 bg = texture(tex_bg, uv2);\n"
#endif #endif
" if (fg.a == 1.0/255.0) { frag = bg; return; }\n" " if (fg.a < 1.0/255.0) { frag = bg; return; }\n"
" mediump float contribution = max((1.0 - bg.a) * fg.a, 1.0/255.0);\n" " mediump float contribution = max((1.0 - bg.a) * fg.a, 1.0/255.0);\n"
" mediump float alpha_tot = bg.a + contribution;" " mediump float alpha_tot = bg.a + contribution;"
" mediump vec3 rgb = mix(bg.rgb, fg.rgb, fg.a / alpha_tot);\n" " mediump vec3 rgb = mix(bg.rgb, fg.rgb, fg.a / alpha_tot);\n"

View File

@@ -49,6 +49,7 @@ class Canvas
public: public:
static Canvas* I; static Canvas* I;
bool m_alpha_lock = false; bool m_alpha_lock = false;
bool m_touch_lock = true;
glm::mat4 m_mv; glm::mat4 m_mv;
glm::mat4 m_proj; glm::mat4 m_proj;
glm::vec4 m_box; glm::vec4 m_box;

View File

@@ -47,6 +47,13 @@ enum class kEventType : uint8_t
ButtonUp, ButtonUp,
}; };
enum class kEventSource : uint8_t
{
Mouse,
Touch,
Stylus,
};
class Event class Event
{ {
public: public:
@@ -59,8 +66,9 @@ class MouseEvent : public Event
public: public:
MouseEvent() { m_cat = kEventCategory::MouseEvent; } MouseEvent() { m_cat = kEventCategory::MouseEvent; }
glm::vec2 m_pos; glm::vec2 m_pos;
float m_pressure; float m_pressure = 0;
float m_scroll_delta; float m_scroll_delta = 0;
kEventSource m_source = kEventSource::Mouse;
}; };
class KeyEvent : public Event class KeyEvent : public Event

View File

@@ -234,8 +234,9 @@ kEventResult NodeCanvas::handle_event(Event* e)
case kEventType::MouseUpR: case kEventType::MouseUpR:
case kEventType::MouseMove: case kEventType::MouseMove:
case kEventType::MouseCancel: case kEventType::MouseCancel:
for (auto& mode : *m_canvas->m_mode) if (!(m_canvas->m_touch_lock && me->m_source == kEventSource::Touch))
mode->on_MouseEvent(me, loc); for (auto& mode : *m_canvas->m_mode)
mode->on_MouseEvent(me, loc);
break; break;
case kEventType::KeyDown: case kEventType::KeyDown:
// if (ke->m_key == kKey::KeyE) // if (ke->m_key == kKey::KeyE)

View File

@@ -98,11 +98,14 @@ kEventResult NodeSliderH::handle_event(Event* e)
break; break;
case kEventType::MouseCancel: case kEventType::MouseCancel:
mouse_release(); mouse_release();
if (dragging)
{
m_value = m_old_value;
set_value(glm::length(m_value));
if (on_value_changed)
on_value_changed(this, glm::length(m_value));
}
dragging = false; dragging = false;
m_value = m_old_value;
set_value(glm::length(m_value));
if (on_value_changed)
on_value_changed(this, glm::length(m_value));
break; break;
default: default:
return kEventResult::Available; return kEventResult::Available;