script_ui.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. use crate::scripting::ui_ext::{
  2. ScriptUiPanel, ScriptUiWidget, ScriptUiWidgetKind, UiAnchor, UiEvent, UiExtensionRegistry,
  3. UiScreenKind, UiSurface, UiWidgetValue,
  4. };
  5. fn render_widget(
  6. ui: &mut egui::Ui,
  7. script_id: &str,
  8. panel_id: &str,
  9. w: &mut ScriptUiWidget,
  10. events: &mut Vec<UiEvent>,
  11. compact: bool,
  12. ) {
  13. match &mut w.kind {
  14. ScriptUiWidgetKind::Heading { text } => {
  15. if compact {
  16. ui.strong(text.as_str());
  17. } else {
  18. ui.heading(text.as_str());
  19. }
  20. }
  21. ScriptUiWidgetKind::Label { text, color, bold } => {
  22. let mut rt = egui::RichText::new(text.as_str());
  23. if let Some([r, g, b]) = *color {
  24. rt = rt.color(egui::Color32::from_rgb(r, g, b));
  25. }
  26. if *bold {
  27. rt = rt.strong();
  28. }
  29. ui.label(rt);
  30. }
  31. ScriptUiWidgetKind::Button { label, accent } => {
  32. let btn = if let Some([r, g, b]) = *accent {
  33. egui::Button::new(
  34. egui::RichText::new(label.as_str())
  35. .color(egui::Color32::from_rgb(r, g, b))
  36. .strong(),
  37. )
  38. } else {
  39. egui::Button::new(label.as_str())
  40. };
  41. if ui.add(btn).clicked() {
  42. events.push(UiEvent::Click {
  43. script_id: script_id.to_string(),
  44. panel_id: panel_id.to_string(),
  45. widget_id: w.id.clone(),
  46. });
  47. }
  48. }
  49. ScriptUiWidgetKind::Checkbox { label, value } => {
  50. if ui.checkbox(value, label.as_str()).changed() {
  51. events.push(UiEvent::Changed {
  52. script_id: script_id.to_string(),
  53. panel_id: panel_id.to_string(),
  54. widget_id: w.id.clone(),
  55. value: UiWidgetValue::Bool(*value),
  56. });
  57. }
  58. }
  59. ScriptUiWidgetKind::Slider {
  60. label,
  61. value,
  62. min,
  63. max,
  64. } => {
  65. let lo = (*min).min(*max);
  66. let hi = (*min).max(*max);
  67. let resp = if compact {
  68. ui.add(
  69. egui::DragValue::new(value)
  70. .range(lo..=hi)
  71. .speed(((hi - lo) * 0.01).max(0.05))
  72. .prefix(format!("{label}: ")),
  73. )
  74. } else {
  75. ui.add(egui::Slider::new(value, lo..=hi).text(label.as_str()))
  76. };
  77. if resp.changed() {
  78. events.push(UiEvent::Changed {
  79. script_id: script_id.to_string(),
  80. panel_id: panel_id.to_string(),
  81. widget_id: w.id.clone(),
  82. value: UiWidgetValue::Float(*value),
  83. });
  84. }
  85. }
  86. ScriptUiWidgetKind::TextInput { label, value, hint } => {
  87. let resp = ui.horizontal(|ui| {
  88. if !label.is_empty() {
  89. ui.label(label.as_str());
  90. }
  91. let mut edit = egui::TextEdit::singleline(value);
  92. if compact {
  93. edit = edit.desired_width(96.0);
  94. } else {
  95. edit = edit.desired_width(140.0);
  96. }
  97. if !hint.is_empty() {
  98. edit = edit.hint_text(hint.as_str());
  99. }
  100. ui.add(edit)
  101. });
  102. if resp.inner.changed() {
  103. events.push(UiEvent::Changed {
  104. script_id: script_id.to_string(),
  105. panel_id: panel_id.to_string(),
  106. widget_id: w.id.clone(),
  107. value: UiWidgetValue::Str(value.clone()),
  108. });
  109. }
  110. }
  111. ScriptUiWidgetKind::ProgressBar {
  112. label,
  113. fraction,
  114. text,
  115. } => {
  116. if !label.is_empty() {
  117. ui.small(label.as_str());
  118. }
  119. let mut bar = egui::ProgressBar::new(fraction.clamp(0.0, 1.0));
  120. if !text.is_empty() {
  121. bar = bar.text(text.as_str());
  122. } else {
  123. bar = bar.show_percentage();
  124. }
  125. if compact {
  126. ui.add_sized([90.0, 16.0], bar);
  127. } else {
  128. ui.add(bar);
  129. }
  130. }
  131. ScriptUiWidgetKind::ComboBox {
  132. label,
  133. options,
  134. selected,
  135. } => {
  136. if options.is_empty() {
  137. return;
  138. }
  139. if *selected >= options.len() {
  140. *selected = 0;
  141. }
  142. let prev = *selected;
  143. let cur_txt = options[*selected].as_str();
  144. let id_salt = ("script_ui_combo", script_id, panel_id, w.id.as_str());
  145. ui.horizontal(|ui| {
  146. if !compact && !label.is_empty() {
  147. ui.label(label.as_str());
  148. }
  149. egui::ComboBox::from_id_salt(id_salt)
  150. .selected_text(cur_txt)
  151. .show_ui(ui, |ui| {
  152. for (idx, opt) in options.iter().enumerate() {
  153. ui.selectable_value(selected, idx, opt.as_str());
  154. }
  155. });
  156. });
  157. if *selected != prev {
  158. if let Some(val_str) = options.get(*selected) {
  159. events.push(UiEvent::Changed {
  160. script_id: script_id.to_string(),
  161. panel_id: panel_id.to_string(),
  162. widget_id: w.id.clone(),
  163. value: UiWidgetValue::Str(val_str.clone()),
  164. });
  165. }
  166. }
  167. }
  168. ScriptUiWidgetKind::Separator => {
  169. ui.separator();
  170. }
  171. ScriptUiWidgetKind::RowBegin | ScriptUiWidgetKind::RowEnd => {}
  172. }
  173. }
  174. fn draw_panel_body(ui: &mut egui::Ui, panel: &mut ScriptUiPanel, events: &mut Vec<UiEvent>) {
  175. let script_id = panel.owner_script_id.clone();
  176. let panel_id = panel.id.clone();
  177. let mut i = 0;
  178. while i < panel.widgets.len() {
  179. if matches!(panel.widgets[i].kind, ScriptUiWidgetKind::RowBegin) {
  180. let start = i + 1;
  181. let mut end = start;
  182. while end < panel.widgets.len()
  183. && !matches!(panel.widgets[end].kind, ScriptUiWidgetKind::RowEnd)
  184. {
  185. end += 1;
  186. }
  187. ui.horizontal_wrapped(|ui| {
  188. for w in &mut panel.widgets[start..end] {
  189. render_widget(ui, &script_id, &panel_id, w, events, true);
  190. }
  191. });
  192. i = if end < panel.widgets.len() { end + 1 } else { end };
  193. } else {
  194. render_widget(
  195. ui,
  196. &script_id,
  197. &panel_id,
  198. &mut panel.widgets[i],
  199. events,
  200. false,
  201. );
  202. i += 1;
  203. }
  204. }
  205. }
  206. /// Draws script-injected UI inside an existing toolbar or side panel (`EditorToolbar`,
  207. /// `EditorLeft`, `EditorInspector`, `GmToolbar`, `GmPanel`, or `TacticalBar`).
  208. pub fn draw_script_ui_inline(
  209. ui: &mut egui::Ui,
  210. registry: &mut UiExtensionRegistry,
  211. surface: UiSurface,
  212. screen: UiScreenKind,
  213. ) -> Vec<UiEvent> {
  214. let mut events = Vec::new();
  215. let toolbar = surface.is_toolbar();
  216. for panel in &mut registry.panels {
  217. if panel.surface != surface || !panel.open || !panel.screen_filter.matches(screen) {
  218. continue;
  219. }
  220. if panel.widgets.is_empty() {
  221. continue;
  222. }
  223. if toolbar {
  224. ui.separator();
  225. let script_id = panel.owner_script_id.clone();
  226. let panel_id = panel.id.clone();
  227. for w in &mut panel.widgets {
  228. render_widget(ui, &script_id, &panel_id, w, &mut events, true);
  229. }
  230. } else {
  231. ui.separator();
  232. egui::CollapsingHeader::new(
  233. egui::RichText::new(&panel.title)
  234. .color(egui::Color32::from_rgb(135, 215, 250))
  235. .strong(),
  236. )
  237. .id_salt(("script_panel", &panel.owner_script_id, &panel.id))
  238. .default_open(true)
  239. .show(ui, |ui| {
  240. draw_panel_body(ui, panel, &mut events);
  241. });
  242. }
  243. }
  244. events
  245. }
  246. /// Draws floating script windows (`UiSurface::Window`), HUD overlay cards (`UiSurface::HudOverlay`),
  247. /// and active `cw.ui.toast(...)` notifications across any live screen.
  248. pub fn draw_script_ui_overlays_and_windows(
  249. ctx: &egui::Context,
  250. registry: &mut UiExtensionRegistry,
  251. screen: UiScreenKind,
  252. ) -> Vec<UiEvent> {
  253. let mut events = Vec::new();
  254. for panel in &mut registry.panels {
  255. if !panel.open || !panel.screen_filter.matches(screen) {
  256. continue;
  257. }
  258. match panel.surface {
  259. UiSurface::HudOverlay => {
  260. let area_id = egui::Id::new(("script_hud_area", &panel.owner_script_id, &panel.id));
  261. let mut area = egui::Area::new(area_id).order(egui::Order::Middle);
  262. area = match panel.anchor {
  263. UiAnchor::TopLeft => area.anchor(egui::Align2::LEFT_TOP, [14.0, 52.0]),
  264. UiAnchor::TopCenter => area.anchor(egui::Align2::CENTER_TOP, [0.0, 52.0]),
  265. UiAnchor::TopRight => area.anchor(egui::Align2::RIGHT_TOP, [-14.0, 52.0]),
  266. UiAnchor::BottomLeft => area.anchor(egui::Align2::LEFT_BOTTOM, [14.0, -52.0]),
  267. UiAnchor::BottomCenter => {
  268. area.anchor(egui::Align2::CENTER_BOTTOM, [0.0, -52.0])
  269. }
  270. UiAnchor::BottomRight => {
  271. area.anchor(egui::Align2::RIGHT_BOTTOM, [-14.0, -52.0])
  272. }
  273. UiAnchor::Fixed([x, y]) => area.fixed_pos([x, y]),
  274. };
  275. area.show(ctx, |ui| {
  276. egui::Frame::window(&ctx.style())
  277. .fill(egui::Color32::from_rgba_unmultiplied(16, 21, 27, 215))
  278. .stroke(egui::Stroke::new(
  279. 1.0_f32,
  280. egui::Color32::from_rgba_unmultiplied(95, 195, 245, 150),
  281. ))
  282. .show(ui, |ui| {
  283. ui.set_min_width(panel.default_size[0].min(360.0));
  284. if !panel.title.is_empty() {
  285. ui.strong(
  286. egui::RichText::new(&panel.title)
  287. .color(egui::Color32::from_rgb(135, 215, 250)),
  288. );
  289. ui.separator();
  290. }
  291. draw_panel_body(ui, panel, &mut events);
  292. });
  293. });
  294. }
  295. UiSurface::Window => {
  296. let win_id = egui::Id::new(("script_win", &panel.owner_script_id, &panel.id));
  297. let mut win = egui::Window::new(&panel.title)
  298. .id(win_id)
  299. .default_size(panel.default_size)
  300. .resizable(true);
  301. let mut open_flag = panel.open;
  302. if panel.closable {
  303. win = win.open(&mut open_flag);
  304. }
  305. win.show(ctx, |ui| {
  306. egui::ScrollArea::vertical().show(ui, |ui| {
  307. draw_panel_body(ui, panel, &mut events);
  308. });
  309. });
  310. if panel.closable && !open_flag {
  311. panel.open = false;
  312. events.push(UiEvent::Closed {
  313. script_id: panel.owner_script_id.clone(),
  314. panel_id: panel.id.clone(),
  315. });
  316. }
  317. }
  318. _ => {}
  319. }
  320. }
  321. if !registry.toasts.is_empty() {
  322. egui::Area::new(egui::Id::new("script_ui_toasts_tc"))
  323. .anchor(egui::Align2::CENTER_TOP, [0.0, 96.0])
  324. .interactable(false)
  325. .order(egui::Order::Foreground)
  326. .show(ctx, |ui| {
  327. ui.vertical_centered(|ui| {
  328. for (txt, _) in &registry.toasts {
  329. egui::Frame::window(&ctx.style())
  330. .fill(egui::Color32::from_rgba_unmultiplied(18, 28, 38, 232))
  331. .stroke(egui::Stroke::new(
  332. 1.2_f32,
  333. egui::Color32::from_rgb(100, 210, 255),
  334. ))
  335. .show(ui, |ui| {
  336. ui.label(
  337. egui::RichText::new(txt.as_str())
  338. .strong()
  339. .color(egui::Color32::from_rgb(230, 245, 255)),
  340. );
  341. });
  342. }
  343. });
  344. });
  345. }
  346. events
  347. }