mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-06 06:13:10 +00:00
REDESIGNED: core_clipboard_text, based on #5248
This commit is contained in:
parent
0b4815b8fe
commit
f76e371436
@ -69,7 +69,7 @@ Examples using raylib[core](../src/rcore.c) platform functionality like window c
|
|||||||
| [core_directory_files](core/core_directory_files.c) | <img src="core/core_directory_files.png" alt="core_directory_files" width="80"> | ⭐☆☆☆ | 5.5 | 5.6 | [Hugo ARNAL](https://github.com/hugoarnal) |
|
| [core_directory_files](core/core_directory_files.c) | <img src="core/core_directory_files.png" alt="core_directory_files" width="80"> | ⭐☆☆☆ | 5.5 | 5.6 | [Hugo ARNAL](https://github.com/hugoarnal) |
|
||||||
| [core_highdpi_testbed](core/core_highdpi_testbed.c) | <img src="core/core_highdpi_testbed.png" alt="core_highdpi_testbed" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
| [core_highdpi_testbed](core/core_highdpi_testbed.c) | <img src="core/core_highdpi_testbed.png" alt="core_highdpi_testbed" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
||||||
| [core_screen_recording](core/core_screen_recording.c) | <img src="core/core_screen_recording.png" alt="core_screen_recording" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
| [core_screen_recording](core/core_screen_recording.c) | <img src="core/core_screen_recording.png" alt="core_screen_recording" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
||||||
| [core_clipboard_text](core/core_clipboard_text.c) | <img src="core/core_clipboard_text.png" alt="core_clipboard_text" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6-dev | [Robin](https://github.com/RobinsAviary) |
|
| [core_clipboard_text](core/core_clipboard_text.c) | <img src="core/core_clipboard_text.png" alt="core_clipboard_text" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6-dev | [Ananth S](https://github.com/Ananth1839) |
|
||||||
| [core_text_file_loading](core/core_text_file_loading.c) | <img src="core/core_text_file_loading.png" alt="core_text_file_loading" width="80"> | ⭐☆☆☆ | 5.5 | 5.6 | [Aanjishnu Bhattacharyya](https://github.com/NimComPoo-04) |
|
| [core_text_file_loading](core/core_text_file_loading.c) | <img src="core/core_text_file_loading.png" alt="core_text_file_loading" width="80"> | ⭐☆☆☆ | 5.5 | 5.6 | [Aanjishnu Bhattacharyya](https://github.com/NimComPoo-04) |
|
||||||
| [core_compute_hash](core/core_compute_hash.c) | <img src="core/core_compute_hash.png" alt="core_compute_hash" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
| [core_compute_hash](core/core_compute_hash.c) | <img src="core/core_compute_hash.png" alt="core_compute_hash" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
||||||
|
|
||||||
|
|||||||
@ -2,22 +2,23 @@
|
|||||||
*
|
*
|
||||||
* raylib [core] example - clipboard text
|
* raylib [core] example - clipboard text
|
||||||
*
|
*
|
||||||
* Example complexity rating: [★☆☆☆] 1/4
|
|
||||||
*
|
|
||||||
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
|
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
|
||||||
*
|
*
|
||||||
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
|
* Example contributed by Ananth S (@Ananth1839) and reviewed by Ramon Santamaria (@raysan5)
|
||||||
*
|
*
|
||||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||||
* BSD-like license that allows static linking with closed source software
|
* BSD-like license that allows static linking with closed source software
|
||||||
*
|
*
|
||||||
* Copyright (c) 2025 Robin (@RobinsAviary)
|
* Copyright (c) 2025 Ananth S (@Ananth1839)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#define RAYGUI_IMPLEMENTATION
|
||||||
|
#include "raygui.h"
|
||||||
|
|
||||||
|
#define MAX_TEXT_SAMPLES 5
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Program main entry point
|
// Program main entry point
|
||||||
@ -31,30 +32,32 @@ int main(void)
|
|||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text");
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text");
|
||||||
|
|
||||||
const char *clipboardText = NULL;
|
// Define some sample texts
|
||||||
|
const char *sampleTexts[MAX_TEXT_SAMPLES] = {
|
||||||
|
"Hello from raylib!",
|
||||||
|
"The quick brown fox jumps over the lazy dog",
|
||||||
|
"Clipboard operations are useful!",
|
||||||
|
"raylib is a simple and easy-to-use library",
|
||||||
|
"Copy and paste me!"
|
||||||
|
};
|
||||||
|
|
||||||
// List of text the user can switch through and copy
|
char *clipboardText = NULL;
|
||||||
const char *copyableText[] = { "raylib is fun", "hello, clipboard!", "potato chips" };
|
char inputBuffer[256] = "Hello from raylib!"; // Random initial string
|
||||||
|
|
||||||
unsigned int textIndex = 0;
|
// UI required variables
|
||||||
|
bool textBoxEditMode = false;
|
||||||
|
|
||||||
const char *popupText = NULL;
|
bool btnCutPressed = false;
|
||||||
|
bool btnCopyPressed = false;
|
||||||
|
bool btnPastePressed = false;
|
||||||
|
bool btnClearPressed = false;
|
||||||
|
bool btnRandomPressed = false;
|
||||||
|
|
||||||
// Initialize timers
|
// Set UI style
|
||||||
// The amount of time the pop-up text is on screen, before fading
|
GuiSetStyle(DEFAULT, TEXT_SIZE, 20);
|
||||||
const float maxTime = 3.0f;
|
GuiSetIconScale(2);
|
||||||
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;
|
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Main game loop
|
// Main game loop
|
||||||
@ -62,83 +65,56 @@ int main(void)
|
|||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Check if the user has pressed the copy/paste key combinations
|
// Handle button interactions
|
||||||
bool pastePressed = (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V));
|
if (btnCutPressed)
|
||||||
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();
|
|
||||||
|
|
||||||
if (pastePressed)
|
|
||||||
{
|
{
|
||||||
// Most operating systems hide this information until the user presses Ctrl-V on the window.
|
SetClipboardText(inputBuffer);
|
||||||
|
clipboardText = GetClipboardText();
|
||||||
|
inputBuffer[0] = '\0'; // Quick solution to clear text
|
||||||
|
//memset(inputBuffer, 0, 256); // Clear full buffer properly
|
||||||
|
}
|
||||||
|
|
||||||
// Check to see if the clipboard contains an image
|
if (btnCopyPressed)
|
||||||
// This function does nothing outside of Windows, as it directly calls the Windows API
|
{
|
||||||
Image image = GetClipboardImage();
|
SetClipboardText(inputBuffer); // Copy text to clipboard
|
||||||
|
clipboardText = GetClipboardText(); // Get text from clipboard
|
||||||
if (IsImageValid(image))
|
}
|
||||||
|
|
||||||
|
if (btnPastePressed)
|
||||||
|
{
|
||||||
|
// Paste text from clipboard
|
||||||
|
clipboardText = GetClipboardText();
|
||||||
|
if (clipboardText != NULL) TextCopy(inputBuffer, clipboardText);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (btnClearPressed)
|
||||||
|
{
|
||||||
|
inputBuffer[0] = '\0'; // Quick solution to clear text
|
||||||
|
//memset(inputBuffer, 0, 256); // Clear full buffer properly
|
||||||
|
}
|
||||||
|
|
||||||
|
if (btnRandomPressed)
|
||||||
|
{
|
||||||
|
// Get random text from sample list
|
||||||
|
TextCopy(inputBuffer, sampleTexts[GetRandomValue(0, MAX_TEXT_SAMPLES - 1)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quick cut/copy/paste with keyboard shortcuts
|
||||||
|
if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL))
|
||||||
|
{
|
||||||
|
if (IsKeyPressed(KEY_X))
|
||||||
{
|
{
|
||||||
UnloadImage(image);
|
SetClipboardText(inputBuffer);
|
||||||
popupText = "clipboard contains image";
|
inputBuffer[0] = '\0'; // Quick solution to clear text
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (IsKeyPressed(KEY_C)) SetClipboardText(inputBuffer);
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_V))
|
||||||
{
|
{
|
||||||
clipboardText = GetClipboardText();
|
clipboardText = GetClipboardText();
|
||||||
|
if (clipboardText != NULL) TextCopy(inputBuffer, clipboardText);
|
||||||
popupText = "text pasted";
|
|
||||||
pasteAnim = animMaxTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset animation values
|
|
||||||
textTimer = maxTime;
|
|
||||||
textAnim = animMaxTime;
|
|
||||||
textAlpha = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
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) textIndex = (sizeof(copyableText)/sizeof(const char*)) - 1;
|
|
||||||
else textIndex -= 1;
|
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -146,40 +122,32 @@ int main(void)
|
|||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
// Draw the user's pasted text, if there is any yet
|
// Draw instructions
|
||||||
if (clipboardText)
|
GuiLabel((Rectangle){ 50, 20, 700, 36 }, "Use the BUTTONS or KEY SHORTCUTS:");
|
||||||
{
|
DrawText("[CTRL+X] - CUT | [CTRL+C] COPY | [CTRL+V] | PASTE", 50, 60, 20, MAROON);
|
||||||
// Offset animation
|
|
||||||
int offset = 0;
|
|
||||||
if (pasteAnim > 0) offset = offsetAmount;
|
|
||||||
|
|
||||||
// Draw the pasted text
|
// Draw text box
|
||||||
DrawText("pasted clipboard:", 10, 10 + offset, 20, DARKGREEN);
|
if (GuiTextBox((Rectangle){ 50, 120, 652, 40 }, inputBuffer, 256, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
|
||||||
DrawText(clipboardText, 10, 30 + offset, 20, DARKGRAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Offset animation
|
// Random text button
|
||||||
int textOffset = 0;
|
btnRandomPressed = GuiButton((Rectangle){ 50 + 652 + 8, 120, 40, 40 }, "#77#");
|
||||||
if (copyAnim > 0) textOffset = offsetAmount;
|
|
||||||
|
|
||||||
// Draw copyable text and controls
|
// Draw buttons
|
||||||
DrawText(copyableText[textIndex], 10, 330 + (textOffset * copyAnimMult), 20, MAROON);
|
btnCutPressed = GuiButton((Rectangle){ 50, 180, 158, 40 }, "#17#CUT");
|
||||||
DrawText("up/down to change string, ctrl-c to copy, ctrl-v to paste", 10, 355, 20, DARKGRAY);
|
btnCopyPressed = GuiButton((Rectangle){ 50 + 165, 180, 158, 40 }, "#16#COPY");
|
||||||
|
btnPastePressed = GuiButton((Rectangle){ 50 + 165*2, 180, 158, 40 }, "#18#PASTE");
|
||||||
|
btnClearPressed = GuiButton((Rectangle){ 50 + 165*3, 180, 158, 40 }, "#143#CLEAR");
|
||||||
|
|
||||||
// Alpha / Offset animation
|
// Draw clipboard status
|
||||||
if (textAlpha > 0)
|
GuiSetState(STATE_DISABLED);
|
||||||
{
|
GuiLabel((Rectangle){ 50, 260, 700, 40 }, "Clipboard current text data:");
|
||||||
// Offset animation
|
GuiSetStyle(TEXTBOX, TEXT_READONLY, 1);
|
||||||
int offset = 0;
|
GuiTextBox((Rectangle){ 50, 300, 700, 40 }, clipboardText, 256, false);
|
||||||
if (textAnim > 0) offset = offsetAmount;
|
GuiSetStyle(TEXTBOX, TEXT_READONLY, 0);
|
||||||
// Draw pop up text
|
GuiLabel((Rectangle){ 50, 360, 700, 40 }, "Try copying text from other applications and pasting here!");
|
||||||
DrawText(popupText, 10, 425 + offset, 20, ColorAlpha(DARKGREEN, textAlpha));
|
GuiSetState(STATE_NORMAL);
|
||||||
|
|
||||||
// Fade-out animation
|
|
||||||
if (textTimer < 0) textAlpha -= GetFrameTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
@ -191,4 +159,4 @@ int main(void)
|
|||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 17 KiB |
@ -51,7 +51,7 @@ core;core_input_actions;★★☆☆;5.5;5.6;2025;2025;"Jett";@JettMonstersGoBoo
|
|||||||
core;core_directory_files;★☆☆☆;5.5;5.6;2025;2025;"Hugo ARNAL";@hugoarnal
|
core;core_directory_files;★☆☆☆;5.5;5.6;2025;2025;"Hugo ARNAL";@hugoarnal
|
||||||
core;core_highdpi_testbed;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
core;core_highdpi_testbed;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
||||||
core;core_screen_recording;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
core;core_screen_recording;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
||||||
core;core_clipboard_text;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Robin";@RobinsAviary
|
core;core_clipboard_text;★☆☆☆;5.6-dev;5.6-dev;2025;2025;"Ananth S";@Ananth1839
|
||||||
core;core_text_file_loading;★☆☆☆;5.5;5.6;0;0;"Aanjishnu Bhattacharyya";@NimComPoo-04
|
core;core_text_file_loading;★☆☆☆;5.5;5.6;0;0;"Aanjishnu Bhattacharyya";@NimComPoo-04
|
||||||
core;core_compute_hash;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
core;core_compute_hash;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Ramon Santamaria";@raysan5
|
||||||
shapes;shapes_basic_shapes;★☆☆☆;1.0;4.2;2014;2025;"Ramon Santamaria";@raysan5
|
shapes;shapes_basic_shapes;★☆☆☆;1.0;4.2;2014;2025;"Ramon Santamaria";@raysan5
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user