summaryrefslogtreecommitdiffstats
path: root/src/window.rs
blob: 3c395d5a4a837babf9aac8927ce4ba7500c1f659 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::cell::RefCell;
use std::sync::{Arc};
use std::time::Duration;

use winit::event::{ElementState, WindowEvent};
use winit::keyboard::KeyCode;

use crate::tacmap::TacticalMap;
use crate::{GameState, ui};
use crate::wgpuctx::{RenderPassBuilder, SceneCtx, WgpuCtx};
use crate::eguictx::EguiCtx;

pub struct GameWindow
{
    wgpuctx: WgpuCtx,
    eguictx: EguiCtx,

    window: Arc<winit::window::Window>,

    tactical_map: TacticalMap,

    ui_state: ui::State
}

impl GameWindow
{
    pub fn new(
        instance: &wgpu::Instance,
        event_loop: &winit::event_loop::ActiveEventLoop)
    -> Result<GameWindow, ()>
    {
        let window_attrs = winit::window::Window::default_attributes()
            .with_title("Systemic 4X")
            .with_inner_size(winit::dpi::LogicalSize::new(640, 480));

        let window = Arc::new(event_loop.create_window(window_attrs).unwrap());

        let wgpuctx = pollster::block_on(WgpuCtx::new(instance, window.clone()));
        let eguictx = EguiCtx::new(&window, &wgpuctx);

        let tacmap = TacticalMap::new(&wgpuctx);

        let ui_state = ui::State{
            camera_target: Some(0),
            topbar_sate: ui::topbar::TopBarState {
                current_system : Some(0),
                ..Default::default()
            },
            ..Default::default()
        };

        Ok(Self {
        window: window,
            wgpuctx: wgpuctx,
            eguictx: eguictx,
            tactical_map: tacmap,
            
            ui_state: ui_state
        })
    }
    
    pub fn update(
        &mut self,
        game_state: &RefCell<GameState>,
        dt: Duration)
    {
        let mut game_state = game_state.borrow_mut();
        if self.ui_state.topbar_sate.do_auto_tick {
            game_state.timeman.auto_tick = self.ui_state.topbar_sate.auto_tick;
        }else{
            game_state.timeman.auto_tick = None;
        }

        let current_system = match self.ui_state.topbar_sate.current_system {
            Some(id) => &game_state.solar_systems()[id],
            None => { return; }
        };

        self.tactical_map.update(
            current_system, 
            &mut self.ui_state, 
            game_state.timeman().seconds(),
            dt);
    }

    pub fn keyboard_input(
        &mut self,
        key_code: KeyCode,
        key_state: ElementState)
    {
        self.tactical_map.keyboard_input(key_code, key_state);
    }

    pub fn render(
        &mut self,
        game_state: &RefCell<GameState>,
        egui_event_resp: egui_winit::EventResponse)
    -> Result<(), wgpu::CurrentSurfaceTexture> {
        if !self.wgpuctx.is_ready() {
            return Ok(());
        }

        let screen_size = egui::vec2(
            self.wgpuctx.surface_config().width as f32,
            self.wgpuctx.surface_config().height as f32);
        
        let view = self.wgpuctx.prepare_surface(&wgpu::TextureViewDescriptor::default())?;
        let mut scene = SceneCtx::from_view_default(&self.wgpuctx, &view, Some("Systemic window scene"));

        self.eguictx.prepare(&self.window);
        
        egui::Window::new("Systemic 4X")
            .fixed_rect(egui::Rect::from_min_size(egui::pos2(0.0, 0.0), screen_size))
            .resizable(false)
            .collapsible(false)
            .title_bar(false)
            .interactable(false)
            .frame(egui::Frame::NONE)
            .show(self.eguictx.context(), |ui| {
            self.ui_state.paint(game_state, ui);

            if self.ui_state.topbar_sate.current_system.is_some() {
                let game_state = game_state.borrow();
                let fleets_manager = game_state.fleets();
                let current_system = &game_state.solar_systems()[self.ui_state.topbar_sate.current_system.unwrap()];
        
                match self.tactical_map.prepare(
                    &mut scene,
                    &self.wgpuctx,
                    fleets_manager,
                    current_system,
                    game_state.timeman()) 
                {
                    Ok(_) => {},
                    Err(_) => {
                        println!("Error in tactical map prepare");
                        panic!();
                    }
                }
        
                let mut pass = RenderPassBuilder::new(Some("Systemic window render pass"), &view)
                    .clear_color(wgpu::Color { r: 0.0, g: 0.0, b: 0.1, a: 1.0 })
                    .build_from_scene(&mut scene);
        
                //Draw the tactical map canvas.
                self.tactical_map.paint(&mut pass);
        
                self.tactical_map.paint_labels(
                    current_system,
                    game_state.timeman().seconds(),
                    screen_size, 
                    ui
                );
            }
        });
        self.eguictx.present(
            &self.window, 
            &self.wgpuctx, 
            scene.encoder_mut(), 
            &view);

        scene.submit(&self.wgpuctx);
        self.wgpuctx.present_surface();
        self.window.request_redraw();

        Ok(())
    }

    pub fn on_event(
        &mut self,
        event: &WindowEvent)
    -> egui_winit::EventResponse
    {
        self.eguictx.window_event(&self.window, event)
    }

    pub fn resize(
        &mut self,
        width: u32,
        height: u32
    ) {
        if width > 0 && height > 0 {
            self.wgpuctx.resize(width, height);
            self.tactical_map.resize(width, height);
            self.window.request_redraw();
        }
    }

    pub fn size(
        &self)
    -> winit::dpi::PhysicalSize<u32> {
        self.window.inner_size()
    }
}