| 1 | // (_||_/ |
| 2 | // ( ) |
| 3 | // ( o 0 ) |
| 4 | //-OOO°--(_)---°OOO--------------------------------------- |
| 5 | // Copyright (C) 2006 By Dominique Roux-Serret |
| 6 | // .OOOo oOOO. [email protected] |
| 7 | //-( )------( )--------------------------------------- |
| 8 | // ( ( ) / Copyright (C) 2023 By Johnny Jazeix |
| 9 | // (_) (_/ [email protected] |
| 10 | |
| 11 | // This program is free software; you can redistribute it and/or modify |
| 12 | // it under the terms of the GNU General Public License as published by |
| 13 | // the Free Software Foundation; either version 2 or version 3 of the License. |
| 14 | |
| 15 | // This program is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License for more details. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License along |
| 21 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | |
| 24 | #include <SDL2/SDL_keycode.h> // for SDLK_RETURN, SDL_Keycode |
| 25 | #include <SDL2/SDL_mouse.h> // for SDL_WarpMouseInWindow |
| 26 | #include <SDL2/SDL_video.h> // for SDL_Window |
| 27 | #include "audio.h" |
| 28 | #include "mouse.h" |
| 29 | #include "screen.h" |
| 30 | #include "sprite.h" // for e_Sprite |
| 31 | |
| 32 | /*** Variables Globales ***/ |
| 33 | /**************************/ |
| 34 | extern int Horloge; |
| 35 | extern Screen Ec; |
| 36 | extern SDL_Window *sdlWindow; |
| 37 | |
| 38 | /*** Initialise la sourie ***/ |
| 39 | /****************************/ |
| 40 | void Mouse::InitStart() |
| 41 | { |
| 42 | // Initialise les coordonnées de la sourie |
| 43 | Px = 400; |
| 44 | Py = 300; |
| 45 | SDL_WarpMouseInWindow(sdlWindow, Px, Py); |
| 46 | } |
| 47 | |
| 48 | /*** Initialise un bebut d'utilisation ***/ |
| 49 | /*****************************************/ |
| 50 | void Mouse::Init(struct mPy *TablePy, struct mButton *B) |
| 51 | { |
| 52 | // Sauve les adresses utils |
| 53 | tPy = TablePy; |
| 54 | Bo = B; |
| 55 | } |
| 56 | |
| 57 | /*** Prend les evenements de la sourie ***/ |
| 58 | /*****************************************/ |
| 59 | void Mouse::GetEvent(SDL_Event &event, int &pPy) |
| 60 | { |
| 61 | int i; |
| 62 | |
| 63 | switch (event.type) { |
| 64 | case SDL_MOUSEMOTION: // Si mouvement de la sourie |
| 65 | Px = event.motion.x; |
| 66 | Py = event.motion.y; |
| 67 | // regarde si doit bouger la position de Py |
| 68 | if (tPy) { // Si bien une table |
| 69 | i = 0; |
| 70 | while (tPy[i].DepX != -1) { // Fait toutes les coordonnées |
| 71 | if (Px >= tPy[i].DepX && Px <= tPy[i].FinX && Py >= tPy[i].DepY && Py <= tPy[i].FinY) { |
| 72 | if (pPy != tPy[i].Py) { |
| 73 | pPy = tPy[i].Py; |
| 74 | m_audio.Play(sClic); |
| 75 | } |
| 76 | } |
| 77 | i++; |
| 78 | }; |
| 79 | } |
| 80 | break; |
| 81 | case SDL_MOUSEBUTTONDOWN: |
| 82 | if (event.button.state == SDL_PRESSED) { |
| 83 | Px = event.button.x; |
| 84 | Py = event.button.y; |
| 85 | |
| 86 | // regarde si doit valider un enter |
| 87 | if (tPy) { // Si bien une table |
| 88 | i = 0; |
| 89 | while (tPy[i].DepX != -1) { // Fait toutes les coordonnées |
| 90 | if (Px >= tPy[i].DepX && Px <= tPy[i].FinX && Py >= tPy[i].DepY && Py <= tPy[i].FinY) { |
| 91 | if (tPy[i].Valide == true) { |
| 92 | event.type = SDL_KEYDOWN; |
| 93 | event.key.state = SDL_PRESSED; |
| 94 | event.key.keysym.sym = SDLK_RETURN; |
| 95 | } |
| 96 | } |
| 97 | i++; |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | // Fait la gestion des bouttons |
| 102 | if (Bo) { // Si bien une table |
| 103 | i = 0; |
| 104 | while (Bo[i].DepX != -1) { // Fait toutes les coordonnées |
| 105 | if (Px >= Bo[i].DepX && Px <= Bo[i].FinX && Py >= Bo[i].DepY && Py <= Bo[i].FinY) { |
| 106 | if (Bo[i].Adr == nullptr) { // Si doit fair une touche |
| 107 | event.type = SDL_KEYDOWN; |
| 108 | event.key.state = SDL_PRESSED; |
| 109 | event.key.keysym.sym = (SDL_Keycode)Bo[i].Valeur; |
| 110 | } |
| 111 | else { // Si doit changer une variable |
| 112 | *(Bo[i].Adr) = Bo[i].Valeur; |
| 113 | } |
| 114 | } |
| 115 | i++; |
| 116 | }; |
| 117 | } |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /*** Affiche le curseur ***/ |
| 124 | /**************************/ |
| 125 | void Mouse::Print() const |
| 126 | { |
| 127 | int X = Px, Y = Py; |
| 128 | int const NumSp = (Horloge / 50) % 20; |
| 129 | |
| 130 | // Corrige la position du curseur au cas ou déborde de l'écran |
| 131 | if (X < 5) { |
| 132 | X = 5; |
| 133 | } |
| 134 | if (X >= 800 - 38) { |
| 135 | X = 800 - 39; |
| 136 | } |
| 137 | if (Y < 10) { |
| 138 | Y = 10; |
| 139 | } |
| 140 | if (Y >= 600 - 35) { |
| 141 | Y = 600 - 36; |
| 142 | } |
| 143 | |
| 144 | #ifndef ANDROID |
| 145 | // Affiche le curseur |
| 146 | Ec.PrintSprite(curseur, NumSp, X, Y); |
| 147 | #endif |
| 148 | } |