1
General / Re: Write a text when an object/or shape is clicked
« 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
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#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();
};
#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.hvoid 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);
}
#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#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;
};
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{
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);
}
}
}
#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;
}
#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;
}