MaidFaceSliderPane.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6. using System.Linq;
  7. namespace COM3D2.MeidoPhotoStudio.Plugin
  8. {
  9. using static Meido;
  10. public class MaidFaceSliderPane : BasePane
  11. {
  12. private static readonly Dictionary<string, float> SliderLimits = new Dictionary<string, float>()
  13. {
  14. // Eye Shut
  15. ["eyeclose"] = 1f,
  16. // Eye Smile
  17. ["eyeclose2"] = 1f,
  18. // Glare
  19. ["eyeclose3"] = 1f,
  20. // Wide Eyes
  21. ["eyebig"] = 1f,
  22. // Wink 1
  23. ["eyeclose6"] = 1f,
  24. // Wink 2
  25. ["eyeclose5"] = 1f,
  26. // Highlight
  27. ["hitomih"] = 2f,
  28. // Pupil Size
  29. ["hitomis"] = 3f,
  30. // Brow 1
  31. ["mayuha"] = 1f,
  32. // Brow 2
  33. ["mayuw"] = 1f,
  34. // Brow Up
  35. ["mayuup"] = 1f,
  36. // Brow Down 1
  37. ["mayuv"] = 1f,
  38. // Brow Down 2
  39. ["mayuvhalf"] = 1f,
  40. // Mouth Open 1
  41. ["moutha"] = 1f,
  42. // Mouth Open 2
  43. ["mouths"] = 1f,
  44. // Mouth Narrow
  45. ["mouthc"] = 1f,
  46. // Mouth Widen
  47. ["mouthi"] = 1f,
  48. // Smile
  49. ["mouthup"] = 1.4f,
  50. // Frown
  51. ["mouthdw"] = 1f,
  52. // Mouth Pucker
  53. ["mouthhe"] = 1f,
  54. // Grin
  55. ["mouthuphalf"] = 2f,
  56. // Tongue Out
  57. ["tangout"] = 1f,
  58. // Tongue Up
  59. ["tangup"] = 1f,
  60. // Tongue Base
  61. ["tangopen"] = 1f
  62. };
  63. private readonly MeidoManager meidoManager;
  64. private readonly Dictionary<string, BaseControl> faceControls;
  65. private bool hasTangOpen;
  66. public MaidFaceSliderPane(MeidoManager meidoManager)
  67. {
  68. this.meidoManager = meidoManager;
  69. faceControls = new Dictionary<string, BaseControl>();
  70. foreach (string key in faceKeys)
  71. {
  72. string uiName = Translation.Get("faceBlendValues", key);
  73. Slider slider = new Slider(uiName, 0f, SliderLimits[key]);
  74. string myKey = key;
  75. slider.ControlEvent += (s, a) => SetFaceValue(myKey, slider.Value);
  76. faceControls[key] = slider;
  77. }
  78. foreach (string key in faceToggleKeys)
  79. {
  80. string uiName = Translation.Get("faceBlendValues", key);
  81. Toggle toggle = new Toggle(uiName);
  82. string myKey = key;
  83. toggle.ControlEvent += (s, a) => SetFaceValue(myKey, toggle.Value);
  84. faceControls[key] = toggle;
  85. }
  86. InitializeSliderLimits(faceControls);
  87. }
  88. private static void InitializeSliderLimits(Dictionary<string, BaseControl> controls)
  89. {
  90. try
  91. {
  92. string sliderLimitsPath = Path.Combine(Constants.databasePath, "face_slider_limits.json");
  93. string sliderLimitsJson = File.ReadAllText(sliderLimitsPath);
  94. foreach (var kvp in JsonConvert.DeserializeObject<Dictionary<string, float>>(sliderLimitsJson))
  95. {
  96. string key = kvp.Key;
  97. if (faceKeys.Contains(key) && controls.ContainsKey(key))
  98. {
  99. float limit = kvp.Value;
  100. limit = kvp.Value >= 1f ? limit : SliderLimits[key];
  101. Slider slider = (Slider)controls[kvp.Key];
  102. slider.SetBounds(slider.Left, limit);
  103. }
  104. else Utility.LogWarning($"'{key}' is not a valid face key");
  105. }
  106. }
  107. catch (IOException e)
  108. {
  109. Utility.LogWarning($"Could not open face slider limit database because {e.Message}");
  110. }
  111. catch (Exception e)
  112. {
  113. Utility.LogError($"Could not apply face slider limit database because {e.Message}");
  114. }
  115. }
  116. protected override void ReloadTranslation()
  117. {
  118. for (int i = 0; i < faceKeys.Length; i++)
  119. {
  120. Slider slider = (Slider)faceControls[faceKeys[i]];
  121. slider.Label = Translation.Get("faceBlendValues", faceKeys[i]);
  122. }
  123. for (int i = 0; i < faceToggleKeys.Length; i++)
  124. {
  125. Toggle toggle = (Toggle)faceControls[faceToggleKeys[i]];
  126. toggle.Label = Translation.Get("faceBlendValues", faceToggleKeys[i]);
  127. }
  128. }
  129. public override void UpdatePane()
  130. {
  131. updating = true;
  132. Meido meido = meidoManager.ActiveMeido;
  133. for (int i = 0; i < faceKeys.Length; i++)
  134. {
  135. Slider slider = (Slider)faceControls[faceKeys[i]];
  136. try
  137. {
  138. slider.Value = meido.GetFaceBlendValue(faceKeys[i]);
  139. }
  140. catch { }
  141. }
  142. for (int i = 0; i < faceToggleKeys.Length; i++)
  143. {
  144. string hash = faceToggleKeys[i];
  145. Toggle toggle = (Toggle)faceControls[hash];
  146. toggle.Value = meido.GetFaceBlendValue(hash) > 0f;
  147. if (hash == "toothoff") toggle.Value = !toggle.Value;
  148. }
  149. hasTangOpen = meido.Body.Face.morph.Contains("tangopen");
  150. updating = false;
  151. }
  152. public override void Draw()
  153. {
  154. GUI.enabled = meidoManager.HasActiveMeido;
  155. DrawSliders("eyeclose", "eyeclose2");
  156. DrawSliders("eyeclose3", "eyebig");
  157. DrawSliders("eyeclose6", "eyeclose5");
  158. DrawSliders("hitomih", "hitomis");
  159. DrawSliders("mayuha", "mayuw");
  160. DrawSliders("mayuup", "mayuv");
  161. DrawSliders("mayuvhalf");
  162. DrawSliders("moutha", "mouths");
  163. DrawSliders("mouthc", "mouthi");
  164. DrawSliders("mouthup", "mouthdw");
  165. DrawSliders("mouthhe", "mouthuphalf");
  166. DrawSliders("tangout", "tangup");
  167. if (hasTangOpen) DrawSliders("tangopen");
  168. MpsGui.WhiteLine();
  169. DrawToggles("hoho2", "shock", "nosefook");
  170. DrawToggles("namida", "yodare", "toothoff");
  171. DrawToggles("tear1", "tear2", "tear3");
  172. DrawToggles("hohos", "hoho", "hohol");
  173. GUI.enabled = true;
  174. }
  175. private void DrawSliders(params string[] keys)
  176. {
  177. GUILayout.BeginHorizontal();
  178. foreach (string key in keys) faceControls[key].Draw(MpsGui.HalfSlider);
  179. GUILayout.EndHorizontal();
  180. }
  181. private void DrawToggles(params string[] keys)
  182. {
  183. GUILayout.BeginHorizontal();
  184. foreach (string key in keys) faceControls[key].Draw();
  185. GUILayout.EndHorizontal();
  186. }
  187. private void SetFaceValue(string key, float value)
  188. {
  189. if (updating) return;
  190. meidoManager.ActiveMeido.SetFaceBlendValue(key, value);
  191. }
  192. private void SetFaceValue(string key, bool value)
  193. {
  194. if (key == "toothoff") value = !value;
  195. SetFaceValue(key, value ? 1f : 0f);
  196. }
  197. }
  198. }