You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.1 KiB

2 years ago
2 years ago
2 years ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - rectangle scaling by mouse
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  6. *
  7. * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2018-2024 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define MOUSE_SCALE_MARK_SIZE 12
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse");
  27. Rectangle rec = { 100, 100, 200, 80 };
  28. Vector2 mousePosition = { 0 };
  29. bool mouseScaleReady = false;
  30. bool mouseScaleMode = false;
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. mousePosition = GetMousePosition();
  39. if (CheckCollisionPointRec(mousePosition, (Rectangle){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE }))
  40. {
  41. mouseScaleReady = true;
  42. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) mouseScaleMode = true;
  43. }
  44. else mouseScaleReady = false;
  45. if (mouseScaleMode)
  46. {
  47. mouseScaleReady = true;
  48. rec.width = (mousePosition.x - rec.x);
  49. rec.height = (mousePosition.y - rec.y);
  50. // Check minimum rec size
  51. if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
  52. if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
  53. // Check maximum rec size
  54. if (rec.width > (GetScreenWidth() - rec.x)) rec.width = GetScreenWidth() - rec.x;
  55. if (rec.height > (GetScreenHeight() - rec.y)) rec.height = GetScreenHeight() - rec.y;
  56. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) mouseScaleMode = false;
  57. }
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(RAYWHITE);
  63. DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
  64. DrawRectangleRec(rec, Fade(GREEN, 0.5f));
  65. if (mouseScaleReady)
  66. {
  67. DrawRectangleLinesEx(rec, 1, RED);
  68. DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
  69. (Vector2){ rec.x + rec.width, rec.y + rec.height },
  70. (Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
  71. }
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }