Advertisement
TermSpar

SFML Textbox Class

Apr 11th, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. // Made by Ben Bollinger
  2.  
  3. #pragma once
  4.  
  5. #include <iostream>
  6. #include <sstream>
  7. #include <SFML/Graphics.hpp>
  8.  
  9. // Define keys:
  10. #define DELETE_KEY 8
  11. #define ENTER_KEY 13
  12. #define ESCAPE_KEY 27
  13.  
  14. class Textbox {
  15. public:
  16.     Textbox(int size, sf::Color color, bool sel) {
  17.         textbox.setCharacterSize(size);
  18.         textbox.setColor(color);
  19.         isSelected = sel;
  20.  
  21.         // Check if the textbox is selected upon creation and display it accordingly:
  22.         if(isSelected)
  23.             textbox.setString("_");
  24.         else
  25.             textbox.setString("");
  26.     }
  27.  
  28.     // Make sure font is passed by reference:
  29.     void setFont(sf::Font &fonts) {
  30.         textbox.setFont(fonts);
  31.     }
  32.  
  33.     void setPosition(sf::Vector2f point) {
  34.         textbox.setPosition(point);
  35.     }
  36.  
  37.     // Set char limits:
  38.     void setLimit(bool ToF) {
  39.         hasLimit = ToF;
  40.     }
  41.  
  42.     void setLimit(bool ToF, int lim) {
  43.         hasLimit = ToF;
  44.         limit = lim - 1;
  45.     }
  46.  
  47.     // Change selected state:
  48.     void setSelected(bool sel) {
  49.         isSelected = sel;
  50.  
  51.         // If not selected, remove the '_' at the end:
  52.         if (!sel) {
  53.             std::string t = text.str();
  54.             std::string newT = "";
  55.             for (int i = 0; i < t.length(); i++) {
  56.                 newT += t[i];
  57.             }
  58.             textbox.setString(newT);
  59.         }
  60.     }
  61.  
  62.     std::string getText() {
  63.         return text.str();
  64.     }
  65.  
  66.     void drawTo(sf::RenderWindow &window) {
  67.         window.draw(textbox);
  68.     }
  69.  
  70.     // Function for event loop:
  71.     void typedOn(sf::Event input) {
  72.         if (isSelected) {
  73.             int charTyped = input.text.unicode;
  74.  
  75.             // Only allow normal inputs:
  76.             if (charTyped < 128) {
  77.                 if (hasLimit) {
  78.                     // If there's a limit, don't go over it:
  79.                     if (text.str().length() <= limit) {
  80.                         inputLogic(charTyped);
  81.                     }
  82.                     // But allow for char deletions:
  83.                     else if (text.str().length() > limit && charTyped == DELETE_KEY) {
  84.                         deleteLastChar();
  85.                     }
  86.                 }
  87.                 // If no limit exists, just run the function:
  88.                 else {
  89.                     inputLogic(charTyped);
  90.                 }
  91.             }
  92.         }
  93.     }
  94. private:
  95.     sf::Text textbox;
  96.  
  97.     std::ostringstream text;
  98.     bool isSelected = false;
  99.     bool hasLimit = false;
  100.     int limit = 0;
  101.  
  102.     // Delete the last character of the text:
  103.     void deleteLastChar() {
  104.         std::string t = text.str();
  105.         std::string newT = "";
  106.         for (int i = 0; i < t.length() - 1; i++) {
  107.             newT += t[i];
  108.         }
  109.         text.str("");
  110.         text << newT;
  111.         textbox.setString(text.str() + "_");
  112.     }
  113.  
  114.     // Get user input:
  115.     void inputLogic(int charTyped) {
  116.         // If the key pressed isn't delete, or the two selection keys, then append the text with the char:
  117.         if (charTyped != DELETE_KEY && charTyped != ENTER_KEY && charTyped != ESCAPE_KEY) {
  118.             text << static_cast<char>(charTyped);
  119.         }
  120.         // If the key is delete, then delete the char:
  121.         else if (charTyped == DELETE_KEY) {
  122.             if (text.str().length() > 0) {
  123.                 deleteLastChar();
  124.             }
  125.         }
  126.         // Set the textbox text:
  127.         textbox.setString(text.str() + "_");
  128.     }
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement