Skip to content

Commit

Permalink
keys now clickable woohoo
Browse files Browse the repository at this point in the history
  • Loading branch information
mehanix committed Apr 11, 2020
1 parent 6a7ed29 commit 3455963
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 40 deletions.
Binary file modified GRTAGS
Binary file not shown.
Binary file modified GTAGS
Binary file not shown.
1 change: 1 addition & 0 deletions include/BlackKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class BlackKey : public Key {
void playSound();
void setPosition(int x,int y) override;
sf::Sprite& getSprite() override;
void setPressed(bool value) override;

};

Expand Down
3 changes: 2 additions & 1 deletion include/Key.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Key {
virtual sf::Sprite& getSprite() = 0;
virtual void playSound() = 0;
virtual void setPosition(int x, int y) = 0;

virtual void setPressed(bool value) = 0;

~Key() {
std::cout<<"papa clapa"<<std::endl;
}
Expand Down
5 changes: 3 additions & 2 deletions include/WhiteKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include "Key.h"
class WhiteKey : public Key {

static sf::Texture t;
static sf::Texture t_Pressed;
sf::Texture t;
sf::Texture t_Pressed;
public:
WhiteKey(sf::Texture& t, sf::Texture& t_Pressed);
void playSound();
void setPosition(int x,int y) override;
sf::Sprite& getSprite() override;
void setPressed(bool value) override;

};
#endif
Binary file added res/piano/key_black_pressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/piano/key_white_pressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 20 additions & 7 deletions src/BlackKey.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
#include "BlackKey.h"

BlackKey::BlackKey(sf::Texture& t, sf::Texture& t_Pressed) : Key(t, t_Pressed) {

BlackKey::BlackKey(sf::Texture &t, sf::Texture &t_Pressed) : Key(t, t_Pressed)
{
}

void BlackKey::playSound() {
void BlackKey::playSound()
{
}

void BlackKey::setPosition(int x, int y) {
Key::sprite.setPosition(x,y);
void BlackKey::setPosition(int x, int y)
{
Key::sprite.setPosition(x, y);
}

sf::Sprite& BlackKey::getSprite() {
sf::Sprite &BlackKey::getSprite()
{
return sprite;
}

void BlackKey::setPressed(bool value)
{
if (value == true)
{
sprite.setTexture(texturePressed);
}
else
{
sprite.setTexture(texture);
}
}
4 changes: 2 additions & 2 deletions src/GUI/Piano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ Key* Piano::findKeyClicked(sf::Vector2f mouse) {
sf::FloatRect bounds = piano[i]->getSprite().getGlobalBounds();
if (bounds.contains(mouse))
{
std::cout<<"click pe clapa\n";
break;
return piano[i];
}
}
return nullptr;

}

Expand Down
11 changes: 10 additions & 1 deletion src/GUI/WhiteKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ void WhiteKey::playSound() {
}

void WhiteKey::setPosition(int x,int y) {
Key::sprite.setPosition(x,y);
sprite.setPosition(x,y);
}

sf::Sprite& WhiteKey::getSprite() {
return sprite;
}

void WhiteKey::setPressed(bool value) {
if (value==true) {
sprite.setTexture(texturePressed);
}
else {
sprite.setTexture(texture);
}
}

68 changes: 41 additions & 27 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,77 @@ void MainWindow::loadLayout()
//background image/pattern
bkgTexture.loadFromFile("res/gui/piano.png");
bkgTexture.setRepeated(true);
bkg = sf::Sprite(bkgTexture,sf::IntRect(0,0,getSize().x,getSize().y));
bkg.setColor(sf::Color(0,0,0,35));
bkg = sf::Sprite(bkgTexture, sf::IntRect(0, 0, getSize().x, getSize().y));
bkg.setColor(sf::Color(0, 0, 0, 35));

//main panel
panelTexture.loadFromFile("res/gui/panel2.png");
panel = sf::Sprite(panelTexture,sf::IntRect(0,0,718,518));
panel.setPosition(41,41);
panel = sf::Sprite(panelTexture, sf::IntRect(0, 0, 718, 518));
panel.setPosition(41, 41);

//app logo
logoTexture.loadFromFile("res/gui/logo.png");
logoTexture.setSmooth(true);
logo = sf::Sprite(logoTexture);
logo.setPosition(70,67);
logo.setScale(0.5f,0.5f);
logo.setPosition(70, 67);
logo.setScale(0.5f, 0.5f);

//app label
adLabel.setFont(font);
adLabel.setString("easy to use virtual piano");
adLabel.setCharacterSize(20);
adLabel.setFillColor(sf::Color(26,26,26));
adLabel.setPosition(515,70);

adLabel.setFillColor(sf::Color(26, 26, 26));
adLabel.setPosition(515, 70);
}
void MainWindow::render()
{

Key *activeKey;
while (sf::RenderWindow::isOpen())
{
sf::Event event;
while (sf::RenderWindow::pollEvent(event))
{
if (event.type == sf::Event::Closed)
sf::RenderWindow::close();

if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
auto key = piano->findKeyClicked((*this).mapPixelToCoords(sf::Mouse::getPosition((*this))));
}
}
{
if (event.mouseButton.button == sf::Mouse::Left)
{
activeKey = piano->findKeyClicked(mapPixelToCoords(sf::Mouse::getPosition((*this))));
if (activeKey == nullptr)
{
std::cout << "click pe nimic\n";
}
else
{
activeKey->setPressed(true);
}
}
}
if(event.type == sf::Event::MouseButtonReleased) {
if(event.mouseButton.button == sf::Mouse::Left) {
if(activeKey != nullptr) {
activeKey->setPressed(false);
}
}
}
}


sf::RenderWindow::clear(sf::Color(223,219,229,255));
sf::RenderWindow::clear(sf::Color(223, 219, 229, 255));
drawGUI();
sf::RenderWindow::display();
}
}
void MainWindow::drawGUI()
{
// sf::RenderWindow::draw(b.getSprite());
sf::RenderWindow::draw(bkg);
sf::RenderWindow::draw(panel);
sf::RenderWindow::draw(logo);
sf::RenderWindow::draw(adLabel);
for(auto &k:piano->getKeys()) {
sf::RenderWindow::draw(k->getSprite());
}
// sf::RenderWindow::draw(b.getSprite());
sf::RenderWindow::draw(bkg);
sf::RenderWindow::draw(panel);
sf::RenderWindow::draw(logo);
sf::RenderWindow::draw(adLabel);
for (auto &k : piano->getKeys())
{
sf::RenderWindow::draw(k->getSprite());
}
}

0 comments on commit 3455963

Please sign in to comment.