From e1864beed2d2bb5c33f2b164009aff22eb0694a4 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Sun, 27 Apr 2025 22:59:45 +0800 Subject: [PATCH] add example --- .../LocalMediaViewer/LocalMediaViewer.tscn | 68 ++++++++++++++-- .../Script/ViewLocalVideoMediaViewer.cs | 77 ++++++++++++++++++- project.godot | 2 +- 3 files changed, 139 insertions(+), 8 deletions(-) diff --git a/Example/LocalMediaViewer/LocalMediaViewer.tscn b/Example/LocalMediaViewer/LocalMediaViewer.tscn index 5b81fb7..e3f6c9a 100644 --- a/Example/LocalMediaViewer/LocalMediaViewer.tscn +++ b/Example/LocalMediaViewer/LocalMediaViewer.tscn @@ -1,7 +1,11 @@ -[gd_scene load_steps=2 format=3 uid="uid://df4gmjq2o1ttn"] +[gd_scene load_steps=4 format=3 uid="uid://df4gmjq2o1ttn"] [ext_resource type="Script" path="res://Example/LocalMediaViewer/Script/ViewLocalVideoMediaViewer.cs" id="1_hfhp3"] +[sub_resource type="VideoStreamTheora" id="VideoStreamTheora_6wg02"] + +[sub_resource type="ImageTexture" id="ImageTexture_2g46x"] + [node name="LocalMediaViewer" type="Control"] layout_mode = 3 anchors_preset = 15 @@ -20,10 +24,11 @@ anchor_right = 1.0 anchor_bottom = 0.8 grow_horizontal = 2 grow_vertical = 2 +stream = SubResource("VideoStreamTheora_6wg02") +expand = true metadata/_edit_use_anchors_ = true [node name="ViewImage" type="TextureRect" parent="."] -visible = false layout_mode = 1 anchors_preset = -1 anchor_left = 0.2 @@ -31,19 +36,28 @@ anchor_right = 1.0 anchor_bottom = 0.8 grow_horizontal = 2 grow_vertical = 2 +texture = SubResource("ImageTexture_2g46x") +expand_mode = 1 metadata/_edit_use_anchors_ = true -[node name="Scroll" type="ScrollContainer" parent="."] +[node name="Tab" type="TabContainer" parent="."] layout_mode = 1 anchors_preset = -1 anchor_right = 0.2 -anchor_bottom = 1.0 +anchor_bottom = 0.8 grow_horizontal = 2 grow_vertical = 2 +current_tab = 0 +metadata/_edit_use_anchors_ = true + +[node name="Media" type="ScrollContainer" parent="Tab"] +layout_mode = 2 metadata/_edit_use_anchors_ = true +metadata/_tab_index = 0 -[node name="MediaList" type="VBoxContainer" parent="Scroll"] +[node name="MediaList" type="VBoxContainer" parent="Tab/Media"] layout_mode = 2 +size_flags_horizontal = 3 [node name="ControlList" type="HBoxContainer" parent="."] layout_mode = 1 @@ -55,3 +69,47 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 metadata/_edit_use_anchors_ = true + +[node name="ImportList" type="HBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = -1 +anchor_top = 0.8 +anchor_right = 0.2 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +metadata/_edit_use_anchors_ = true + +[node name="UploadImage" type="Button" parent="ImportList"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "导入图片" + +[node name="UploadVideo" type="Button" parent="ImportList"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "导入视频" + +[node name="ImportImage" type="FileDialog" parent="."] +title = "Open a File" +initial_position = 1 +size = Vector2i(960, 640) +ok_button_text = "Open" +file_mode = 0 +access = 2 +filters = PackedStringArray("*.png", "*.jpg") + +[node name="ImportVideo" type="FileDialog" parent="."] +auto_translate_mode = 1 +title = "Open a File" +initial_position = 1 +size = Vector2i(960, 640) +ok_button_text = "Open" +file_mode = 0 +access = 2 +filters = PackedStringArray("*.ogv") + +[connection signal="pressed" from="ImportList/UploadImage" to="." method="OpenImportImage"] +[connection signal="pressed" from="ImportList/UploadVideo" to="." method="OpenImportVideo"] +[connection signal="file_selected" from="ImportImage" to="." method="UploadImage"] +[connection signal="file_selected" from="ImportVideo" to="." method="UploadVideo"] diff --git a/Example/LocalMediaViewer/Script/ViewLocalVideoMediaViewer.cs b/Example/LocalMediaViewer/Script/ViewLocalVideoMediaViewer.cs index 143896f..a27e79f 100644 --- a/Example/LocalMediaViewer/Script/ViewLocalVideoMediaViewer.cs +++ b/Example/LocalMediaViewer/Script/ViewLocalVideoMediaViewer.cs @@ -1,22 +1,95 @@ using Godot; using System; using EGFramework; +using System.IO; public partial class ViewLocalVideoMediaViewer : Node,IEGFramework { + public EGLocalFileSave localFileSave { get; set; } + public VideoStreamPlayer ViewVideo { get; set; } + public TextureRect ViewImage { get; set; } + public VBoxContainer VideoList { get; set; } + public FileDialog ImportImage { get; set; } + public FileDialog ImportVideo { get; set; } // Called when the node enters the scene tree for the first time. public override void _Ready() { - + ViewVideo = this.GetNode("ViewVideo"); + ViewImage = this.GetNode("ViewImage"); + VideoList = this.GetNode("Tab/Media/MediaList"); + ImportImage = this.GetNode("ImportImage"); + ImportVideo = this.GetNode("ImportVideo"); + LoadFileSystem(); } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { + } + public void LoadFileSystem(){ + this.localFileSave = new EGLocalFileSave(); + localFileSave.RootPath = "SaveData"; + if(!localFileSave.IsRemoteDirectoryExist("Media")){ + localFileSave.MakeDirectory("Media"); + } + RefreshList(); } public void RefreshList(){ - + string combinedPath = Path.Combine(localFileSave.RootPath, "Media"); + VideoList.ClearChildren(); + foreach (var file in localFileSave.ListRemoteFilePath("Media")){ + if(file.FileName.GetStrBehindSymbol('.')!="ogv" && file.FileName.GetStrBehindSymbol('.')!="jpg" && file.FileName.GetStrBehindSymbol('.')!="png"){ + continue; + } + Button button= new Button(); + button.Name = file.FileName; + button.Text = file.FileName; + string combinedFile = Path.Combine(combinedPath, button.Text); + button.Connect("pressed",Callable.From(()=>{ + if(button.Text.GetStrBehindSymbol('.')=="ogv"){ + PlayVideo(combinedFile); + } + if(button.Text.GetStrBehindSymbol('.')=="jpg" || button.Text.GetStrBehindSymbol('.')=="png"){ + PlayImage(combinedFile); + } + })); + VideoList.AddChild(button); + } + } + + public void PlayVideo(string videoName){ + ViewVideo.Visible = true; + ViewImage.Visible = false; + GD.Print("PlayVideo: " + videoName); + ViewVideo.Stream.File = videoName; + ViewVideo.Play(); + } + public void PlayImage(string imageName){ + ViewVideo.Visible = false; + ViewImage.Visible = true; + GD.Print("PlayImage: " + imageName); + Image image = Image.LoadFromFile(imageName); + ImageTexture texture = ImageTexture.CreateFromImage(image); + ViewImage.Texture = texture; + // ViewImage.Texture = imageName; + } + + public void OpenImportVideo(){ + ImportVideo.Popup(); + } + public void OpenImportImage(){ + ImportImage.Popup(); + } + public void UploadVideo(string localVideoPath){ + localFileSave.UploadFile(localVideoPath,"Media\\"+Path.GetFileName(localVideoPath)); + RefreshList(); + OS.Alert("Video Upload Success!","success"); + } + public void UploadImage(string localImagePath){ + localFileSave.UploadFile(localImagePath,"Media\\"+Path.GetFileName(localImagePath)); + RefreshList(); + OS.Alert("Image Upload Success!","success"); } } diff --git a/project.godot b/project.godot index d93e807..c694b77 100644 --- a/project.godot +++ b/project.godot @@ -12,7 +12,7 @@ config_version=5 config/name="EGFramework" config/tags=PackedStringArray("official") -run/main_scene="res://Example/UsingTest/Scene/TestEGFramework.tscn" +run/main_scene="res://Example/LocalMediaViewer/LocalMediaViewer.tscn" config/features=PackedStringArray("4.3", "C#", "GL Compatibility") config/icon="res://icon.svg"