diff --git a/PanoPainter-OSX/main.cpp b/PanoPainter-OSX/main.cpp index 769e59d..4a71cf7 100644 --- a/PanoPainter-OSX/main.cpp +++ b/PanoPainter-OSX/main.cpp @@ -45,6 +45,14 @@ std::mutex task_mutex; { [[NSApplication sharedApplication] terminate:nil]; } +- (void)show_cursor:(bool)visible +{ + if (cursor_visible == visible) + return; + cursor_visible = visible; + if (cursor_inside) + visible ? [NSCursor unhide] : [NSCursor hide]; +} - (std::string)pick_file:(NSArray*)types { NSOpenPanel *panel = [NSOpenPanel openPanel]; @@ -107,6 +115,8 @@ std::mutex task_mutex; airdrop_service = [NSSharingService sharingServiceNamed:NSSharingServiceNameSendViaAirDrop]; airdrop_service.delegate = self; gl_ready = false; + cursor_visible = CGCursorIsVisible(); + cursor_inside = false; NSOpenGLPixelFormatAttribute attrs[] = { NSOpenGLPFADoubleBuffer, @@ -125,7 +135,7 @@ std::mutex task_mutex; [self setPixelFormat:pf]; [self setOpenGLContext:context]; - self->trackingArea = [[NSTrackingArea alloc] initWithRect:frameRect + trackingArea = [[NSTrackingArea alloc] initWithRect:frameRect options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow ) owner:self userInfo:nil]; [self addTrackingArea:trackingArea]; @@ -268,9 +278,9 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime } - (void)updateTrackingAreas { - if(self->trackingArea != nil) - [self removeTrackingArea:self->trackingArea]; - self->trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] + if(trackingArea != nil) + [self removeTrackingArea:trackingArea]; + trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil]; [self addTrackingArea:trackingArea]; @@ -389,14 +399,15 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime } -(void)mouseExited:(NSEvent *)event { - self->cursor_visible = CGCursorIsVisible(); - if (!self->cursor_visible) + if (!cursor_visible) [NSCursor unhide]; + cursor_inside = false; } -(void)mouseEntered:(NSEvent *)event { - if (!self->cursor_visible) + if (!cursor_visible) [NSCursor hide]; + cursor_inside = true; } - (void)scrollWheel:(NSEvent *)theEvent { diff --git a/PanoPainter-OSX/main.h b/PanoPainter-OSX/main.h index 67275c3..fe90c85 100644 --- a/PanoPainter-OSX/main.h +++ b/PanoPainter-OSX/main.h @@ -17,6 +17,7 @@ _CGLContextObject* cgl; bool gl_ready; bool cursor_visible; + bool cursor_inside; NSTrackingArea* trackingArea; @public NSString* file2open; } @@ -29,4 +30,5 @@ - (std::string)pick_dir; - (void)share_file:(NSString*)file_path; - (void)hockeyapp_crash; +- (void)show_cursor:(bool)visible; @end diff --git a/src/app_events.cpp b/src/app_events.cpp index 01c6a18..0fa106d 100644 --- a/src/app_events.cpp +++ b/src/app_events.cpp @@ -43,8 +43,7 @@ void App::show_cursor() #ifdef _WIN32 win32_show_cursor(true); #elif __OSX__ - if (!CGCursorIsVisible()) - [NSCursor unhide]; + [osx_view show_cursor:true]; #endif } @@ -53,8 +52,7 @@ void App::hide_cursor() #ifdef _WIN32 win32_show_cursor(false); #elif __OSX__ - if (CGCursorIsVisible()) - [NSCursor hide]; + [osx_view show_cursor:false]; #endif } diff --git a/src/binary_stream.cpp b/src/binary_stream.cpp index 906a2ef..a2ec39c 100644 --- a/src/binary_stream.cpp +++ b/src/binary_stream.cpp @@ -115,10 +115,13 @@ std::wstring BinaryStreamReader::rwstring(size_t len) // std::swap(ptr[i * 2], ptr[i * 2 + 1]); // right trim trailing zeroes - auto wptr = (uint16_t*)ptr; - for (int i = len - 1; i >= 0; i--) - if (wptr[i] == 0) len--; - + if (len > 0) + { + auto wptr = (uint16_t*)ptr; + for (size_t i = len - 1; i >= 0 && len > 0; i--) + if (wptr[i] == 0) len--; + } + // wide to UTF-16le std::wstring_convert> converter; return converter.from_bytes(ptr, ptr + len * 2); @@ -257,7 +260,7 @@ void BinaryStreamWriter::wdbl(double v) void BinaryStreamWriter::wstring(std::string s) { - wu32(s.size()); + wu32((int)s.size()); write(s.data(), s.size()); } @@ -268,7 +271,7 @@ void BinaryStreamWriter::wstring_raw(std::string s) void BinaryStreamWriter::wwstring(std::wstring s) { - wu32(s.size()); + wu32((int)s.size()); std::wstring_convert> converter; wstring_raw(converter.to_bytes(s)); } @@ -287,13 +290,13 @@ void BinaryStreamWriter::wpascal(std::string s) void BinaryStreamWriter::wraw(std::vector raw) { - wu32(raw.size()); + wu32((int)raw.size()); write(raw.data(), raw.size()); } void BinaryStreamWriter::wrle(std::vector data) { - int idx = 0; + size_t idx = 0; auto ptr = data.data(); // find the next sequence and return the position diff --git a/src/node.cpp b/src/node.cpp index 61a84b9..f9281cf 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -104,11 +104,13 @@ kEventResult Node::on_event(Event* e) MouseEvent e2 = *me; e2.m_type = kEventType::MouseUnfocus; child_mouse_focus->handle_event(&e2); + child_mouse_focus->m_mouse_focus = false; } MouseEvent e2 = *me; e2.m_type = kEventType::MouseFocus; current_mouse_capture->handle_event(&e2); child_mouse_focus = current_mouse_capture; + child_mouse_focus->m_mouse_focus = true; } return current_mouse_capture->on_event(e); } @@ -140,11 +142,13 @@ kEventResult Node::on_event(Event* e) MouseEvent e2 = *me; e2.m_type = kEventType::MouseUnfocus; child_mouse_focus->handle_event(&e2); + child_mouse_focus->m_mouse_focus = false; } MouseEvent e2 = *me; e2.m_type = kEventType::MouseFocus; (*it)->handle_event(&e2); child_mouse_focus = it->get(); + child_mouse_focus->m_mouse_focus = true; } ret = kEventResult::Consumed; break; diff --git a/src/node.h b/src/node.h index 84f4f67..7f314a7 100644 --- a/src/node.h +++ b/src/node.h @@ -110,6 +110,7 @@ public: glm::mat4 m_proj; glm::mat4 m_mvp; + bool m_mouse_focus = false; bool m_mouse_inside = false; bool m_flood_events = false; bool m_force_mouse_capture = false; diff --git a/src/node_canvas.cpp b/src/node_canvas.cpp index d15a009..808dee8 100644 --- a/src/node_canvas.cpp +++ b/src/node_canvas.cpp @@ -466,8 +466,9 @@ kEventResult NodeCanvas::handle_event(Event* e) App::I.show_cursor(); break; case kEventType::MouseFocus: - m_canvas->m_current_mode == kCanvasMode::Draw || - m_canvas->m_current_mode == kCanvasMode::Erase ? + (m_canvas->m_current_mode == kCanvasMode::Draw || + m_canvas->m_current_mode == kCanvasMode::Erase) && + !App::I.keys[(int)kKey::KeyAlt] ? App::I.hide_cursor() : App::I.show_cursor(); break; case kEventType::KeyDown: @@ -476,11 +477,11 @@ kEventResult NodeCanvas::handle_event(Event* e) if (ke->m_key == kKey::AndroidBack) if (!ActionManager::empty()) ActionManager::undo(); - if (ke->m_key == kKey::KeyAlt) + if (ke->m_key == kKey::KeyAlt && m_mouse_focus) App::I.show_cursor(); break; case kEventType::KeyUp: - if (ke->m_key == kKey::KeyAlt) + if (ke->m_key == kKey::KeyAlt && m_mouse_focus) m_canvas->m_current_mode == kCanvasMode::Draw || m_canvas->m_current_mode == kCanvasMode::Erase ? App::I.hide_cursor() : App::I.show_cursor();