imgui_impl_marmalade.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // dear imgui: Renderer + Platform Backend for Marmalade + IwGx
  2. // Marmalade code: Copyright (C) 2015 by Giovanni Zito (this file is part of Dear ImGui)
  3. // Implemented features:
  4. // [X] Renderer: User texture binding. Use 'CIwTexture*' as ImTextureID. Read the FAQ about ImTextureID!
  5. // Missing features:
  6. // [ ] Renderer: Clipping rectangles are not honored.
  7. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  8. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  9. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  10. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  11. // CHANGELOG
  12. // (minor and older changes stripped away, please see git history for details)
  13. // 2021-05-19: Renderer: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement)
  14. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
  15. // 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter().
  16. // 2018-11-30: Misc: Setting up io.BackendPlatformName/io.BackendRendererName so they can be displayed in the About Window.
  17. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_Marmalade_RenderDrawData() in the .h file so you can call it yourself.
  18. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  19. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  20. #include "imgui.h"
  21. #include "imgui_impl_marmalade.h"
  22. #include <s3eClipboard.h>
  23. #include <s3ePointer.h>
  24. #include <s3eKeyboard.h>
  25. #include <IwTexture.h>
  26. #include <IwGx.h>
  27. // Data
  28. static double g_Time = 0.0f;
  29. static bool g_MousePressed[3] = { false, false, false };
  30. static CIwTexture* g_FontTexture = NULL;
  31. static char* g_ClipboardText = NULL;
  32. static bool g_osdKeyboardEnabled = false;
  33. // use this setting to scale the interface - e.g. on device you could use 2 or 3 scale factor
  34. static ImVec2 g_RenderScale = ImVec2(1.0f, 1.0f);
  35. // Render function.
  36. void ImGui_Marmalade_RenderDrawData(ImDrawData* draw_data)
  37. {
  38. // Avoid rendering when minimized
  39. if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
  40. return;
  41. // Render command lists
  42. for (int n = 0; n < draw_data->CmdListsCount; n++)
  43. {
  44. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  45. const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
  46. const int nVert = cmd_list->VtxBuffer.Size;
  47. CIwFVec2* pVertStream = IW_GX_ALLOC(CIwFVec2, nVert);
  48. CIwFVec2* pUVStream = IW_GX_ALLOC(CIwFVec2, nVert);
  49. CIwColour* pColStream = IW_GX_ALLOC(CIwColour, nVert);
  50. for (int i = 0; i < nVert; i++)
  51. {
  52. // FIXME-OPT: optimize multiplication on GPU using vertex shader/projection matrix.
  53. pVertStream[i].x = cmd_list->VtxBuffer[i].pos.x * g_RenderScale.x;
  54. pVertStream[i].y = cmd_list->VtxBuffer[i].pos.y * g_RenderScale.y;
  55. pUVStream[i].x = cmd_list->VtxBuffer[i].uv.x;
  56. pUVStream[i].y = cmd_list->VtxBuffer[i].uv.y;
  57. pColStream[i] = cmd_list->VtxBuffer[i].col;
  58. }
  59. IwGxSetVertStreamScreenSpace(pVertStream, nVert);
  60. IwGxSetUVStream(pUVStream);
  61. IwGxSetColStream(pColStream, nVert);
  62. IwGxSetNormStream(0);
  63. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  64. {
  65. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  66. if (pcmd->UserCallback)
  67. {
  68. pcmd->UserCallback(cmd_list, pcmd);
  69. }
  70. else
  71. {
  72. // FIXME: Not honoring ClipRect fields.
  73. CIwMaterial* pCurrentMaterial = IW_GX_ALLOC_MATERIAL();
  74. pCurrentMaterial->SetShadeMode(CIwMaterial::SHADE_FLAT);
  75. pCurrentMaterial->SetCullMode(CIwMaterial::CULL_NONE);
  76. pCurrentMaterial->SetFiltering(false);
  77. pCurrentMaterial->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
  78. pCurrentMaterial->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_NORMAL);
  79. pCurrentMaterial->SetAlphaTestMode(CIwMaterial::ALPHATEST_DISABLED);
  80. pCurrentMaterial->SetTexture((CIwTexture*)pcmd->GetTexID());
  81. IwGxSetMaterial(pCurrentMaterial);
  82. IwGxDrawPrims(IW_GX_TRI_LIST, (uint16*)idx_buffer, pcmd->ElemCount);
  83. }
  84. idx_buffer += pcmd->ElemCount;
  85. }
  86. IwGxFlush();
  87. }
  88. // TODO: restore modified state (i.e. mvp matrix)
  89. }
  90. static const char* ImGui_Marmalade_GetClipboardText(void* /*user_data*/)
  91. {
  92. if (!s3eClipboardAvailable())
  93. return NULL;
  94. if (int size = s3eClipboardGetText(NULL, 0))
  95. {
  96. if (g_ClipboardText)
  97. delete[] g_ClipboardText;
  98. g_ClipboardText = new char[size];
  99. g_ClipboardText[0] = '\0';
  100. s3eClipboardGetText(g_ClipboardText, size);
  101. }
  102. return g_ClipboardText;
  103. }
  104. static void ImGui_Marmalade_SetClipboardText(void* /*user_data*/, const char* text)
  105. {
  106. if (s3eClipboardAvailable())
  107. s3eClipboardSetText(text);
  108. }
  109. int32 ImGui_Marmalade_PointerButtonEventCallback(void* system_data, void* user_data)
  110. {
  111. // pEvent->m_Button is of type s3ePointerButton and indicates which mouse
  112. // button was pressed. For touchscreen this should always have the value
  113. // S3E_POINTER_BUTTON_SELECT
  114. s3ePointerEvent* pEvent = (s3ePointerEvent*)system_data;
  115. if (pEvent->m_Pressed == 1)
  116. {
  117. if (pEvent->m_Button == S3E_POINTER_BUTTON_LEFTMOUSE)
  118. g_MousePressed[0] = true;
  119. if (pEvent->m_Button == S3E_POINTER_BUTTON_RIGHTMOUSE)
  120. g_MousePressed[1] = true;
  121. if (pEvent->m_Button == S3E_POINTER_BUTTON_MIDDLEMOUSE)
  122. g_MousePressed[2] = true;
  123. if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELUP)
  124. io.MouseWheel += pEvent->m_y;
  125. if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELDOWN)
  126. io.MouseWheel += pEvent->m_y;
  127. }
  128. return 0;
  129. }
  130. int32 ImGui_Marmalade_KeyCallback(void* system_data, void* user_data)
  131. {
  132. ImGuiIO& io = ImGui::GetIO();
  133. s3eKeyboardEvent* e = (s3eKeyboardEvent*)system_data;
  134. if (e->m_Pressed == 1)
  135. io.KeysDown[e->m_Key] = true;
  136. if (e->m_Pressed == 0)
  137. io.KeysDown[e->m_Key] = false;
  138. io.KeyCtrl = s3eKeyboardGetState(s3eKeyLeftControl) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightControl) == S3E_KEY_STATE_DOWN;
  139. io.KeyShift = s3eKeyboardGetState(s3eKeyLeftShift) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightShift) == S3E_KEY_STATE_DOWN;
  140. io.KeyAlt = s3eKeyboardGetState(s3eKeyLeftAlt) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightAlt) == S3E_KEY_STATE_DOWN;
  141. io.KeySuper = s3eKeyboardGetState(s3eKeyLeftWindows) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightWindows) == S3E_KEY_STATE_DOWN;
  142. return 0;
  143. }
  144. int32 ImGui_Marmalade_CharCallback(void* system_data, void* user_data)
  145. {
  146. ImGuiIO& io = ImGui::GetIO();
  147. s3eKeyboardCharEvent* e = (s3eKeyboardCharEvent*)system_data;
  148. io.AddInputCharacter((unsigned int)e->m_Char);
  149. return 0;
  150. }
  151. bool ImGui_Marmalade_CreateDeviceObjects()
  152. {
  153. // Build texture atlas
  154. ImGuiIO& io = ImGui::GetIO();
  155. unsigned char* pixels;
  156. int width, height;
  157. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  158. // Upload texture to graphics system
  159. g_FontTexture = new CIwTexture();
  160. g_FontTexture->SetModifiable(true);
  161. CIwImage& image = g_FontTexture->GetImage();
  162. image.SetFormat(CIwImage::ARGB_8888);
  163. image.SetWidth(width);
  164. image.SetHeight(height);
  165. image.SetBuffers(); // allocates and own buffers
  166. image.ReadTexels(pixels);
  167. g_FontTexture->SetMipMapping(false);
  168. g_FontTexture->SetFiltering(false);
  169. g_FontTexture->Upload();
  170. // Store our identifier
  171. io.Fonts->SetTexID((ImTextureID)g_FontTexture);
  172. return true;
  173. }
  174. void ImGui_Marmalade_InvalidateDeviceObjects()
  175. {
  176. if (g_ClipboardText)
  177. {
  178. delete[] g_ClipboardText;
  179. g_ClipboardText = NULL;
  180. }
  181. if (g_FontTexture)
  182. {
  183. ImGui::GetIO().Fonts->SetTexID(0);
  184. delete g_FontTexture;
  185. g_FontTexture = NULL;
  186. }
  187. }
  188. bool ImGui_Marmalade_Init(bool install_callbacks)
  189. {
  190. ImGuiIO& io = ImGui::GetIO();
  191. io.BackendPlatformName = io.BackendRendererName = "imgui_impl_marmalade";
  192. // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array.
  193. io.KeyMap[ImGuiKey_Tab] = s3eKeyTab
  194. io.KeyMap[ImGuiKey_LeftArrow] = s3eKeyLeft;
  195. io.KeyMap[ImGuiKey_RightArrow] = s3eKeyRight;
  196. io.KeyMap[ImGuiKey_UpArrow] = s3eKeyUp;
  197. io.KeyMap[ImGuiKey_DownArrow] = s3eKeyDown;
  198. io.KeyMap[ImGuiKey_PageUp] = s3eKeyPageUp;
  199. io.KeyMap[ImGuiKey_PageDown] = s3eKeyPageDown;
  200. io.KeyMap[ImGuiKey_Home] = s3eKeyHome;
  201. io.KeyMap[ImGuiKey_End] = s3eKeyEnd;
  202. io.KeyMap[ImGuiKey_Insert] = s3eKeyInsert;
  203. io.KeyMap[ImGuiKey_Delete] = s3eKeyDelete;
  204. io.KeyMap[ImGuiKey_Backspace] = s3eKeyBackspace;
  205. io.KeyMap[ImGuiKey_Space] = s3eKeySpace;
  206. io.KeyMap[ImGuiKey_Enter] = s3eKeyEnter;
  207. io.KeyMap[ImGuiKey_Escape] = s3eKeyEsc;
  208. io.KeyMap[ImGuiKey_KeyPadEnter] = s3eKeyNumPadEnter;
  209. io.KeyMap[ImGuiKey_A] = s3eKeyA;
  210. io.KeyMap[ImGuiKey_C] = s3eKeyC;
  211. io.KeyMap[ImGuiKey_V] = s3eKeyV;
  212. io.KeyMap[ImGuiKey_X] = s3eKeyX;
  213. io.KeyMap[ImGuiKey_Y] = s3eKeyY;
  214. io.KeyMap[ImGuiKey_Z] = s3eKeyZ;
  215. io.SetClipboardTextFn = ImGui_Marmalade_SetClipboardText;
  216. io.GetClipboardTextFn = ImGui_Marmalade_GetClipboardText;
  217. if (install_callbacks)
  218. {
  219. s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, ImGui_Marmalade_PointerButtonEventCallback, 0);
  220. s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, ImGui_Marmalade_KeyCallback, 0);
  221. s3eKeyboardRegister(S3E_KEYBOARD_CHAR_EVENT, ImGui_Marmalade_CharCallback, 0);
  222. }
  223. return true;
  224. }
  225. void ImGui_Marmalade_Shutdown()
  226. {
  227. ImGui_Marmalade_InvalidateDeviceObjects();
  228. }
  229. void ImGui_Marmalade_NewFrame()
  230. {
  231. if (!g_FontTexture)
  232. ImGui_Marmalade_CreateDeviceObjects();
  233. ImGuiIO& io = ImGui::GetIO();
  234. // Setup display size (every frame to accommodate for window resizing)
  235. int w = IwGxGetScreenWidth(), h = IwGxGetScreenHeight();
  236. io.DisplaySize = ImVec2((float)w, (float)h);
  237. // For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui.
  238. io.DisplayFramebufferScale = g_scale;
  239. // Setup time step
  240. double current_time = s3eTimerGetUST() / 1000.0f;
  241. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
  242. g_Time = current_time;
  243. double mouse_x, mouse_y;
  244. mouse_x = s3ePointerGetX();
  245. mouse_y = s3ePointerGetY();
  246. 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.)
  247. for (int i = 0; i < 3; i++)
  248. {
  249. 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.
  250. g_MousePressed[i] = false;
  251. }
  252. // TODO: Hide OS mouse cursor if ImGui is drawing it
  253. // s3ePointerSetInt(S3E_POINTER_HIDE_CURSOR,(io.MouseDrawCursor ? 0 : 1));
  254. // Show/hide OSD keyboard
  255. if (io.WantTextInput)
  256. {
  257. // Some text input widget is active?
  258. if (!g_osdKeyboardEnabled)
  259. {
  260. g_osdKeyboardEnabled = true;
  261. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 1); // show OSD keyboard
  262. }
  263. }
  264. else
  265. {
  266. // No text input widget is active
  267. if (g_osdKeyboardEnabled)
  268. {
  269. g_osdKeyboardEnabled = false;
  270. s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 0); // hide OSD keyboard
  271. }
  272. }
  273. }