diff --git a/program/Language.cs b/program/Language.cs new file mode 100644 index 0000000..70695ae --- /dev/null +++ b/program/Language.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Gtk; + +public class Language +{ + public static bool LanguageSwitchValue = false; + + private const string EngCssPath = "lang_eng.css"; + private const string JpnCssPath = "lang_jpn.css"; + private const string EngCsvPath = "info_eng.csv"; + private const string JpnCsvPath = "info_jpn.csv"; + + private readonly Dictionary _engText; + private readonly Dictionary _jpnText; + private readonly Dictionary _buttonsById; + public readonly CssProvider _languageCssProvider = new CssProvider(); + private readonly Label _languageLabel; + + public Language(Dictionary buttonsById, Label languageLabel) + { + _buttonsById = buttonsById; + _languageLabel = languageLabel; + StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600); + + + _engText = LoadButtonText(EngCsvPath); + _jpnText = LoadButtonText(JpnCsvPath); + } + + private Dictionary LoadButtonText(string path) + { + var result = new Dictionary(); + if (!File.Exists(path)) + { + Console.WriteLine($"Warning: text file not found: {path}"); + return result; + } + + var lines = File.ReadAllLines(path); + for (int i = 1; i < lines.Length; i++) + { + var line = lines[i].Trim(); + if (string.IsNullOrEmpty(line)) continue; + var parts = line.Split(new[] { ',' }, 2); + if (parts.Length < 2) continue; + result[parts[0]] = parts[1]; + } + return result; + } + + public void ApplyLanguage(bool isEnglish) + { + var text = isEnglish ? _engText : _jpnText; + string cssPath = isEnglish ? EngCssPath : JpnCssPath; + + foreach (var kvp in _buttonsById) + if (text.TryGetValue(kvp.Key, out var buttonText)) + kvp.Value.Label = buttonText; + + if (File.Exists(cssPath)) + _languageCssProvider.LoadFromPath(cssPath); + else + Console.WriteLine($"Warning: CSS file not found: {cssPath}"); + + _languageLabel.Text = isEnglish ? "あ" : "A"; + } + + //for others + public Language(Dictionary buttonsById) + { + _buttonsById = buttonsById; + StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600); + + + _engText = LoadButtonText(EngCsvPath); + _jpnText = LoadButtonText(JpnCsvPath); + } + + + public void ApplyLanguages(bool isEnglish) + { + var text = isEnglish ? _engText : _jpnText; + string cssPath = isEnglish ? EngCssPath : JpnCssPath; + + foreach (var kvp in _buttonsById) + if (text.TryGetValue(kvp.Key, out var buttonText)) + kvp.Value.Label = buttonText; + + if (File.Exists(cssPath)) + _languageCssProvider.LoadFromPath(cssPath); + else + Console.WriteLine($"Warning: CSS file not found: {cssPath}"); + + } +} \ No newline at end of file diff --git a/program/MainWindow.cs b/program/MainWindow.cs index 216d3d2..d0d2d96 100644 --- a/program/MainWindow.cs +++ b/program/MainWindow.cs @@ -17,21 +17,22 @@ [UI] private Image ConnectionIcon = null; // was GtkIconView in the old glade private CssProvider _scaleCssProvider; - private CssProvider _languageCssProvider; + // private CssProvider _languageCssProvider; private int _lastAppliedWidth = -1; private const int BaseWidth = 1920; private const int BaseHeight = 1080; - private const string EngCssPath = "lang_eng.css"; - private const string JpnCssPath = "lang_jpn.css"; + // private const string EngCssPath = "lang_eng.css"; + // private const string JpnCssPath = "lang_jpn.css"; - private Dictionary _engText; - private Dictionary _jpnText; + // private Dictionary _engText; + // private Dictionary _jpnText; private Dictionary _buttonsById; private readonly ArduinoConnection _arduino = new ArduinoConnection(); - private SettingsWindow _settingsWindow; + internal SettingsWindow _settingsWindow; + internal Language _Language; public MainWindow() : this(CreateBuilder()) { } @@ -47,10 +48,10 @@ builder.Autoconnect(this); _scaleCssProvider = new CssProvider(); - _languageCssProvider = new CssProvider(); + // _languageCssProvider = new CssProvider(); StyleContext.AddProviderForScreen(Gdk.Screen.Default, _scaleCssProvider, 600); - StyleContext.AddProviderForScreen(Gdk.Screen.Default, _languageCssProvider, 600); + // StyleContext.AddProviderForScreen(Gdk.Screen.Default, _Language._languageCssProvider, 600); this.Fullscreen(); @@ -79,10 +80,12 @@ foreach (var kvp in _buttonsById) kvp.Value.Name = kvp.Key; - _engText = LoadButtonText("info_eng.csv"); - _jpnText = LoadButtonText("info_jpn.csv"); + // _engText = LoadButtonText("info_eng.csv"); + // _jpnText = LoadButtonText("info_jpn.csv"); - ApplyLanguage(LanguageSwitch.Active); + _Language = new Language(_buttonsById,LanguageLabel); + + _Language.ApplyLanguage(LanguageSwitch.Active); OnArduinoConnectionChanged(false); } @@ -148,24 +151,24 @@ return result; } - private void ApplyLanguage(bool isEnglish) - { - var text = isEnglish ? _engText : _jpnText; - string cssPath = isEnglish ? EngCssPath : JpnCssPath; + // private void ApplyLanguage(bool isEnglish) + // { + // var text = isEnglish ? _engText : _jpnText; + // string cssPath = isEnglish ? EngCssPath : JpnCssPath; - foreach (var kvp in _buttonsById) - { - if (text.TryGetValue(kvp.Key, out var buttonText)) - kvp.Value.Label = buttonText; - } + // foreach (var kvp in _buttonsById) + // { + // if (text.TryGetValue(kvp.Key, out var buttonText)) + // kvp.Value.Label = buttonText; + // } - if (File.Exists(cssPath)) - _languageCssProvider.LoadFromPath(cssPath); - else - Console.WriteLine($"Warning: CSS file not found: {cssPath}"); + // if (File.Exists(cssPath)) + // _languageCssProvider.LoadFromPath(cssPath); + // else + // Console.WriteLine($"Warning: CSS file not found: {cssPath}"); - LanguageLabel.Text = isEnglish ? "あ" : "A"; - } + // LanguageLabel.Text = isEnglish ? "あ" : "A"; + // } // ---------- scaling ---------- @@ -247,6 +250,8 @@ private void OnLanguageSwitchNotify(object o, GLib.NotifyArgs args) { - ApplyLanguage(LanguageSwitch.Active); + Language.LanguageSwitchValue = LanguageSwitch.Active; + _Language.ApplyLanguage(LanguageSwitch.Active); + _settingsWindow?._Language?.ApplyLanguages(LanguageSwitch.Active); } } \ No newline at end of file diff --git a/program/SettingWindow.cs b/program/SettingWindow.cs index 10a53e6..2d94a53 100644 --- a/program/SettingWindow.cs +++ b/program/SettingWindow.cs @@ -1,5 +1,6 @@ using System; using Gtk; +using System.Collections.Generic; using UI = Gtk.Builder.ObjectAttribute; public class SettingsWindow : Window @@ -14,6 +15,10 @@ [UI] private Label LogLabel = null; [UI] private TextView LogtextBox = null; + private readonly Dictionary _buttonsById; + private readonly Dictionary _labelsById; + internal Language _Language; + private const int MaxLogLines = 500; private readonly ArduinoConnection _arduino; @@ -55,6 +60,21 @@ _arduino.LineReceived += OnLineReceived; _arduino.ConnectionChanged += OnConnectionChanged; + _buttonsById = new Dictionary + { + { "ConnectButton", ConnectButton }, + { "AutoConnectButton", AutoConnectButton }, + { "RefreshButton", RefreshButton }, + { "BackButton", BackButton }, + }; + + // CSS ID selectors (#Option1Button etc.) need the widget Name set explicitly + foreach (var kvp in _buttonsById) + kvp.Value.Name = kvp.Key; + + _Language = new Language(_buttonsById); + _Language.ApplyLanguages(Language.LanguageSwitchValue); + RefreshPorts(); OnConnectionChanged(_arduino.IsOpen); } diff --git a/program/info_eng.csv b/program/info_eng.csv index a9c11e2..3a0481a 100644 --- a/program/info_eng.csv +++ b/program/info_eng.csv @@ -2,4 +2,10 @@ Option1Button,Normal Session Option2Button,Position Recording Mode Option3Button,Exam Mode -CloseButton,Close \ No newline at end of file +CloseButton,Close +SettingButton,Settings +ConnectButton,Connect +AutoConnectButton,Auto Connect +RefreshButton, Refresh Port +LogLabel,Log +BackButton,Back diff --git a/program/info_jpn.csv b/program/info_jpn.csv index 9273e75..1d4e6e8 100644 --- a/program/info_jpn.csv +++ b/program/info_jpn.csv @@ -2,4 +2,10 @@ Option1Button,通常セッション Option2Button,位置記録モード Option3Button,試験モード -CloseButton,閉じる \ No newline at end of file +CloseButton,閉じる +SettingButton,設定 +ConnectButton,接続 +AutoConnectButton,自動接続 +RefreshButton, ポートの更新 +LogLabel,情報 +BackButton,戻る diff --git a/program/lang_eng.css b/program/lang_eng.css index 89a9bc2..fcf83ec 100644 --- a/program/lang_eng.css +++ b/program/lang_eng.css @@ -24,4 +24,46 @@ font-size: 18px; font-weight: bold; font-style: normal; -} \ No newline at end of file +} + +#StatusLabel.status-on { color: #2e7d32; font-weight: bold; } +#StatusLabel.status-off { color: #c62828; font-weight: bold; } + +/* Setting Window +#ConnectButton { + font-family: 'Sans'; + font-size: 20px; + font-weight: normal; + font-style: normal; +} + +#AutoConnectButton { + font-family: 'Sans'; + font-size: 20px; + font-weight: normal; + font-style: normal; +} + +#Option3Button { + font-family: 'Sans'; + font-size: 20px; + font-weight: normal; + font-style: normal; +} + +#CloseButton { + font-family: 'Sans'; + font-size: 18px; + font-weight: bold; + font-style: normal; +} */ + +/* [UI] private Label ComPortLabel = null; + [UI] private ComboBoxText ComPortComboBox = null; + [UI] private Button ConnectButton = null; + [UI] private Button AutoConnectButton = null; + [UI] private Button RefreshButton = null; + [UI] private Button BackButton = null; + [UI] private Label StatusLabel = null; + [UI] private Label LogLabel = null; + [UI] private TextView LogtextBox = null; */ \ No newline at end of file