diff --git a/Core/Contents/Include/PolyCore.h b/Core/Contents/Include/PolyCore.h index 4b1f1f4a6..eb4d6d244 100755 --- a/Core/Contents/Include/PolyCore.h +++ b/Core/Contents/Include/PolyCore.h @@ -404,7 +404,9 @@ namespace Polycode { String defaultWorkingDirectory; void *userPointer; - + + //used for pausing workaround.. + int frameRate; long refreshInterval; unsigned int timeSleptMs; diff --git a/Core/Contents/Include/PolyWinCore.h b/Core/Contents/Include/PolyWinCore.h index 5a0d84723..53ece0f66 100644 --- a/Core/Contents/Include/PolyWinCore.h +++ b/Core/Contents/Include/PolyWinCore.h @@ -228,6 +228,12 @@ class Gamepad_devicePrivate { void initTouch(); void handleViewResize(int width, int height); + + /** + * Handles focus changes + * @param newFocus bool value: true if gained focus, false if lost focus + */ + void handleFocusChange(bool newFocus); String executeExternalCommand(String command, String args, String inDirectory); std::vector openFilePicker(std::vector extensions, bool allowMultiple); diff --git a/Core/Contents/PolycodeView/MSVC/PolycodeView.cpp b/Core/Contents/PolycodeView/MSVC/PolycodeView.cpp index 2de1ca730..a6395e062 100644 --- a/Core/Contents/PolycodeView/MSVC/PolycodeView.cpp +++ b/Core/Contents/PolycodeView/MSVC/PolycodeView.cpp @@ -146,6 +146,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_DESTROY: PostQuitMessage(0); break; + case WM_KILLFOCUS: + core->handleFocusChange(false); + break; + case WM_SETFOCUS: + core->handleFocusChange(true); + break; default: useDefault = true; break; diff --git a/Core/Contents/Source/PolyCocoaCore.mm b/Core/Contents/Source/PolyCocoaCore.mm index f29b5f9af..b1a05db9c 100644 --- a/Core/Contents/Source/PolyCocoaCore.mm +++ b/Core/Contents/Source/PolyCocoaCore.mm @@ -426,16 +426,17 @@ long getThreadID() { } void CocoaCore::warpCursor(int x, int y) { - - CGSetLocalEventsSuppressionInterval(0); - NSArray *theScreens = [NSScreen screens]; - for (NSScreen *theScreen in theScreens) { - CGPoint CenterOfWindow = CGPointMake([glView window].frame.origin.x+x, (-1)*([glView window].frame.origin.y-theScreen.frame.size.height)-yRes+y); - CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, CenterOfWindow); - break; + if(!paused){ + CGSetLocalEventsSuppressionInterval(0); + NSArray *theScreens = [NSScreen screens]; + for (NSScreen *theScreen in theScreens) { + CGPoint CenterOfWindow = CGPointMake([glView window].frame.origin.x+x, (-1)*([glView window].frame.origin.y-theScreen.frame.size.height)-yRes+y); + CGDisplayMoveCursorToPoint (kCGDirectMainDisplay, CenterOfWindow); + break; + } + lastMouseX = x; + lastMouseY = y; } - lastMouseX = x; - lastMouseY = y; } @@ -659,13 +660,14 @@ long getThreadID() { if(!running) return false; doSleep(); - - if(modeChangeInfo.needResolutionChange) { - _setVideoMode(modeChangeInfo.xRes, modeChangeInfo.yRes, modeChangeInfo.fullScreen, modeChangeInfo.vSync, modeChangeInfo.aaLevel, modeChangeInfo.anisotropyLevel); - modeChangeInfo.needResolutionChange = false; - } + if(!paused){ + if(modeChangeInfo.needResolutionChange) { + _setVideoMode(modeChangeInfo.xRes, modeChangeInfo.yRes, modeChangeInfo.fullScreen, modeChangeInfo.vSync, modeChangeInfo.aaLevel, modeChangeInfo.anisotropyLevel); + modeChangeInfo.needResolutionChange = false; + } - updateCore(); + updateCore(); + } checkEvents(); return running; } diff --git a/Core/Contents/Source/PolyCore.cpp b/Core/Contents/Source/PolyCore.cpp index f6f18d564..39b71091d 100755 --- a/Core/Contents/Source/PolyCore.cpp +++ b/Core/Contents/Source/PolyCore.cpp @@ -82,6 +82,7 @@ namespace Polycode { frameRate = 60; setFramerate(frameRate); + refreshInterval = 1000 / frameRate; threadedEventMutex = NULL; } @@ -98,6 +99,7 @@ namespace Polycode { } void Core::setFramerate(int frameRate, int maxFixedCycles) { + this->frameRate = frameRate; refreshInterval = 1000 / frameRate; fixedTimestep = 1.0 / ((double) frameRate); maxFixedElapsed = fixedTimestep * maxFixedCycles; @@ -168,6 +170,7 @@ namespace Polycode { void Core::loseFocus() { if(pauseOnLoseFocus) { paused = true; + refreshInterval = 1000 / 2; } input->clearInput(); dispatchEvent(new Event(), EVENT_LOST_FOCUS); @@ -177,7 +180,8 @@ namespace Polycode { if(pauseOnLoseFocus) { paused = false; } - input->clearInput(); + input->clearInput(); + refreshInterval = 1000 / frameRate; dispatchEvent(new Event(), EVENT_GAINED_FOCUS); } @@ -197,7 +201,11 @@ namespace Polycode { bool Core::updateAndRender() { bool ret = Update(); - Render(); + + if (!paused){ + Render(); + } + return ret; } diff --git a/Core/Contents/Source/PolySDLCore.cpp b/Core/Contents/Source/PolySDLCore.cpp index 831ef3e8f..b6a13e1d8 100644 --- a/Core/Contents/Source/PolySDLCore.cpp +++ b/Core/Contents/Source/PolySDLCore.cpp @@ -318,8 +318,10 @@ bool SDLCore::systemUpdate() { return false; doSleep(); - updateCore(); - + if(!paused) { + updateCore(); + } + SDL_Event event; while ( SDL_PollEvent(&event) ) { switch (event.type) { @@ -417,7 +419,8 @@ void SDLCore::setCursor(int cursorType) { } void SDLCore::warpCursor(int x, int y) { - SDL_WarpMouse(x, y); + if(!paused) + SDL_WarpMouse(x, y); } void SDLCore::lockMutex(CoreMutex *mutex) { diff --git a/Core/Contents/Source/PolyWinCore.cpp b/Core/Contents/Source/PolyWinCore.cpp index 85713dda0..9a64c3baf 100644 --- a/Core/Contents/Source/PolyWinCore.cpp +++ b/Core/Contents/Source/PolyWinCore.cpp @@ -188,13 +188,15 @@ void Win32Core::captureMouse(bool newval) { } void Win32Core::warpCursor(int x, int y) { - POINT point; - point.x = x; - point.y = y; - ClientToScreen(hWnd, &point); - SetCursorPos(point.x,point.y); - lastMouseX = x; - lastMouseY = y; + if(!paused){ + POINT point; + point.x = x; + point.y = y; + ClientToScreen(hWnd, &point); + SetCursorPos(point.x,point.y); + lastMouseX = x; + lastMouseY = y; + } } unsigned int Win32Core::getTicks() { @@ -213,11 +215,13 @@ void Win32Core::Render() { bool Win32Core::systemUpdate() { if(!running) return false; - captureMouse(Core::mouseCaptured); - doSleep(); - checkEvents(); - Gamepad_processEvents(); - updateCore(); + doSleep(); + if(!paused){ + checkEvents(); + Gamepad_processEvents(); + updateCore(); + captureMouse(Core::mouseCaptured); + } return running; } @@ -731,6 +735,25 @@ bool Win32Core::checkSpecialKeyEvents(PolyKEY key) { void Win32Core::checkEvents() { lockMutex(eventMutex); + + if ((GetKeyState(VK_LBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON1)){ + input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, true, getTicks()); + } else if ((GetKeyState(VK_LBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON1)) { + input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, false, getTicks()); + } + + if ((GetKeyState(VK_RBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON2)){ + input->setMouseButtonState(CoreInput::MOUSE_BUTTON2, true, getTicks()); + } else if ((GetKeyState(VK_RBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON2)){ + input->setMouseButtonState(CoreInput::MOUSE_BUTTON2, false, getTicks()); + } + + if ((GetKeyState(VK_MBUTTON) & 0x80) != 0 && !input->getMouseButtonState(CoreInput::MOUSE_BUTTON3)){ + input->setMouseButtonState(CoreInput::MOUSE_BUTTON3, true, getTicks()); + } else if ((GetKeyState(VK_MBUTTON) & 0x80) == 0 && input->getMouseButtonState(CoreInput::MOUSE_BUTTON3)) { + input->setMouseButtonState(CoreInput::MOUSE_BUTTON3, false, getTicks()); + } + Win32Event event; for(int i=0; i < win32Events.size(); i++) { event = win32Events[i]; @@ -752,12 +775,6 @@ void Win32Core::checkEvents() { lastMouseY = event.mouseY; input->setMousePosition(event.mouseX, event.mouseY, getTicks()); break; - case InputEvent::EVENT_MOUSEDOWN: - input->setMouseButtonState(event.mouseButton, true, getTicks()); - break; - case InputEvent::EVENT_MOUSEUP: - input->setMouseButtonState(event.mouseButton, false, getTicks()); - break; case InputEvent::EVENT_MOUSEWHEEL_UP: input->mouseWheelUp(getTicks()); break; @@ -1411,3 +1428,12 @@ String Win32Core::getClipboardString() { GlobalUnlock(clip0); return retString; } + +void Win32Core::handleFocusChange(bool newFocus){ + if (newFocus){ + gainFocus(); + } + else { + loseFocus(); + } +} diff --git a/IDE/Build/WindowsShared/PolycodeWinIDEView.cpp b/IDE/Build/WindowsShared/PolycodeWinIDEView.cpp index 420a3b4f0..dda05e728 100644 --- a/IDE/Build/WindowsShared/PolycodeWinIDEView.cpp +++ b/IDE/Build/WindowsShared/PolycodeWinIDEView.cpp @@ -254,6 +254,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_DESTROY: PostQuitMessage(0); break; + case WM_KILLFOCUS: + core->handleFocusChange(false); + break; + case WM_SETFOCUS: + core->handleFocusChange(true); + break; default: useDefault = true; break; diff --git a/IDE/Contents/Source/PolycodeIDEApp.cpp b/IDE/Contents/Source/PolycodeIDEApp.cpp index 90e9a5edf..1d9e1d0c8 100644 --- a/IDE/Contents/Source/PolycodeIDEApp.cpp +++ b/IDE/Contents/Source/PolycodeIDEApp.cpp @@ -39,7 +39,7 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() { core = new POLYCODE_CORE((PolycodeView*)view, 1100, 700,false,false, 0, 0,60, -1, true); #endif -// core->pauseOnLoseFocus = true; + core->pauseOnLoseFocus = true; printf("DIR: %s\n", core->getDefaultWorkingDirectory().c_str()); @@ -55,8 +55,6 @@ core = new POLYCODE_CORE((PolycodeView*)view, 1100, 700,false,false, 0, 0,60, -1 runNextFrame = false; core->addEventListener(this, Core::EVENT_CORE_RESIZE); - core->addEventListener(this, Core::EVENT_LOST_FOCUS); - core->addEventListener(this, Core::EVENT_GAINED_FOCUS); globalClipboard = new PolycodeClipboard(); @@ -483,6 +481,8 @@ void PolycodeIDEApp::doRunProject() { frame->showConsole(); + CoreServices::getInstance()->getCore()->pauseOnLoseFocus = false; + String outPath = PolycodeToolLauncher::generateTempPath(projectManager->getActiveProject()) + ".polyapp"; PolycodeToolLauncher::buildProject(projectManager->getActiveProject(), outPath, false); PolycodeToolLauncher::runPolyapp(outPath); @@ -828,12 +828,6 @@ void PolycodeIDEApp::handleEvent(Event *event) { if(event->getDispatcher() == core) { switch(event->getEventCode()) { - case Core::EVENT_LOST_FOCUS: - core->setFramerate(3); - break; - case Core::EVENT_GAINED_FOCUS: - core->setFramerate(60); - break; case Core::EVENT_CORE_RESIZE: if(menuBar) { frame->Resize(core->getXRes(), core->getYRes()-25); @@ -1368,51 +1362,51 @@ PolycodeIDEApp::~PolycodeIDEApp() { bool PolycodeIDEApp::Update() { + if(!core->paused){ + if(willRunProject) { + willRunProject = false; + runProject(); + } - if(willRunProject) { - willRunProject = false; - runProject(); - } - - if(runNextFrame) { - runNextFrame = false; - doRunProject(); - } + if(runNextFrame) { + runNextFrame = false; + doRunProject(); + } - if(lastConnected != debugger->isConnected()) { - needsRedraw = true; - lastConnected = debugger->isConnected(); - } + if(lastConnected != debugger->isConnected()) { + needsRedraw = true; + lastConnected = debugger->isConnected(); + } - if(debugger->isConnected()) { - frame->stopButton->visible = true; - frame->stopButton->enabled = true; + if(debugger->isConnected()) { + frame->stopButton->visible = true; + frame->stopButton->enabled = true; - frame->playButton->visible = false; - frame->playButton->enabled = false; + frame->playButton->visible = false; + frame->playButton->enabled = false; - } else { - frame->stopButton->visible = false; - frame->stopButton->enabled = false; + } else { + frame->stopButton->visible = false; + frame->stopButton->enabled = false; - frame->playButton->visible = true; - frame->playButton->enabled = true; - } + frame->playButton->visible = true; + frame->playButton->enabled = true; + } - if(projectManager->getProjectCount() == 1) { - projectManager->setActiveProject(projectManager->getProjectByIndex(0)); - } + if(projectManager->getProjectCount() == 1) { + projectManager->setActiveProject(projectManager->getProjectByIndex(0)); + } - if(projectManager->getProjectCount() > 0) { - frame->welcomeEntity->enabled = false; + if(projectManager->getProjectCount() > 0) { + frame->welcomeEntity->enabled = false; - frame->getConsoleSizer()->enabled = true; - } else { - frame->welcomeEntity->enabled = true; - frame->getConsoleSizer()->enabled = false; - } - + frame->getConsoleSizer()->enabled = true; + } else { + frame->welcomeEntity->enabled = true; + frame->getConsoleSizer()->enabled = false; + } + } return core->updateAndRender(); } diff --git a/IDE/Contents/Source/PolycodeRemoteDebugger.cpp b/IDE/Contents/Source/PolycodeRemoteDebugger.cpp index 46602ff35..806da2291 100644 --- a/IDE/Contents/Source/PolycodeRemoteDebugger.cpp +++ b/IDE/Contents/Source/PolycodeRemoteDebugger.cpp @@ -52,6 +52,7 @@ void PolycodeRemoteDebugger::Disconnect() { for(int i=0; i < debuggerClients.size(); i++) { server->DisconnectClient(debuggerClients[i]->client); } + CoreServices::getInstance()->getCore()->pauseOnLoseFocus = true; debuggerClients.clear(); } @@ -124,16 +125,20 @@ void PolycodeRemoteDebugger::handleEvent(Event *event) { PolycodeConsole::print("Remote debugger client disconnected...\n"); } } + if (debuggerClients.size() == 0){ + CoreServices::getInstance()->getCore()->pauseOnLoseFocus = true; + } } break; case ServerEvent::EVENT_CLIENT_CONNECTED: { + CoreServices::getInstance()->getCore()->pauseOnLoseFocus = false; DebuggerClient *newClient = new DebuggerClient(); newClient->client = serverEvent->client; PolycodeConsole::print("Remote debugger client connected...\n"); printf("CLIENT CONNECTED\n"); - debuggerClients.push_back(newClient); + debuggerClients.push_back(newClient); } break; diff --git a/Player/Build/MSVC/PolycodePlayer/PolycodePlayerView.cpp b/Player/Build/MSVC/PolycodePlayer/PolycodePlayerView.cpp index cb5a806f9..99441a917 100644 --- a/Player/Build/MSVC/PolycodePlayer/PolycodePlayerView.cpp +++ b/Player/Build/MSVC/PolycodePlayer/PolycodePlayerView.cpp @@ -122,6 +122,12 @@ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; break; } break; + case WM_KILLFOCUS: + core->handleFocusChange(false); + break; + case WM_SETFOCUS: + core->handleFocusChange(true); + break; default: return DefWindowProc(hWnd, message, wParam, lParam); } diff --git a/Player/Contents/Source/PolycodePlayerView.cpp b/Player/Contents/Source/PolycodePlayerView.cpp index 0d0066930..c5a95ffab 100644 --- a/Player/Contents/Source/PolycodePlayerView.cpp +++ b/Player/Contents/Source/PolycodePlayerView.cpp @@ -146,6 +146,12 @@ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; break; } break; + case WM_KILLFOCUS: + core->handleFocusChange(false); + break; + case WM_SETFOCUS: + core->handleFocusChange(true); + break; default: useDefault = true; break;