Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
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 m_imgui_overlay->addFont("rigsofrods/fonts/Roboto-Medium",
81 ContentManager::ResourcePack::FONTS.resource_group_name);
82
83 // Start rendering
84 m_imgui_overlay->setZOrder(300);
85 m_imgui_overlay->initialise(); // Build font texture
86 m_imgui_overlay->show();
87}
88
89void OgreImGui::InjectMouseMoved( const OIS::MouseEvent &arg )
90{
91
92 ImGuiIO& io = ImGui::GetIO();
93
94 io.MousePos.x = arg.state.X.abs;
95 io.MousePos.y = arg.state.Y.abs;
96 io.MouseWheel = Ogre::Math::Clamp((float)arg.state.Z.rel, -1/3.f, 1/3.f);
97}
98
99void OgreImGui::SetMouseButtonState( OIS::MouseButtonID id, bool down )
100{
101 ImGuiIO& io = ImGui::GetIO();
102 if (id >= 0 && id < 5)
103 {
104 io.MouseDown[id] = down;
105 }
106}
107
109{
110 ImGuiIO& io = ImGui::GetIO();
111 io.MouseDown[0] = false;
112 io.MouseDown[1] = false;
113 io.MouseDown[2] = false;
114 io.MouseDown[3] = false;
115 io.MouseDown[4] = false;
116}
117
118void OgreImGui::InjectKeyPressed( const OIS::KeyEvent &arg )
119{
120 ImGuiIO& io = ImGui::GetIO();
121 io.KeysDown[arg.key] = true;
122
123 if (arg.text>0)
124 {
125 io.AddInputCharacter((unsigned short)arg.text);
126 }
127}
128
129void OgreImGui::InjectKeyReleased( const OIS::KeyEvent &arg )
130{
131 ImGuiIO& io = ImGui::GetIO();
132 io.KeysDown[arg.key] = false;
133}
134
135void OgreImGui::renderQueueStarted(Ogre::uint8 queueGroupId,
136 const Ogre::String& invocation, bool& skipThisInvocation)
137{
138 // Shamelessly copy-pasted from `Ogre::OverlaySystem::renderQueueStarted()`
139 if(queueGroupId == Ogre::RENDER_QUEUE_OVERLAY)
140 {
141 Ogre::Viewport* vp = Ogre::Root::getSingletonPtr()->getRenderSystem()->_getViewport();
142 if(vp != NULL)
143 {
144 Ogre::SceneManager* sceneMgr = vp->getCamera()->getSceneManager();
145 if (vp->getOverlaysEnabled() && sceneMgr->_getCurrentRenderStage() != Ogre::SceneManager::IRS_RENDER_TO_TEXTURE)
146 {
147 // Checking `sceneMgr->_getCurrentRenderStage() == Ogre::SceneManager::IRS_RENDER_TO_TEXTURE`)
148 // doesn't do the trick if the RTT is updated by calling `Ogre::RenderTarget::update()` by hand,
149 // which we do frequently.
150 // To compensate, we also check if the active viewport matches our screen viewport.
151 Ogre::Viewport* vp_target = App::GetAppContext()->GetViewport();
152 if (vp == vp_target)
153 {
154 m_imgui_overlay->_findVisibleObjects(vp->getCamera(), sceneMgr->getRenderQueue(), vp);
155 }
156 }
157 }
158 }
159}
System integration layer; inspired by OgreBites::ApplicationContext.
void InjectKeyReleased(const OIS::KeyEvent &arg)
void InjectMouseMoved(const OIS::MouseEvent &arg)
Definition OgreImGui.cpp:89
void ResetAllMouseButtons()
void SetMouseButtonState(OIS::MouseButtonID id, bool down)
Definition OgreImGui.cpp:99
void Init()
Definition OgreImGui.cpp:38
void InjectKeyPressed(const OIS::KeyEvent &arg)
virtual void renderQueueStarted(Ogre::uint8 queueGroupId, const Ogre::String &invocation, bool &skipThisInvocation) override
std::unique_ptr< Ogre::ImGuiOverlay > m_imgui_overlay
Definition OgreImGui.h:71
Ogre::Viewport * GetViewport()
Definition AppContext.h:66
AppContext * GetAppContext()
static const ResourcePack FONTS