From 9a6288455f840e50c69a1dc240658b95747d4ff2 Mon Sep 17 00:00:00 2001 From: Maicon Santana Date: Fri, 1 Aug 2025 15:37:21 +0100 Subject: [PATCH] Add simple side to side ui controller example --- examples/core/core_input_ui.c | 113 ++++++++++++++++++++++++++++++++ examples/core/core_input_ui.png | Bin 0 -> 6740 bytes 2 files changed, 113 insertions(+) create mode 100644 examples/core/core_input_ui.c create mode 100644 examples/core/core_input_ui.png diff --git a/examples/core/core_input_ui.c b/examples/core/core_input_ui.c new file mode 100644 index 000000000..c2b71aa5b --- /dev/null +++ b/examples/core/core_input_ui.c @@ -0,0 +1,113 @@ +/******************************************************************************************* +* +* raylib [core] example - UI input +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.6, last time updated with raylib 5.6 +* +* Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2018-2025 Maicon Santana (@maiconpintoabreu) +* +********************************************************************************************/ + +#include "raylib.h" + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +static bool UIButtomIcon(Vector2 position, Texture2D icon); // Check if button clicked + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - ui input"); + Image arrowImage = GenImageColor(64, 64, BLANK); + ImageDrawTriangle(&arrowImage, (Vector2){4, 31}, (Vector2){37, 4}, (Vector2){37, 59}, GRAY); + Texture2D leftArrow = LoadTextureFromImage(arrowImage); + ImageRotate(&arrowImage, 180); + Texture2D rightArrow = LoadTextureFromImage(arrowImage); + UnloadImage(arrowImage); + + Vector2 ballPosition = { (float)screenWidth/2, (float)screenHeight/3 }; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsKeyDown(KEY_RIGHT)) ballPosition.x += 2.0f; + if (IsKeyDown(KEY_LEFT)) ballPosition.x -= 2.0f; + if (IsKeyDown(KEY_UP)) ballPosition.y -= 2.0f; + if (IsKeyDown(KEY_DOWN)) ballPosition.y += 2.0f; + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawText("move the ball with ui buttons", 10, 10, 20, DARKGRAY); + + if (UIButtomIcon((Vector2){250, screenHeight - 120}, rightArrow)) ballPosition.x += 2.0f; + if (UIButtomIcon((Vector2){100, screenHeight - 120}, leftArrow)) ballPosition.x -= 2.0f; + + DrawCircleV(ballPosition, 50, MAROON); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + + UnloadTexture(rightArrow); + UnloadTexture(leftArrow); + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} + +static bool UIButtomIcon(Vector2 position, Texture2D icon) { + // Check for Houver and Draw the button + //-------------------------------------------------------------------------------------- + bool checkHouver = CheckCollisionPointCircle(GetMousePosition(), position, icon.width); + DrawCircle(position.x, position.y, icon.width, checkHouver ? LIGHTGRAY : DARKGRAY); + DrawTexture(icon, position.x - icon.width / 2, position.y - icon.width / 2, WHITE); + //-------------------------------------------------------------------------------------- + + // Check Mouse Left Button + //-------------------------------------------------------------------------------------- + if (checkHouver && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { + return true; + } + //-------------------------------------------------------------------------------------- + + // Check All Current Touch Points + //-------------------------------------------------------------------------------------- + int touchCount = GetTouchPointCount(); + for (int i = 0; i < touchCount; i++) { + if (CheckCollisionPointCircle(GetTouchPosition(i), position, icon.width)) { + return true; + } + } + //-------------------------------------------------------------------------------------- + return false; +} \ No newline at end of file diff --git a/examples/core/core_input_ui.png b/examples/core/core_input_ui.png new file mode 100644 index 0000000000000000000000000000000000000000..497265f74dc7ee22cad76a10c14919bf0b311f87 GIT binary patch literal 6740 zcmc&(2{@E%`+we*`_GL&26`j(CbV^!IL}Uq( zrLu%0CQ8$z;UD^2unn*t*E?l$4Z2rByXl6xBD8l@!q+2rDZqJAs{_gM(jDTu5B;fBmAD0ul>|M<7`V zfFU7R5<;&4q7Ww&!gwuFL5X5 z<~Fs(OL+1qh9%r!k<`g5<27p>T}!s}3TI{GTg@*ZD78*nX8i^wggL8 z8g1XR*L>f8ivyPSzdATNIlCP3K7PW-*UvxTWW=e+sOZx%=YP9!G4aynD_4KNc`NO9 z`klM?vLEH-KF-T8D1264QCao;#mnkHn_f4!(B8DRb#?dje)!n;sefQ>d}4BH`peAh z9Lfvj`R%pD?B95iATJCKhsEJhUI@k)B~HRIi7PO#(l*80dU8uBhOzMIB;3d-l3eu zIOun8a}uz9tNAQ%%esaq`?0B&OgudsIMqD=q>?vJRWxPb%q*=81s+RQm_)j#J??y{ zTB?!NbTxL@7~2uc)42!t$DUSAi0m}ci{Bec+tV=4+a12yg; z?>crku`AXonLd|1wL!(FjoAn*d6y0Z6NUGx@6X=hs$( z z<02(zxI%h51jTtU7%G>w%y`q+*#1AMis>}HqPR1*9?$D6;w395xJmI-o^e2XULmFK zT7?l#v9|5Rgu$B6)zPo?t@ysqZ+;QbbLTqA?1OA^cYckypJE7UoySub+5xmd2u4(E8AH6jtarY8Ly6I_Fb{)z2PRkFs`y7Gsb+Yt>kH7 zkX2#LdKu#{c)}cVAh$-ffc#lrMbIz6C3ob6W8$@Lj)R83ZrM8V?4B2K%bvwq^^l80 z)~sVWAyTIvWSHB%wzJC4Y)=R+DoN=s+LTkIb$3fbX6s?ROxAWn!a{j@NJJxR{=?@s zF5T-NUn*|AZrG^D`#Q+tA1H-P_(m(W{Hgr)?W$9J>kTQP`TmC+V(K!FkL88ikI8QI zcB|pLxvJ|x$x!S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