imgui_impl_marmalade.cpp 12 KB

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