add support for system clipboard copy and paste

This commit is contained in:
Crispy 2025-03-29 19:44:44 +01:00
parent fc1670f97d
commit 57512a4c6b
5 changed files with 280 additions and 26 deletions

View file

@ -5,6 +5,7 @@ use std::{
time::Instant,
};
use arboard::Clipboard;
use raylib::prelude::*;
use crate::{
@ -449,7 +450,7 @@ impl Editor {
self.push_action(Action::SetTile(resize, pos, old_tile, tile));
}
pub fn update(&mut self, rl: &RaylibHandle) {
pub fn update(&mut self, rl: &RaylibHandle, clipboard: Option<&mut Clipboard>) {
self.tooltip.init_frame(rl);
self.mouse = MouseInput::get(rl);
if self.popup != Popup::None {
@ -550,12 +551,15 @@ impl Editor {
if self.sim_state == SimState::Editing {
if rl.is_key_down(KeyboardKey::KEY_LEFT_CONTROL) {
if rl.is_key_pressed(KeyboardKey::KEY_V) {
if let Ok(text) = rl.get_clipboard_text() {
let b = Board::from_user_str(&text);
self.pasting_board = Some(b);
if let Some(clipboard) = clipboard {
if rl.is_key_pressed(KeyboardKey::KEY_V) {
if let Ok(text) = clipboard.get_text() {
let b = Board::from_user_str(&text);
self.pasting_board = Some(b);
}
}
} else if rl.is_key_pressed(KeyboardKey::KEY_Z) {
}
if rl.is_key_pressed(KeyboardKey::KEY_Z) {
self.undo();
} else if rl.is_key_pressed(KeyboardKey::KEY_Y) {
self.redo();
@ -593,7 +597,12 @@ impl Editor {
}
}
pub fn draw(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
pub fn draw(
&mut self,
d: &mut RaylibDrawHandle,
textures: &Textures,
clipboard: Option<&mut Clipboard>,
) {
d.clear_background(BG_WORLD);
if self.draw_overlay && self.zoom >= 0.5 {
@ -612,7 +621,7 @@ impl Editor {
self.draw_board(d, textures);
self.board_overlay(d, textures);
self.draw_bottom_bar(d, textures);
self.draw_bottom_bar(d, textures, clipboard);
self.draw_top_bar(d, textures);
if self.active_tool == Tool::Blueprint {
@ -986,7 +995,12 @@ impl Editor {
}
}
fn draw_bottom_bar(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
fn draw_bottom_bar(
&mut self,
d: &mut RaylibDrawHandle,
textures: &Textures,
clipboard: Option<&mut Clipboard>,
) {
let height = d.get_screen_height();
let footer_top = (height - FOOTER_HEIGHT) as f32;
// background
@ -1036,7 +1050,13 @@ impl Editor {
&& d.is_key_down(KeyboardKey::KEY_LEFT_CONTROL))
{
let board = self.get_selected_as_board(selection);
self.pasting_board = Some(board);
if let Some(clipboard) = clipboard {
clipboard
.set_text(serde_json::to_string(&board).unwrap())
.unwrap();
} else {
self.pasting_board = Some(board);
}
}
draw_scaled_texture(d, textures.get("copy"), 192, y + 4, 2.);

View file

@ -3,6 +3,7 @@ use std::{
fs::{read_dir, read_to_string},
};
use arboard::Clipboard;
use raylib::prelude::*;
use marble_machinations::*;
@ -27,6 +28,7 @@ struct Game {
delete_solution: Option<usize>,
editing_solution_name: bool,
level_desc_text: ShapedText,
clipboard: Option<Clipboard>,
}
#[derive(Debug)]
@ -68,6 +70,9 @@ impl Game {
delete_solution: None,
editing_solution_name: false,
level_desc_text: ShapedText::new(20),
clipboard: Clipboard::new()
.map_err(|e| eprintln!("System clipboard error: {e}"))
.ok(),
}
}
@ -75,8 +80,8 @@ impl Game {
while !rl.window_should_close() {
let mut d = rl.begin_drawing(thread);
if let Some(editor) = &mut self.open_editor {
editor.update(&d);
editor.draw(&mut d, &self.textures);
editor.update(&d, self.clipboard.as_mut());
editor.draw(&mut d, &self.textures, self.clipboard.as_mut());
match editor.get_exit_state() {
ExitState::Dont => (),
ExitState::ExitAndSave => {