imgui_impl_marmalade.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // ImGui Renderer + Platform Binding for: Marmalade + IwGx
  2. // Implemented features:
  3. // [X] Renderer: User texture binding. Use 'CIwTexture*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  4. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  5. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  6. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // Copyright (C) 2015 by Giovanni Zito
  9. // This file is part of ImGui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_Marmalade_RenderDrawData() in the .h file so you can call it yourself.
  13. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  14. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  15. #include "imgui.h"
  16. #include "imgui_impl_marmalade.h"
  17. #include <s3eClipboard.h>
  18. #include <s3ePointer.h>
  19. #include <s3eKeyboard.h>
  20. #include <IwTexture.h>
  21. #include <IwGx.h>
  22. // Data
  23. static double g_Time = 0.0f;
  24. static bool g_MousePressed[3] = { false, false, false };
  25. static CIwTexture* g_FontTexture = NULL;
  26. static char* g_ClipboardText = NULL;
  27. static bool g_osdKeyboardEnabled = false;
  28. // use this setting to scale the interface - e.g. on device you could use 2 or 3 scale factor
  29. static ImVec2 g_RenderScale = ImVec2(1.0f,1.0f);
  30. // Render function.
  31. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  32. void ImGui_Marmalade_RenderDrawData(ImDrawData* draw_data)
  33. {
  34. // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
  35. ImGuiIO& io = ImGui::GetIO();
  36. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  37. // Render command lists
  38. for(int n = 0; n < draw_data->CmdListsCount; n++)
  39. {
  40. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  41. const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
  42. const int nVert = cmd_list->VtxBuffer.Size;
  43. CIwFVec2* pVertStream = IW_GX_ALLOC(CIwFVec2, nVert);
  44. CIwFVec2* pUVStream = IW_GX_ALLOC(CIwFVec2, nVert);
  45. CIwColour* pColStream = IW_GX_ALLOC(CIwColour, nVert);
  46. for( int i=0; i < nVert; i++ )
  47. {
  48. // TODO: optimize multiplication on gpu using vertex shader/projection matrix.
  49. pVertStream[i].x = cmd_list->VtxBuffer[i].pos.x * g_RenderScale.x;
  50. pVertStream[i].y = cmd_list->VtxBuffer[i].pos.y * g_RenderScale.y;
  51. pUVStream[i].x = cmd_list->VtxBuffer[i].uv.x;
  52. pUVStream[i].y = cmd_list->VtxBuffer[i].uv.y;
  53. pColStream[i] = cmd_list->VtxBuffer[i].col;
  54. }
  55. IwGxSetVertStreamScreenSpace(pVertStream, nVert);
  56. IwGxSetUVStream(pUVStream);
  57. IwGxSetColStream(pColStream, nVert);
  58. IwGxSetNormStream(0);
  59. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  60. {
  61. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  62. if (pcmd->UserCallback)
  63. {
  64. pcmd->UserCallback(cmd_list, pcmd);
  65. }
  66. else
  67. {
  68. CIwMaterial* pCurrentMaterial = IW_GX_ALLOC_MATERIAL();
  69. pCurrentMaterial->SetShadeMode(CIwMaterial::SHADE_FLAT);
  70. pCurrentMaterial->SetCullMode(CIwMaterial::CULL_NONE);
  71. pCurrentMaterial->SetFiltering(false);
  72. pCurrentMaterial->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
  73. pCurrentMaterial->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_NORMAL);
  74. pCurrentMaterial->SetAlphaTestMode(CIwMaterial::ALPHATEST_DISABLED);
  75. pCurrentMaterial->SetTexture((CIwTexture*)pcmd->TextureId);
  76. IwGxSetMaterial(pCurrentMaterial);
  77. IwGxDrawPrims(IW_GX_TRI_LIST, (uint16*)idx_buffer, pcmd->ElemCount);
  78. }
  79. idx_buffer += pcmd->ElemCount;
  80. }
  81. IwGxFlush();
  82. }
  83. // TODO: restore modified state (i.e. mvp matrix)
  84. }
  85. static const char* ImGui_Marmalade_GetClipboardText(void* /*user_data*/)
  86. {
  87. if (!s3eClipboardAvailable())
  88. return NULL;
  89. if (int size = s3eClipboardGetText(NULL, 0))
  90. {
  91. if (g_ClipboardText)
  92. delete[] g_ClipboardText;
  93. g_ClipboardText = new char[size];
  94. g_ClipboardText[0] = '\0';
  95. s3eClipboardGetText(g_ClipboardText, size);
  96. }
  97. return g_ClipboardText;
  98. }
  99. static void ImGui_Marmalade_SetClipboardText(void* /*user_data*/, const char* text)
  100. {
  101. if (s3eClipboardAvailable())
  102. s3eClipboardSetText(text);
  103. }
  104. int32 ImGui_Marmalade_PointerButtonEventCallback(void* system_data, void* user_data)
  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*)system_data;
  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. io.MouseWheel += pEvent->m_y;
  120. if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELDOWN)
  121. io.MouseWheel += pEvent->m_y;
  122. }
  123. return 0;
  124. }
  125. int32 ImGui_Marmalade_KeyCallback(void* system_data, void* user_data)
  126. {
  127. ImGuiIO& io = ImGui::GetIO();
  128. s3eKeyboardEvent* e = (s3eKeyboardEvent*)system_data;
  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. io.KeySuper = s3eKeyboardGetState(s3eKeyLeftWindows) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightWindows) == S3E_KEY_STATE_DOWN;
  137. return 0;
  138. }
  139. int32 ImGui_Marmalade_CharCallback(void* system_data, void* user_data)
  140. {
  141. ImGuiIO& io = ImGui::GetIO();
  142. s3eKeyboardCharEvent* e = (s3eKeyboardCharEvent*)system_data;
  143. if ((e->m_Char > 0 && e->m_Char < 0x10000))
  144. io.AddInputCharacter((unsigned short)e->m_Char);
  145. return 0;
  146. }
  147. bool ImGui_Marmalade_CreateDeviceObjects()
  148. {
  149. // Build texture atlas
  150. ImGuiIO& io = ImGui::GetIO();
  151. unsigned char* pixels;
  152. int width, height;
  153. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  154. // Upload texture to graphics system
  155. g_FontTexture = new CIwTexture();
  156. g_FontTexture->SetModifiable(true);
  157. CIwImage& image = g_FontTexture->GetImage();
  158. image.SetFormat(CIwImage::ARGB_8888);
  159. image.SetWidth(width);
  160. image.SetHeight(height);
  161. image.SetBuffers(); // allocates and own buffers
  162. image.ReadTexels(pixels);
  163. g_FontTexture->SetMipMapping(false);
  164. g_FontTexture->SetFiltering(false);
  165. g_FontTexture->Upload();
  166. // Store our identifier
  167. io.Fonts->TexID = (void *)g_FontTexture;
  168. return true;
  169. }
  170. void ImGui_Marmalade_InvalidateDeviceObjects()
  171. {
  172. if (g_ClipboardText)
  173. {
  174. delete[] g_ClipboardText;
  175. g_ClipboardText = NULL;
  176. }
  177. if (g_FontTexture)
  178. {
  179. delete g_FontTexture;
  180. ImGui::GetIO().Fonts->TexID = 0;
  181. g_FontTexture = NULL;
  182. }
  183. }
  184. bool ImGui_Marmalade_Init(bool install_callbacks)
  185. {
  186. ImGuiIO& io = ImGui::GetIO();
  187. io.KeyMap[ImGuiKey_Tab] = s3eKeyTab; // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] 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_Insert] = s3eKeyInsert;
  197. io.KeyMap[ImGuiKey_Delete] = s3eKeyDelete;
  198. io.KeyMap[ImGuiKey_Backspace] = s3eKeyBackspace;
  199. io.KeyMap[ImGuiKey_Space] = s3eKeySpace;
  200. io.KeyMap[ImGuiKey_Enter] = s3eKeyEnter;
  201. io.KeyMap[ImGuiKey_Escape] = s3eKeyEsc;
  202. io.KeyMap[ImGuiKey_A] = s3eKeyA;
  203. io.KeyMap[ImGuiKey_C] = s3eKeyC;
  204. io.KeyMap[ImGuiKey_V] = s3eKeyV;
  205. io.KeyMap[ImGuiKey_X] = s3eKeyX;
  206. io.KeyMap[ImGuiKey_Y] = s3eKeyY;
  207. io.KeyMap[ImGuiKey_Z] = s3eKeyZ;
  208. io.SetClipboardTextFn = ImGui_Marmalade_SetClipboardText;
  209. io.GetClipboardTextFn = ImGui_Marmalade_GetClipboardText;
  210. if (install_callbacks)
  211. {
  212. s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, ImGui_Marmalade_PointerButtonEventCallback, 0);
  213. s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, ImGui_Marmalade_KeyCallback, 0);
  214. s3eKeyboardRegister(S3E_KEYBOARD_CHAR_EVENT, ImGui_Marmalade_CharCallback, 0);
  215. }
  216. return true;
  217. }
  218. void ImGui_Marmalade_Shutdown()
  219. {
  220. ImGui_Marmalade_InvalidateDeviceObjects();
  221. }
  222. void ImGui_Marmalade_NewFrame()
  223. {
  224. if (!g_FontTexture)
  225. ImGui_Marmalade_CreateDeviceObjects();
  226. ImGuiIO& io = ImGui::GetIO();
  227. // Setup display size (every frame to accommodate for window resizing)
  228. int w = IwGxGetScreenWidth(), h = IwGxGetScreenHeight();
  229. io.DisplaySize = ImVec2((float)w, (float)h);
  230. // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui.
  231. io.DisplayFramebufferScale = g_scale;
  232. // Setup time step
  233. double current_time = s3eTimerGetUST() / 1000.0f;
  234. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  235. g_Time = current_time;
  236. double mouse_x, mouse_y;
  237. mouse_x = s3ePointerGetX();
  238. mouse_y = s3ePointerGetY();
  239. io.MousePos = ImVec2((float)mouse_x/g_scale.x, (float)mouse_y/g_scale.y); // Mouse position (set to -FLT_MAX,-FLT_MAX if no mouse / on another screen, etc.)
  240. for (int i = 0; i < 3; i++)
  241. {
  242. 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.
  243. g_MousePressed[i] = false;
  244. }
  245. // TODO: Hide OS mouse cursor if ImGui is drawing it
  246. // s3ePointerSetInt(S3E_POINTER_HIDE_CURSOR,(io.MouseDrawCursor ? 0 : 1));
  247. // Show/hide OSD keyboard
  248. if (io.WantTextInput)
  249. {
  250. // Some text input widget is active?
  251. if (!g_osdKeyboardEnabled)
  252. {
  253. g_osdKeyboardEnabled = true;
  254. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 1); // show OSD keyboard
  255. }
  256. }
  257. else
  258. {
  259. // No text input widget is active
  260. if (g_osdKeyboardEnabled)
  261. {
  262. g_osdKeyboardEnabled = false;
  263. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 0); // hide OSD keyboard
  264. }
  265. }
  266. }