You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整macroquad文本输入框大小?实现全屏宽度输入

解决Macroquad中输入框宽度适配屏幕的问题

要让input_text占满编辑器窗口的宽度,核心是利用Macroquad UI的set_next_widget_size方法强制指定输入框尺寸,具体实现如下:

修改方案

调整draw_editor函数,在调用输入框组件前设置其尺寸:

fn draw_editor(&mut self) {
    let window_id = hash!();
    root_ui().window(
        window_id,
        vec2(0.0, EDITOR_TOP_MARGIN),
        vec2(screen_width(), EDITOR_HEIGHT),
        |ui| {
            let input_text_id = hash!();
            
            // 设置输入框占满窗口可用宽度,高度预留边距避免超出
            ui.set_next_widget_size(vec2(ui.available_size().x, EDITOR_HEIGHT - 20.0));
            
            ui.input_text(input_text_id, "", &mut self.editor_content);

            // Focus the editor when a cell is selected
            if self.selected_cell.is_some() {
                ui.set_input_focus(input_text_id);
            } else {
                ui.set_input_focus(hash!());
            }

            if is_key_pressed(KeyCode::Enter) {
                self.commit_editor();
                self.selected_cell = None;
                self.editor_content.clear();
            }
        },
    );
}

逻辑说明

  • ui.available_size()会自动计算窗口去掉默认padding后的可用空间,确保输入框不会超出编辑器区域边界。
  • 高度减去20.0是为了预留上下边距,避免输入框和窗口边缘贴紧,可根据视觉需求调整该数值。
  • set_next_widget_size会作用于下一个创建的UI组件,强制覆盖input_text的默认固定尺寸。

内容的提问来源于stack exchange,提问作者Doruk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:37:03