瀏覽代碼

Add InputText that supports std::string.

Former-commit-id: 97297ca7378cf66bf2210196daa06de5d278a00b
Jérémie Dumas 7 年之前
父節點
當前提交
c49b9a8001
共有 2 個文件被更改,包括 18 次插入1 次删除
  1. 14 0
      include/igl/opengl/glfw/imgui/ImGuiHelpers.h
  2. 4 1
      tutorial/106_ViewerMenu/main.cpp

+ 14 - 0
include/igl/opengl/glfw/imgui/ImGuiHelpers.h

@@ -12,6 +12,7 @@
 #include <imgui/imgui.h>
 #include <vector>
 #include <string>
+#include <algorithm>
 ////////////////////////////////////////////////////////////////////////////////
 
 // Extend ImGui by populating its namespace directly
@@ -43,6 +44,19 @@ inline bool ListBox(const char* label, int* idx, std::vector<std::string>& value
 		static_cast<void*>(&values), values.size());
 }
 
+inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)
+{
+  char buf[1024];
+  std::fill_n(buf, 1024, 0);
+  std::copy_n(str.begin(), std::min(1024, (int) str.size()), buf);
+  if (ImGui::InputText(label, buf, 1024, flags, callback, user_data))
+  {
+    str = std::string(buf);
+    return true;
+  }
+  return false;
+}
+
 } // namespace ImGui
 
 #endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H

+ 4 - 1
tutorial/106_ViewerMenu/main.cpp

@@ -64,7 +64,7 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
   {
     // Define next window position + size
     ImGui::SetNextWindowPos(ImVec2(180.f * menu_scaling(), 10), ImGuiSetCond_FirstUseEver);
-    ImGui::SetNextWindowSize(ImVec2(200, 80), ImGuiSetCond_FirstUseEver);
+    ImGui::SetNextWindowSize(ImVec2(200, 160), ImGuiSetCond_FirstUseEver);
     ImGui::Begin(
         "New Window", nullptr,
         ImGuiWindowFlags_NoSavedSettings
@@ -75,6 +75,9 @@ class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
     ImGui::DragFloat("float", &floatVariable, 0.0, 0.0, 3.0);
     ImGui::PopItemWidth();
 
+    static std::string str = "bunny";
+    ImGui::InputText("Name", str);
+
     ImGui::End();
   }