WASM examples (+ backports of new official examples) (#344)
* chg: New build system that uses the officially distributed binaries, bumped version to 8.0.0, simplified git workflow, removed deprecated OpenGL 1.1 functionality. * chg: Modernize CI workflow, enable SourceLink - Bump workflow actions to latest majors (Node 24); drop deprecated softprops/action-gh-release@v1 - Trigger push builds on main instead of master - Create local nuget feed dir before pack (fixes NU1301) - Enable Microsoft.SourceLink.GitHub for debugging symbols (ref PR #340) * fix: centralized version data in Directory.build.props, and fixed various interop details that had incorrect function signatures * chore: updated readme * fix: version the native extract marker and chain download via DependsOnTargets The .extracted marker now includes the raylib package name, so bumping TargetRaylibTag re-extracts the new archive instead of silently keeping (and packing/copying) the previous version's files. _PrepareNativeLibrary and _StageWasmNative now depend directly on _DownloadAndExtractInternal instead of CallTarget-ing it; dependency targets run in the same project instance, so the resolved properties (RaylibPackageName etc.) propagate naturally. * fix: let the binding build for browser-wasm on both net8.0 and net10.0 The net8-era wasm workload (Microsoft.NET.Runtime.WebAssembly.Sdk 8.0.x, auto-imported for RID browser-wasm) treats every browser-wasm project as a wasm app: it forces OutputType=Exe after project evaluation (CS5001 for a classlib) and hooks its app-bundle build after Build, which errors because a library has no assemblies to bundle. Opt Raylib-cs out via DisableAutoWasmBuildApp (props time, before the workload defaults its trigger) and pin OutputType back to Library in Directory.Build.targets (evaluated after the workload props, so the assignment wins). net10's wasm SDK needs neither workaround. * chore: readme updated * chg: simplifying build logic - a simple line in the documentation should save us the code here * fix: Wrong signature of FrameBufferComplete * chore: readme update * feat: samples default to local project reference, and can optionally use the nuget package * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * fix: Examples.csproj runs the download task when building locally * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * chore: clean up linter warnings * feat: html harness focuses the example and allows quick navigation with J/K instead. * chore: readme mentions the property to use nuget vs. the local project reference * feat: replaced the J/K navigation with good old HTML buttons * chore: run dotnet format scoped default (was previously scoped to just 'style')
This commit is contained in:
parent
b207e633ae
commit
8c22e68c2a
236 changed files with 40405 additions and 10896 deletions
311
Examples/Core/ClipboardText.cs
Normal file
311
Examples/Core/ClipboardText.cs
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - clipboard text
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
||||
*
|
||||
* 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,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2025 Ananth S (@Ananth1839)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
// NOTE: The upstream C example uses raygui (GuiTextBox/GuiButton/GuiLabel) for its UI.
|
||||
// raygui is not bound in raylib-cs, so the widgets below are minimal re-implementations
|
||||
// using plain raylib drawing/input. Clipboard behaviour (cut/copy/paste and CTRL+X/C/V
|
||||
// shortcuts) matches the original.
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace Examples.Core;
|
||||
|
||||
public partial class ClipboardText : IExample
|
||||
{
|
||||
private const int MaxTextSamples = 5;
|
||||
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public string Name => "Core / Clipboard Text";
|
||||
|
||||
public string Title => "raylib [core] example - clipboard text";
|
||||
|
||||
private string[] sampleTexts;
|
||||
private string clipboardText;
|
||||
private StringBuilder inputBuffer;
|
||||
|
||||
// UI required variables
|
||||
private bool textBoxEditMode;
|
||||
|
||||
private bool btnCutPressed;
|
||||
private bool btnCopyPressed;
|
||||
private bool btnPastePressed;
|
||||
private bool btnClearPressed;
|
||||
private bool btnRandomPressed;
|
||||
|
||||
private int framesCounter;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// Define some sample texts
|
||||
sampleTexts = new string[]
|
||||
{
|
||||
"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!"
|
||||
};
|
||||
|
||||
clipboardText = null;
|
||||
inputBuffer = new StringBuilder("Hello from raylib!"); // Random initial string
|
||||
|
||||
// UI required variables
|
||||
textBoxEditMode = false;
|
||||
|
||||
btnCutPressed = false;
|
||||
btnCopyPressed = false;
|
||||
btnPastePressed = false;
|
||||
btnClearPressed = false;
|
||||
btnRandomPressed = false;
|
||||
|
||||
framesCounter = 0;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
framesCounter++;
|
||||
|
||||
// Handle button interactions
|
||||
if (btnCutPressed)
|
||||
{
|
||||
SetClipboardText(inputBuffer.ToString());
|
||||
clipboardText = GetClipboardText_();
|
||||
inputBuffer.Clear(); // Quick solution to clear text
|
||||
}
|
||||
|
||||
if (btnCopyPressed)
|
||||
{
|
||||
SetClipboardText(inputBuffer.ToString()); // Copy text to clipboard
|
||||
clipboardText = GetClipboardText_(); // Get text from clipboard
|
||||
}
|
||||
|
||||
if (btnPastePressed)
|
||||
{
|
||||
// Paste text from clipboard
|
||||
clipboardText = GetClipboardText_();
|
||||
if (clipboardText != null)
|
||||
{
|
||||
SetInputBuffer(clipboardText);
|
||||
}
|
||||
}
|
||||
|
||||
if (btnClearPressed)
|
||||
{
|
||||
inputBuffer.Clear(); // Quick solution to clear text
|
||||
}
|
||||
|
||||
if (btnRandomPressed)
|
||||
{
|
||||
// Get random text from sample list
|
||||
SetInputBuffer(sampleTexts[GetRandomValue(0, MaxTextSamples - 1)]);
|
||||
}
|
||||
|
||||
// Quick cut/copy/paste with keyboard shortcuts
|
||||
if (IsKeyDown(KeyboardKey.LeftControl) || IsKeyDown(KeyboardKey.RightControl))
|
||||
{
|
||||
if (IsKeyPressed(KeyboardKey.X))
|
||||
{
|
||||
SetClipboardText(inputBuffer.ToString());
|
||||
inputBuffer.Clear(); // Quick solution to clear text
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.C))
|
||||
{
|
||||
SetClipboardText(inputBuffer.ToString());
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.V))
|
||||
{
|
||||
clipboardText = GetClipboardText_();
|
||||
if (clipboardText != null)
|
||||
{
|
||||
SetInputBuffer(clipboardText);
|
||||
}
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
// Draw instructions
|
||||
GuiLabel(new Rectangle(50, 20, 700, 36), "Use the BUTTONS or KEY SHORTCUTS:");
|
||||
DrawText("[CTRL+X] - CUT | [CTRL+C] COPY | [CTRL+V] | PASTE", 50, 60, 20, Color.Maroon);
|
||||
|
||||
// Draw text box
|
||||
if (GuiTextBox(new Rectangle(50, 120, 652, 40), inputBuffer, 256, textBoxEditMode))
|
||||
{
|
||||
textBoxEditMode = !textBoxEditMode;
|
||||
}
|
||||
|
||||
// Random text button
|
||||
btnRandomPressed = GuiButton(new Rectangle(50 + 652 + 8, 120, 40, 40), "RND");
|
||||
|
||||
// Draw buttons
|
||||
btnCutPressed = GuiButton(new Rectangle(50, 180, 158, 40), "CUT");
|
||||
btnCopyPressed = GuiButton(new Rectangle(50 + 165, 180, 158, 40), "COPY");
|
||||
btnPastePressed = GuiButton(new Rectangle(50 + 165 * 2, 180, 158, 40), "PASTE");
|
||||
btnClearPressed = GuiButton(new Rectangle(50 + 165 * 3, 180, 158, 40), "CLEAR");
|
||||
|
||||
// Draw clipboard status
|
||||
GuiLabel(new Rectangle(50, 260, 700, 40), "Clipboard current text data:");
|
||||
GuiTextBoxReadOnly(new Rectangle(50, 300, 700, 40), clipboardText);
|
||||
GuiLabel(new Rectangle(50, 360, 700, 40), "Try copying text from other applications and pasting here!");
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
}
|
||||
|
||||
// Replace input buffer contents (equivalent to raylib TextCopy into a fixed buffer)
|
||||
private void SetInputBuffer(string text)
|
||||
{
|
||||
inputBuffer.Clear();
|
||||
if (text != null)
|
||||
{
|
||||
if (text.Length > 255)
|
||||
{
|
||||
text = text.Substring(0, 255);
|
||||
}
|
||||
|
||||
inputBuffer.Append(text);
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Minimal raygui-like widgets (plain raylib re-implementation)
|
||||
//----------------------------------------------------------------------------------
|
||||
private static void GuiLabel(Rectangle bounds, string text)
|
||||
{
|
||||
DrawText(text, (int)bounds.X, (int)(bounds.Y + (bounds.Height - 20) / 2), 20, Color.DarkGray);
|
||||
}
|
||||
|
||||
private static bool GuiButton(Rectangle bounds, string text)
|
||||
{
|
||||
bool pressed = false;
|
||||
Vector2 mouse = GetMousePosition();
|
||||
bool hover = CheckCollisionPointRec(mouse, bounds);
|
||||
|
||||
Color fill = hover ? (IsMouseButtonDown(MouseButton.Left) ? Color.SkyBlue : Color.LightGray) : Color.RayWhite;
|
||||
DrawRectangleRec(bounds, fill);
|
||||
DrawRectangleLinesEx(bounds, 1, hover ? Color.Blue : Color.Gray);
|
||||
|
||||
int textWidth = MeasureText(text, 20);
|
||||
DrawText(text, (int)(bounds.X + (bounds.Width - textWidth) / 2), (int)(bounds.Y + (bounds.Height - 20) / 2), 20, Color.DarkGray);
|
||||
|
||||
if (hover && IsMouseButtonReleased(MouseButton.Left))
|
||||
{
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
return pressed;
|
||||
}
|
||||
|
||||
private bool GuiTextBox(Rectangle bounds, StringBuilder text, int maxChars, bool editMode)
|
||||
{
|
||||
bool pressed = false;
|
||||
Vector2 mouse = GetMousePosition();
|
||||
bool hover = CheckCollisionPointRec(mouse, bounds);
|
||||
|
||||
if (editMode)
|
||||
{
|
||||
// Get char pressed (unicode character) on the queue
|
||||
int key = GetCharPressed();
|
||||
while (key > 0)
|
||||
{
|
||||
if ((key >= 32) && (key <= 125) && (text.Length < maxChars - 1))
|
||||
{
|
||||
text.Append((char)key);
|
||||
}
|
||||
|
||||
key = GetCharPressed();
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Backspace) && (text.Length > 0))
|
||||
{
|
||||
text.Remove(text.Length - 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
DrawRectangleRec(bounds, Color.RayWhite);
|
||||
DrawRectangleLinesEx(bounds, editMode ? 2 : 1, editMode ? Color.Red : (hover ? Color.Blue : Color.Gray));
|
||||
|
||||
string content = text.ToString();
|
||||
DrawText(content, (int)bounds.X + 4, (int)(bounds.Y + (bounds.Height - 20) / 2), 20, Color.DarkGray);
|
||||
|
||||
// Draw blinking cursor while editing
|
||||
if (editMode && ((framesCounter / 20) % 2 == 0))
|
||||
{
|
||||
DrawText("_", (int)bounds.X + 4 + MeasureText(content, 20), (int)(bounds.Y + (bounds.Height - 20) / 2), 20, Color.DarkGray);
|
||||
}
|
||||
|
||||
if (hover && IsMouseButtonPressed(MouseButton.Left))
|
||||
{
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
return pressed;
|
||||
}
|
||||
|
||||
private static void GuiTextBoxReadOnly(Rectangle bounds, string text)
|
||||
{
|
||||
DrawRectangleRec(bounds, Color.LightGray);
|
||||
DrawRectangleLinesEx(bounds, 1, Color.Gray);
|
||||
if (text != null)
|
||||
{
|
||||
DrawText(text, (int)bounds.X + 4, (int)(bounds.Y + (bounds.Height - 20) / 2), 20, Color.Gray);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - clipboard text");
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ClipboardText();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue