2019-04-11 19:07:20 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [shapes] example - rectangle scaling by mouse
|
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example originally created with raylib 2.5, last time updated with raylib 2.5
|
2019-04-11 19:07:20 +03:00
|
|
|
*
|
2019-05-14 16:34:23 +03:00
|
|
|
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
|
|
|
|
*
|
2022-07-20 02:28:37 +03:00
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
|
|
* BSD-like license that allows static linking with closed source software
|
|
|
|
*
|
2023-01-01 18:00:56 +03:00
|
|
|
* Copyright (c) 2018-2023 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
|
2019-04-11 19:07:20 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#define MOUSE_SCALE_MARK_SIZE 12
|
|
|
|
|
2022-06-21 20:53:18 +03:00
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Program main entry point
|
|
|
|
//------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2019-04-11 19:07:20 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2019-04-11 19:07:20 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse");
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
Rectangle rec = { 100, 100, 200, 80 };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
Vector2 mousePosition = { 0 };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
bool mouseScaleReady = false;
|
|
|
|
bool mouseScaleMode = false;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2019-04-11 19:07:20 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
mousePosition = GetMousePosition();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2022-03-24 13:44:52 +03:00
|
|
|
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 }))
|
2019-04-11 19:07:20 +03:00
|
|
|
{
|
|
|
|
mouseScaleReady = true;
|
2021-05-08 19:26:24 +03:00
|
|
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) mouseScaleMode = true;
|
2019-04-11 19:07:20 +03:00
|
|
|
}
|
|
|
|
else mouseScaleReady = false;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
if (mouseScaleMode)
|
|
|
|
{
|
|
|
|
mouseScaleReady = true;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
rec.width = (mousePosition.x - rec.x);
|
|
|
|
rec.height = (mousePosition.y - rec.y);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2022-03-24 13:44:52 +03:00
|
|
|
// Check minimum rec size
|
2019-04-11 19:07:20 +03:00
|
|
|
if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
|
|
|
|
if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
|
2022-03-24 13:44:52 +03:00
|
|
|
|
|
|
|
// Check maximum rec size
|
|
|
|
if (rec.width > (GetScreenWidth() - rec.x)) rec.width = GetScreenWidth() - rec.x;
|
|
|
|
if (rec.height > (GetScreenHeight() - rec.y)) rec.height = GetScreenHeight() - rec.y;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2021-05-08 19:26:24 +03:00
|
|
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) mouseScaleMode = false;
|
2019-04-11 19:07:20 +03:00
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2019-04-11 19:07:20 +03:00
|
|
|
DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
|
|
|
|
|
|
|
|
DrawRectangleRec(rec, Fade(GREEN, 0.5f));
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
if (mouseScaleReady)
|
2019-04-11 19:07:20 +03:00
|
|
|
{
|
|
|
|
DrawRectangleLinesEx(rec, 1, RED);
|
2019-05-20 17:36:42 +03:00
|
|
|
DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
|
2019-04-11 19:07:20 +03:00
|
|
|
(Vector2){ rec.x + rec.width, rec.y + rec.height },
|
|
|
|
(Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
2019-05-20 17:36:42 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
2019-04-11 19:07:20 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|