ImGUI improvements + font resizable

This commit is contained in:
LightCat 2019-03-11 18:41:16 +01:00
parent d2ba19aa0e
commit a2b390f314
6 changed files with 86 additions and 49 deletions

View File

@ -18,6 +18,8 @@
<AutoVariable width="fill" target="visual.snowflakes.fall-speed.max" label="Maximum fall speed"/>
<AutoVariable width="fill" target="visual.snowflakes.sideways-speed.min" label="Minimum sidemove speed"/>
<AutoVariable width="fill" target="visual.snowflakes.sideways-speed.max" label="Maximum sidemove speed"/>
<AutoVariable width="fill" target="visual.font_size.esp" label="Main font size"/>
<AutoVariable width="fill" target="visual.font_size.center_size" label="Center string font size"/>
</List>
</Box>
</Tab>

View File

@ -19,6 +19,7 @@
#include <string>
#include <memory>
#include <vector>
#include <map>
class CachedEntity;
class Vector;
@ -34,14 +35,14 @@ struct font
font(std::string path, int fontsize, bool outline = false) : size{ fontsize }, path{ path }, outline{ outline }
{
}
unsigned int id;
std::string path;
int size;
bool init = false;
bool outline = false;
operator unsigned int();
void stringSize(std::string string, float *x, float *y);
void changeSize(int new_font_size);
void Init();
std::map<int, unsigned int> size_map;
};
#elif ENABLE_IMGUI_DRAWING
typedef im_renderer::font font;

View File

@ -1,4 +1,4 @@
/*
/*
* drawing.cpp
*
* Created on: Mar 10, 2019
@ -38,6 +38,8 @@ std::array<rgba_t, 32> side_strings_colors{ colors::empty };
std::array<rgba_t, 32> center_strings_colors{ colors::empty };
size_t side_strings_count{ 0 };
size_t center_strings_count{ 0 };
static settings::Int esp_font_size{ "visual.font_size.esp", "13" };
static settings::Int center_font_size{ "visual.font_size.center_size", "14" };
void InitStrings()
{
@ -92,40 +94,71 @@ namespace fonts
#if ENABLE_ENGINE_DRAWING
font::operator unsigned int()
{
if (!init)
if (!size_map[size])
Init();
return id;
return size_map[size];
}
void font::Init()
{
size += 3;
static std::string filename;
filename.append("ab");
id = g_ISurface->CreateFont();
auto flag = vgui::ISurface::FONTFLAG_ANTIALIAS;
g_ISurface->SetFontGlyphSet(id, filename.c_str(), size, 500, 0, 0, flag);
size_map[size] = g_ISurface->CreateFont();
auto flag = vgui::ISurface::FONTFLAG_ANTIALIAS;
g_ISurface->SetFontGlyphSet(size_map[size], filename.c_str(), size, 500, 0, 0, flag);
g_ISurface->AddCustomFontFile(filename.c_str(), path.c_str());
init = true;
}
void font::stringSize(std::string string, float *x, float *y)
{
if (!init)
if (!size_map[size])
Init();
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
std::wstring ws = converter.from_bytes(string.c_str());
int w, h;
g_ISurface->GetTextSize(id, ws.c_str(), w, h);
g_ISurface->GetTextSize(size_map[size], ws.c_str(), w, h);
if (x)
*x = w;
if (y)
*y = h;
}
void font::changeSize(int new_font_size)
{
size = new_font_size;
if (!size_map[size])
Init();
}
#endif
std::unique_ptr<font> menu{ nullptr };
std::unique_ptr<font> esp{ nullptr };
std::unique_ptr<font> center_screen{ nullptr };
} // namespace fonts
static InitRoutine font_size([]() {
esp_font_size.installChangeCallback([](settings::VariableBase<int> &var, int after) {
if (after > 0 && after < 100)
{
#if !ENABLE_ENGINE_DRAWING && !ENABLE_IMGUI_DRAWING
fonts::esp_font_size->unload();
fonts::esp_font_size.reset(new fonts::font(DATA_PATH "/fonts/verasans.ttf", after));
#else
logging::Info("test");
fonts::esp->changeSize(after);
#endif
}
});
center_font_size.installChangeCallback([](settings::VariableBase<int> &var, int after) {
if (after > 0 && after < 100)
{
#if !ENABLE_ENGINE_DRAWING && !ENABLE_IMGUI_DRAWING
fonts::center_screen->unload();
fonts::center_screen.reset(new fonts::font(DATA_PATH "/fonts/verasans.ttf", after));
#else
logging::Info("test");
fonts::center_screen->changeSize(after);
#endif
}
});
});
namespace draw
{
@ -319,13 +352,13 @@ void RectangleTextured(float x, float y, float w, float h, rgba_t color, Texture
g_ISurface->DrawSetColor(color.r, color.g, color.b, color.a);
g_ISurface->DrawSetTexture(texture.get());
float tex_width = texture.getWidth();
float tex_width = texture.getWidth();
float tex_height = texture.getHeight();
Vector2D scr_top_left = { x, y };
Vector2D scr_top_right = { x + w, y };
Vector2D scr_top_left = { x, y };
Vector2D scr_top_right = { x + w, y };
Vector2D scr_bottom_right = { x + w, y + h };
Vector2D scr_botton_left = { x, y + w };
Vector2D scr_botton_left = { x, y + w };
if (angle != 0.0f)
{
@ -342,10 +375,10 @@ void RectangleTextured(float x, float y, float w, float h, rgba_t color, Texture
f(scr_bottom_right);
}
Vector2D tex_top_left = { tx / tex_width, ty / tex_height };
Vector2D tex_top_right = { (tx + tw) / tex_width, ty / tex_height };
Vector2D tex_top_left = { tx / tex_width, ty / tex_height };
Vector2D tex_top_right = { (tx + tw) / tex_width, ty / tex_height };
Vector2D tex_bottom_right = { (tx + tw) / tex_width, (ty + th) / tex_height };
Vector2D tex_botton_left = { tx / tex_width, (ty + th) / tex_height };
Vector2D tex_botton_left = { tx / tex_width, (ty + th) / tex_height };
// logging::Info("%f,%f %f,%f", tex_top_left.x, tex_top_left.y, tex_top_right.x, tex_top_right.y);
vertices[0].Init(scr_top_left, tex_top_left);

View File

@ -286,19 +286,19 @@ bool ImGui_ImplSdl_Init()
ImGuiStyle *style = &ImGui::GetStyle();
style->WindowPadding = ImVec2(15, 15);
style->WindowPadding = ImVec2(15, 15);
style->WindowRounding = 1.0f;
style->FramePadding = ImVec2(5, 5);
style->FramePadding = ImVec2(5, 5);
style->FrameRounding = 1.0f;
style->ItemSpacing = ImVec2(12, 8);
style->ItemInnerSpacing = ImVec2(6, 6);
style->IndentSpacing = 25.0f;
style->ScrollbarSize = 15.0f;
style->ItemSpacing = ImVec2(12, 8);
style->ItemInnerSpacing = ImVec2(6, 6);
style->IndentSpacing = 25.0f;
style->ScrollbarSize = 15.0f;
style->ScrollbarRounding = 1.0f;
style->GrabMinSize = 5.0f;
style->GrabMinSize = 5.0f;
style->GrabRounding = 1.0f;
style->Colors[ImGuiCol_Text] = ImVec4(0.80f, 0.80f, 0.83f, 1.00f);
@ -319,21 +319,21 @@ bool ImGui_ImplSdl_Init()
style->Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_CheckMark] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_Button] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_ButtonHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_ButtonActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_Header] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_HeaderHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_HeaderActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_Column] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ColumnHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_ColumnActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style->Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_CheckMark] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_Button] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_ButtonHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_ButtonActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_Header] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_HeaderHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_HeaderActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_Column] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ColumnHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_ColumnActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style->Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_PlotLines] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
style->Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f);
style->Colors[ImGuiCol_PlotHistogram] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
@ -388,7 +388,7 @@ void ImGui_ImplSdl_NewFrame(SDL_Window *window)
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
return;
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
/*ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
{
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
@ -399,7 +399,7 @@ void ImGui_ImplSdl_NewFrame(SDL_Window *window)
// Show OS mouse cursor
SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
SDL_ShowCursor(SDL_TRUE);
}
}*/
ImGui::NewFrame();
}
}

View File

@ -73,12 +73,12 @@ void font::changeSize(int new_font_size)
void font::rebuild()
{
ImGui_Impl_DestroyFontsTexture(font_atlas);
if (!size_map[size])
if (!size_map[new_size])
{
size_map[size] = font_atlas->AddFontFromFileTTF(path.c_str(), new_size, NULL, font_atlas->GetGlyphRangesDefault());
size_map[new_size] = font_atlas->AddFontFromFileTTF(path.c_str(), new_size, NULL, font_atlas->GetGlyphRangesDefault());
font_atlas->Build();
}
ImFont *font_new = size_map[size];
ImFont *font_new = size_map[new_size];
if (!font_new || !font_new->ContainerAtlas)
return; // what?
ImFontConfig *font_config = (ImFontConfig *) font_new->ConfigData;
@ -87,6 +87,7 @@ void font::rebuild()
ImGuiFreeType::BuildFontAtlas(font_new->ContainerAtlas, 0);
ImGui_Impl_CreateFontsTexture(font_new->ContainerAtlas);
needs_rebuild = false;
size = new_size;
}
Texture::Texture(std::string path) : path{ path }
@ -267,4 +268,4 @@ void circle(float x, float y, float radius, rgba_t color, float thickness, int s
buffers[currentBuffer]->AddCircle(ImVec2(x, y), radius, ImGui::GetColorU32(ImVec4(color.r, color.g, color.b, color.a)), steps, thickness);
}
} // namespace draw
} // namespace im_renderer
} // namespace im_renderer

View File

@ -77,8 +77,8 @@ Menu *Menu::instance{ nullptr };
namespace resource::font
{
// FIXME dynamic font change..
fonts::font base{ DATA_PATH "/menu/Verdana.ttf", 10 };
fonts::font bold{ DATA_PATH "/menu/VerdanaBold.ttf", 9 };
fonts::font base{ DATA_PATH "/menu/Verdana.ttf", 12 };
fonts::font bold{ DATA_PATH "/menu/VerdanaBold.ttf", 11 };
} // namespace resource::font
namespace style::colors