From bee3b3ea848b0b63437c86957fbd4c6d13af3b8a Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 11 Sep 2021 21:18:18 +0100
Subject: [PATCH] add edge scrolling back in

---
 demo/main.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/demo/main.cpp b/demo/main.cpp
index ab6c4d9..a341298 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -161,6 +161,54 @@ void process_freecam(fggl::ecs::ECS& ecs, InputManager input, fggl::ecs::entity_
 	camComp->target = pivot;
 }
 
+void process_edgescroll(fggl::ecs::ECS& ecs, InputManager input, fggl::ecs::entity_t cam) {
+	glm::vec3 translation(0.0f);
+
+	auto& mouse = input->mouse;
+
+	// calulate movement (user input)
+	if ( mouse.axis( MouseAxis::Y ) < 0.9f ) {
+		translation -= fggl::math::RIGHT;
+	}
+
+	if ( mouse.axis( MouseAxis::Y) > -0.9f ) {
+		translation += fggl::math::RIGHT;
+	}
+
+	if ( mouse.axis( MouseAxis::X) > -0.9f ) {
+		translation += fggl::math::FORWARD;
+	}
+
+	if ( mouse.axis( MouseAxis::X ) < 0.9f ) {
+		translation -= fggl::math::FORWARD;
+	}
+
+	// apply rotation/movement
+	auto camTransform = ecs.getComponent<fggl::math::Transform>(cam);
+	auto camComp = ecs.getComponent<fggl::gfx::Camera>(cam);
+
+	glm::vec4 position( camTransform->origin(), 1.0f );
+	glm::vec4 pivot( camComp->target, 1.0f );
+
+	// apply movement
+	if ( translation != glm::vec3(0.0f) ) {
+		const auto rotation = (position - pivot);
+		const float angle = atan2( rotation.x, rotation.z );
+		const auto rotationMat = glm::rotate( MAT_IDENTITY, angle, fggl::math::UP );
+
+		auto deltaMove = (rotationMat * glm::vec4( translation, 1.0f )) * PAN_SPEED;
+		deltaMove.w = 0.0f;
+
+		position += deltaMove;
+		pivot += deltaMove;
+	}
+
+	// move camera
+	camTransform->origin( position );
+	camComp->target = pivot;
+}
+
+
 void process_camera(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, InputManager input, fggl::ecs::entity_t cam) {
 	auto camTransform = ecs.getComponent<fggl::math::Transform>(cam);
 	auto camComp = ecs.getComponent<fggl::gfx::Camera>(cam);
@@ -181,6 +229,7 @@ void process_camera(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, InputManager
 	} else if ( cam_mode == cam_free ) {
 		process_freecam(ecs, input, cam);
 	}
+	process_edgescroll( ecs, input, cam );
 }
 
 int main(int argc, char* argv[]) {
-- 
GitLab