Browse Source

New version of IMGUI_ONCE_UPON_A_FRAME helper macro

ocornut 11 years ago
parent
commit
311a2f8328
2 changed files with 30 additions and 22 deletions
  1. 10 6
      imgui.cpp
  2. 20 16
      imgui.h

+ 10 - 6
imgui.cpp

@@ -111,7 +111,7 @@
  Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix.
  Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code.
 
- - 2014/11/26 (1.17) retired IMGUI_ONCE_UPON_A_FRAME helper macro in favor of ImGuiOnceUponAFrame type that works on all compilers.
+ - 2014/11/26 (1.17) reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility
  - 2014/11/07 (1.15) renamed IsHovered() to IsItemHovered()
  - 2014/10/02 (1.14) renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly)
  - 2014/09/25 (1.13) removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity)
@@ -158,7 +158,7 @@
       e.g. "##Foobar" display an empty label and uses "##Foobar" as ID
    - read articles about the imgui principles (see web links) to understand the requirement and use of ID.
 
- - tip: the construct 'static ImGuiOnceUponAFrame once; if (once)' will evaluate to 'true' only once a frame, you can use it to quickly add custom UI in the middle of a deep nested inner loop in your code.
+ - tip: the construct 'IMGUI_ONCE_UPON_A_FRAME { ... }' will evaluate to a block of code only once a frame. You can use it to quickly add custom UI in the middle of a deep nested inner loop in your code.
  - tip: you can call Render() multiple times (e.g for VR renders), up to you to communicate the extra state to your RenderDrawListFn function.
  - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug"
  - tip: read the ShowTestWindow() code for more example of how to use ImGui!
@@ -6687,12 +6687,16 @@ void ImGui::ShowTestWindow(bool* open)
             ImGui::EndTooltip();
         }
 
-        //static ImGuiOnceUponAFrame oaf;
-        //if (oaf) ImGui::Text("This will be displayed.");
-        //if (oaf) ImGui::Text("This won't be displayed!");
+        // Testing IMGUI_ONCE_UPON_A_FRAME macro
+        //for (int i = 0; i < 5; i++)
+        //{
+        //	IMGUI_ONCE_UPON_A_FRAME
+        //	{
+        //		ImGui::Text("This will be displayed only once.");
+        //	}
+        //}
 
         ImGui::Separator();
-        ImGui::Text("^ Horizontal separator");
 
         static int item = 1;
         ImGui::Combo("combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");

+ 20 - 16
imgui.h

@@ -122,7 +122,7 @@ public:
 #endif // #ifndef ImVector
 
 // Helpers at bottom of the file:
-// - struct ImGuiOnceUponAFrame         // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times)
+// - IMGUI_ONCE_UPON_A_FRAME            // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times)
 // - struct ImGuiTextFilter             // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
 // - struct ImGuiTextBuffer             // Text buffer for logging/accumulating text
 // - struct ImGuiStorage                // Custom key value storage (if you need to alter open/close states manually)
@@ -529,9 +529,13 @@ struct ImGuiIO
 
 // Helper: execute a block of code once a frame only
 // Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.
-// Usage: 
-//   static ImGuiOnceUponAFrame once;
-//   if (once) { ... }
+// Usage:
+//   IMGUI_ONCE_UPON_A_FRAME
+//   {
+//      // code block will be executed one per frame
+//   }
+// Attention! the macro expand into 2 statement so make sure you don't use it within e.g. an if() statement without curly braces.
+#define IMGUI_ONCE_UPON_A_FRAME    static ImGuiOnceUponAFrame imgui_oaf##__LINE__; if (imgui_oaf##__LINE__)
 struct ImGuiOnceUponAFrame
 {
     ImGuiOnceUponAFrame() { RefFrame = -1; }
@@ -750,22 +754,22 @@ struct ImFont
     ImVector<int>           IndexLookup;        // (built)
 
     IMGUI_API ImFont();
-    IMGUI_API ~ImFont()      { Clear(); }
+    IMGUI_API ~ImFont()         { Clear(); }
 
-    IMGUI_API bool                 LoadFromMemory(const void* data, size_t data_size);
-    IMGUI_API bool                 LoadFromFile(const char* filename);
-    IMGUI_API void                 Clear();
-    IMGUI_API void                 BuildLookupTable();
-    IMGUI_API const FntGlyph *     FindGlyph(unsigned short c, const FntGlyph* fallback = NULL) const;
-    IMGUI_API float                GetFontSize() const { return (float)Info->FontSize; }
-    IMGUI_API bool                 IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
+    IMGUI_API bool			    LoadFromMemory(const void* data, size_t data_size);
+    IMGUI_API bool              LoadFromFile(const char* filename);
+    IMGUI_API void              Clear();
+    IMGUI_API void              BuildLookupTable();
+    IMGUI_API const FntGlyph*   FindGlyph(unsigned short c, const FntGlyph* fallback = NULL) const;
+    IMGUI_API float             GetFontSize() const { return (float)Info->FontSize; }
+    IMGUI_API bool              IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
 
     // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
     // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
-    IMGUI_API ImVec2               CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
-    IMGUI_API ImVec2               CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
-    IMGUI_API void                 RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
+    IMGUI_API ImVec2            CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
+    IMGUI_API ImVec2            CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
+    IMGUI_API void              RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
 
 private:
-    IMGUI_API const char*          CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width, const FntGlyph* fallback_glyph) const;
+    IMGUI_API const char*       CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width, const FntGlyph* fallback_glyph) const;
 };