metropolis 0.9.1

A high level easy to use graphics renderer
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use crate::setup::*;
use crate::vertex::*;
use std::sync::Arc;
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
use vulkano::command_buffer::{AutoCommandBufferBuilder,AutoCommandBuffer};
use vulkano::swapchain;
use vulkano::swapchain::{AcquireError, SwapchainCreationError};
use vulkano::sync;
use vulkano::sync::{FlushError, GpuFuture};
use winit::{ElementState,KeyboardInput,Event, WindowEvent,VirtualKeyCode,ModifiersState};
use winit::dpi::LogicalPosition;
use winit::MouseScrollDelta;
use crate::text::{DrawText, DrawTextTrait};
use crate::{FPS,HEIGHT,WIDTH};
use std::time::{Duration, Instant};
use vulkano::image::{ImmutableImage, Dimensions};
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use winit::MouseButton;
use vulkano::format::Format;
#[derive(Copy,Clone,PartialEq)]
pub struct Key{
    pub keycode:Option<VirtualKeyCode>,
    pub moder:ModifiersState,
    pub keep_key: bool,
}
impl Key{
    pub fn get_mod(self)->ModifiersState{
        self.moder
    }
}
#[derive(Copy,Clone,PartialEq)]
pub struct MouseScroll{
    pub delta:(i64,i64),
    pub moder:ModifiersState,
}
impl MouseScroll{
    pub fn delta_x(self)->i64{
        self.delta.0
    }
    pub fn delta_y(self)->i64{
        self.delta.1//.PixelDelta.y as i64
    }
}
#[derive(Copy,Clone,PartialEq)]
pub struct Mouse{
    pub btn:Option<MouseButton>,
    pub moder:ModifiersState,
}
#[derive(Copy, Clone, PartialEq)]
pub struct CanvasGlob {
    pub size: (u16, u16),
    pub stroke: bool,
    pub color: [f32; 4],
    pub stroke_weight: u8,
    pub fill: bool,
    pub fill_color: [f32; 4],
    pub background_color: [f32; 4],
    pub fps: f32,
    pub resizeable: bool,
    pub text_size: f32,
    pub key:Key,
    pub cursor_pos:(u16,u16),
    pub mouse:Mouse,
    pub mouse_scroll:MouseScroll,
}
impl CanvasGlob{
    pub fn show<F>(&mut self, mut draw_fn:F)
    where F :FnMut()+ 'static,
    {
                let set;
        let mut previous_frame_end;
        let (mut env, mut events_loop) = init(self.size.0, self.size.1);
        unsafe{
        draw_fn();
        match &TEXTURE{
            Some((vec1,dim1))=>{
                let vec_tex = vec1.to_vec();
                let dimensions = *dim1;
        let (texture, tex_future) = {
                ImmutableImage::from_iter(
                vec_tex.iter().cloned(),
                dimensions,
                Format::R8G8B8A8Srgb,
                env.queue.clone()
                ).unwrap()
            };
    let sampler = Sampler::new(env.device.clone(), Filter::Linear, Filter::Linear,
    MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat,
    SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap();
    set = Some(Arc::new(PersistentDescriptorSet::start(env.tex_pipeline.clone(),0)
    .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
    .build().unwrap()));
        previous_frame_end = Box::new(tex_future) as Box<dyn GpuFuture>;
            },
            None =>{
                set =None;
                previous_frame_end =Box::new(env.previous_frame_end.unwrap());
            }
        }
        }
        let mut text = DrawText::new(env.device.clone(), env.queue.clone(), env.swapchain.clone(), &env.images);
        let mut counter1 = 0;
        let start = Instant::now();
        let mut end;
        let mut recreate_swapchain = env.recreate_swapchain;

        loop {
            let mut done = false;
                previous_frame_end.cleanup_finished();
            events_loop.poll_events(|ev| match ev {
                Event::WindowEvent {
                    event: WindowEvent::CloseRequested,
                    ..
                } => done = true,
                Event::WindowEvent {
                    event: WindowEvent::Resized(_),
                    ..
                } => recreate_swapchain = true,
                Event::WindowEvent{event,..}=>
                match event{
                    WindowEvent::KeyboardInput{
                        input:KeyboardInput{
                            state: ElementState::Pressed,
                            virtual_keycode: Some(key),
                            modifiers,
                            ..
                        },
                        ..
                } => {
                    if key == VirtualKeyCode::W && modifiers.ctrl{
                        done = true;
                    }
                    self.key = Key{keycode:Some(key),moder:modifiers,keep_key:false};
                },
                    WindowEvent::CursorMoved{
                        position:LogicalPosition{x:posx,y:posy},
                        ..
                   }=>{self.cursor_pos = (posx as u16,posy as u16);},
                    WindowEvent::MouseInput{
                            state: ElementState::Pressed,
                            button: button1,
                            modifiers,
                            ..
                } => {
                    self.mouse = Mouse{btn:Some(button1),moder:modifiers};
                },
                    WindowEvent::MouseWheel{
                            delta: MouseScrollDelta::PixelDelta(pos),
                            modifiers,
                            ..
                } => {
                    self.mouse_scroll = MouseScroll{delta:(pos.x as i64,pos.y as i64),moder:modifiers};
                },
                _=>{},
                }
                _ => (),
            });
            if done {
                return;
            }
            unsafe {
            env.dynamic_state.line_width = Some(CANVAS.stroke_weight as f32);
                match &STROKE_VERTECIES {
                    Some(vec1) => STROKE_VERTECIES = Some(vec1.to_vec()),
                    None => {
                        let vec2 = vec![];
                        STROKE_VERTECIES = Some(vec2);
                    }
                };
                match &FILL_VERTECIES {
                    Some(vec1) => FILL_VERTECIES = Some(vec1.to_vec()),
                    None => {
                        let vec2 = vec![];
                        FILL_VERTECIES = Some(vec2);
                    }
                };
                let stroke_vertex_buffer = CpuAccessibleBuffer::from_iter(
                    env.device.clone(),
                    BufferUsage::all(),
                    STROKE_VERTECIES.clone().unwrap().iter().cloned(),
                )
                .unwrap();
                let fill_vertex_buffer = CpuAccessibleBuffer::from_iter(
                    env.device.clone(),
                    BufferUsage::all(),
                    FILL_VERTECIES.clone().unwrap().iter().cloned(),
                )
                .unwrap();
                let window = env.surface.window();
                if recreate_swapchain {
                    let dimensions = {
                        let dimensions: (u32, u32) = window
                            .get_inner_size()
                            .unwrap()
                            .to_physical(window.get_hidpi_factor())
                            .into();
                        [dimensions.0, dimensions.1]
                    };
                    let (new_swapchain, new_images) =
                        match env.swapchain.recreate_with_dimension(dimensions) {
                            Ok(r) => r,
                            Err(SwapchainCreationError::UnsupportedDimensions) => continue,
                            Err(err) => panic!("{:?}", err),
                        };
                    env.swapchain = new_swapchain;
                    env.framebuffers = window_size_dependent_setup(
                        &new_images,
                        env.render_pass.clone(),
                        &mut env.dynamic_state,
                    );
                    text = DrawText::new(env.device.clone(), env.queue.clone(), env.swapchain.clone(), &new_images);
                    recreate_swapchain = false;
                }
                let (image_num, acquire_future) =
                    match swapchain::acquire_next_image(env.swapchain.clone(), None) {
                        Ok(r) => r,
                        Err(AcquireError::OutOfDate) => {
                            recreate_swapchain = true;
                            continue;
                        }
                        Err(err) => panic!("{:?}", err),
                    };
                let clear_values = vec![self.background_color.into()];
                let command_buffer:AutoCommandBuffer;
                match &TEXT_VEC {
                    Some(vec1) => {if vec1.len()>0{
                        for txt in vec1{
                            text.queue_text(txt.position[0],txt.position[0], CANVAS.text_size, txt.color,txt.text);
                        }
                        match set.clone(){
                            Some(set)=>{
                command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
                    env.device.clone(),
                    env.queue.family(),
                )
                .unwrap()
                .begin_render_pass(env.framebuffers[image_num].clone(), false, clear_values)
                .unwrap()
                .draw(
                    env.tex_pipeline.clone(),
                    &env.dynamic_state,
                    vec![fill_vertex_buffer.clone()],
                    set.clone(),
                    (),
                )
                .unwrap()
                .draw(
                    env.stroke_pipeline.clone(),
                    &env.dynamic_state,
                    vec![stroke_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .end_render_pass()
                .unwrap()
                .draw_text(&mut text, image_num)
                .build()
                .unwrap();
                            },
                None=>{
                command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
                    env.device.clone(),
                    env.queue.family(),
                )
                .unwrap()
                .begin_render_pass(env.framebuffers[image_num].clone(), false, clear_values)
                .unwrap()
                .draw(
                    env.fill_pipeline.clone(),
                    &env.dynamic_state,
                    vec![fill_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .draw(
                    env.stroke_pipeline.clone(),
                    &env.dynamic_state,
                    vec![stroke_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .end_render_pass()
                .unwrap()
                .draw_text(&mut text, image_num)
                .build()
                .unwrap();
                }
                        }
                    }else{
                        match set.clone(){
                            Some(set)=>{
                        command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
                    env.device.clone(),
                    env.queue.family(),
                )
                .unwrap()
                .begin_render_pass(env.framebuffers[image_num].clone(), false, clear_values)
                .unwrap()
                .draw(
                    env.tex_pipeline.clone(),
                    &env.dynamic_state,
                    vec![fill_vertex_buffer.clone()],
                    set.clone(),
                    (),
                )
                .unwrap()
                .draw(
                    env.stroke_pipeline.clone(),
                    &env.dynamic_state,
                    vec![stroke_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .end_render_pass()
                .unwrap()
                .build()
                .unwrap();
                            },

                None =>{
                    command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
                    env.device.clone(),
                    env.queue.family(),
                )
                .unwrap()
                .begin_render_pass(env.framebuffers[image_num].clone(), false, clear_values)
                .unwrap()
                .draw(
                    env.fill_pipeline.clone(),
                    &env.dynamic_state,
                    vec![fill_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .draw(
                    env.stroke_pipeline.clone(),
                    &env.dynamic_state,
                    vec![stroke_vertex_buffer.clone()],
                    (),
                    (),
                )
                .unwrap()
                .end_render_pass()
                .unwrap()
                .build()
                .unwrap();
                }
                        }
                    }
                    },
                    None => {
                        let vec2 = vec![];
                        TEXT_VEC = Some(vec2);
                        command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
                    env.device.clone(),
                    env.queue.family(),
                )
                .unwrap()
                .build()
                .unwrap();
                    }
                };
                //let prev =/* env.*/ (&mut *previous_frame_end).take();
                let future = previous_frame_end
                    .join(acquire_future)
                    .then_execute(env.queue.clone(), command_buffer)
                    .unwrap()
                    .then_swapchain_present(env.queue.clone(), env.swapchain.clone(), image_num)
                    .then_signal_fence_and_flush();
                match future {
                    Ok(future) => {
                        future.wait(None).unwrap();
                        previous_frame_end = Box::new(future) as Box<_>;
                    }
                    Err(FlushError::OutOfDate) => {
                        recreate_swapchain = true;
                        previous_frame_end =
                            Box::new(sync::now(env.device.clone())) as Box<_>;
                    }
                    Err(e) => {
                        println!("{:?}", e);
                        previous_frame_end =
                            Box::new(sync::now(env.device.clone())) as Box<_>;
                    }
                }
            end = Instant::now();
            if (end-start)>Duration::new(1,0){
                CANVAS.fps = counter1 as f32/(end-start).as_secs() as f32;
                FPS = CANVAS.fps;
            }
            HEIGHT = CANVAS.size.1;
            WIDTH = CANVAS.size.0;
            }
            zero_out();
            draw_fn();
            if self.key.keep_key == false{
            self.key.keycode = Some(VirtualKeyCode::Power);
            }
            self.mouse.btn = Some(MouseButton::Other(99));
            counter1+=1;
        }
        //});
    }
}
pub fn zero_out() {
    unsafe {
        match &STROKE_VERTECIES {
            None => {}
            Some(_vec1) => {
                let vec2 = vec![];
                STROKE_VERTECIES = Some(vec2);
            }
        };
        match &FILL_VERTECIES {
            None => {}
            Some(_vec1) => {
                let vec2 = vec![];
                FILL_VERTECIES = Some(vec2);
            }
        };
        match &TEXT_VEC {
            None => {}
            Some(_vec1) => {
                let vec2 = vec![];
                TEXT_VEC = Some(vec2);
            }
        };
    }
}
pub static mut CANVAS: CanvasGlob = CanvasGlob {
    size: (0, 0),
    stroke: true,
    color: [0.0, 0.0, 0.0, 1.0],
    stroke_weight: 8,
    fill: false,
    fill_color: [1.0, 1.0, 1.0, 1.0],
    background_color: [1.0, 1.0, 1.0, 1.0],
    fps: 30.0,
    resizeable: false,
    text_size: 18.0,
    key:Key{keycode:None,moder:ModifiersState{shift:false,ctrl:false,alt:false,logo:false},keep_key:false},
    cursor_pos:(0,0),
    mouse:Mouse{btn:None,moder:ModifiersState{shift:false,ctrl:false,alt:false,logo:false},},
    mouse_scroll:MouseScroll{delta:(0,0),moder:ModifiersState{shift:false,ctrl:false,alt:false,logo:false},},

};
pub static mut FILL_VERTECIES: Option<Vec<Vertex>> = None;
pub static mut STROKE_VERTECIES: Option<Vec<Vertex>> = None;
pub static mut TEXT_VEC: Option<Vec<Stext>> = None;
pub static mut TEXTURE:Option<(Vec<u8>,Dimensions)> = None;