Style: added PushStyleVarX(), PushStyleVarY() helpers to modify only one component of a ImVec2 var.

+ tweak existing function to early out on error.
This commit is contained in:
ocornut
2024-08-22 13:46:54 +02:00
parent 521f84a3a9
commit bf75504d7a
6 changed files with 52 additions and 22 deletions

View File

@@ -3309,28 +3309,56 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val)
{
ImGuiContext& g = *GImGui;
const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1)
if (var_info->Type != ImGuiDataType_Float || var_info->Count != 1)
{
float* pvar = (float*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
float* pvar = (float*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
}
void ImGui::PushStyleVarX(ImGuiStyleVar idx, float val_x)
{
ImGuiContext& g = *GImGui;
const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
pvar->x = val_x;
}
void ImGui::PushStyleVarY(ImGuiStyleVar idx, float val_y)
{
ImGuiContext& g = *GImGui;
const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
pvar->y = val_y;
}
void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)
{
ImGuiContext& g = *GImGui;
const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2)
if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2)
{
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
}
void ImGui::PopStyleVar(int count)