SpaceTangent

ImGui text with inline color changes

Mar 29th, 2017
2,464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. const char ColorMarkerStart = '{';
  2. const char ColorMarkerEnd = '}';
  3.  
  4. bool ProcessInlineHexColor( const char* start, const char* end, ImVec4& color )
  5. {
  6.     const int hexCount = ( int )( end - start );
  7.     if( hexCount == 6 || hexCount == 8 )
  8.     {
  9.         char hex[9];
  10.         strncpy( hex, start, hexCount );
  11.         hex[hexCount] = 0;
  12.  
  13.         unsigned int hexColor = 0;
  14.         if( sscanf( hex, "%x", &hexColor ) > 0 )
  15.         {
  16.             color.x = static_cast< float >( ( hexColor & 0x00FF0000 ) >> 16 ) / 255.0f;
  17.             color.y = static_cast< float >( ( hexColor & 0x0000FF00 ) >> 8  ) / 255.0f;
  18.             color.z = static_cast< float >( ( hexColor & 0x000000FF )       ) / 255.0f;
  19.             color.w = 1.0f;
  20.  
  21.             if( hexCount == 8 )
  22.             {
  23.                 color.w = static_cast< float >( ( hexColor & 0xFF000000 ) >> 24 ) / 255.0f;
  24.             }
  25.  
  26.             return true;
  27.         }
  28.     }
  29.  
  30.     return false;
  31. }
  32.  
  33. void TextWithColors( const char* fmt, ... )
  34. {
  35.     char tempStr[4096];
  36.  
  37.     va_list argPtr;
  38.     va_start( argPtr, fmt );
  39.     _vsnprintf( tempStr, sizeof( tempStr ), fmt, argPtr );
  40.     va_end( argPtr );
  41.     tempStr[sizeof( tempStr ) - 1] = '\0';
  42.  
  43.     bool pushedColorStyle = false;
  44.     const char* textStart = tempStr;
  45.     const char* textCur = tempStr;
  46.     while( textCur < ( tempStr + sizeof( tempStr ) ) && *textCur != '\0' )
  47.     {
  48.         if( *textCur == ColorMarkerStart )
  49.         {
  50.             // Print accumulated text
  51.             if( textCur != textStart )
  52.             {
  53.                 ImGui::TextUnformatted( textStart, textCur );
  54.                 ImGui::SameLine( 0.0f, 0.0f );
  55.             }
  56.  
  57.             // Process color code
  58.             const char* colorStart = textCur + 1;
  59.             do
  60.             {
  61.                 ++textCur;
  62.             }
  63.             while( *textCur != '\0' && *textCur != ColorMarkerEnd );
  64.  
  65.             // Change color
  66.             if( pushedColorStyle )
  67.             {
  68.                 ImGui::PopStyleColor();
  69.                 pushedColorStyle = false;
  70.             }
  71.  
  72.             ImVec4 textColor;
  73.             if( ProcessInlineHexColor( colorStart, textCur, textColor ) )
  74.             {
  75.                 ImGui::PushStyleColor( ImGuiCol_Text, textColor );
  76.                 pushedColorStyle = true;
  77.             }
  78.  
  79.             textStart = textCur + 1;
  80.         }
  81.         else if( *textCur == '\n' )
  82.         {
  83.             // Print accumulated text an go to next line
  84.             ImGui::TextUnformatted( textStart, textCur );
  85.             textStart = textCur + 1;
  86.         }
  87.  
  88.         ++textCur;
  89.     }
  90.  
  91.     if( textCur != textStart )
  92.     {
  93.         ImGui::TextUnformatted( textStart, textCur );
  94.     }
  95.     else
  96.     {
  97.         ImGui::NewLine();
  98.     }
  99.  
  100.     if( pushedColorStyle )
  101.     {
  102.         ImGui::PopStyleColor();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment