Compare commits

...
4 Commits
4 changed files with 154 additions and 46 deletions
+102
View File
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSharpToJsonSchema;
using Ollama;
using RhythmBase.Components;
using System.ComponentModel;
namespace RDTKEditor.AIAssistant
{
public class Assistant
{
public const string Deepseek = "deepseek-r1:8b";
public const string Smollm = "smollm2:135m";
public const string Qwen_0_5 = "qwen2.5-coder:0.5b";
public const string Qwen_3 = "qwen2.5-coder:3b";
public OllamaApiClient Ollama { get; } = new(baseUri: new Uri("http://127.0.0.1:11434/api"));
public Chat Chat { get; }
public Assistant(string modelName)
{
Chat = Ollama.Chat(
model: modelName,
autoCallTools: true
);
var utils = new RhythmBaseUtils();
Chat.AddToolService(utils.AsTools().AsOllamaTools(), utils.AsCalls());
}
public async IAsyncEnumerable<GenerateCompletionResponse> GetResponse(string modelName, string userInput)
{
await foreach (var responce in Ollama.Completions.GenerateCompletionAsync(modelName, userInput))
{
if (responce != null)
{
yield return responce;
}
}
}
}
[GenerateJsonSchema]
public interface IRhythmBaseUtils
{
[Description("创建空关卡")]
public void CreateEmptyLevel();
[Description("创建关卡模板(默认关卡)")]
public void CreateLevelTemplate();
[Description("打开关卡")]
public void OpenLevel([Description("关卡路径")] string filepath);
[Description("添加轨道")]
public void AddRow(
[Description("轨道所在的房间")] RDRoomIndex roomIndex,
[Description("轨道名称")] string trackName
);
[Description("保存关卡")]
public void SaveLevel([Description("关卡路径,后缀为 .rdlevel")] string filepath);
}
public class RhythmBaseUtils : IRhythmBaseUtils
{
private RDLevel? level;
public void CreateEmptyLevel()
{
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine($"Called CreateEmptyLevel");
Console.ResetColor();
level = [];
}
public void CreateLevelTemplate()
{
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine($"Called CreateLevelTemplate");
Console.ResetColor();
level = RDLevel.Default;
}
public void OpenLevel(string filepath)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine($"Called OpenLevel: {filepath}");
Console.ResetColor();
level = RDLevel.Read(filepath);
}
public void AddRow(RDRoomIndex roomIndex, string trackName)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine($"Called AddRow: [{roomIndex}] {trackName}");
Console.ResetColor();
level?.CreateRow(roomIndex, trackName);
}
public void SaveLevel(string filepath)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine($"Called SaveLevel: {filepath}");
Console.ResetColor();
string dir = Path.GetDirectoryName(filepath) ?? "";
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
level?.Write(filepath);
}
}
}
+9 -2
View File
@@ -54,12 +54,19 @@
<GridSplitter Grid.Column="3" Width="5" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Border Grid.Column="4" Background="LightBlue" >
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid x:Name="timelineCanvas3" Background="LightGray" />
<Grid Background="LightGray">
<TextBox x:Name="AIAssistantText" HorizontalAlignment="Left" VerticalAlignment="Top">
传给 AI 的文本
</TextBox>
<Button x:Name="AIAssistantButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Click="AIAssistantButton_Click" >
测试
</Button>
</Grid>
</ScrollViewer>
</Border>
<GridSplitter Grid.Row="1" Grid.ColumnSpan="5" Height="5" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Border Grid.ColumnSpan="5" Grid.Row="2" Background="LightSalmon">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ScrollViewer x:Name="timelineViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas x:Name="timelineCanvas" Background="LightGray" />
</ScrollViewer>
</Border>
+42 -44
View File
@@ -1,9 +1,11 @@
using Microsoft.Win32;
using Ollama;
using RDTKEditor.AIAssistant;
using RDTKEditor.Models;
using RhythmBase.Events;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
@@ -16,6 +18,8 @@ namespace RDTKEditor
/// </summary>
public partial class MainWindow : Window
{
private Assistant Assistant = new(Assistant.Qwen_3);
private SKSizeI _vsScale = new(1600,900);
private readonly RDTKView viewModel = new();
public MainWindow()
{
@@ -251,51 +255,45 @@ namespace RDTKEditor
});
}*/
private void mainUI_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
{
var sfc = e.Surface;
var cvs = sfc.Canvas;
var info = e.Info;
private void mainUI_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
var sfc = e.Surface;
var cvs = sfc.Canvas;
var info = e.Info;
cvs.Clear(SKColors.Black);
SKPoint scale = new(info.Width / _vsScale.Width, info.Height / _vsScale.Height);
cvs.Save();
cvs.Scale(scale);
Draw(cvs);
cvs.Restore();
}
private void Draw(SKCanvas canvas)
{
// 以 1600x900 为基准绘制
// 清除画布
cvs.Clear(SKColors.Black);
canvas.DrawLine(0, 0, _vsScale.Width, _vsScale.Height, new SKPaint
{
Color = SKColors.Red,
StrokeWidth = 3,
IsStroke = true,
});
canvas.DrawLine(0, _vsScale.Height, _vsScale.Width, 0, new SKPaint
{
Color = SKColors.White,
StrokeWidth = 3,
IsStroke = true,
});
}
// 获取绘图表面的宽度和高度
float width = info.Width;
float height = info.Height;
// 设置缩放比例
float scaleX = width / 1600f;
float scaleY = height / 900f;
// 保存画布状态
cvs.Save();
// 应用缩放
cvs.Scale(scaleX, scaleY);
// 绘制内容
cvs.DrawLine(0, 0, 1600, 900, new SKPaint
{
Color = SKColors.Red,
StrokeWidth = 3,
IsStroke = true,
});
cvs.DrawLine(0, 900, 1600, 0, new SKPaint
{
Color = SKColors.White,
StrokeWidth = 3,
IsStroke = true,
});
// 恢复画布状态
cvs.Restore();
// 打印绘图表面的尺寸
Debug.Print(info.Size.ToString());
}
}
private async void AIAssistantButton_Click(object sender, RoutedEventArgs e)
{
string input = AIAssistantText.Text;
if (string.IsNullOrEmpty(input))
return;
Message responce = await Assistant.Chat.SendAsync(input);
AIAssistantText.Text = responce.Content;
}
}
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
+1
View File
@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Ollama" Version="1.15.0" />
<PackageReference Include="SkiaSharp.Views.WPF" Version="3.118.0-preview.2.3" />
<PackageReference Include="sly" Version="3.6.1" />
</ItemGroup>