3 using UnityEngine.EventSystems;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Text.RegularExpressions;
9 using System.ComponentModel;
14 public GameObject textObj;
15 public GameObject tabButton;
16 public GameObject emptyFiller;
17 public GameObject rawImageObj;
18 public GameObject scrollView;
19 public GameObject viewPort;
23 private bool htmlRendered =
false;
38 Texture2D texture =
new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32,
false);
39 texture.LoadImage(imageArray);
42 rawImageObj.GetComponent<RawImage>().material.mainTexture = texture;
45 rawImageObj.GetComponent<RectTransform>().sizeDelta =
new Vector2(imageWidth, imageHeight);
46 rawImageObj.GetComponent<RectTransform>().anchoredPosition =
new Vector2(imageWidth / 2, -imageHeight / 2);
48 textObj.SetActive(
false);
49 rawImageObj.SetActive(
true);
51 scrollView.GetComponent<ScrollRect>().content = rawImageObj.GetComponent<RectTransform>();
58 tabButton.SetActive(
false);
59 rawImageObj.SetActive(
false);
61 text = textObj.GetComponent<Text>();
62 string msg = bold(
"Patient Information");
63 msg +=
"\n\nNo patient loaded.";
66 PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Loaded, eventNewPatientLoaded);
67 PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Closed, eventPatientClosed);
69 Patient loadedPatient = Patient.getLoadedPatient();
70 Debug.Log(
"Loaded patient: " + loadedPatient);
71 if (loadedPatient != null)
73 eventNewPatientLoaded(loadedPatient);
81 void eventNewPatientLoaded(
object obj)
90 List<string> tabNames = patient.getAdditionalInfoTabs();
91 for (
int i = 0; i < tabNames.Count; i++)
93 GameObject newButton = GameObject.Instantiate(tabButton);
94 newButton.SetActive(
true);
95 newButton.name = tabNames[i];
96 newButton.transform.Find(
"Text").GetComponent<Text>().text = tabNames[i];
97 newButton.transform.SetParent(tabButton.transform.parent,
false);
99 Button b = newButton.GetComponent<Button>();
100 b.onClick.AddListener(() => selectTab(b));
104 emptyFiller.transform.SetAsLastSibling ();
106 if (tabButton.transform.parent.childCount > 2)
109 selectTab(tabButton.transform.parent.GetChild(1).GetComponent<Button>());
114 private void selectTab(Button b)
116 foreach (Transform child
in tabButton.transform.parent)
118 if( child.gameObject != emptyFiller )
119 UI.Core.instance.unselectTab(child.GetComponent<Button>());
121 UI.Core.instance.selectTab(b);
123 string tabName = b.GetComponentInChildren<Text>().text;
124 Patient loadedPatient = Patient.getLoadedPatient();
125 if (loadedPatient == null)
130 Debug.Log(
"Loading tab: " + tabName);
135 bool htmlFound =
false;
136 foreach (
Patient.AdditionalInformation info in loadedPatient.getAdditionalInfo())
140 if (info.tabName == tabName)
143 if (info.type ==
"HTML")
150 result +=
"<b>" + info.name +
"</b>" +
"\n\n";
151 result += info.content +
"\n\n";
162 private void showText(
string result)
164 textObj.SetActive(
true);
165 rawImageObj.SetActive(
false);
166 scrollView.GetComponent<ScrollRect>().content = textObj.GetComponent<RectTransform>();
171 private void showHTML(
Patient.AdditionalInformation info)
173 rawImageObj.SetActive(
false);
176 int widthOfViewport = (int)viewPort.GetComponent<RectTransform>().rect.width - 17;
178 widthOfViewport = Math.Max(1, widthOfViewport);
179 int widthBody = findBodyWidth(info.content);
182 if (widthBody > widthOfViewport)
184 html = rewriteBodyWidth(info.content, widthOfViewport);
193 private void showHTMLCallback(
object sender, RunWorkerCompletedEventArgs e)
197 Debug.Log(
"[PatientBriefing.cs] HTML Rendering cancelled");
199 else if (e.Error != null)
201 Debug.LogError(
"[PatientBriefing.cs] HTML Rendering Error");
210 private void showHTMLWorker(
object sender, DoWorkEventArgs e)
212 System.Drawing.Bitmap image = (System.Drawing.Bitmap)TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImageGdiPlus(html);
213 imageWidth = image.Width;
214 imageHeight = image.Height;
215 using (MemoryStream ms =
new MemoryStream())
217 image.Save(ms, image.RawFormat);
218 imageArray = ms.ToArray();
222 private int findBodyWidth(
string input){
224 string pattern =
"width:\\s*\\d+px\\s*;";
225 Regex rgx =
new Regex(pattern);
226 if(!rgx.IsMatch(input)){
228 return Int32.MaxValue;
230 string width = rgx.Match(input).Value;
233 string pattern2 =
"\\d+";
234 Regex rgx2 =
new Regex(pattern2);
235 int widthInt = Int32.MaxValue;
236 if (!Int32.TryParse (rgx2.Match (width).Value, out widthInt)) {
237 Debug.LogError (
"HTML parsing error");
242 private string rewriteBodyWidth(
string input,
int width)
244 string pattern =
"width:\\s*\\d+px\\s*;";
245 string replacement =
"width:"+ width +
"px;";
246 Regex rgx =
new Regex(pattern);
248 if (rgx.IsMatch(input))
250 string result = rgx.Replace(input, replacement);
255 string pattern2 =
"<body ";
256 string replacement2 =
"<body width:" + width +
"px;";
257 Regex rgx2 =
new Regex(pattern2);
258 string result2 = rgx2.Replace(input, replacement2);
262 private void clearTabs()
264 foreach (Transform child
in tabButton.transform.parent)
266 if (child.gameObject != tabButton && child.gameObject != emptyFiller)
268 UnityEngine.Object.Destroy(child.gameObject);
273 void eventPatientClosed(
object obj = null)
275 string msg = bold(
"Patient Information");
278 msg += bold(
"Patient Name: ") +
"No patient loaded.";
283 private string bold(
string input)
285 return "<b>" + input +
"</b>";