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

Fixed transform control example #221

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions include/ignition/rendering/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace ignition
/// \return The screen scaling factor.
IGNITION_RENDERING_VISIBLE
float screenScalingFactor();
/// \brief Get the screen scaling factor.
/// \param[out] _xScale The X screen scaling factor.
/// \param[out] _yScale The Y screen scaling factor.
IGNITION_RENDERING_VISIBLE
void screenScalingFactor(float &_xScale, float &_yScale);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions ogre/src/OgreCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ VisualPtr OgreCamera::VisualAt(const ignition::math::Vector2i
}
}

float ratio = screenScalingFactor();
float xScale, yScale;
screenScalingFactor(xScale, yScale);
ignition::math::Vector2i mousePos(
static_cast<int>(std::rint(ratio * _mousePos.X())),
static_cast<int>(std::rint(ratio * _mousePos.Y())));
static_cast<int>(std::rint(xScale * _mousePos.X())),
static_cast<int>(std::rint(yScale * _mousePos.Y())));

Ogre::Entity *entity = this->selectionBuffer->OnSelectionClick(
mousePos.X(), mousePos.Y());
Expand Down
7 changes: 4 additions & 3 deletions ogre2/src/Ogre2Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos)
}
}

float ratio = screenScalingFactor();
float xScale, yScale;
screenScalingFactor(xScale, yScale);
ignition::math::Vector2i mousePos(
static_cast<int>(std::rint(ratio * _mousePos.X())),
static_cast<int>(std::rint(ratio * _mousePos.Y())));
static_cast<int>(std::rint(xScale * _mousePos.X())),
static_cast<int>(std::rint(yScale * _mousePos.Y())));

Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick(
mousePos.X(), mousePos.Y());
Expand Down
58 changes: 45 additions & 13 deletions src/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,30 @@ inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
/////////////////////////////////////////////////
float screenScalingFactor()
{
// todo(anyone) set device pixel ratio for high dpi displays on Windows
float ratio = 1.0;
float x{1.0f};
float y{1.0f};
screenScalingFactor(x, y);
return y;
}

/////////////////////////////////////////////////
void screenScalingFactor(float &_xScale, float &_yScale)
{
_xScale = 1.0f;
_yScale = 1.0f;

// todo(anyone) set device pixel ratio for high dpi displays on other
// platforms
#ifdef __linux__
chapulina marked this conversation as resolved.
Show resolved Hide resolved
Display *disp = XOpenDisplay(nullptr);
char *resourceString = XResourceManagerString(disp);
auto closeDisplay = [](Display * display)
{
if (display)
XCloseDisplay(display);
};
auto display =
std::unique_ptr<Display, decltype(closeDisplay)>(
XOpenDisplay(nullptr), closeDisplay);
char *resourceString = XResourceManagerString(display.get());

if (resourceString)
{
Expand Down Expand Up @@ -65,21 +84,34 @@ float screenScalingFactor()
// = N pixels / (M inch / 25.4)
// = (N * 25.4 pixels) / M inch
//
// We can use either the width or height in the following line. The zero
// values in DisplayHeight and DisplayHeightMM is the screen number. A
// value of zero uses the default screen.
float yDpiRes = (DisplayHeight(disp, 0) * 25.4) /
DisplayHeightMM(disp, 0);
// The zero values in DisplayHeight and DisplayHeightMM is the screen
// number. A value of zero uses the default screen.
auto xPixels = DisplayWidth(display.get(), 0);
auto yPixels = DisplayHeight(display.get(), 0);

auto xMM = DisplayWidthMM(display.get(), 0);
auto yMM = DisplayHeightMM(display.get(), 0);

auto xIn = xMM / 25.4;
auto yIn = yMM / 25.4;

float xDpiRes = xPixels / xIn;
float yDpiRes = yPixels / yIn;

if (!math::equal(dpiDesktop, 0.0f) && !math::equal(xDpiRes, 0.0f))
_xScale = xDpiRes / dpiDesktop;

if (!math::equal(dpiDesktop, 0.0f) && !math::equal(yDpiRes, 0.0f))
ratio = dpiDesktop / yDpiRes;
_yScale = yDpiRes / dpiDesktop;

if (_xScale < 1) _xScale = 1;
if (_yScale < 1) _yScale = 1;

// Debug:
// printf("DPI Desktop: %f, DPI XY: [%f, %f], Ratio XY: [%f, %f]\n",
// dpiDesktop, xDpiRes, yDpiRes, xRatio, yRatio);
// printf("DPI Desktop: %f, DPI XY: [%f, %f], Scale XY: [%f, %f]\n",
// dpiDesktop, xDpiRes, yDpiRes, _xScale, _yScale);
}
#endif
return ratio;
}
}
}
Expand Down