| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- use crate::scripting::ui_ext::{
- ScriptUiPanel, ScriptUiWidget, ScriptUiWidgetKind, UiAnchor, UiEvent, UiExtensionRegistry,
- UiScreenKind, UiSurface, UiWidgetValue,
- };
- fn render_widget(
- ui: &mut egui::Ui,
- script_id: &str,
- panel_id: &str,
- w: &mut ScriptUiWidget,
- events: &mut Vec<UiEvent>,
- compact: bool,
- ) {
- match &mut w.kind {
- ScriptUiWidgetKind::Heading { text } => {
- if compact {
- ui.strong(text.as_str());
- } else {
- ui.heading(text.as_str());
- }
- }
- ScriptUiWidgetKind::Label { text, color, bold } => {
- let mut rt = egui::RichText::new(text.as_str());
- if let Some([r, g, b]) = *color {
- rt = rt.color(egui::Color32::from_rgb(r, g, b));
- }
- if *bold {
- rt = rt.strong();
- }
- ui.label(rt);
- }
- ScriptUiWidgetKind::Button { label, accent } => {
- let btn = if let Some([r, g, b]) = *accent {
- egui::Button::new(
- egui::RichText::new(label.as_str())
- .color(egui::Color32::from_rgb(r, g, b))
- .strong(),
- )
- } else {
- egui::Button::new(label.as_str())
- };
- if ui.add(btn).clicked() {
- events.push(UiEvent::Click {
- script_id: script_id.to_string(),
- panel_id: panel_id.to_string(),
- widget_id: w.id.clone(),
- });
- }
- }
- ScriptUiWidgetKind::Checkbox { label, value } => {
- if ui.checkbox(value, label.as_str()).changed() {
- events.push(UiEvent::Changed {
- script_id: script_id.to_string(),
- panel_id: panel_id.to_string(),
- widget_id: w.id.clone(),
- value: UiWidgetValue::Bool(*value),
- });
- }
- }
- ScriptUiWidgetKind::Slider {
- label,
- value,
- min,
- max,
- } => {
- let lo = (*min).min(*max);
- let hi = (*min).max(*max);
- let resp = if compact {
- ui.add(
- egui::DragValue::new(value)
- .range(lo..=hi)
- .speed(((hi - lo) * 0.01).max(0.05))
- .prefix(format!("{label}: ")),
- )
- } else {
- ui.add(egui::Slider::new(value, lo..=hi).text(label.as_str()))
- };
- if resp.changed() {
- events.push(UiEvent::Changed {
- script_id: script_id.to_string(),
- panel_id: panel_id.to_string(),
- widget_id: w.id.clone(),
- value: UiWidgetValue::Float(*value),
- });
- }
- }
- ScriptUiWidgetKind::TextInput { label, value, hint } => {
- let resp = ui.horizontal(|ui| {
- if !label.is_empty() {
- ui.label(label.as_str());
- }
- let mut edit = egui::TextEdit::singleline(value);
- if compact {
- edit = edit.desired_width(96.0);
- } else {
- edit = edit.desired_width(140.0);
- }
- if !hint.is_empty() {
- edit = edit.hint_text(hint.as_str());
- }
- ui.add(edit)
- });
- if resp.inner.changed() {
- events.push(UiEvent::Changed {
- script_id: script_id.to_string(),
- panel_id: panel_id.to_string(),
- widget_id: w.id.clone(),
- value: UiWidgetValue::Str(value.clone()),
- });
- }
- }
- ScriptUiWidgetKind::ProgressBar {
- label,
- fraction,
- text,
- } => {
- if !label.is_empty() {
- ui.small(label.as_str());
- }
- let mut bar = egui::ProgressBar::new(fraction.clamp(0.0, 1.0));
- if !text.is_empty() {
- bar = bar.text(text.as_str());
- } else {
- bar = bar.show_percentage();
- }
- if compact {
- ui.add_sized([90.0, 16.0], bar);
- } else {
- ui.add(bar);
- }
- }
- ScriptUiWidgetKind::ComboBox {
- label,
- options,
- selected,
- } => {
- if options.is_empty() {
- return;
- }
- if *selected >= options.len() {
- *selected = 0;
- }
- let prev = *selected;
- let cur_txt = options[*selected].as_str();
- let id_salt = ("script_ui_combo", script_id, panel_id, w.id.as_str());
- ui.horizontal(|ui| {
- if !compact && !label.is_empty() {
- ui.label(label.as_str());
- }
- egui::ComboBox::from_id_salt(id_salt)
- .selected_text(cur_txt)
- .show_ui(ui, |ui| {
- for (idx, opt) in options.iter().enumerate() {
- ui.selectable_value(selected, idx, opt.as_str());
- }
- });
- });
- if *selected != prev {
- if let Some(val_str) = options.get(*selected) {
- events.push(UiEvent::Changed {
- script_id: script_id.to_string(),
- panel_id: panel_id.to_string(),
- widget_id: w.id.clone(),
- value: UiWidgetValue::Str(val_str.clone()),
- });
- }
- }
- }
- ScriptUiWidgetKind::Separator => {
- ui.separator();
- }
- ScriptUiWidgetKind::RowBegin | ScriptUiWidgetKind::RowEnd => {}
- }
- }
- fn draw_panel_body(ui: &mut egui::Ui, panel: &mut ScriptUiPanel, events: &mut Vec<UiEvent>) {
- let script_id = panel.owner_script_id.clone();
- let panel_id = panel.id.clone();
- let mut i = 0;
- while i < panel.widgets.len() {
- if matches!(panel.widgets[i].kind, ScriptUiWidgetKind::RowBegin) {
- let start = i + 1;
- let mut end = start;
- while end < panel.widgets.len()
- && !matches!(panel.widgets[end].kind, ScriptUiWidgetKind::RowEnd)
- {
- end += 1;
- }
- ui.horizontal_wrapped(|ui| {
- for w in &mut panel.widgets[start..end] {
- render_widget(ui, &script_id, &panel_id, w, events, true);
- }
- });
- i = if end < panel.widgets.len() { end + 1 } else { end };
- } else {
- render_widget(
- ui,
- &script_id,
- &panel_id,
- &mut panel.widgets[i],
- events,
- false,
- );
- i += 1;
- }
- }
- }
- /// Draws script-injected UI inside an existing toolbar or side panel (`EditorToolbar`,
- /// `EditorLeft`, `EditorInspector`, `GmToolbar`, `GmPanel`, or `TacticalBar`).
- pub fn draw_script_ui_inline(
- ui: &mut egui::Ui,
- registry: &mut UiExtensionRegistry,
- surface: UiSurface,
- screen: UiScreenKind,
- ) -> Vec<UiEvent> {
- let mut events = Vec::new();
- let toolbar = surface.is_toolbar();
- for panel in &mut registry.panels {
- if panel.surface != surface || !panel.open || !panel.screen_filter.matches(screen) {
- continue;
- }
- if panel.widgets.is_empty() {
- continue;
- }
- if toolbar {
- ui.separator();
- let script_id = panel.owner_script_id.clone();
- let panel_id = panel.id.clone();
- for w in &mut panel.widgets {
- render_widget(ui, &script_id, &panel_id, w, &mut events, true);
- }
- } else {
- ui.separator();
- egui::CollapsingHeader::new(
- egui::RichText::new(&panel.title)
- .color(egui::Color32::from_rgb(135, 215, 250))
- .strong(),
- )
- .id_salt(("script_panel", &panel.owner_script_id, &panel.id))
- .default_open(true)
- .show(ui, |ui| {
- draw_panel_body(ui, panel, &mut events);
- });
- }
- }
- events
- }
- /// Draws floating script windows (`UiSurface::Window`), HUD overlay cards (`UiSurface::HudOverlay`),
- /// and active `cw.ui.toast(...)` notifications across any live screen.
- pub fn draw_script_ui_overlays_and_windows(
- ctx: &egui::Context,
- registry: &mut UiExtensionRegistry,
- screen: UiScreenKind,
- ) -> Vec<UiEvent> {
- let mut events = Vec::new();
- for panel in &mut registry.panels {
- if !panel.open || !panel.screen_filter.matches(screen) {
- continue;
- }
- match panel.surface {
- UiSurface::HudOverlay => {
- let area_id = egui::Id::new(("script_hud_area", &panel.owner_script_id, &panel.id));
- let mut area = egui::Area::new(area_id).order(egui::Order::Middle);
- area = match panel.anchor {
- UiAnchor::TopLeft => area.anchor(egui::Align2::LEFT_TOP, [14.0, 52.0]),
- UiAnchor::TopCenter => area.anchor(egui::Align2::CENTER_TOP, [0.0, 52.0]),
- UiAnchor::TopRight => area.anchor(egui::Align2::RIGHT_TOP, [-14.0, 52.0]),
- UiAnchor::BottomLeft => area.anchor(egui::Align2::LEFT_BOTTOM, [14.0, -52.0]),
- UiAnchor::BottomCenter => {
- area.anchor(egui::Align2::CENTER_BOTTOM, [0.0, -52.0])
- }
- UiAnchor::BottomRight => {
- area.anchor(egui::Align2::RIGHT_BOTTOM, [-14.0, -52.0])
- }
- UiAnchor::Fixed([x, y]) => area.fixed_pos([x, y]),
- };
- area.show(ctx, |ui| {
- egui::Frame::window(&ctx.style())
- .fill(egui::Color32::from_rgba_unmultiplied(16, 21, 27, 215))
- .stroke(egui::Stroke::new(
- 1.0_f32,
- egui::Color32::from_rgba_unmultiplied(95, 195, 245, 150),
- ))
- .show(ui, |ui| {
- ui.set_min_width(panel.default_size[0].min(360.0));
- if !panel.title.is_empty() {
- ui.strong(
- egui::RichText::new(&panel.title)
- .color(egui::Color32::from_rgb(135, 215, 250)),
- );
- ui.separator();
- }
- draw_panel_body(ui, panel, &mut events);
- });
- });
- }
- UiSurface::Window => {
- let win_id = egui::Id::new(("script_win", &panel.owner_script_id, &panel.id));
- let mut win = egui::Window::new(&panel.title)
- .id(win_id)
- .default_size(panel.default_size)
- .resizable(true);
- let mut open_flag = panel.open;
- if panel.closable {
- win = win.open(&mut open_flag);
- }
- win.show(ctx, |ui| {
- egui::ScrollArea::vertical().show(ui, |ui| {
- draw_panel_body(ui, panel, &mut events);
- });
- });
- if panel.closable && !open_flag {
- panel.open = false;
- events.push(UiEvent::Closed {
- script_id: panel.owner_script_id.clone(),
- panel_id: panel.id.clone(),
- });
- }
- }
- _ => {}
- }
- }
- if !registry.toasts.is_empty() {
- egui::Area::new(egui::Id::new("script_ui_toasts_tc"))
- .anchor(egui::Align2::CENTER_TOP, [0.0, 96.0])
- .interactable(false)
- .order(egui::Order::Foreground)
- .show(ctx, |ui| {
- ui.vertical_centered(|ui| {
- for (txt, _) in ®istry.toasts {
- egui::Frame::window(&ctx.style())
- .fill(egui::Color32::from_rgba_unmultiplied(18, 28, 38, 232))
- .stroke(egui::Stroke::new(
- 1.2_f32,
- egui::Color32::from_rgb(100, 210, 255),
- ))
- .show(ui, |ui| {
- ui.label(
- egui::RichText::new(txt.as_str())
- .strong()
- .color(egui::Color32::from_rgb(230, 245, 255)),
- );
- });
- }
- });
- });
- }
- events
- }
|