Files
GC/model/contour-map/src/ContourMapRuntime.cs
T
Morris 5b628971a9
PR 审核 / 后端编译 (pull_request) Canceled after 0s
PR 审核 / 后端自检 (pull_request) Canceled after 0s
PR 审核 / 前端检查(Flutter) (pull_request) Canceled after 0s
PR 审核 / 安全扫描 (pull_request) Canceled after 0s
PR 审核 / 审核汇总 (pull_request) Canceled after 0s
feat(contour-map): 重写为可交互 3D 等高线模型,新增五大地貌专题
- 新增 LandformKind 与五套高度场生成器:穹状山峰、宽脊山脊、V 形山谷、双峰鞍部、台地陡崖
- 3D 视图重写:拖动旋转 / 滚轮缩放、按包围盒自动取景、法线光照、四周裙边侧壁、
  切割步骤改为把高度压到切面得到真实截面并用求交线精确出线
- 四步实验动画(等高切割 → 形成交线 → 垂直投影 → 疏密判坡),俯视 / 立体切换
- 界面重排:地貌标签移到画布上方(WrapPanel,窄屏自动换行并隐藏侧栏),
  右侧栏放实验流程 / 观察面板 / 判读要点;播放、重置、视图、上色、实时读数,空格与 1–5 快捷键、全屏
- 文案 90 个键中英双语;LoadTexts 改为「内置默认 + i18n 文件覆盖」,此前完全忽略语言包
- 修复三处渲染缺陷:Chain() 只向尾部延伸把等高线切成虚线;坡度归一化写死导致判坡整片红;
  切割只是叠加半透明板而山尖仍在
- 清单版本 1.1 → 1.2,描述与 help.json 同步;62×62 面片 + 画刷缓存,1440×900 单帧约 35ms
2026-11-19 20:45:03 +08:00

97 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using GC.ModelSdk;
namespace GC.ContourMap;
/// <summary>等高线模型入口(清单里的 type 指向这里)。</summary>
public sealed class ContourMapFactory : IModelFactory
{
public int ApiVersion => 1;
public IModelRuntime Create() => new ContourMapRuntime();
}
/// <summary>
/// 等高线模型运行时。
/// 依据人教版七年级上册「地形图的判读」:海拔与相对高度、等高线与等高距、
/// 山峰/山脊/山谷/鞍部/陡崖的判读、坡度陡缓与等高线疏密的关系。
/// </summary>
public sealed class ContourMapRuntime : IModelRuntime
{
private TerrainField? _field;
private Dictionary<string, string>? _texts;
private ContourPage? _page;
public void Prepare(IModelContext context)
{
context.ReportProgress(0.15, "生成地形高度场");
_field = new TerrainField(TerrainKind.Mountain, 192);
context.ReportProgress(0.70, "提取等高线");
_texts = LoadTexts(context.DataDirectory, context.Language);
context.ReportProgress(0.92, "准备界面");
context.Log($"[contour-map] 地形 {_field.Size}×{_field.Size}," +
$"海拔 {_field.MinHeight:0}~{_field.MaxHeight:0} m,等高距 {_field.EffectiveInterval:0} m");
}
public Control CreateView(IModelContext context)
{
_texts ??= LoadTexts(context.DataDirectory, context.Language);
// 实验地形 + 五大地貌都在界面里现场生成(切换很快,不必预先生成全部)
_page = new ContourPage(_texts);
return _page.Build();
}
public void Dispose()
{
_page?.Dispose();
_page = null;
_field = null;
_texts = null;
System.GC.SuppressFinalize(this);
}
/// <summary>
/// 文案 = 内置默认(保证任何语言都有词)+ dataDir/i18n/&lt;lang&gt;.json 覆盖。
/// 语言包缺失或某条没翻译时自动退回内置默认,不会露出裸 key。
/// </summary>
private static Dictionary<string, string> LoadTexts(string dataDir, string language)
{
var isEn = language.StartsWith("en", StringComparison.OrdinalIgnoreCase);
var map = new Dictionary<string, string>(DefaultTexts.Zh);
if (isEn) foreach (var kv in DefaultTexts.En) map[kv.Key] = kv.Value;
var candidates = new List<string>();
if (!string.IsNullOrWhiteSpace(language)) candidates.Add(language);
candidates.Add(isEn ? "en" : "zh-CN");
foreach (var lang in candidates)
{
var path = System.IO.Path.Combine(dataDir, "i18n", lang + ".json");
if (!System.IO.File.Exists(path)) continue;
try
{
using var doc = System.Text.Json.JsonDocument.Parse(System.IO.File.ReadAllText(path));
if (doc.RootElement.ValueKind != System.Text.Json.JsonValueKind.Object) continue;
foreach (var p in doc.RootElement.EnumerateObject())
{
if (p.Value.ValueKind != System.Text.Json.JsonValueKind.String) continue;
var v = p.Value.GetString();
if (!string.IsNullOrWhiteSpace(v)) map[p.Name] = v!;
}
break;
}
catch
{
// 语言包损坏不影响开课,继续用内置默认
}
}
return map;
}
}