1use crate::gfx::Gfx;
2use crate::{
3 FixedAtlas, FontAndMaterial, FrameLookup, MaterialRef, NineSliceAndMaterial, Render,
4 RenderItem, Renderable, SpriteParams, Text, TileMap, to_wgpu_color,
5};
6use int_math::{URect, UVec2, Vec2, Vec3};
7use mireforge_render::{AspectRatio, Color, ViewportStrategy, VirtualScale};
8use monotonic_time_rs::Millis;
9
10impl Gfx for Render {
11 fn sprite_atlas_frame(&mut self, position: Vec3, frame: u16, atlas: &impl FrameLookup) {
12 self.sprite_atlas_frame(position, frame, atlas);
13 }
14
15 fn sprite_atlas(&mut self, position: Vec3, atlas_rect: URect, material_ref: &MaterialRef) {
16 self.sprite_atlas(position, atlas_rect, material_ref);
17 }
18
19 fn draw_sprite(&mut self, position: Vec3, material_ref: &MaterialRef) {
20 self.draw_sprite(position, material_ref);
21 }
22
23 fn draw_sprite_ex(
24 &mut self,
25 position: Vec3,
26 material_ref: &MaterialRef,
27 params: &SpriteParams,
28 ) {
29 self.draw_sprite_ex(position, material_ref, *params);
30 }
31
32 fn quad(&mut self, position: Vec3, size: UVec2, color: Color) {
33 self.draw_quad(position, size, color);
34 }
35
36 fn draw_with_mask(
37 &mut self,
38 position: Vec3,
39 size: UVec2,
40 color: Color,
41 alpha_masked: &MaterialRef,
42 ) {
43 self.push_mask(position, size, color, alpha_masked);
44 }
45
46 fn nine_slice(
47 &mut self,
48 position: Vec3,
49 size: UVec2,
50 color: Color,
51 nine_slice: &NineSliceAndMaterial,
52 ) {
53 self.nine_slice(position, size, color, nine_slice);
54 }
55
56 fn set_origin(&mut self, position: Vec2) {
57 self.origin = position;
58 }
59
60 fn set_clear_color(&mut self, color: Color) {
61 self.clear_color = to_wgpu_color(color);
62 }
63
64 fn tilemap_params(
65 &mut self,
66 position: Vec3,
67 tiles: &[u16],
68 width: u16,
69 atlas_ref: &FixedAtlas,
70 scale: u8,
71 ) {
72 self.items.push(RenderItem {
73 position,
74 material_ref: atlas_ref.material.clone(),
75 renderable: Renderable::TileMap(TileMap {
76 tiles_data_grid_size: UVec2::new(width, tiles.len() as u16 / width),
77 cell_count_size: atlas_ref.cell_count_size,
78 one_cell_size: atlas_ref.one_cell_size,
79 tiles: Vec::from(tiles),
80 scale,
81 }),
82 });
83 }
84
85 fn text_draw(
86 &mut self,
87 position: Vec3,
88 text: &str,
89 font_and_mat: &FontAndMaterial,
90 color: &Color,
91 ) {
92 self.items.push(RenderItem {
93 position,
94 material_ref: font_and_mat.material_ref.clone(),
95 renderable: Renderable::Text(Text {
96 text: text.to_string(),
97 font_ref: (&font_and_mat.font_ref).into(),
98 color: *color,
99 }),
100 });
101 }
102
103 fn now(&self) -> Millis {
104 self.last_render_at
105 }
106
107 fn physical_aspect_ratio(&self) -> AspectRatio {
108 self.physical_surface_size.into()
109 }
110
111 fn physical_size(&self) -> UVec2 {
112 self.physical_surface_size
113 }
114
115 fn set_virtual_size(&mut self, virtual_size: UVec2) {
116 self.resize_virtual(virtual_size);
117 }
118
119 fn set_viewport(&mut self, viewport_strategy: ViewportStrategy) {
120 self.viewport_strategy = viewport_strategy;
121 }
122
123 fn viewport(&self) -> &ViewportStrategy {
124 &self.viewport_strategy
125 }
126
127 fn set_scale(&mut self, scale_factor: VirtualScale) {
128 match scale_factor {
129 VirtualScale::IntScale(scale) => self.scale = scale as f32,
130 VirtualScale::FloatScale(scale) => self.scale = scale,
131 }
132 }
133}