NAP
Public Member Functions | Protected Member Functions | List of all members
IMGuiService Class Reference

#include <imguiservice.h>

Public Member Functions

 IMGuiService (ServiceConfiguration *configuration)
 
void draw ()
 
void selectWindow (nap::ResourcePtr< RenderWindow > window)
 
ImGuiContext * getContext (nap::ResourcePtr< RenderWindow > window)
 
ImGuiContext * findContext (int windowID)
 
float getScale () const
 
float getScale (const ImGuiContext *context) const
 
const gui::ColorPalettegetPalette () const
 
ImGuiContext * processInputEvent (const InputEvent &event)
 
bool isCapturingKeyboard (ImGuiContext *context)
 
bool isCapturingMouse (ImGuiContext *context)
 
ImTextureID getTextureHandle (const nap::Texture2D &texture) const
 
nap::IcongetIcon (std::string &&name)
 
bool loadIcon (const std::string &name, const nap::Module &module, utility::ErrorState &error)
 
- Public Member Functions inherited from Service
UNPREFIXED_MODULE_NAME_INPUTCASE Service (ServiceConfiguration *configuration)
 
 Service (ServiceConfiguration *configuration)
 
virtual ~Service ()
 
CoregetCore ()
 
const CoregetCore () const
 
std::string getTypeName () const
 
const ModulegetModule () const
 
 Service (Service &)=delete
 
Serviceoperator= (const Service &)=delete
 
 Service (Service &&)=delete
 
Serviceoperator= (Service &&)=delete
 

Protected Member Functions

virtual bool init (utility::ErrorState &error) override
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies) override
 
virtual void update (double deltaTime) override
 
virtual void postUpdate (double deltaTime) override
 
virtual void preShutdown () override
 
virtual void shutdown () override
 
virtual void registerObjectCreators (rtti::Factory &factory) override
 
- Protected Member Functions inherited from Service
virtual void created ()
 
virtual void preUpdate (double deltaTime)
 
virtual void preResourcesLoaded ()
 
virtual void postResourcesLoaded ()
 
template<typename SERVICE_CONFIG >
SERVICE_CONFIG * getConfiguration ()
 
template<typename SERVICE_CONFIG >
const SERVICE_CONFIG * getConfiguration () const
 
std::string getIniFilePath () const
 
std::string getIniFilePath (const std::string &appendix) const
 

Description

This service manages the global ImGui state. Call draw() inside a window render pass to draw the GUI to screen. Call selectWindow() to select the window subsequent ImGUI calls apply to. Explicit window selection is only necessary when there is more than 1 window.

Only call selectWindow() on application update, not when rendering the GUI to screen. The service automatically creates a new GUI frame before application update.

Inheritance diagram for IMGuiService:
[legend]
Collaboration diagram for IMGuiService:
[legend]

Constructor & Destructor Documentation

◆ IMGuiService()

IMGuiService ( ServiceConfiguration configuration)

Default constructor

Member Function Documentation

◆ draw()

void draw ( )

Draws the GUI elements for the currently active window to screen.

// Draw gui window 1
mRenderService->beginFrame();
if (mRenderService->beginRecording(*mRenderWindowOne))
{
mRenderWindowOne->beginRendering();
mGuiService->draw();
mRenderWindowOne->endRendering();
mRenderService->endRecording();
}
// Draw gui window 2
if (mRenderService->beginRecording(*mRenderWindowTwo))
{
mRenderWindowTwo->beginRendering();
mGuiService->draw();
mRenderWindowTwo->endRendering();
mRenderService->endRecording();
}
mRenderService->endFrame();

◆ findContext()

ImGuiContext* findContext ( int  windowID)

Returns the ImGUI context associated with the given window id.

Parameters
windowIDthe render window id
Returns
ImGUI context for the given window, nullptr if it doesn't exist.

◆ getContext()

ImGuiContext* getContext ( nap::ResourcePtr< RenderWindow window)

Returns the ImGUI context associated with the given window.

Parameters
windowthe render window
Returns
ImGUI context for the given window, asserts if it doesn't exist.

◆ getDependentServices()

virtual void getDependentServices ( std::vector< rtti::TypeInfo > &  dependencies)
overrideprotectedvirtual

ImGui depends on the renderer

Parameters
dependenciesthe type of services this service depends on

Reimplemented from Service.

◆ getIcon()

nap::Icon& getIcon ( std::string &&  name)

Returns an icon with the given name and extension. Note that the icon must exist. Only ask for icons with their names explicitly declared in code, for example: 'icon::save'

if (ImGui::ImageButton(gui_service.getIcon(icon::ok)))
{
...
}
Parameters
namethe name, including extension, of the icon to load
Returns
icon with the given name

◆ getPalette()

const gui::ColorPalette& getPalette ( ) const

Returns the GUI color palette.

Returns
the GUI color palette.

◆ getScale() [1/2]

float getScale ( ) const

Returns the scaling factor of the current active context. The scaling factor is calculated using the display DPI (if high DPI rendering is enabled) and the global GUI scale.

Returns
the scaling factor for the current active context, -1.0 if no context is active

◆ getScale() [2/2]

float getScale ( const ImGuiContext *  context) const

Returns the scaling factor for the given context. The scaling factor is calculated using the display DPI (if high DPI rendering is enabled) and the global GUI scale.

Parameters
contextthe ImGUI context
Returns
the scaling factor for the given context

◆ getTextureHandle()

ImTextureID getTextureHandle ( const nap::Texture2D texture) const

Returns a texture handle that can be used to display a Vulkan texture inside ImGUI. Alternatively, use the ImGUI::Image(nap::Texture2D&, ...) utility function, to immediately display a texture instead. Internally the handles are cached, it is therefore fine to call this function every frame. Keep in mind that a handle (descriptor set) is created for every unique texture.

ImGui::Begin("Texture");
ImGui::Image(mGuiService.getTextureHandle(texture), ...);
ImGui::End();
Returns
Vulkan texture handle, used to display a texture in ImGUI

◆ init()

virtual bool init ( utility::ErrorState error)
overrideprotectedvirtual

Initializes the IMGui library. This will also create all associated gui devices objects Note that a GUI can only be associated with the primary window (for now).

Parameters
errorcontains the error message if the lib could not be initialized correctly
Returns
if the lib was initialized successfully

Reimplemented from Service.

◆ isCapturingKeyboard()

bool isCapturingKeyboard ( ImGuiContext *  context)
Returns
if the GUI is capturing keyboard events

◆ isCapturingMouse()

bool isCapturingMouse ( ImGuiContext *  context)
Returns
if the GUI is capturing mouse events

◆ loadIcon()

bool loadIcon ( const std::string &  name,
const nap::Module module,
utility::ErrorState error 
)

Create and add an icon to the default set of icons. The system looks for the icon in the data search paths of the module. On success call getIcon() to retrieve and draw the icon.

Call this function on initialization of your service and make sure your icon names are unique. Duplicates are not allowed!

const auto& icon_names = icon::sequencer::get();
for (const auto& icon_name : icon_names)
{
if (!mGuiService->loadIcon(icon_name, this->getModule(), errorState))
return false;
}
Parameters
namethe name of the icon, including extension
modulethe module that points to the icon
errorcontains the error message if the load operations fails
Returns
if the icon is loaded and added to system defaults

◆ postUpdate()

virtual void postUpdate ( double  deltaTime)
overrideprotectedvirtual

Ends frame operation for all contexts.

Reimplemented from Service.

◆ preShutdown()

virtual void preShutdown ( )
overrideprotectedvirtual

Saves all gui .ini files

Reimplemented from Service.

◆ processInputEvent()

ImGuiContext* processInputEvent ( const InputEvent event)

Forwards window input events to the GUI, called from GUIAppEventHandler.

Returns
context that belongs to the event, nullptr if the event is not related to a window.

◆ registerObjectCreators()

virtual void registerObjectCreators ( rtti::Factory factory)
overrideprotectedvirtual

Registers all gui object creation objects

Reimplemented from Service.

◆ selectWindow()

void selectWindow ( nap::ResourcePtr< RenderWindow window)

Select the window all subsequent ImGUI calls apply to. Explicit selection is only necessary when there is more than 1 window. Only call selectWindow() on application update, not when rendering the GUI to screen.

mGuiService->selectWindow(mRenderWindowOne);
ImGui::Begin("GUI Window One");
...
ImGui::End();
mGuiService->selectWindow(mRenderWindowTwo);
ImGui::Begin("GUI Window Two");
...
ImGui::End();
Parameters
windowthe window to select

◆ shutdown()

virtual void shutdown ( )
overrideprotectedvirtual

Deletes all GUI related resources

Reimplemented from Service.

◆ update()

virtual void update ( double  deltaTime)
overrideprotectedvirtual

Allows IMGUI to draw a new frame. This is called automatically by core in the app loop

Parameters
deltaTimethe time in seconds between ticks

Reimplemented from Service.

nap::RenderService::endRecording
void endRecording()
ImGui::ImageButton
bool IMGUI_API ImageButton(const nap::Texture2D &texture, const ImVec2 &size, const ImVec2 &uv0=ImVec2(0, 1), const ImVec2 &uv1=ImVec2(1, 0), int frame_padding=-1, const ImVec4 &bg_col=ImVec4(0, 0, 0, 0), const ImVec4 &tint_col=ImVec4(1, 1, 1, 1))
nap::RenderService::beginFrame
void beginFrame()
nap::RenderService::beginRecording
bool beginRecording(RenderWindow &renderWindow)
nap::icon::ok
constexpr const char * ok
Definition: imguiservice.h:45
ImGui::Image
void IMGUI_API Image(const nap::Texture2D &texture, const ImVec2 &size, const ImVec2 &uv0=ImVec2(0, 1), const ImVec2 &uv1=ImVec2(1, 0), const ImVec4 &tint_col=ImVec4(1, 1, 1, 1), const ImVec4 &border_col=ImVec4(0, 0, 0, 0))
nap::RenderService::endFrame
void endFrame()