axmol/templates/lua-template-runtime/frameworks/runtime-src/proj.win32/SimulatorWindow.cpp

353 lines
9.9 KiB
C++
Raw Normal View History

2014-03-10 19:33:57 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "SimulatorWindow.h"
#include "cocos2d.h"
#include "glfw3native.h"
#include "resource.h"
2014-05-30 10:50:24 +08:00
#include "runtime/Runtime.h"
2014-05-05 21:04:04 +08:00
#include "ConfigParser.h"
2014-03-10 19:33:57 +08:00
#include <string>
#include <vector>
using namespace std;
using namespace cocos2d;
WNDPROC g_oldProc=NULL;
bool g_landscape=false;
CCSize g_screenSize;
GLView* g_eglView=NULL;
INT_PTR CALLBACK AboutDialogCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
void createViewMenu()
{
2014-05-06 17:03:40 +08:00
HMENU hSysMenu = GetSystemMenu(glfwGetWin32Window(g_eglView->getWindow()), FALSE);
HMENU viewMenu = GetSubMenu(hSysMenu, 8);
2014-05-05 21:04:04 +08:00
for (int i = ConfigParser::getInstance()->getScreenSizeCount() - 1; i >= 0; --i)
2014-04-10 15:51:25 +08:00
{
2014-05-05 21:04:04 +08:00
SimulatorScreenSize size = ConfigParser::getInstance()->getScreenSize(i);
2014-04-10 15:51:25 +08:00
wstring menuName;
menuName.assign(size.title.begin(), size.title.end());
MENUITEMINFO item;
ZeroMemory(&item, sizeof(item));
item.cbSize = sizeof(item);
item.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
item.fType = MFT_STRING;
item.wID = ID_VIEW_SIZE + i;
item.dwTypeData = (LPTSTR)menuName.c_str();
item.cch = menuName.length();
InsertMenuItem(viewMenu, 0, TRUE, &item);
}
2014-03-10 19:33:57 +08:00
}
void updateMenu()
{
2014-05-06 17:03:40 +08:00
HMENU hSysMenu = GetSystemMenu(glfwGetWin32Window(g_eglView->getWindow()), FALSE);
HMENU viewMenu = GetSubMenu(hSysMenu, 8);
2014-04-10 15:51:25 +08:00
if (g_landscape)
{
CheckMenuItem(viewMenu, ID_VIEW_PORTRAIT, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem(viewMenu, ID_VIEW_LANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
}
else
{
CheckMenuItem(viewMenu, ID_VIEW_PORTRAIT, MF_BYCOMMAND | MF_CHECKED);
CheckMenuItem(viewMenu, ID_VIEW_LANDSCAPE, MF_BYCOMMAND | MF_UNCHECKED);
}
int width = g_screenSize.width;
int height = g_screenSize.height;
if (height > width)
{
int w = width;
width = height;
height = w;
}
2014-05-05 21:04:04 +08:00
int count = ConfigParser::getInstance()->getScreenSizeCount();
2014-04-10 15:51:25 +08:00
for (int i = 0; i < count; ++i)
{
bool bSel = false;
2014-05-05 21:04:04 +08:00
SimulatorScreenSize size = ConfigParser::getInstance()->getScreenSize(i);
2014-04-10 15:51:25 +08:00
if (size.width == width && size.height == height)
{
bSel = true;
}
CheckMenuItem(viewMenu, i, MF_BYPOSITION | (bSel? MF_CHECKED : MF_UNCHECKED));
}
int scale=g_eglView->getFrameZoomFactor()*100;
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT100, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT75, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT50, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT25, MF_BYCOMMAND | MF_UNCHECKED);
switch (scale)
{
case 100:
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT100, MF_BYCOMMAND | MF_CHECKED);
break;
case 75:
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT75, MF_BYCOMMAND | MF_CHECKED);
break;
case 50:
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT50, MF_BYCOMMAND | MF_CHECKED);
break;
case 25:
CheckMenuItem(viewMenu, ID_VIEW_ZOOMOUT25, MF_BYCOMMAND | MF_CHECKED);
break;
default:
break;
}
2014-03-10 19:33:57 +08:00
}
/*@brief updateView*/
void updateView()
{
2014-03-11 16:52:06 +08:00
2014-04-10 15:51:25 +08:00
auto policy = g_eglView->getResolutionPolicy();
auto designSize = g_eglView->getDesignResolutionSize();
2014-03-11 16:52:06 +08:00
2014-04-10 15:51:25 +08:00
if (g_landscape)
{
g_eglView->setFrameSize(g_screenSize.width, g_screenSize.height);
}
else
{
g_eglView->setFrameSize(g_screenSize.height, g_screenSize.width);
}
2014-03-11 16:52:06 +08:00
2014-04-10 15:51:25 +08:00
g_eglView->setDesignResolutionSize(designSize.width, designSize.height, policy);
2014-03-11 16:52:06 +08:00
2014-04-10 15:51:25 +08:00
updateMenu();
2014-03-10 19:33:57 +08:00
}
void onViewChangeOrientation(int viewMenuID)
{
2014-04-10 15:51:25 +08:00
if (viewMenuID == ID_VIEW_PORTRAIT && g_landscape)
{
g_landscape = false;
updateView();
}
else if (viewMenuID == ID_VIEW_LANDSCAPE && !g_landscape)
{
g_landscape = true;
updateView();
}
2014-03-10 19:33:57 +08:00
}
2014-03-11 16:52:06 +08:00
void onViewZoomOut(int viewMenuID)
{
2014-04-10 15:51:25 +08:00
float scale = 1.0;
switch (viewMenuID)
{
case ID_VIEW_ZOOMOUT100:
scale=1.0;
break;
case ID_VIEW_ZOOMOUT75:
scale=0.75;
break;
case ID_VIEW_ZOOMOUT50:
scale=0.50;
break;
case ID_VIEW_ZOOMOUT25:
scale=0.25;
break;
default:
break;
}
g_eglView->setFrameZoomFactor(scale);
updateView();
2014-03-11 16:52:06 +08:00
}
2014-03-10 19:33:57 +08:00
void onViewChangeFrameSize(int viewMenuID)
{
2014-04-10 15:51:25 +08:00
int index = viewMenuID - ID_VIEW_SIZE;
2014-05-05 21:04:04 +08:00
if (index >= 0 && index < ConfigParser::getInstance()->getScreenSizeCount())
2014-04-10 15:51:25 +08:00
{
2014-05-05 21:04:04 +08:00
SimulatorScreenSize size = ConfigParser::getInstance()->getScreenSize(index);
2014-04-10 15:51:25 +08:00
g_screenSize.width = size.width;
g_screenSize.height = size.height;
updateView();
}
2014-03-10 19:33:57 +08:00
}
void onHelpAbout()
{
2014-04-10 15:51:25 +08:00
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG_ABOUT), glfwGetWin32Window(g_eglView->getWindow()), AboutDialogCallback);
2014-03-10 19:33:57 +08:00
}
2014-03-17 20:46:49 +08:00
void shutDownApp()
{
2014-04-10 15:51:25 +08:00
HWND hWnd=glfwGetWin32Window(g_eglView->getWindow());
::SendMessage(hWnd,WM_CLOSE,NULL,NULL);
2014-03-17 20:46:49 +08:00
}
2014-05-06 17:03:40 +08:00
void reStart()
{
PROCESS_INFORMATION info;
STARTUPINFO startup;
TCHAR szPath[128]={0};
TCHAR *szCmdLine=NULL;
GetModuleFileName(NULL, szPath, sizeof(szPath));
szCmdLine = GetCommandLine();
GetStartupInfo(&startup);
BOOL bSucc = CreateProcess(szPath, szCmdLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startup, &info);
if(bSucc)
{
ExitProcess(-1);
}
}
2014-03-10 19:33:57 +08:00
/*@brief new windows process*/
LRESULT CALLBACK SNewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
2014-04-10 15:51:25 +08:00
int wmId, wmEvent;
switch (message)
{
2014-05-06 17:03:40 +08:00
case WM_KEYDOWN:
if (wParam == VK_F5)
{
reStart();
break;
}
case WM_SYSCOMMAND:
2014-04-10 15:51:25 +08:00
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_FILE_EXIT:
shutDownApp();
break;
case ID_VIEW_PORTRAIT:
case ID_VIEW_LANDSCAPE:
onViewChangeOrientation(wmId);
break;
case ID_VIEW_ZOOMOUT100:
case ID_VIEW_ZOOMOUT75:
case ID_VIEW_ZOOMOUT50:
case ID_VIEW_ZOOMOUT25:
onViewZoomOut(wmId);
break;
case ID_CONTROL_RELOAD:
2014-05-06 17:03:40 +08:00
reStart();
2014-04-10 15:51:25 +08:00
break;
case ID_HELP_ABOUT:
onHelpAbout();
2014-05-06 17:03:40 +08:00
break;
2014-04-10 15:51:25 +08:00
default:
2014-05-05 21:04:04 +08:00
if (wmId >= ID_VIEW_SIZE && wmId <= ID_VIEW_SIZE + ConfigParser::getInstance()->getScreenSizeCount() - 1)
2014-04-10 15:51:25 +08:00
{
onViewChangeFrameSize(wmId);
break;
}
2014-05-06 17:03:40 +08:00
//return 0;
2014-04-10 15:51:25 +08:00
}
}
break;
}
return g_oldProc(hWnd, message, wParam, lParam);
2014-03-10 19:33:57 +08:00
}
/*@brief AboutDialog Callback*/
INT_PTR CALLBACK AboutDialogCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
2014-04-10 15:51:25 +08:00
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
2014-03-10 19:33:57 +08:00
}
2014-05-07 20:50:29 +08:00
void createSimulator(const char* viewName, float width, float height, bool isLandscape, float frameZoomFactor)
2014-03-10 19:33:57 +08:00
{
2014-04-10 15:51:25 +08:00
if (g_eglView)
{
return;
}
2014-05-22 11:51:56 +08:00
g_landscape = isLandscape;
if(height > width)
2014-05-07 20:50:29 +08:00
{
float tmpvalue =width;
width = height;
2014-05-17 22:24:56 +08:00
height = tmpvalue;
2014-05-07 20:50:29 +08:00
}
2014-04-10 15:51:25 +08:00
g_screenSize.width = width;
g_screenSize.height = height;
2014-05-22 11:51:56 +08:00
if(!g_landscape)
2014-04-10 15:51:25 +08:00
{
2014-05-22 11:51:56 +08:00
float tmpvalue =width;
width = height;
height = tmpvalue;
2014-04-10 15:51:25 +08:00
}
2014-05-22 11:51:56 +08:00
g_eglView = GLView::createWithRect(viewName,Rect(0,0,width,height),frameZoomFactor);
auto director = Director::getInstance();
director->setOpenGLView(g_eglView);
2014-04-10 15:51:25 +08:00
HWND hWnd=glfwGetWin32Window(g_eglView->getWindow());
HMENU hMenu = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MENU_COCOS));
2014-05-06 17:03:40 +08:00
HMENU hSysMenu = GetSystemMenu(hWnd, FALSE);
HMENU hviewMenu = GetSubMenu(hMenu,1);
HMENU hcontrolMenu = GetSubMenu(hMenu,2);
AppendMenu(hSysMenu,MF_SEPARATOR,0,NULL);
if (hSysMenu != INVALID_HANDLE_VALUE && hMenu != INVALID_HANDLE_VALUE)
{
AppendMenu(hSysMenu, MF_POPUP, (UINT)hviewMenu, TEXT("view"));
AppendMenu(hSysMenu, MF_POPUP, (UINT)hcontrolMenu, TEXT("control"));
}
//SetMenu(hWnd, hMenu);
2014-04-10 15:51:25 +08:00
createViewMenu();
updateMenu();
g_oldProc = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (LONG)SNewWndProc);
if (g_oldProc==0)
{
printf("SetWindowLong NewWndProc Error:%d\n",GetLastError());
}
2014-03-10 19:33:57 +08:00
}