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.

93 regels
3.6 KiB

5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - rectangle scaling by mouse
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MOUSE_SCALE_MARK_SIZE 12
  15. int main(void)
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. const int screenWidth = 800;
  20. const int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse");
  22. Rectangle rec = { 100, 100, 200, 80 };
  23. Vector2 mousePosition = { 0 };
  24. bool mouseScaleReady = false;
  25. bool mouseScaleMode = false;
  26. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  27. //--------------------------------------------------------------------------------------
  28. // Main game loop
  29. while (!WindowShouldClose()) // Detect window close button or ESC key
  30. {
  31. // Update
  32. //----------------------------------------------------------------------------------
  33. mousePosition = GetMousePosition();
  34. if (CheckCollisionPointRec(mousePosition, rec) &&
  35. 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 }))
  36. {
  37. mouseScaleReady = true;
  38. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) mouseScaleMode = true;
  39. }
  40. else mouseScaleReady = false;
  41. if (mouseScaleMode)
  42. {
  43. mouseScaleReady = true;
  44. rec.width = (mousePosition.x - rec.x);
  45. rec.height = (mousePosition.y - rec.y);
  46. if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
  47. if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
  48. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) mouseScaleMode = false;
  49. }
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
  56. DrawRectangleRec(rec, Fade(GREEN, 0.5f));
  57. if (mouseScaleReady)
  58. {
  59. DrawRectangleLinesEx(rec, 1, RED);
  60. DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
  61. (Vector2){ rec.x + rec.width, rec.y + rec.height },
  62. (Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
  63. }
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. CloseWindow(); // Close window and OpenGL context
  70. //--------------------------------------------------------------------------------------
  71. return 0;
  72. }