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
|
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, SystemicApp, ui};
use crate::solar_system::{SolarSystem, SystemId};
use crate::wgpuctx::{RenderPassBuilder, SceneCtx, WgpuCtx};
use crate::eguictx::EguiCtx;
pub struct GameWindow
{
window: Arc<winit::window::Window>,
wgpuctx: WgpuCtx,
eguictx: EguiCtx,
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,
winit::dpi::LogicalPosition::new(0.0, 0.0),
winit::dpi::LogicalSize::new(1.0, 1.0));
let ui_state = ui::State{
topbar_sate: ui::topbar::TopBarState {
current_system : Some(0),
..Default::default()
},
camera_info: ui::camera_info::CameraWindowState {
target: 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, 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>)
-> Result<(), wgpu::SurfaceError> {
if !self.wgpuctx.is_ready() {
return Ok(());
}
if self.ui_state.topbar_sate.current_system.is_some() {
let game_state = game_state.borrow();
let current_system = &game_state.solar_systems()[self.ui_state.topbar_sate.current_system.unwrap()];
self.tactical_map.draw(
&self.wgpuctx,
current_system,
game_state.timeman())?;
}
let view = self.wgpuctx.prepare_surface(&wgpu::TextureViewDescriptor::default())?;
let mut scene = SceneCtx::from_view_default(&self.wgpuctx, &view, Some("Systemic window scene"));
{
let mut pass = RenderPassBuilder::new(Some("Systemic window render pass"), &view)
.clear_color(wgpu::Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 })
.build_from_scene(&mut scene);
//Draw the tactical map canvas.
self.tactical_map.present(&mut pass)?;
}
{
self.eguictx.prepare(&self.window);
self.ui_state.render(game_state, &self.eguictx);
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)
{
if self.eguictx.window_event(&self.window, event).consumed {
return;
}
}
pub fn resize(
&mut self,
width: u32,
height: u32
) {
if width > 0 && height > 0 {
self.wgpuctx.resize(width, height);
self.tactical_map.resize(&self.wgpuctx, width, height);
self.window.request_redraw();
}
}
pub fn size(
&self)
-> winit::dpi::PhysicalSize<u32> {
self.window.inner_size()
}
}
|