Skip to content

Commit

Permalink
Mouse Drag
Browse files Browse the repository at this point in the history
  • Loading branch information
quadpixels committed Jun 17, 2024
1 parent de59594 commit 6800406
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ glm::vec3 WindowCoordToPickRayDir(Camera* cam, int x, int y) {
return ret;
}

glm::vec3 WindowCoordToGamePlane(Camera* cam, int x, int y) {
glm::vec3 ray_dir = WindowCoordToPickRayDir(cam, x, y);
glm::vec3 orig = cam->pos;
float t = (0 - orig.z) / ray_dir.z;
glm::vec3 ret = orig + ray_dir * t;
return ret;
}

// These are all scaffolds
Triangle *g_triangle[2];
ColorCube *g_colorcube[2];
Expand Down
7 changes: 7 additions & 0 deletions main_d3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ void OnKeyUp(WPARAM wParam, LPARAM lParam) {
void OnMouseMove(int x, int y) {
g_mouse_x = x;
g_mouse_y = y + g_titlebar_size;
GetCurrentGameScene()->OnMouseMove(g_mouse_x, g_mouse_y);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Expand Down Expand Up @@ -1066,6 +1067,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
case WM_MOUSEMOVE:
OnMouseMove(GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam));
break;
case WM_LBUTTONDOWN:
GetCurrentGameScene()->OnMouseDown();
break;
case WM_LBUTTONUP:
GetCurrentGameScene()->OnMouseUp();
break;
default:
return DefWindowProc(hwnd, message, wparam, lparam);
}
Expand Down
35 changes: 34 additions & 1 deletion scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ extern bool IsGL();
extern bool IsD3D11();
extern bool IsD3D12();
extern glm::vec3 WindowCoordToPickRayDir(Camera* cam, int x, int y);
extern glm::vec3 WindowCoordToGamePlane(Camera* cam, int x, int y);
extern int g_mouse_x, g_mouse_y;

const glm::vec3 ClimbScene::PLAYER_ROPE_ENDPOINT[8] =
Expand Down Expand Up @@ -155,22 +156,33 @@ void ClimbScene::PrepareSpriteListForRender() {
bool should_add_hl = false;
glm::vec3 campos = camera->pos;
campos.z = 0;
hovered_sprite = nullptr;
for (Platform* p : platforms) {
Sprite* sp = p->GetSpriteForDisplay();
if (this->game_state == ClimbGameStateInEditing &&
sp != nullptr &&
curr_edit_option == 0) {
ChunkSprite* csp = dynamic_cast<ChunkSprite*>(sp);

if (is_dragging && dragged_sprite) {
glm::vec3 drag_delta = WindowCoordToGamePlane(camera, mouse_x, mouse_y) - drag_pos0;
dragged_sprite->pos = dragged_sprite_pos0 + drag_delta;
}

if (csp) {
AABB aabb = csp->GetAABBInWorld();

glm::vec3 pickray_dir = WindowCoordToPickRayDir(camera, g_mouse_x, g_mouse_y);
glm::vec3 pickray_dir = WindowCoordToPickRayDir(camera, mouse_x, mouse_y);
glm::vec3 pickray_orig = camera->pos;

bool x1 = (campos.x >= aabb.lb.x && campos.x <= aabb.ub.x &&
campos.y >= aabb.lb.y && campos.y <= aabb.ub.y);
bool x2 = aabb.IntersectRay(pickray_orig, pickray_dir);

if (x2) {
hovered_sprite = csp;
}

if (x1 || x2) {
highlight_sprite->pos = sp->pos;
float phase = fabs(sin(light_phase * 4));
Expand Down Expand Up @@ -1168,6 +1180,27 @@ Sprite* ClimbScene::CurrentCursorSprite() {
} else return nullptr;
}

void ClimbScene::OnMouseMove(int mx, int my) {
mouse_x = mx;
mouse_y = my;
}

void ClimbScene::OnMouseDown() {
is_dragging = true;
drag_mouse_x0 = mouse_x;
drag_mouse_y0 = mouse_y;
drag_pos0 = WindowCoordToGamePlane(camera, drag_mouse_x0, drag_mouse_y0);
dragged_sprite = hovered_sprite;
if (dragged_sprite) {
dragged_sprite_pos0 = dragged_sprite->pos;
}
}

void ClimbScene::OnMouseUp() {
is_dragging = false;
dragged_sprite = nullptr;
}

// 2019-12-18
// 2020-01-01 ?????
#ifdef WIN32
Expand Down
14 changes: 14 additions & 0 deletions scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class GameScene {
virtual void OnKeyPressed(char key) { };
virtual void OnKeyReleased(char key) { };

virtual void OnMouseMove(int mx, int my) { };
virtual void OnMouseDown() { };
virtual void OnMouseUp() { };

Camera* camera;

GameScene() : camera(&g_cam) { }
Expand Down Expand Up @@ -315,6 +319,16 @@ class ClimbScene : public GameScene {
Sprite* CurrentCursorSprite();
Sprite* highlight_sprite;
bool CanHideMenu();

int drag_mouse_x0, drag_mouse_y0;
glm::vec3 drag_pos0, dragged_sprite_pos0;
int mouse_x, mouse_y;
bool is_dragging = false;
ChunkSprite* dragged_sprite, *hovered_sprite;

virtual void OnMouseMove(int mx, int my);
virtual void OnMouseDown();
virtual void OnMouseUp();
};

class LightTestScene : public GameScene {
Expand Down

0 comments on commit 6800406

Please sign in to comment.