Advertisement
TermSpar

SFML Textbox and Button Implementation

Apr 11th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "Textbox.h"
  4. #include "Button.h"
  5.  
  6. int main() {
  7.     sf::RenderWindow window;
  8.  
  9.     sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
  10.  
  11.     window.create(sf::VideoMode(900, 900), "SFML Textbox", sf::Style::Titlebar | sf::Style::Close);
  12.     window.setPosition(centerWindow);
  13.  
  14.     window.setKeyRepeatEnabled(true);
  15.  
  16.     sf::Font font;
  17.     if (!font.loadFromFile("arial.ttf"))
  18.         std::cout << "Font not found!\n";
  19.  
  20.     Textbox text1(20, sf::Color::White, true);
  21.     text1.setPosition({ 100, 100 });
  22.     text1.setLimit(true, 30);
  23.     text1.setFont(font);
  24.  
  25.     Button btn1("Enter", { 200, 100 }, 30, sf::Color::Green, sf::Color::Black);
  26.     btn1.setFont(font);
  27.     btn1.setPosition({ 100, 300 });
  28.    
  29.  
  30.     //Main Loop:
  31.     while (window.isOpen()) {
  32.  
  33.         sf::Event Event;
  34.  
  35.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
  36.             text1.setSelected(true);
  37.         }
  38.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
  39.             text1.setSelected(false);
  40.         }
  41.  
  42.         //Event Loop:
  43.         while (window.pollEvent(Event)) {
  44.             switch (Event.type) {
  45.  
  46.             case sf::Event::Closed:
  47.                 window.close();
  48.             case sf::Event::TextEntered:
  49.                 text1.typedOn(Event);
  50.             case sf::Event::MouseMoved:
  51.                 if (btn1.isMouseOver(window)) {
  52.                     btn1.setBackColor(sf::Color::Magenta);
  53.                 }
  54.                 else {
  55.                     btn1.setBackColor(sf::Color::Green);
  56.                 }
  57.                 break;
  58.             case sf::Event::MouseButtonPressed:
  59.                 if (btn1.isMouseOver(window)) {
  60.                     std::cout << "Hello " << text1.getText() << "\n";
  61.                 }
  62.             }
  63.         }
  64.         window.clear();
  65.         text1.drawTo(window);
  66.         btn1.drawTo(window);
  67.         window.display();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement