From cf3f4e8d89904d6d9da52e0aadf212932f9a9b3f Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 23 Dec 2023 19:04:30 -0600 Subject: [PATCH] Use `sf::Vector2`s for expressing sizes and positions --- src/SFML/Window/macOS/SFViewController.mm | 14 ++++----- src/SFML/Window/macOS/SFWindowController.mm | 29 +++++++++---------- src/SFML/Window/macOS/WindowImplCocoa.mm | 6 ++-- .../Window/macOS/WindowImplDelegateProtocol.h | 17 +++++------ 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/SFML/Window/macOS/SFViewController.mm b/src/SFML/Window/macOS/SFViewController.mm index 542071527..34fd964ca 100644 --- a/src/SFML/Window/macOS/SFViewController.mm +++ b/src/SFML/Window/macOS/SFViewController.mm @@ -147,10 +147,9 @@ ////////////////////////////////////////////////////////. -- (void)setWindowPositionToX:(int)x Y:(int)y +- (void)setWindowPositionTo:(sf::Vector2i)position { - (void)x; - (void)y; + (void)position; sf::err() << "Cannot move SFML area when SFML is integrated in a NSView. Use the view handler directly instead." << std::endl; } @@ -164,9 +163,9 @@ //////////////////////////////////////////////////////// -- (void)resizeTo:(unsigned int)width by:(unsigned int)height +- (void)resizeTo:(sf::Vector2u)size { - NSRect frame = NSMakeRect([m_view frame].origin.x, [m_view frame].origin.y, width, height); + NSRect frame = NSMakeRect([m_view frame].origin.x, [m_view frame].origin.y, size.x, size.y); [m_view setFrame:frame]; [m_oglView setFrame:frame]; @@ -249,10 +248,9 @@ //////////////////////////////////////////////////////// -- (void)setIconTo:(unsigned int)width by:(unsigned int)height with:(const std::uint8_t*)pixels +- (void)setIconTo:(sf::Vector2u)size with:(const std::uint8_t*)pixels { - (void)width; - (void)height; + (void)size; (void)pixels; sf::err() << "Cannot set an icon when SFML is integrated in a NSView." << std::endl; } diff --git a/src/SFML/Window/macOS/SFWindowController.mm b/src/SFML/Window/macOS/SFWindowController.mm index a909870f8..c497ec773 100644 --- a/src/SFML/Window/macOS/SFWindowController.mm +++ b/src/SFML/Window/macOS/SFWindowController.mm @@ -404,9 +404,9 @@ //////////////////////////////////////////////////////// -- (void)setWindowPositionToX:(int)x Y:(int)y +- (void)setWindowPositionTo:(sf::Vector2i)position { - NSPoint point = NSMakePoint(x, y); + NSPoint point = NSMakePoint(position.x, position.y); // Flip for SFML window coordinate system and take titlebar into account point.y = static_cast([self screenHeight]) - point.y + static_cast([self titlebarHeight]); @@ -427,7 +427,7 @@ //////////////////////////////////////////////////////// -- (void)resizeTo:(unsigned int)width by:(unsigned int)height +- (void)resizeTo:(sf::Vector2u)size { if (m_fullscreen) { @@ -436,12 +436,11 @@ sf::VideoMode desktop = sf::VideoMode::getDesktopMode(); sf::priv::scaleInXY(desktop.size, nil); - width = std::min(width, desktop.size.x); - height = std::min(height, desktop.size.y); + size.x = std::min(size.x, desktop.size.x); + size.y = std::min(size.y, desktop.size.y); - CGFloat x = (desktop.size.x - width) / 2.0; - CGFloat y = (desktop.size.y - height) / 2.0; - NSRect oglRect = NSMakeRect(x, y, width, height); + const auto origin = sf::Vector2(desktop.size - size) / CGFloat{2}; + NSRect oglRect = NSMakeRect(origin.x, origin.y, size.x, size.y); [m_oglView setFrame:oglRect]; [m_oglView setNeedsDisplay:YES]; @@ -455,19 +454,19 @@ [m_window setStyleMask:styleMask ^ NSResizableWindowMask]; // Add titlebar height. - height += static_cast([self titlebarHeight]); + size.y += static_cast([self titlebarHeight]); // Corner case: don't set the window height bigger than the screen height // or the view will be resized _later_ without generating a resize event. NSRect screenFrame = [[NSScreen mainScreen] visibleFrame]; CGFloat maxVisibleHeight = screenFrame.size.height; - if (height > maxVisibleHeight) - height = static_cast(maxVisibleHeight); + if (size.y > maxVisibleHeight) + size.y = static_cast(maxVisibleHeight); if (m_requester != nil) - m_requester->windowResized({width, height - static_cast([self titlebarHeight])}); + m_requester->windowResized({size.x, size.y - static_cast([self titlebarHeight])}); - NSRect frame = NSMakeRect([m_window frame].origin.x, [m_window frame].origin.y, width, height); + NSRect frame = NSMakeRect([m_window frame].origin.x, [m_window frame].origin.y, size.x, size.y); [m_window setFrame:frame display:YES]; @@ -555,10 +554,10 @@ //////////////////////////////////////////////////////// -- (void)setIconTo:(unsigned int)width by:(unsigned int)height with:(const std::uint8_t*)pixels +- (void)setIconTo:(sf::Vector2u)size with:(const std::uint8_t*)pixels { // Load image and set app icon. - NSImage* icon = [NSImage imageWithRawData:pixels andSize:NSMakeSize(width, height)]; + NSImage* icon = [NSImage imageWithRawData:pixels andSize:NSMakeSize(size.x, size.y)]; [[SFApplication sharedApplication] setApplicationIconImage:icon]; diff --git a/src/SFML/Window/macOS/WindowImplCocoa.mm b/src/SFML/Window/macOS/WindowImplCocoa.mm index 90335a157..2f903b5a4 100644 --- a/src/SFML/Window/macOS/WindowImplCocoa.mm +++ b/src/SFML/Window/macOS/WindowImplCocoa.mm @@ -428,7 +428,7 @@ void WindowImplCocoa::setPosition(const Vector2i& position) const AutoreleasePool pool; sf::Vector2i backingPosition = position; scaleInXY(backingPosition, m_delegate); - [m_delegate setWindowPositionToX:backingPosition.x Y:backingPosition.y]; + [m_delegate setWindowPositionTo:backingPosition]; } @@ -448,7 +448,7 @@ void WindowImplCocoa::setSize(const Vector2u& size) { sf::Vector2u backingSize = size; scaleInXY(backingSize, m_delegate); - [m_delegate resizeTo:backingSize.x by:backingSize.y]; + [m_delegate resizeTo:backingSize]; } @@ -485,7 +485,7 @@ void WindowImplCocoa::setTitle(const String& title) void WindowImplCocoa::setIcon(const Vector2u& size, const std::uint8_t* pixels) { const AutoreleasePool pool; - [m_delegate setIconTo:size.x by:size.y with:pixels]; + [m_delegate setIconTo:size with:pixels]; } diff --git a/src/SFML/Window/macOS/WindowImplDelegateProtocol.h b/src/SFML/Window/macOS/WindowImplDelegateProtocol.h index b51ab7d1e..63e56d32e 100644 --- a/src/SFML/Window/macOS/WindowImplDelegateProtocol.h +++ b/src/SFML/Window/macOS/WindowImplDelegateProtocol.h @@ -30,6 +30,8 @@ #include +#include + #import #include @@ -128,11 +130,10 @@ class WindowImplCocoa; /// /// Doesn't apply if the implementation is 'only' a view. /// -/// \param x x position in SFML coordinates -/// \param y y position in SFML coordinates +/// \param position x and y position in SFML coordinates /// //////////////////////////////////////////////////////////// -- (void)setWindowPositionToX:(int)x Y:(int)y; +- (void)setWindowPositionTo:(sf::Vector2i)position; //////////////////////////////////////////////////////////// /// \brief Get window/view's size @@ -145,11 +146,10 @@ class WindowImplCocoa; //////////////////////////////////////////////////////////// /// \brief Resize the window/view /// -/// \param width new width -/// \param height new height +/// \param size new width and height /// //////////////////////////////////////////////////////////// -- (void)resizeTo:(unsigned int)width by:(unsigned int)height; +- (void)resizeTo:(sf::Vector2u)size; //////////////////////////////////////////////////////////// /// \brief Set the minimize window/view size @@ -231,12 +231,11 @@ class WindowImplCocoa; //////////////////////////////////////////////////////////// /// \brief Set an icon to the application /// -/// \param width icon's width -/// \param height icon's height +/// \param size icon's width and height /// \param pixels icon's data /// //////////////////////////////////////////////////////////// -- (void)setIconTo:(unsigned int)width by:(unsigned int)height with:(const std::uint8_t*)pixels; +- (void)setIconTo:(sf::Vector2u)size with:(const std::uint8_t*)pixels; //////////////////////////////////////////////////////////// /// \brief Fetch new event