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
GUI_TextureToolWindow.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5
6 For more information, see http://www.rigsofrods.org/
7
8 Rigs of Rods is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 3, as
10 published by the Free Software Foundation.
11
12 Rigs of Rods is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
23
24#include <Ogre.h>
25
26#include "Actor.h"
27#include "Application.h"
28#include "Console.h"
29#include "GUIManager.h"
30#include "Language.h"
31#include "OgreImGui.h"
32#include "PlatformUtils.h"
33#include "Utils.h"
34
35using namespace RoR;
36using namespace GUI;
37
39{
40 ImGui::SetNextWindowPosCenter(ImGuiCond_FirstUseEver);
41 ImGui::SetNextWindowSize(ImVec2(WINDOW_WIDTH, 550.f), ImGuiCond_FirstUseEver);
42 if (!ImGui::Begin(_LC("TextureToolWindow", "Texture Tool"), &m_is_visible))
43 {
44 ImGui::End(); // The window is collapsed
45 return;
46 }
47
48 // left
49 ImGui::BeginGroup();
50
51 ImGui::Checkbox(_LC("TextureToolWindow", "Dynamic only"), &m_show_dynamic_only);
52
53 ImGui::BeginChild("texture list", ImVec2(LEFT_PANE_WIDTH, 0), true);
54 auto itor = Ogre::TextureManager::getSingleton().getResourceIterator();
55 while (itor.hasMoreElements())
56 {
57 Ogre::TexturePtr tex = Ogre::static_pointer_cast<Ogre::Texture>(itor.getNext());
58 if (m_show_dynamic_only && ((tex->getUsage() & Ogre::TU_STATIC) != 0))
59 {
60 continue;
61 }
62
63 bool selected = m_display_tex == tex;
64 if (ImGui::Selectable(tex->getName().c_str(), &selected))
65 {
66 m_display_tex = tex;
67 }
68 }
69 ImGui::EndChild(); // texture list
70 ImGui::EndGroup(); // left
71 ImGui::SameLine();
72
73 // right
74 ImGui::BeginGroup();
75
76 if (m_display_tex)
77 {
78 ImGui::Text("%s", m_display_tex->getName().c_str());
79 ImGui::Separator();
80
81 // Draw the image
82 float max_width = ImGui::GetWindowSize().x -
83 (LEFT_PANE_WIDTH + ImGui::GetStyle().ItemSpacing.x + 2*ImGui::GetStyle().WindowPadding.x);
84 float max_height = ImGui::GetWindowSize().y * 0.5;
85 ImVec2 size(m_display_tex->getWidth(), m_display_tex->getHeight());
86 size *= max_width / size.x; // Fit size along X
87 if (size.y > max_height) // Reduce size along Y if needed
88 {
89 size *= max_height / size.y;
90 }
91 ImGui::Image(reinterpret_cast<ImTextureID>(m_display_tex->getHandle()), size);
92
93 // Draw image info
94 ImGui::BeginChild("tex info", ImVec2(0, -ImGui::GetItemsLineHeightWithSpacing())); // Leave room for 1 line below us
95
96 ImGui::Text("Res: %u x %u pixels", m_display_tex->getWidth(), m_display_tex->getHeight());
97 ImGui::Text("Size: %s", formatBytes(m_display_tex->getSize()).c_str());
98 ImGui::Text("Format: %s", Ogre::PixelUtil::getFormatName(m_display_tex->getFormat()).c_str());
99 if (m_display_tex->getNumFaces() > 1)
100 ImGui::Text("Num. faces: %d", static_cast<int>(m_display_tex->getNumFaces()));
101 if (m_display_tex->getFSAA() > 0)
102 ImGui::Text("FSAA: %u", m_display_tex->getFSAA());
103 if (m_display_tex->getNumMipmaps() > 0)
104 ImGui::Text("Num. mipmaps: %u", m_display_tex->getNumMipmaps());
105
106 static const char* tex_type_names[] =
107 { "~invalid~", "1D", "2D", "3D", "Cubemap", "2D array", "2D rect", "External OES" };
108 ImGui::Text("Type: %s", tex_type_names[m_display_tex->getTextureType()]);
109
110 std::string usageStr = "";
111 if (m_display_tex->getUsage() & Ogre::TU_STATIC)
112 usageStr += "static,\n";
113 if (m_display_tex->getUsage() & Ogre::TU_DYNAMIC)
114 usageStr += "dynamic,\n";
115 if (m_display_tex->getUsage() & Ogre::TU_WRITE_ONLY)
116 usageStr += "write only,\n";
117 if (m_display_tex->getUsage() & Ogre::TU_STATIC_WRITE_ONLY)
118 usageStr += "static write only,\n";
119 if (m_display_tex->getUsage() & Ogre::TU_DYNAMIC_WRITE_ONLY)
120 usageStr += "dynamic write only,\n";
121 if (m_display_tex->getUsage() & Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE)
122 usageStr += "dynamic write only discardable,\n";
123 if (m_display_tex->getUsage() & Ogre::TU_AUTOMIPMAP)
124 usageStr += "automipmap,\n";
125 if (m_display_tex->getUsage() & Ogre::TU_RENDERTARGET)
126 usageStr += "rendertarget,\n";
127 if (m_display_tex->getUsage() & Ogre::TU_DEFAULT)
128 usageStr += "default\n";
129 ImGui::TextWrapped("Usage: %s", usageStr.c_str());
130
131 ImGui::Text("Depth: %u", m_display_tex->getDepth());
132 ImGui::EndChild(); // tex info
133
134 ImGui::BeginChild("buttons");
135 if (ImGui::Button(_LC("TextureToolWindow", "Save as PNG")))
136 {
137 this->SaveTexture(m_display_tex->getName(), /*usePNG =*/ true);
138 }
139 ImGui::SameLine();
140 if (ImGui::Button(_LC("TextureToolWindow", "Save Raw")))
141 {
142 this->SaveTexture(m_display_tex->getName(), /*usePNG =*/ false);
143 }
144 ImGui::EndChild(); // buttons
145 }
146
147 ImGui::EndGroup(); // right
148
149 m_is_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
151
152 ImGui::End();
153}
154
155void TextureToolWindow::SaveTexture(std::string texName, bool usePNG)
156{
157 using namespace Ogre;
158 try
159 {
160 TexturePtr tex = TextureManager::getSingleton().getByName(texName);
161 if (!tex)
162 return;
163
164 Image img;
165 tex->convertToImage(img);
166
167 // Save to disk!
168 std::string outname = PathCombine(App::sys_user_dir->getStr(), StringUtil::replaceAll(texName, "/", "_"));
169 if (usePNG)
170 outname += ".png";
171
172 img.save(outname);
173
174 std::string msg = _LC("TextureToolWindow", "saved texture as ") + outname;
176 }
177 catch (Exception& e)
178 {
179 std::string str = "Exception while saving image: " + e.getFullDescription();
181 }
182}
Central state/object manager and communications hub.
#define _LC(ctx, str)
Definition Language.h:38
Platform-specific utilities. We use narrow UTF-8 encoded strings as paths. Inspired by http://utf8eve...
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition Console.h:60
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition Console.cpp:103
@ CONSOLE_SYSTEM_ERROR
Definition Console.h:52
@ CONSOLE_SYSTEM_NOTICE
Definition Console.h:51
void SaveTexture(std::string texName, bool usePNG)
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
std::string PathCombine(std::string a, std::string b)
GUIManager * GetGuiManager()
CVar * sys_user_dir
Console * GetConsole()
std::string formatBytes(double bytes)
Definition Utils.cpp:67