imgui_impl_marmalade.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // ImGui Marmalade binding with IwGx
  2. // In this binding, ImTextureID is used to store a 'CIwTexture*' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
  3. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  4. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  5. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  6. // https://github.com/ocornut/imgui
  7. // Copyright (C) 2015 by Giovanni Zito
  8. // This file is part of ImGui
  9. #include <imgui.h>
  10. #include "imgui_impl_marmalade.h"
  11. #include <s3eClipboard.h>
  12. #include <s3ePointer.h>
  13. #include <s3eKeyboard.h>
  14. #include <IwTexture.h>
  15. #include <IwGx.h>
  16. // Data
  17. static double g_Time = 0.0f;
  18. static bool g_MousePressed[3] = { false, false, false };
  19. static float g_MouseWheel = 0.0f;
  20. static CIwTexture* g_FontTexture = NULL;
  21. static char* g_ClipboardText = NULL;
  22. static bool g_osdKeyboardEnabled = false;
  23. // use this setting to scale the interface - e.g. on device you could use 2 or 3 scale factor
  24. static ImVec2 g_scale = ImVec2(1.0f,1.0f);
  25. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  26. void ImGui_Marmalade_RenderDrawLists(ImDrawData* draw_data)
  27. {
  28. // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
  29. ImGuiIO& io = ImGui::GetIO();
  30. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  31. // Render command lists
  32. for(int n = 0; n < draw_data->CmdListsCount; n++)
  33. {
  34. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  35. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
  36. const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
  37. int nVert = cmd_list->VtxBuffer.size();
  38. CIwFVec2* pVertStream = IW_GX_ALLOC(CIwFVec2, nVert);
  39. CIwFVec2* pUVStream = IW_GX_ALLOC(CIwFVec2, nVert);
  40. CIwColour* pColStream = IW_GX_ALLOC(CIwColour, nVert);
  41. for( int i=0; i < nVert; i++ )
  42. {
  43. // TODO: optimize multiplication on gpu using vertex shader
  44. pVertStream[i].x = cmd_list->VtxBuffer[i].pos.x * g_scale.x;
  45. pVertStream[i].y = cmd_list->VtxBuffer[i].pos.y * g_scale.y;
  46. pUVStream[i].x = cmd_list->VtxBuffer[i].uv.x;
  47. pUVStream[i].y = cmd_list->VtxBuffer[i].uv.y;
  48. pColStream[i] = cmd_list->VtxBuffer[i].col;
  49. }
  50. IwGxSetVertStreamScreenSpace(pVertStream, nVert);
  51. IwGxSetUVStream(pUVStream);
  52. IwGxSetColStream(pColStream, nVert);
  53. IwGxSetNormStream(0);
  54. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  55. {
  56. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  57. if (pcmd->UserCallback)
  58. {
  59. pcmd->UserCallback(cmd_list,pcmd);
  60. }
  61. else
  62. {
  63. CIwMaterial* pCurrentMaterial = IW_GX_ALLOC_MATERIAL();
  64. pCurrentMaterial->SetShadeMode(CIwMaterial::SHADE_FLAT);
  65. pCurrentMaterial->SetCullMode(CIwMaterial::CULL_NONE);
  66. pCurrentMaterial->SetFiltering(false);
  67. pCurrentMaterial->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
  68. pCurrentMaterial->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_NORMAL);
  69. pCurrentMaterial->SetAlphaTestMode(CIwMaterial::ALPHATEST_DISABLED);
  70. pCurrentMaterial->SetTexture((CIwTexture*)pcmd->TextureId);
  71. IwGxSetMaterial(pCurrentMaterial);
  72. IwGxDrawPrims(IW_GX_TRI_LIST, (uint16*)idx_buffer, pcmd->ElemCount);
  73. }
  74. idx_buffer += pcmd->ElemCount;
  75. }
  76. IwGxFlush();
  77. }
  78. // TODO: restore modified state (i.e. mvp matrix)
  79. }
  80. static const char* ImGui_Marmalade_GetClipboardText()
  81. {
  82. if (s3eClipboardAvailable())
  83. {
  84. int size = s3eClipboardGetText(NULL, 0);
  85. if (size > 0)
  86. {
  87. if (g_ClipboardText)
  88. {
  89. delete[] g_ClipboardText;
  90. g_ClipboardText = NULL;
  91. }
  92. g_ClipboardText = new char[size];
  93. g_ClipboardText[0] = '\0';
  94. s3eClipboardGetText(g_ClipboardText, size);
  95. }
  96. }
  97. return g_ClipboardText;
  98. }
  99. static void ImGui_Marmalade_SetClipboardText(const char* text)
  100. {
  101. if (s3eClipboardAvailable())
  102. s3eClipboardSetText(text);
  103. }
  104. int32 ImGui_Marmalade_PointerButtonEventCallback(void* SystemData, void* pUserData)
  105. {
  106. // pEvent->m_Button is of type s3ePointerButton and indicates which mouse
  107. // button was pressed. For touchscreen this should always have the value
  108. // S3E_POINTER_BUTTON_SELECT
  109. s3ePointerEvent* pEvent = (s3ePointerEvent*)SystemData;
  110. if (pEvent->m_Pressed == 1)
  111. {
  112. if (pEvent->m_Button == S3E_POINTER_BUTTON_LEFTMOUSE)
  113. g_MousePressed[0] = true;
  114. if (pEvent->m_Button == S3E_POINTER_BUTTON_RIGHTMOUSE)
  115. g_MousePressed[1] = true;
  116. if (pEvent->m_Button == S3E_POINTER_BUTTON_MIDDLEMOUSE)
  117. g_MousePressed[2] = true;
  118. if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELUP)
  119. g_MouseWheel += pEvent->m_y;
  120. if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELDOWN)
  121. g_MouseWheel += pEvent->m_y;
  122. }
  123. return 0;
  124. }
  125. int32 ImGui_Marmalade_KeyCallback(void* SystemData, void* userData)
  126. {
  127. ImGuiIO& io = ImGui::GetIO();
  128. s3eKeyboardEvent* e = (s3eKeyboardEvent*)SystemData;
  129. if (e->m_Pressed == 1)
  130. io.KeysDown[e->m_Key] = true;
  131. if (e->m_Pressed == 0)
  132. io.KeysDown[e->m_Key] = false;
  133. io.KeyCtrl = s3eKeyboardGetState(s3eKeyLeftControl) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightControl) == S3E_KEY_STATE_DOWN;
  134. io.KeyShift = s3eKeyboardGetState(s3eKeyLeftShift) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightShift) == S3E_KEY_STATE_DOWN;
  135. io.KeyAlt = s3eKeyboardGetState(s3eKeyLeftAlt) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightAlt) == S3E_KEY_STATE_DOWN;
  136. return 0;
  137. }
  138. int32 ImGui_Marmalade_CharCallback(void* SystemData, void* userData)
  139. {
  140. ImGuiIO& io = ImGui::GetIO();
  141. s3eKeyboardCharEvent* e = (s3eKeyboardCharEvent*)SystemData;
  142. if ((e->m_Char > 0 && e->m_Char < 0x10000))
  143. io.AddInputCharacter((unsigned short)e->m_Char);
  144. return 0;
  145. }
  146. bool ImGui_Marmalade_CreateDeviceObjects()
  147. {
  148. // Build texture atlas
  149. ImGuiIO& io = ImGui::GetIO();
  150. unsigned char* pixels;
  151. int width, height;
  152. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  153. // Upload texture to graphics system
  154. g_FontTexture = new CIwTexture();
  155. g_FontTexture->SetModifiable(true);
  156. CIwImage& image = g_FontTexture->GetImage();
  157. image.SetFormat(CIwImage::ARGB_8888);
  158. image.SetWidth(width);
  159. image.SetHeight(height);
  160. image.SetBuffers(); // allocates and own buffers
  161. image.ReadTexels(pixels);
  162. g_FontTexture->SetMipMapping(false);
  163. g_FontTexture->SetFiltering(false);
  164. g_FontTexture->Upload();
  165. // Store our identifier
  166. io.Fonts->TexID = (void *)g_FontTexture;
  167. return true;
  168. }
  169. void ImGui_Marmalade_InvalidateDeviceObjects()
  170. {
  171. if (g_ClipboardText)
  172. {
  173. delete[] g_ClipboardText;
  174. g_ClipboardText = NULL;
  175. }
  176. if (g_FontTexture)
  177. {
  178. delete g_FontTexture;
  179. ImGui::GetIO().Fonts->TexID = 0;
  180. g_FontTexture = NULL;
  181. }
  182. }
  183. bool ImGui_Marmalade_Init(bool install_callbacks)
  184. {
  185. IwGxInit();
  186. ImGuiIO& io = ImGui::GetIO();
  187. io.KeyMap[ImGuiKey_Tab] = s3eKeyTab; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  188. io.KeyMap[ImGuiKey_LeftArrow] = s3eKeyLeft;
  189. io.KeyMap[ImGuiKey_RightArrow] = s3eKeyRight;
  190. io.KeyMap[ImGuiKey_UpArrow] = s3eKeyUp;
  191. io.KeyMap[ImGuiKey_DownArrow] = s3eKeyDown;
  192. io.KeyMap[ImGuiKey_PageUp] = s3eKeyPageUp;
  193. io.KeyMap[ImGuiKey_PageDown] = s3eKeyPageDown;
  194. io.KeyMap[ImGuiKey_Home] = s3eKeyHome;
  195. io.KeyMap[ImGuiKey_End] = s3eKeyEnd;
  196. io.KeyMap[ImGuiKey_Delete] = s3eKeyDelete;
  197. io.KeyMap[ImGuiKey_Backspace] = s3eKeyBackspace;
  198. io.KeyMap[ImGuiKey_Enter] = s3eKeyEnter;
  199. io.KeyMap[ImGuiKey_Escape] = s3eKeyEsc;
  200. io.KeyMap[ImGuiKey_A] = s3eKeyA;
  201. io.KeyMap[ImGuiKey_C] = s3eKeyC;
  202. io.KeyMap[ImGuiKey_V] = s3eKeyV;
  203. io.KeyMap[ImGuiKey_X] = s3eKeyX;
  204. io.KeyMap[ImGuiKey_Y] = s3eKeyY;
  205. io.KeyMap[ImGuiKey_Z] = s3eKeyZ;
  206. io.RenderDrawListsFn = ImGui_Marmalade_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  207. io.SetClipboardTextFn = ImGui_Marmalade_SetClipboardText;
  208. io.GetClipboardTextFn = ImGui_Marmalade_GetClipboardText;
  209. if (install_callbacks)
  210. {
  211. s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, ImGui_Marmalade_PointerButtonEventCallback, 0);
  212. s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, ImGui_Marmalade_KeyCallback, 0);
  213. s3eKeyboardRegister(S3E_KEYBOARD_CHAR_EVENT, ImGui_Marmalade_CharCallback, 0);
  214. }
  215. return true;
  216. }
  217. void ImGui_Marmalade_Shutdown()
  218. {
  219. ImGui_Marmalade_InvalidateDeviceObjects();
  220. ImGui::Shutdown();
  221. IwGxTerminate();
  222. }
  223. void ImGui_Marmalade_NewFrame()
  224. {
  225. if (!g_FontTexture)
  226. ImGui_Marmalade_CreateDeviceObjects();
  227. ImGuiIO& io = ImGui::GetIO();
  228. // Setup display size (every frame to accommodate for window resizing)
  229. int w = IwGxGetScreenWidth(), h = IwGxGetScreenHeight();
  230. io.DisplaySize = ImVec2((float)w, (float)h);
  231. // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui.
  232. io.DisplayFramebufferScale = g_scale;
  233. // Setup time step
  234. double current_time = s3eTimerGetUST() / 1000.0f;
  235. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  236. g_Time = current_time;
  237. double mouse_x, mouse_y;
  238. mouse_x = s3ePointerGetX();
  239. mouse_y = s3ePointerGetY();
  240. io.MousePos = ImVec2((float)mouse_x/g_scale.x, (float)mouse_y/g_scale.y); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
  241. for (int i = 0; i < 3; i++)
  242. {
  243. io.MouseDown[i] = g_MousePressed[i] || s3ePointerGetState((s3ePointerButton)i) != S3E_POINTER_STATE_UP; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  244. g_MousePressed[i] = false;
  245. }
  246. io.MouseWheel = g_MouseWheel;
  247. g_MouseWheel = 0.0f;
  248. // TODO: Hide OS mouse cursor if ImGui is drawing it
  249. // s3ePointerSetInt(S3E_POINTER_HIDE_CURSOR,(io.MouseDrawCursor ? 0 : 1));
  250. // Start the frame
  251. ImGui::NewFrame();
  252. // Show/hide OSD keyboard
  253. if (io.WantTextInput)
  254. {
  255. // Some text input widget is active?
  256. if (!g_osdKeyboardEnabled)
  257. {
  258. g_osdKeyboardEnabled = true;
  259. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 1); // show OSD keyboard
  260. }
  261. }
  262. else
  263. {
  264. // No text input widget is active
  265. if (g_osdKeyboardEnabled)
  266. {
  267. g_osdKeyboardEnabled = false;
  268. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 0); // hide OSD keyboard
  269. }
  270. }
  271. }