218 lines
5.3 KiB
Plaintext
218 lines
5.3 KiB
Plaintext
#include "Window.h"
|
|||
|
|
#include "Foundation/NSSharedPtr.hpp"
|
||
|
|
#include "Graphics/Texture.h"
|
||
|
|
using namespace Seele;
|
||
|
|
using namespace Seele::Metal;
|
||
|
|
|
||
|
|
double currentFrameDelta = 0;
|
||
|
|
double Gfx::getCurrentFrameDelta()
|
||
|
|
{
|
||
|
|
return currentFrameDelta;
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
|
||
|
|
{
|
||
|
|
if (key == -1)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->keyPress((KeyCode)key, (InputAction)action, (KeyModifier)modifier);
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwMouseMoveCallback(GLFWwindow* handle, double xpos, double ypos)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->mouseMove(xpos, ypos);
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwMouseButtonCallback(GLFWwindow* handle, int button, int action, int modifier)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->mouseButton((MouseButton)button, (InputAction)action, (KeyModifier)modifier);
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwScrollCallback(GLFWwindow* handle, double xoffset, double yoffset)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->scroll(xoffset, yoffset);
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwFileCallback(GLFWwindow* handle, int count, const char** paths)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->fileDrop(count, paths);
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwCloseCallback(GLFWwindow* handle)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->close();
|
||
|
|
}
|
||
|
|
|
||
|
|
void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height)
|
||
|
|
{
|
||
|
|
Window* window = (Window*)glfwGetWindowUserPointer(handle);
|
||
|
|
window->resize(width, height);
|
||
|
|
}
|
||
|
|
|
||
|
|
Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
|
||
|
|
: graphics(graphics)
|
||
|
|
, preferences(createInfo)
|
||
|
|
{
|
||
|
|
float xscale, yscale;
|
||
|
|
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, &yscale);
|
||
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||
|
|
GLFWwindow *handle = glfwCreateWindow(createInfo.width / xscale, createInfo.height / yscale, createInfo.title, nullptr, nullptr);
|
||
|
|
windowHandle = handle;
|
||
|
|
glfwSetWindowUserPointer(handle, this);
|
||
|
|
|
||
|
|
glfwSetKeyCallback(handle, &glfwKeyCallback);
|
||
|
|
glfwSetCursorPosCallback(handle, &glfwMouseMoveCallback);
|
||
|
|
glfwSetMouseButtonCallback(handle, &glfwMouseButtonCallback);
|
||
|
|
glfwSetScrollCallback(handle, &glfwScrollCallback);
|
||
|
|
glfwSetDropCallback(handle, &glfwFileCallback);
|
||
|
|
glfwSetWindowCloseCallback(handle, &glfwCloseCallback);
|
||
|
|
glfwSetFramebufferSizeCallback(handle, &glfwFramebufferResizeCallback);
|
||
|
|
|
||
|
|
metalWindow = glfwGetCocoaWindow(handle);
|
||
|
|
metalLayer = [CAMetalLayer layer];
|
||
|
|
metalLayer.device = (__bridge id<MTLDevice>)graphics->getDevice();
|
||
|
|
metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
||
|
|
metalLayer.drawableSize = CGSizeMake(createInfo.width, createInfo.height);
|
||
|
|
metalWindow.contentView.layer = metalLayer;
|
||
|
|
metalWindow.contentView.wantsLayer = YES;
|
||
|
|
}
|
||
|
|
|
||
|
|
Window::~Window()
|
||
|
|
{
|
||
|
|
glfwDestroyWindow(static_cast<GLFWwindow *>(windowHandle));
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::pollInput()
|
||
|
|
{
|
||
|
|
glfwPollEvents();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::beginFrame()
|
||
|
|
{
|
||
|
|
@autoreleasepool {
|
||
|
|
drawable = (__bridge CA::MetalDrawable*)[metalLayer nextDrawable];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::endFrame()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::onWindowCloseEvent()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
Gfx::PTexture2D Window::getBackBuffer()
|
||
|
|
{
|
||
|
|
return nullptr; //drawable->texture()
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback)
|
||
|
|
{
|
||
|
|
keyCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setMouseMoveCallback(std::function<void(double, double)> callback)
|
||
|
|
{
|
||
|
|
mouseMoveCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback)
|
||
|
|
{
|
||
|
|
mouseButtonCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setScrollCallback(std::function<void(double, double)> callback)
|
||
|
|
{
|
||
|
|
scrollCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setFileCallback(std::function<void(int, const char**)> callback)
|
||
|
|
{
|
||
|
|
fileCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setCloseCallback(std::function<void()> callback)
|
||
|
|
{
|
||
|
|
closeCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::setResizeCallback(std::function<void(uint32, uint32)> callback)
|
||
|
|
{
|
||
|
|
resizeCallback = callback;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::keyPress(KeyCode code, InputAction action, KeyModifier modifier)
|
||
|
|
{
|
||
|
|
keyCallback(code, action, modifier);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::mouseMove(double x, double y)
|
||
|
|
{
|
||
|
|
mouseMoveCallback(x, y);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier)
|
||
|
|
{
|
||
|
|
mouseButtonCallback(button, action, modifier);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::scroll(double x, double y)
|
||
|
|
{
|
||
|
|
scrollCallback(x, y);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::fileDrop(int num, const char** files)
|
||
|
|
{
|
||
|
|
fileCallback(num, files);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::close()
|
||
|
|
{
|
||
|
|
closeCallback();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Window::resize(int width, int height)
|
||
|
|
{
|
||
|
|
if(width == 0 || height == 0)
|
||
|
|
{
|
||
|
|
paused = true;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
paused = true;
|
||
|
|
resizeCallback(width, height);
|
||
|
|
}
|
||
|
|
|
||
|
|
Viewport::Viewport(PWindow owner, const ViewportCreateInfo& createInfo)
|
||
|
|
: Gfx::Viewport(owner, createInfo)
|
||
|
|
{
|
||
|
|
viewport.size.x = sizeX;
|
||
|
|
viewport.size.y = sizeY;
|
||
|
|
viewport.offset.x = offsetX;
|
||
|
|
viewport.offset.y = offsetY;
|
||
|
|
}
|
||
|
|
|
||
|
|
Viewport::~Viewport()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void Viewport::resize(uint32 newX, uint32 newY)
|
||
|
|
{
|
||
|
|
viewport.size.x = newX;
|
||
|
|
viewport.size.y = newY;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Viewport::move(uint32 newOffset, uint32 newOffsetY)
|
||
|
|
{
|
||
|
|
viewport.offset.x = newOffset;
|
||
|
|
viewport.offset.y = newOffsetY;
|
||
|
|
}
|