Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[glass] Fix Field2d position and scaling #6222

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions glass/src/lib/native/cpp/other/Field2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,16 @@ bool FieldInfo::LoadJson(std::span<const char> is, std::string_view filename) {
height = units::convert<units::feet, units::meters>(height);
}

// check scaling
int fieldWidth = m_right - m_left;
int fieldHeight = m_bottom - m_top;
if (std::abs((fieldWidth / width) - (fieldHeight / height)) > 0.1) {
fmt::print(stderr,
"GUI: Field X and Y scaling substantially different: "
"xscale={} yscale={}\n",
(fieldWidth / width), (fieldHeight / height));
}

if (!filename.empty()) {
// the image filename is relative to the json file
auto pathname = fs::path{filename}.replace_filename(image).string();
Expand Down Expand Up @@ -560,23 +570,29 @@ FieldFrameData FieldInfo::GetFrameData(ImVec2 min, ImVec2 max) const {
// fit the image into the window
if (m_texture && m_imageHeight != 0 && m_imageWidth != 0) {
gui::MaxFit(&min, &max, m_imageWidth, m_imageHeight);
} else {
gui::MaxFit(&min, &max, m_width, m_height);
}

FieldFrameData ffd;
ffd.imageMin = min;
ffd.imageMax = max;

// size down the box by the image corners (if any)
if (m_bottom > 0 && m_right > 0) {
min.x += m_left * (max.x - min.x) / m_imageWidth;
min.y += m_top * (max.y - min.y) / m_imageHeight;
max.x -= (m_imageWidth - m_right) * (max.x - min.x) / m_imageWidth;
max.y -= (m_imageHeight - m_bottom) * (max.y - min.y) / m_imageHeight;
if (m_bottom > 0 && m_right > 0 && m_imageWidth != 0) {
// size down the box by the image corners
float scale = (max.x - min.x) / m_imageWidth;
min.x += m_left * scale;
min.y += m_top * scale;
max.x -= (m_imageWidth - m_right) * scale;
max.y -= (m_imageHeight - m_bottom) * scale;
} else if ((max.x - min.x) > 40 && (max.y - min.y > 40)) {
// ensure there's some padding
min.x += 20;
max.x -= 20;
min.y += 20;
max.y -= 20;
}

// draw the field "active area" as a yellow boundary box
gui::MaxFit(&min, &max, m_width, m_height);

ffd.min = min;
ffd.max = max;
ffd.scale = (max.x - min.x) / m_width;
Expand Down