scriptconsole.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. class ScriptConsole extends UIScriptedMenu
  2. {
  3. protected bool m_HintEditMode;
  4. protected float m_HoverTime;
  5. protected bool m_HoverSuccessTriggered;
  6. protected Widget m_CurrentHoverWidget;
  7. protected Widget m_HintWidgetRoot;
  8. protected ImageWidget m_HintWidgetBackground;
  9. protected Widget m_EditTooltipRoot;
  10. protected RichTextWidget m_HintWidget;
  11. protected ButtonWidget m_HintOkButton;
  12. protected ButtonWidget m_HintCancelButton;
  13. protected ButtonWidget m_HintClearButton;
  14. protected EditBoxWidget m_HintInputText;
  15. protected float m_PrevMouseX;
  16. protected float m_PrevMouseY;
  17. Widget m_ButtonsWindowWidget;
  18. ScriptConsoleTabBase m_SelectedHandler;
  19. protected int m_Id;
  20. static ref array<ref MapMarker> m_MarkedEntities = new array<ref MapMarker>();
  21. int m_SelectedTab;
  22. protected ref map<Widget, ref ScriptConsoleTabBase> m_TabHandlers = new map<Widget, ref ScriptConsoleTabBase>;
  23. protected ref map<int, ref ScriptConsoleTabBase> m_TabHandlersByID = new map<int, ref ScriptConsoleTabBase>;
  24. ButtonWidget m_CloseConsoleButton;
  25. protected static const string HINTS_PATH_DEFAULT = "scripts/data/internal/script_console_hints.json";
  26. protected static const string HINTS_PATH_OPTIONAL = "$mission:script_console_hints.json";
  27. static ref JsonHintsData m_JsonData;
  28. const string NO_HINT_TEXT = "No hint";
  29. void ScriptConsole()
  30. {
  31. #ifndef SERVER
  32. if (GetGame() && GetGame().GetMission() && GetGame().GetMission().GetHud())
  33. {
  34. GetGame().GetMission().GetHud().ShowHudPlayer(false);
  35. GetGame().GetMission().GetHud().ShowQuickbarPlayer(false);
  36. }
  37. #endif
  38. PluginItemDiagnostic plugin = PluginItemDiagnostic.Cast(GetPlugin(PluginItemDiagnostic));
  39. if (plugin)
  40. plugin.OnScriptMenuOpened(true);
  41. }
  42. void ~ScriptConsole()
  43. {
  44. #ifndef SERVER
  45. if (GetGame() && GetGame().GetMission() && GetGame().GetMission().GetHud())
  46. {
  47. GetGame().GetMission().GetHud().ShowHudPlayer(true);
  48. GetGame().GetMission().GetHud().ShowQuickbarPlayer(true);
  49. }
  50. if (m_HintWidgetRoot)
  51. m_HintWidgetRoot.Unlink();
  52. #endif
  53. PluginItemDiagnostic plugin = PluginItemDiagnostic.Cast(GetPlugin(PluginItemDiagnostic));
  54. if (plugin)
  55. plugin.OnScriptMenuOpened(false);
  56. if (GetGame() && GetGame().GetMission())
  57. {
  58. GetGame().GetMission().EnableAllInputs(true);
  59. }
  60. }
  61. ScriptConsoleTabBase GetSelectedHandler()
  62. {
  63. return m_SelectedHandler;
  64. }
  65. static void SaveData()
  66. {
  67. string errorMessage;
  68. if (!JsonFileLoader<JsonHintsData>.SaveFile(HINTS_PATH_OPTIONAL, m_JsonData, errorMessage))
  69. ErrorEx(errorMessage);
  70. }
  71. protected static JsonHintsData GetData()
  72. {
  73. string path = HINTS_PATH_OPTIONAL;
  74. if (!FileExist(path))
  75. {
  76. path = HINTS_PATH_DEFAULT;
  77. //ErrorEx(string.Format("Using default hints file: %1", path), ErrorExSeverity.INFO);
  78. }
  79. string errorMessage;
  80. JsonHintsData data;
  81. if (!JsonFileLoader<JsonHintsData>.LoadFile(path, data, errorMessage))
  82. ErrorEx(errorMessage);
  83. return data;
  84. }
  85. void SetHintText(string text, Widget w)
  86. {
  87. if (m_JsonData && m_JsonData.WidgetHintBindings && w)
  88. {
  89. int hash = GetWidgetCombinedHash(w);
  90. m_JsonData.WidgetHintBindings.Set(hash, text);
  91. Print("setting: " + text);
  92. }
  93. HoverInterrupt();
  94. }
  95. void RegisterTab(ScriptConsoleTabBase handler)
  96. {
  97. m_TabHandlers.Insert(handler.GetButton(), handler);
  98. m_TabHandlersByID.Insert(m_Id, handler);
  99. handler.Init(m_Id);
  100. m_Id++;
  101. }
  102. override Widget Init()
  103. {
  104. m_ConfigDebugProfile = PluginConfigDebugProfile.Cast(GetPlugin(PluginConfigDebugProfile));
  105. layoutRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/script_console/script_console.layout");
  106. m_EditTooltipRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/script_console/script_console_tooltip_edit.layout", layoutRoot);
  107. m_EditTooltipRoot.Show(false);
  108. m_HintOkButton = ButtonWidget.Cast(m_EditTooltipRoot.FindAnyWidget("ButtonOk"));
  109. m_HintCancelButton = ButtonWidget.Cast(m_EditTooltipRoot.FindAnyWidget("ButtonCancel"));
  110. m_HintClearButton = ButtonWidget.Cast(m_EditTooltipRoot.FindAnyWidget("ButtonClear"));
  111. m_HintInputText = EditBoxWidget.Cast(m_EditTooltipRoot.FindAnyWidget("InputText"));
  112. m_ButtonsWindowWidget = layoutRoot.FindAnyWidget("TabButtons");
  113. m_ButtonsWindowWidget.Show(true);
  114. //(Widget root, ScriptConsole console, Widget button, ScriptConsoleTabBase parent = null)
  115. RegisterTab(new ScriptConsoleItemsTab(layoutRoot.FindAnyWidget("ItemsPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("ItemsButtonWidget"))));
  116. RegisterTab(new ScriptConsoleConfigTab(layoutRoot.FindAnyWidget("ConfigsPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("ConfigsButtonWidget"))));
  117. RegisterTab(new ScriptConsoleEnfScriptTab(layoutRoot.FindAnyWidget("EnScriptPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("EnScriptButtonWidget"))));
  118. RegisterTab(new ScriptConsoleEnfScriptServerTab(layoutRoot.FindAnyWidget("EnScriptPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("EnScriptButtonWidgetServer"))));
  119. RegisterTab(new ScriptConsoleGeneralTab(layoutRoot.FindAnyWidget("GeneralPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("GeneralButtonWidget"))));
  120. RegisterTab(new ScriptConsoleOutputTab(layoutRoot.FindAnyWidget("OutputPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("OutputButtonWidget"))));
  121. RegisterTab(new ScriptConsoleVicinityTab(layoutRoot.FindAnyWidget("VicinityPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("VicinityWidget"))));
  122. RegisterTab(new ScriptConsoleSoundsTab(layoutRoot.FindAnyWidget("SoundsPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("SoundsWidget"))));
  123. RegisterTab(new ScriptConsoleWeatherTab(layoutRoot.FindAnyWidget("WeatherPanel"),this,ButtonWidget.Cast(layoutRoot.FindAnyWidget("WeatherButtonWidget"))));
  124. m_CloseConsoleButton = ButtonWidget.Cast(layoutRoot.FindAnyWidget("CloseConsoleButtonWidget"));
  125. // load data from profile
  126. m_SelectedTab = m_ConfigDebugProfile.GetTabSelected();
  127. SelectTabByID(m_SelectedTab);
  128. m_JsonData = GetData();
  129. return layoutRoot;
  130. }
  131. protected void HideHint()
  132. {
  133. if (m_HintWidgetRoot)
  134. m_HintWidgetRoot.Unlink();
  135. }
  136. int GetWidgetCombinedHash(Widget w)
  137. {
  138. string nameThis = w.GetName();
  139. string nameParent = "";
  140. if (w.GetParent())
  141. {
  142. nameParent = w.GetParent().GetName();
  143. }
  144. string namesCombined = nameThis + nameParent;
  145. return namesCombined.Hash();
  146. }
  147. protected string GetMessage()
  148. {
  149. int hash = GetWidgetCombinedHash(m_CurrentHoverWidget);
  150. if (m_JsonData && m_JsonData.WidgetHintBindings)
  151. {
  152. if (m_JsonData.WidgetHintBindings.Contains(hash))
  153. {
  154. return m_JsonData.WidgetHintBindings.Get(hash);
  155. }
  156. }
  157. //return "";
  158. //return "No hint" + hash.ToString();
  159. return NO_HINT_TEXT;
  160. }
  161. protected void HoverSuccess()
  162. {
  163. m_HoverSuccessTriggered = true;
  164. DisplayHint(GetMessage());
  165. }
  166. protected void HoverInterrupt()
  167. {
  168. m_HoverSuccessTriggered = false;
  169. m_HoverTime = 0;
  170. m_HintEditMode = false;
  171. HideHint();
  172. }
  173. override bool OnKeyPress(Widget w, int x, int y, int key)
  174. {
  175. super.OnKeyPress(w, x, y, key);
  176. if (m_SelectedHandler.OnKeyPress(w,x,y,key))
  177. return true;
  178. return false;
  179. }
  180. override bool OnKeyDown(Widget w, int x, int y, int key)
  181. {
  182. super.OnKeyDown(w, x, y, key);
  183. if (m_SelectedHandler.OnKeyDown(w,x,y,key))
  184. return true;
  185. return false;
  186. }
  187. override void Update(float timeslice)
  188. {
  189. super.Update(timeslice);
  190. int mouseX, mouseY;
  191. GetMousePos(mouseX,mouseY);
  192. float dist = Math.Sqrt(Math.AbsFloat(mouseX - m_PrevMouseX) + Math.AbsFloat(mouseY - m_PrevMouseY));
  193. m_PrevMouseX = mouseX;
  194. m_PrevMouseY = mouseY;
  195. if (dist < 1 && m_CurrentHoverWidget && !m_HoverSuccessTriggered)
  196. {
  197. m_HoverTime += timeslice;
  198. if (m_HoverTime > 1)
  199. {
  200. HoverSuccess();
  201. }
  202. }
  203. if(dist > 1 && m_HoverSuccessTriggered)
  204. HoverInterrupt();
  205. if (GetGame() && GetUApi().GetInputByID(UAUIBack).LocalPress())
  206. {
  207. GetGame().GetUIManager().Back();
  208. }
  209. if (!GetGame().IsMultiplayer() && KeyState(KeyCode.KC_RCONTROL) && KeyState(KeyCode.KC_NUMPAD0) && m_HintWidgetRoot && m_HintWidgetRoot.IsVisible())
  210. {
  211. ClearKey(KeyCode.KC_NUMPAD0);
  212. m_EditTooltipRoot.Show(true);
  213. string text = GetMessage();
  214. if (text == NO_HINT_TEXT)
  215. text = "";
  216. m_HintInputText.SetText(text);
  217. }
  218. foreach (ScriptConsoleTabBase handler: m_TabHandlers)
  219. {
  220. handler.Update(timeslice);
  221. }
  222. }
  223. override bool OnMouseButtonDown(Widget w, int x, int y, int button)
  224. {
  225. super.OnMouseButtonDown(w,x,y,button);
  226. if (m_SelectedHandler.OnMouseButtonDown(w,x,y,button))
  227. return true;
  228. return false;
  229. }
  230. override bool OnClick(Widget w, int x, int y, int button)
  231. {
  232. super.OnClick(w, x, y, button);
  233. if (w == m_CloseConsoleButton)
  234. {
  235. Close();
  236. GetGame().GetMission().EnableAllInputs(true);
  237. return true;
  238. }
  239. else if (w == m_HintOkButton)
  240. {
  241. SetHintText(m_HintInputText.GetText(), m_CurrentHoverWidget);
  242. HoverInterrupt();
  243. m_EditTooltipRoot.Show(false);
  244. SaveData();
  245. }
  246. else if (w == m_HintCancelButton)
  247. {
  248. HoverInterrupt();
  249. m_EditTooltipRoot.Show(false);
  250. }
  251. else if (w == m_HintClearButton)
  252. {
  253. m_HintInputText.SetText("");
  254. }
  255. SelectTabByButton(w);
  256. if (m_SelectedHandler.OnClick(w,x,y,button))
  257. return true;
  258. return false;
  259. }
  260. override bool OnDoubleClick(Widget w, int x, int y, int button)
  261. {
  262. super.OnDoubleClick(w, x, y, button);
  263. if (m_SelectedHandler.OnDoubleClick(w,x,y,button))
  264. return true;
  265. return false;
  266. }
  267. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  268. {
  269. super.OnMouseLeave(w, enterW, x, y);
  270. if (!m_EditTooltipRoot.IsVisible())
  271. HoverInterrupt();
  272. return false;
  273. }
  274. override bool OnMouseEnter(Widget w, int x, int y)
  275. {
  276. super.OnMouseEnter(w, x, y);
  277. if (!m_EditTooltipRoot.IsVisible())
  278. m_CurrentHoverWidget = w;
  279. #ifdef PLATFORM_CONSOLE
  280. return false;
  281. #endif
  282. return true;
  283. }
  284. override bool OnChange(Widget w, int x, int y, bool finished)
  285. {
  286. super.OnChange(w, x, y, finished);
  287. if (m_SelectedHandler.OnChange(w,x,y,finished))
  288. return true;
  289. return false;
  290. }
  291. override bool OnItemSelected(Widget w, int x, int y, int row, int column, int oldRow, int oldColumn)
  292. {
  293. super.OnItemSelected(w, x, y, row, column, oldRow, oldColumn);
  294. if (m_SelectedHandler.OnItemSelected(w, x, y, row, column,oldRow, oldColumn))
  295. return true;
  296. return false;
  297. }
  298. ScriptConsoleTabBase GetTabHandler(typename tabType)
  299. {
  300. foreach (ScriptConsoleTabBase handler: m_TabHandlers)
  301. {
  302. if (tabType == handler.Type())
  303. {
  304. return handler;
  305. }
  306. }
  307. return null;
  308. }
  309. void SelectTabByID(int id)
  310. {
  311. ScriptConsoleTabBase tab = m_TabHandlersByID.Get(id);
  312. if (tab)
  313. SelectTab(tab);
  314. }
  315. void SelectTabByButton(Widget button)
  316. {
  317. ScriptConsoleTabBase tab = m_TabHandlers.Get(button);
  318. if (tab)
  319. SelectTab(tab);
  320. }
  321. void SelectTab(ScriptConsoleTabBase selectedHandler)
  322. {
  323. foreach (ScriptConsoleTabBase handler:m_TabHandlers)
  324. {
  325. handler.Select(handler == selectedHandler, selectedHandler);
  326. }
  327. m_SelectedHandler = selectedHandler;
  328. m_ConfigDebugProfile.SetTabSelected(selectedHandler.GetID());
  329. }
  330. protected void DisplayHint(string message)
  331. {
  332. if (message)
  333. {
  334. m_HintWidgetRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/script_console/script_console_hint.layout");
  335. m_HintWidgetBackground = ImageWidget.Cast(m_HintWidgetRoot.FindAnyWidget("Background"));
  336. m_HintWidget = RichTextWidget.Cast(m_HintWidgetRoot.FindAnyWidget("HintText"));
  337. m_HintWidgetRoot.Show(true);
  338. m_HintWidget.SetText(message);
  339. int offsetX = 0;
  340. int offsetY = 10;
  341. int screenW, screenH;
  342. GetScreenSize(screenW, screenH);
  343. int mouseX, mouseY;
  344. GetMousePos(mouseX,mouseY);
  345. float relativeX = mouseX / screenW;
  346. float relativeY = mouseY / screenH;
  347. int width, height;
  348. m_HintWidget.GetTextSize(width, height);
  349. if (relativeX > 0.8)
  350. offsetX = -width - offsetX;
  351. if (relativeY > 0.8)
  352. offsetY = -height - offsetY;
  353. m_HintWidgetRoot.SetPos(mouseX + offsetX ,mouseY + offsetY);
  354. m_HintWidgetBackground.SetScreenSize(width + 5, height + 5);
  355. }
  356. }
  357. override void OnRPCEx(int rpc_type, ParamsReadContext ctx)
  358. {
  359. super.OnRPCEx(rpc_type, ctx);
  360. #ifdef DIAG_DEVELOPER
  361. foreach (ScriptConsoleTabBase handler:m_TabHandlers)
  362. {
  363. handler.OnRPCEx(rpc_type,ctx);
  364. }
  365. #endif
  366. }
  367. PluginConfigDebugProfile m_ConfigDebugProfile;
  368. }
  369. class ScriptConsoleToolTipEventHandler : ScriptedWidgetEventHandler
  370. {
  371. reference string HintMessage;
  372. protected Widget m_Root;
  373. protected float m_HoverTime;
  374. protected bool m_HoverSuccessTriggered;
  375. protected Widget m_CurrentHoverWidget;
  376. protected Widget m_HintWidgetRoot;
  377. protected ImageWidget m_HintWidgetBackground;
  378. protected RichTextWidget m_HintWidget;
  379. protected ref Timer m_Timer;
  380. void OnWidgetScriptInit(Widget w)
  381. {
  382. m_Root = w;
  383. m_Root.SetHandler(this);
  384. m_Root.SetFlags(WidgetFlags.VEXACTPOS);
  385. }
  386. override bool OnMouseEnter(Widget w, int x, int y)
  387. {
  388. m_Timer = new Timer();
  389. m_Timer.Run(0.1, this, "Tick", NULL, true);
  390. m_CurrentHoverWidget = w;
  391. return true;
  392. }
  393. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  394. {
  395. HoverInterrupt();
  396. return true;
  397. }
  398. protected bool Tick()
  399. {
  400. if (!m_Root.IsVisibleHierarchy())
  401. HoverInterrupt();
  402. if (m_CurrentHoverWidget && !m_HoverSuccessTriggered)
  403. {
  404. m_HoverTime += 0.1;
  405. if (m_HoverTime > 1)
  406. {
  407. HoverSuccess();
  408. }
  409. }
  410. return true;
  411. }
  412. protected void DisplayHint(string message)
  413. {
  414. if (message)
  415. {
  416. m_HintWidgetRoot = GetGame().GetWorkspace().CreateWidgets("gui/layouts/script_console/script_console_hint.layout");
  417. m_HintWidgetBackground = ImageWidget.Cast(m_HintWidgetRoot.FindAnyWidget("Background"));
  418. m_HintWidget = RichTextWidget.Cast(m_HintWidgetRoot.FindAnyWidget("HintText"));
  419. m_HintWidgetRoot.Show(true);
  420. m_HintWidget.SetText(message);
  421. int offsetX = 0;
  422. int offsetY = 10;
  423. int screenW, screenH;
  424. GetScreenSize(screenW, screenH);
  425. int mouseX, mouseY;
  426. GetMousePos(mouseX,mouseY);
  427. float relativeX = mouseX / screenW;
  428. float relativeY = mouseY / screenH;
  429. int width, height;
  430. m_HintWidget.GetTextSize(width, height);
  431. if (relativeX > 0.8)
  432. offsetX = -width - offsetX;
  433. if (relativeY > 0.8)
  434. offsetY = -height - offsetY;
  435. m_HintWidgetRoot.SetPos(mouseX + offsetX ,mouseY + offsetY);
  436. m_HintWidgetBackground.SetScreenSize(width + 5, height + 5);
  437. }
  438. }
  439. protected void HideHint()
  440. {
  441. if (m_HintWidgetRoot)
  442. m_HintWidgetRoot.Show(false);
  443. }
  444. protected string GetMessage()
  445. {
  446. return HintMessage;
  447. }
  448. protected void HoverSuccess()
  449. {
  450. m_HoverSuccessTriggered = true;
  451. DisplayHint(GetMessage());
  452. }
  453. protected void HoverInterrupt()
  454. {
  455. m_Timer = null;
  456. m_HoverSuccessTriggered = false;
  457. m_CurrentHoverWidget = null;
  458. m_HoverTime = 0;
  459. HideHint();
  460. }
  461. }
  462. class JsonHintsData
  463. {
  464. ref map<int, string> WidgetHintBindings;
  465. }