Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve touch overlays #112

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ void init()
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
style.Colors[ImGuiCol_PopupBg].w = 1.0f;
style.AntiAliasedFill = true;
style.AntiAliasedLines = true;
style.AntiAliasedLinesUseTex = true;

default_style = style;

Expand Down
79 changes: 65 additions & 14 deletions src/view_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,70 @@ static void draw_button(ImDrawList* d, int btn, touched_buttons_t const& pressed
constexpr ImU32 pcol = IM_COL32(150, 150, 150, 150);
constexpr ImU32 col = IM_COL32(100, 100, 100, 70);
constexpr ImU32 outline_col = IM_COL32(50, 50, 50, 150);
constexpr float ROT[4] = { 270, 90, 180, 0 };
float thickness = pixel_ratio * 3.f;
auto r = touch_rect(btn);
float s = 0.1f * (r.x1 - r.x0);
r.x0 += s;
r.y0 += s;
r.x1 -= s;
r.y1 -= s;
float rounding = pixel_ratio * s;
d->AddRectFilled(
{ r.x0, r.y0 }, { r.x1, r.y1 },
pressed.btns[btn] ? pcol : col,
rounding, 0);
d->AddRect(
{ r.x0, r.y0 }, { r.x1, r.y1 },
outline_col,
rounding, 0, 5.f * pixel_ratio);

switch(btn)
{
case TOUCH_U:
case TOUCH_D:
case TOUCH_L:
case TOUCH_R:
{
// bevel
constexpr float B = 0.075f;
constexpr ImVec2 DPAD[] =
{
{ -1.f, +0.f + B },
{ -1.f, +0.f - B },
{ +0.f - B, -1.f },
{ +1.f - B, -1.f },
{ +1.f, -1.f + B },
{ +1.f, +1.f - B },
{ +1.f - B, +1.f },
{ +0.f - B, +1.f },
};
constexpr int N_DPAD = sizeof(DPAD) / sizeof(DPAD[0]);
float rot = ROT[btn] * (3.1415926535f / 180);
float tc = cosf(rot);
float ts = sinf(rot);
float scale = (r.x1 - r.x0) * 0.72f;
r.x0 = (r.x0 + r.x1) * 0.5f;
r.y0 = (r.y0 + r.y1) * 0.5f;
ImVec2 dp[N_DPAD + 2];
for(int i = 0; i < N_DPAD; ++i)
{
dp[i].x = (tc * DPAD[i].x - ts * DPAD[i].y) * scale + r.x0;
dp[i].y = (ts * DPAD[i].x + tc * DPAD[i].y) * scale + r.y0;
}
dp[N_DPAD + 0] = dp[0];
dp[N_DPAD + 1] = dp[1];
d->AddConvexPolyFilled(dp, N_DPAD, pressed.btns[btn] ? pcol : col);
d->AddPolyline(dp, N_DPAD, outline_col, ImDrawFlags_Closed, thickness);
break;
}
case TOUCH_A:
case TOUCH_B:
{
constexpr int SEGMENTS = 32;
float radius = (r.x1 - r.x0) * 0.65f;
r.x0 = (r.x0 + r.x1) * 0.5f;
r.y0 = (r.y0 + r.y1) * 0.5f;
d->AddCircleFilled(
{ r.x0, r.y0 }, radius,
pressed.btns[btn] ? pcol : col,
SEGMENTS);
d->AddCircle(
{ r.x0, r.y0 }, radius,
outline_col,
SEGMENTS,
thickness);
break;
}
default:
break;
}
}

touched_buttons_t touched_buttons()
Expand All @@ -92,6 +141,8 @@ touched_buttons_t touched_buttons()

void display_with_scanlines(ImDrawList* d, ImVec2 const& a, ImVec2 const& b)
{
platform_texture_scale_nearest(display_texture);

{
std::array<ImVec2, 4> vs;
vs[0] = {a.x, a.y};
Expand Down