void Start() { // 設定ファイル(json読み込み) FileInfo info = new FileInfo(Application.streamingAssetsPath + "/KeyboardLayout.json"); StreamReader reader = new StreamReader(info.OpenRead()); string json = reader.ReadToEnd(); // 構造体に変換 KeyboardLayoutList data = JsonUtility.FromJson <KeyboardLayoutList>(json); // 構造体からキーの配置に格納 setKeyLayout(data); }
// jsonから取得した構造体を各キーの配置座標に変換します void setKeyLayout(KeyboardLayoutList data) { Dictionary <string, int> dicSize = new Dictionary <string, int>(); // 本体サイズ設定取得 body.sizeX = data.bodySize.sizeX; body.sizeZ = data.bodySize.sizeZ; // キーサイズ設定取得 foreach (Sizes size in data.sizes) { dicSize.Add(size.name, size.size); } // 各キーの処理 foreach (Row row in data.rows) { string[] saOffset = row.offset.Split(','); Vector3 v3Offset = new Vector3(float.Parse(saOffset[0]), float.Parse(saOffset[1]), -1 * float.Parse(saOffset[2]) - 10f); Vector3 v3Nowpos = v3Offset; float fMargin_x = row.margin_x; foreach (Column column in row.columns) { ArrayList key = new ArrayList(); string[] saSize = column.size.Split(','); // サイズ変換 Vector3 v3Size = new Vector3(dicSize[saSize[0]], 1, dicSize[saSize[1]]); key.Add(v3Nowpos + v3Size / 2); key.Add(v3Size); // 反映 layout.Add(column.text, key); //次の位置更新 v3Nowpos += new Vector3(v3Size.x + fMargin_x, 0, 0); } } }
private void LoadKeyboardLayoutList(bool removeOld) { if (removeOld) { lvLayouts.Items.Clear(); ToolStripItem defItem = cmsLeft.Items[0]; cmsLeft.Items.Clear(); cmsLeft.Items.Add(defItem); ActiveKeyboardList.Clear(); ActiveKeyboardList.Add(DefaultLayout); } KeyboardLayoutList keyboardList = new KeyboardLayoutList(); FileStream fs = null; System.Xml.XmlReader reader = null; try { fs = new FileStream(layoutXMLFile, FileMode.Open, FileAccess.Read); reader = System.Xml.XmlReader.Create(fs); reader.MoveToContent(); if (reader.LocalName == "Layouts") { reader.ReadStartElement("Layouts"); keyboardList.ReadXml(reader); reader.ReadEndElement(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (reader != null) reader.Close(); if (fs != null) fs.Close(); } //long lPtr = DllPtrHotkeys.ToInt64(); List<Hotkey> hotkeys = new List<Hotkey>(); hotkeys.Add(htkyOnOff.Hotkey); foreach (KeyboardLayout layout in keyboardList) { KeyMagicDotNet.InfoList infoList = KeyMagicDotNet.KeyMagicKeyboard.GetInfosFromKeyboardFile(GetSaveKeyboardPath(layout.file)); ListViewItem lvItem = new ListViewItem(layout.file); hotkeys.Add(new Hotkey(layout.hotkey)); lvItem.SubItems.Add(layout.display); lvItem.SubItems.Add(layout.hotkey); lvItem.SubItems.Add(KeyMagicDotNet.KeyMagicKeyboard.GetVersion(GetSaveKeyboardPath(layout.file)).ToString()); lvItem.SubItems.Add(infoList.GetDescription()); using (Bitmap bmIcon = infoList.GetIcon()) { if (bmIcon != null) { imageList.Images.Add(layout.file, bmIcon); lvItem.ImageKey = layout.file; } if (layout.enable) { ActiveKeyboardList.Add(layout); lvItem.Group = lvLayouts.Groups["Enabled"]; KToolStripMenuItem menuItem = new KToolStripMenuItem(layout.display); if (bmIcon != null) using (Icon icon = Icon.FromHandle(bmIcon.GetHicon())) { iconList[layout.file] = menuItem.Icon = icon; } else iconList[layout.file] = defaultKeyboardIcon; menuItem.ImageKey = lvItem.ImageKey; menuItem.ShortcutKeyDisplayString = layout.hotkey; String fontFamily = infoList.GetFontFamily(); menuItem.Click += new EventHandler(cmsLeftMenuItem_Click); cmsLeft.Items.Add(menuItem); } else { lvItem.Group = lvLayouts.Groups["Disabled"]; } } lvLayouts.Items.Add(lvItem); } keyEventHandler.Hotkeys = hotkeys.ToArray(); }
private void SaveKeyboardLayoutList() { int idxFilename = lvLayouts.Columns.IndexOf(colFileName); int idxDisplay = lvLayouts.Columns.IndexOf(colDisplayText); int idxHotkey = lvLayouts.Columns.IndexOf(colHotkey); KeyboardLayoutList keyboardList = new KeyboardLayoutList(); foreach (ListViewItem lvi in lvLayouts.Items) { keyboardList.Add(new KeyboardLayout( lvi.SubItems[idxFilename].Text, lvi.SubItems[idxDisplay].Text, lvi.SubItems[idxHotkey].Text, lvi.Group.Name == "Enabled")); } FileStream fs = null; System.Xml.XmlWriter writer = null; try { fs = new FileStream(layoutXMLFile, FileMode.Create); System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); writer = System.Xml.XmlWriter.Create(fs, settings); writer.WriteStartElement("Layouts"); keyboardList.WriteXml(writer); writer.WriteEndElement(); } catch (UnauthorizedAccessException) { AskToRunAsAdministrator("Access is denied and failed to save keyboard layouts list. Do you want to run KeyMagic as administrator?"); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (writer != null) writer.Close(); if (fs != null) fs.Close(); } }