Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
OgreImGui.cpp
Go to the documentation of this file.
1
2// -------------------------------------------
3// OGRE-IMGUI bindings
4// See file 'README-OgreImGui.txt' for details
5// -------------------------------------------
6
7/*
8 This source file is part of Rigs of Rods
9 Copyright 2016-2020 Petr Ohlidal
10
11 For more information, see http://www.rigsofrods.org/
12
13 Rigs of Rods is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License version 3, as
15 published by the Free Software Foundation.
16
17 Rigs of Rods is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "OgreImGui.h"
27
28#include "Actor.h"
29#include "AppContext.h"
30#include "ContentManager.h"
31#include "OgreImGuiOverlay.h"
32
33#include <imgui.h>
34#include <Ogre.h>
35
36using namespace RoR;
37
38void OgreImGui::Init(float font_scale)
39{
40 m_imgui_overlay = std::unique_ptr<Ogre::ImGuiOverlay>(new Ogre::ImGuiOverlay());
41
42 ImGuiIO& io = ImGui::GetIO();
43
44 // Disable 'imgui.ini' - we don't need to persist window positions.
45 io.IniFilename = nullptr;
46
47 // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
48 io.KeyMap[ImGuiKey_Tab] = OIS::KC_TAB;
49 io.KeyMap[ImGuiKey_LeftArrow] = OIS::KC_LEFT;
50 io.KeyMap[ImGuiKey_RightArrow] = OIS::KC_RIGHT;
51 io.KeyMap[ImGuiKey_UpArrow] = OIS::KC_UP;
52 io.KeyMap[ImGuiKey_DownArrow] = OIS::KC_DOWN;
53 io.KeyMap[ImGuiKey_PageUp] = OIS::KC_PGUP;
54 io.KeyMap[ImGuiKey_PageDown] = OIS::KC_PGDOWN;
55 io.KeyMap[ImGuiKey_Home] = OIS::KC_HOME;
56 io.KeyMap[ImGuiKey_End] = OIS::KC_END;
57 io.KeyMap[ImGuiKey_Delete] = OIS::KC_DELETE;
58 io.KeyMap[ImGuiKey_Backspace] = OIS::KC_BACK;
59 io.KeyMap[ImGuiKey_Enter] = OIS::KC_RETURN;
60 io.KeyMap[ImGuiKey_Escape] = OIS::KC_ESCAPE;
61 io.KeyMap[ImGuiKey_A] = OIS::KC_A;
62 io.KeyMap[ImGuiKey_C] = OIS::KC_C;
63 io.KeyMap[ImGuiKey_V] = OIS::KC_V;
64 io.KeyMap[ImGuiKey_X] = OIS::KC_X;
65 io.KeyMap[ImGuiKey_Y] = OIS::KC_Y;
66 io.KeyMap[ImGuiKey_Z] = OIS::KC_Z;
67 io.KeyMap[ImGuiKey_KeyPad0] = OIS::KC_NUMPAD0;
68 io.KeyMap[ImGuiKey_KeyPad1] = OIS::KC_NUMPAD1;
69 io.KeyMap[ImGuiKey_KeyPad2] = OIS::KC_NUMPAD2;
70 io.KeyMap[ImGuiKey_KeyPad3] = OIS::KC_NUMPAD3;
71 io.KeyMap[ImGuiKey_KeyPad4] = OIS::KC_NUMPAD4;
72 io.KeyMap[ImGuiKey_KeyPad5] = OIS::KC_NUMPAD5;
73 io.KeyMap[ImGuiKey_KeyPad6] = OIS::KC_NUMPAD6;
74 io.KeyMap[ImGuiKey_KeyPad7] = OIS::KC_NUMPAD7;
75 io.KeyMap[ImGuiKey_KeyPad8] = OIS::KC_NUMPAD8;
76 io.KeyMap[ImGuiKey_KeyPad9] = OIS::KC_NUMPAD9;
77 io.KeyMap[ImGuiKey_Slash] = OIS::KC_SLASH;
78
79 // Load font
80 this->AddFonts(font_scale);
81
82 // Start rendering
83 m_imgui_overlay->setZOrder(300);
84 m_imgui_overlay->initialise(); // Build font texture
85 m_imgui_overlay->show();
86}
87
88void OgreImGui::ReloadFonts(float font_scale)
89{
90 ImGui::GetIO().Fonts->Clear();
91 this->AddFonts(font_scale);
92 m_imgui_overlay->rebuildFontTexture();
93}
94
95void OgreImGui::AddFonts(float font_scale)
96{
97 m_imgui_overlay->addFont("rigsofrods/fonts/Roboto-Medium",
98 ContentManager::ResourcePack::FONTS.resource_group_name, font_scale);
99}
100
101void OgreImGui::InjectMouseMoved( const OIS::MouseEvent &arg )
102{
103
104 ImGuiIO& io = ImGui::GetIO();
105
106 io.MousePos.x = arg.state.X.abs;
107 io.MousePos.y = arg.state.Y.abs;
108 io.MouseWheel = Ogre::Math::Clamp((float)arg.state.Z.rel, -1/3.f, 1/3.f);
109}
110
111void OgreImGui::SetMouseButtonState( OIS::MouseButtonID id, bool down )
112{
113 ImGuiIO& io = ImGui::GetIO();
114 if (id >= 0 && id < 5)
115 {
116 io.MouseDown[id] = down;
117 }
118}
119
121{
122 ImGuiIO& io = ImGui::GetIO();
123 io.MouseDown[0] = false;
124 io.MouseDown[1] = false;
125 io.MouseDown[2] = false;
126 io.MouseDown[3] = false;
127 io.MouseDown[4] = false;
128}
129
130void OgreImGui::InjectKeyPressed( const OIS::KeyEvent &arg )
131{
132 ImGuiIO& io = ImGui::GetIO();
133 io.KeysDown[arg.key] = true;
134
135 if (arg.text>0)
136 {
137 io.AddInputCharacter((unsigned short)arg.text);
138 }
139}
140
141void OgreImGui::InjectKeyReleased( const OIS::KeyEvent &arg )
142{
143 ImGuiIO& io = ImGui::GetIO();
144 io.KeysDown[arg.key] = false;
145}
146
147void OgreImGui::renderQueueStarted(Ogre::uint8 queueGroupId,
148 const Ogre::String& invocation, bool& skipThisInvocation)
149{
150 // Shamelessly copy-pasted from `Ogre::OverlaySystem::renderQueueStarted()`
151 if(queueGroupId == Ogre::RENDER_QUEUE_OVERLAY)
152 {
153 Ogre::Viewport* vp = Ogre::Root::getSingletonPtr()->getRenderSystem()->_getViewport();
154 if(vp != NULL)
155 {
156 Ogre::SceneManager* sceneMgr = vp->getCamera()->getSceneManager();
157 if (vp->getOverlaysEnabled() && sceneMgr->_getCurrentRenderStage() != Ogre::SceneManager::IRS_RENDER_TO_TEXTURE)
158 {
159 // Checking `sceneMgr->_getCurrentRenderStage() == Ogre::SceneManager::IRS_RENDER_TO_TEXTURE`)
160 // doesn't do the trick if the RTT is updated by calling `Ogre::RenderTarget::update()` by hand,
161 // which we do frequently.
162 // To compensate, we also check if the active viewport matches our screen viewport.
163 Ogre::Viewport* vp_target = App::GetAppContext()->GetViewport();
164 if (vp == vp_target)
165 {
166 m_imgui_overlay->_findVisibleObjects(vp->getCamera(), sceneMgr->getRenderQueue(), vp);
167 }
168 }
169 }
170 }
171}
System integration layer; inspired by OgreBites::ApplicationContext.
void InjectKeyReleased(const OIS::KeyEvent &arg)
void Init(float font_scale)
Definition OgreImGui.cpp:38
void ReloadFonts(float font_scale)
Must not be called while an ImGui frame is in progress.
Definition OgreImGui.cpp:88
void InjectMouseMoved(const OIS::MouseEvent &arg)
void ResetAllMouseButtons()
void SetMouseButtonState(OIS::MouseButtonID id, bool down)
void InjectKeyPressed(const OIS::KeyEvent &arg)
void AddFonts(float font_scale)
Definition OgreImGui.cpp:95
virtual void renderQueueStarted(Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &skipThisInvocation) override
std::unique_ptr< Ogre::ImGuiOverlay > m_imgui_overlay
Definition OgreImGui.h:74
Ogre::Viewport * GetViewport()
Definition AppContext.h:69
AppContext * GetAppContext()
static const ResourcePack FONTS