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]);
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]);
}
- (void)rightMouseDown:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
- (void)mouseUp:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
- (void)rightMouseUp:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
- (void)mouseMoved:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
-(void)mouseDragged:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
- (void)rightMouseDragged:(NSEvent *)theEvent
{
CGLLockContext([[self openGLContext] CGLContextObj]);
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]);
}
- (void)scrollWheel:(NSEvent *)theEvent

View File

@@ -115,7 +115,8 @@ glm::vec2 t_pos;
CGPoint touchLocation = [touch locationInView:self.view];
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_pos = {touchLocation.x * scale, touchLocation.y * scale};
}
@@ -162,7 +163,8 @@ glm::vec2 t_pos;
}
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};
@@ -172,11 +174,13 @@ glm::vec2 t_pos;
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
float scale = self.view.contentScaleFactor;
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
if (t_count == 2)
App::I.gesture_end();
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_pos = {touchLocation.x * scale, touchLocation.y * scale};
}

View File

@@ -454,7 +454,8 @@
<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-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-cam" width="70" height="100%" margin="0 0 0 5" text="Camera"/>

View File

@@ -139,6 +139,8 @@ void App::update(float dt)
CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0];
layout[main_id]->find<NodeButton>("btn-pick")->set_color(
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)

View File

@@ -80,9 +80,9 @@ public:
void update_memory_usage(size_t bytes);
void update(float dt);
void resize(float w, float h);
bool mouse_down(int button, float x, float y, float pressure);
bool mouse_move(float x, float y, float pressure);
bool mouse_up(int button, float x, float y);
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_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

@@ -28,35 +28,38 @@ void App::hideKeyboard()
#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;
MouseEvent e;
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure;
e.m_source = source;
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)
bool App::mouse_move(float x, float y, float pressure, kEventSource source)
{
redraw = true;
MouseEvent e;
e.m_type = kEventType::MouseMove;
e.m_pos = { x / zoom, y / zoom };
e.m_pressure = pressure;
e.m_source = source;
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)
bool App::mouse_up(int button, float x, float y, kEventSource source)
{
redraw = true;
MouseEvent e;
e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL;
e.m_pos = { x / zoom, y / zoom };
e.m_source = source;
auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update();
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"))
{
button->on_click = [this](Node*) {

View File

@@ -228,7 +228,7 @@ void App::initShaders()
#else
" mediump vec4 bg = texture(tex_bg, uv2);\n"
#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 alpha_tot = bg.a + contribution;"
" mediump vec3 rgb = mix(bg.rgb, fg.rgb, fg.a / alpha_tot);\n"

View File

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

View File

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

View File

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

View File

@@ -98,11 +98,14 @@ kEventResult NodeSliderH::handle_event(Event* e)
break;
case kEventType::MouseCancel:
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;
m_value = m_old_value;
set_value(glm::length(m_value));
if (on_value_changed)
on_value_changed(this, glm::length(m_value));
break;
default:
return kEventResult::Available;