quakeball-rust/rust/src/ui/cmd.rs

71 lines
1.8 KiB
Rust

use godot::classes::ScrollContainer;
use godot::prelude::*;
use godot::{
classes::{IPanel, Label, Panel, TextEdit},
global::godot_print,
obj::Base,
register::{GodotClass, godot_api},
};
#[derive(GodotClass)]
#[class(base=Panel)]
struct CommandLinePanel {
#[export]
pub input_field: Option<Gd<TextEdit>>,
#[export]
pub text_field: Option<Gd<Label>>,
#[export]
pub scroll_container: Option<Gd<ScrollContainer>>,
scroll_next_update: bool,
base: Base<Panel>,
}
#[godot_api]
impl IPanel for CommandLinePanel {
fn init(base: Base<Panel>) -> Self {
CommandLinePanel {
input_field: None,
text_field: None,
scroll_container: None,
scroll_next_update: false,
base,
}
}
fn process(&mut self, _delta: f64) {
if let Some(scroll) = &mut self.scroll_container
&& self.scroll_next_update
{
scroll.set_v_scroll(i32::MAX);
self.scroll_next_update = false;
}
if let Some(input) = self.input_field.clone() {
let text = input.get_text();
if text.contains("\n") {
let parts = text.split("\n");
let first = parts.get(0).unwrap();
self.publish_message(first.to_string());
self.input_field.as_mut().unwrap().clear();
self.scroll_next_update = true;
}
}
}
}
impl CommandLinePanel {
pub fn publish_message(&mut self, message: String) {
if let Some(field) = &mut self.text_field {
let mut text = field.get_text().to_string();
if !text.is_empty() {
text += "\n";
}
text += &message;
field.set_text(&text);
}
}
}