Skip to main content

base_ui/widget/
mod.rs

1pub mod widgets;
2
3use crate::animation::animation::{ Vec2Animation, FadeAnimation };
4use crate::graphics::Renderer;
5use crate::style::color::Color;
6use nalgebra_glm as glm;
7
8pub trait Widget {
9    // 위젯 정보 반환
10    fn get_position(&self) -> (f32, f32);
11    fn get_size(&self) -> (f32, f32);
12    fn get_background_color(&self) -> Color;
13    fn get_text_color(&self) -> Color;
14    fn get_hover_background_color(&self) -> Color;
15    fn get_hover_text_color(&self) -> Color;
16    fn get_opacity(&self) -> f32;
17    fn get_is_hovered(&self) -> bool;
18    fn get_is_pressed(&self) -> bool;
19
20    fn draw(&self, renderer: &mut Renderer, screen_width: f32, screen_height: f32);
21    fn set_position(&mut self, x: f32, y: f32);
22    fn set_size(&mut self, width: f32, height: f32);
23    fn position(&self) -> (f32, f32);
24    fn size(&self) -> (f32, f32);
25
26    // 클릭 이벤트 핸들러 설정
27    fn set_on_click<F>(&mut self, _callback: F) where F: FnMut() + 'static {
28        // 기본 구현은 아무것도 하지 않음
29    }
30
31    // hover 이벤트 핸들러 설정
32    fn set_on_hover<F>(&mut self, _callback: F) where F: FnMut(bool) + 'static {
33        // 기본 구현은 아무것도 하지 않음
34    }
35
36    // 새로운 마우스 이벤트 처리 메서드들
37    fn on_mouse_press(&mut self, _x: f32, _y: f32) -> bool {
38        false // 기본적으로는 이벤트를 처리하지 않음
39    }
40
41    fn on_mouse_release(&mut self, _x: f32, _y: f32) -> bool {
42        false
43    }
44
45    // 마우스 포인터가 위젯 영역 내에 있는지 확인하는 헬퍼 메서드
46    fn contains_point(&self, x: f32, y: f32) -> bool {
47        let (widget_x, widget_y) = self.position();
48        let (width, height) = self.size();
49
50        x >= widget_x && x <= widget_x + width && y >= widget_y && y <= widget_y + height
51    }
52
53    fn animate_position(&mut self, target_x: f32, target_y: f32, duration: f32) {
54        let current_pos = self.position();
55        let start = glm::vec2(current_pos.0, current_pos.1);
56        let end = glm::vec2(target_x, target_y);
57        self.set_position_animation(Vec2Animation::new(start, end, duration));
58    }
59
60    fn animate_fade(&mut self, start: f32, end: f32, duration: f32) {
61        self.set_fade_animation(FadeAnimation::new(start, end, duration));
62    }
63
64    fn set_position_animation(&mut self, animation: Vec2Animation);
65    fn set_fade_animation(&mut self, animation: FadeAnimation);
66    fn update_animations(&mut self, delta_time: f32);
67
68    fn resize_by_window_size(&mut self, scale_x: f32, scale_y: f32) {
69        let (old_width, old_height) = self.size();
70        let (old_x, old_y) = self.position();
71
72        // 위치와 크기를 화면 크기에 대한 비율로 유지
73        let relative_x = old_x / (1.0 / scale_x);
74        let relative_y = old_y / (1.0 / scale_y);
75        let relative_width = old_width / (1.0 / scale_x);
76        let relative_height = old_height / (1.0 / scale_y);
77
78        self.set_position(relative_x, relative_y);
79        self.set_size(relative_width, relative_height);
80    }
81}