ImFontAtlas: Moved mouse cursor data out of ImGuiContext, fix drawing them with multiple context. Also remove the last remaining undesirable dependency on GImGui in imgui_draw.cpp, finishing the work recently done with ImDrawListSharedData. Hurra! (#939)

This commit is contained in:
omar 2018-01-20 20:25:12 +01:00
parent 7e7c017b75
commit cd1409f4bf
4 changed files with 31 additions and 39 deletions

View File

@ -3069,17 +3069,16 @@ void ImGui::Render()
g.DrawDataBuilder.FlattenIntoSingleLayer(); g.DrawDataBuilder.FlattenIntoSingleLayer();
// Draw software mouse cursor if requested // Draw software mouse cursor if requested
if (g.IO.MouseDrawCursor) ImVec2 offset, size, uv[4];
if (g.IO.MouseDrawCursor && g.IO.Fonts->GetMouseCursorTexData(g.MouseCursor, &offset, &size, &uv[0], &uv[2]))
{ {
const ImGuiMouseCursorData& cursor_data = g.MouseCursorData[g.MouseCursor]; const ImVec2 pos = g.IO.MousePos - offset;
const ImVec2 pos = g.IO.MousePos - cursor_data.HotOffset;
const ImVec2 size = cursor_data.Size;
const ImTextureID tex_id = g.IO.Fonts->TexID; const ImTextureID tex_id = g.IO.Fonts->TexID;
g.OverlayDrawList.PushTextureID(tex_id); g.OverlayDrawList.PushTextureID(tex_id);
g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(1,0), pos+ImVec2(1,0) + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], IM_COL32(0,0,0,48)); // Shadow g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(1,0), pos+ImVec2(1,0) + size, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow
g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(2,0), pos+ImVec2(2,0) + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], IM_COL32(0,0,0,48)); // Shadow g.OverlayDrawList.AddImage(tex_id, pos+ImVec2(2,0), pos+ImVec2(2,0) + size, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow
g.OverlayDrawList.AddImage(tex_id, pos, pos + size, cursor_data.TexUvMin[1], cursor_data.TexUvMax[1], IM_COL32(0,0,0,255)); // Black border g.OverlayDrawList.AddImage(tex_id, pos, pos + size, uv[2], uv[3], IM_COL32(0,0,0,255)); // Black border
g.OverlayDrawList.AddImage(tex_id, pos, pos + size, cursor_data.TexUvMin[0], cursor_data.TexUvMax[0], IM_COL32(255,255,255,255)); // White fill g.OverlayDrawList.AddImage(tex_id, pos, pos + size, uv[0], uv[1], IM_COL32(255,255,255,255)); // White fill
g.OverlayDrawList.PopTextureID(); g.OverlayDrawList.PopTextureID();
} }
if (!g.OverlayDrawList.VtxBuffer.empty()) if (!g.OverlayDrawList.VtxBuffer.empty())

View File

@ -1611,9 +1611,12 @@ struct ImFontAtlas
IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList
IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x10000 to register a rectangle to map into a specific font. IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x10000 to register a rectangle to map into a specific font.
IMGUI_API void CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max);
const CustomRect* GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } const CustomRect* GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; }
// Internals
IMGUI_API void CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max);
IMGUI_API bool GetMouseCursorTexData(ImGuiMouseCursor cursor, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]);
//------------------------------------------- //-------------------------------------------
// Members // Members
//------------------------------------------- //-------------------------------------------

View File

@ -1352,7 +1352,6 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_Count_][
{ ImVec2(55,0), ImVec2(17,17), ImVec2( 9, 9) }, // ImGuiMouseCursor_ResizeNWSE { ImVec2(55,0), ImVec2(17,17), ImVec2( 9, 9) }, // ImGuiMouseCursor_ResizeNWSE
}; };
ImFontAtlas::ImFontAtlas() ImFontAtlas::ImFontAtlas()
{ {
TexID = NULL; TexID = NULL;
@ -1609,6 +1608,26 @@ void ImFontAtlas::CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, I
*out_uv_max = ImVec2((float)(rect->X + rect->Width) / TexWidth, (float)(rect->Y + rect->Height) / TexHeight); *out_uv_max = ImVec2((float)(rect->X + rect->Width) / TexWidth, (float)(rect->Y + rect->Height) / TexHeight);
} }
bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2])
{
if (cursor_type <= ImGuiMouseCursor_None || cursor_type >= ImGuiMouseCursor_Count_)
return false;
ImFontAtlas::CustomRect& r = CustomRects[CustomRectIds[0]];
IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID);
ImVec2 uv_scale(1.0f / TexWidth, 1.0f / TexHeight);
ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y);
ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1];
*out_size = size;
*out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2];
out_uv_border[0] = (pos) * uv_scale;
out_uv_border[1] = (pos + size) * uv_scale;
pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1;
out_uv_fill[0] = (pos) * uv_scale;
out_uv_fill[1] = (pos + size) * uv_scale;
return true;
}
bool ImFontAtlas::Build() bool ImFontAtlas::Build()
{ {
return ImFontAtlasBuildWithStbTruetype(this); return ImFontAtlasBuildWithStbTruetype(this);
@ -1891,22 +1910,6 @@ static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas)
} }
const ImVec2 tex_uv_scale(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); const ImVec2 tex_uv_scale(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);
atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * tex_uv_scale.x, (r.Y + 0.5f) * tex_uv_scale.y); atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * tex_uv_scale.x, (r.Y + 0.5f) * tex_uv_scale.y);
// Setup mouse cursors
for (int type = 0; type < ImGuiMouseCursor_Count_; type++)
{
ImGuiMouseCursorData& cursor_data = GImGui->MouseCursorData[type];
ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[type][0] + ImVec2((float)r.X, (float)r.Y);
const ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[type][1];
cursor_data.Type = type;
cursor_data.Size = size;
cursor_data.HotOffset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[type][2];
cursor_data.TexUvMin[0] = (pos) * tex_uv_scale;
cursor_data.TexUvMax[0] = (pos + size) * tex_uv_scale;
pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1;
cursor_data.TexUvMin[1] = (pos) * tex_uv_scale;
cursor_data.TexUvMax[1] = (pos + size) * tex_uv_scale;
}
} }
void ImFontAtlasBuildFinish(ImFontAtlas* atlas) void ImFontAtlasBuildFinish(ImFontAtlas* atlas)

View File

@ -38,7 +38,6 @@ struct ImGuiGroupData;
struct ImGuiMenuColumns; struct ImGuiMenuColumns;
struct ImGuiDrawContext; struct ImGuiDrawContext;
struct ImGuiTextEditState; struct ImGuiTextEditState;
struct ImGuiMouseCursorData;
struct ImGuiPopupRef; struct ImGuiPopupRef;
struct ImGuiWindow; struct ImGuiWindow;
struct ImGuiWindowSettings; struct ImGuiWindowSettings;
@ -382,16 +381,6 @@ struct ImGuiSettingsHandler
ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); }
}; };
// Mouse cursor data (used when io.MouseDrawCursor is set)
struct ImGuiMouseCursorData
{
ImGuiMouseCursor Type;
ImVec2 HotOffset;
ImVec2 Size;
ImVec2 TexUvMin[2];
ImVec2 TexUvMax[2];
};
// Storage for current popup stack // Storage for current popup stack
struct ImGuiPopupRef struct ImGuiPopupRef
{ {
@ -557,7 +546,6 @@ struct ImGuiContext
float ModalWindowDarkeningRatio; float ModalWindowDarkeningRatio;
ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays ImDrawList OverlayDrawList; // Optional software render of mouse cursors, if io.MouseDrawCursor is set + a few debug overlays
ImGuiMouseCursor MouseCursor; ImGuiMouseCursor MouseCursor;
ImGuiMouseCursorData MouseCursorData[ImGuiMouseCursor_Count_];
// Drag and Drop // Drag and Drop
bool DragDropActive; bool DragDropActive;
@ -645,7 +633,6 @@ struct ImGuiContext
OverlayDrawList._Data = &DrawListSharedData; OverlayDrawList._Data = &DrawListSharedData;
OverlayDrawList._OwnerName = "##Overlay"; // Give it a name for debugging OverlayDrawList._OwnerName = "##Overlay"; // Give it a name for debugging
MouseCursor = ImGuiMouseCursor_Arrow; MouseCursor = ImGuiMouseCursor_Arrow;
memset(MouseCursorData, 0, sizeof(MouseCursorData));
DragDropActive = false; DragDropActive = false;
DragDropSourceFlags = 0; DragDropSourceFlags = 0;