imgui

FORK: Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
git clone https://git.neptards.moe/neptards/imgui.git
Log | Files | Refs

imgui_impl_marmalade.cpp (12164B)


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