Как фиксить текст в ImGui?

niklive

Знающий
Регистрация
16.06.2022
Сообщения
165
Сделал вывод имени игрока, а текст плавает в зависимости от приближения.

Если боксы фиксятся так:
C++:
float width { (TransformScreenPos.y - BonesScreenHead.y) * 0.3f};

Тогда как можно пофиксить вывод текста, чтобы он был строго на своём месте?
 
тебе нужно сделать текст над боксами есп или что?
 
[TABLE] [TR] [TD]float RenderText(const std::string text, const ImVec2& position, float size, ImVec4 color, bool center, ImFont* DefaultFont)[/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD]{[/TD] [/TR] [TR] [TD][/TD] [TD] ImDrawList* Draw = ImGui::GetWindowDrawList();[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] std::stringstream stream(text);[/TD] [/TR] [TR] [TD][/TD] [TD] std::string line;[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] float y = 0.0f;[/TD] [/TR] [TR] [TD][/TD] [TD] int i = 0;[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] while (std::getline(stream, line))[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] ImVec2 textSize = DefaultFont->CalcTextSizeA(size, FLT_MAX, 0.0f, line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] if (center)[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { position.x - textSize.x / 2.0f, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD] else[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { position.x, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] y = position.y + textSize.y * (i + 1);[/TD] [/TR] [TR] [TD][/TD] [TD] i++;[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] return y;[/TD] [/TR] [TR] [TD][/TD] [TD]}[/TD] [/TR] [/TABLE]
 
[TABLE] [TR] [TD]float RenderText(const std::string text, const ImVec2& position, float size, ImVec4 color, bool center, ImFont* DefaultFont)[/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD]{[/TD] [/TR] [TR] [TD][/TD] [TD] ImDrawList* Draw = ImGui::GetWindowDrawList();[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] std::stringstream stream(text);[/TD] [/TR] [TR] [TD][/TD] [TD] std::string line;[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] float y = 0.0f;[/TD] [/TR] [TR] [TD][/TD] [TD] int i = 0;[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] while (std::getline(stream, line))[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] ImVec2 textSize = DefaultFont->CalcTextSizeA(size, FLT_MAX, 0.0f, line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] if (center)[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { position.x - textSize.x / 2.0f, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD] else[/TD] [/TR] [TR] [TD][/TD] [TD] {[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] Draw->AddText(DefaultFont, size, { position.x, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] y = position.y + textSize.y * (i + 1);[/TD] [/TR] [TR] [TD][/TD] [TD] i++;[/TD] [/TR] [TR] [TD][/TD] [TD] }[/TD] [/TR] [TR] [TD][/TD] [TD][/TD] [/TR] [TR] [TD][/TD] [TD] return y;[/TD] [/TR] [TR] [TD][/TD] [TD]}[/TD] [/TR] [/TABLE]
сорян не так сделал

float RenderText(const std::string text, const ImVec2& position, float size, ImVec4 color, bool center, ImFont* DefaultFont)
{
ImDrawList* Draw = ImGui::GetWindowDrawList();
std::stringstream stream(text);
std::string line;
float y = 0.0f;
int i = 0;
while (std::getline(stream, line))
{
ImVec2 textSize = DefaultFont->CalcTextSizeA(size, FLT_MAX, 0.0f, line.c_str());
if (center)
{
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { position.x - textSize.x / 2.0f, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());
}
else
{
Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { position.x, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());
}
y = position.y + textSize.y * (i + 1);
i++;
}
return y;
}
 
сорян не так сделал

float RenderText(const std::string text, const ImVec2& position, float size, ImVec4 color, bool center, ImFont* DefaultFont)
{
ImDrawList* Draw = ImGui::GetWindowDrawList();
std::stringstream stream(text);
std::string line;
float y = 0.0f;
int i = 0;
while (std::getline(stream, line))
{
ImVec2 textSize = DefaultFont->CalcTextSizeA(size, FLT_MAX, 0.0f, line.c_str());
if (center)
{
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x - textSize.x / 2.0f) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { position.x - textSize.x / 2.0f, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());
}
else
{
Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) + 1.0f, (position.y + textSize.y * i) - 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { (position.x) - 1.0f, (position.y + textSize.y * i) + 1.0f }, ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, color.w)), line.c_str());
Draw->AddText(DefaultFont, size, { position.x, position.y + textSize.y * i }, ImGui::GetColorU32(color), line.c_str());
}
y = position.y + textSize.y * (i + 1);
i++;
}
return y;
}
Напиши свой дс
 
Назад
Верх Низ