Add fill/keep aspect ratio modes in engine rendering

This commit is contained in:
SweetId
2023-11-25 11:22:07 +05:30
parent 736c6387dc
commit 77ae80b98e
7 changed files with 88 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ LOC_EDITOR_MENU_TOOLS;Tools;Outils
LOC_EDITOR_MENU_LANGUAGE;Language;Langue
LOC_EDITOR_MENU_VIEW;View;Fenetre
LOC_EDITOR_MENU_RESOLUTION;Resolution;Résolution
LOC_EDITOR_MENU_ASPECTRATIO;Aspect Ratio;Aspect Ratio
LOC_EDITOR_ACTION_LEVEL_NEW_DESC;Create new level;Créé un nouveau niveau
LOC_EDITOR_ACTION_LEVEL_OPEN_DESC;Open a level;Ouvre un niveau
LOC_EDITOR_ACTION_LEVEL_SAVE_DESC;Save current level;Sauvegarde le niveau actuel
@@ -29,3 +30,7 @@ LOC_EDITOR_WINDOW_MAIN_TITLE;MainWindow;Fenêtre principale
LOC_EDITOR_WINDOW_OUTPUT_TITLE;Output;Sortie
LOC_EDITOR_POPUP_CREATE_LEVEL_TITLE;Warning;Attention
LOC_EDITOR_POPUP_CREATE_LEVEL_DESC;Are you sure you want to create a new level?;Etes-vous sur de vouloir créer un nouveau niveau?
LOC_EDITOR_CAMERA_KEEPASPECTRATIO_TITLE;Aspect Ratio;Aspect Ratio
LOC_EDITOR_CAMERA_KEEPASPECTRATIO_DESC;Keep Aspect Ratio;Garder l'aspect ratio
LOC_EDITOR_CAMERA_FILL_TITLE;Fill;Remplir
LOC_EDITOR_CAMERA_FILL_DESC;Fills the window;Remplis la fenetre
1 ;en-US;fr-FR;
16 LOC_EDITOR_MENU_LANGUAGE;Language;Langue
17 LOC_EDITOR_MENU_VIEW;View;Fenetre
18 LOC_EDITOR_MENU_RESOLUTION;Resolution;Résolution
19 LOC_EDITOR_MENU_ASPECTRATIO;Aspect Ratio;Aspect Ratio
20 LOC_EDITOR_ACTION_LEVEL_NEW_DESC;Create new level;Créé un nouveau niveau
21 LOC_EDITOR_ACTION_LEVEL_OPEN_DESC;Open a level;Ouvre un niveau
22 LOC_EDITOR_ACTION_LEVEL_SAVE_DESC;Save current level;Sauvegarde le niveau actuel
30 LOC_EDITOR_WINDOW_OUTPUT_TITLE;Output;Sortie
31 LOC_EDITOR_POPUP_CREATE_LEVEL_TITLE;Warning;Attention
32 LOC_EDITOR_POPUP_CREATE_LEVEL_DESC;Are you sure you want to create a new level?;Etes-vous sur de vouloir créer un nouveau niveau?
33 LOC_EDITOR_CAMERA_KEEPASPECTRATIO_TITLE;Aspect Ratio;Aspect Ratio
34 LOC_EDITOR_CAMERA_KEEPASPECTRATIO_DESC;Keep Aspect Ratio;Garder l'aspect ratio
35 LOC_EDITOR_CAMERA_FILL_TITLE;Fill;Remplir
36 LOC_EDITOR_CAMERA_FILL_DESC;Fills the window;Remplis la fenetre

View File

@@ -32,4 +32,29 @@ namespace Nz
protected:
Nz::Vector2ui m_size;
};
class NAZARAEDITOR_CORE_API EditorAction_SetCameraStretchMode final
: public EditorAction
{
public:
EditorAction_SetCameraStretchMode(const Properties& properties, StretchMode mode)
: EditorAction(properties)
, m_mode(mode)
{}
EditorAction_SetCameraStretchMode(const std::shared_ptr<Properties>& properties, StretchMode mode)
: EditorAction(properties)
, m_mode(mode)
{}
~EditorAction_SetCameraStretchMode() = default;
std::unique_ptr<EditorAction> Clone() const override { return std::make_unique<EditorAction_SetCameraStretchMode>(m_properties, m_mode); }
const std::string& GetName() const override { return m_properties->className; }
bool IsUndoRedoable() const override { return false; }
static const char* GetClassName() { return "EditorAction_SetCameraStretchMode"; }
void Execute() override;
protected:
StretchMode m_mode;
};
}

View File

@@ -83,6 +83,9 @@ namespace Nz
inline Nz::Texture* GetEngineTexture() { return m_engineTexture.get(); }
inline const Nz::Texture* GetEngineTexture() const { return m_engineTexture.get(); }
inline StretchMode GetEngineTextureStretchMode() const { return m_engineTextureStretchMode; }
inline void SetEngineTextureStretchMode(StretchMode mode) { m_engineTextureStretchMode = mode; }
void CreateEngineTexture(const Nz::Vector2ui& resolution);
private:
@@ -103,5 +106,7 @@ namespace Nz
Nz::Level m_level;
entt::handle m_mainCamera;
StretchMode m_engineTextureStretchMode;
};
}

View File

@@ -15,4 +15,13 @@
#define NAZARAEDITOR_CORE_API
#endif
namespace Nz
{
enum StretchMode
{
Fill,
KeepAspectRatio
};
}
#endif // NAZARAEDITOR_CORE_CONFIG_HPP

View File

@@ -21,10 +21,29 @@ namespace Nz
.category = "Tools",
}, size);
}
app.RegisterAction<Nz::EditorAction_SetCameraStretchMode>({
.className = std::format("{}_{}", Nz::EditorAction_SetCameraSize::GetClassName(), "Fill"),
.description = "LOC_EDITOR_CAMERA_FILL_DESC",
.path = { "LOC_EDITOR_MENU_VIEW", "LOC_EDITOR_MENU_ASPECTRATIO", "LOC_EDITOR_CAMERA_FILL_TITLE" },
.category = "Tools",
}, StretchMode::Fill);
app.RegisterAction<Nz::EditorAction_SetCameraStretchMode>({
.className = std::format("{}_{}", Nz::EditorAction_SetCameraSize::GetClassName(), "AspectRatio"),
.description = "LOC_EDITOR_CAMERA_KEEPASPECTRATIO_DESC",
.path = { "LOC_EDITOR_MENU_VIEW", "LOC_EDITOR_MENU_ASPECTRATIO", "LOC_EDITOR_CAMERA_KEEPASPECTRATIO_TITLE" },
.category = "Tools",
}, StretchMode::KeepAspectRatio);
}
void EditorAction_SetCameraSize::Execute()
{
Nz::EditorBaseApplication::Instance()->CreateEngineTexture(m_size);
}
void EditorAction_SetCameraStretchMode::Execute()
{
Nz::EditorBaseApplication::Instance()->SetEngineTextureStretchMode(m_mode);
}
}

View File

@@ -18,6 +18,7 @@ namespace Nz
EditorBaseApplication::EditorBaseApplication()
: m_level(this)
, m_engineTextureStretchMode(StretchMode::KeepAspectRatio)
{
NazaraAssert(s_instance == nullptr, "EditorBaseApplication already exists");
s_instance = this;

View File

@@ -22,7 +22,29 @@ namespace NzEditor
{
auto pos = ImGui::GetCursorPos();
auto size = ImGui::GetContentRegionAvail();
auto stretch = GetApplication()->GetEngineTextureStretchMode();
auto texSize = GetApplication()->GetEngineTexture()->GetSize();
if (stretch == Nz::StretchMode::KeepAspectRatio)
{
if (texSize.x > texSize.y)
{
float x = size.x;
float y = texSize.y * size.x / texSize.x;
ImGui::Image(GetApplication()->GetEngineTexture(), Nz::Vector2f{ x, y });
}
else
{
float x = texSize.x * size.y / texSize.y;
float y = size.y;
ImGui::Image(GetApplication()->GetEngineTexture(), Nz::Vector2f{ x, y });
}
}
else
{
ImGui::Image(GetApplication()->GetEngineTexture(), Nz::Vector2f{ size.x, size.y });
}
ImGui::SetCursorPos(pos); // everything else will be drawn on top of the texture
auto cam = GetApplication()->GetMainCamera();