Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Write a text when an object/or shape is clicked  (Read 1646 times)

0 Members and 1 Guest are viewing this topic.

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Write a text when an object/or shape is clicked
« on: November 22, 2020, 10:00:32 pm »
Hello, can anyone help me with this problem. I'm trying to set up something like when I click on an object(or a shape), I would be able to write something inside of that object/shape. Thank you!

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Write a text when an object/or shape is clicked
« Reply #1 on: November 23, 2020, 01:06:19 am »
What part do you have trouble with?
Detecting the click on your shape?
Capturing the keyboard inputs?
Writing a text on the screen?

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Write a text when an object/or shape is clicked
« Reply #2 on: November 23, 2020, 02:22:28 am »
I suppose writing the text so it is shown on the screen. Here's what I try to do, when I click on the shape, I wasn't able to write anything:
Typing.h
#ifndef SFML_PROJECT_TYPING_H
#define SFML_PROJECT_TYPING_H

#include <SFML/Graphics.hpp>

class Typing: public sf::Text {
private:
    sf::Font font;
public:
    void addEvents(sf::RenderWindow& window, sf::Event event);
    Typing();
};
Typing.cpp
#include "Typing.h"
void Typing::addEvents(sf::RenderWindow& window, sf::Event event)
{
    while(window.pollEvent(event)){
        if(event.type == sf::Event::TextEntered)
        {
            setString(getString() + event.text.unicode);
        }
    }
}
Typing::Typing()
{
    font.loadFromFile("arial.ttf");
    setFont(font);
    setCharacterSize(12);
    setFillColor(sf::Color::Red);
}
task.h
#include <SFML/Graphics.hpp>
#include "Typing.h"
class task : public sf::Drawable, sf::Transformable {
private:
    Typing test;
    sf::Text text;
    sf::Font font;
    sf::RectangleShape background;

public:
    task();
    void setPosition(float x,float y);
    void setText();
    void setBackGround();
    void addEvent(sf::RenderWindow& window, sf::Event event);
    void draw(sf::RenderTarget &window, sf::RenderStates state) const;

};
task.cpp
void task::setText()
{
    sf::FloatRect bounds = background.getGlobalBounds();
    test.setPosition(bounds.left+20,bounds.top+30);
}
void task::setBackGround() {
    background.setSize({300.f,50.f});
    background.setFillColor(sf::Color::White);
}
task::task() {
    this->setBackGround();
}

void task::draw(sf::RenderTarget &window, sf::RenderStates state) const
{
    window.draw(background);
    window.draw(test);
}
void task::setPosition(float x, float y) {
    background.move(x,y);
    this->setText();
}
void task::addEvent(sf::RenderWindow& window, sf::Event event)
{  if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::FloatRect bounds = background.getGlobalBounds();
        sf::Vector2f mpos = (sf::Vector2f)sf::Mouse::getPosition(window);
        if (bounds.contains(mpos))
        {
            test.addEvents(window,event);
        }
    }
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "task.h"
int main() {
sf::RenderWindow window(sf::VideoMode(1980, 1080, 32), "Test");
    Card card("Ace","Hearts");
    task task;
    Button test;
    test.setText("Add task");
    test.setPosition(400,300);
    TaskList theTest;
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            } else
                task.addEvent(window,event);

        }
        window.clear(sf::Color::Black);
        window.draw(task);
        window.display();
    }
    return 0;
}