Examples: reference implementations honors drawlist user callbacks.

This commit is contained in:
ocornut 2015-03-09 15:26:58 +00:00
parent db5fc8a265
commit d5b7b8398f
4 changed files with 57 additions and 31 deletions

View File

@ -125,10 +125,17 @@ static void ImGui_ImplDX11_RenderDrawLists(ImDrawList** const cmd_lists, int cmd
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
if (pcmd->user_callback)
{
pcmd->user_callback(cmd_list, pcmd);
}
else
{
const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
g_pd3dDeviceContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->texture_id); g_pd3dDeviceContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->texture_id);
g_pd3dDeviceContext->RSSetScissorRects(1, &r); g_pd3dDeviceContext->RSSetScissorRects(1, &r);
g_pd3dDeviceContext->Draw(pcmd->vtx_count, vtx_offset); g_pd3dDeviceContext->Draw(pcmd->vtx_count, vtx_offset);
}
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }

View File

@ -94,10 +94,17 @@ static void ImGui_ImplDX9_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
if (pcmd->user_callback)
{
pcmd->user_callback(cmd_list, pcmd);
}
else
{
const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
g_pd3dDevice->SetTexture( 0, (LPDIRECT3DTEXTURE9)pcmd->texture_id ); g_pd3dDevice->SetTexture( 0, (LPDIRECT3DTEXTURE9)pcmd->texture_id );
g_pd3dDevice->SetScissorRect(&r); g_pd3dDevice->SetScissorRect(&r);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3);
}
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }

View File

@ -90,10 +90,17 @@ static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int
int vtx_offset = cmd_offset; int vtx_offset = cmd_offset;
const ImDrawCmd* pcmd_end = cmd_list->commands.end(); const ImDrawCmd* pcmd_end = cmd_list->commands.end();
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
{
if (pcmd->user_callback)
{
pcmd->user_callback(cmd_list, pcmd);
}
else
{ {
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id); glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id);
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y)); glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
}
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
cmd_offset = vtx_offset; cmd_offset = vtx_offset;
@ -327,12 +334,11 @@ void ImGui_ImplGlfwGL3_NewFrame()
mouse_y *= (float)display_h / h; mouse_y *= (float)display_h / h;
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io.MouseDown[0] = g_MousePressed[0] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_LEFT) != 0; // 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. for (int i = 0; i < 3; i++)
io.MouseDown[1] = g_MousePressed[1] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_RIGHT) != 0; {
io.MouseDown[2] = g_MousePressed[2] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_MIDDLE) != 0; io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // 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[0] = false; g_MousePressed[i] = false;
g_MousePressed[1] = false; }
g_MousePressed[2] = false;
io.MouseWheel = g_MouseWheel; io.MouseWheel = g_MouseWheel;
g_MouseWheel = 0.0f; g_MouseWheel = 0.0f;

View File

@ -67,9 +67,16 @@ static void ImGui_ImplGlfw_RenderDrawLists(ImDrawList** const cmd_lists, int cmd
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++)
{ {
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i];
if (pcmd->user_callback)
{
pcmd->user_callback(cmd_list, pcmd);
}
else
{
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id); glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id);
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y)); glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
}
vtx_offset += pcmd->vtx_count; vtx_offset += pcmd->vtx_count;
} }
} }
@ -231,12 +238,11 @@ void ImGui_ImplGlfw_NewFrame()
mouse_y *= (float)display_h / h; mouse_y *= (float)display_h / h;
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io.MouseDown[0] = g_MousePressed[0] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_LEFT) != 0; // 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. for (int i = 0; i < 3; i++)
io.MouseDown[1] = g_MousePressed[1] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_RIGHT) != 0; {
io.MouseDown[2] = g_MousePressed[2] || glfwGetMouseButton(g_Window, GLFW_MOUSE_BUTTON_MIDDLE) != 0; io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // 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[0] = false; g_MousePressed[i] = false;
g_MousePressed[1] = false; }
g_MousePressed[2] = false;
io.MouseWheel = g_MouseWheel; io.MouseWheel = g_MouseWheel;
g_MouseWheel = 0.0f; g_MouseWheel = 0.0f;