同步
This commit is contained in:
1 parent
936aec8185
commit
874c97ee27
96 files changed
+3412
-106
No files matched your search
@@ -0,0 +1,251 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>内置全量文案兜底:即使模型包里没有语言包也能完整显示。</summary>
|
||||
internal static class DefaultTexts
|
||||
{
|
||||
public static readonly Dictionary<string, string> Zh = new()
|
||||
{
|
||||
["app.title"] = "比例尺计算器",
|
||||
["app.subtitle"] = "把「图上距离 ÷ 实地距离」变成可以拖、可以量的可视化标尺",
|
||||
["mode.real"] = "求实地距离",
|
||||
["mode.map"] = "求图上距离",
|
||||
["mode.scale"] = "求比例尺",
|
||||
["mode.hint"] = "选择要求什么,蓝色结果卡就是答案",
|
||||
|
||||
["input.title"] = "已知量",
|
||||
["input.map"] = "图上距离",
|
||||
["input.real"] = "实地距离",
|
||||
["input.scale"] = "比例尺分母 N",
|
||||
["input.nPrefix"] = "1 :",
|
||||
["input.unit"] = "单位",
|
||||
["input.reset"] = "重置",
|
||||
["input.presets"] = "常用比例尺",
|
||||
["input.dragHint"] = "拖动滑块或直接输入数字,读数实时联动",
|
||||
|
||||
["result.title"] = "计算结果",
|
||||
["result.real"] = "实地距离",
|
||||
["result.map"] = "图上距离",
|
||||
["result.scale"] = "比例尺",
|
||||
["result.formula"] = "计算公式",
|
||||
|
||||
["forms.title"] = "比例尺的三种写法",
|
||||
["forms.digital"] = "数字式",
|
||||
["forms.verbal"] = "文字式",
|
||||
["forms.segment"] = "线段式",
|
||||
["forms.verbalTpl"] = "图上 1 厘米代表实地 {0}",
|
||||
["forms.segmentDesc"] = "每格 {0},共 {1} 格",
|
||||
|
||||
["steps.title"] = "解题步骤",
|
||||
["steps.show"] = "展开步骤",
|
||||
["steps.hide"] = "收起步骤",
|
||||
["steps.1.unify"] = "① 统一单位:把实地距离化成厘米(1 km = 100000 cm,去掉 5 个 0)",
|
||||
["steps.2.formulaReal"] = "② 实地距离 = 图上距离 × 比例尺分母",
|
||||
["steps.2.formulaMap"] = "② 图上距离 = 实地距离 ÷ 比例尺分母",
|
||||
["steps.2.formulaScale"] = "② 比例尺分母 = 实地距离 ÷ 图上距离",
|
||||
["steps.3.substitute"] = "③ 代入数值:{0}",
|
||||
["steps.4.result"] = "④ 算出结果:{0}",
|
||||
["steps.5.convert"] = "⑤ 换算成合适单位(cm → m → km)",
|
||||
|
||||
["grade.title"] = "大小判读",
|
||||
["grade.large"] = "大比例尺",
|
||||
["grade.medium"] = "中比例尺",
|
||||
["grade.small"] = "小比例尺",
|
||||
["grade.largeNote"] = "范围小 · 内容详细 · 如校园平面图",
|
||||
["grade.mediumNote"] = "范围中等 · 如城市交通图",
|
||||
["grade.smallNote"] = "范围大 · 内容简略 · 如世界地图",
|
||||
["grade.rule"] = "分母越大 → 比例尺越小 → 范围越大 → 越简略",
|
||||
|
||||
["pit.title"] = "易错点",
|
||||
["pit.1"] = "① 忘记统一单位:km 要 ×100000 才成 cm",
|
||||
["pit.2"] = "② 「放大 2 倍」是分母 ÷ 2,不是 × 2",
|
||||
["pit.3"] = "③ 图上距离与实地距离别代反了",
|
||||
|
||||
["field.title"] = "实地尺 · 这段距离画到图上多长",
|
||||
["field.hint"] = "选一个真实距离,改变比例尺看它在图上的长度",
|
||||
["field.mapLength"] = "图上 {0}",
|
||||
["field.tooLong"] = "画不下",
|
||||
["field.tooShort"] = "几乎看不见",
|
||||
|
||||
["quiz.title"] = "练习闯关",
|
||||
["quiz.promptReal"] = "比例尺 1:{0},图上 {1} 厘米,实地距离是多少?(按提示单位填数字)",
|
||||
["quiz.promptMap"] = "比例尺 1:{0},实地 {1},图上距离是多少厘米?",
|
||||
["quiz.promptScale"] = "图上 {0} 厘米代表实地 {1},比例尺分母是多少?",
|
||||
["quiz.answer"] = "输入答案",
|
||||
["quiz.check"] = "检查",
|
||||
["quiz.next"] = "下一题",
|
||||
["quiz.correct"] = "✓ 答对了!",
|
||||
["quiz.wrong"] = "✗ 再想想,看看解题步骤",
|
||||
["quiz.streak"] = "连对 {0} 题",
|
||||
["quiz.showSteps"] = "查看解题步骤",
|
||||
["quiz.unit"] = "单位:{0}",
|
||||
|
||||
["ruler.title"] = "可视化标尺",
|
||||
["ruler.mapBar"] = "图上距离(真实厘米)",
|
||||
["ruler.realBar"] = "实地距离(按同一比例尺压缩)",
|
||||
["ruler.sameWidth"] = "与上方同宽",
|
||||
["ruler.equals"] = "等于",
|
||||
["ruler.segment"] = "线段式比例尺",
|
||||
["ruler.segmentHint"] = "刻度自动换挡",
|
||||
["ruler.spectrum"] = "比例尺光谱",
|
||||
["ruler.scopeUp"] = "◀ 范围越大",
|
||||
["ruler.detailDown"] = "内容越详细 ▶",
|
||||
["ruler.hint"] = "蓝色是图上真实长度;绿色是同一段实地按比例尺压缩后的长度",
|
||||
["ruler.overflowMap"] = "这段图上距离超出图幅,需要更小的比例尺",
|
||||
["ruler.overflowReal"] = "这段实地距离在该比例尺下画不下,需要更小的比例尺",
|
||||
|
||||
["unit.cm"] = "厘米",
|
||||
["unit.m"] = "米",
|
||||
["unit.km"] = "千米",
|
||||
["unit.none"] = "(无单位)",
|
||||
|
||||
["spec.campus"] = "校园",
|
||||
["spec.city"] = "城市",
|
||||
["spec.china"] = "中国",
|
||||
["spec.world"] = "世界",
|
||||
|
||||
["preset.campus"] = "校园 1:1万",
|
||||
["preset.campusNote"] = "操场、教学楼都画得清",
|
||||
["preset.town"] = "乡镇 1:5万",
|
||||
["preset.townNote"] = "村镇与道路",
|
||||
["preset.city"] = "城市 1:25万",
|
||||
["preset.cityNote"] = "城区与主干道",
|
||||
["preset.province"] = "省区 1:100万",
|
||||
["preset.provinceNote"] = "地级市与河流",
|
||||
["preset.china"] = "中国 1:1000万",
|
||||
["preset.chinaNote"] = "省级轮廓",
|
||||
["preset.world"] = "世界 1:1亿",
|
||||
["preset.worldNote"] = "大洲大洋",
|
||||
|
||||
["lm.classroom"] = "教室 10 m",
|
||||
["lm.track"] = "操场一圈 400 m",
|
||||
["lm.marathon"] = "马拉松 42.195 km",
|
||||
["lm.beijingShanghai"] = "北京—上海 1080 km",
|
||||
["lm.equator"] = "赤道 4 万 km",
|
||||
|
||||
["common.footer"] = "比例尺 = 图上距离 ÷ 实地距离 · 单位统一是第一步 · 分母越大,比例尺越小"
|
||||
};
|
||||
|
||||
public static readonly Dictionary<string, string> En = new()
|
||||
{
|
||||
["app.title"] = "Scale Calculator",
|
||||
["app.subtitle"] = "Turn map-distance ÷ real-distance into a ruler you can drag and measure",
|
||||
["mode.real"] = "Find real distance",
|
||||
["mode.map"] = "Find map distance",
|
||||
["mode.scale"] = "Find scale",
|
||||
["mode.hint"] = "Pick what to solve; the blue card is the answer",
|
||||
|
||||
["input.title"] = "Given",
|
||||
["input.map"] = "Map distance",
|
||||
["input.real"] = "Real distance",
|
||||
["input.scale"] = "Scale denominator N",
|
||||
["input.nPrefix"] = "1 :",
|
||||
["input.unit"] = "Unit",
|
||||
["input.reset"] = "Reset",
|
||||
["input.presets"] = "Common scales",
|
||||
["input.dragHint"] = "Drag the slider or type a number — everything updates live",
|
||||
|
||||
["result.title"] = "Result",
|
||||
["result.real"] = "Real distance",
|
||||
["result.map"] = "Map distance",
|
||||
["result.scale"] = "Scale",
|
||||
["result.formula"] = "Formula",
|
||||
|
||||
["forms.title"] = "Three ways to write a scale",
|
||||
["forms.digital"] = "Numeric",
|
||||
["forms.verbal"] = "Verbal",
|
||||
["forms.segment"] = "Graphic (bar)",
|
||||
["forms.verbalTpl"] = "1 cm on the map = {0} on the ground",
|
||||
["forms.segmentDesc"] = "Each division {0}, {1} divisions",
|
||||
|
||||
["steps.title"] = "Worked solution",
|
||||
["steps.show"] = "Show steps",
|
||||
["steps.hide"] = "Hide steps",
|
||||
["steps.1.unify"] = "① Unify units: convert the real distance to cm (1 km = 100000 cm)",
|
||||
["steps.2.formulaReal"] = "② Real distance = map distance × scale denominator",
|
||||
["steps.2.formulaMap"] = "② Map distance = real distance ÷ scale denominator",
|
||||
["steps.2.formulaScale"] = "② Scale denominator = real distance ÷ map distance",
|
||||
["steps.3.substitute"] = "③ Substitute: {0}",
|
||||
["steps.4.result"] = "④ Result: {0}",
|
||||
["steps.5.convert"] = "⑤ Convert to a sensible unit (cm → m → km)",
|
||||
|
||||
["grade.title"] = "Large / small scale",
|
||||
["grade.large"] = "Large scale",
|
||||
["grade.medium"] = "Medium scale",
|
||||
["grade.small"] = "Small scale",
|
||||
["grade.largeNote"] = "Small area · detailed · e.g. campus plan",
|
||||
["grade.mediumNote"] = "Medium area · e.g. city map",
|
||||
["grade.smallNote"] = "Large area · simplified · e.g. world map",
|
||||
["grade.rule"] = "Bigger denominator → smaller scale → larger area → less detail",
|
||||
|
||||
["pit.title"] = "Common mistakes",
|
||||
["pit.1"] = "① Forgetting to unify units: km ×100000 = cm",
|
||||
["pit.2"] = "② \"Zoom 2×\" means denominator ÷ 2, not × 2",
|
||||
["pit.3"] = "③ Don't swap map distance and real distance",
|
||||
|
||||
["field.title"] = "Real-world ruler · how long on the map",
|
||||
["field.hint"] = "Pick a real distance and change the scale to see its map length",
|
||||
["field.mapLength"] = "On map {0}",
|
||||
["field.tooLong"] = "too long",
|
||||
["field.tooShort"] = "barely visible",
|
||||
|
||||
["quiz.title"] = "Practice",
|
||||
["quiz.promptReal"] = "Scale 1:{0}, map distance {1} cm — what is the real distance? (enter the number in the shown unit)",
|
||||
["quiz.promptMap"] = "Scale 1:{0}, real distance {1} — what is the map distance in cm?",
|
||||
["quiz.promptScale"] = "{0} cm on the map = {1} on the ground — what is the scale denominator?",
|
||||
["quiz.answer"] = "Your answer",
|
||||
["quiz.check"] = "Check",
|
||||
["quiz.next"] = "Next",
|
||||
["quiz.correct"] = "✓ Correct!",
|
||||
["quiz.wrong"] = "✗ Try again — read the steps",
|
||||
["quiz.streak"] = "{0} in a row",
|
||||
["quiz.showSteps"] = "Show solution",
|
||||
["quiz.unit"] = "Unit: {0}",
|
||||
|
||||
["ruler.title"] = "Visual ruler",
|
||||
["ruler.mapBar"] = "Map distance (real cm)",
|
||||
["ruler.realBar"] = "Real distance (compressed by the same scale)",
|
||||
["ruler.sameWidth"] = "same width as above",
|
||||
["ruler.equals"] = "equals",
|
||||
["ruler.segment"] = "Graphic scale",
|
||||
["ruler.segmentHint"] = "auto-ranging ticks",
|
||||
["ruler.spectrum"] = "Scale spectrum",
|
||||
["ruler.scopeUp"] = "◀ larger area",
|
||||
["ruler.detailDown"] = "more detail ▶",
|
||||
["ruler.hint"] = "Blue is the real map length; green is the same real distance compressed by the scale",
|
||||
["ruler.overflowMap"] = "This map distance overflows the sheet — use a smaller scale",
|
||||
["ruler.overflowReal"] = "This real distance won't fit at this scale — use a smaller scale",
|
||||
|
||||
["unit.cm"] = "cm",
|
||||
["unit.m"] = "m",
|
||||
["unit.km"] = "km",
|
||||
["unit.none"] = "(unitless)",
|
||||
|
||||
["spec.campus"] = "Campus",
|
||||
["spec.city"] = "City",
|
||||
["spec.china"] = "China",
|
||||
["spec.world"] = "World",
|
||||
|
||||
["preset.campus"] = "Campus 1:10k",
|
||||
["preset.campusNote"] = "Buildings clearly drawn",
|
||||
["preset.town"] = "Town 1:50k",
|
||||
["preset.townNote"] = "Villages and roads",
|
||||
["preset.city"] = "City 1:250k",
|
||||
["preset.cityNote"] = "Urban area and arterials",
|
||||
["preset.province"] = "Province 1:1M",
|
||||
["preset.provinceNote"] = "Cities and rivers",
|
||||
["preset.china"] = "China 1:10M",
|
||||
["preset.chinaNote"] = "Province outlines",
|
||||
["preset.world"] = "World 1:100M",
|
||||
["preset.worldNote"] = "Continents & oceans",
|
||||
|
||||
["lm.classroom"] = "Classroom 10 m",
|
||||
["lm.track"] = "Running track 400 m",
|
||||
["lm.marathon"] = "Marathon 42.195 km",
|
||||
["lm.beijingShanghai"] = "Beijing–Shanghai 1080 km",
|
||||
["lm.equator"] = "Equator 40,000 km",
|
||||
|
||||
["common.footer"] = "Scale = map distance ÷ real distance · unify units first · bigger denominator = smaller scale"
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using EarthModelStudio.ModelSdk;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>比例尺计算器模型入口(清单里的 type 指向这里)。</summary>
|
||||
public sealed class ScaleCalculatorFactory : IModelFactory
|
||||
{
|
||||
public int ApiVersion => 1;
|
||||
public IModelRuntime Create() => new ScaleCalculatorRuntime();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<RootNamespace>NeoRF.ScaleCalculator</RootNamespace>
|
||||
<AssemblyName>ScaleCalculatorModel</AssemblyName>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- 本模型不复用 _engine 的球面渲染,只用 Avalonia 基础控件 + 一个自绘标尺 -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\_sdk\EarthModelStudio.ModelSdk\EarthModelStudio.ModelSdk.csproj">
|
||||
<Private>false</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Avalonia.Controls;
|
||||
using EarthModelStudio.ModelSdk;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>
|
||||
/// 比例尺计算器运行时。Prepare 在后台线程读数据/语言包,CreateView 在 UI 线程构建界面。
|
||||
/// 不依赖 _engine(没有球面渲染),只用 Avalonia 基础控件 + 一个自绘标尺。
|
||||
/// </summary>
|
||||
public sealed class ScaleCalculatorRuntime : IModelRuntime
|
||||
{
|
||||
private ScaleData? _data;
|
||||
private Dictionary<string, string>? _texts;
|
||||
|
||||
public void Prepare(IModelContext context)
|
||||
{
|
||||
context.ReportProgress(0.1, "载入预设数据");
|
||||
_data = ScaleData.Load(context.DataDirectory);
|
||||
|
||||
context.ReportProgress(0.55, "载入语言包");
|
||||
_texts = LoadTexts(context.DataDirectory, context.Language);
|
||||
|
||||
context.ReportProgress(0.9, "准备界面");
|
||||
context.Log($"[scale-calculator] 语言 {context.Language}," +
|
||||
$"预设 {_data.Presets.Count} 档 / 地标 {_data.Landmarks.Count} 个");
|
||||
}
|
||||
|
||||
public Control CreateView(IModelContext context)
|
||||
{
|
||||
context.ReportProgress(0.96, "构建界面");
|
||||
_data ??= ScaleData.Load(context.DataDirectory);
|
||||
_texts ??= LoadTexts(context.DataDirectory, context.Language);
|
||||
var view = new ScaleCalculatorView(_data, _texts, context.Language);
|
||||
context.ReportProgress(1.0, "准备就绪");
|
||||
return view;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data = null;
|
||||
_texts = null;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> LoadTexts(string dataDir, string language)
|
||||
{
|
||||
// 先内置中文兜底,再按 language 覆盖,最后用 zh-CN 补齐缺键
|
||||
var map = new Dictionary<string, string>(DefaultTexts.Zh);
|
||||
var isEn = language.StartsWith("en", StringComparison.OrdinalIgnoreCase);
|
||||
if (isEn)
|
||||
foreach (var kv in DefaultTexts.En) map[kv.Key] = kv.Value;
|
||||
|
||||
Overlay(Path.Combine(dataDir, "i18n", "zh-CN.json"), map);
|
||||
if (isEn) Overlay(Path.Combine(dataDir, "i18n", "en.json"), map);
|
||||
else Overlay(Path.Combine(dataDir, "i18n", language + ".json"), map);
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void Overlay(string path, Dictionary<string, string> target)
|
||||
{
|
||||
if (!File.Exists(path)) return;
|
||||
try
|
||||
{
|
||||
var extra = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(path),
|
||||
new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true });
|
||||
if (extra == null) return;
|
||||
foreach (var kv in extra) target[kv.Key] = kv.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"[scale-calculator] 语言包读取失败 {path}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,489 @@
|
||||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
public sealed partial class ScaleCalculatorView
|
||||
{
|
||||
// 模式条
|
||||
private Button _btnReal = null!, _btnMap = null!, _btnScale = null!;
|
||||
|
||||
// 左栏输入
|
||||
private Slider _mapSlider = null!;
|
||||
private TextBox _mapBox = null!, _realBox = null!, _nBox = null!;
|
||||
private ComboBox _unitBox = null!;
|
||||
private TextBlock _mapCardLabel = null!, _realCardLabel = null!, _scaleCardLabel = null!;
|
||||
private TextBlock _mapResultText = null!, _realResultText = null!, _scaleResultText = null!;
|
||||
private Border _mapCard = null!, _realCard = null!, _scaleCard = null!;
|
||||
private WrapPanel _presetPanel = null!;
|
||||
|
||||
// 中栏
|
||||
private ScaleRuler _ruler = null!;
|
||||
|
||||
// 右栏
|
||||
private TextBlock _digitalText = null!, _verbalText = null!, _segmentText = null!;
|
||||
private TextBlock _gradeTitle = null!, _gradeNote = null!, _gradeRule = null!;
|
||||
private Border _gradeChip = null!;
|
||||
private StackPanel _stepsPanel = null!;
|
||||
private Button _stepsToggle = null!;
|
||||
private bool _stepsOpen;
|
||||
|
||||
// 底部:实地尺
|
||||
private ComboBox _landmarkBox = null!;
|
||||
private TextBlock _landmarkResult = null!;
|
||||
|
||||
// 底部:练习
|
||||
private TextBlock _quizPrompt = null!, _quizFeedback = null!, _quizStreak = null!, _quizUnit = null!;
|
||||
private TextBox _quizInput = null!;
|
||||
private Button _quizCheck = null!, _quizNext = null!;
|
||||
private StackPanel _quizSteps = null!;
|
||||
|
||||
private void BuildLayout()
|
||||
{
|
||||
var root = new Grid { Background = B(CAppBg) };
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); // 顶部
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Star)); // 三栏
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); // 底部
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); // 页脚
|
||||
|
||||
var header = BuildHeader();
|
||||
Grid.SetRow(header, 0);
|
||||
root.Children.Add(header);
|
||||
|
||||
var columns = new Grid { Margin = new Thickness(16, 12, 16, 8) };
|
||||
columns.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(300)));
|
||||
columns.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(16)));
|
||||
columns.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
|
||||
columns.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(16)));
|
||||
columns.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(320)));
|
||||
|
||||
var left = BuildLeft();
|
||||
Grid.SetColumn(left, 0);
|
||||
columns.Children.Add(left);
|
||||
var mid = BuildMiddle();
|
||||
Grid.SetColumn(mid, 2);
|
||||
columns.Children.Add(mid);
|
||||
var right = BuildRight();
|
||||
Grid.SetColumn(right, 4);
|
||||
columns.Children.Add(right);
|
||||
Grid.SetRow(columns, 1);
|
||||
root.Children.Add(columns);
|
||||
|
||||
var bottom = BuildBottom();
|
||||
Grid.SetRow(bottom, 2);
|
||||
root.Children.Add(bottom);
|
||||
|
||||
var footer = new Border
|
||||
{
|
||||
Background = B(CSurfaceAlt),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(0, 1, 0, 0),
|
||||
Padding = new Thickness(20, 9),
|
||||
Child = Tb(T("common.footer"), 11, CInkFaint)
|
||||
};
|
||||
Grid.SetRow(footer, 3);
|
||||
root.Children.Add(footer);
|
||||
|
||||
Content = root;
|
||||
}
|
||||
|
||||
private Control BuildHeader()
|
||||
{
|
||||
var title = Tb(T("app.title"), 22, CInk, FontWeight.SemiBold);
|
||||
var sub = Tb(T("app.subtitle"), 12, CInkDim);
|
||||
|
||||
_btnReal = Btn(T("mode.real"));
|
||||
_btnMap = Btn(T("mode.map"));
|
||||
_btnScale = Btn(T("mode.scale"));
|
||||
|
||||
var modes = H(8);
|
||||
modes.Children.Add(_btnReal);
|
||||
modes.Children.Add(_btnMap);
|
||||
modes.Children.Add(_btnScale);
|
||||
|
||||
var leftStack = V(2);
|
||||
leftStack.Children.Add(title);
|
||||
leftStack.Children.Add(sub);
|
||||
|
||||
var grid = new Grid
|
||||
{
|
||||
Background = B(CSurface),
|
||||
Margin = new Thickness(0)
|
||||
};
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Auto));
|
||||
var leftBorder = new Border { Padding = new Thickness(20, 14, 10, 14), Child = leftStack };
|
||||
Grid.SetColumn(leftBorder, 0);
|
||||
grid.Children.Add(leftBorder);
|
||||
var rightBorder = new Border
|
||||
{
|
||||
Padding = new Thickness(10, 14, 20, 14),
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Child = V(4)
|
||||
};
|
||||
// 右侧:提示 + 模式按钮
|
||||
var rv = (StackPanel)rightBorder.Child!;
|
||||
rv.Children.Add(Tb(T("mode.hint"), 10.5, CInkFaint, align: TextAlignment.Right));
|
||||
rv.Children.Add(modes);
|
||||
Grid.SetColumn(rightBorder, 1);
|
||||
grid.Children.Add(rightBorder);
|
||||
|
||||
var outer = new Border
|
||||
{
|
||||
Background = B(CSurface),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(0, 0, 0, 1),
|
||||
Child = grid
|
||||
};
|
||||
return outer;
|
||||
}
|
||||
|
||||
private Control BuildLeft()
|
||||
{
|
||||
var panel = V(10);
|
||||
|
||||
var head = H(6);
|
||||
head.Children.Add(new Border
|
||||
{
|
||||
Width = 4, Height = 16, Background = B(CAccent),
|
||||
CornerRadius = new CornerRadius(2)
|
||||
});
|
||||
head.Children.Add(H3(T("input.title")));
|
||||
panel.Children.Add(head);
|
||||
|
||||
// ── 图上距离卡
|
||||
panel.Children.Add(BuildInputCard(
|
||||
"input.map", "unit.cm", out _mapCard, out _mapCardLabel, out _mapResultText,
|
||||
out _mapSlider, out _mapBox, withSlider: true));
|
||||
|
||||
// ── 实地距离卡
|
||||
panel.Children.Add(BuildRealCard());
|
||||
|
||||
// ── 比例尺卡
|
||||
panel.Children.Add(BuildScaleCard());
|
||||
|
||||
// 预设
|
||||
panel.Children.Add(Label(T("input.presets"), 11.5, CInkFaint));
|
||||
_presetPanel = new WrapPanel { Orientation = Orientation.Horizontal };
|
||||
foreach (var p in _data.Presets)
|
||||
{
|
||||
var b = Btn(T(p.NameKey));
|
||||
b.Tag = p;
|
||||
b.Click += (_, _) => ApplyPreset((PresetItem)b.Tag!);
|
||||
b.Margin = new Thickness(0, 0, 6, 6);
|
||||
_presetPanel.Children.Add(b);
|
||||
}
|
||||
panel.Children.Add(_presetPanel);
|
||||
|
||||
panel.Children.Add(Divider());
|
||||
var reset = Btn(T("input.reset"));
|
||||
reset.Click += (_, _) => ResetState();
|
||||
panel.Children.Add(reset);
|
||||
panel.Children.Add(Tb(T("input.dragHint"), 10.5, CInkFaint));
|
||||
|
||||
var scroll = new ScrollViewer
|
||||
{
|
||||
Content = new Border { Padding = new Thickness(14), Child = panel },
|
||||
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
|
||||
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled
|
||||
};
|
||||
return Card(scroll);
|
||||
}
|
||||
|
||||
private Control BuildInputCard(string labelKey, string unitKey,
|
||||
out Border card, out TextBlock label, out TextBlock resultText,
|
||||
out Slider slider, out TextBox box, bool withSlider)
|
||||
{
|
||||
label = Label(T(labelKey), 11.5);
|
||||
resultText = Tb("—", 15, CInk, FontWeight.SemiBold);
|
||||
var top = new DockPanel();
|
||||
top.Children.Add(resultText);
|
||||
DockPanel.SetDock(resultText, Dock.Right);
|
||||
top.Children.Add(label);
|
||||
|
||||
box = Input("3");
|
||||
box.Width = 110;
|
||||
box.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
|
||||
slider = new Slider
|
||||
{
|
||||
Minimum = 0.1,
|
||||
Maximum = 20,
|
||||
Value = 3,
|
||||
TickFrequency = 0.5,
|
||||
IsSnapToTickEnabled = false,
|
||||
Foreground = B(CAccent),
|
||||
Margin = new Thickness(0, 2, 0, 0)
|
||||
};
|
||||
|
||||
var stack = V(6);
|
||||
stack.Children.Add(top);
|
||||
stack.Children.Add(box);
|
||||
if (withSlider) stack.Children.Add(slider);
|
||||
|
||||
card = new Border
|
||||
{
|
||||
Background = B(CSurfaceAlt),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(10),
|
||||
Padding = new Thickness(12),
|
||||
Child = stack
|
||||
};
|
||||
return card;
|
||||
}
|
||||
|
||||
private Control BuildRealCard()
|
||||
{
|
||||
_realCardLabel = Label(T("input.real"), 11.5);
|
||||
_realResultText = Tb("—", 15, CInk, FontWeight.SemiBold);
|
||||
var top = new DockPanel();
|
||||
top.Children.Add(_realResultText);
|
||||
DockPanel.SetDock(_realResultText, Dock.Right);
|
||||
top.Children.Add(_realCardLabel);
|
||||
|
||||
_realBox = Input("3");
|
||||
_unitBox = new ComboBox
|
||||
{
|
||||
FontSize = 12.5,
|
||||
Background = B(CSurface),
|
||||
Foreground = B(CInk),
|
||||
BorderBrush = B(CLineStrong),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(9),
|
||||
Padding = new Thickness(10, 6),
|
||||
MinHeight = 34,
|
||||
Width = 92,
|
||||
ItemsSource = new[] { "unit.km", "unit.m", "unit.cm" },
|
||||
SelectedIndex = 0
|
||||
};
|
||||
// 显示本地化单位名
|
||||
_unitBox.SelectionChanged += (_, _) => { SyncUnitDisplay(); RefreshAll(syncInputs: true); };
|
||||
|
||||
var row = H(8);
|
||||
row.Children.Add(_realBox);
|
||||
row.Children.Add(_unitBox);
|
||||
|
||||
var stack = V(6);
|
||||
stack.Children.Add(top);
|
||||
stack.Children.Add(row);
|
||||
|
||||
_realCard = new Border
|
||||
{
|
||||
Background = B(CSurfaceAlt),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(10),
|
||||
Padding = new Thickness(12),
|
||||
Child = stack
|
||||
};
|
||||
return _realCard;
|
||||
}
|
||||
|
||||
private Control BuildScaleCard()
|
||||
{
|
||||
_scaleCardLabel = Label(T("input.scale"), 11.5);
|
||||
_scaleResultText = Tb("—", 15, CInk, FontWeight.SemiBold);
|
||||
var top = new DockPanel();
|
||||
top.Children.Add(_scaleResultText);
|
||||
DockPanel.SetDock(_scaleResultText, Dock.Right);
|
||||
top.Children.Add(_scaleCardLabel);
|
||||
|
||||
_nBox = Input("100000");
|
||||
var prefix = Tb(T("input.nPrefix"), 13, CInkDim, FontWeight.SemiBold);
|
||||
var row = H(8);
|
||||
row.Children.Add(prefix);
|
||||
row.Children.Add(_nBox);
|
||||
|
||||
var stack = V(6);
|
||||
stack.Children.Add(top);
|
||||
stack.Children.Add(row);
|
||||
|
||||
_scaleCard = new Border
|
||||
{
|
||||
Background = B(CSurfaceAlt),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(10),
|
||||
Padding = new Thickness(12),
|
||||
Child = stack
|
||||
};
|
||||
return _scaleCard;
|
||||
}
|
||||
|
||||
private Control BuildMiddle()
|
||||
{
|
||||
_ruler = new ScaleRuler
|
||||
{
|
||||
Data = _data,
|
||||
State = _state,
|
||||
T = T,
|
||||
Height = 470,
|
||||
VerticalAlignment = VerticalAlignment.Top
|
||||
};
|
||||
var holder = new Border
|
||||
{
|
||||
Background = B(CSurface),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(14),
|
||||
Padding = new Thickness(6),
|
||||
Child = _ruler
|
||||
};
|
||||
return holder;
|
||||
}
|
||||
|
||||
private Control BuildRight()
|
||||
{
|
||||
var panel = V(10);
|
||||
|
||||
// 三种写法
|
||||
panel.Children.Add(SectionTitle(T("forms.title")));
|
||||
_digitalText = Tb("", 16, CAccentDark, FontWeight.SemiBold);
|
||||
_verbalText = Tb("", 12, CInk);
|
||||
_segmentText = Tb("", 12, CInk);
|
||||
var forms = V(8);
|
||||
forms.Children.Add(FormRow(T("forms.digital"), _digitalText));
|
||||
forms.Children.Add(FormRow(T("forms.verbal"), _verbalText));
|
||||
forms.Children.Add(FormRow(T("forms.segment"), _segmentText));
|
||||
panel.Children.Add(forms);
|
||||
|
||||
panel.Children.Add(Divider());
|
||||
|
||||
// 解题步骤
|
||||
_stepsToggle = Btn(T("steps.show"));
|
||||
_stepsToggle.Click += (_, _) => ToggleSteps();
|
||||
var sh = new DockPanel();
|
||||
DockPanel.SetDock(_stepsToggle, Dock.Right);
|
||||
sh.Children.Add(_stepsToggle);
|
||||
sh.Children.Add(SectionTitle(T("steps.title")));
|
||||
panel.Children.Add(sh);
|
||||
_stepsPanel = V(4);
|
||||
_stepsPanel.IsVisible = false;
|
||||
panel.Children.Add(_stepsPanel);
|
||||
|
||||
panel.Children.Add(Divider());
|
||||
|
||||
// 判读
|
||||
panel.Children.Add(SectionTitle(T("grade.title")));
|
||||
_gradeChip = Chip("", CAccentSoft, CAccentDark);
|
||||
_gradeChip.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
_gradeTitle = Tb("", 13, CInk, FontWeight.SemiBold);
|
||||
_gradeNote = Tb("", 11.5, CInkDim);
|
||||
_gradeRule = Tb(T("grade.rule"), 11, CAccentDark, FontWeight.SemiBold);
|
||||
var gradeBox = V(6);
|
||||
gradeBox.Children.Add(_gradeChip);
|
||||
gradeBox.Children.Add(_gradeNote);
|
||||
gradeBox.Children.Add(_gradeRule);
|
||||
panel.Children.Add(gradeBox);
|
||||
|
||||
panel.Children.Add(Divider());
|
||||
|
||||
// 易错点
|
||||
panel.Children.Add(SectionTitle(T("pit.title")));
|
||||
var pit = V(4);
|
||||
pit.Children.Add(Tb(T("pit.1"), 11, CInkDim));
|
||||
pit.Children.Add(Tb(T("pit.2"), 11, CInkDim));
|
||||
pit.Children.Add(Tb(T("pit.3"), 11, CInkDim));
|
||||
panel.Children.Add(pit);
|
||||
|
||||
var scroll = new ScrollViewer
|
||||
{
|
||||
Content = new Border { Padding = new Thickness(14), Child = panel },
|
||||
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
|
||||
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled
|
||||
};
|
||||
return Card(scroll);
|
||||
}
|
||||
|
||||
private Control FormRow(string name, TextBlock value)
|
||||
{
|
||||
var g = new Grid();
|
||||
g.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(62)));
|
||||
g.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
|
||||
var n = Tb(name, 11, CInkFaint);
|
||||
Grid.SetColumn(n, 0);
|
||||
g.Children.Add(n);
|
||||
Grid.SetColumn(value, 1);
|
||||
g.Children.Add(value);
|
||||
return g;
|
||||
}
|
||||
|
||||
private Control SectionTitle(string text)
|
||||
=> Tb(text, 12.5, CInk, FontWeight.SemiBold);
|
||||
|
||||
private Control BuildBottom()
|
||||
{
|
||||
var grid = new Grid { Margin = new Thickness(16, 4, 16, 12) };
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(12)));
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(1.4, GridUnitType.Star)));
|
||||
|
||||
// ── 实地尺
|
||||
var field = V(8);
|
||||
field.Children.Add(SectionTitle(T("field.title")));
|
||||
field.Children.Add(Tb(T("field.hint"), 10.5, CInkFaint));
|
||||
_landmarkBox = new ComboBox
|
||||
{
|
||||
FontSize = 12.5,
|
||||
Background = B(CSurface),
|
||||
Foreground = B(CInk),
|
||||
BorderBrush = B(CLineStrong),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(9),
|
||||
Padding = new Thickness(10, 6),
|
||||
MinHeight = 34,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch
|
||||
};
|
||||
foreach (var lm in _data.Landmarks) _landmarkBox.Items!.Add(T(lm.NameKey));
|
||||
_landmarkBox.SelectedIndex = 0;
|
||||
_landmarkBox.SelectionChanged += (_, _) => RefreshField();
|
||||
field.Children.Add(_landmarkBox);
|
||||
_landmarkResult = Tb("", 14, CSuccess, FontWeight.SemiBold);
|
||||
field.Children.Add(_landmarkResult);
|
||||
var fieldCard = Card(field);
|
||||
Grid.SetColumn(fieldCard, 0);
|
||||
grid.Children.Add(fieldCard);
|
||||
|
||||
// ── 练习
|
||||
var quiz = V(8);
|
||||
_quizStreak = Tb(TF("quiz.streak", 0), 11, CAccentDark, FontWeight.SemiBold);
|
||||
var qh = new DockPanel();
|
||||
DockPanel.SetDock(_quizStreak, Dock.Right);
|
||||
qh.Children.Add(_quizStreak);
|
||||
qh.Children.Add(SectionTitle(T("quiz.title")));
|
||||
quiz.Children.Add(qh);
|
||||
|
||||
_quizPrompt = Tb("", 12, CInk);
|
||||
quiz.Children.Add(_quizPrompt);
|
||||
_quizUnit = Tb("", 10.5, CInkFaint);
|
||||
quiz.Children.Add(_quizUnit);
|
||||
|
||||
var qrow = H(8);
|
||||
_quizInput = Input("");
|
||||
_quizInput.Width = 140;
|
||||
_quizCheck = Btn(T("quiz.check"), primary: true);
|
||||
_quizNext = Btn(T("quiz.next"));
|
||||
_quizCheck.Click += (_, _) => CheckQuiz();
|
||||
_quizNext.Click += (_, _) => NewQuiz();
|
||||
qrow.Children.Add(_quizInput);
|
||||
qrow.Children.Add(_quizCheck);
|
||||
qrow.Children.Add(_quizNext);
|
||||
quiz.Children.Add(qrow);
|
||||
|
||||
_quizFeedback = Tb("", 12, CInkDim, FontWeight.SemiBold);
|
||||
quiz.Children.Add(_quizFeedback);
|
||||
_quizSteps = V(2);
|
||||
quiz.Children.Add(_quizSteps);
|
||||
|
||||
var quizCard = Card(quiz);
|
||||
Grid.SetColumn(quizCard, 2);
|
||||
grid.Children.Add(quizCard);
|
||||
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
public sealed partial class ScaleCalculatorView
|
||||
{
|
||||
private readonly ScaleState _state = new();
|
||||
private bool _suppress;
|
||||
private string _realUnit = "unit.km";
|
||||
private QuizItem _quiz = new();
|
||||
private int _streak;
|
||||
private bool _quizAnswered;
|
||||
|
||||
private void WireEvents()
|
||||
{
|
||||
_btnReal.Click += (_, _) => SetMode(SolveMode.Real);
|
||||
_btnMap.Click += (_, _) => SetMode(SolveMode.Map);
|
||||
_btnScale.Click += (_, _) => SetMode(SolveMode.Scale);
|
||||
|
||||
_mapSlider.ValueChanged += (_, e) =>
|
||||
{
|
||||
if (_suppress) return;
|
||||
_state.MapCm = e.NewValue;
|
||||
_suppress = true;
|
||||
_mapBox.Text = ScaleMath.Num(_state.MapCm);
|
||||
_suppress = false;
|
||||
RefreshAll(syncInputs: false);
|
||||
};
|
||||
_mapBox.TextChanged += (_, _) => OnTextInput(_mapBox, v => { _state.MapCm = v; }, v =>
|
||||
{
|
||||
_suppress = true;
|
||||
_mapSlider.Value = Math.Clamp(v, 0.1, 20);
|
||||
_suppress = false;
|
||||
});
|
||||
_realBox.TextChanged += (_, _) =>
|
||||
{
|
||||
if (_suppress) return;
|
||||
if (ScaleMath.TryParseReal(_realBox.Text ?? "", _realUnit, out var cm))
|
||||
{
|
||||
_state.RealCm = cm;
|
||||
RefreshAll(syncInputs: false);
|
||||
}
|
||||
};
|
||||
_nBox.TextChanged += (_, _) => OnTextInput(_nBox, v => { _state.N = v; }, null);
|
||||
|
||||
_quizInput.KeyDown += (_, e) =>
|
||||
{
|
||||
if (e.Key == Avalonia.Input.Key.Enter) CheckQuiz();
|
||||
};
|
||||
}
|
||||
|
||||
private void OnTextInput(TextBox box, Action<double> apply, Action<double>? extra)
|
||||
{
|
||||
if (_suppress) return;
|
||||
if (!double.TryParse((box.Text ?? "").Replace(",", "").Trim(), NumberStyles.Float,
|
||||
CultureInfo.InvariantCulture, out var v) || v < 0 || double.IsNaN(v) || double.IsInfinity(v))
|
||||
return;
|
||||
apply(v);
|
||||
extra?.Invoke(v);
|
||||
RefreshAll(syncInputs: false);
|
||||
}
|
||||
|
||||
private void SetMode(SolveMode mode, bool initial = false)
|
||||
{
|
||||
_state.Mode = mode;
|
||||
if (!initial) ScaleMath.Solve(_state);
|
||||
UpdateModeButtons();
|
||||
RefreshAll(syncInputs: true);
|
||||
}
|
||||
|
||||
private void UpdateModeButtons()
|
||||
{
|
||||
StyleMode(_btnReal, _state.Mode == SolveMode.Real);
|
||||
StyleMode(_btnMap, _state.Mode == SolveMode.Map);
|
||||
StyleMode(_btnScale, _state.Mode == SolveMode.Scale);
|
||||
|
||||
// 结果卡高亮:待求的那一张变蓝
|
||||
StyleCard(_mapCard, _state.Mode == SolveMode.Map);
|
||||
StyleCard(_realCard, _state.Mode == SolveMode.Real);
|
||||
StyleCard(_scaleCard, _state.Mode == SolveMode.Scale);
|
||||
|
||||
_mapResultText.Foreground = B(_state.Mode == SolveMode.Map ? CAccentDark : CInk);
|
||||
_realResultText.Foreground = B(_state.Mode == SolveMode.Real ? CAccentDark : CInk);
|
||||
_scaleResultText.Foreground = B(_state.Mode == SolveMode.Scale ? CAccentDark : CInk);
|
||||
}
|
||||
|
||||
private void StyleMode(Button b, bool active)
|
||||
{
|
||||
b.Background = B(active ? CAccent : CSurface);
|
||||
b.BorderBrush = B(active ? CAccent : CLineStrong);
|
||||
b.BorderThickness = new Thickness(active ? 0 : 1);
|
||||
if (b.Content is TextBlock t)
|
||||
t.Foreground = B(active ? Colors.White : CInk);
|
||||
}
|
||||
|
||||
private static void StyleCard(Border card, bool active)
|
||||
{
|
||||
card.Background = B(active ? Color.Parse("#F0F5FF") : Color.Parse("#F7F9FE"));
|
||||
card.BorderBrush = B(active ? Color.Parse("#2F6BFF") : Color.Parse("#E3E8F3"));
|
||||
card.BorderThickness = new Thickness(active ? 1.5 : 1);
|
||||
}
|
||||
|
||||
private void ApplyPreset(PresetItem p)
|
||||
{
|
||||
_state.N = p.N;
|
||||
ScaleMath.Solve(_state);
|
||||
RefreshAll(syncInputs: true);
|
||||
}
|
||||
|
||||
private void ResetState()
|
||||
{
|
||||
_state.MapCm = 3.0;
|
||||
_state.N = 100000.0;
|
||||
ScaleMath.Solve(_state);
|
||||
RefreshAll(syncInputs: true);
|
||||
}
|
||||
|
||||
private void SyncUnitDisplay()
|
||||
{
|
||||
// ComboBox 的 ItemsSource 是 "unit.km" 等键;这里只记录选择,显示由下拉项本身处理
|
||||
var idx = _unitBox.SelectedIndex;
|
||||
_realUnit = idx switch { 1 => "unit.m", 2 => "unit.cm", _ => "unit.km" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全量刷新。syncInputs=true 时把三个输入框/滑块同步成 state(切换模式、预设、重置后需要);
|
||||
/// false 时保留用户正在输入的文本(避免光标跳动)。
|
||||
/// </summary>
|
||||
private void RefreshAll(bool syncInputs)
|
||||
{
|
||||
ScaleMath.Solve(_state);
|
||||
|
||||
if (syncInputs)
|
||||
{
|
||||
_suppress = true;
|
||||
_mapBox.Text = ScaleMath.Num(_state.MapCm);
|
||||
_mapSlider.Value = Math.Clamp(_state.MapCm, 0.1, 20);
|
||||
_nBox.Text = ScaleMath.FormatN(_state.N);
|
||||
var (rv, _) = ScaleMath.HumanReal(_state.RealCm);
|
||||
// 用当前选择的单位显示
|
||||
_realBox.Text = ScaleMath.Num(ScaleMath.ToUnit(_state.RealCm, _realUnit));
|
||||
_suppress = false;
|
||||
}
|
||||
|
||||
UpdateModeButtons();
|
||||
|
||||
// 结果卡读数(三张都显示当前值,蓝色那张是"答案")
|
||||
_mapResultText.Text = ScaleMath.Num(_state.MapCm) + " " + T("unit.cm");
|
||||
var (rv2, rkey2) = ScaleMath.HumanReal(_state.RealCm);
|
||||
_realResultText.Text = ScaleMath.Num(rv2) + " " + T(rkey2);
|
||||
_scaleResultText.Text = "1 : " + ScaleMath.FormatN(_state.N);
|
||||
|
||||
// 三种写法
|
||||
_digitalText.Text = ScaleMath.Digital(_state.N);
|
||||
_verbalText.Text = TF("forms.verbalTpl", ScaleMath.VerbalDistance(_state.N));
|
||||
_segmentText.Text = TF("forms.segmentDesc",
|
||||
ScaleMath.SegmentLabel(_ruler.Segment.MetersPerDiv), _ruler.Segment.DivCount);
|
||||
|
||||
// 判读
|
||||
var gradeKey = ScaleMath.GradeKey(_state.N);
|
||||
_gradeTitle.Text = T(gradeKey);
|
||||
_gradeChip.Child = Tb(T(gradeKey), 11, CAccentDark, FontWeight.SemiBold);
|
||||
_gradeNote.Text = T(gradeKey + "Note");
|
||||
_gradeRule.Text = T("grade.rule");
|
||||
|
||||
// 标尺
|
||||
_ruler.State = _state.Clone();
|
||||
_ruler.Refresh();
|
||||
_segmentText.Text = TF("forms.segmentDesc",
|
||||
ScaleMath.SegmentLabel(_ruler.Segment.MetersPerDiv), _ruler.Segment.DivCount);
|
||||
|
||||
BuildSteps();
|
||||
RefreshField();
|
||||
}
|
||||
|
||||
private void BuildSteps()
|
||||
{
|
||||
_stepsPanel.Children.Clear();
|
||||
var map = ScaleMath.Num(_state.MapCm);
|
||||
var realCm = ScaleMath.Num(_state.RealCm);
|
||||
var n = ScaleMath.FormatN(_state.N);
|
||||
var (rv, rk) = ScaleMath.HumanReal(_state.RealCm);
|
||||
var result = _state.Mode switch
|
||||
{
|
||||
SolveMode.Real => ScaleMath.Num(rv) + " " + T(rk),
|
||||
SolveMode.Map => ScaleMath.Num(_state.MapCm) + " " + T("unit.cm"),
|
||||
_ => "1 : " + ScaleMath.FormatN(_state.N)
|
||||
};
|
||||
var formulaKey = _state.Mode switch
|
||||
{
|
||||
SolveMode.Real => "steps.2.formulaReal",
|
||||
SolveMode.Map => "steps.2.formulaMap",
|
||||
_ => "steps.2.formulaScale"
|
||||
};
|
||||
var substitute = _state.Mode switch
|
||||
{
|
||||
SolveMode.Real => map + " cm × " + n,
|
||||
SolveMode.Map => realCm + " cm ÷ " + n,
|
||||
_ => realCm + " cm ÷ " + map + " cm"
|
||||
};
|
||||
|
||||
var lines = new System.Collections.Generic.List<string>
|
||||
{
|
||||
T("steps.1.unify"),
|
||||
T(formulaKey),
|
||||
TF("steps.3.substitute", substitute),
|
||||
TF("steps.4.result", result),
|
||||
T("steps.5.convert")
|
||||
};
|
||||
var i = 0;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
_stepsPanel.Children.Add(Tb(line, 11, i == 3 ? CAccentDark : CInkDim,
|
||||
i == 3 ? FontWeight.SemiBold : null));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private void ToggleSteps()
|
||||
{
|
||||
_stepsOpen = !_stepsOpen;
|
||||
_stepsPanel.IsVisible = _stepsOpen;
|
||||
_stepsToggle.Content = Tb(T(_stepsOpen ? "steps.hide" : "steps.show"), 12.5, CInk, FontWeight.SemiBold);
|
||||
}
|
||||
|
||||
private void RefreshField()
|
||||
{
|
||||
var idx = _landmarkBox.SelectedIndex;
|
||||
if (idx < 0 || idx >= _data.Landmarks.Count) return;
|
||||
var meters = _data.Landmarks[idx].Meters;
|
||||
var realCm = meters * ScaleMath.CmPerM;
|
||||
var mapCm = ScaleMath.RealToMapCm(realCm, _state.N);
|
||||
var px = ScaleMath.CmToPx(mapCm, _ruler.PxPerCm);
|
||||
// mapCm 是厘米,按 HumanReal 换算成"图上长度"的合适单位
|
||||
var (mv, mk) = ScaleMath.HumanReal(mapCm);
|
||||
|
||||
string text;
|
||||
if (px > _ruler.MaxBarWidth)
|
||||
text = TF("field.mapLength", ScaleMath.Num(mv) + " " + T(mk)) + " · " + T("field.tooLong");
|
||||
else if (px < 1)
|
||||
text = TF("field.mapLength", ScaleMath.Num(mv) + " " + T(mk)) + " · " + T("field.tooShort");
|
||||
else
|
||||
text = TF("field.mapLength", ScaleMath.Num(mv) + " " + T(mk));
|
||||
_landmarkResult.Text = text;
|
||||
_landmarkResult.Foreground = B(px > _ruler.MaxBarWidth ? CDanger : px < 1 ? CWarn : CSuccess);
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────── 练习
|
||||
|
||||
private void NewQuiz()
|
||||
{
|
||||
_quiz = ScaleMath.GenerateQuiz(_rnd);
|
||||
_quizAnswered = false;
|
||||
_quizInput.Text = "";
|
||||
_quizFeedback.Text = "";
|
||||
_quizSteps.Children.Clear();
|
||||
_quizPrompt.Text = TF(_quiz.PromptKey, _quiz.PromptArgs);
|
||||
var unitText = _quiz.AnswerUnitKey == "unit.none" ? T("unit.none") : T(_quiz.AnswerUnitKey);
|
||||
_quizUnit.Text = TF("quiz.unit", unitText);
|
||||
_quizStreak.Text = TF("quiz.streak", _streak);
|
||||
}
|
||||
|
||||
private void CheckQuiz()
|
||||
{
|
||||
if (_quizAnswered) return;
|
||||
if (!double.TryParse((_quizInput.Text ?? "").Replace(",", "").Trim(), NumberStyles.Float,
|
||||
CultureInfo.InvariantCulture, out var v))
|
||||
{
|
||||
_quizFeedback.Text = T("quiz.wrong");
|
||||
_quizFeedback.Foreground = B(CDanger);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ScaleMath.CheckAnswer(_quiz, v))
|
||||
{
|
||||
_streak++;
|
||||
_quizFeedback.Text = T("quiz.correct");
|
||||
_quizFeedback.Foreground = B(CSuccess);
|
||||
}
|
||||
else
|
||||
{
|
||||
_streak = 0;
|
||||
_quizFeedback.Text = T("quiz.wrong");
|
||||
_quizFeedback.Foreground = B(CDanger);
|
||||
ShowQuizSteps();
|
||||
}
|
||||
_quizAnswered = true;
|
||||
_quizStreak.Text = TF("quiz.streak", _streak);
|
||||
}
|
||||
|
||||
private void ShowQuizSteps()
|
||||
{
|
||||
_quizSteps.Children.Clear();
|
||||
_quizSteps.Children.Add(Tb(T("quiz.showSteps"), 10.5, CInkFaint, FontWeight.SemiBold));
|
||||
foreach (var key in _quiz.StepKeys)
|
||||
{
|
||||
var line = T(key);
|
||||
if (line.Contains("{0}")) line = TF(key, ScaleMath.Num(_quiz.Answer) + " " +
|
||||
(_quiz.AnswerUnitKey == "unit.none" ? "" : T(_quiz.AnswerUnitKey)));
|
||||
_quizSteps.Children.Add(Tb(line, 10.5, CInkDim));
|
||||
}
|
||||
_quizSteps.Children.Add(Tb(TF("steps.4.result",
|
||||
ScaleMath.Num(_quiz.Answer) + " " +
|
||||
(_quiz.AnswerUnitKey == "unit.none" ? "" : T(_quiz.AnswerUnitKey))),
|
||||
10.5, CAccentDark, FontWeight.SemiBold));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>
|
||||
/// 比例尺计算器主界面。三栏(已知量 / 可视化标尺 / 三种写法与判读)+ 底部实地尺与练习。
|
||||
/// 全部用显式画刷(与启动器 AppTheme 同色),不依赖 DynamicResource —— 模型 dll 在
|
||||
/// 独立 ALC 里加载,headless 生成封面时也没有 Application 样式。
|
||||
/// </summary>
|
||||
public sealed partial class ScaleCalculatorView : UserControl
|
||||
{
|
||||
// 调色(与 src/main/dotnet/EarthModelStudio/Styles/AppTheme.axaml 一致)
|
||||
internal static readonly Color CAppBg = Color.Parse("#F4F6FB");
|
||||
internal static readonly Color CSurface = Color.Parse("#FFFFFF");
|
||||
internal static readonly Color CSurfaceAlt = Color.Parse("#F7F9FE");
|
||||
internal static readonly Color CLine = Color.Parse("#E3E8F3");
|
||||
internal static readonly Color CLineStrong = Color.Parse("#D2DAEA");
|
||||
internal static readonly Color CInk = Color.Parse("#111A2E");
|
||||
internal static readonly Color CInkDim = Color.Parse("#5B6A86");
|
||||
internal static readonly Color CInkFaint = Color.Parse("#8A97B1");
|
||||
internal static readonly Color CAccent = Color.Parse("#2F6BFF");
|
||||
internal static readonly Color CAccentDark = Color.Parse("#1F4FD0");
|
||||
internal static readonly Color CAccentSoft = Color.Parse("#EAF1FF");
|
||||
internal static readonly Color CSuccess = Color.Parse("#1F9D6B");
|
||||
internal static readonly Color CDanger = Color.Parse("#D64545");
|
||||
internal static readonly Color CWarn = Color.Parse("#D98613");
|
||||
internal static readonly Color CHero = Color.Parse("#12203C");
|
||||
|
||||
internal static SolidColorBrush B(Color c) => new(c);
|
||||
internal static SolidColorBrush Brush(string hex) => new(Color.Parse(hex));
|
||||
|
||||
private readonly ScaleData _data;
|
||||
private readonly Dictionary<string, string> _texts;
|
||||
private readonly string _language;
|
||||
private readonly Random _rnd = new();
|
||||
|
||||
public string T(string key)
|
||||
=> _texts.TryGetValue(key, out var v) ? v
|
||||
: DefaultTexts.Zh.TryGetValue(key, out var f) ? f : key;
|
||||
|
||||
public string TF(string key, params object[] args)
|
||||
=> string.Format(CultureInfo.InvariantCulture, T(key), args);
|
||||
|
||||
public ScaleCalculatorView(ScaleData data, Dictionary<string, string> texts, string language)
|
||||
{
|
||||
_data = data;
|
||||
_texts = texts;
|
||||
_language = language;
|
||||
|
||||
BuildLayout();
|
||||
WireEvents();
|
||||
SetMode(SolveMode.Real, initial: true);
|
||||
NewQuiz();
|
||||
RefreshAll(syncInputs: true);
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────── 小工具
|
||||
|
||||
internal static Border Card(Control content, double radius = 14)
|
||||
=> new()
|
||||
{
|
||||
Background = B(CSurface),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(radius),
|
||||
Padding = new Thickness(14),
|
||||
Child = content
|
||||
};
|
||||
|
||||
internal static TextBlock Tb(string text, double size, Color color, FontWeight? weight = null,
|
||||
TextAlignment align = TextAlignment.Left)
|
||||
=> new()
|
||||
{
|
||||
Text = text,
|
||||
FontSize = size,
|
||||
Foreground = B(color),
|
||||
FontWeight = weight ?? FontWeight.Normal,
|
||||
TextAlignment = align,
|
||||
TextWrapping = TextWrapping.Wrap
|
||||
};
|
||||
|
||||
internal static TextBlock Label(string text, double size = 12, Color? color = null)
|
||||
=> Tb(text, size, color ?? CInkDim);
|
||||
|
||||
internal static TextBlock H3(string text) => Tb(text, 13.5, CInk, FontWeight.SemiBold);
|
||||
|
||||
internal static Border Chip(string text, Color bg, Color fg, double radius = 8)
|
||||
=> new()
|
||||
{
|
||||
Background = B(bg),
|
||||
CornerRadius = new CornerRadius(radius),
|
||||
Padding = new Thickness(10, 4),
|
||||
Child = Tb(text, 11, fg, FontWeight.SemiBold)
|
||||
};
|
||||
|
||||
internal static Button Btn(string text, bool primary = false)
|
||||
=> new()
|
||||
{
|
||||
Content = Tb(text, 12.5, primary ? Colors.White : CInk, FontWeight.SemiBold),
|
||||
Background = B(primary ? CAccent : CSurface),
|
||||
BorderBrush = B(primary ? CAccent : CLineStrong),
|
||||
BorderThickness = new Thickness(primary ? 0 : 1),
|
||||
CornerRadius = new CornerRadius(9),
|
||||
Padding = new Thickness(12, 7),
|
||||
HorizontalContentAlignment = HorizontalAlignment.Center,
|
||||
VerticalContentAlignment = VerticalAlignment.Center,
|
||||
Cursor = new Cursor(StandardCursorType.Hand)
|
||||
};
|
||||
|
||||
internal static TextBox Input(string text = "")
|
||||
=> new()
|
||||
{
|
||||
Text = text,
|
||||
FontSize = 12.5,
|
||||
Background = B(CSurface),
|
||||
Foreground = B(CInk),
|
||||
BorderBrush = B(CLineStrong),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(9),
|
||||
Padding = new Thickness(10, 7),
|
||||
VerticalContentAlignment = VerticalAlignment.Center,
|
||||
MinHeight = 34
|
||||
};
|
||||
|
||||
internal static Border Divider(double h = 1)
|
||||
=> new() { Background = B(CLine), Height = h, Margin = new Thickness(0, 10, 0, 10) };
|
||||
|
||||
internal static StackPanel V(double spacing = 8)
|
||||
=> new() { Orientation = Orientation.Vertical, Spacing = spacing };
|
||||
|
||||
internal static StackPanel H(double spacing = 8)
|
||||
=> new() { Orientation = Orientation.Horizontal, Spacing = spacing, VerticalAlignment = VerticalAlignment.Center };
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
public sealed class PresetItem
|
||||
{
|
||||
public double N { get; set; }
|
||||
public string NameKey { get; set; } = "";
|
||||
public string NoteKey { get; set; } = "";
|
||||
}
|
||||
|
||||
public sealed class SpectrumItem
|
||||
{
|
||||
public double N { get; set; }
|
||||
public string LabelKey { get; set; } = "";
|
||||
public string ScopeKey { get; set; } = "";
|
||||
}
|
||||
|
||||
public sealed class LandmarkItem
|
||||
{
|
||||
public string NameKey { get; set; } = "";
|
||||
/// <summary>实地距离(米)。</summary>
|
||||
public double Meters { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ScaleData
|
||||
{
|
||||
public List<PresetItem> Presets { get; set; } = new();
|
||||
public List<SpectrumItem> Spectrum { get; set; } = new();
|
||||
public List<LandmarkItem> Landmarks { get; set; } = new();
|
||||
|
||||
private static readonly JsonSerializerOptions Opts = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
AllowTrailingCommas = true,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||
};
|
||||
|
||||
public static ScaleData Load(string dataDir)
|
||||
{
|
||||
var path = Path.Combine(dataDir, "scales.json");
|
||||
if (!File.Exists(path)) return Fallback();
|
||||
try
|
||||
{
|
||||
var d = JsonSerializer.Deserialize<ScaleData>(File.ReadAllText(path), Opts);
|
||||
if (d == null || d.Presets.Count == 0) return Fallback();
|
||||
return d;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"[scale-calculator] 读取 scales.json 失败: {ex.Message}");
|
||||
return Fallback();
|
||||
}
|
||||
}
|
||||
|
||||
private static ScaleData Fallback() => new()
|
||||
{
|
||||
Presets = new List<PresetItem>
|
||||
{
|
||||
new() { N = 10000, NameKey = "preset.campus", NoteKey = "preset.campusNote" },
|
||||
new() { N = 50000, NameKey = "preset.town", NoteKey = "preset.townNote" },
|
||||
new() { N = 250000, NameKey = "preset.city", NoteKey = "preset.cityNote" },
|
||||
new() { N = 1000000, NameKey = "preset.province", NoteKey = "preset.provinceNote" },
|
||||
new() { N = 10000000, NameKey = "preset.china", NoteKey = "preset.chinaNote" },
|
||||
new() { N = 100000000, NameKey = "preset.world", NoteKey = "preset.worldNote" }
|
||||
},
|
||||
Spectrum = new List<SpectrumItem>
|
||||
{
|
||||
new() { N = 10000, LabelKey = "spec.campus", ScopeKey = "spec.campusScope" },
|
||||
new() { N = 250000, LabelKey = "spec.city", ScopeKey = "spec.cityScope" },
|
||||
new() { N = 10000000, LabelKey = "spec.china", ScopeKey = "spec.chinaScope" },
|
||||
new() { N = 100000000, LabelKey = "spec.world", ScopeKey = "spec.worldScope" }
|
||||
},
|
||||
Landmarks = new List<LandmarkItem>
|
||||
{
|
||||
new() { NameKey = "lm.classroom", Meters = 10 },
|
||||
new() { NameKey = "lm.track", Meters = 400 },
|
||||
new() { NameKey = "lm.marathon", Meters = 42195 },
|
||||
new() { NameKey = "lm.beijingShanghai", Meters = 1080000 },
|
||||
new() { NameKey = "lm.equator", Meters = 40000000 }
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>三种求解模式:待求的量。</summary>
|
||||
public enum SolveMode
|
||||
{
|
||||
/// <summary>求实地距离</summary>
|
||||
Real,
|
||||
/// <summary>求图上距离</summary>
|
||||
Map,
|
||||
/// <summary>求比例尺</summary>
|
||||
Scale
|
||||
}
|
||||
|
||||
public enum ScaleGrade { Large, Medium, Small }
|
||||
|
||||
/// <summary>
|
||||
/// 三个量始终同时存在:图上距离(cm)、实地距离(cm)、比例尺分母 N。
|
||||
/// <see cref="Mode"/> 指定哪个是"待求结果",另两个是输入;
|
||||
/// <see cref="ScaleMath.Solve"/> 只由两个输入重算结果量,切换模式时数值保持连续。
|
||||
/// </summary>
|
||||
public sealed class ScaleState
|
||||
{
|
||||
public double MapCm = 3.0;
|
||||
public double RealCm = 300000.0; // 3 km
|
||||
public double N = 100000.0; // 1:100000
|
||||
public SolveMode Mode = SolveMode.Real;
|
||||
|
||||
public ScaleState Clone() => new()
|
||||
{
|
||||
MapCm = MapCm, RealCm = RealCm, N = N, Mode = Mode
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>线段式比例尺换挡结果。</summary>
|
||||
public sealed class SegmentScale
|
||||
{
|
||||
/// <summary>每一格代表的实地距离(米)。</summary>
|
||||
public double MetersPerDiv;
|
||||
/// <summary>每一格在屏幕上的像素宽度。</summary>
|
||||
public double DivPx;
|
||||
/// <summary>绘制的格数。</summary>
|
||||
public int DivCount = 5;
|
||||
}
|
||||
|
||||
/// <summary>一道练习题。</summary>
|
||||
public sealed class QuizItem
|
||||
{
|
||||
public SolveMode Mode;
|
||||
public string PromptKey = "";
|
||||
public object[] PromptArgs = Array.Empty<object>();
|
||||
public double Answer;
|
||||
/// <summary>相对容差。</summary>
|
||||
public double Tolerance = 0.02;
|
||||
public string AnswerUnitKey = "unit.cm";
|
||||
public string[] StepKeys = Array.Empty<string>();
|
||||
}
|
||||
|
||||
public static class ScaleMath
|
||||
{
|
||||
public const double CmPerM = 100.0;
|
||||
public const double CmPerKm = 100000.0;
|
||||
|
||||
/// <summary>由两个输入量重算"待求"量。</summary>
|
||||
public static void Solve(ScaleState s)
|
||||
{
|
||||
// 先做一次合法性收敛,避免 0 / 负数进入除法
|
||||
if (s.MapCm < 0) s.MapCm = 0;
|
||||
if (s.RealCm < 0) s.RealCm = 0;
|
||||
if (s.N < 1) s.N = 1;
|
||||
|
||||
switch (s.Mode)
|
||||
{
|
||||
case SolveMode.Real:
|
||||
s.RealCm = s.MapCm * s.N;
|
||||
break;
|
||||
case SolveMode.Map:
|
||||
s.MapCm = s.N > 0 ? s.RealCm / s.N : 0;
|
||||
break;
|
||||
case SolveMode.Scale:
|
||||
s.N = s.MapCm > 1e-9 ? s.RealCm / s.MapCm : 1;
|
||||
if (s.N < 1) s.N = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>厘米 → 更合适的单位与数值。</summary>
|
||||
public static (double value, string unitKey) HumanReal(double cm)
|
||||
{
|
||||
if (cm < 1) return (cm, "unit.cm");
|
||||
if (cm < CmPerM) return (cm, "unit.cm");
|
||||
if (cm < CmPerKm) return (cm / CmPerM, "unit.m");
|
||||
return (cm / CmPerKm, "unit.km");
|
||||
}
|
||||
|
||||
public static string Num(double v)
|
||||
{
|
||||
if (double.IsNaN(v) || double.IsInfinity(v)) return "—";
|
||||
var a = Math.Abs(v);
|
||||
if (a >= 1000) return v.ToString("#,##0", CultureInfo.InvariantCulture);
|
||||
if (a >= 10) return v.ToString("0.#", CultureInfo.InvariantCulture);
|
||||
if (a >= 1) return v.ToString("0.##", CultureInfo.InvariantCulture);
|
||||
if (a >= 0.01) return v.ToString("0.###", CultureInfo.InvariantCulture);
|
||||
if (a == 0) return "0";
|
||||
return v.ToString("0.####", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static string FormatRealCm(double cm)
|
||||
{
|
||||
var (v, key) = HumanReal(cm);
|
||||
return Num(v) + " " + key;
|
||||
}
|
||||
|
||||
public static string FormatN(double n) => Num(Math.Round(n));
|
||||
|
||||
/// <summary>数字式写法 1:N。</summary>
|
||||
public static string Digital(double n) => "1 : " + FormatN(n);
|
||||
|
||||
/// <summary>"文字式"里那一段实地距离(含单位),由调用方套 i18n 模板。</summary>
|
||||
public static string VerbalDistance(double n)
|
||||
{
|
||||
// 图上 1 厘米代表实地 n 厘米
|
||||
var (v, key) = HumanReal(n);
|
||||
return Num(v) + " " + key;
|
||||
}
|
||||
|
||||
/// <summary>按当前比例尺,把一段实地距离换算成图上厘米。</summary>
|
||||
public static double RealToMapCm(double realCm, double n) => n > 0 ? realCm / n : 0;
|
||||
|
||||
/// <summary>把图上厘米换算为屏幕像素。</summary>
|
||||
public static double CmToPx(double cm, double pxPerCm) => cm * pxPerCm;
|
||||
|
||||
/// <summary>把一段实地距离按当前比例尺换算成屏幕像素。</summary>
|
||||
public static double RealToMapPx(double realCm, double n, double pxPerCm)
|
||||
=> RealToMapCm(realCm, n) * pxPerCm;
|
||||
|
||||
/// <summary>
|
||||
/// 线段式比例尺自动换挡:在 1/2/5×10^k(米)里挑一个,
|
||||
/// 使每格画出来最接近 targetPx,保证任何比例尺都画得下、刻度又是整数。
|
||||
/// </summary>
|
||||
public static SegmentScale PickSegment(double n, double pxPerCm, double targetPx)
|
||||
{
|
||||
if (n < 1) n = 1;
|
||||
if (pxPerCm < 1) pxPerCm = 1;
|
||||
// 每格实地米数(理想值):px = m*100/n*pxPerCm
|
||||
var idealMeters = targetPx * n / (100.0 * pxPerCm);
|
||||
if (idealMeters <= 0) idealMeters = 1;
|
||||
|
||||
double best = 1;
|
||||
double bestErr = double.MaxValue;
|
||||
var logIdeal = Math.Log10(idealMeters);
|
||||
for (var k = -3; k <= 9; k++)
|
||||
{
|
||||
foreach (var m in new[] { 1.0, 2.0, 5.0 })
|
||||
{
|
||||
var cand = m * Math.Pow(10, k);
|
||||
var err = Math.Abs(Math.Log10(cand) - logIdeal);
|
||||
if (err < bestErr) { bestErr = err; best = cand; }
|
||||
}
|
||||
}
|
||||
|
||||
var divPx = best * 100.0 / n * pxPerCm;
|
||||
// 极端情况下(换挡后仍过长/过短)把格数收敛一下,保证总长在合理区间
|
||||
var count = 5;
|
||||
var total = divPx * count;
|
||||
if (total > targetPx * 9) count = 4;
|
||||
else if (total < targetPx * 2.5) count = 6;
|
||||
|
||||
return new SegmentScale { MetersPerDiv = best, DivPx = divPx, DivCount = count };
|
||||
}
|
||||
|
||||
/// <summary>线段式每格的标注文字(不带 i18n,纯数值+单位)。</summary>
|
||||
public static string SegmentLabel(double meters)
|
||||
{
|
||||
if (meters < 1) return Num(meters * 100) + " cm";
|
||||
if (meters < 1000) return Num(meters) + " m";
|
||||
return Num(meters / 1000) + " km";
|
||||
}
|
||||
|
||||
public static ScaleGrade Grade(double n)
|
||||
{
|
||||
if (n <= 100000) return ScaleGrade.Large;
|
||||
if (n <= 1000000) return ScaleGrade.Medium;
|
||||
return ScaleGrade.Small;
|
||||
}
|
||||
|
||||
public static string GradeKey(double n) => Grade(n) switch
|
||||
{
|
||||
ScaleGrade.Large => "grade.large",
|
||||
ScaleGrade.Medium => "grade.medium",
|
||||
_ => "grade.small"
|
||||
};
|
||||
|
||||
/// <summary>光谱上 0..1 的位置(对数刻度,1:1千 ~ 1:10亿)。</summary>
|
||||
public static double SpectrumPosition(double n)
|
||||
{
|
||||
if (n < 1) n = 1;
|
||||
var p = (Math.Log10(n) - 3.0) / 6.0;
|
||||
return p < 0 ? 0 : p > 1 ? 1 : p;
|
||||
}
|
||||
|
||||
/// <summary>把用户输入的"数值+单位"解析成厘米。</summary>
|
||||
public static bool TryParseReal(string text, string unitKey, out double cm)
|
||||
{
|
||||
cm = 0;
|
||||
if (!double.TryParse(text.Replace(",", "").Trim(), NumberStyles.Float,
|
||||
CultureInfo.InvariantCulture, out var v) || v < 0 || double.IsNaN(v) || double.IsInfinity(v))
|
||||
return false;
|
||||
cm = unitKey switch
|
||||
{
|
||||
"unit.m" => v * CmPerM,
|
||||
"unit.km" => v * CmPerKm,
|
||||
_ => v * 1.0
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>由厘米得到该单位下的数值(用于输入框显示)。</summary>
|
||||
public static double ToUnit(double cm, string unitKey) => unitKey switch
|
||||
{
|
||||
"unit.m" => cm / CmPerM,
|
||||
"unit.km" => cm / CmPerKm,
|
||||
_ => cm
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------ 练习
|
||||
|
||||
public static QuizItem GenerateQuiz(Random rnd)
|
||||
{
|
||||
var mode = (SolveMode)rnd.Next(0, 3);
|
||||
// 用整齐的数,方便心算
|
||||
var mapCm = new[] { 1.0, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 12.0 }[rnd.Next(0, 10)];
|
||||
var ns = new[] { 1000.0, 5000.0, 10000.0, 25000.0, 50000.0, 100000.0, 500000.0, 1000000.0, 5000000.0, 10000000.0 };
|
||||
var n = ns[rnd.Next(0, ns.Length)];
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case SolveMode.Real:
|
||||
{
|
||||
var realCm = mapCm * n;
|
||||
var (v, key) = HumanReal(realCm);
|
||||
return new QuizItem
|
||||
{
|
||||
Mode = SolveMode.Real,
|
||||
PromptKey = "quiz.promptReal",
|
||||
PromptArgs = new object[] { FormatN(n), Num(mapCm) },
|
||||
Answer = v,
|
||||
AnswerUnitKey = key,
|
||||
StepKeys = new[] { "steps.2.formulaReal", "steps.4.result" }
|
||||
};
|
||||
}
|
||||
case SolveMode.Map:
|
||||
{
|
||||
// 让实地距离是整齐数字,反推图上距离也是整齐数字
|
||||
var realCm = mapCm * n;
|
||||
var (v, key) = HumanReal(realCm);
|
||||
return new QuizItem
|
||||
{
|
||||
Mode = SolveMode.Map,
|
||||
PromptKey = "quiz.promptMap",
|
||||
PromptArgs = new object[] { FormatN(n), Num(v) },
|
||||
Answer = mapCm,
|
||||
AnswerUnitKey = "unit.cm",
|
||||
StepKeys = new[] { "steps.2.formulaMap", "steps.4.result" }
|
||||
};
|
||||
}
|
||||
default:
|
||||
{
|
||||
var realCm = mapCm * n;
|
||||
var (v, key) = HumanReal(realCm);
|
||||
return new QuizItem
|
||||
{
|
||||
Mode = SolveMode.Scale,
|
||||
PromptKey = "quiz.promptScale",
|
||||
PromptArgs = new object[] { Num(mapCm), Num(v) },
|
||||
Answer = n,
|
||||
AnswerUnitKey = "unit.none",
|
||||
StepKeys = new[] { "steps.1.unify", "steps.2.formulaScale", "steps.4.result" }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CheckAnswer(QuizItem q, double input)
|
||||
{
|
||||
if (q.Answer <= 0) return false;
|
||||
return Math.Abs(input - q.Answer) / q.Answer <= q.Tolerance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace NeoRF.ScaleCalculator;
|
||||
|
||||
/// <summary>
|
||||
/// 自绘可视化标尺:上条「图上距离」按真实厘米画,下条「实地距离」按同一比例尺压缩,
|
||||
/// 再画线段式比例尺(自动换挡)与比例尺光谱。全部在 Render 里完成,不产生子控件。
|
||||
/// </summary>
|
||||
public sealed class ScaleRuler : Control
|
||||
{
|
||||
// 与启动器 AppTheme 保持一致的调色(模型 dll 独立加载,取不到 DynamicResource 时也能显示)
|
||||
private static readonly Color CInk = Color.Parse("#111A2E");
|
||||
private static readonly Color CInkDim = Color.Parse("#5B6A86");
|
||||
private static readonly Color CInkFaint = Color.Parse("#8A97B1");
|
||||
private static readonly Color CLine = Color.Parse("#E3E8F3");
|
||||
private static readonly Color CLineStrong = Color.Parse("#D2DAEA");
|
||||
private static readonly Color CAccent = Color.Parse("#2F6BFF");
|
||||
private static readonly Color CAccentSoft = Color.Parse("#EAF1FF");
|
||||
private static readonly Color CAccentDark = Color.Parse("#1F4FD0");
|
||||
private static readonly Color CSuccess = Color.Parse("#1F9D6B");
|
||||
private static readonly Color CWarn = Color.Parse("#D98613");
|
||||
private static readonly Color CDanger = Color.Parse("#D64545");
|
||||
private static readonly Color CSurfaceAlt = Color.Parse("#F7F9FE");
|
||||
private static readonly Color CSurface = Color.Parse("#FFFFFF");
|
||||
|
||||
private static readonly Typeface Font = new("Noto Sans CJK SC, Microsoft YaHei, Arial");
|
||||
|
||||
/// <summary>1 厘米对应多少屏幕像素。</summary>
|
||||
public double PxPerCm = 46;
|
||||
|
||||
/// <summary>能画下的最大宽度(超出即溢出提示)。</summary>
|
||||
public double MaxBarWidth = 520;
|
||||
|
||||
public ScaleState State { get; set; } = new();
|
||||
public ScaleData Data { get; set; } = new();
|
||||
public Func<string, string> T { get; set; } = k => k;
|
||||
|
||||
/// <summary>线段式比例尺换挡结果(供界面读数)。</summary>
|
||||
public SegmentScale Segment { get; private set; } = new() { MetersPerDiv = 1000, DivPx = 46 };
|
||||
|
||||
/// <summary>图上距离是否超出可画范围。</summary>
|
||||
public bool MapOverflow { get; private set; }
|
||||
|
||||
/// <summary>实地条溢出(按当前比例尺,这段实地图上也画不下)。</summary>
|
||||
public bool RealOverflow { get; private set; }
|
||||
|
||||
public void Refresh() => InvalidateVisual();
|
||||
|
||||
private static FormattedText MakeText(string text, double size, Color color, FontWeight? weight = null)
|
||||
{
|
||||
var ft = new FormattedText(text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
|
||||
new Typeface(Font.FontFamily, FontStyle.Normal, weight ?? FontWeight.Normal), size,
|
||||
new SolidColorBrush(color));
|
||||
return ft;
|
||||
}
|
||||
|
||||
private static void Text(DrawingContext dc, string s, double x, double y, double size, Color color,
|
||||
FontWeight? weight = null, TextAlignment align = TextAlignment.Left)
|
||||
{
|
||||
var ft = MakeText(s, size, color, weight);
|
||||
ft.TextAlignment = align;
|
||||
dc.DrawText(ft, new Point(align == TextAlignment.Center ? x - ft.Width / 2 : x, y));
|
||||
}
|
||||
|
||||
private static void RoundRect(DrawingContext dc, Rect r, double radius, IBrush fill, IPen? pen = null)
|
||||
=> dc.DrawRectangle(fill, pen, new RoundedRect(r, radius));
|
||||
|
||||
private static void Dot(DrawingContext dc, double x, double y, double r, IBrush fill)
|
||||
=> dc.DrawEllipse(fill, null, new Point(x, y), r, r);
|
||||
|
||||
public override void Render(DrawingContext dc)
|
||||
{
|
||||
base.Render(dc);
|
||||
var w = Bounds.Width;
|
||||
var h = Bounds.Height;
|
||||
if (w < 40 || h < 40) return;
|
||||
|
||||
// 背景
|
||||
dc.FillRectangle(new SolidColorBrush(CSurface), new Rect(0, 0, w, h));
|
||||
|
||||
var padL = 34.0;
|
||||
var padR = 30.0;
|
||||
var barW = Math.Max(60, w - padL - padR);
|
||||
MaxBarWidth = barW;
|
||||
|
||||
var mapPx = ScaleMath.CmToPx(State.MapCm, PxPerCm);
|
||||
var realPx = ScaleMath.RealToMapPx(State.RealCm, State.N, PxPerCm);
|
||||
MapOverflow = mapPx > barW + 0.5;
|
||||
RealOverflow = realPx > barW + 0.5;
|
||||
|
||||
// 线段式比例尺换挡,取"每格约 60px"
|
||||
Segment = ScaleMath.PickSegment(State.N, PxPerCm, 62);
|
||||
|
||||
var y = 16.0;
|
||||
|
||||
// ── 标题
|
||||
Text(dc, T("ruler.title"), padL, y, 13, CInk, FontWeight.SemiBold);
|
||||
Text(dc, ScaleMath.Digital(State.N), w - padR, y + 1, 14, CAccentDark,
|
||||
FontWeight.SemiBold, TextAlignment.Right);
|
||||
y += 30;
|
||||
|
||||
// ═══════════════════════════════════════════ 1. 图上距离条
|
||||
y = DrawMapBar(dc, padL, y, barW, mapPx);
|
||||
y += 22;
|
||||
|
||||
// ═══════════════════════════════════════════ 2. 等式连接
|
||||
DrawEquation(dc, padL, y, barW);
|
||||
y += 30;
|
||||
|
||||
// ═══════════════════════════════════════════ 3. 实地距离条(压缩)
|
||||
y = DrawRealBar(dc, padL, y, barW, realPx);
|
||||
y += 24;
|
||||
|
||||
// ═══════════════════════════════════════════ 4. 线段式比例尺
|
||||
y = DrawSegmentScale(dc, padL, y, barW);
|
||||
y += 24;
|
||||
|
||||
// ═══════════════════════════════════════════ 5. 比例尺光谱
|
||||
y = DrawSpectrum(dc, padL, y, barW);
|
||||
y += 34;
|
||||
|
||||
// ═══════════════════════════════════════════ 6. 提示 / 溢出
|
||||
DrawFooter(dc, padL, y, barW);
|
||||
}
|
||||
|
||||
private double DrawMapBar(DrawingContext dc, double x, double y, double barW, double mapPx)
|
||||
{
|
||||
Text(dc, T("ruler.mapBar"), x, y, 11.5, CInkDim, FontWeight.SemiBold);
|
||||
Text(dc, ScaleMath.Num(State.MapCm) + " " + T("unit.cm"), x + barW, y, 12.5, CAccent,
|
||||
FontWeight.SemiBold, TextAlignment.Right);
|
||||
y += 20;
|
||||
|
||||
var barH = 34.0;
|
||||
var r = new Rect(x, y, barW, barH);
|
||||
RoundRect(dc, r, 9, new SolidColorBrush(CSurfaceAlt), new Pen(new SolidColorBrush(CLine), 1));
|
||||
|
||||
// 底上的厘米刻度(每 1cm 一格,最多画 30 格)
|
||||
var maxTicks = Math.Min(30, (int)(barW / PxPerCm) + 1);
|
||||
for (var i = 1; i <= maxTicks; i++)
|
||||
{
|
||||
var tx = x + i * PxPerCm;
|
||||
if (tx > x + barW - 2) break;
|
||||
dc.DrawLine(new Pen(new SolidColorBrush(CLineStrong), 1),
|
||||
new Point(tx, y + barH - 7), new Point(tx, y + barH));
|
||||
if (i % 2 == 0)
|
||||
Text(dc, i.ToString(CultureInfo.InvariantCulture), tx, y + barH + 2, 9.5, CInkFaint,
|
||||
align: TextAlignment.Center);
|
||||
}
|
||||
|
||||
// 实长(蓝色),超出则截断并染红
|
||||
var drawW = Math.Min(mapPx, barW);
|
||||
if (drawW > 0.5)
|
||||
{
|
||||
var fill = new LinearGradientBrush
|
||||
{
|
||||
StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
|
||||
EndPoint = new RelativePoint(1, 0, RelativeUnit.Relative),
|
||||
GradientStops = new GradientStops
|
||||
{
|
||||
new(CAccentSoft, 0), new(CAccent, 1)
|
||||
}
|
||||
};
|
||||
RoundRect(dc, new Rect(x + 1, y + 1, drawW - 2, barH - 2), 8, fill,
|
||||
new Pen(new SolidColorBrush(MapOverflow ? CDanger : CAccentDark), 1.5));
|
||||
}
|
||||
|
||||
// 端点
|
||||
var endX = x + Math.Min(mapPx, barW);
|
||||
Dot(dc, x + 2, y + barH / 2, 4, new SolidColorBrush(CSurface));
|
||||
if (!MapOverflow)
|
||||
{
|
||||
Dot(dc, endX - 2, y + barH / 2, 5, new SolidColorBrush(CSurface));
|
||||
Dot(dc, endX - 2, y + barH / 2, 3, new SolidColorBrush(CAccentDark));
|
||||
// 长度标注
|
||||
Text(dc, ScaleMath.Num(State.MapCm) + " " + T("unit.cm"),
|
||||
endX - 4, y - 16, 11, CAccentDark, FontWeight.SemiBold, TextAlignment.Right);
|
||||
}
|
||||
else
|
||||
{
|
||||
Text(dc, "»", x + barW - 14, y + barH / 2 - 9, 16, CDanger, FontWeight.Bold);
|
||||
}
|
||||
|
||||
return y + barH;
|
||||
}
|
||||
|
||||
private void DrawEquation(DrawingContext dc, double x, double y, double barW)
|
||||
{
|
||||
var cx = x + barW / 2;
|
||||
dc.DrawLine(new Pen(new SolidColorBrush(CLineStrong), 1, dashStyle: DashStyle.Dash),
|
||||
new Point(x, y + 10), new Point(x + barW, y + 10));
|
||||
var label = T("ruler.equals");
|
||||
var ft = MakeText(label, 11, CInkDim, FontWeight.SemiBold);
|
||||
var bw = ft.Width + 22;
|
||||
RoundRect(dc, new Rect(cx - bw / 2, y, bw, 21), 10, new SolidColorBrush(CSurface),
|
||||
new Pen(new SolidColorBrush(CLine), 1));
|
||||
Text(dc, label, cx, y + 4, 11, CInkDim, FontWeight.SemiBold, TextAlignment.Center);
|
||||
}
|
||||
|
||||
private double DrawRealBar(DrawingContext dc, double x, double y, double barW, double realPx)
|
||||
{
|
||||
var (rv, rkey) = ScaleMath.HumanReal(State.RealCm);
|
||||
Text(dc, T("ruler.realBar"), x, y, 11.5, CInkDim, FontWeight.SemiBold);
|
||||
Text(dc, ScaleMath.Num(rv) + " " + T(rkey), x + barW, y, 12.5, CSuccess,
|
||||
FontWeight.SemiBold, TextAlignment.Right);
|
||||
y += 20;
|
||||
|
||||
var barH = 30.0;
|
||||
RoundRect(dc, new Rect(x, y, barW, barH), 9, new SolidColorBrush(CSurfaceAlt),
|
||||
new Pen(new SolidColorBrush(CLine), 1));
|
||||
|
||||
// 用"按同一比例尺压缩后"的长度表现:与上图同宽才直观
|
||||
var drawW = Math.Min(realPx, barW);
|
||||
if (drawW > 0.5)
|
||||
{
|
||||
var fill = new LinearGradientBrush
|
||||
{
|
||||
StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
|
||||
EndPoint = new RelativePoint(1, 0, RelativeUnit.Relative),
|
||||
GradientStops = new GradientStops { new(Color.Parse("#DFF5EA"), 0), new(CSuccess, 1) }
|
||||
};
|
||||
RoundRect(dc, new Rect(x + 1, y + 1, drawW - 2, barH - 2), 8, fill,
|
||||
new Pen(new SolidColorBrush(RealOverflow ? CDanger : CSuccess), 1.5));
|
||||
}
|
||||
|
||||
// 画上"实地"的等分虚线,表达这段距离很长
|
||||
for (var i = 1; i < 6; i++)
|
||||
{
|
||||
var tx = x + barW * i / 6.0;
|
||||
dc.DrawLine(new Pen(new SolidColorBrush(Color.Parse("#80FFFFFF")), 1, dashStyle: DashStyle.Dash),
|
||||
new Point(tx, y + 5), new Point(tx, y + barH - 5));
|
||||
}
|
||||
|
||||
if (!RealOverflow)
|
||||
{
|
||||
Dot(dc, x + drawW - 2, y + barH / 2, 4, new SolidColorBrush(CSurface));
|
||||
}
|
||||
else
|
||||
{
|
||||
Text(dc, "»", x + barW - 14, y + barH / 2 - 9, 16, CDanger, FontWeight.Bold);
|
||||
}
|
||||
|
||||
// 中间写"= 图上这段"
|
||||
Text(dc, T("ruler.sameWidth"), x + barW / 2, y + barH / 2 - 7, 10.5, CInkDim,
|
||||
align: TextAlignment.Center);
|
||||
|
||||
return y + barH;
|
||||
}
|
||||
|
||||
private double DrawSegmentScale(DrawingContext dc, double x, double y, double barW)
|
||||
{
|
||||
Text(dc, T("ruler.segment"), x, y, 11.5, CInkDim, FontWeight.SemiBold);
|
||||
Text(dc, T("ruler.segmentHint"), x + barW, y + 1, 10, CInkFaint, align: TextAlignment.Right);
|
||||
y += 20;
|
||||
|
||||
var seg = Segment;
|
||||
var total = seg.DivPx * seg.DivCount;
|
||||
if (total > barW)
|
||||
{
|
||||
// 再降一档,避免画不下
|
||||
seg = ScaleMath.PickSegment(State.N * 3, PxPerCm, 62);
|
||||
Segment = seg;
|
||||
total = seg.DivPx * seg.DivCount;
|
||||
}
|
||||
var sx = x + Math.Max(0, (barW - total) / 2);
|
||||
var sy = y;
|
||||
var segH = 13.0;
|
||||
|
||||
for (var i = 0; i < seg.DivCount; i++)
|
||||
{
|
||||
var r = new Rect(sx + i * seg.DivPx, sy, seg.DivPx, segH);
|
||||
dc.FillRectangle(new SolidColorBrush(i % 2 == 0 ? CInk : CSurface), r);
|
||||
dc.DrawRectangle(null, new Pen(new SolidColorBrush(CInk), 1), r);
|
||||
Text(dc, i == 0 ? "0" : ScaleMath.SegmentLabel(seg.MetersPerDiv * i),
|
||||
sx + (i + 1) * seg.DivPx, sy + segH + 3, 9.5, CInkDim, align: TextAlignment.Center);
|
||||
}
|
||||
Text(dc, ScaleMath.SegmentLabel(seg.MetersPerDiv), sx + total / 2, sy + segH + 17, 10,
|
||||
CInkDim, FontWeight.SemiBold, TextAlignment.Center);
|
||||
|
||||
return sy + segH + 32;
|
||||
}
|
||||
|
||||
private double DrawSpectrum(DrawingContext dc, double x, double y, double barW)
|
||||
{
|
||||
Text(dc, T("ruler.spectrum"), x, y, 11.5, CInkDim, FontWeight.SemiBold);
|
||||
y += 20;
|
||||
|
||||
var trackY = y + 6;
|
||||
var trackH = 8.0;
|
||||
var grad = new LinearGradientBrush
|
||||
{
|
||||
StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
|
||||
EndPoint = new RelativePoint(1, 0, RelativeUnit.Relative),
|
||||
GradientStops = new GradientStops
|
||||
{
|
||||
new(CAccent, 0), new(Color.Parse("#5B8DEF"), 0.4),
|
||||
new(Color.Parse("#8FB4F5"), 0.7), new(Color.Parse("#C9DAF8"), 1)
|
||||
}
|
||||
};
|
||||
RoundRect(dc, new Rect(x, trackY, barW, trackH), 4, grad);
|
||||
|
||||
// 光谱层级刻度
|
||||
foreach (var item in Data.Spectrum)
|
||||
{
|
||||
var p = ScaleMath.SpectrumPosition(item.N);
|
||||
var px = x + barW * p;
|
||||
dc.DrawLine(new Pen(new SolidColorBrush(CSurface), 1.5),
|
||||
new Point(px, trackY), new Point(px, trackY + trackH));
|
||||
Text(dc, T(item.LabelKey), px, trackY + trackH + 3, 9.5, CInkFaint,
|
||||
align: TextAlignment.Center);
|
||||
}
|
||||
|
||||
// 当前值指针
|
||||
var pos = ScaleMath.SpectrumPosition(State.N);
|
||||
var cx = x + barW * pos;
|
||||
dc.DrawLine(new Pen(new SolidColorBrush(CInk), 2),
|
||||
new Point(cx, trackY - 7), new Point(cx, trackY + trackH + 5));
|
||||
Dot(dc, cx, trackY - 9, 5, new SolidColorBrush(CAccent));
|
||||
Dot(dc, cx, trackY - 9, 2.5, new SolidColorBrush(CSurface));
|
||||
|
||||
// 两端说明
|
||||
Text(dc, T("ruler.scopeUp"), x, trackY + trackH + 20, 10, CInkFaint);
|
||||
Text(dc, T("ruler.detailDown"), x + barW, trackY + trackH + 20, 10, CInkFaint,
|
||||
align: TextAlignment.Right);
|
||||
|
||||
return trackY + trackH + 36;
|
||||
}
|
||||
|
||||
private void DrawFooter(DrawingContext dc, double x, double y, double barW)
|
||||
{
|
||||
if (MapOverflow || RealOverflow)
|
||||
{
|
||||
var msg = T(MapOverflow ? "ruler.overflowMap" : "ruler.overflowReal");
|
||||
var r = new Rect(x, y, barW, 30);
|
||||
RoundRect(dc, r, 8, new SolidColorBrush(Color.Parse("#FDECEC")),
|
||||
new Pen(new SolidColorBrush(Color.Parse("#F5C2C2")), 1));
|
||||
Text(dc, "⚠ " + msg, x + 12, y + 7, 11, CDanger, FontWeight.SemiBold);
|
||||
}
|
||||
else
|
||||
{
|
||||
Text(dc, T("ruler.hint"), x, y + 4, 10.5, CInkFaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user