Skip to content

Commit

Permalink
Fix various linux scaling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sirjuddington committed Jun 5, 2024
1 parent 0906680 commit 597af04
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 43 deletions.
19 changes: 12 additions & 7 deletions src/Graphics/WxGfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ void Context::applyView() const
void Context::setPen(const ColRGBA& colour, double width) const
{
if (gc)
gc->SetPen(gc->CreatePen(wxGraphicsPenInfo{ colour, width / (view ? view->scale().x : 1.0) }));
{
auto w = gc->GetWindow();
auto p_width = (width / (view ? view->scale().x : 1.0)) / w->GetContentScaleFactor();
gc->SetPen(gc->CreatePen(wxGraphicsPenInfo{ colour, p_width }));
}
}

void Context::setBrush(const ColRGBA& colour) const
Expand Down Expand Up @@ -140,16 +144,16 @@ void Context::drawRect(int x, int y, int width, int height) const
if (!gc)
return;

const auto w = gc->GetWindow();
gc->DrawRectangle(w->FromPhys(x), w->FromPhys(y), w->FromPhys(width), w->FromPhys(height));
const auto scale = gc->GetContentScaleFactor();
gc->DrawRectangle(x / scale, y / scale, width / scale, height / scale);
}

void Context::drawBitmap(const wxBitmap& bitmap, int x, int y, double alpha, int width, int height) const
{
if (!gc)
return;

const auto w = gc->GetWindow();
const auto scale = gc->GetContentScaleFactor();

if (alpha < 1.)
gc->BeginLayer(alpha);
Expand All @@ -159,7 +163,7 @@ void Context::drawBitmap(const wxBitmap& bitmap, int x, int y, double alpha, int
if (height < 0)
height = bitmap.GetHeight();

gc->DrawBitmap(bitmap, w->FromPhys(x), w->FromPhys(y), w->FromPhys(width), w->FromPhys(height));
gc->DrawBitmap(bitmap, x / scale, y / scale, width / scale, height / scale);

if (alpha < 1.)
gc->EndLayer();
Expand Down Expand Up @@ -297,10 +301,11 @@ unique_ptr<wxGraphicsContext> wxgfx::createGraphicsContext(wxWindowDC& dc)
// -----------------------------------------------------------------------------
void wxgfx::applyViewToGC(const gl::View& view, wxGraphicsContext* gc)
{
auto scale = gc->GetContentScaleFactor();
if (view.centered())
gc->Translate(view.size().x * 0.5, view.size().y * 0.5);
gc->Translate((view.size().x * 0.5) / scale, (view.size().y * 0.5) / scale);
gc->Scale(view.scale().x, view.scale().y);
gc->Translate(-view.offset().x, -view.offset().y);
gc->Translate(-view.offset().x / scale, -view.offset().y / scale);
}

bool wxgfx::nearestInterpolationSupported()
Expand Down
6 changes: 3 additions & 3 deletions src/UI/Canvas/CTextureCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CTextureCanvas::CTextureCanvas(wxWindow* parent) : wxPanel(parent), palette_{ ne
wxEVT_SIZE,
[this](wxSizeEvent&)
{
view_.setSize(GetSize().x, GetSize().y);
view_.setSize(ToPhys(GetSize().x), ToPhys(GetSize().y));
Refresh();
});
}
Expand Down Expand Up @@ -219,7 +219,7 @@ void CTextureCanvas::drawPatch(const wxgfx::Context& ctx, int index)
}

// Draw patch
ctx.drawBitmap(patch_bitmaps_[index], patch->xOffset(), patch->yOffset());
ctx.drawBitmap(patch_bitmaps_[index], patch->xOffset(), patch->yOffset(), 1.0, patch_image->width(), patch_image->height());
}


Expand All @@ -241,7 +241,7 @@ void CTextureCanvas::onPaint(wxPaintEvent& e)
auto ctx = wxgfx::Context(dc, &view_);

// Background
wxgfx::generateCheckeredBackground(background_bitmap_, GetSize().x, GetSize().y);
wxgfx::generateCheckeredBackground(background_bitmap_, view_.size().x, view_.size().y);
ctx.drawBitmap(background_bitmap_, 0, 0);

// Aspect Ratio Correction
Expand Down
9 changes: 5 additions & 4 deletions src/UI/Canvas/CTextureCanvasBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,16 @@ void CTextureCanvasBase::loadTexturePreview()
void CTextureCanvasBase::onMouseEvent(wxMouseEvent& e)
{
bool refresh = false;
auto p_x = e.GetX() * window()->GetContentScaleFactor();
auto p_y = e.GetY() * window()->GetContentScaleFactor();

// MOUSE MOVEMENT
if (e.Moving() || e.Dragging())
{
dragging_ = e.LeftIsDown();

// Check if patch hilight changes
const auto pos = view().canvasPos({ e.GetX(), e.GetY() });
const auto pos = view().canvasPos({ p_x, p_y });
const int patch = patchAt(pos.x, pos.y);
if (hilight_patch_ != patch)
{
Expand Down Expand Up @@ -410,7 +412,7 @@ void CTextureCanvasBase::onMouseEvent(wxMouseEvent& e)
}
if (!wxGetKeyState(WXK_CONTROL) && linked_zoom_control_ && e.GetWheelAxis() == wxMOUSE_WHEEL_VERTICAL)
{
zoom_point_ = { e.GetPosition().x, e.GetPosition().y };
zoom_point_ = { p_x, p_y };

if (e.GetWheelRotation() > 0)
linked_zoom_control_->zoomIn(true);
Expand All @@ -426,6 +428,5 @@ void CTextureCanvasBase::onMouseEvent(wxMouseEvent& e)
window()->Refresh();

// Update 'previous' mouse coordinates
mouse_prev_ = { e.GetPosition().x * window()->GetContentScaleFactor(),
e.GetPosition().y * window()->GetContentScaleFactor() };
mouse_prev_ = { p_x, p_y };
}
20 changes: 12 additions & 8 deletions src/UI/Canvas/GL/GLCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GLCanvas::GLCanvas(wxWindow* parent, BGStyle bg_style, const ColRGBA& bg_colour,
wxEVT_SIZE,
[this](wxSizeEvent& e)
{
view_.setSize(GetSize().x, GetSize().y);
view_.setSize(ToPhys(GetSize().x), ToPhys(GetSize().y));
if (vb_background_)
vb_background_->buffer().clear();
});
Expand All @@ -50,10 +50,12 @@ void GLCanvas::setupMousewheelZoom()
wxEVT_MOUSEWHEEL,
[&](wxMouseEvent& e)
{
auto phys_pos = ToPhys(e.GetPosition());

if (e.GetWheelRotation() < 0)
view_.zoomToward(0.8, { e.GetPosition().x, e.GetPosition().y });
view_.zoomToward(0.8, { phys_pos.x, phys_pos.y });
else if (e.GetWheelRotation() > 0)
view_.zoomToward(1.25, { e.GetPosition().x, e.GetPosition().y });
view_.zoomToward(1.25, { phys_pos.x, phys_pos.y });

Refresh();
});
Expand All @@ -65,9 +67,11 @@ void GLCanvas::setupMousePanning()
wxEVT_MOTION,
[&](wxMouseEvent& e)
{
auto phys_pos = ToPhys(e.GetPosition());

if (e.MiddleIsDown())
{
auto cpos_current = view_.canvasPos({ e.GetPosition().x, e.GetPosition().y });
auto cpos_current = view_.canvasPos({ phys_pos.x, phys_pos.y });
auto cpos_prev = view_.canvasPos(mouse_prev_);

view_.pan(cpos_prev.x - cpos_current.x, cpos_prev.y - cpos_current.y);
Expand All @@ -77,7 +81,7 @@ void GLCanvas::setupMousePanning()
else
e.Skip();

mouse_prev_ = { e.GetPosition().x, e.GetPosition().y };
mouse_prev_ = { phys_pos.x, phys_pos.y };
});
}

Expand Down Expand Up @@ -184,8 +188,8 @@ void GLCanvas::updateBackgroundVB()
if (!vb_background_)
vb_background_ = std::make_unique<gl::VertexBuffer2D>();

auto widthf = static_cast<float>(GetSize().x);
auto heightf = static_cast<float>(GetSize().y);
auto widthf = static_cast<float>(ToPhys(GetSize().x));
auto heightf = static_cast<float>(ToPhys(GetSize().y));

vb_background_->add({ { 0.f, 0.f }, { 1.f, 1.f, 1.f, 1.f }, { 0.f, 0.f } });
vb_background_->add({ { 0.f, heightf }, { 1.f, 1.f, 1.f, 1.f }, { 0.f, heightf / 16.f } });
Expand Down Expand Up @@ -228,7 +232,7 @@ void GLCanvas::onPaint(wxPaintEvent& e)
init();

// Set viewport
glViewport(0, 0, GetSize().x, GetSize().y);
glViewport(0, 0, ToPhys(GetSize().x), ToPhys(GetSize().y));

// Clear
glClearColor(bg_colour_.fr(), bg_colour_.fg(), bg_colour_.fb(), 1.f);
Expand Down
41 changes: 31 additions & 10 deletions src/UI/Canvas/GfxCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ GfxCanvas::GfxCanvas(wxWindow* parent) : wxPanel(parent)
wxEVT_SIZE,
[this](wxSizeEvent&)
{
view_.setSize(GetSize().x, GetSize().y);
view_.setSize(ToPhys(GetSize().x), ToPhys(GetSize().y));
Refresh();
});

Expand Down Expand Up @@ -130,6 +130,22 @@ void GfxCanvas::generateBrushShadow()
brush_bitmap_ = wxBitmap(wx_img);
}

// -----------------------------------------------------------------------------
// Returns true if the image bitmap needs to be updated
// -----------------------------------------------------------------------------
bool GfxCanvas::shouldUpdateImage() const
{
if (update_image_)
return true;

// Check if resize required
if (!wxgfx::nearestInterpolationSupported() && image_bitmap_.GetWidth() != image_->width() * view_.scale().x
|| image_bitmap_.GetHeight() != image_->height() * view_.scale().y)
return true;

return false;
}

// -----------------------------------------------------------------------------
// Updates the wx bitmap(s) for the image and other related data
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -172,7 +188,7 @@ void GfxCanvas::drawImage(const wxgfx::Context& ctx)
&& editing_mode_ == EditMode::None;

// Load/update image if needed
if (update_image_ || hilight != image_hilighted_)
if (shouldUpdateImage() || hilight != image_hilighted_)
updateImage(hilight);

// Get top left coord to draw at
Expand All @@ -185,7 +201,7 @@ void GfxCanvas::drawImage(const wxgfx::Context& ctx)
}

// Draw image
ctx.drawBitmap(image_bitmap_, tl.x, tl.y, dragging ? 0.5 : 1.0);
ctx.drawBitmap(image_bitmap_, tl.x, tl.y, dragging ? 0.5 : 1.0, image_->width(), image_->height());

// Draw brush shadow when in editing mode
if (editing_mode_ != EditMode::None && brush_bitmap_.IsOk() && cursor_pos_ != Vec2i{ -1, -1 })
Expand All @@ -196,7 +212,7 @@ void GfxCanvas::drawImage(const wxgfx::Context& ctx)
{
tl.x += math::scaleInverse(drag_pos_.x - drag_origin_.x, view().scale().x);
tl.y += math::scaleInverse(drag_pos_.y - drag_origin_.y, view().scale().y);
ctx.drawBitmap(image_bitmap_, tl.x, tl.y);
ctx.drawBitmap(image_bitmap_, tl.x, tl.y, 1.0, image_->width(), image_->height());
}

// Draw outline
Expand All @@ -214,14 +230,14 @@ void GfxCanvas::drawImage(const wxgfx::Context& ctx)
void GfxCanvas::drawImageTiled(const wxgfx::Context& ctx)
{
// Load/update image if needed
if (update_image_ || image_hilighted_)
if (shouldUpdateImage() || image_hilighted_)
updateImage(false);

// Draw image multiple times to fill canvas
auto left = view().canvasX(0);
auto y = view().canvasY(0);
auto right = view().canvasX(GetSize().x);
auto bottom = view().canvasY(GetSize().y);
auto right = view().canvasX(ToPhys(GetSize().x));
auto bottom = view().canvasY(ToPhys(GetSize().y));
while (y < bottom)
{
auto x = left;
Expand All @@ -240,8 +256,13 @@ void GfxCanvas::drawImageTiled(const wxgfx::Context& ctx)
// -----------------------------------------------------------------------------
void GfxCanvas::drawCropRect(const wxgfx::Context& ctx) const
{
// Recti vr = view_.visibleRegion();
const Recti vr{ view_.visibleRegion().tl, view_.visibleRegion().br };
Recti vr{ view_.visibleRegion().tl, view_.visibleRegion().br };

// Expand visible region by 1 pixel to ensure everything is drawn right to the edges
vr.tl.x--;
vr.tl.y--;
vr.br.x++;
vr.br.y++;

// Draw cropping lines
ctx.setPen(ColRGBA::BLACK);
Expand Down Expand Up @@ -276,7 +297,7 @@ void GfxCanvas::onPaint(wxPaintEvent& e)
auto ctx = wxgfx::Context{ dc, &view_ };

// Background
wxgfx::generateCheckeredBackground(background_bitmap_, GetSize().x, GetSize().y);
wxgfx::generateCheckeredBackground(background_bitmap_, view_.size().x, view_.size().y);
ctx.drawBitmap(background_bitmap_, 0, 0);

// Aspect Ratio Correction
Expand Down
1 change: 1 addition & 0 deletions src/UI/Canvas/GfxCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GfxCanvas : public wxPanel, public GfxCanvasBase
wxBitmap brush_bitmap_;

void generateBrushShadow() override;
bool shouldUpdateImage() const;
void updateImage(bool hilight);
void drawImage(const wxgfx::Context& ctx);
void drawImageTiled(const wxgfx::Context& ctx);
Expand Down
14 changes: 7 additions & 7 deletions src/UI/Canvas/GfxCanvasBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ void GfxCanvasBase::onMouseMovement(wxMouseEvent& e)
bool refresh = false;

// Check if the mouse is over the image
auto scalefactor = window()->GetContentScaleFactor();
const int x = e.GetPosition().x * scalefactor;
const int y = e.GetPosition().y * scalefactor - 2;
const auto p_pos = window()->ToPhys(e.GetPosition());
const int x = window()->ToPhys(e.GetPosition().x);
const int y = window()->ToPhys(e.GetPosition().y);
const bool on_image = onImage(x, y);
cursor_pos_ = imageCoords(x, y);
if (on_image && editing_mode_ != EditMode::None)
Expand Down Expand Up @@ -443,7 +443,7 @@ void GfxCanvasBase::onMouseMovement(wxMouseEvent& e)
}
else
{
drag_pos_ = { e.GetPosition().x * scalefactor, e.GetPosition().y * scalefactor };
drag_pos_ = { p_pos.x, p_pos.y };
refresh = true;
}
}
Expand All @@ -455,7 +455,7 @@ void GfxCanvasBase::onMouseMovement(wxMouseEvent& e)
// Middle mouse down (pan view)
if (e.MiddleIsDown())
{
auto cpos_current = view().canvasPos({ e.GetPosition().x, e.GetPosition().y });
auto cpos_current = view().canvasPos({ p_pos.x, p_pos.y });
auto cpos_prev = view().canvasPos(mouse_prev_);

view().pan(cpos_prev.x - cpos_current.x, cpos_prev.y - cpos_current.y);
Expand All @@ -466,7 +466,7 @@ void GfxCanvasBase::onMouseMovement(wxMouseEvent& e)
if (refresh)
window()->Refresh();

mouse_prev_ = { e.GetPosition().x * scalefactor, e.GetPosition().y * scalefactor };
mouse_prev_ = { p_pos.x, p_pos.y };

e.Skip();
}
Expand Down Expand Up @@ -512,7 +512,7 @@ void GfxCanvasBase::onMouseWheel(wxMouseEvent& e)
if (!wxGetKeyState(WXK_CONTROL) && linked_zoom_control_ && e.GetWheelAxis() == wxMOUSE_WHEEL_VERTICAL)
{
// Zoom towards cursor
zoom_point_ = { e.GetPosition().x, e.GetPosition().y };
zoom_point_ = { window()->ToPhys(e.GetPosition().x), window()->ToPhys(e.GetPosition().y) };

if (e.GetWheelRotation() > 0)
linked_zoom_control_->zoomIn(true);
Expand Down
8 changes: 4 additions & 4 deletions src/UI/Canvas/PaletteCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ void PaletteCanvas::onMouseLeftDown(wxMouseEvent& e)
rows = 8;
cols = 32;
}
int x = ((e.GetX() - offset_.x) * GetContentScaleFactor()) / square_size_;
int y = ((e.GetY() - offset_.y) * GetContentScaleFactor()) / square_size_;
int x = (e.GetX() - offset_.x) / square_size_;
int y = (e.GetY() - offset_.y) / square_size_;

// If it was within the palette box, select the cell
if (x >= 0 && x < cols && y >= 0 && y < rows)
Expand Down Expand Up @@ -352,8 +352,8 @@ void PaletteCanvas::onMouseMotion(wxMouseEvent& e)
rows = 8;
cols = 32;
}
int x = ((e.GetX() - offset_.x) * GetContentScaleFactor()) / square_size_;
int y = ((e.GetY() - offset_.y) * GetContentScaleFactor()) / square_size_;
int x = (e.GetX() - offset_.x) / square_size_;
int y = (e.GetY() - offset_.y) / square_size_;

// Set selection accordingly
if (x >= 0 && x < cols && y >= 0 && y < rows)
Expand Down

0 comments on commit 597af04

Please sign in to comment.