mirror of https://github.com/ocornut/imgui.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
312 lines
12 KiB
312 lines
12 KiB
7 years ago
|
// ImGui Renderer + Platform Binding for: Marmalade + IwGx
|
||
7 years ago
|
|
||
|
// Implemented features:
|
||
|
// [X] User texture binding. Use 'CIwTexture*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
|
||
9 years ago
|
|
||
9 years ago
|
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||
|
// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
|
||
|
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
|
||
9 years ago
|
// https://github.com/ocornut/imgui
|
||
|
|
||
9 years ago
|
// Copyright (C) 2015 by Giovanni Zito
|
||
|
// This file is part of ImGui
|
||
|
|
||
7 years ago
|
// CHANGELOG
|
||
|
// (minor and older changes stripped away, please see git history for details)
|
||
|
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_Marmalade_RenderDrawData() in the .h file so you can call it yourself.
|
||
|
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||
|
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||
|
|
||
7 years ago
|
#include "imgui.h"
|
||
9 years ago
|
#include "imgui_impl_marmalade.h"
|
||
|
|
||
|
#include <s3eClipboard.h>
|
||
9 years ago
|
#include <s3ePointer.h>
|
||
9 years ago
|
#include <s3eKeyboard.h>
|
||
|
#include <IwTexture.h>
|
||
|
#include <IwGx.h>
|
||
|
|
||
|
// Data
|
||
|
static double g_Time = 0.0f;
|
||
|
static bool g_MousePressed[3] = { false, false, false };
|
||
9 years ago
|
static CIwTexture* g_FontTexture = NULL;
|
||
|
static char* g_ClipboardText = NULL;
|
||
|
static bool g_osdKeyboardEnabled = false;
|
||
9 years ago
|
|
||
|
// use this setting to scale the interface - e.g. on device you could use 2 or 3 scale factor
|
||
7 years ago
|
static ImVec2 g_RenderScale = ImVec2(1.0f,1.0f);
|
||
9 years ago
|
|
||
7 years ago
|
// Render function.
|
||
|
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
|
||
|
void ImGui_Marmalade_RenderDrawData(ImDrawData* draw_data)
|
||
9 years ago
|
{
|
||
9 years ago
|
// Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
|
||
|
|
||
|
// Render command lists
|
||
|
for(int n = 0; n < draw_data->CmdListsCount; n++)
|
||
|
{
|
||
|
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||
8 years ago
|
const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;
|
||
|
const int nVert = cmd_list->VtxBuffer.Size;
|
||
9 years ago
|
CIwFVec2* pVertStream = IW_GX_ALLOC(CIwFVec2, nVert);
|
||
|
CIwFVec2* pUVStream = IW_GX_ALLOC(CIwFVec2, nVert);
|
||
|
CIwColour* pColStream = IW_GX_ALLOC(CIwColour, nVert);
|
||
|
|
||
9 years ago
|
for( int i=0; i < nVert; i++ )
|
||
9 years ago
|
{
|
||
7 years ago
|
// TODO: optimize multiplication on gpu using vertex shader/projection matrix.
|
||
|
pVertStream[i].x = cmd_list->VtxBuffer[i].pos.x * g_RenderScale.x;
|
||
|
pVertStream[i].y = cmd_list->VtxBuffer[i].pos.y * g_RenderScale.y;
|
||
9 years ago
|
pUVStream[i].x = cmd_list->VtxBuffer[i].uv.x;
|
||
|
pUVStream[i].y = cmd_list->VtxBuffer[i].uv.y;
|
||
|
pColStream[i] = cmd_list->VtxBuffer[i].col;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
IwGxSetVertStreamScreenSpace(pVertStream, nVert);
|
||
|
IwGxSetUVStream(pUVStream);
|
||
|
IwGxSetColStream(pColStream, nVert);
|
||
|
IwGxSetNormStream(0);
|
||
9 years ago
|
|
||
8 years ago
|
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
|
||
9 years ago
|
{
|
||
|
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
|
||
9 years ago
|
if (pcmd->UserCallback)
|
||
9 years ago
|
{
|
||
8 years ago
|
pcmd->UserCallback(cmd_list, pcmd);
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
CIwMaterial* pCurrentMaterial = IW_GX_ALLOC_MATERIAL();
|
||
|
pCurrentMaterial->SetShadeMode(CIwMaterial::SHADE_FLAT);
|
||
|
pCurrentMaterial->SetCullMode(CIwMaterial::CULL_NONE);
|
||
|
pCurrentMaterial->SetFiltering(false);
|
||
|
pCurrentMaterial->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
|
||
9 years ago
|
pCurrentMaterial->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_NORMAL);
|
||
|
pCurrentMaterial->SetAlphaTestMode(CIwMaterial::ALPHATEST_DISABLED);
|
||
9 years ago
|
pCurrentMaterial->SetTexture((CIwTexture*)pcmd->TextureId);
|
||
|
IwGxSetMaterial(pCurrentMaterial);
|
||
|
IwGxDrawPrims(IW_GX_TRI_LIST, (uint16*)idx_buffer, pcmd->ElemCount);
|
||
9 years ago
|
}
|
||
|
idx_buffer += pcmd->ElemCount;
|
||
|
}
|
||
9 years ago
|
IwGxFlush();
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// TODO: restore modified state (i.e. mvp matrix)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
static const char* ImGui_Marmalade_GetClipboardText(void* /*user_data*/)
|
||
9 years ago
|
{
|
||
8 years ago
|
if (!s3eClipboardAvailable())
|
||
|
return NULL;
|
||
|
|
||
|
if (int size = s3eClipboardGetText(NULL, 0))
|
||
9 years ago
|
{
|
||
8 years ago
|
if (g_ClipboardText)
|
||
|
delete[] g_ClipboardText;
|
||
|
g_ClipboardText = new char[size];
|
||
|
g_ClipboardText[0] = '\0';
|
||
|
s3eClipboardGetText(g_ClipboardText, size);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
return g_ClipboardText;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
static void ImGui_Marmalade_SetClipboardText(void* /*user_data*/, const char* text)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (s3eClipboardAvailable())
|
||
9 years ago
|
s3eClipboardSetText(text);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
int32 ImGui_Marmalade_PointerButtonEventCallback(void* SystemData, void* pUserData)
|
||
9 years ago
|
{
|
||
9 years ago
|
// pEvent->m_Button is of type s3ePointerButton and indicates which mouse
|
||
9 years ago
|
// button was pressed. For touchscreen this should always have the value
|
||
9 years ago
|
// S3E_POINTER_BUTTON_SELECT
|
||
9 years ago
|
s3ePointerEvent* pEvent = (s3ePointerEvent*)SystemData;
|
||
9 years ago
|
|
||
9 years ago
|
if (pEvent->m_Pressed == 1)
|
||
9 years ago
|
{
|
||
|
if (pEvent->m_Button == S3E_POINTER_BUTTON_LEFTMOUSE)
|
||
9 years ago
|
g_MousePressed[0] = true;
|
||
9 years ago
|
if (pEvent->m_Button == S3E_POINTER_BUTTON_RIGHTMOUSE)
|
||
9 years ago
|
g_MousePressed[1] = true;
|
||
9 years ago
|
if (pEvent->m_Button == S3E_POINTER_BUTTON_MIDDLEMOUSE)
|
||
9 years ago
|
g_MousePressed[2] = true;
|
||
9 years ago
|
if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELUP)
|
||
7 years ago
|
io.MouseWheel += pEvent->m_y;
|
||
9 years ago
|
if (pEvent->m_Button == S3E_POINTER_BUTTON_MOUSEWHEELDOWN)
|
||
7 years ago
|
io.MouseWheel += pEvent->m_y;
|
||
9 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
|
int32 ImGui_Marmalade_KeyCallback(void* SystemData, void* userData)
|
||
|
{
|
||
9 years ago
|
ImGuiIO& io = ImGui::GetIO();
|
||
9 years ago
|
s3eKeyboardEvent* e = (s3eKeyboardEvent*)SystemData;
|
||
|
if (e->m_Pressed == 1)
|
||
9 years ago
|
io.KeysDown[e->m_Key] = true;
|
||
9 years ago
|
if (e->m_Pressed == 0)
|
||
9 years ago
|
io.KeysDown[e->m_Key] = false;
|
||
9 years ago
|
|
||
9 years ago
|
io.KeyCtrl = s3eKeyboardGetState(s3eKeyLeftControl) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightControl) == S3E_KEY_STATE_DOWN;
|
||
|
io.KeyShift = s3eKeyboardGetState(s3eKeyLeftShift) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightShift) == S3E_KEY_STATE_DOWN;
|
||
|
io.KeyAlt = s3eKeyboardGetState(s3eKeyLeftAlt) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightAlt) == S3E_KEY_STATE_DOWN;
|
||
9 years ago
|
io.KeySuper = s3eKeyboardGetState(s3eKeyLeftWindows) == S3E_KEY_STATE_DOWN || s3eKeyboardGetState(s3eKeyRightWindows) == S3E_KEY_STATE_DOWN;
|
||
9 years ago
|
|
||
9 years ago
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
int32 ImGui_Marmalade_CharCallback(void* SystemData, void* userData)
|
||
9 years ago
|
{
|
||
9 years ago
|
ImGuiIO& io = ImGui::GetIO();
|
||
9 years ago
|
s3eKeyboardCharEvent* e = (s3eKeyboardCharEvent*)SystemData;
|
||
|
if ((e->m_Char > 0 && e->m_Char < 0x10000))
|
||
9 years ago
|
io.AddInputCharacter((unsigned short)e->m_Char);
|
||
9 years ago
|
|
||
9 years ago
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
|
bool ImGui_Marmalade_CreateDeviceObjects()
|
||
|
{
|
||
|
// Build texture atlas
|
||
9 years ago
|
ImGuiIO& io = ImGui::GetIO();
|
||
9 years ago
|
unsigned char* pixels;
|
||
|
int width, height;
|
||
|
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
|
||
|
|
||
9 years ago
|
// Upload texture to graphics system
|
||
9 years ago
|
g_FontTexture = new CIwTexture();
|
||
|
g_FontTexture->SetModifiable(true);
|
||
|
CIwImage& image = g_FontTexture->GetImage();
|
||
|
image.SetFormat(CIwImage::ARGB_8888);
|
||
|
image.SetWidth(width);
|
||
|
image.SetHeight(height);
|
||
9 years ago
|
image.SetBuffers(); // allocates and own buffers
|
||
9 years ago
|
image.ReadTexels(pixels);
|
||
|
g_FontTexture->SetMipMapping(false);
|
||
|
g_FontTexture->SetFiltering(false);
|
||
|
g_FontTexture->Upload();
|
||
9 years ago
|
|
||
9 years ago
|
// Store our identifier
|
||
9 years ago
|
io.Fonts->TexID = (void *)g_FontTexture;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void ImGui_Marmalade_InvalidateDeviceObjects()
|
||
|
{
|
||
9 years ago
|
if (g_ClipboardText)
|
||
9 years ago
|
{
|
||
|
delete[] g_ClipboardText;
|
||
|
g_ClipboardText = NULL;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (g_FontTexture)
|
||
|
{
|
||
9 years ago
|
delete g_FontTexture;
|
||
9 years ago
|
ImGui::GetIO().Fonts->TexID = 0;
|
||
9 years ago
|
g_FontTexture = NULL;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
bool ImGui_Marmalade_Init(bool install_callbacks)
|
||
9 years ago
|
{
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
7 years ago
|
io.KeyMap[ImGuiKey_Tab] = s3eKeyTab; // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
|
||
9 years ago
|
io.KeyMap[ImGuiKey_LeftArrow] = s3eKeyLeft;
|
||
|
io.KeyMap[ImGuiKey_RightArrow] = s3eKeyRight;
|
||
|
io.KeyMap[ImGuiKey_UpArrow] = s3eKeyUp;
|
||
|
io.KeyMap[ImGuiKey_DownArrow] = s3eKeyDown;
|
||
|
io.KeyMap[ImGuiKey_PageUp] = s3eKeyPageUp;
|
||
|
io.KeyMap[ImGuiKey_PageDown] = s3eKeyPageDown;
|
||
|
io.KeyMap[ImGuiKey_Home] = s3eKeyHome;
|
||
|
io.KeyMap[ImGuiKey_End] = s3eKeyEnd;
|
||
7 years ago
|
io.KeyMap[ImGuiKey_Insert] = s3eKeyInsert;
|
||
9 years ago
|
io.KeyMap[ImGuiKey_Delete] = s3eKeyDelete;
|
||
|
io.KeyMap[ImGuiKey_Backspace] = s3eKeyBackspace;
|
||
7 years ago
|
io.KeyMap[ImGuiKey_Space] = s3eKeySpace;
|
||
9 years ago
|
io.KeyMap[ImGuiKey_Enter] = s3eKeyEnter;
|
||
|
io.KeyMap[ImGuiKey_Escape] = s3eKeyEsc;
|
||
|
io.KeyMap[ImGuiKey_A] = s3eKeyA;
|
||
|
io.KeyMap[ImGuiKey_C] = s3eKeyC;
|
||
|
io.KeyMap[ImGuiKey_V] = s3eKeyV;
|
||
|
io.KeyMap[ImGuiKey_X] = s3eKeyX;
|
||
|
io.KeyMap[ImGuiKey_Y] = s3eKeyY;
|
||
|
io.KeyMap[ImGuiKey_Z] = s3eKeyZ;
|
||
|
|
||
|
io.SetClipboardTextFn = ImGui_Marmalade_SetClipboardText;
|
||
|
io.GetClipboardTextFn = ImGui_Marmalade_GetClipboardText;
|
||
|
|
||
|
if (install_callbacks)
|
||
|
{
|
||
9 years ago
|
s3ePointerRegister(S3E_POINTER_BUTTON_EVENT, ImGui_Marmalade_PointerButtonEventCallback, 0);
|
||
|
s3eKeyboardRegister(S3E_KEYBOARD_KEY_EVENT, ImGui_Marmalade_KeyCallback, 0);
|
||
|
s3eKeyboardRegister(S3E_KEYBOARD_CHAR_EVENT, ImGui_Marmalade_CharCallback, 0);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void ImGui_Marmalade_Shutdown()
|
||
|
{
|
||
|
ImGui_Marmalade_InvalidateDeviceObjects();
|
||
|
}
|
||
|
|
||
|
void ImGui_Marmalade_NewFrame()
|
||
|
{
|
||
9 years ago
|
if (!g_FontTexture)
|
||
9 years ago
|
ImGui_Marmalade_CreateDeviceObjects();
|
||
|
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
|
||
|
// Setup display size (every frame to accommodate for window resizing)
|
||
9 years ago
|
int w = IwGxGetScreenWidth(), h = IwGxGetScreenHeight();
|
||
9 years ago
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
||
9 years ago
|
// For retina display or other situations where window coordinates are different from framebuffer coordinates. User storage only, presently not used by ImGui.
|
||
|
io.DisplayFramebufferScale = g_scale;
|
||
9 years ago
|
|
||
|
// Setup time step
|
||
9 years ago
|
double current_time = s3eTimerGetUST() / 1000.0f;
|
||
9 years ago
|
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
|
||
|
g_Time = current_time;
|
||
|
|
||
9 years ago
|
double mouse_x, mouse_y;
|
||
|
mouse_x = s3ePointerGetX();
|
||
|
mouse_y = s3ePointerGetY();
|
||
7 years ago
|
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.)
|
||
9 years ago
|
|
||
|
for (int i = 0; i < 3; i++)
|
||
9 years ago
|
{
|
||
9 years ago
|
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.
|
||
|
g_MousePressed[i] = false;
|
||
|
}
|
||
|
|
||
9 years ago
|
// TODO: Hide OS mouse cursor if ImGui is drawing it
|
||
|
// s3ePointerSetInt(S3E_POINTER_HIDE_CURSOR,(io.MouseDrawCursor ? 0 : 1));
|
||
9 years ago
|
|
||
9 years ago
|
// Show/hide OSD keyboard
|
||
|
if (io.WantTextInput)
|
||
9 years ago
|
{
|
||
9 years ago
|
// Some text input widget is active?
|
||
9 years ago
|
if (!g_osdKeyboardEnabled)
|
||
9 years ago
|
{
|
||
|
g_osdKeyboardEnabled = true;
|
||
|
s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 1); // show OSD keyboard
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
else
|
||
9 years ago
|
{
|
||
|
// No text input widget is active
|
||
|
if (g_osdKeyboardEnabled)
|
||
|
{
|
||
|
g_osdKeyboardEnabled = false;
|
||
|
s3eKeyboardSetInt(S3E_KEYBOARD_GET_CHAR, 0); // hide OSD keyboard
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
}
|