Skip to content

Commit

Permalink
support for android screen rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tope99 committed Nov 6, 2023
1 parent 07318a4 commit a9d46ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/imrad.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ struct CustomWidgetArgs
struct IOUserData
{
float dpiScale = 1;
float androidNavBarHeight = 0;
int imeType = 0;
ImVec2 displayRectMinOffset;
ImVec2 displayRectMaxOffset;
int requestedIMEType = 0;
};

//------------------------------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,10 @@ void TopWindow::Export(std::ostream& os, UIContext& ctx)
{
os << ctx.ind << "auto* ioUserData = (ImRad::IOUserData*)ImGui::GetIO().UserData;\n";
os << ctx.ind << "ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);\n";
os << ctx.ind << "ImGui::SetNextWindowPos({ 0, 0 });\n";
os << ctx.ind << "ImGui::SetNextWindowSize({ ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y - ioUserData->androidNavBarHeight });"
<< " //{ " << size_x.to_arg(ctx.unit) << ", " << size_y.to_arg(ctx.unit) << " }\n";
os << ctx.ind << "ImGui::SetNextWindowPos(ioUserData->displayRectMinOffset);\n";
os << ctx.ind << "ImGui::SetNextWindowSize({ ImGui::GetIO().DisplaySize.x - ioUserData->displayRectMinOffset.x - ioUserData->displayRectMaxOffset.x, \n";
os << ctx.ind << " ImGui::GetIO().DisplaySize.y - ioUserData->displayRectMinOffset.y - ioUserData->displayRectMaxOffset.y });";
os << " //{ " << size_x.to_arg(ctx.unit) << ", " << size_y.to_arg(ctx.unit) << " }\n";
std::string fl = "ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings";
os << ctx.ind << "bool tmpOpen;\n";
os << ctx.ind << "if (ImGui::Begin(\"###" << ctx.codeGen->GetName() << "\", &tmpOpen, " << fl << "))\n";
Expand Down
8 changes: 8 additions & 0 deletions template/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MainActivity extends NativeActivity {
private EditText mEditText;

private native void OnKeyboardShown(boolean b);
private native void OnScreenRotation(int deg);

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -102,6 +103,13 @@ else if (ev.getAction() == KeyEvent.ACTION_MULTIPLE) {
return super.dispatchKeyEvent(ev);
}

@Override
public void onConfigurationChanged(Configuration cfg) {
super.onConfigurationChanged(cfg);
int angle = 90 * getWindowManager().getDefaultDisplay().getRotation();
OnScreenRotation(angle);
}

// JNI calls --------------------------------------------------

public int getDpi() {
Expand Down
24 changes: 22 additions & 2 deletions template/android-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static struct android_app* g_App = nullptr;
static bool g_Initialized = false;
static char g_LogTag[] = "ImGuiExample";
static std::string g_IniFilename = "";
static int g_NavBarHeight = 0;
static ImRad::IOUserData g_IOUserData;
static int g_IMEType = 0;

Expand All @@ -48,6 +49,25 @@ Java_com_example_myapplication_MainActivity_OnKeyboardShown(JNIEnv *env, jobject
g_IMEType = 0;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapplication_MainActivity_OnScreenRotation(JNIEnv *env, jobject thiz, jint angle) {
switch (angle) {
case 0:
g_IOUserData.displayRectMinOffset = { 0, 0 };
g_IOUserData.displayRectMaxOffset = { 0, (float)g_NavBarHeight };
break;
case 90:
g_IOUserData.displayRectMinOffset = { 0, 0 };
g_IOUserData.displayRectMaxOffset = { (float)g_NavBarHeight, 0 };
break;
case 270:
g_IOUserData.displayRectMinOffset = { (float)g_NavBarHeight, 0 };
g_IOUserData.displayRectMaxOffset = { 0, 0 };
break;
}
}

// Main code
static void handleAppCmd(struct android_app* app, int32_t appCmd)
{
Expand Down Expand Up @@ -166,7 +186,6 @@ void Init(struct android_app* app)
// Load ImRAD style including fonts:
// ImRad::LoadStyle(fname, g_IOUserData.dpiScale);
// Alternatively, setup Dear ImGui style

ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();

Expand Down Expand Up @@ -369,8 +388,9 @@ static void GetDisplayInfo()
return;

jint dpi = java_env->CallIntMethod(g_App->activity->clazz, method_id);
g_NavBarHeight = 48 * dpi / 160.0;
g_IOUserData.dpiScale = dpi / 120.0; //relative to laptop screen DPI;
g_IOUserData.androidNavBarHeight = 48 * dpi / 160.0;
g_IOUserData.displayRectMaxOffset.y = g_NavBarHeight; //TODO: detect initial screen rotation
ImGui::GetIO().UserData = &g_IOUserData;

jni_return = java_vm->DetachCurrentThread();
Expand Down

0 comments on commit a9d46ce

Please sign in to comment.