Skip to content
Merged
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
74 changes: 68 additions & 6 deletions atlas/application/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ void Window::initializeRunLoop() {

void Window::pollEvents() {
if (this->renderToExternalMetalView) {
SDL_PumpEvents();
return;
}
SDL_Event event;
Expand Down Expand Up @@ -1463,9 +1464,13 @@ bool Window::stepFrame() {
constexpr float MAX_DELTA_TIME = 1.0f / 30.0f;

currentFrame++;
this->relativeMousePos = {.x = 0.0f, .y = 0.0f};
this->keysPressedThisFrame.fill(false);
this->mouseButtonsPressedThisFrame.fill(false);
this->relativeMousePos = this->editorRuntimeRelativeMousePending;
this->editorRuntimeRelativeMousePending = {.x = 0.0f, .y = 0.0f};
this->keysPressedThisFrame = this->editorRuntimeKeysPressedPending;
this->editorRuntimeKeysPressedPending.fill(false);
this->mouseButtonsPressedThisFrame =
this->editorRuntimeMouseButtonsPressedPending;
this->editorRuntimeMouseButtonsPressedPending.fill(false);
this->textInputBuffer.clear();
this->pollEvents();

Expand Down Expand Up @@ -2118,6 +2123,8 @@ void Window::setEditorCameraFocused(bool focused) {

void Window::setEditorSimulationEnabled(bool enabled) {
editorSimulationEnabled = enabled;
if (!enabled)
clearEditorRuntimeInput();
editorDragging = false;
editorKeyboardTransform = false;
editorActiveGizmoAxis = 0;
Expand Down Expand Up @@ -2473,6 +2480,52 @@ void Window::editorKeyEvent(int key, bool pressed) {
editorCameraKeys[static_cast<std::size_t>(key)] = pressed;
}

void Window::editorRuntimeKeyEvent(int key, bool pressed) {
if (key < 0 || key >= static_cast<int>(editorRuntimeKeysActive.size()))
return;
const std::size_t index = static_cast<std::size_t>(key);
if (pressed && !editorRuntimeKeysActive[index])
editorRuntimeKeysPressedPending[index] = true;
editorRuntimeKeysActive[index] = pressed;
}

void Window::editorRuntimeMouseMove(float x, float y, float deltaX,
float deltaY) {
editorRuntimeRelativeMousePending.x += deltaX;
editorRuntimeRelativeMousePending.y += deltaY;
lastMouseX = x;
lastMouseY = y;
if (editorSimulationEnabled && currentScene != nullptr)
currentScene->onMouseMove(*this, {.x = deltaX, .y = deltaY});
}

void Window::editorRuntimeMouseButtonEvent(int action, int button) {
if (button <= 0 ||
button >= static_cast<int>(editorRuntimeMouseButtonsActive.size()))
return;
const std::size_t index = static_cast<std::size_t>(button);
if (action == 0) {
if (!editorRuntimeMouseButtonsActive[index])
editorRuntimeMouseButtonsPressedPending[index] = true;
editorRuntimeMouseButtonsActive[index] = true;
} else if (action == 2) {
editorRuntimeMouseButtonsActive[index] = false;
}
}

void Window::editorRuntimeScrollEvent(float x, float y) {
if (editorSimulationEnabled && currentScene != nullptr)
currentScene->onMouseScroll(*this, {.x = x, .y = y});
}

void Window::clearEditorRuntimeInput() {
editorRuntimeKeysActive.fill(false);
editorRuntimeKeysPressedPending.fill(false);
editorRuntimeMouseButtonsActive.fill(false);
editorRuntimeMouseButtonsPressedPending.fill(false);
editorRuntimeRelativeMousePending = {.x = 0.0f, .y = 0.0f};
}

void Window::selectEditorObjectAt(float x, float y, float scale) {
if (camera == nullptr) {
selectedEditorObject = nullptr;
Expand Down Expand Up @@ -4187,8 +4240,12 @@ bool Window::isKeyActive(Key key) {
int keyCount = 0;
const bool *state = SDL_GetKeyboardState(&keyCount);
const int scancode = static_cast<int>(key);
return state != nullptr && scancode >= 0 && scancode < keyCount &&
state[scancode];
const bool editorActive =
scancode >= 0 &&
scancode < static_cast<int>(editorRuntimeKeysActive.size()) &&
editorRuntimeKeysActive[static_cast<std::size_t>(scancode)];
return editorActive || (state != nullptr && scancode >= 0 &&
scancode < keyCount && state[scancode]);
}

bool Window::isKeyPressed(Key key) {
Expand All @@ -4200,7 +4257,12 @@ bool Window::isKeyPressed(Key key) {

bool Window::isMouseButtonActive(MouseButton button) {
const SDL_MouseButtonFlags state = SDL_GetMouseState(nullptr, nullptr);
return (state & SDL_BUTTON_MASK(static_cast<int>(button))) != 0;
const int index = static_cast<int>(button);
const bool editorActive =
index >= 0 &&
index < static_cast<int>(editorRuntimeMouseButtonsActive.size()) &&
editorRuntimeMouseButtonsActive[static_cast<std::size_t>(index)];
return editorActive || (state & SDL_BUTTON_MASK(index)) != 0;
}

bool Window::isMouseButtonPressed(MouseButton button) {
Expand Down
76 changes: 37 additions & 39 deletions atlas/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,48 +193,51 @@ void Camera::update(Window &window) {
void Camera::updateWithActions(Window &window, const std::string &moveAxis,
const std::string &lookAction,
const std::string &upAndDownAction) {
AxisPacket moveInput = window.getAxisActionValue(moveAxis);
AxisPacket lookInput = window.getAxisActionValue(lookAction);
AxisPacket upDownInput = window.getAxisActionValue(upAndDownAction);
AxisPacket moveInput = moveAxis.empty()
? AxisPacket{}
: window.getAxisActionValue(moveAxis);
AxisPacket lookInput = lookAction.empty()
? AxisPacket{}
: window.getAxisActionValue(lookAction);
AxisPacket upDownInput =
upAndDownAction.empty()
? AxisPacket{}
: window.getAxisActionValue(upAndDownAction);

float deltaTime = window.getDeltaTime();
float xoffset = lookInput.inputDeltaX * mouseSensitivity;
float yoffset = lookInput.inputDeltaY * mouseSensitivity;

if (lookInput.hasValueInput) {
glm::vec2 lookVector(lookInput.valueX, lookInput.valueY);
if (glm::length(lookVector) > 1.0f) {
lookVector = glm::normalize(lookVector);
if (!lookAction.empty()) {
float xoffset = lookInput.inputDeltaX * mouseSensitivity;
float yoffset = lookInput.inputDeltaY * mouseSensitivity;

if (lookInput.hasValueInput) {
glm::vec2 lookVector(lookInput.valueX, lookInput.valueY);
if (glm::length(lookVector) > 1.0f) {
lookVector = glm::normalize(lookVector);
}
xoffset +=
lookVector.x * controllerLookSensitivity * deltaTime;
yoffset +=
lookVector.y * controllerLookSensitivity * deltaTime;
}
xoffset += lookVector.x * controllerLookSensitivity * deltaTime;
yoffset += lookVector.y * controllerLookSensitivity * deltaTime;
}

glm::vec2 fallbackLook = sampleControllerAxisPair(
window, CONTROLLER_AXIS_RIGHT_X, CONTROLLER_AXIS_RIGHT_Y, true);
if (glm::length(fallbackLook) > 0.0f &&
glm::length(fallbackLook) >
glm::length(glm::vec2(lookInput.valueX, lookInput.valueY))) {
xoffset += fallbackLook.x * controllerLookSensitivity * deltaTime;
yoffset += fallbackLook.y * controllerLookSensitivity * deltaTime;
}

targetYaw += xoffset;
targetPitch += yoffset;
targetYaw += xoffset;
targetPitch += yoffset;

targetPitch = std::min(targetPitch, 89.0f);
targetPitch = std::max(targetPitch, -89.0f);
targetPitch = std::min(targetPitch, 89.0f);
targetPitch = std::max(targetPitch, -89.0f);

yaw += (targetYaw - yaw) * lookSmoothness;
pitch += (targetPitch - pitch) * lookSmoothness;
yaw += (targetYaw - yaw) * lookSmoothness;
pitch += (targetPitch - pitch) * lookSmoothness;

glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(front);
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(front);

target = {position.x + front.x, position.y + front.y, position.z + front.z};
target = {position.x + front.x, position.y + front.y,
position.z + front.z};
}

glm::vec3 camPos = glm::vec3(position.x, position.y, position.z);
glm::vec3 camFront =
Expand All @@ -244,11 +247,6 @@ void Camera::updateWithActions(Window &window, const std::string &moveAxis,
glm::vec2 moveVector = moveInput.hasValueInput
? glm::vec2(moveInput.valueX, moveInput.valueY)
: glm::vec2(moveInput.x, moveInput.y);
glm::vec2 fallbackMove = sampleControllerAxisPair(
window, CONTROLLER_AXIS_LEFT_X, CONTROLLER_AXIS_LEFT_Y, true);
if (glm::length(fallbackMove) > glm::length(moveVector)) {
moveVector = fallbackMove;
}
if (glm::length(moveVector) > 1.0f) {
moveVector = glm::normalize(moveVector);
}
Expand Down
1 change: 1 addition & 0 deletions editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ if (APPLE)
"-framework QuartzCore"
"-framework Foundation"
"-framework Cocoa"
"-framework CoreGraphics"
"-framework IOKit"
)
endif ()
29 changes: 24 additions & 5 deletions editor/views/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,6 @@ void EditorWindow::setupMenus() {
auto *toolsSettings = addCommand(toolsMenu, "Project Settings…", QString(),
[this] { showProjectSettings(); });
toolsSettings->setMenuRole(QAction::NoRole);
addCommand(toolsMenu, "Input Actions…", QString(),
[this] { showInputActions(); });
addCommand(toolsMenu, "Install Atlas Toolchain…", QString(),
[this] { ToolchainInstaller::install(this); });
addCommand(toolsMenu, "Command Palette…", "Meta+Shift+P",
Expand All @@ -589,6 +587,9 @@ void EditorWindow::setupMenus() {
windowMenu->addAction("Zoom", this, [this] {
isMaximized() ? showNormal() : showMaximized();
});
windowMenu->addSeparator();
windowMenu->addAction("Controller Actions", this,
[this] { showInputActions(); });

auto *helpMenu = menuBar()->addMenu("Help");
auto *aboutAction = helpMenu->addAction("About Atlas Engine", this, [this] {
Expand All @@ -602,9 +603,24 @@ void EditorWindow::setupMenus() {
}

void EditorWindow::showInputActions() {
InputActionsDialog dialog(projectFile, this);
if (dialog.exec() == QDialog::Accepted && viewportPanel != nullptr)
viewportPanel->reloadRuntime();
if (inputActionsDialog != nullptr) {
inputActionsDialog->showNormal();
inputActionsDialog->raise();
inputActionsDialog->activateWindow();
return;
}
inputActionsDialog = new InputActionsDialog(projectFile, this);
inputActionsDialog->setAttribute(Qt::WA_DeleteOnClose);
connect(inputActionsDialog, &InputActionsDialog::actionsSaved, this,
[this] {
if (viewportPanel != nullptr)
viewportPanel->reloadRuntime();
});
connect(inputActionsDialog, &QObject::destroyed, this,
[this] { inputActionsDialog = nullptr; });
inputActionsDialog->show();
inputActionsDialog->raise();
inputActionsDialog->activateWindow();
}

void EditorWindow::setupDocks() {
Expand Down Expand Up @@ -2193,6 +2209,9 @@ bool EditorWindow::eventFilter(QObject *watched, QEvent *event) {
dialog->setAttribute(Qt::WA_TranslucentBackground);
}
}
if (viewportPanel != nullptr &&
viewportPanel->routeRuntimeInputEvent(event))
return true;
if (event->type() == QEvent::KeyPress) {
auto *key = static_cast<QKeyEvent *>(event);
if (!key->isAutoRepeat() && key->matches(QKeySequence::Undo)) {
Expand Down
Loading
Loading