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
/*
Copyright 2024 Nicolas Cesar Sabbatini Vrech
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use macroquad::prelude::*;
pub struct Canvas2D {
camera: Camera2D,
width: f32,
height: f32,
}
impl Canvas2D {
/// Create a new canvas with the given width and height.
#[must_use]
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn new(width: f32, height: f32) -> Self {
let mut camera = Camera2D::from_display_rect(Rect::new(0.0, 0.0, width, height));
camera.render_target = Some(render_target(width as u32, height as u32));
// Temp fix or maybe I am doing something wrong
// https://github.com/not-fl3/macroquad/issues/171#issuecomment-880601087
camera.zoom.y = -camera.zoom.y;
Canvas2D {
camera,
width,
height,
}
}
/// Get width.
#[must_use]
pub fn width(&self) -> f32 {
self.width
}
/// Get height.
#[must_use]
pub fn height(&self) -> f32 {
self.height
}
/// Get width and height.
#[must_use]
pub fn width_height(&self) -> (f32, f32) {
(self.width, self.height)
}
/// Get a reference of the canvas texture.
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn get_texture(&self) -> &Texture2D {
&self.camera.render_target.as_ref().unwrap().texture
}
/// Get a mutable reference of the canvas texture.
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn get_texture_mut(&mut self) -> &mut Texture2D {
&mut self.camera.render_target.as_mut().unwrap().texture
}
/// Set the canvas as te default camera to draw
/// if you want to draw to the screen you should call
/// `macroquad::camera::set_default_camera()`
pub fn set_camera(&self) {
set_camera(&self.camera);
}
/// Calculate size and padding of the canvas so it can fit inside
/// of the target and its position is in the center.
#[must_use]
pub fn calculate_size_and_padding(
&self,
target_width: f32,
target_height: f32,
) -> (f32, f32, Vec2) {
let new_size: Vec2 = self.calculate_size(target_width, target_height);
// Calculate padding
let left_padding: f32 = (target_width - new_size.x) / 2.0;
let top_padding: f32 = (target_height - new_size.y) / 2.0;
(left_padding, top_padding, new_size)
}
/// Calculate size of the canvas so it can fit inside of the target
/// respecting the aspect ratio.
#[must_use]
pub fn calculate_size(&self, target_width: f32, target_height: f32) -> Vec2 {
let min_scale_factor: f32 = self.calculate_min_scale_factor(target_width, target_height);
// Calculate windows new size
let new_width: f32 = self.width * min_scale_factor;
let new_height: f32 = self.height * min_scale_factor;
Vec2::new(new_width, new_height)
}
/// Calculate the minimum scale factor.
#[must_use]
pub fn calculate_min_scale_factor(&self, target_width: f32, target_height: f32) -> f32 {
let (scale_factor_w, scale_factor_h) =
self.calculate_scale_factor(target_width, target_height);
f32::min(scale_factor_w, scale_factor_h)
}
/// Calculate scale factor.
#[must_use]
pub fn calculate_scale_factor(&self, target_width: f32, target_height: f32) -> (f32, f32) {
(target_width / self.width, target_height / self.height)
}
/// Convert from the parent coordinates to canvas coordinates.
///
/// Warning it can return negative numbers or values grater than the canvas
/// when the mouse is outside of the canvas.
#[must_use]
pub fn parent_coordinates_to_canvas_coordinates(
&self,
parent_width: f32,
parent_height: f32,
screen_x: f32,
screen_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32) {
let scale_factor = self.calculate_min_scale_factor(parent_width, parent_height);
let camera_offset = self.camera.offset / self.camera.zoom;
let x = (screen_x - offset_x) / scale_factor - camera_offset.x;
let y = (screen_y - offset_y) / scale_factor - camera_offset.y;
(x, y)
}
/// Convert from the canvas coordinates to parent coordinates.
///
/// Warning do to float division it can be a small margin of error.
#[must_use]
pub fn canvas_coordinates_to_parent_coordinates(
&self,
parent_width: f32,
parent_height: f32,
canvas_x: f32,
canvas_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32) {
let scale_factor = self.calculate_min_scale_factor(parent_width, parent_height);
let camera_offset = self.camera.offset / self.camera.zoom;
let x = ((canvas_x + camera_offset.x) * scale_factor) + offset_x;
let y = ((canvas_y + camera_offset.y) * scale_factor) + offset_y;
(x, y)
}
/// A wrapper around the `parent_to_canvas` for better ergonomic.
/// Convert from the screen coordinates to canvas coordinates.
///
/// Warning it can return negative numbers or values grater than the canvas
/// when the mouse is outside of the canvas.
#[must_use]
pub fn screen_coordinates_to_canvas_coordinates(
&self,
screen_x: f32,
screen_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32) {
self.parent_coordinates_to_canvas_coordinates(
screen_width(),
screen_height(),
screen_x,
screen_y,
offset_x,
offset_y,
)
}
/// A wrapper around the `canvas_to_parent` for better ergonomic.
/// Convert from the canvas coordinates to screen coordinates.
///
/// Warning do to float division it can be a small margin of error.
#[must_use]
pub fn canvas_coordinates_to_screen_coordinates(
&self,
canvas_x: f32,
canvas_y: f32,
offset_x: f32,
offset_y: f32,
) -> (f32, f32) {
self.canvas_coordinates_to_parent_coordinates(
screen_width(),
screen_height(),
canvas_x,
canvas_y,
offset_x,
offset_y,
)
}
/// Get the mouse position in canvas coordinates.
///
/// Warning it can return negative numbers or values grater than the canvas
#[must_use]
pub fn screen_mouse_position_to_canvas(&self, offset_x: f32, offset_y: f32) -> (f32, f32) {
let (x, y) = mouse_position();
self.screen_coordinates_to_canvas_coordinates(x, y, offset_x, offset_y)
}
/// Draws the canvas to the middle of the screen, keeping the aspect ratio.
/// It calls `set_default_camera` before drawing.
pub fn draw_to_screen(&self) {
set_default_camera();
// Get canvas dimensions and padding
let (left_padding, top_padding, dimensions) =
self.calculate_size_and_padding(screen_width(), screen_height());
// Draw canvas on screen
draw_texture_ex(
self.get_texture(),
left_padding,
top_padding,
WHITE,
DrawTextureParams {
dest_size: Some(dimensions),
..Default::default()
},
);
}
/// Zoom the camera by a factor.
pub fn zoom_add(&mut self, zoom: f32) {
self.camera.zoom += zoom;
}
/// Zoom the camera to a factor.
pub fn zoom_to(&mut self, zoom: f32) {
self.camera.zoom = vec2(zoom, zoom);
}
/// Rotate the camera by a factor.
/// The factor is in degrees.
pub fn rotate_add(&mut self, rotation: f32) {
self.camera.rotation += rotation;
}
/// Rotate the camera to a factor.
/// The factor is in degrees.
pub fn rotate_to(&mut self, rotation: f32) {
self.camera.rotation = rotation;
}
/// Move the camera by a factor in pixels.
pub fn move_camera(&mut self, offset_x: f32, offset_y: f32) {
let offset = vec2(offset_x * self.camera.zoom.x, offset_y * self.camera.zoom.y);
self.camera.offset += offset;
}
/// Set the camera offset to a factor in pixels.
pub fn move_camera_to(&mut self, offset_x: f32, offset_y: f32) {
let offset = vec2(offset_x * self.camera.zoom.x, offset_y * self.camera.zoom.y);
self.camera.offset = offset;
}
}