Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions examples/core/core_clipboard_text.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*******************************************************************************************
*
* raylib [core] example - Clipboard Text Operations
*
* Example originally created with raylib 5.0, last time updated with raylib 5.0
*
* Example contributed by [Your Name] and reviewed by the raylib community
*
* 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) 2024 [ANANTH S] (@Ananth1836)
*
********************************************************************************************/

#include "raylib.h"
#include <cstddef>

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text");

// Define some sample texts
const char *sampleTexts[] = {
"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!"
};
const int sampleTextsCount = sizeof(sampleTexts) / sizeof(sampleTexts[0]);

int currentTextIndex = 0;
char inputBuffer[256] = "Type here to copy to clipboard...";
bool textEdited = false;

// UI state
Rectangle copyButton = { 50, 350, 150, 40 };
Rectangle pasteButton = { 220, 350, 150, 40 };
Rectangle clearButton = { 390, 350, 150, 40 };
Rectangle cycleButton = { 560, 350, 150, 40 };

Rectangle textBox = { 50, 250, 700, 40 };

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
//----------------------------------------------------------------------------------
Vector2 mousePoint = GetMousePosition();

// Handle text input
if (CheckCollisionPointRec(mousePoint, textBox) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
if (TextIsEqual(inputBuffer, "Type here to copy to clipboard..."))
{
inputBuffer[0] = '\0'; // Clear the default text
}
}

// Get key presses and update input buffer
if (textEdited) textEdited = false;

int key = GetCharPressed();
while (key > 0)
{
if ((key >= 32) && (key <= 125))
{
int length = TextLength(inputBuffer);
if (length < 255)
{
inputBuffer[length] = (char)key;
inputBuffer[length + 1] = '\0';
textEdited = true;
}
}
key = GetCharPressed();
}

// Handle backspace
if (IsKeyPressed(KEY_BACKSPACE))
{
int length = TextLength(inputBuffer);
if (length > 0)
{
inputBuffer[length - 1] = '\0';
textEdited = true;
}
}

// Handle button interactions
if (CheckCollisionPointRec(mousePoint, copyButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
// Copy text to clipboard
SetClipboardText(inputBuffer);
}
}

if (CheckCollisionPointRec(mousePoint, pasteButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
// Paste text from clipboard
const char *clipboardText = GetClipboardText();
if (clipboardText != NULL)
{
TextCopy(inputBuffer, clipboardText);
}
}
}

if (CheckCollisionPointRec(mousePoint, clearButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
// Clear input buffer
inputBuffer[0] = '\0';
}
}

if (CheckCollisionPointRec(mousePoint, cycleButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
// Cycle through sample texts
currentTextIndex = (currentTextIndex + 1) % sampleTextsCount;
TextCopy(inputBuffer, sampleTexts[currentTextIndex]);
}
}

// Quick copy/paste with keyboard shortcuts
if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL))
{
if (IsKeyPressed(KEY_C))
{
SetClipboardText(inputBuffer);
}
if (IsKeyPressed(KEY_V))
{
const char *clipboardText = GetClipboardText();
if (clipboardText != NULL)
{
TextCopy(inputBuffer, clipboardText);
}
}
}
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

DrawText("Clipboard Text Operations", 20, 20, 32, DARKBLUE);

// Draw instructions
DrawText("Use the buttons below or keyboard shortcuts:", 20, 70, 20, DARKGRAY);
DrawText("CTRL+C to copy, CTRL+V to paste", 20, 100, 20, DARKGRAY);

// Draw text box
DrawRectangleRec(textBox, LIGHTGRAY);
DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, DARKGRAY);
DrawText(inputBuffer, (int)textBox.x + 5, (int)textBox.y + 10, 20, MAROON);

// Draw cursor in text box
if (((int)(GetTime() * 2) % 2) == 0 && textEdited)
{
DrawText("_", (int)textBox.x + 5 + MeasureText(inputBuffer, 20), (int)textBox.y + 10, 20, MAROON);
}

// Draw buttons
DrawRectangleRec(copyButton, CheckCollisionPointRec(mousePoint, copyButton) ? SKYBLUE : BLUE);
DrawRectangleLines((int)copyButton.x, (int)copyButton.y, (int)copyButton.width, (int)copyButton.height, DARKBLUE);
DrawText("Copy", (int)copyButton.x + 45, (int)copyButton.y + 10, 20, WHITE);

DrawRectangleRec(pasteButton, CheckCollisionPointRec(mousePoint, pasteButton) ? SKYBLUE : BLUE);
DrawRectangleLines((int)pasteButton.x, (int)pasteButton.y, (int)pasteButton.width, (int)pasteButton.height, DARKBLUE);
DrawText("Paste", (int)pasteButton.x + 40, (int)pasteButton.y + 10, 20, WHITE);

DrawRectangleRec(clearButton, CheckCollisionPointRec(mousePoint, clearButton) ? SKYBLUE : BLUE);
DrawRectangleLines((int)clearButton.x, (int)clearButton.y, (int)clearButton.width, (int)clearButton.height, DARKBLUE);
DrawText("Clear", (int)clearButton.x + 40, (int)clearButton.y + 10, 20, WHITE);

DrawRectangleRec(cycleButton, CheckCollisionPointRec(mousePoint, cycleButton) ? SKYBLUE : BLUE);
DrawRectangleLines((int)cycleButton.x, (int)cycleButton.y, (int)cycleButton.width, (int)cycleButton.height, DARKBLUE);
DrawText("Sample Text", (int)cycleButton.x + 15, (int)cycleButton.y + 10, 20, WHITE);

// Draw clipboard status
DrawText("Try copying text from other applications and pasting here!", 50, 420, 18, DARKGREEN);

// Draw current sample text info
DrawText(TextFormat("Sample Text %d/%d", currentTextIndex + 1, sampleTextsCount),
600, 300, 18, DARKGRAY);

EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}
Binary file added examples/core/core_clipboard_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/shapes/shapemultiline_test_diagram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Raylib Example",
"type": "shell",
"command": "gcc",
"args": [
"-o", "core_clipboard_text.exe",
"core_clipboard_text.c",
"-IC:/raylib/raylib-5.5_win64_mingw-w64/include",
"C:/raylib/raylib-5.5_win64_mingw-w64/lib/libraylib.a",
"-lgdi32",
"-lwinmm",
"-lopengl32"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"problemMatcher": ["$gcc"]
}
]
}
116 changes: 116 additions & 0 deletions examples/shapes/shapes_Multiline_test_diagram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "raylib.h"
#include <stdlib.h>

int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text operations");

char textBuffer[256] = "Try copying this text!";
char clipboardText[256] = "Nothing copied yet...";

Rectangle textBox = { 100, 180, 600, 50 };
Rectangle copyButton = { 100, 250, 200, 40 };
Rectangle pasteButton = { 500, 250, 200, 40 };

bool mouseOnText = false;
int framesCounter = 0;

SetTargetFPS(60);

while (!WindowShouldClose())
{
mouseOnText = CheckCollisionPointRec(GetMousePosition(), textBox);

if (mouseOnText)
{
SetMouseCursor(MOUSE_CURSOR_IBEAM);

int key = GetCharPressed();
while (key > 0)
{
if ((key >= 32) && (key <= 125))
{
int length = TextLength(textBuffer);
if (length < 255)
{
textBuffer[length] = (char)key;
textBuffer[length + 1] = '\0';
}
}
key = GetCharPressed();
}

if (IsKeyPressed(KEY_BACKSPACE))
{
int length = TextLength(textBuffer);
if (length > 0) textBuffer[length - 1] = '\0';
}
}
else
{
SetMouseCursor(MOUSE_CURSOR_DEFAULT);
}

if (CheckCollisionPointRec(GetMousePosition(), copyButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
SetClipboardText(textBuffer);
TextCopy(clipboardText, "Text copied to clipboard!");
}
}

if (CheckCollisionPointRec(GetMousePosition(), pasteButton))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
const char *clipboard = GetClipboardText();
if (clipboard != NULL && TextLength(clipboard) > 0)
{
TextCopy(clipboardText, TextFormat("Pasted: %s", clipboard));
}
else
{
TextCopy(clipboardText, "Clipboard is empty!");
}
}
}

if (mouseOnText) framesCounter++;
else framesCounter = 0;

BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("CLIPBOARD TEXT OPERATIONS", 240, 40, 20, DARKGRAY);
DrawText("Try copying text from this input box:", 100, 140, 20, DARKGRAY);

DrawRectangleRec(textBox, LIGHTGRAY);
if (mouseOnText) DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, RED);
else DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, DARKGRAY);

DrawText(textBuffer, (int)textBox.x + 5, (int)textBox.y + 8, 20, MAROON);

if (mouseOnText && (((framesCounter/20)%2) == 0))
{
DrawText("_", (int)textBox.x + 8 + MeasureText(textBuffer, 20), (int)textBox.y + 12, 20, MAROON);
}

DrawRectangleRec(copyButton, CheckCollisionPointRec(GetMousePosition(), copyButton) ? SKYBLUE : BLUE);
DrawRectangleRec(pasteButton, CheckCollisionPointRec(GetMousePosition(), pasteButton) ? SKYBLUE : BLUE);

DrawText("COPY TO CLIPBOARD", (int)copyButton.x + 10, (int)copyButton.y + 10, 20, WHITE);
DrawText("PASTE FROM CLIPBOARD", (int)pasteButton.x + 10, (int)pasteButton.y + 10, 20, WHITE);

DrawText("Clipboard Status:", 100, 320, 20, DARKGRAY);
DrawText(clipboardText, 100, 350, 20, DARKBLUE);

DrawText("Try copying text from other applications and paste here!", 100, 400, 15, GRAY);
EndDrawing();
}

CloseWindow();
return 0;
}