Support mouse input on example #1465

This commit is contained in:
Ray 2020-12-18 18:36:04 +01:00
parent 0987507ef5
commit 893a64712e

View file

@ -57,9 +57,10 @@ int main(void)
int currentProcess = NONE; int currentProcess = NONE;
bool textureReload = false; bool textureReload = false;
Rectangle selectRecs[NUM_PROCESSES] = { 0 }; Rectangle toggleRecs[NUM_PROCESSES] = { 0 };
int mouseHoverRec = -1;
for (int i = 0; i < NUM_PROCESSES; i++) selectRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f }; for (int i = 0; i < NUM_PROCESSES; i++) toggleRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
SetTargetFPS(60); SetTargetFPS(60);
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
@ -69,6 +70,25 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Mouse toggle group logic
for (int i = 0; i < NUM_PROCESSES; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
{
mouseHoverRec = i;
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
currentProcess = i;
textureReload = true;
}
break;
}
else mouseHoverRec = -1;
}
// Keyboard toggle group logic
if (IsKeyPressed(KEY_DOWN)) if (IsKeyPressed(KEY_DOWN))
{ {
currentProcess++; currentProcess++;
@ -82,6 +102,7 @@ int main(void)
textureReload = true; textureReload = true;
} }
// Reload texture when required
if (textureReload) if (textureReload)
{ {
UnloadImage(image); // Unload current image data UnloadImage(image); // Unload current image data
@ -121,9 +142,9 @@ int main(void)
// Draw rectangles // Draw rectangles
for (int i = 0; i < NUM_PROCESSES; i++) for (int i = 0; i < NUM_PROCESSES; i++)
{ {
DrawRectangleRec(selectRecs[i], (i == currentProcess) ? SKYBLUE : LIGHTGRAY); DrawRectangleRec(toggleRecs[i], ((i == currentProcess) || (i == mouseHoverRec)) ? SKYBLUE : LIGHTGRAY);
DrawRectangleLines((int)selectRecs[i].x, (int) selectRecs[i].y, (int) selectRecs[i].width, (int) selectRecs[i].height, (i == currentProcess) ? BLUE : GRAY); DrawRectangleLines((int)toggleRecs[i].x, (int) toggleRecs[i].y, (int) toggleRecs[i].width, (int) toggleRecs[i].height, ((i == currentProcess) || (i == mouseHoverRec)) ? BLUE : GRAY);
DrawText( processText[i], (int)( selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2), (int) selectRecs[i].y + 11, 10, (i == currentProcess) ? DARKBLUE : DARKGRAY); DrawText( processText[i], (int)( toggleRecs[i].x + toggleRecs[i].width/2 - MeasureText(processText[i], 10)/2), (int) toggleRecs[i].y + 11, 10, ((i == currentProcess) || (i == mouseHoverRec)) ? DARKBLUE : DARKGRAY);
} }
DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE); DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE);