Use sf::Vector2s for expressing sizes and positions

This commit is contained in:
Chris Thrasher 2023-12-23 19:04:30 -06:00
parent 8509f0fddd
commit cf3f4e8d89
4 changed files with 31 additions and 35 deletions

View File

@ -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;
}

View File

@ -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<double>([self screenHeight]) - point.y + static_cast<double>([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<CGFloat>(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<unsigned int>([self titlebarHeight]);
size.y += static_cast<unsigned int>([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<unsigned int>(maxVisibleHeight);
if (size.y > maxVisibleHeight)
size.y = static_cast<unsigned int>(maxVisibleHeight);
if (m_requester != nil)
m_requester->windowResized({width, height - static_cast<unsigned int>([self titlebarHeight])});
m_requester->windowResized({size.x, size.y - static_cast<unsigned int>([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];

View File

@ -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];
}

View File

@ -30,6 +30,8 @@
#include <SFML/Window/WindowHandle.hpp>
#include <SFML/System/Vector2.hpp>
#import <AppKit/AppKit.h>
#include <cstdint>
@ -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