To use the dear imgui console widget, do the following:

  1. Define the ULOG_IMGUI macro
  2. Make sure that imgui.h is in your include path
  3. Include the ULogImGui.hpp or C/CULogImGui.h headers

C++ API

To use the console widget, create an instance of the ULog::ImGuiConsole class. It looks like this:

class ImGuiConsole
{
public:
    void displayFull(bool& bOpen, bool* bInteractingWithTextbox = nullptr) const noexcept;
    void display(bool* bInteractingWithTextbox = nullptr) const noexcept;

    static void addToMessageLog(const std::string& msg, LogType type) noexcept;
    static void addCommand(const CommandType& cmd) noexcept;

    void setLogColour(ImVec4 colour, LogType type) noexcept;
};

Display functions

There are 2 functions which can be used to render the widget:

  1. display - renders the widget inline. Takes an optional pointer to a boolean (defaults to nullptr) that, if set, is written to true while the user is interacting with the command text box
  2. displayFull - renders the widget inside a dear imgui window titled Developer Console. Takes a boolean reference for checking whether the window is open, and the same optional bInteractingWithTextbox pointer (defaults to nullptr) as display

The rendered widget shows the message log followed by a command input box and a Send button. Pressing Enter inside the box submits the command just like the Send button, and the view auto-scrolls to the bottom while it is already scrolled to the end.

Built-in commands

When the widget is compiled in, two commands are registered automatically:

  1. clear - clears the message log
  2. help - prints every registered command together with its hint

Messages and commands

The remaining functions deal with adding custom commands and controlling the formatting of messages:

  1. addToMessageLog - Adds a coloured message without the formatting that's prepended to all messages
  2. addCommand - Adds a custom console command
  3. setLogColour - Changes the colour used for a given log type

Each log type has a default colour: success is green, warning is yellow, error is red, note is blue and message is white. Use setLogColour with the desired ImVec4 (or ULog_Vec4 in the C API) to override any of them per console instance.

The addCommand function takes an instance of CommandType, which looks like this:

struct CommandType
{
    std::string cmd; // name of the command
    std::string cmdHint; // command description when one uses the `help` command
    std::function<void(const std::string&)> func; // A callback that gets called when the command is called
};

C API

The C API looks like this:

ULog_CImGuiConsole ULog_ImGuiConsole_init();
void ULog_ImGuiConsole_displayFull(ULog_CImGuiConsole* self, bool* bOpen, bool* bInteractingWithTextbox);
void ULog_ImGuiConsole_display(ULog_CImGuiConsole* self, bool* bInteractingWithTextbox);

void ULog_addMessageToLog(const char* message, ULog_LogType type);
void ULog_addCommand(const char* cmd, const char* cmdHint, ULog_ImGuiConsole_CommandFunc func);

void ULog_setLogColour(ULog_CImGuiConsole* self, ULog_Vec4 colour, ULog_LogType type);

As you can see, the functions are the same, except that they take a reference to ULog_CImGuiConsole. To get an instance of it, call ULog_ImGuiConsole_init.

Another difference is that addCommand uses the members of CommandType directly as function arguments, instead of using a ported version of the CommandType struct.