RigsofRods
Soft-body Physics Simulation
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 
22 #include "GUI_TextureToolWindow.h"
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 
35 using namespace RoR;
36 using 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()).asUTF8_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 
155 void 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.isNull())
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 }
RoR::App::sys_user_dir
CVar * sys_user_dir
Definition: Application.cpp:163
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
RoR::GUI::TextureToolWindow::m_display_tex
Ogre::TexturePtr m_display_tex
Definition: GUI_TextureToolWindow.h:46
Console.h
RoR::Console::putMessage
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition: Console.cpp:97
RoR::GUI::TextureToolWindow::m_is_hovered
bool m_is_hovered
Definition: GUI_TextureToolWindow.h:44
Utils.h
Language.h
OgreImGui.h
RoR::Console::CONSOLE_SYSTEM_ERROR
@ CONSOLE_SYSTEM_ERROR
Definition: Console.h:52
GUIManager.h
Actor.h
RoR::Console::CONSOLE_SYSTEM_NOTICE
@ CONSOLE_SYSTEM_NOTICE
Definition: Console.h:51
RoR::GUI::TextureToolWindow::LEFT_PANE_WIDTH
const float LEFT_PANE_WIDTH
Definition: GUI_TextureToolWindow.h:31
RoR::PathCombine
std::string PathCombine(std::string a, std::string b)
Definition: PlatformUtils.h:48
RoR::formatBytes
Ogre::UTFString formatBytes(double bytes)
Definition: Utils.cpp:64
RoR::GUI::TextureToolWindow::Draw
void Draw()
Definition: GUI_TextureToolWindow.cpp:38
PlatformUtils.h
Platform-specific utilities. We use narrow UTF-8 encoded strings as paths. Inspired by http://utf8eve...
Application.h
Central state/object manager and communications hub.
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::GUI::TextureToolWindow::m_is_visible
bool m_is_visible
Definition: GUI_TextureToolWindow.h:43
RoR::GUI::TextureToolWindow::SaveTexture
void SaveTexture(std::string texName, bool usePNG)
Definition: GUI_TextureToolWindow.cpp:155
_LC
#define _LC(ctx, str)
Definition: Language.h:42
GUI_TextureToolWindow.h
RoR::GUI::TextureToolWindow::WINDOW_WIDTH
const float WINDOW_WIDTH
Definition: GUI_TextureToolWindow.h:32
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::Console::CONSOLE_MSGTYPE_INFO
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition: Console.h:60
RoR::GUIManager::RequestGuiCaptureKeyboard
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
Definition: GUIManager.cpp:450
RoR
Definition: AppContext.h:36
RoR::GUI::TextureToolWindow::m_show_dynamic_only
bool m_show_dynamic_only
Definition: GUI_TextureToolWindow.h:45