clat_gui 0.1.3

High-performance, cross-platform Rust desktop GUI framework.
Documentation
use serde::{Serialize, Deserialize};
use wgpu::{Device, Queue, RenderPass};
use winit::event::MouseEvent;

// 按钮状态枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonState {
    Normal,
    Hovered,
    Pressed,
}

// 按钮组件结构体
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Button {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
    pub text: String,
    pub state: ButtonState,
    pub on_click: Option<Box<dyn Fn() + Send + Sync>>,
}

impl Button {
    // 创建新的按钮实例
    pub fn new(x: f32, y: f32, width: f32, height: f32, text: &str) -> Self {
        Self {
            x,
            y,
            width,
            height,
            text: text.to_string(),
            state: ButtonState::Normal,
            on_click: None,
        }
    }
    
    // 设置点击回调函数
    pub fn set_on_click<F: Fn() + Send + Sync + 'static>(&mut self, callback: F) {
        self.on_click = Some(Box::new(callback));
    }
    
    // 处理鼠标事件
    pub fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
        match event {
            MouseEvent::CursorMoved { position, .. } => {
                let (x, y) = (position.x as f32, position.y as f32);
                let is_inside = x >= self.x && x <= self.x + self.width && 
                              y >= self.y && y <= self.y + self.height;
                
                if is_inside {
                    if self.state != ButtonState::Pressed {
                        self.state = ButtonState::Hovered;
                    }
                } else {
                    self.state = ButtonState::Normal;
                }
                is_inside
            },
            MouseEvent::ButtonPressed { .. } => {
                if self.state == ButtonState::Hovered {
                    self.state = ButtonState::Pressed;
                    true
                } else {
                    false
                }
            },
            MouseEvent::ButtonReleased { .. } => {
                let was_pressed = self.state == ButtonState::Pressed;
                if was_pressed {
                    self.state = ButtonState::Hovered;
                    if let Some(callback) = self.on_click.as_ref() {
                        callback();
                    }
                }
                was_pressed
            },
            _ => false,
        }
    }
    
    // 渲染按钮
    pub fn render(&self, device: &Device, queue: &Queue, render_pass: &mut RenderPass) {
        // TODO: 实现实际的按钮渲染逻辑
        // 这里应该绘制按钮的背景、边框和文本
        
        // 根据按钮状态选择不同的颜色
        let bg_color = match self.state {
            ButtonState::Normal => [0.2, 0.4, 0.6, 1.0],
            ButtonState::Hovered => [0.3, 0.5, 0.7, 1.0],
            ButtonState::Pressed => [0.1, 0.3, 0.5, 1.0],
        };
        
        // 这里应该使用wgpu API绘制矩形和文本
        // 简化版本中,我们只是打印信息表示按钮被渲染
        // 实际实现中需要使用顶点缓冲区、索引缓冲区和着色器
    }
}