Skip to content

Commit

Permalink
Merge pull request #18 from wutipong/feature/font-list-scroll
Browse files Browse the repository at this point in the history
make the font list scroll to the selected item on open.
  • Loading branch information
wutipong authored Jul 18, 2023
2 parents 9b05dc4 + ea92a42 commit a3cec86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
33 changes: 19 additions & 14 deletions context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
#include <SDL2/SDL.h>
#include <filesystem>

constexpr int WIDTH = 800;
constexpr int HEIGHT = 600;
constexpr int MININUM_WIDTH = 800;
constexpr int MINIMUM_HEIGHT = 600;

struct Context {
SDL_Rect windowBound = {SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT};
bool debug = false;
std::string fontPath = std::filesystem::absolute("fonts").string();

SDL_Renderer *renderer = nullptr;

SDL_Color debugGlyphBoundColor = {0xFF, 0xFF, 0x80, 0xFF};
SDL_Color debugLineColor = {0xFF, 0x00, 0x00, 0xFF};
SDL_Color debugAscendColor = {0x40, 0x40, 0xFF, 0x80};
SDL_Color debugDescendColor = {0x40, 0xFF, 0x40, 0x80};
SDL_Color backgroundColor = {0x55, 0x40, 0xAA, 0xFF};
SDL_Rect windowBound{
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MININUM_WIDTH,
MINIMUM_HEIGHT,
};

bool debug{false};
std::string fontPath{std::filesystem::absolute("fonts").string()};

SDL_Renderer *renderer{nullptr};

const SDL_Color debugGlyphBoundColor{0xFF, 0xFF, 0x80, 0xFF};
const SDL_Color debugLineColor{0xFF, 0x00, 0x00, 0xFF};
const SDL_Color debugAscendColor{0x40, 0x40, 0xFF, 0x80};
const SDL_Color debugDescendColor{0x40, 0xFF, 0x40, 0x80};
const SDL_Color backgroundColor{0x80, 0x80, 0x80, 0xFF};
};

void SaveContext(const Context &ctx, const std::filesystem::path &path);
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char **argv) {
"font-render-tester", ctx.windowBound.x, ctx.windowBound.y,
ctx.windowBound.w, ctx.windowBound.h,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL);
SDL_SetWindowMinimumSize(window, WIDTH, HEIGHT);
SDL_SetWindowMinimumSize(window, MININUM_WIDTH, MINIMUM_HEIGHT);

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
ctx.renderer = renderer;
Expand Down
53 changes: 34 additions & 19 deletions main_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
#include <filesystem>
#include <imgui.h>

static std::array<float, 4> SDLColorToF4(const SDL_Color &color) {
namespace {
std::array<float, 4> SDLColorToF4(const SDL_Color &color) {
std::array<float, 4> output;
output[0] = (float)color.r / 255.0f;
output[1] = (float)color.g / 255.0f;
output[2] = (float)color.b / 255.0f;
output[3] = (float)color.a / 255.0f;
output[0] = static_cast<float>(color.r) / 255.0f;
output[1] = static_cast<float>(color.g) / 255.0f;
output[2] = static_cast<float>(color.b) / 255.0f;
output[3] = static_cast<float>(color.a) / 255.0f;

return output;
}
} // namespace

bool MainScene::Init(Context &context) {
if (!Font::Init())
Expand Down Expand Up @@ -51,9 +53,12 @@ void MainScene::Tick(Context &ctx) {

font.SetFontSize(fontSize);

SDL_Color sdlColor = {(uint8_t)(color[0] * 255.0f),
(uint8_t)(color[1] * 255.0f),
(uint8_t)(color[2] * 255.0f), 0xFF};
SDL_Color sdlColor = {
static_cast<uint8_t>(color[0] * 255.0f),
static_cast<uint8_t>(color[1] * 255.0f),
static_cast<uint8_t>(color[2] * 255.0f),
0xFF,
};

font.RenderText(ctx, std::string(buffer.data()), sdlColor,
languages[selectedLanguage].code,
Expand All @@ -72,20 +77,25 @@ void MainScene::DoUI(Context &context) {
if (ImGui::Button("Browse")) {
dirChooser.Open();
}
if (ImGui::Button("Re-scan")){
if (ImGui::Button("Re-scan")) {
fontPaths = ListFontFiles(context.fontPath);
}
}

if (ImGui::CollapsingHeader("Font", ImGuiTreeNodeFlags_DefaultOpen)) {
if (ImGui::BeginCombo(
"Font File",
(selectedFontIndex == -1)
? "<None>"
: fontPaths[selectedFontIndex].filename().string().c_str())) {
auto currentFile = selectedFontIndex == -1
? "<None>"
: fontPaths[selectedFontIndex].filename().string();

if (ImGui::BeginCombo("Font File", currentFile.c_str())) {
for (int i = 0; i < fontPaths.size(); i++) {
auto isSelected = i == selectedFontIndex;
if (isSelected) {
ImGui::SetItemDefaultFocus();
}

if (ImGui::Selectable(fontPaths[i].filename().string().c_str(),
i == selectedFontIndex)) {
isSelected)) {
newSelected = i;
}
}
Expand All @@ -96,10 +106,11 @@ void MainScene::DoUI(Context &context) {
auto familyName = font.GetFamilyName();
auto subFamily = font.GetSubFamilyName();

ImGui::InputText("Family Name", const_cast<char *>(familyName.c_str()),
familyName.size(), ImGuiInputTextFlags_ReadOnly);
ImGui::InputText("Sub Family Name", const_cast<char *>(subFamily.c_str()),
subFamily.size(), ImGuiInputTextFlags_ReadOnly);
ImGui::InputText("Family Name", familyName.data(), familyName.size(),
ImGuiInputTextFlags_ReadOnly);

ImGui::InputText("Sub Family Name", subFamily.data(), subFamily.size(),
ImGuiInputTextFlags_ReadOnly);
}

if (ImGui::CollapsingHeader("Parameters", ImGuiTreeNodeFlags_DefaultOpen)) {
Expand Down Expand Up @@ -168,21 +179,25 @@ void MainScene::DoUI(Context &context) {
ImGuiColorEditFlags_NoInputs |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip);

ImGui::ColorEdit4("Ascend", debugAscendColor.data(),
ImGuiColorEditFlags_NoInputs |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip);

ImGui::ColorEdit4("Descend", debugDescendColor.data(),
ImGuiColorEditFlags_NoInputs |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip);

ImGui::ColorEdit4("Baseline", debugLineColor.data(),
ImGuiColorEditFlags_NoInputs |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip);
}
}
ImGui::End();

dirChooser.Display();
if (dirChooser.HasSelected()) {
OnDirectorySelected(context, dirChooser.GetSelected());
Expand Down

0 comments on commit a3cec86

Please sign in to comment.