PolycodePlayerView.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "PolycodePlayerView.h"
  2. #include "resource.h"
  3. #include <Commdlg.h>
  4. using namespace Polycode;
  5. Win32Core *core = NULL;
  6. HWND consoleHwnd;
  7. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  8. {
  9. int wmId, wmEvent;
  10. PAINTSTRUCT ps;
  11. HDC hdc;
  12. int nWidth, nHeight;
  13. bool useDefault = false;
  14. if(!core)
  15. return DefWindowProc(hWnd, message, wParam, lParam);
  16. switch (message)
  17. {
  18. case WM_SIZE:
  19. nWidth = LOWORD(lParam);
  20. nHeight = HIWORD(lParam);
  21. if(core)
  22. core->getServices()->getRenderer()->Resize(nWidth, nHeight);
  23. break;
  24. case WM_MOUSEMOVE:
  25. if(core)
  26. core->handleMouseMove(lParam,wParam);
  27. break;
  28. case WM_MOUSEWHEEL:
  29. if(core)
  30. core->handleMouseWheel(lParam,wParam);
  31. break;
  32. case WM_LBUTTONDOWN:
  33. if(core)
  34. core->handleMouseDown(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  35. break;
  36. case WM_LBUTTONUP:
  37. if(core)
  38. core->handleMouseUp(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  39. break;
  40. case WM_RBUTTONDOWN:
  41. if(core)
  42. core->handleMouseDown(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  43. break;
  44. case WM_RBUTTONUP:
  45. if(core)
  46. core->handleMouseUp(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  47. break;
  48. case WM_TOUCH:
  49. if(core) {
  50. if(core->isMultiTouchEnabled()) {
  51. core->handleTouchEvent(lParam, wParam);
  52. }
  53. }
  54. break;
  55. case WM_MBUTTONDOWN:
  56. if(core)
  57. core->handleMouseDown(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  58. break;
  59. case WM_MBUTTONUP:
  60. if(core)
  61. core->handleMouseUp(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  62. break;
  63. case WM_KEYDOWN:
  64. if(core) {
  65. wchar_t unicodeChar = 0;
  66. MSG m;
  67. m.hwnd = hWnd;
  68. m.message = message;
  69. m.wParam = wParam;
  70. m.lParam = lParam;
  71. m.time = 0;
  72. if ( PeekMessage(&m, hWnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
  73. GetMessage(&m, hWnd, 0, WM_USER);
  74. unicodeChar = (wchar_t)m.wParam;
  75. }
  76. core->handleKeyDown(lParam,wParam, unicodeChar);
  77. }
  78. break;
  79. case WM_KEYUP:
  80. if(core)
  81. core->handleKeyUp(lParam,wParam);
  82. break;
  83. case WM_CLOSE:
  84. if(core)
  85. core->Shutdown();
  86. useDefault = true;
  87. break;
  88. case WM_DESTROY:
  89. PostQuitMessage(0);
  90. break;
  91. case WM_COMMAND:
  92. switch(LOWORD(wParam))
  93. {
  94. case ID_FILE_EXIT:
  95. if(core)
  96. core->Shutdown();
  97. break;
  98. case ID_FILE_OPEN:
  99. {
  100. TCHAR szFile[2048];
  101. OPENFILENAME ofn;
  102. ZeroMemory(&ofn, sizeof(ofn));
  103. ofn.lStructSize = sizeof(ofn);
  104. ofn.hwndOwner = hWnd;
  105. ofn.lpstrFile = szFile;
  106. ofn.lpstrFile[0] = '\0';
  107. ofn.nMaxFile = sizeof(szFile);
  108. ofn.lpstrFilter = L"Polycode Application (*.polyapp)\0*.POLYAPP\0";
  109. ofn.nFilterIndex = 0;
  110. ofn.lpstrFileTitle = NULL;
  111. ofn.nMaxFileTitle = 0;
  112. ofn.lpstrInitialDir = NULL;
  113. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  114. if (GetOpenFileName(&ofn)==TRUE) {
  115. TCHAR tpath[2049];
  116. GetModuleFileName(NULL, (LPWSTR)tpath, 2048);
  117. STARTUPINFO si;
  118. PROCESS_INFORMATION pi;
  119. ZeroMemory(&si, sizeof(si));
  120. String fullLine = String(tpath);
  121. CreateProcess(fullLine.getWDataWithEncoding(String::ENCODING_UTF8), szFile, NULL, NULL, false, 0, 0, NULL, &si, &pi);
  122. }
  123. }
  124. break;
  125. case ID_SHOW_CONSOLE:
  126. ShowWindow(consoleHwnd, SW_SHOW);
  127. UpdateWindow(consoleHwnd);
  128. break;
  129. }
  130. break;
  131. default:
  132. useDefault = true;
  133. break;
  134. }
  135. if (useDefault)
  136. return DefWindowProc(hWnd, message, wParam, lParam);
  137. else
  138. return 0;
  139. }
  140. LRESULT CALLBACK ConsoleWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  141. {
  142. int wmId, wmEvent;
  143. PAINTSTRUCT ps;
  144. HDC hdc;
  145. int nWidth, nHeight;
  146. switch (message)
  147. {
  148. case WM_CLOSE:
  149. ShowWindow(consoleHwnd, SW_HIDE);
  150. UpdateWindow(consoleHwnd);
  151. break;
  152. default:
  153. return DefWindowProc(hWnd, message, wParam, lParam);
  154. }
  155. return 0;
  156. }
  157. void AppendTextToEditCtrl(HWND hWndEdit, LPCTSTR pszText)
  158. {
  159. int nLength = Edit_GetTextLength(hWndEdit);
  160. Edit_SetSel(hWndEdit, nLength, nLength);
  161. Edit_ReplaceSel(hWndEdit, pszText);
  162. }
  163. void PolycodePlayerView::handleEvent(Event *event){
  164. PolycodeDebugEvent *debugEvent = (PolycodeDebugEvent*) event;
  165. switch(event->getEventCode()) {
  166. case PolycodeDebugEvent::EVENT_ERROR:
  167. {
  168. String lineString = String("Error on line ");
  169. lineString = lineString + String::NumberToString(debugEvent->lineNumber);
  170. lineString = lineString + " - ";
  171. AppendTextToEditCtrl(consoleTextArea, lineString.getWDataWithEncoding(String::ENCODING_UTF8));
  172. AppendTextToEditCtrl(consoleTextArea, debugEvent->errorString.getWDataWithEncoding(String::ENCODING_UTF8));
  173. ShowWindow(consoleHwnd, SW_SHOW);
  174. UpdateWindow(consoleHwnd);
  175. }
  176. break;
  177. case PolycodeDebugEvent::EVENT_PRINT:
  178. AppendTextToEditCtrl(consoleTextArea, debugEvent->errorString.getWDataWithEncoding(String::ENCODING_UTF8));
  179. ShowWindow(consoleHwnd, SW_SHOW);
  180. UpdateWindow(consoleHwnd);
  181. break;
  182. }
  183. }
  184. PolycodePlayerView::PolycodePlayerView(bool standaloneMode, HINSTANCE hInstance, int nCmdShow, LPCTSTR windowTitle) : PolycodeViewBase(), EventHandler() {
  185. WNDCLASSEX wcex;
  186. wcex.cbSize = sizeof(WNDCLASSEX);
  187. wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  188. wcex.lpfnWndProc = WndProc;
  189. wcex.cbClsExtra = 0;
  190. wcex.cbWndExtra = 0;
  191. wcex.hInstance = hInstance;
  192. wcex.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  193. //wcex.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
  194. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  195. wcex.hbrBackground = NULL;
  196. if(standaloneMode) {
  197. wcex.lpszMenuName = NULL;
  198. } else {
  199. wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);;
  200. }
  201. wcex.lpszClassName = L"POLYCODEAPPLICATION";
  202. wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
  203. RegisterClassEx(&wcex);
  204. hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"POLYCODEAPPLICATION", windowTitle, WS_OVERLAPPED|WS_SYSMENU,
  205. 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
  206. windowData = (void*)&hwnd;
  207. ShowWindow(hwnd, nCmdShow);
  208. UpdateWindow(hwnd);
  209. WNDCLASSEX console_wcex;
  210. console_wcex.cbSize = sizeof(WNDCLASSEX);
  211. console_wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  212. console_wcex.lpfnWndProc = ConsoleWindowProc;
  213. console_wcex.cbClsExtra = 0;
  214. console_wcex.cbWndExtra = 0;
  215. console_wcex.hInstance = hInstance;
  216. console_wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  217. console_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  218. console_wcex.hbrBackground = NULL;
  219. console_wcex.lpszMenuName = NULL;
  220. console_wcex.lpszClassName = L"CONSOLEWINDOW";
  221. console_wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
  222. RegisterClassEx(&console_wcex);
  223. consoleHwnd = CreateWindowEx(WS_EX_TOOLWINDOW, L"CONSOLEWINDOW", L"Debug console.", WS_OVERLAPPED|WS_SYSMENU,
  224. 640, 100, 500, 300, hwnd, NULL, hInstance, NULL);
  225. //ShowWindow(consoleHwnd, SW_SHOW);
  226. UpdateWindow(consoleHwnd);
  227. consoleTextArea = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"", WS_CHILD | WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_READONLY,
  228. 0, 0, 500, 275, consoleHwnd, NULL, hInstance, NULL);
  229. ShowWindow(consoleTextArea, SW_SHOW);
  230. UpdateWindow(consoleTextArea);
  231. HFONT defaultFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  232. SendMessage (consoleTextArea, WM_SETFONT, WPARAM (defaultFont), TRUE);
  233. }
  234. PolycodePlayerView::~PolycodePlayerView() {
  235. }