Manually merged #449.

This commit is contained in:
Branimir Karadžić 2015-07-15 09:52:17 -07:00
parent 5f1b3f2e66
commit af2bb3874d
2 changed files with 48 additions and 66 deletions

View file

@ -22,9 +22,9 @@ struct OcornutImguiContext
{ {
static void* memAlloc(size_t _size); static void* memAlloc(size_t _size);
static void memFree(void* _ptr); static void memFree(void* _ptr);
static void renderDrawLists(ImDrawList** const _lists, int _count); static void renderDrawLists(ImDrawData* draw_data);
void render(ImDrawList** const _lists, int _count) void render(ImDrawData* draw_data)
{ {
const float width = ImGui::GetIO().DisplaySize.x; const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y; const float height = ImGui::GetIO().DisplaySize.y;
@ -35,37 +35,42 @@ struct OcornutImguiContext
bgfx::setViewTransform(m_viewId, NULL, ortho); bgfx::setViewTransform(m_viewId, NULL, ortho);
// Render command lists // Render command lists
for (int32_t ii = 0; ii < _count; ++ii) for (int32_t ii = 0; ii < draw_data->CmdListsCount; ++ii)
{ {
bgfx::TransientVertexBuffer tvb; bgfx::TransientVertexBuffer tvb;
bgfx::TransientIndexBuffer tib;
const ImDrawList* cmd_list = _lists[ii]; const ImDrawList* cmd_list = draw_data->CmdLists[ii];
const ImDrawVert* vtx_buffer = cmd_list->vtx_buffer.begin(); uint32_t vtx_size = (uint32_t)cmd_list->VtxBuffer.size();
uint32_t vtx_size = (uint32_t)cmd_list->vtx_buffer.size(); uint32_t idx_size = (uint32_t)cmd_list->IdxBuffer.size();
if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl)) if (!bgfx::checkAvailTransientVertexBuffer(vtx_size, m_decl) || !bgfx::checkAvailTransientIndexBuffer(idx_size))
{ {
// not enough space in transient buffer just quit drawing the rest... // not enough space in transient buffer just quit drawing the rest...
break; break;
} }
bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl); bgfx::allocTransientVertexBuffer(&tvb, vtx_size, m_decl);
bgfx::allocTransientIndexBuffer(&tib, idx_size);
ImDrawVert* verts = (ImDrawVert*)tvb.data; ImDrawVert* verts = (ImDrawVert*)tvb.data;
memcpy(verts, vtx_buffer, vtx_size * sizeof(ImDrawVert)); memcpy(verts, cmd_list->VtxBuffer.begin(), vtx_size * sizeof(ImDrawVert));
uint32_t vtx_offset = 0; ImDrawIdx* indices = (ImDrawIdx*)tib.data;
const ImDrawCmd* pcmd_begin = cmd_list->commands.begin(); memcpy(indices, cmd_list->IdxBuffer.begin(), idx_size * sizeof(ImDrawIdx));
const ImDrawCmd* pcmd_end = cmd_list->commands.end();
uint32_t elem_offset = 0;
const ImDrawCmd* pcmd_begin = cmd_list->CmdBuffer.begin();
const ImDrawCmd* pcmd_end = cmd_list->CmdBuffer.end();
for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++) for (const ImDrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++)
{ {
if (pcmd->user_callback) if (pcmd->UserCallback)
{ {
pcmd->user_callback(cmd_list, pcmd); pcmd->UserCallback(cmd_list, pcmd);
vtx_offset += pcmd->vtx_count; elem_offset += pcmd->ElemCount;
continue; continue;
} }
if (0 == pcmd->vtx_count) if (0 == pcmd->ElemCount)
{ {
continue; continue;
} }
@ -76,23 +81,24 @@ struct OcornutImguiContext
| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
| BGFX_STATE_MSAA | BGFX_STATE_MSAA
); );
bgfx::setScissor(uint16_t(bx::fmax(pcmd->clip_rect.x, 0.0f)) bgfx::setScissor(uint16_t(bx::fmax(pcmd->ClipRect.x, 0.0f))
, uint16_t(bx::fmax(pcmd->clip_rect.y, 0.0f)) , uint16_t(bx::fmax(pcmd->ClipRect.y, 0.0f))
, uint16_t(bx::fmin(pcmd->clip_rect.z, 65535.0f)-bx::fmax(pcmd->clip_rect.x, 0.0f)) , uint16_t(bx::fmin(pcmd->ClipRect.z, 65535.0f)-bx::fmax(pcmd->ClipRect.x, 0.0f))
, uint16_t(bx::fmin(pcmd->clip_rect.w, 65535.0f)-bx::fmax(pcmd->clip_rect.y, 0.0f)) , uint16_t(bx::fmin(pcmd->ClipRect.w, 65535.0f)-bx::fmax(pcmd->ClipRect.y, 0.0f))
); );
union { void* ptr; bgfx::TextureHandle handle; } texture ={ pcmd->texture_id }; union { void* ptr; bgfx::TextureHandle handle; } texture = { pcmd->TextureId };
bgfx::setTexture(0, s_tex, 0 != texture.handle.idx bgfx::setTexture(0, s_tex, 0 != texture.handle.idx
? texture.handle ? texture.handle
: m_texture : m_texture
); );
bgfx::setVertexBuffer(&tvb, vtx_offset, pcmd->vtx_count); bgfx::setVertexBuffer(&tvb, 0, vtx_size);
bgfx::setIndexBuffer(&tib, elem_offset, pcmd->ElemCount);
bgfx::setProgram(m_program); bgfx::setProgram(m_program);
bgfx::submit(m_viewId); bgfx::submit(m_viewId);
vtx_offset += pcmd->vtx_count; elem_offset += pcmd->ElemCount;
} }
} }
} }
@ -101,7 +107,7 @@ struct OcornutImguiContext
{ {
m_viewId = 255; m_viewId = 255;
m_allocator = _allocator; m_allocator = _allocator;
m_lastScroll = 0; m_lastScroll = 0;
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.RenderDrawListsFn = renderDrawLists; io.RenderDrawListsFn = renderDrawLists;
@ -179,11 +185,11 @@ struct OcornutImguiContext
io.Fonts->GetTexDataAsRGBA32(&data, &width, &height); io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
m_texture = bgfx::createTexture2D((uint16_t)width m_texture = bgfx::createTexture2D( (uint16_t)width
, (uint16_t)height , (uint16_t)height
, 1 , 1
, bgfx::TextureFormat::BGRA8 , bgfx::TextureFormat::BGRA8
, BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT , 0
, bgfx::copy(data, width*height*4) , bgfx::copy(data, width*height*4)
); );
@ -206,9 +212,9 @@ struct OcornutImguiContext
{ {
m_viewId = _viewId; m_viewId = _viewId;
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (_inputChar < 0x7f) if (_inputChar < 0x7f)
{ {
io.AddInputCharacter(_inputChar); // ASCII or GTFO! :( io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
} }
io.DisplaySize = ImVec2((float)_width, (float)_height); io.DisplaySize = ImVec2((float)_width, (float)_height);
@ -246,7 +252,7 @@ struct OcornutImguiContext
bgfx::TextureHandle m_texture; bgfx::TextureHandle m_texture;
bgfx::UniformHandle s_tex; bgfx::UniformHandle s_tex;
uint8_t m_viewId; uint8_t m_viewId;
int32_t m_lastScroll; int32_t m_lastScroll;
}; };
static OcornutImguiContext s_ctx; static OcornutImguiContext s_ctx;
@ -261,9 +267,9 @@ void OcornutImguiContext::memFree(void* _ptr)
BX_FREE(s_ctx.m_allocator, _ptr); BX_FREE(s_ctx.m_allocator, _ptr);
} }
void OcornutImguiContext::renderDrawLists(ImDrawList** const _lists, int _count) void OcornutImguiContext::renderDrawLists(ImDrawData* draw_data)
{ {
s_ctx.render(_lists, _count); s_ctx.render(draw_data);
} }
void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator) void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)

View file

@ -80,8 +80,8 @@ static inline uint32_t makeRgba(uint32_t r, uint32_t g, uint32_t b, uint32_t a =
struct FontInt struct FontInt
{ {
ImFont* m_font; ImFont* m_font;
float m_scale; float m_scale;
float m_fontSize; float m_fontSize;
}; };
@ -236,7 +236,7 @@ public:
virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE virtual Scintilla::XYPOSITION WidthChar(Scintilla::Font& _font, char ch) BX_OVERRIDE
{ {
FontInt* fi = (FontInt*)_font.GetID(); FontInt* fi = (FontInt*)_font.GetID();
return fi->m_font->GetCharAdvance((unsigned int)ch) * fi->m_scale; return fi->m_font->GetCharAdvance((unsigned int)ch) * fi->m_scale;
} }
virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION Ascent(Scintilla::Font& _font) BX_OVERRIDE
@ -251,14 +251,14 @@ public:
return -fi->m_font->Descent * fi->m_scale; return -fi->m_font->Descent * fi->m_scale;
} }
virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE virtual Scintilla::XYPOSITION InternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
{ {
return 0; return 0;
} }
virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE virtual Scintilla::XYPOSITION ExternalLeading(Scintilla::Font& /*_font*/) BX_OVERRIDE
{ {
return 0; return 0;
} }
virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE virtual Scintilla::XYPOSITION Height(Scintilla::Font& _font) BX_OVERRIDE
@ -633,7 +633,7 @@ public:
return 0; return 0;
} }
intptr_t command(unsigned int _msg, uintptr_t _p0 = 0, intptr_t _p1 = 0) intptr_t command(unsigned int _msg, uintptr_t _p0 = 0, intptr_t _p1 = 0)
{ {
return WndProc(_msg, _p0, _p1); return WndProc(_msg, _p0, _p1);
} }
@ -741,7 +741,7 @@ public:
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y); Scintilla::Point pt = Scintilla::Point::FromInts( (int)io.MouseClickedPos[0].x, (int)io.MouseClickedPos[0].y);
ButtonDown(pt, (unsigned int)io.MouseDownTime[0], false, false, false); ButtonDown(pt, (unsigned int)io.MouseDownDuration[0], false, false, false);
} }
Tick(); Tick();
@ -854,7 +854,7 @@ namespace Scintilla
} }
void Font::Create(const FontParameters& fp) void Font::Create(const FontParameters& fp)
{ {
FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) ); FontInt* newFont = (FontInt*)ImGui::MemAlloc(sizeof(FontInt) );
fid = newFont; fid = newFont;
newFont->m_font = ImGui::GetIO().Fonts->Fonts[0]; newFont->m_font = ImGui::GetIO().Fonts->Fonts[0];
@ -1063,28 +1063,4 @@ namespace Scintilla
} // namespace Scintilla } // namespace Scintilla
ScintillaEditor* ImGuiScintilla(const char* _name, bool* _opened, const ImVec2& _size)
{
ScintillaEditor* sci = NULL;
if (ImGui::Begin(_name, _opened, _size) )
{
ImGuiStorage* storage = ImGui::GetStateStorage();
ImGuiID id = ImGui::GetID(_name);
sci = (ScintillaEditor*)storage->GetVoidPtr(id);
if (NULL == sci)
{
ImVec2 size = ImGui::GetWindowSize();
sci = ScintillaEditor::create(size.x, size.y);
storage->SetVoidPtr(id, (void*)sci);
}
sci->draw();
}
ImGui::End();
return sci;
}
#endif // defined(SCI_NAMESPACE) #endif // defined(SCI_NAMESPACE)