22*
33* raylib [core] example - clipboard text
44*
5- * Example complexity rating: [★☆☆☆] 1/4
6- *
75* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
86*
9- * Example contributed by Robin (@RobinsAviary ) and reviewed by Ramon Santamaria (@raysan5)
7+ * Example contributed by Ananth S (@Ananth1839 ) and reviewed by Ramon Santamaria (@raysan5)
108*
119* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
1210* BSD-like license that allows static linking with closed source software
1311*
14- * Copyright (c) 2025 Robin (@RobinsAviary )
12+ * Copyright (c) 2025 Ananth S (@Ananth1839 )
1513*
1614********************************************************************************************/
1715
1816#include "raylib.h"
1917
20- #include <stdio.h>
18+ #define RAYGUI_IMPLEMENTATION
19+ #include "raygui.h"
20+
21+ #define MAX_TEXT_SAMPLES 5
2122
2223//------------------------------------------------------------------------------------
2324// Program main entry point
@@ -31,155 +32,122 @@ int main(void)
3132
3233 InitWindow (screenWidth , screenHeight , "raylib [core] example - clipboard text" );
3334
34- const char * clipboardText = NULL ;
35+ // Define some sample texts
36+ const char * sampleTexts [MAX_TEXT_SAMPLES ] = {
37+ "Hello from raylib!" ,
38+ "The quick brown fox jumps over the lazy dog" ,
39+ "Clipboard operations are useful!" ,
40+ "raylib is a simple and easy-to-use library" ,
41+ "Copy and paste me!"
42+ };
3543
36- // List of text the user can switch through and copy
37- const char * copyableText [ ] = { "raylib is fun" , "hello, clipboard!" , "potato chips" };
44+ char * clipboardText = NULL ;
45+ char inputBuffer [ 256 ] = "Hello from raylib!" ; // Random initial string
3846
39- unsigned int textIndex = 0 ;
47+ // UI required variables
48+ bool textBoxEditMode = false;
4049
41- const char * popupText = NULL ;
50+ bool btnCutPressed = false;
51+ bool btnCopyPressed = false;
52+ bool btnPastePressed = false;
53+ bool btnClearPressed = false;
54+ bool btnRandomPressed = false;
4255
43- // Initialize timers
44- // The amount of time the pop-up text is on screen, before fading
45- const float maxTime = 3.0f ;
46- float textTimer = 0.0f ;
47- // The length of time text is offset
48- const float animMaxTime = 0.1f ;
49- float pasteAnim = 0.0f ;
50- float copyAnim = 0.0f ;
51- int copyAnimMult = 1 ;
52- float textAnim = 0.0f ;
53- float textAlpha = 0.0f ;
54- // Offset amount for animations
55- const int offsetAmount = -4 ;
56+ // Set UI style
57+ GuiSetStyle (DEFAULT , TEXT_SIZE , 20 );
58+ GuiSetIconScale (2 );
5659
57- SetTargetFPS (60 );
60+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
5861 //--------------------------------------------------------------------------------------
5962
6063 // Main game loop
6164 while (!WindowShouldClose ()) // Detect window close button or ESC key
6265 {
6366 // Update
6467 //----------------------------------------------------------------------------------
65- // Check if the user has pressed the copy/paste key combinations
66- bool pastePressed = (IsKeyDown (KEY_LEFT_CONTROL ) && IsKeyPressed (KEY_V ));
67- bool copyPressed = (IsKeyDown (KEY_LEFT_CONTROL ) && IsKeyPressed (KEY_C ));
68-
69- // Update animation timers
70- if (textTimer > 0 ) textTimer -= GetFrameTime ();
71- if (pasteAnim > 0 ) pasteAnim -= GetFrameTime ();
72- if (copyAnim > 0 ) copyAnim -= GetFrameTime ();
73- if (textAnim > 0 ) textAnim -= GetFrameTime ();
74-
75- if (pastePressed )
68+ // Handle button interactions
69+ if (btnCutPressed )
7670 {
77- // Most operating systems hide this information until the user presses Ctrl-V on the window.
78-
79- // Check to see if the clipboard contains an image
80- // This function does nothing outside of Windows, as it directly calls the Windows API
81- Image image = GetClipboardImage ();
82-
83- if (IsImageValid (image ))
84- {
85- UnloadImage (image );
86- popupText = "clipboard contains image" ;
87- }
88- else
89- {
90- clipboardText = GetClipboardText ();
91-
92- popupText = "text pasted" ;
93- pasteAnim = animMaxTime ;
94- }
95-
96- // Reset animation values
97- textTimer = maxTime ;
98- textAnim = animMaxTime ;
99- textAlpha = 1 ;
71+ SetClipboardText (inputBuffer );
72+ clipboardText = GetClipboardText ();
73+ inputBuffer [0 ] = '\0' ; // Quick solution to clear text
74+ //memset(inputBuffer, 0, 256); // Clear full buffer properly
10075 }
10176
102- // React to the user pressing copy
103- if (copyPressed )
77+ if (btnCopyPressed )
10478 {
105- // Set the text on the user's clipboard
106- SetClipboardText (copyableText [textIndex ]);
107-
108- // Reset values
109- textTimer = maxTime ;
110- textAnim = animMaxTime ;
111- copyAnim = animMaxTime ;
112- copyAnimMult = 1 ;
113- textAlpha = 1 ;
114- popupText = "text copied" ;
79+ SetClipboardText (inputBuffer ); // Copy text to clipboard
80+ clipboardText = GetClipboardText (); // Get text from clipboard
11581 }
11682
117- // Switch to the next item in the list when the user presses up
118- if (IsKeyPressed (KEY_UP ))
83+ if (btnPastePressed )
11984 {
120- // Reset animation
121- copyAnim = animMaxTime ;
122- copyAnimMult = 1 ;
85+ // Paste text from clipboard
86+ clipboardText = GetClipboardText ();
87+ if (clipboardText != NULL ) TextCopy (inputBuffer , clipboardText );
88+ }
12389
124- textIndex += 1 ;
90+ if (btnClearPressed )
91+ {
92+ inputBuffer [0 ] = '\0' ; // Quick solution to clear text
93+ //memset(inputBuffer, 0, 256); // Clear full buffer properly
94+ }
12595
126- if (textIndex >= sizeof (copyableText ) / sizeof (const char * )) // Length of array
127- {
128- // Loop back to the other end
129- textIndex = 0 ;
130- }
96+ if (btnRandomPressed )
97+ {
98+ // Get random text from sample list
99+ TextCopy (inputBuffer , sampleTexts [GetRandomValue (0 , MAX_TEXT_SAMPLES - 1 )]);
131100 }
132101
133- // Switch to the previous item in the list when the user presses down
134- if (IsKeyPressed ( KEY_DOWN ))
102+ // Quick cut/copy/paste with keyboard shortcuts
103+ if (IsKeyDown ( KEY_LEFT_CONTROL ) || IsKeyDown ( KEY_RIGHT_CONTROL ))
135104 {
136- // Reset animation
137- copyAnim = animMaxTime ;
138- copyAnimMult = -1 ;
105+ if (IsKeyPressed (KEY_X ))
106+ {
107+ SetClipboardText (inputBuffer );
108+ inputBuffer [0 ] = '\0' ; // Quick solution to clear text
109+ }
139110
140- if (textIndex == 0 ) textIndex = (sizeof (copyableText )/sizeof (const char * )) - 1 ;
141- else textIndex -= 1 ;
111+ if (IsKeyPressed (KEY_C )) SetClipboardText (inputBuffer );
112+
113+ if (IsKeyPressed (KEY_V ))
114+ {
115+ clipboardText = GetClipboardText ();
116+ if (clipboardText != NULL ) TextCopy (inputBuffer , clipboardText );
117+ }
142118 }
143119 //----------------------------------------------------------------------------------
144120
145121 // Draw
146122 //----------------------------------------------------------------------------------
147123 BeginDrawing ();
148124
149- ClearBackground (RAYWHITE );
150-
151- // Draw the user's pasted text, if there is any yet
152- if (clipboardText )
153- {
154- // Offset animation
155- int offset = 0 ;
156- if (pasteAnim > 0 ) offset = offsetAmount ;
157-
158- // Draw the pasted text
159- DrawText ("pasted clipboard:" , 10 , 10 + offset , 20 , DARKGREEN );
160- DrawText (clipboardText , 10 , 30 + offset , 20 , DARKGRAY );
161- }
162-
163- // Offset animation
164- int textOffset = 0 ;
165- if (copyAnim > 0 ) textOffset = offsetAmount ;
166-
167- // Draw copyable text and controls
168- DrawText (copyableText [textIndex ], 10 , 330 + (textOffset * copyAnimMult ), 20 , MAROON );
169- DrawText ("up/down to change string, ctrl-c to copy, ctrl-v to paste" , 10 , 355 , 20 , DARKGRAY );
170-
171- // Alpha / Offset animation
172- if (textAlpha > 0 )
173- {
174- // Offset animation
175- int offset = 0 ;
176- if (textAnim > 0 ) offset = offsetAmount ;
177- // Draw pop up text
178- DrawText (popupText , 10 , 425 + offset , 20 , ColorAlpha (DARKGREEN , textAlpha ));
179-
180- // Fade-out animation
181- if (textTimer < 0 ) textAlpha -= GetFrameTime ();
182- }
125+ ClearBackground (RAYWHITE );
126+
127+ // Draw instructions
128+ GuiLabel ((Rectangle ){ 50 , 20 , 700 , 36 }, "Use the BUTTONS or KEY SHORTCUTS:" );
129+ DrawText ("[CTRL+X] - CUT | [CTRL+C] COPY | [CTRL+V] | PASTE" , 50 , 60 , 20 , MAROON );
130+
131+ // Draw text box
132+ if (GuiTextBox ((Rectangle ){ 50 , 120 , 652 , 40 }, inputBuffer , 256 , textBoxEditMode )) textBoxEditMode = !textBoxEditMode ;
133+
134+ // Random text button
135+ btnRandomPressed = GuiButton ((Rectangle ){ 50 + 652 + 8 , 120 , 40 , 40 }, "#77#" );
136+
137+ // Draw buttons
138+ btnCutPressed = GuiButton ((Rectangle ){ 50 , 180 , 158 , 40 }, "#17#CUT" );
139+ btnCopyPressed = GuiButton ((Rectangle ){ 50 + 165 , 180 , 158 , 40 }, "#16#COPY" );
140+ btnPastePressed = GuiButton ((Rectangle ){ 50 + 165 * 2 , 180 , 158 , 40 }, "#18#PASTE" );
141+ btnClearPressed = GuiButton ((Rectangle ){ 50 + 165 * 3 , 180 , 158 , 40 }, "#143#CLEAR" );
142+
143+ // Draw clipboard status
144+ GuiSetState (STATE_DISABLED );
145+ GuiLabel ((Rectangle ){ 50 , 260 , 700 , 40 }, "Clipboard current text data:" );
146+ GuiSetStyle (TEXTBOX , TEXT_READONLY , 1 );
147+ GuiTextBox ((Rectangle ){ 50 , 300 , 700 , 40 }, clipboardText , 256 , false);
148+ GuiSetStyle (TEXTBOX , TEXT_READONLY , 0 );
149+ GuiLabel ((Rectangle ){ 50 , 360 , 700 , 40 }, "Try copying text from other applications and pasting here!" );
150+ GuiSetState (STATE_NORMAL );
183151
184152 EndDrawing ();
185153 //----------------------------------------------------------------------------------
@@ -191,4 +159,4 @@ int main(void)
191159 //--------------------------------------------------------------------------------------
192160
193161 return 0 ;
194- }
162+ }
0 commit comments