2015-01-17 09:41:37 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2014-2015 Daniel Collin. All rights reserved.
|
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2015-09-19 06:16:24 +03:00
|
|
|
#include <bgfx/bgfx.h>
|
2015-04-16 06:00:15 +03:00
|
|
|
#include <bx/allocator.h>
|
2015-01-17 09:41:37 +03:00
|
|
|
#include <bx/fpumath.h>
|
2015-07-18 05:57:24 +03:00
|
|
|
#include <bx/timer.h>
|
2015-01-17 09:41:37 +03:00
|
|
|
#include <ocornut-imgui/imgui.h>
|
2015-10-01 02:22:51 +03:00
|
|
|
#include <ocornut-imgui/imgui_wm.h>
|
2015-01-20 04:16:59 +03:00
|
|
|
#include "imgui.h"
|
2015-01-17 09:41:37 +03:00
|
|
|
#include "ocornut_imgui.h"
|
|
|
|
#include <stb/stb_image.c>
|
2015-06-10 19:53:09 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
#ifndef USE_ENTRY
|
|
|
|
# define USE_ENTRY defined(SCI_NAMESPACE)
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
|
|
|
#if USE_ENTRY
|
|
|
|
# include "../entry/entry.h"
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
2015-06-10 19:53:09 +03:00
|
|
|
#if defined(SCI_NAMESPACE)
|
|
|
|
# include "../entry/input.h"
|
2015-07-18 10:30:46 +03:00
|
|
|
# include "scintilla.h"
|
2015-06-10 19:53:09 +03:00
|
|
|
#endif // defined(SCI_NAMESPACE)
|
2015-01-17 09:41:37 +03:00
|
|
|
|
|
|
|
#include "vs_ocornut_imgui.bin.h"
|
|
|
|
#include "fs_ocornut_imgui.bin.h"
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
void viewCallback(const ImDrawList* _parentList, const ImDrawCmd* _cmd);
|
|
|
|
|
2015-10-01 08:36:56 +03:00
|
|
|
class PlatformWindow : public ImGuiWM::PlatformWindow
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
2015-10-01 08:36:56 +03:00
|
|
|
typedef ImGuiWM::PlatformWindow Super;
|
2015-10-01 02:22:51 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
PlatformWindow(bool _mainWindow, bool _isDragWindow)
|
2015-10-01 08:36:56 +03:00
|
|
|
: ImGuiWM::PlatformWindow(_mainWindow, _isDragWindow)
|
2015-10-01 02:22:51 +03:00
|
|
|
, m_pos(0.0f, 0.0f)
|
|
|
|
, m_size(0.0f, 0.0f)
|
|
|
|
, m_drag(false)
|
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
if (!_mainWindow
|
|
|
|
&& !_isDragWindow)
|
|
|
|
{
|
|
|
|
m_window = entry::createWindow(0, 0, 640, 380);
|
|
|
|
extern void pwToWindow(entry::WindowHandle _handle, PlatformWindow* _pw);
|
|
|
|
pwToWindow(m_window, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_window.idx = 0;
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
2015-10-01 02:22:51 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 05:35:37 +03:00
|
|
|
virtual ~PlatformWindow()
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:36:56 +03:00
|
|
|
virtual bool Init(ImGuiWM::PlatformWindow* /*_parent*/) BX_OVERRIDE
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const ImVec2& GetPosition() const BX_OVERRIDE
|
|
|
|
{
|
|
|
|
return m_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const ImVec2& GetSize() const BX_OVERRIDE
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Show() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Hide() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetSize(const ImVec2& _size) BX_OVERRIDE
|
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
if (0 != m_window.idx
|
|
|
|
&& m_size.x != _size.x
|
|
|
|
&& m_size.y != _size.y)
|
|
|
|
{
|
|
|
|
entry::setWindowSize(m_window, int32_t(_size.x), int32_t(_size.y) );
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
2015-10-01 02:22:51 +03:00
|
|
|
m_size = _size;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetPosition(const ImVec2& _pos) BX_OVERRIDE
|
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
|
|
|
|
#if USE_ENTRY
|
|
|
|
if (0 != m_window.idx
|
|
|
|
&& m_pos.x != _pos.x
|
|
|
|
&& m_pos.y != _pos.y)
|
|
|
|
{
|
|
|
|
entry::setWindowPos(m_window, int32_t(_pos.x), int32_t(_pos.y) );
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
2015-10-01 02:22:51 +03:00
|
|
|
m_pos = _pos;
|
|
|
|
}
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
virtual void SetTitle(const char* _title) BX_OVERRIDE
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
entry::setWindowTitle(m_window, _title);
|
|
|
|
#else
|
|
|
|
BX_UNUSED(_title);
|
|
|
|
#endif // USE_ENTRY
|
2015-10-01 02:22:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PreUpdate() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
virtual void PaintBegin()
|
|
|
|
{
|
|
|
|
#if USE_ENTRY
|
|
|
|
if (!m_bIsDragWindow)
|
|
|
|
{
|
|
|
|
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
|
|
|
union { entry::WindowHandle handle; void* ptr; } cast = { m_window };
|
|
|
|
drawList->AddCallback(viewCallback, cast.ptr);
|
|
|
|
drawList->PushClipRect(ImVec4(0.0f, 0.0f, m_size.x, m_size.y) );
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
}
|
|
|
|
|
2015-10-01 02:22:51 +03:00
|
|
|
virtual void Paint() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
if (!m_bIsDragWindow)
|
|
|
|
{
|
|
|
|
Super::Paint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
virtual void PaintEnd()
|
|
|
|
{
|
|
|
|
#if USE_ENTRY
|
|
|
|
if (!m_bIsDragWindow)
|
|
|
|
{
|
|
|
|
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
|
|
|
drawList->PopClipRect();
|
|
|
|
drawList->AddCallback(viewCallback, NULL);
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
}
|
|
|
|
|
2015-10-01 02:22:51 +03:00
|
|
|
virtual void Destroy() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StartDrag() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
m_drag = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StopDrag() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
m_drag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsDraging() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
return m_drag;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ImVec2 m_pos;
|
|
|
|
ImVec2 m_size;
|
|
|
|
bool m_drag;
|
2015-10-09 08:46:17 +03:00
|
|
|
|
|
|
|
#if USE_ENTRY
|
|
|
|
entry::WindowHandle m_window;
|
|
|
|
#endif // USE_ENTRY
|
2015-10-01 02:22:51 +03:00
|
|
|
};
|
|
|
|
|
2015-10-01 08:36:56 +03:00
|
|
|
class WindowManager : public ImGuiWM::WindowManager
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
2015-10-01 08:36:56 +03:00
|
|
|
typedef ImGuiWM::WindowManager Super;
|
2015-10-01 02:22:51 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
WindowManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-19 05:35:37 +03:00
|
|
|
virtual ~WindowManager()
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-10-01 08:36:56 +03:00
|
|
|
virtual ImGuiWM::PlatformWindow* CreatePlatformWindow(bool _main, ImGuiWM::PlatformWindow* _parent, bool _isDragWindow) BX_OVERRIDE
|
2015-10-01 02:22:51 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
#else
|
|
|
|
if (!_main
|
|
|
|
&& !_isDragWindow)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
2015-10-05 21:36:14 +03:00
|
|
|
PlatformWindow* window = new (ImGui::MemAlloc(sizeof(PlatformWindow) ) ) PlatformWindow(_main, _isDragWindow);
|
2015-10-01 02:22:51 +03:00
|
|
|
window->Init(_parent);
|
2015-10-01 08:36:56 +03:00
|
|
|
return static_cast<ImGuiWM::PlatformWindow*>(window);
|
2015-10-01 02:22:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void LogFormatted(const char* _str) BX_OVERRIDE
|
|
|
|
{
|
|
|
|
BX_TRACE("%s", _str); BX_UNUSED(_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void InternalRun() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
PreUpdate();
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
struct OcornutImguiContext
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-04-16 06:00:15 +03:00
|
|
|
static void* memAlloc(size_t _size);
|
|
|
|
static void memFree(void* _ptr);
|
2015-10-09 08:46:17 +03:00
|
|
|
static void renderDrawLists(ImDrawData* _drawData);
|
2015-04-16 06:00:15 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
void render(ImDrawData* _drawData)
|
2015-01-20 04:16:59 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
const ImGuiIO& io = ImGui::GetIO();
|
|
|
|
const float width = io.DisplaySize.x;
|
|
|
|
const float height = io.DisplaySize.y;
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
{
|
|
|
|
float ortho[16];
|
|
|
|
bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);
|
|
|
|
bgfx::setViewTransform(m_viewId, NULL, ortho);
|
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
for (uint32_t ii = 1; ii < BX_COUNTOF(m_window); ++ii)
|
|
|
|
{
|
|
|
|
Window& window = m_window[ii];
|
|
|
|
if (bgfx::isValid(window.m_fbh) )
|
|
|
|
{
|
|
|
|
const uint8_t viewId = window.m_viewId;
|
|
|
|
bgfx::setViewClear(viewId
|
|
|
|
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
|
|
|
|
, 0x303030ff + rand()
|
|
|
|
, 1.0f
|
|
|
|
, 0
|
|
|
|
);
|
|
|
|
bgfx::setViewFrameBuffer(viewId, window.m_fbh);
|
|
|
|
bgfx::setViewRect(viewId
|
|
|
|
, 0
|
|
|
|
, 0
|
|
|
|
, window.m_state.m_width
|
|
|
|
, window.m_state.m_height
|
|
|
|
);
|
|
|
|
float ortho[16];
|
|
|
|
bx::mtxOrtho(ortho
|
|
|
|
, 0.0f
|
|
|
|
, float(window.m_state.m_width)
|
|
|
|
, float(window.m_state.m_height)
|
|
|
|
, 0.0f
|
|
|
|
, -1.0f
|
|
|
|
, 1.0f
|
|
|
|
);
|
|
|
|
bgfx::setViewTransform(viewId
|
|
|
|
, NULL
|
|
|
|
, ortho
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
// Render command lists
|
2015-10-09 08:46:17 +03:00
|
|
|
for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
|
2015-01-20 07:34:54 +03:00
|
|
|
{
|
2015-01-20 04:16:59 +03:00
|
|
|
bgfx::TransientVertexBuffer tvb;
|
2015-07-15 20:53:37 +03:00
|
|
|
bgfx::TransientIndexBuffer tib;
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
const ImDrawList* drawList = _drawData->CmdLists[ii];
|
|
|
|
uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
|
|
|
|
uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
|
2015-01-18 23:58:56 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
if (!bgfx::checkAvailTransientVertexBuffer(numVertices, m_decl)
|
|
|
|
|| !bgfx::checkAvailTransientIndexBuffer(numIndices) )
|
2015-01-20 07:34:54 +03:00
|
|
|
{
|
2015-01-20 04:16:59 +03:00
|
|
|
// not enough space in transient buffer just quit drawing the rest...
|
|
|
|
break;
|
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_decl);
|
|
|
|
bgfx::allocTransientIndexBuffer(&tib, numIndices);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
ImDrawVert* verts = (ImDrawVert*)tvb.data;
|
2015-10-09 08:46:17 +03:00
|
|
|
memcpy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-07-15 20:53:37 +03:00
|
|
|
ImDrawIdx* indices = (ImDrawIdx*)tib.data;
|
2015-10-09 08:46:17 +03:00
|
|
|
memcpy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
|
2015-07-15 19:52:17 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
uint32_t offset = 0;
|
|
|
|
for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
|
2015-01-20 07:34:54 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
if (cmd->UserCallback)
|
2015-07-15 20:53:37 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
cmd->UserCallback(drawList, cmd);
|
2015-07-15 20:53:37 +03:00
|
|
|
}
|
2015-10-09 08:46:17 +03:00
|
|
|
else if (0 != cmd->ElemCount)
|
2015-06-03 00:28:22 +03:00
|
|
|
{
|
2015-10-10 01:37:22 +03:00
|
|
|
uint64_t state = 0
|
|
|
|
| BGFX_STATE_RGB_WRITE
|
|
|
|
| BGFX_STATE_ALPHA_WRITE
|
|
|
|
| BGFX_STATE_MSAA
|
|
|
|
;
|
|
|
|
|
|
|
|
bgfx::TextureHandle th = m_texture;
|
|
|
|
|
|
|
|
if (NULL != cmd->TextureId)
|
|
|
|
{
|
|
|
|
union { ImTextureID ptr; struct { uint16_t flags; bgfx::TextureHandle handle; } s; } texture = { cmd->TextureId };
|
|
|
|
state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
|
|
|
|
? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
|
|
|
|
: BGFX_STATE_NONE
|
|
|
|
;
|
|
|
|
th = texture.s.handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
|
|
|
|
}
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
const uint16_t xx = uint16_t(bx::fmax(cmd->ClipRect.x, 0.0f) );
|
|
|
|
const uint16_t yy = uint16_t(bx::fmax(cmd->ClipRect.y, 0.0f) );
|
|
|
|
bgfx::setScissor(xx, yy
|
|
|
|
, uint16_t(bx::fmin(cmd->ClipRect.z, 65535.0f)-xx)
|
|
|
|
, uint16_t(bx::fmin(cmd->ClipRect.w, 65535.0f)-yy)
|
|
|
|
);
|
|
|
|
|
2015-10-10 01:37:22 +03:00
|
|
|
bgfx::setState(state);
|
|
|
|
bgfx::setTexture(0, s_tex, th);
|
2015-10-09 08:46:17 +03:00
|
|
|
bgfx::setVertexBuffer(&tvb, 0, numVertices);
|
|
|
|
bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount);
|
|
|
|
bgfx::submit(m_viewId, m_program);
|
2015-06-03 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
offset += cmd->ElemCount;
|
2015-01-20 04:16:59 +03:00
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:00:15 +03:00
|
|
|
void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
|
2015-01-20 04:16:59 +03:00
|
|
|
{
|
2015-02-22 02:51:03 +03:00
|
|
|
m_viewId = 255;
|
2015-04-16 06:00:15 +03:00
|
|
|
m_allocator = _allocator;
|
2015-07-15 20:53:37 +03:00
|
|
|
m_lastScroll = 0;
|
2015-07-18 05:57:24 +03:00
|
|
|
m_last = bx::getHPCounter();
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
2015-04-16 06:00:15 +03:00
|
|
|
io.RenderDrawListsFn = renderDrawLists;
|
|
|
|
if (NULL != m_allocator)
|
|
|
|
{
|
|
|
|
io.MemAllocFn = memAlloc;
|
|
|
|
io.MemFreeFn = memFree;
|
|
|
|
}
|
2015-07-18 05:57:24 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
io.DisplaySize = ImVec2(1280.0f, 720.0f);
|
2015-07-18 05:20:10 +03:00
|
|
|
io.DeltaTime = 1.0f / 60.0f;
|
2015-04-05 10:04:16 +03:00
|
|
|
io.IniFilename = NULL;
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-06-10 19:53:09 +03:00
|
|
|
#if defined(SCI_NAMESPACE)
|
|
|
|
io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
|
|
|
|
io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
|
|
|
|
io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
|
|
|
|
io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
|
|
|
|
io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
|
|
|
|
io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
|
|
|
|
io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
|
|
|
|
io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
|
|
|
|
io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
|
|
|
|
io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
|
|
|
|
io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
|
|
|
|
io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
|
|
|
|
io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
|
|
|
|
io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
|
|
|
|
io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
|
|
|
|
io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
|
|
|
|
io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
|
|
|
|
#endif // defined(SCI_NAMESPACE)
|
2015-06-03 00:28:22 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
const bgfx::Memory* vsmem;
|
|
|
|
const bgfx::Memory* fsmem;
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-07-17 06:28:43 +03:00
|
|
|
switch (bgfx::getRendererType() )
|
2015-01-20 07:34:54 +03:00
|
|
|
{
|
2015-01-20 04:16:59 +03:00
|
|
|
case bgfx::RendererType::Direct3D9:
|
2015-07-17 06:28:43 +03:00
|
|
|
vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9) );
|
|
|
|
fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9) );
|
2015-01-20 04:16:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case bgfx::RendererType::Direct3D11:
|
2015-02-17 08:58:13 +03:00
|
|
|
case bgfx::RendererType::Direct3D12:
|
2015-07-17 06:28:43 +03:00
|
|
|
vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11) );
|
|
|
|
fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11) );
|
2015-01-20 04:16:59 +03:00
|
|
|
break;
|
|
|
|
|
2015-08-14 16:12:44 +03:00
|
|
|
case bgfx::RendererType::Metal:
|
|
|
|
vsmem = bgfx::makeRef(vs_ocornut_imgui_mtl, sizeof(vs_ocornut_imgui_mtl) );
|
|
|
|
fsmem = bgfx::makeRef(fs_ocornut_imgui_mtl, sizeof(fs_ocornut_imgui_mtl) );
|
|
|
|
break;
|
2015-08-20 07:51:25 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
default:
|
2015-07-17 06:28:43 +03:00
|
|
|
vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl) );
|
|
|
|
fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl) );
|
2015-01-20 04:16:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
|
|
|
|
bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
|
|
|
|
m_program = bgfx::createProgram(vsh, fsh, true);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
m_decl
|
|
|
|
.begin()
|
2015-10-01 06:02:59 +03:00
|
|
|
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
|
2015-01-20 04:16:59 +03:00
|
|
|
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
2015-10-01 06:02:59 +03:00
|
|
|
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
2015-01-20 04:16:59 +03:00
|
|
|
.end();
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-05-29 01:27:00 +03:00
|
|
|
s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-23 08:01:09 +03:00
|
|
|
uint8_t* data;
|
|
|
|
int32_t width;
|
|
|
|
int32_t height;
|
2015-02-01 09:28:51 +03:00
|
|
|
void* font = ImGui::MemAlloc(_size);
|
2015-01-23 08:01:09 +03:00
|
|
|
memcpy(font, _data, _size);
|
2015-02-01 09:28:51 +03:00
|
|
|
io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-23 08:01:09 +03:00
|
|
|
io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-07-17 06:28:43 +03:00
|
|
|
m_texture = bgfx::createTexture2D( (uint16_t)width
|
2015-01-23 08:01:09 +03:00
|
|
|
, (uint16_t)height
|
2015-01-20 04:16:59 +03:00
|
|
|
, 1
|
|
|
|
, bgfx::TextureFormat::BGRA8
|
2015-07-15 19:52:17 +03:00
|
|
|
, 0
|
2015-01-23 08:01:09 +03:00
|
|
|
, bgfx::copy(data, width*height*4)
|
2015-01-20 04:16:59 +03:00
|
|
|
);
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-02-01 06:08:13 +03:00
|
|
|
ImGuiStyle& style = ImGui::GetStyle();
|
|
|
|
style.FrameRounding = 4.0f;
|
2015-10-01 02:22:51 +03:00
|
|
|
|
|
|
|
m_wm = BX_NEW(m_allocator, WindowManager);
|
|
|
|
m_wm->Init();
|
2015-10-02 09:00:45 +03:00
|
|
|
|
2015-10-09 09:01:11 +03:00
|
|
|
#if 0
|
2015-10-02 09:00:45 +03:00
|
|
|
{
|
|
|
|
class Window : public ImGuiWM::Window
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Window(const char* _title)
|
|
|
|
: ImGuiWM::Window()
|
|
|
|
{
|
|
|
|
SetTitle(_title);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnGui() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
class WindowX : public ImGuiWM::Window
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WindowX(const char* _title)
|
|
|
|
: ImGuiWM::Window()
|
|
|
|
{
|
|
|
|
SetTitle(_title);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnGui() BX_OVERRIDE
|
|
|
|
{
|
|
|
|
#if defined(SCI_NAMESPACE) && 0
|
|
|
|
bool opened = true;
|
|
|
|
ImGuiScintilla("Scintilla Editor", &opened, ImVec2(640.0f, 480.0f) );
|
|
|
|
#endif // 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Window* w0 = new Window("test");
|
|
|
|
WindowX* w1 = new WindowX("abcd");
|
|
|
|
Window* w2 = new Window("xyzw");
|
|
|
|
Window* w3 = new Window("0123");
|
2015-10-02 09:00:45 +03:00
|
|
|
|
|
|
|
m_wm->Dock(w0);
|
|
|
|
m_wm->DockWith(w1, w0, ImGuiWM::E_DOCK_ORIENTATION_RIGHT);
|
|
|
|
m_wm->DockWith(w2, w1, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM);
|
2015-10-03 07:10:18 +03:00
|
|
|
m_wm->DockWith(w3, w0, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM);
|
2015-10-02 09:00:45 +03:00
|
|
|
}
|
|
|
|
#endif // 0
|
2015-11-04 22:04:47 +03:00
|
|
|
|
|
|
|
ImGui::NewFrame();
|
2015-01-20 04:16:59 +03:00
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
void destroy()
|
|
|
|
{
|
2015-10-01 02:22:51 +03:00
|
|
|
m_wm->Exit();
|
|
|
|
BX_DELETE(m_allocator, m_wm);
|
2015-04-16 06:00:15 +03:00
|
|
|
ImGui::Shutdown();
|
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
bgfx::destroyUniform(s_tex);
|
|
|
|
bgfx::destroyTexture(m_texture);
|
|
|
|
bgfx::destroyProgram(m_program);
|
2015-04-16 06:00:15 +03:00
|
|
|
|
|
|
|
m_allocator = NULL;
|
2015-01-20 04:16:59 +03:00
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-06-10 17:53:50 +03:00
|
|
|
void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
|
2015-01-20 04:16:59 +03:00
|
|
|
{
|
2015-10-09 08:46:17 +03:00
|
|
|
m_viewId = _viewId;
|
|
|
|
m_defaultViewId = _viewId;
|
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
2015-07-15 20:53:37 +03:00
|
|
|
if (_inputChar < 0x7f)
|
2015-06-10 19:53:09 +03:00
|
|
|
{
|
2015-07-15 20:53:37 +03:00
|
|
|
io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
|
2015-06-10 19:53:09 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 06:28:43 +03:00
|
|
|
io.DisplaySize = ImVec2( (float)_width, (float)_height);
|
2015-07-18 05:57:24 +03:00
|
|
|
|
|
|
|
const int64_t now = bx::getHPCounter();
|
|
|
|
const int64_t frameTime = now - m_last;
|
|
|
|
m_last = now;
|
|
|
|
const double freq = double(bx::getHPFrequency() );
|
|
|
|
io.DeltaTime = float(frameTime/freq);
|
|
|
|
|
2015-07-17 06:28:43 +03:00
|
|
|
io.MousePos = ImVec2( (float)_mx, (float)_my);
|
2015-01-20 04:16:59 +03:00
|
|
|
io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
|
2015-06-10 19:53:09 +03:00
|
|
|
io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
|
2015-08-26 08:59:20 +03:00
|
|
|
io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
|
2015-06-10 19:53:09 +03:00
|
|
|
io.MouseWheel = (float)(_scroll - m_lastScroll);
|
|
|
|
m_lastScroll = _scroll;
|
|
|
|
|
|
|
|
#if defined(SCI_NAMESPACE)
|
|
|
|
uint8_t modifiers = inputGetModifiersState();
|
|
|
|
io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
|
|
|
|
io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
|
|
|
|
io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
|
|
|
|
for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
|
|
|
|
{
|
|
|
|
io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
|
|
|
|
}
|
|
|
|
#endif // defined(SCI_NAMESPACE)
|
2015-06-03 00:28:22 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
ImGui::NewFrame();
|
2015-02-01 06:08:13 +03:00
|
|
|
|
2015-08-26 06:44:36 +03:00
|
|
|
#if 0
|
|
|
|
ImGui::ShowTestWindow(); //Debug only.
|
|
|
|
#endif // 0
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
extern void ShowExampleAppCustomNodeGraph(bool* opened);
|
|
|
|
bool opened = true;
|
|
|
|
ShowExampleAppCustomNodeGraph(&opened);
|
|
|
|
#endif // 0
|
2015-01-20 04:16:59 +03:00
|
|
|
}
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
void endFrame()
|
|
|
|
{
|
2015-10-01 02:22:51 +03:00
|
|
|
m_wm->Run();
|
2015-01-20 04:16:59 +03:00
|
|
|
ImGui::Render();
|
|
|
|
}
|
|
|
|
|
2015-04-16 06:00:15 +03:00
|
|
|
bx::AllocatorI* m_allocator;
|
2015-01-20 04:16:59 +03:00
|
|
|
bgfx::VertexDecl m_decl;
|
|
|
|
bgfx::ProgramHandle m_program;
|
|
|
|
bgfx::TextureHandle m_texture;
|
|
|
|
bgfx::UniformHandle s_tex;
|
2015-10-01 02:22:51 +03:00
|
|
|
WindowManager* m_wm;
|
2015-07-18 05:57:24 +03:00
|
|
|
int64_t m_last;
|
2015-07-15 20:53:37 +03:00
|
|
|
int32_t m_lastScroll;
|
2015-07-18 05:57:24 +03:00
|
|
|
uint8_t m_viewId;
|
2015-10-09 08:46:17 +03:00
|
|
|
uint8_t m_defaultViewId;
|
|
|
|
|
|
|
|
#if USE_ENTRY
|
|
|
|
struct Window
|
|
|
|
{
|
|
|
|
Window()
|
|
|
|
{
|
|
|
|
m_fbh.idx = bgfx::invalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry::WindowState m_state;
|
|
|
|
PlatformWindow* m_pw;
|
|
|
|
bgfx::FrameBufferHandle m_fbh;
|
|
|
|
uint8_t m_viewId;
|
|
|
|
};
|
|
|
|
|
|
|
|
Window m_window[16];
|
|
|
|
#endif // USE_ENTRY
|
2015-01-20 04:16:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static OcornutImguiContext s_ctx;
|
2015-01-17 09:41:37 +03:00
|
|
|
|
2015-10-09 08:46:17 +03:00
|
|
|
#if USE_ENTRY
|
|
|
|
|
|
|
|
void viewCallback(const ImDrawList* /*_parentList*/, const ImDrawCmd* _cmd)
|
|
|
|
{
|
|
|
|
union { void* ptr; entry::WindowHandle handle; } cast = { _cmd->UserCallbackData };
|
|
|
|
|
|
|
|
if (0 != cast.handle.idx)
|
|
|
|
{
|
|
|
|
s_ctx.m_viewId = s_ctx.m_window[cast.handle.idx].m_viewId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_ctx.m_viewId = s_ctx.m_defaultViewId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwToWindow(entry::WindowHandle _handle, PlatformWindow* _pw)
|
|
|
|
{
|
|
|
|
s_ctx.m_window[_handle.idx].m_pw = _pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void imguiUpdateWindow(const entry::WindowState& _state)
|
|
|
|
{
|
|
|
|
OcornutImguiContext::Window& window = s_ctx.m_window[_state.m_handle.idx];
|
|
|
|
|
|
|
|
if (window.m_state.m_nwh != _state.m_nwh
|
|
|
|
|| (window.m_state.m_width != _state.m_width
|
|
|
|
|| window.m_state.m_height != _state.m_height) )
|
|
|
|
{
|
|
|
|
// When window changes size or native window handle changed
|
|
|
|
// frame buffer must be recreated.
|
|
|
|
if (bgfx::isValid(window.m_fbh) )
|
|
|
|
{
|
|
|
|
bgfx::destroyFrameBuffer(window.m_fbh);
|
|
|
|
window.m_fbh.idx = bgfx::invalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != _state.m_nwh)
|
|
|
|
{
|
|
|
|
window.m_fbh = bgfx::createFrameBuffer(_state.m_nwh, _state.m_width, _state.m_height);
|
|
|
|
window.m_viewId = 200 + _state.m_handle.idx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.m_viewId = s_ctx.m_defaultViewId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&window.m_state, &_state, sizeof(entry::WindowState) );
|
|
|
|
}
|
|
|
|
#endif // USE_ENTRY
|
|
|
|
|
2015-04-16 06:00:15 +03:00
|
|
|
void* OcornutImguiContext::memAlloc(size_t _size)
|
|
|
|
{
|
|
|
|
return BX_ALLOC(s_ctx.m_allocator, _size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OcornutImguiContext::memFree(void* _ptr)
|
|
|
|
{
|
|
|
|
BX_FREE(s_ctx.m_allocator, _ptr);
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:52:17 +03:00
|
|
|
void OcornutImguiContext::renderDrawLists(ImDrawData* draw_data)
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-07-15 19:52:17 +03:00
|
|
|
s_ctx.render(draw_data);
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 06:00:15 +03:00
|
|
|
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-04-16 06:00:15 +03:00
|
|
|
s_ctx.create(_data, _size, _fontSize, _allocator);
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
void IMGUI_destroy()
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-01-20 04:16:59 +03:00
|
|
|
s_ctx.destroy();
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:53:50 +03:00
|
|
|
void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-06-10 17:53:50 +03:00
|
|
|
s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|
|
|
|
|
2015-01-20 04:16:59 +03:00
|
|
|
void IMGUI_endFrame()
|
2015-01-17 09:41:37 +03:00
|
|
|
{
|
2015-01-20 04:16:59 +03:00
|
|
|
s_ctx.endFrame();
|
2015-01-17 09:41:37 +03:00
|
|
|
}
|