bgfx/examples/common/imgui/imgui.cpp

466 lines
13 KiB
C++
Raw Normal View History

/*
* Copyright 2014-2015 Daniel Collin. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include <bgfx/bgfx.h>
2016-12-05 07:11:10 +03:00
#include <bgfx/embedded_shader.h>
2015-04-16 06:00:15 +03:00
#include <bx/allocator.h>
2017-07-16 07:01:08 +03:00
#include <bx/math.h>
2015-07-18 05:57:24 +03:00
#include <bx/timer.h>
#include <ocornut-imgui/imgui.h>
2017-07-08 20:51:38 +03:00
#include "imgui.h"
#include "../bgfx_utils.h"
2015-06-10 19:53:09 +03:00
2015-10-09 08:46:17 +03:00
#ifndef USE_ENTRY
2015-11-12 03:43:32 +03:00
# if defined(SCI_NAMESPACE)
# define USE_ENTRY 1
# else
# define USE_ENTRY 0
# endif // defined(SCI_NAMESPACE)
2015-10-09 08:46:17 +03:00
#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)
#include "vs_ocornut_imgui.bin.h"
#include "fs_ocornut_imgui.bin.h"
2017-07-08 20:51:38 +03:00
#include "vs_imgui_image.bin.h"
#include "fs_imgui_image.bin.h"
#include "roboto_regular.ttf.h"
#include "robotomono_regular.ttf.h"
2016-05-28 22:12:59 +03:00
#include "icons_kenney.ttf.h"
#include "icons_font_awesome.ttf.h"
2016-12-05 07:11:10 +03:00
static const bgfx::EmbeddedShader s_embeddedShaders[] =
{
BGFX_EMBEDDED_SHADER(vs_ocornut_imgui),
BGFX_EMBEDDED_SHADER(fs_ocornut_imgui),
2017-07-08 20:51:38 +03:00
BGFX_EMBEDDED_SHADER(vs_imgui_image),
BGFX_EMBEDDED_SHADER(fs_imgui_image),
2016-12-06 03:55:49 +03:00
BGFX_EMBEDDED_SHADER_END()
2016-12-05 07:11:10 +03:00
};
2016-05-28 22:12:59 +03:00
struct FontRangeMerge
{
const void* data;
size_t size;
ImWchar ranges[3];
};
static FontRangeMerge s_fontRangeMerge[] =
{
{ s_iconsKenneyTtf, sizeof(s_iconsKenneyTtf), { ICON_MIN_KI, ICON_MAX_KI, 0 } },
{ s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), { ICON_MIN_FA, ICON_MAX_FA, 0 } },
2016-05-28 22:12:59 +03:00
};
2017-07-09 02:16:50 +03:00
static void* memAlloc(size_t _size);
static void memFree(void* _ptr);
struct OcornutImguiContext
{
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-10-09 08:46:17 +03:00
const ImGuiIO& io = ImGui::GetIO();
const float width = io.DisplaySize.x;
const float height = io.DisplaySize.y;
2017-07-08 23:09:44 +03:00
bgfx::setViewName(m_viewId, "ImGui");
bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential);
const bgfx::HMD* hmd = bgfx::getHMD();
const bgfx::Caps* caps = bgfx::getCaps();
if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
{
float proj[16];
bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
static float time = 0.0f;
time += 0.05f;
const float dist = 10.0f;
const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
float ortho[2][16];
const float viewOffset = width/4.0f;
const float viewWidth = width/2.0f;
bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, height, 0.0f, 0.0f, 1000.0f, offset0, caps->homogeneousDepth);
bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, height, 0.0f, 0.0f, 1000.0f, offset1, caps->homogeneousDepth);
bgfx::setViewTransform(m_viewId, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
bgfx::setViewRect(m_viewId, 0, 0, hmd->width, hmd->height);
}
else
2015-10-09 08:46:17 +03:00
{
float ortho[16];
2017-07-08 23:09:44 +03:00
bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
2015-10-09 08:46:17 +03:00
bgfx::setViewTransform(m_viewId, NULL, ortho);
2017-07-10 05:47:59 +03:00
bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) );
2015-10-09 08:46:17 +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
{
bgfx::TransientVertexBuffer tvb;
2015-07-15 20:53:37 +03:00
bgfx::TransientIndexBuffer tib;
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
if (!checkAvailTransientBuffers(numVertices, m_decl, numIndices) )
2015-01-20 07:34:54 +03:00
{
// not enough space in transient buffer just quit drawing the rest...
break;
}
2015-10-09 08:46:17 +03:00
bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_decl);
bgfx::allocTransientIndexBuffer(&tib, numIndices);
ImDrawVert* verts = (ImDrawVert*)tvb.data;
2017-02-09 06:55:31 +03:00
bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
2015-07-15 20:53:37 +03:00
ImDrawIdx* indices = (ImDrawIdx*)tib.data;
2017-02-09 06:55:31 +03:00
bx::memCopy(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;
2016-02-16 03:55:32 +03:00
bgfx::ProgramHandle program = m_program;
2015-10-10 01:37:22 +03:00
if (NULL != cmd->TextureId)
{
2016-02-16 03:55:32 +03:00
union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId };
2015-10-10 01:37:22 +03:00
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;
2016-02-16 03:55:32 +03:00
if (0 != texture.s.mip)
{
2017-07-08 20:51:38 +03:00
const float lodEnabled[4] = { float(texture.s.mip), 1.0f, 0.0f, 0.0f };
bgfx::setUniform(u_imageLodEnabled, lodEnabled);
program = m_imageProgram;
2016-02-16 03:55:32 +03:00
}
2015-10-10 01:37:22 +03:00
}
else
{
state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
}
2017-12-03 05:15:31 +03:00
const uint16_t xx = uint16_t(bx::max(cmd->ClipRect.x, 0.0f) );
const uint16_t yy = uint16_t(bx::max(cmd->ClipRect.y, 0.0f) );
2015-10-09 08:46:17 +03:00
bgfx::setScissor(xx, yy
2017-12-03 05:15:31 +03:00
, uint16_t(bx::min(cmd->ClipRect.z, 65535.0f)-xx)
, uint16_t(bx::min(cmd->ClipRect.w, 65535.0f)-yy)
2015-10-09 08:46:17 +03:00
);
2015-10-10 01:37:22 +03:00
bgfx::setState(state);
bgfx::setTexture(0, s_tex, th);
2017-05-14 21:48:59 +03:00
bgfx::setVertexBuffer(0, &tvb, 0, numVertices);
2015-10-09 08:46:17 +03:00
bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount);
2016-02-16 03:55:32 +03:00
bgfx::submit(cmd->ViewId, program);
2015-06-03 00:28:22 +03:00
}
2015-10-09 08:46:17 +03:00
offset += cmd->ElemCount;
}
}
}
void create(float _fontSize, bx::AllocatorI* _allocator)
{
2015-04-16 06:00:15 +03:00
m_allocator = _allocator;
2017-07-08 20:51:38 +03:00
if (NULL == _allocator)
{
static bx::DefaultAllocator allocator;
m_allocator = &allocator;
}
m_viewId = 255;
2015-07-15 20:53:37 +03:00
m_lastScroll = 0;
2015-07-18 05:57:24 +03:00
m_last = bx::getHPCounter();
ImGuiIO& io = ImGui::GetIO();
2015-04-16 06:00:15 +03:00
io.RenderDrawListsFn = renderDrawLists;
2017-07-08 20:51:38 +03:00
io.MemAllocFn = memAlloc;
io.MemFreeFn = memFree;
2015-07-18 05:57:24 +03:00
io.DisplaySize = ImVec2(1280.0f, 720.0f);
2015-07-18 05:20:10 +03:00
io.DeltaTime = 1.0f / 60.0f;
io.IniFilename = NULL;
2016-05-11 17:44:13 +03:00
setupStyle(true);
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
2016-12-05 07:11:10 +03:00
bgfx::RendererType::Enum type = bgfx::getRendererType();
m_program = bgfx::createProgram(
bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui")
, bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui")
, true
);
2017-07-08 20:51:38 +03:00
u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4);
m_imageProgram = bgfx::createProgram(
bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image")
, bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image")
, true
);
m_decl
.begin()
2015-10-01 06:02:59 +03:00
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.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)
.end();
2015-05-29 01:27:00 +03:00
s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
2015-01-23 08:01:09 +03:00
uint8_t* data;
int32_t width;
int32_t height;
2015-11-12 03:43:32 +03:00
{
2016-05-28 22:12:59 +03:00
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.MergeMode = false;
// config.MergeGlyphCenterV = true;
2016-05-28 22:12:59 +03:00
2017-07-19 08:43:50 +03:00
const ImWchar* ranges = io.Fonts->GetGlyphRangesCyrillic();
m_font[ImGui::Font::Regular] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoRegularTtf, sizeof(s_robotoRegularTtf), _fontSize, &config, ranges);
m_font[ImGui::Font::Mono ] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoMonoRegularTtf, sizeof(s_robotoMonoRegularTtf), _fontSize-3.0f, &config, ranges);
2016-05-28 22:12:59 +03:00
config.MergeMode = true;
config.DstFont = m_font[ImGui::Font::Regular];
2016-05-28 22:12:59 +03:00
for (uint32_t ii = 0; ii < BX_COUNTOF(s_fontRangeMerge); ++ii)
{
const FontRangeMerge& frm = s_fontRangeMerge[ii];
io.Fonts->AddFontFromMemoryTTF( (void*)frm.data
2016-06-01 03:45:58 +03:00
, (int)frm.size
2016-06-06 07:36:58 +03:00
, _fontSize-3.0f
2016-05-28 22:12:59 +03:00
, &config
, frm.ranges
);
}
2015-11-12 03:43:32 +03:00
}
2015-01-23 08:01:09 +03:00
io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
2016-08-20 07:05:37 +03:00
m_texture = bgfx::createTexture2D(
(uint16_t)width
2015-01-23 08:01:09 +03:00
, (uint16_t)height
2016-08-20 07:05:37 +03:00
, false
, 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)
);
ImGui::InitDockContext();
}
void destroy()
{
ImGui::ShutdownDockContext();
2015-04-16 06:00:15 +03:00
ImGui::Shutdown();
bgfx::destroy(s_tex);
bgfx::destroy(m_texture);
2017-07-08 20:51:38 +03:00
bgfx::destroy(u_imageLodEnabled);
bgfx::destroy(m_imageProgram);
bgfx::destroy(m_program);
2015-04-16 06:00:15 +03:00
m_allocator = NULL;
}
2016-05-11 17:44:13 +03:00
void setupStyle(bool _dark)
{
2016-05-12 19:42:44 +03:00
// Doug Binks' darl color scheme
2016-05-11 17:44:13 +03:00
// https://gist.github.com/dougbinks/8089b4bbaccaaf6fa204236978d165a9
ImGuiStyle& style = ImGui::GetStyle();
if (_dark)
{
ImGui::StyleColorsDark(&style);
2016-05-11 17:44:13 +03:00
}
else
{
ImGui::StyleColorsLight(&style);
}
style.FrameRounding = 4.0f;
2016-05-11 17:44:13 +03:00
}
2017-07-08 23:09:44 +03:00
void beginFrame(
int32_t _mx
, int32_t _my
, uint8_t _button
, int32_t _scroll
, int _width
, int _height
, char _inputChar
2017-11-28 02:57:31 +03:00
, bgfx::ViewId _viewId
2017-07-08 23:09:44 +03:00
)
{
2015-11-12 03:43:32 +03:00
m_viewId = _viewId;
2015-10-09 08:46:17 +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);
io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
2015-06-10 19:53:09 +03:00
io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
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
ImGui::NewFrame();
2015-11-12 03:43:32 +03:00
ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)_viewId);
2017-03-24 03:51:58 +03:00
ImGuizmo::BeginFrame();
}
void endFrame()
{
2015-11-12 03:43:32 +03:00
ImGui::PopStyleVar(1);
ImGui::Render();
}
2015-04-16 06:00:15 +03:00
bx::AllocatorI* m_allocator;
bgfx::VertexDecl m_decl;
bgfx::ProgramHandle m_program;
2017-07-08 20:51:38 +03:00
bgfx::ProgramHandle m_imageProgram;
bgfx::TextureHandle m_texture;
bgfx::UniformHandle s_tex;
2017-07-08 20:51:38 +03:00
bgfx::UniformHandle u_imageLodEnabled;
ImFont* m_font[ImGui::Font::Count];
2015-07-18 05:57:24 +03:00
int64_t m_last;
2015-07-15 20:53:37 +03:00
int32_t m_lastScroll;
2017-11-28 02:57:31 +03:00
bgfx::ViewId m_viewId;
};
static OcornutImguiContext s_ctx;
2017-07-09 02:16:50 +03:00
static void* memAlloc(size_t _size)
2015-04-16 06:00:15 +03:00
{
return BX_ALLOC(s_ctx.m_allocator, _size);
}
2017-07-09 02:16:50 +03:00
static void memFree(void* _ptr)
2015-04-16 06:00:15 +03:00
{
BX_FREE(s_ctx.m_allocator, _ptr);
}
2015-11-12 03:43:32 +03:00
void OcornutImguiContext::renderDrawLists(ImDrawData* _drawData)
{
2015-11-12 03:43:32 +03:00
s_ctx.render(_drawData);
}
2017-07-08 20:51:38 +03:00
void imguiCreate(float _fontSize, bx::AllocatorI* _allocator)
{
s_ctx.create(_fontSize, _allocator);
}
2017-07-08 20:51:38 +03:00
void imguiDestroy()
{
s_ctx.destroy();
}
2017-11-28 02:57:31 +03:00
void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, bgfx::ViewId _viewId)
{
s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
}
2017-07-08 23:09:44 +03:00
void imguiEndFrame()
{
s_ctx.endFrame();
}
namespace ImGui
{
void PushFont(Font::Enum _font)
{
PushFont(s_ctx.m_font[_font]);
}
} // namespace ImGui
2017-07-08 23:09:44 +03:00
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function"); // warning: int rect_width_compare(const void*, const void*) defined but not used
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wunknown-pragmas")
2017-10-14 21:04:45 +03:00
//BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-but-set-variable"); // warning: variable L1 set but not used
2017-07-08 23:09:44 +03:00
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits"); // warning: comparison is always true due to limited range of data type
2017-07-09 02:16:50 +03:00
#define STBTT_malloc(_size, _userData) memAlloc(_size)
#define STBTT_free(_ptr, _userData) memFree(_ptr)
2017-07-08 23:09:44 +03:00
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb/stb_rect_pack.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
BX_PRAGMA_DIAGNOSTIC_POP();