| 1 | /* |
| 2 | * Copyright 2020-2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use crate::animation::{self, Animation, InfiniteAnimation}; |
| 21 | use crate::arena::ObjectType; |
| 22 | use crate::image::Image; |
| 23 | use gtk::{glib, glib::subclass::prelude::*, graphene, gsk, prelude::*}; |
| 24 | use std::error::Error; |
| 25 | use std::ops::Range; |
| 26 | use std::path::{Path, PathBuf}; |
| 27 | |
| 28 | pub const PLAYER_ANIMATION_FRAMES: Range<usize> = 0..4; |
| 29 | pub const PLAYER_DEAD_ANIMATION_FRAMES: Range<usize> = 4..5; |
| 30 | pub const ROBOT1_ANIMATION_FRAMES: Range<usize> = 5..9; |
| 31 | pub const ROBOT2_ANIMATION_FRAMES: Range<usize> = 9..13; |
| 32 | pub const HEAP_ANIMATION_FRAMES: Range<usize> = 13..14; |
| 33 | pub const FRAMES_COUNT: usize = 14; |
| 34 | |
| 35 | mod imp { |
| 36 | use super::*; |
| 37 | use std::cell::OnceCell; |
| 38 | |
| 39 | #[derive(glib::Properties, Default)] |
| 40 | #[properties(wrapper_type = super::Theme)] |
| 41 | pub struct Theme { |
| 42 | pub image: OnceCell<Image>, |
| 43 | #[property(get, construct_only)] |
| 44 | pub path: OnceCell<PathBuf>, |
| 45 | #[property(get, construct_only)] |
| 46 | pub name: OnceCell<String>, |
| 47 | } |
| 48 | |
| 49 | #[glib::object_subclass] |
| 50 | impl ObjectSubclass for Theme { |
| 51 | const NAME: &'static str = "RobotsTheme"; |
| 52 | type Type = super::Theme; |
| 53 | type ParentType = glib::Object; |
| 54 | } |
| 55 | |
| 56 | #[glib::derived_properties] |
| 57 | impl ObjectImpl for Theme {} |
| 58 | } |
| 59 | |
| 60 | glib::wrapper! { |
| 61 | pub struct Theme(ObjectSubclass<imp::Theme>); |
| 62 | } |
| 63 | |
| 64 | impl Theme { |
| 65 | pub fn from_file(path: &Path) -> Result<Self, Box<dyn Error>> { |
| 66 | let image = Image::from_file(path)?; |
| 67 | let name = path |
| 68 | .file_stem() |
| 69 | .ok_or("Bad file name")? |
| 70 | .to_string_lossy() |
| 71 | .replace("_", " "); |
| 72 | let this: Self = glib::Object::builder() |
| 73 | .property("path", path) |
| 74 | .property("name", name) |
| 75 | .build(); |
| 76 | this.imp().image.set(image).ok().unwrap(); |
| 77 | Ok(this) |
| 78 | } |
| 79 | |
| 80 | pub fn draw_object( |
| 81 | &self, |
| 82 | r#type: ObjectType, |
| 83 | frame_no: usize, |
| 84 | snapshot: >k::Snapshot, |
| 85 | rect: &graphene::Rect, |
| 86 | renderer: &gsk::Renderer, |
| 87 | ) { |
| 88 | let tile_no = match r#type { |
| 89 | ObjectType::Player => frame_index(PLAYER_ANIMATION_FRAMES, frame_no), |
| 90 | ObjectType::Robot1 => frame_index(ROBOT1_ANIMATION_FRAMES, frame_no), |
| 91 | ObjectType::Robot2 => frame_index(ROBOT2_ANIMATION_FRAMES, frame_no), |
| 92 | ObjectType::Heap => frame_index(HEAP_ANIMATION_FRAMES, frame_no), |
| 93 | ObjectType::None => return, |
| 94 | }; |
| 95 | |
| 96 | snapshot.push_clip(rect); |
| 97 | |
| 98 | snapshot.save(); |
| 99 | let frame_offset = (tile_no as i32) * (rect.width() as i32); |
| 100 | let texture_width = (rect.width() as i32) * (FRAMES_COUNT as i32); |
| 101 | let texture_height = rect.height() as i32; |
| 102 | let image = self.imp().image.get().unwrap(); |
| 103 | match image.scaled(texture_width, texture_height, renderer) { |
| 104 | Ok(texture) => { |
| 105 | snapshot.translate(&graphene::Point::new( |
| 106 | rect.x() - frame_offset as f32, |
| 107 | rect.y(), |
| 108 | )); |
| 109 | texture.snapshot(snapshot, texture_width as f64, texture_height as f64); |
| 110 | } |
| 111 | Err(error) => { |
| 112 | eprintln!("Failed to get a scaled texture: {error}"); |
| 113 | } |
| 114 | } |
| 115 | snapshot.restore(); |
| 116 | |
| 117 | snapshot.pop(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn frame_index(range: Range<usize>, frame_no: usize) -> usize { |
| 122 | range.start + frame_no % range.len() |
| 123 | } |
| 124 | |
| 125 | pub fn player_animation() -> impl InfiniteAnimation { |
| 126 | animation::sequence(PLAYER_ANIMATION_FRAMES) |
| 127 | .limit(1) |
| 128 | .repeat(20) |
| 129 | .then(animation::bounce(PLAYER_ANIMATION_FRAMES).repeat(2)) |
| 130 | .forever() |
| 131 | } |
| 132 | |
| 133 | pub fn player_dead_animation() -> impl InfiniteAnimation { |
| 134 | animation::bounce(PLAYER_DEAD_ANIMATION_FRAMES).forever() |
| 135 | } |
| 136 | |
| 137 | pub fn robot1_animation() -> impl InfiniteAnimation { |
| 138 | animation::sequence(ROBOT1_ANIMATION_FRAMES).forever() |
| 139 | } |
| 140 | |
| 141 | pub fn robot2_animation() -> impl InfiniteAnimation { |
| 142 | animation::sequence(ROBOT2_ANIMATION_FRAMES).forever() |
| 143 | } |