@@ -2,12 +2,20 @@ use std::fs::File;
22
33use destiny_pkg:: { TagHash , TagHash64 } ;
44use eframe:: egui;
5+ use image:: ImageFormat ;
6+ use lazy_static:: lazy_static;
57use log:: { error, info, warn} ;
6- use std:: io:: Write ;
8+ use std:: io:: { Cursor , Write } ;
9+ use std:: num:: NonZeroU32 ;
710
811use crate :: { packages:: package_manager, tagtypes:: TagType } ;
912
10- use super :: texture:: TextureCache ;
13+ use super :: texture:: { Texture , TextureCache } ;
14+
15+ lazy_static ! {
16+ static ref CF_PNG : NonZeroU32 = clipboard_win:: register_format( "PNG" ) . unwrap( ) ;
17+ static ref CF_FILENAME : NonZeroU32 = clipboard_win:: register_format( "FileNameW" ) . unwrap( ) ;
18+ }
1119
1220pub trait ResponseExt {
1321 fn tag_context ( & self , tag : TagHash , tag64 : Option < TagHash64 > ) -> & Self ;
@@ -34,7 +42,46 @@ impl ResponseExt for egui::Response {
3442 texture_cache : & TextureCache ,
3543 is_texture : bool ,
3644 ) -> Self {
37- self . context_menu ( |ui| tag_context ( ui, tag, tag64) ) ;
45+ self . context_menu ( |ui| {
46+ if is_texture {
47+ if ui. selectable_label ( false , "📷 Copy texture" ) . clicked ( ) {
48+ match Texture :: load ( & texture_cache. render_state , tag, false ) {
49+ Ok ( o) => {
50+ let image = o. to_image ( & texture_cache. render_state ) . unwrap ( ) ;
51+ let mut png_data = vec ! [ ] ;
52+ let mut png_writer = Cursor :: new ( & mut png_data) ;
53+ image. write_to ( & mut png_writer, ImageFormat :: Png ) . unwrap ( ) ;
54+
55+ let _clipboard = clipboard_win:: Clipboard :: new ( ) ;
56+ if let Err ( e) = clipboard_win:: raw:: set ( CF_PNG . get ( ) , & png_data) {
57+ error ! ( "Failed to copy texture to clipboard: {e}" ) ;
58+ }
59+
60+ // Save to temp
61+ let path = std:: env:: temp_dir ( ) . join ( format ! ( "{tag}.png" ) ) ;
62+ let mut file = File :: create ( & path) . unwrap ( ) ;
63+ file. write_all ( & png_data) . unwrap ( ) ;
64+
65+ let mut path_utf16 =
66+ path. to_string_lossy ( ) . encode_utf16 ( ) . collect :: < Vec < u16 > > ( ) ;
67+ path_utf16. push ( 0 ) ;
68+
69+ if let Err ( e) = clipboard_win:: raw:: set_without_clear (
70+ CF_FILENAME . get ( ) ,
71+ bytemuck:: cast_slice ( & path_utf16) ,
72+ ) {
73+ error ! ( "Failed to copy texture path to clipboard: {e}" ) ;
74+ }
75+ }
76+ Err ( e) => {
77+ error ! ( "Failed to load texture: {e}" ) ;
78+ }
79+ }
80+ ui. close_menu ( ) ;
81+ }
82+ }
83+ tag_context ( ui, tag, tag64) ;
84+ } ) ;
3885 if is_texture {
3986 self . on_hover_ui ( |ui| {
4087 texture_cache. texture_preview ( tag, ui) ;
0 commit comments