From b5b54c20f50a66cba7f1a21cebe80f810b06ab76 Mon Sep 17 00:00:00 2001 From: RobinsAviary Date: Wed, 1 Oct 2025 19:10:11 -0400 Subject: [PATCH 1/4] added clipboard example --- examples/core/core_clipboard_text.c | 190 ++++++++++++++++++++++++++ examples/core/core_clipboard_text.png | Bin 0 -> 15863 bytes 2 files changed, 190 insertions(+) create mode 100644 examples/core/core_clipboard_text.c create mode 100644 examples/core/core_clipboard_text.png diff --git a/examples/core/core_clipboard_text.c b/examples/core/core_clipboard_text.c new file mode 100644 index 000000000000..302e21918679 --- /dev/null +++ b/examples/core/core_clipboard_text.c @@ -0,0 +1,190 @@ +/******************************************************************************************* +* +* raylib [core] example - clipboard text +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.5 +* +* Example contributed by Robin (@RobinsAviary) 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) 2025-2025 Robin (@RobinsAviary) +* +********************************************************************************************/ + +#include "raylib.h" + +#include + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text"); + + const char* clipboardText = NULL; + + // List of text the user can switch through and copy + const char* copyableText[] = {"raylib is fun", "hello, clipboard!", "potato chips"}; + + unsigned int textIndex = 0; + + const char* popupText = NULL; + + // Initialize timers + // The amount of time the pop-up text is on screen, before fading + const float maxTime = 3.0f; + float textTimer = 0.0f; + // The length of time text is offset + const float animMaxTime = 0.1f; + float pasteAnim = 0.0f; + float copyAnim = 0.0f; + int copyAnimMult = 1; + float textAnim = 0.0f; + float textAlpha = 0.0f; + // Offset amount for animations + const int offsetAmount = -4; + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Check if the user has pressed the copy/paste key combinations + bool pastePressed = (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V)); + bool copyPressed = (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_C)); + + // Update animation timers + if (textTimer > 0) textTimer -= GetFrameTime(); + if (pasteAnim > 0) pasteAnim -= GetFrameTime(); + if (copyAnim > 0) copyAnim -= GetFrameTime(); + if (textAnim > 0) textAnim -= GetFrameTime(); + + // React to the user pressing paste + if (pastePressed) + { + // Get the text from the user's clipboard. Most operating systems explicitly hide this information until the key combination is pressed while the window is selected + clipboardText = GetClipboardText(); + + // Reset values + textTimer = maxTime; + textAnim = animMaxTime; + pasteAnim = animMaxTime; + textAlpha = 1; + // Update the text that pops up at the bottom of the screen + popupText = "text pasted"; + } + + // React to the user pressing copy + if (copyPressed) + { + // Set the text on the user's clipboard + SetClipboardText(copyableText[textIndex]); + + // Reset values + textTimer = maxTime; + textAnim = animMaxTime; + copyAnim = animMaxTime; + copyAnimMult = 1; + textAlpha = 1; + // Update the text that pops up at the bottom of the screen + popupText = "text copied"; + } + + // Switch to the next item in the list when the user presses up + if (IsKeyPressed(KEY_UP)) + { + // Reset animation + copyAnim = animMaxTime; + copyAnimMult = 1; + + textIndex += 1; + + if (textIndex >= sizeof(copyableText) / sizeof(const char*)) { // Length of array + // Loop back to the other end + textIndex = 0; + } + } + + // Switch to the previous item in the list when the user presses down + if (IsKeyPressed(KEY_DOWN)) + { + // Reset animation + copyAnim = animMaxTime; + copyAnimMult = -1; + + if (textIndex == 0) + { + // Loop back to the other end + textIndex = (sizeof(copyableText) / sizeof(const char*)) - 1; // Length of array minus one + } + else + { + textIndex -= 1; + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + // Draw the user's pasted text, if there is any yet + if (clipboardText) + { + // Offset animation + int offset = 0; + if (pasteAnim > 0) offset = offsetAmount; + + // Draw the pasted text + DrawText("pasted clipboard:", 10, 10 + offset, 20, DARKGREEN); + DrawText(clipboardText, 10, 30 + offset, 20, DARKGRAY); + } + + // Offset animation + int textOffset = 0; + if (copyAnim > 0) textOffset = offsetAmount; + + // Draw copyable text and controls + DrawText(copyableText[textIndex], 10, 330 + (textOffset * copyAnimMult), 20, MAROON); + DrawText("up/down to change string, ctrl-c to copy, ctrl-v to paste", 10, 355, 20, DARKGRAY); + + // Alpha / Offset animation + if (textAlpha > 0) + { + // Offset animation + int offset = 0; + if (textAnim > 0) offset = offsetAmount; + // Draw pop up text + DrawText(popupText, 10, 425 + offset, 20, ColorAlpha(DARKGREEN, textAlpha)); + + // Fade-out animation + if (textTimer < 0) + { + textAlpha -= GetFrameTime(); + } + } + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/core/core_clipboard_text.png b/examples/core/core_clipboard_text.png new file mode 100644 index 0000000000000000000000000000000000000000..437d8ea1615371b5a70271a8973797487691d22f GIT binary patch literal 15863 zcmeHOeK=J49-l!>twB*3Z!?REi^No3nlNexA<^5F?06rPB$J{v#HibdwpPXFWyedC z6}nQ*HZ4ZmY|%AK=^+)B^-dRgc^cg_b9&k3%(?qK`^Wa&d;U3(Ib+WG{64?$_w)Py z&iQ;#czIH>3vdfiC=}M!WxY2FC67m;&~+F^;EZNanGl5{-F98?wB?|G(A(+p{-dnT zmN+>MLB=5#gDw_%>0^^bYFdy7bbM+JT`8DYmd>i7W7rS}OZEXrih3>dm%tAyzDN>j zXNuO7WWjK}uBJ7}WG}6jiGc@VsFaC80-;$3J^;c92w}@#3xQxSU)Mo<0k9T`>HjMArEFq3g`_mS$g)SxVEOQ=UowH}=QV3EqQuLo6C}V+5Mp4;Hjj z%O^RETls0N^b1x{^JxP>RtzQ@pMnS!Y49|eE4VzBPmWw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pygj*fFN}{l1{^w3tw}w{j zhIDSX5iITGt+JD;5RPD}_p@h-9Q&&u6U7c1bvci7n$qIiv`uDo8(%^IiRFSs5X9?#au9AXotoWBgA+aIThLq^H-2s;g&daQl>Y;&e@i4Ej3PHof*N z9nrb++RD2wlK2&NXj?7aPWSI+)uOjHZEglw^gYlSyBN+HyMyJsLl8=m7+JI3rDHx0 z1lL=u>@BlMQQQco@*U_@`IO3``c1D+bmTb=2+bKZpbw+d4g$ z`?)fd+(|cIJAR))-?NSom#JgWaw^PiblJ7$#aE(r%sMxO&v!2!!3|91`A=zY=Q~~c zws@=9((9FV5b5^sv&lOv{FciValusi8F2LW_J zAZ7h9=xXvw^G^Ck9-=n?@?0gU)=qByp19L?G3&CUl)^A(dZZ(JiQy{AYFZv?lZHu8 z{++Y!)mV@FU3%q8oZNXZYfWcO)TfU?)*g#e436y%3NB8J7`~ZL)ZnhFa~aYh0IJ+eQ}%{Ro}$?oXfaU z!KAL~R9@Q@mgGliCWPA>WT`Iq_}7g-r}LAJt?8V7N6l9f90y~qvQ^T;csk~rDEB2c{zQ<>%Wx8C z#t8rvS756k+|_QMZR+LBY>Cz-jfW8DiM~{zGXIQU4#)=k_Q(JgD}n;f$R+nav< zFLh4fgsED#h&iR!>W(S6urOlq0{)?Hpg48NVNA=Ip~%`H2FhGwIdr9sB7ucCANp%x ziwEpZ=?4}p;l{C^XV2H}4G*wRxi4jd`%+uDed+SsN($ZHW2fWE37VL}DZEj}{lhU~{?Vy6+!^wk zv|mWN^(@1Nry`Oer|2#enKTvf;esvnL`a*zXp%1EccPKvL=1Z Date: Thu, 2 Oct 2025 15:18:53 -0400 Subject: [PATCH 2/4] added image check --- examples/core/core_clipboard_text.c | 35 +++++++++++++++++++------- examples/core/core_clipboard_text.png | Bin 15863 -> 15878 bytes 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/examples/core/core_clipboard_text.c b/examples/core/core_clipboard_text.c index 302e21918679..9e9f3a3d13a7 100644 --- a/examples/core/core_clipboard_text.c +++ b/examples/core/core_clipboard_text.c @@ -1,10 +1,10 @@ -/******************************************************************************************* +/******************************************************************************************* * * raylib [core] example - clipboard text * * Example complexity rating: [★☆☆☆] 1/4 * -* Example originally created with raylib 5.5 +* Example originally created with raylib 5.6-dev * * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5) * @@ -73,16 +73,32 @@ int main(void) // React to the user pressing paste if (pastePressed) { - // Get the text from the user's clipboard. Most operating systems explicitly hide this information until the key combination is pressed while the window is selected - clipboardText = GetClipboardText(); + // Most operating systems hide this information until the user presses Ctrl-V on the window. - // Reset values + // Check to see if the clipboard contains an image + Image image = GetClipboardImage(); + + if (IsImageValid(image)) + { + // Unload the image + UnloadImage(image); + // Update visuals + popupText = "clipboard contains image"; + } + else + { + // Get text from the user's clipboard + clipboardText = GetClipboardText(); + + // Update visuals + popupText = "text pasted"; + pasteAnim = animMaxTime; + } + + // Reset animation values textTimer = maxTime; textAnim = animMaxTime; - pasteAnim = animMaxTime; textAlpha = 1; - // Update the text that pops up at the bottom of the screen - popupText = "text pasted"; } // React to the user pressing copy @@ -110,7 +126,8 @@ int main(void) textIndex += 1; - if (textIndex >= sizeof(copyableText) / sizeof(const char*)) { // Length of array + if (textIndex >= sizeof(copyableText) / sizeof(const char*)) // Length of array + { // Loop back to the other end textIndex = 0; } diff --git a/examples/core/core_clipboard_text.png b/examples/core/core_clipboard_text.png index 437d8ea1615371b5a70271a8973797487691d22f..caa9b314a694bdb9316f9f8d07161887d53887e6 100644 GIT binary patch delta 457 zcmV;)0XF{kdxm_FBmvE_B{Vt;!$A3KxQ6FwVKtM%4-^SFLtu0X)@aHclfelU41v)Q zpj!xxc0fiG+mnF`83kwuWHb$vp#c{-Xb23y5MUwIOX&MRzBZghF@o5AA~1b0_2}ln z48N}YlR*j>2@D}Hx&&)99gdSRJR37eo&mu% zQ-SVcLNgjL^|)q22+fQT>kdL8Nvb<>nTKmeg;aM^JABc@8QuMZ;bgPX{63Sx4-^P; zLSS?TWHhytFgzR_SZHMvsUz#e?g<&Fv_bAn0U>?l&g6{FT#S|ylkox+2}3aiMrS}q z)9sUi3K$>&-9mt7Ga#gn?4$4bAhZW$pwbAf_lywhFKX?98O>{xu?!ao&kz`$0U1qi zlaVzUDs&40ithn|xs=rVDhTcQAS6$%Q>RGXOF^u9YVR2#H+*67GrDJFv{V_B!3h)y z3?VQ&12UQplQAwL2@=D5um0TRTC>N=CyxKV-DwT36Pq6kW+*+O3`2!OSZ|;zR8V@5&{==St8sH zR~57zxZXB-aiHO3K~r%KI~KQe2RGrLlN-4t^ce0sH9V0N_)@FzLiziVcfyS?n^zt; zWp41EH^G{tRbl5ll@z^Q`#nPaSN^-W$9VEVelgBwORxc#pCcQP^j%9}*2RXO0@r;w z7S0sRdOK6$s!Y<~)_3+_Bveb;+9Lig*_Cgye)2~iF-`_s=Y$fr6qCI`o}@U?jTvQJ zVk^tr9?asDe3_YOaA&X4EcwZXdSaXnK<`*F8_k+FInY!_++;SRVrf3>uX+4V)edf6 zQa`P#>`!DAPTt5a%Gu)tHqx>m6)VSxPn%T zmH(W~+b`M59BETNbMK<9^5Uh(Co3|Fa3=6IUX+}0@rxzUm(qd>e|~*^{r}>JWpx}q z?-C__LnkY;igQRZEjCtIyfSI>MH8{f1}v Date: Thu, 2 Oct 2025 15:34:10 -0400 Subject: [PATCH 3/4] added note about windows --- examples/core/core_clipboard_text.c | 37 +++++++++++++++-------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/examples/core/core_clipboard_text.c b/examples/core/core_clipboard_text.c index 9e9f3a3d13a7..1e5c8db98df2 100644 --- a/examples/core/core_clipboard_text.c +++ b/examples/core/core_clipboard_text.c @@ -76,24 +76,25 @@ int main(void) // Most operating systems hide this information until the user presses Ctrl-V on the window. // Check to see if the clipboard contains an image - Image image = GetClipboardImage(); - - if (IsImageValid(image)) - { - // Unload the image - UnloadImage(image); - // Update visuals - popupText = "clipboard contains image"; - } - else - { - // Get text from the user's clipboard - clipboardText = GetClipboardText(); - - // Update visuals - popupText = "text pasted"; - pasteAnim = animMaxTime; - } + // This function does nothing outside of Windows, as it directly calls the Windows API + Image image = GetClipboardImage(); + + if (IsImageValid(image)) + { + // Unload the image + UnloadImage(image); + // Update visuals + popupText = "clipboard contains image"; + } + else + { + // Get text from the user's clipboard + clipboardText = GetClipboardText(); + + // Update visuals + popupText = "text pasted"; + pasteAnim = animMaxTime; + } // Reset animation values textTimer = maxTime; From b2c48cd5d4efe9d5d9c4619523a827c51dbe79e3 Mon Sep 17 00:00:00 2001 From: RobinsAviary Date: Thu, 2 Oct 2025 18:01:10 -0400 Subject: [PATCH 4/4] added indent --- examples/core/core_clipboard_text.c | 102 ++++++++++++++-------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/examples/core/core_clipboard_text.c b/examples/core/core_clipboard_text.c index 1e5c8db98df2..973163cb5328 100644 --- a/examples/core/core_clipboard_text.c +++ b/examples/core/core_clipboard_text.c @@ -76,25 +76,25 @@ int main(void) // Most operating systems hide this information until the user presses Ctrl-V on the window. // Check to see if the clipboard contains an image - // This function does nothing outside of Windows, as it directly calls the Windows API - Image image = GetClipboardImage(); - - if (IsImageValid(image)) - { - // Unload the image - UnloadImage(image); - // Update visuals - popupText = "clipboard contains image"; - } - else - { - // Get text from the user's clipboard - clipboardText = GetClipboardText(); - - // Update visuals - popupText = "text pasted"; - pasteAnim = animMaxTime; - } + // This function does nothing outside of Windows, as it directly calls the Windows API + Image image = GetClipboardImage(); + + if (IsImageValid(image)) + { + // Unload the image + UnloadImage(image); + // Update visuals + popupText = "clipboard contains image"; + } + else + { + // Get text from the user's clipboard + clipboardText = GetClipboardText(); + + // Update visuals + popupText = "text pasted"; + pasteAnim = animMaxTime; + } // Reset animation values textTimer = maxTime; @@ -157,43 +157,43 @@ int main(void) //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - // Draw the user's pasted text, if there is any yet - if (clipboardText) - { - // Offset animation - int offset = 0; - if (pasteAnim > 0) offset = offsetAmount; + // Draw the user's pasted text, if there is any yet + if (clipboardText) + { + // Offset animation + int offset = 0; + if (pasteAnim > 0) offset = offsetAmount; - // Draw the pasted text - DrawText("pasted clipboard:", 10, 10 + offset, 20, DARKGREEN); - DrawText(clipboardText, 10, 30 + offset, 20, DARKGRAY); - } + // Draw the pasted text + DrawText("pasted clipboard:", 10, 10 + offset, 20, DARKGREEN); + DrawText(clipboardText, 10, 30 + offset, 20, DARKGRAY); + } - // Offset animation - int textOffset = 0; - if (copyAnim > 0) textOffset = offsetAmount; + // Offset animation + int textOffset = 0; + if (copyAnim > 0) textOffset = offsetAmount; - // Draw copyable text and controls - DrawText(copyableText[textIndex], 10, 330 + (textOffset * copyAnimMult), 20, MAROON); - DrawText("up/down to change string, ctrl-c to copy, ctrl-v to paste", 10, 355, 20, DARKGRAY); + // Draw copyable text and controls + DrawText(copyableText[textIndex], 10, 330 + (textOffset * copyAnimMult), 20, MAROON); + DrawText("up/down to change string, ctrl-c to copy, ctrl-v to paste", 10, 355, 20, DARKGRAY); - // Alpha / Offset animation - if (textAlpha > 0) - { - // Offset animation - int offset = 0; - if (textAnim > 0) offset = offsetAmount; - // Draw pop up text - DrawText(popupText, 10, 425 + offset, 20, ColorAlpha(DARKGREEN, textAlpha)); - - // Fade-out animation - if (textTimer < 0) - { - textAlpha -= GetFrameTime(); - } - } + // Alpha / Offset animation + if (textAlpha > 0) + { + // Offset animation + int offset = 0; + if (textAnim > 0) offset = offsetAmount; + // Draw pop up text + DrawText(popupText, 10, 425 + offset, 20, ColorAlpha(DARKGREEN, textAlpha)); + + // Fade-out animation + if (textTimer < 0) + { + textAlpha -= GetFrameTime(); + } + } EndDrawing(); //----------------------------------------------------------------------------------