Browse Source

Demo: added simple Log demo

ocornut 10 years ago
parent
commit
72d3fca52f
2 changed files with 64 additions and 2 deletions
  1. 2 0
      imgui.cpp
  2. 62 2
      imgui_demo.cpp

+ 2 - 0
imgui.cpp

@@ -396,6 +396,7 @@
  - input text: add ImGuiInputTextFlags_EnterToApply? (off #218)
  - input text multi-line: way to dynamically grow the buffer without forcing the user to initially allocate for worse case (follow up on #200)
  - input text multi-line: line numbers? status bar? (follow up on #200)
+ - input text: read-only mode (can still select/copy, always display input buffer)
  - input number: optional range min/max for Input*() functions
  - input number: holding [-]/[+] buttons could increase the step speed non-linearly (or user-controlled)
  - input number: use mouse wheel to step up/down
@@ -4672,6 +4673,7 @@ void ImGui::SetScrollFromPosY(float pos_y, float center_y_ratio)
     window->ScrollTargetCenterRatioY = center_y_ratio;
 }
 
+// center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item.
 void ImGui::SetScrollHere(float center_y_ratio)
 {
     ImGuiWindow* window = GetCurrentWindow();

+ 62 - 2
imgui_demo.cpp

@@ -44,6 +44,7 @@
 #ifndef IMGUI_DISABLE_TEST_WINDOWS
 
 static void ShowExampleAppConsole(bool* opened);
+static void ShowExampleAppLog(bool* opened);
 static void ShowExampleAppLayout(bool* opened);
 static void ShowExampleAppLongText(bool* opened);
 static void ShowExampleAppAutoResize(bool* opened);
@@ -89,6 +90,7 @@ void ImGui::ShowTestWindow(bool* opened)
     static bool show_app_metrics = false;
     static bool show_app_main_menu_bar = false;
     static bool show_app_console = false;
+    static bool show_app_log = false;
     static bool show_app_layout = false;
     static bool show_app_long_text = false;
     static bool show_app_auto_resize = false;
@@ -99,6 +101,7 @@ void ImGui::ShowTestWindow(bool* opened)
     if (show_app_metrics) ImGui::ShowMetricsWindow(&show_app_metrics);
     if (show_app_main_menu_bar) ShowExampleAppMainMenuBar();
     if (show_app_console) ShowExampleAppConsole(&show_app_console);
+    if (show_app_log) ShowExampleAppLog(&show_app_log);
     if (show_app_layout) ShowExampleAppLayout(&show_app_layout);
     if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text);
     if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize);
@@ -157,6 +160,7 @@ void ImGui::ShowTestWindow(bool* opened)
         {
             ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar);
             ImGui::MenuItem("Console", NULL, &show_app_console);
+            ImGui::MenuItem("Log", NULL, &show_app_log);
             ImGui::MenuItem("Simple layout", NULL, &show_app_layout);
             ImGui::MenuItem("Long text display", NULL, &show_app_long_text);
             ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize);
@@ -1715,7 +1719,7 @@ struct ExampleAppConsole
         ScrollToBottom = true;
     }
 
-    void    Run(const char* title, bool* opened)
+    void    Draw(const char* title, bool* opened)
     {
         ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiSetCond_FirstUseEver);
         if (!ImGui::Begin(title, opened))
@@ -1938,7 +1942,63 @@ struct ExampleAppConsole
 static void ShowExampleAppConsole(bool* opened)
 {
     static ExampleAppConsole console;
-    console.Run("Example: Console", opened);
+    console.Draw("Example: Console", opened);
+}
+
+// Usage:
+//  static ExampleAppLog my_log;
+//  my_log.AddLog("Hello %d world\n", 123);
+//  my_log.Draw("title");
+struct ExampleAppLog
+{
+    ImGuiTextBuffer     Buf;
+    bool                ScrollToBottom;
+
+    void    Clear()     { Buf.clear(); }
+
+    void    AddLog(const char* fmt, ...) IM_PRINTFARGS(2)
+    {
+        va_list args;
+        va_start(args, fmt);
+        Buf.appendv(fmt, args);
+        va_end(args);
+        ScrollToBottom = true;
+    }
+
+    void    Draw(const char* title, bool* p_opened = NULL)
+    {
+        ImGui::SetNextWindowSize(ImVec2(500,400), ImGuiSetCond_FirstUseEver);
+        ImGui::Begin(title, p_opened);
+        if (ImGui::Button("Clear")) Buf.clear();
+        ImGui::SameLine();
+        bool copy = ImGui::Button("Copy");
+        ImGui::Separator();
+        ImGui::BeginChild("scrolling");
+        if (copy)
+            ImGui::LogToClipboard();
+        ImGui::TextUnformatted(Buf.begin());
+        if (ScrollToBottom)
+            ImGui::SetScrollHere(1.0f);
+        ScrollToBottom = false;
+        ImGui::EndChild();
+        ImGui::End();
+    }
+};
+
+static void ShowExampleAppLog(bool* opened)
+{
+    static ExampleAppLog log;
+
+    // Demo fill
+    static float last_time = -1.0f;
+    float time = ImGui::GetTime();
+    if (time - last_time >= 0.3f)
+    {
+        log.AddLog("Hello, time is %.1f sec, rand() is %d\n", time, (int)rand());
+        last_time = time;
+    }
+
+    log.Draw("Example: Log", opened);
 }
 
 static void ShowExampleAppLayout(bool* opened)