[nostalgia] Fix scaling of SceneEditorView not to be completely broken
This commit is contained in:
@@ -12,7 +12,7 @@ namespace nostalgia::scene {
|
||||
|
||||
SceneEditorImGui::SceneEditorImGui(turbine::Context &ctx, ox::StringView path):
|
||||
m_ctx(ctx),
|
||||
m_itemPath(std::move(path)),
|
||||
m_itemPath(path),
|
||||
m_editor(m_ctx, m_itemPath),
|
||||
m_view(m_ctx, m_editor.scene()) {
|
||||
const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset();
|
||||
@@ -29,18 +29,28 @@ ox::CRString SceneEditorImGui::itemDisplayName() const noexcept {
|
||||
}
|
||||
|
||||
void SceneEditorImGui::draw(turbine::Context*) noexcept {
|
||||
const auto paneSize = ImGui::GetContentRegionAvail();
|
||||
const ox::Size fbSize{
|
||||
static_cast<int>(paneSize.x),
|
||||
static_cast<int>(paneSize.y)};
|
||||
m_view.draw(fbSize.width, fbSize.height);
|
||||
auto const paneSize = ImGui::GetContentRegionAvail();
|
||||
m_view.draw(ox::Size{static_cast<int>(paneSize.x), static_cast<int>(paneSize.y)});
|
||||
auto &fb = m_view.framebuffer();
|
||||
const uintptr_t buffId = fb.color.id;
|
||||
auto const srcH = static_cast<float>(fb.height) / static_cast<float>(fb.width);
|
||||
auto const dstH = paneSize.y / paneSize.x;
|
||||
float xScale{}, yScale{};
|
||||
if (dstH > srcH) {
|
||||
// crop off width
|
||||
xScale = srcH / dstH;
|
||||
yScale = 1;
|
||||
} else {
|
||||
auto const srcW = static_cast<float>(fb.width) / static_cast<float>(fb.height);
|
||||
auto const dstW = (paneSize.x / paneSize.y);
|
||||
xScale = 1;
|
||||
yScale = srcW / dstW;
|
||||
}
|
||||
uintptr_t const buffId = fb.color.id;
|
||||
ImGui::Image(
|
||||
reinterpret_cast<void*>(buffId),
|
||||
std::bit_cast<void*>(buffId),
|
||||
paneSize,
|
||||
ImVec2(0, 1),
|
||||
ImVec2(1, 0));
|
||||
ImVec2(0, yScale),
|
||||
ImVec2(xScale, 0));
|
||||
}
|
||||
|
||||
void SceneEditorImGui::onActivated() noexcept {
|
||||
|
@@ -9,21 +9,25 @@
|
||||
namespace nostalgia::scene {
|
||||
|
||||
SceneEditorView::SceneEditorView(turbine::Context &tctx, SceneStatic const&sceneStatic):
|
||||
m_cctx(core::init(tctx, {.glInstallDrawer = false}).unwrapThrow()),
|
||||
m_sceneStatic(sceneStatic),
|
||||
m_scene(m_sceneStatic) {
|
||||
oxThrowError(core::init(tctx, {.glInstallDrawer = false}).moveTo(&m_cctx));
|
||||
}
|
||||
|
||||
ox::Error SceneEditorView::setupScene() noexcept {
|
||||
glutils::resizeInitFrameBuffer(m_frameBuffer, core::gl::drawSize(1));
|
||||
return m_scene.setupDisplay(*m_cctx);
|
||||
}
|
||||
|
||||
void SceneEditorView::draw(int width, int height) noexcept {
|
||||
if (width != m_frameBuffer.width || height != m_frameBuffer.height) {
|
||||
glutils::resizeInitFrameBuffer(m_frameBuffer, width, height);
|
||||
void SceneEditorView::draw(ox::Size const&targetSz) noexcept {
|
||||
auto const scaleSz = targetSz / core::gl::drawSize(1);
|
||||
auto const scale = ox::max(1, ox::max(scaleSz.width, scaleSz.height));
|
||||
if (m_scale != scale) {
|
||||
m_scale = scale;
|
||||
glutils::resizeInitFrameBuffer(m_frameBuffer, core::gl::drawSize(m_scale));
|
||||
}
|
||||
glutils::FrameBufferBind const frameBufferBind(m_frameBuffer);
|
||||
core::gl::draw(*m_cctx, {width, height});
|
||||
core::gl::draw(*m_cctx, m_scale);
|
||||
}
|
||||
|
||||
glutils::FrameBuffer const&SceneEditorView::framebuffer() const noexcept {
|
||||
|
@@ -18,13 +18,14 @@ class SceneEditorView {
|
||||
SceneStatic const&m_sceneStatic;
|
||||
Scene m_scene;
|
||||
glutils::FrameBuffer m_frameBuffer;
|
||||
int m_scale = 1;
|
||||
|
||||
public:
|
||||
SceneEditorView(turbine::Context &ctx, SceneStatic const&sceneStatic);
|
||||
|
||||
ox::Error setupScene() noexcept;
|
||||
|
||||
void draw(int width, int height) noexcept;
|
||||
void draw(ox::Size const&targetSz) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
glutils::FrameBuffer const&framebuffer() const noexcept;
|
||||
|
Reference in New Issue
Block a user