Advertisement
Egor_Vakar

Button.cpp(coursework)

Dec 26th, 2022
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include "Button.hpp"
  2.  
  3. Button::Button(TableHandle* event){
  4.  
  5.     this->event = event;
  6. }
  7.  
  8. Button::~Button(){
  9. }
  10.  
  11. void Button::Draw(sf::RenderWindow& app){
  12.  
  13.     app.draw(btnSpr);
  14. }
  15.  
  16. void Button::EventHandle(sf::RenderWindow& app, float& dt){
  17.  
  18.     if (dt < 0.01) {
  19.         dt = 0.02;
  20.     }
  21.  
  22.     // # if playerturn is false, this button became unable
  23.     this->enable = !this->event->buttonLocked;
  24.  
  25.     if(currentTimeLocked >= timeLocked)
  26.         isLocked = false;
  27.  
  28.     if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
  29.  
  30.         if(sf::Mouse::getPosition(app).x > this->btnSpr.getPosition().x
  31.         && sf::Mouse::getPosition(app).x < this->btnSpr.getPosition().x + this->btnSize.x
  32.         && sf::Mouse::getPosition(app).y > this->btnSpr.getPosition().y
  33.         && sf::Mouse::getPosition(app).y < this->btnSpr.getPosition().y + this->btnSize.y ){
  34.  
  35.             if(!isLocked && enable){
  36.  
  37.                 this->isLocked = true;
  38.                 this->currentTimeLocked = 0;
  39.  
  40.                 // # Execute event call here
  41.                 this->event->EventHandle(this->action);
  42.             }
  43.         }
  44.     }
  45.  
  46.     if( isLocked )
  47.         this->currentTimeLocked += dt;
  48. }
  49.  
  50. void Button::setEvent(enum TableHandle::Actions action){
  51.     this->action = action;
  52. }
  53.  
  54. void Button::setImage(sf::Texture& tex){
  55.  
  56.     this->btnSpr.setTexture(tex);
  57.     this->btnSize = sf::Vector2f(btnSpr.getTexture()->getSize().x, btnSpr.getTexture()->getSize().y);
  58. }
  59.  
  60. void Button::setLocation(sf::Vector2f ploc){
  61.  
  62.     this->btnSpr.setPosition(ploc);
  63. }
  64.  
  65. void Button::setTimeLocked(float newTimeLocked){
  66.  
  67.     this->timeLocked = newTimeLocked;
  68. }
  69.  
  70. void Button::setEnable(bool state){
  71.  
  72.     this->enable = state;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement