c++ - 在屏幕上用 SFML 编写文本输入

标签 c++ sfml

所以我正在创建一个图形计算器。我有一个输入字符串 s。从字符串中,我可以使用 SFML 对其进行绘图。我从 MIN x 坐标到 MAX x 坐标开始,从 EvaluateString() 方法获取相应的 y,然后将所有坐标发送到 VertexArray v。我已经编写了我的方法和绘图方法,它们都运行良好.

但是,我有一个小问题。我想在屏幕上输入我的字符串,比如这样的“sin(cos(tan(x)))”。我正在努力寻找一种方法来做到这一点。我有点想通了它与事件 TextEntered 有关,但我仍然无法完全找到任何东西。

请给我一个方法。

Sample graph

class Calculator{
public:
    void main();
private:
    WindowSize DefaultWindow;
    sf::RenderWindow window;
    Cartesian vertexX[2],vertexY[2];
    sf::Vertex axis[4];
    const double MAX = 10;
    const double MIN = -10;
    const double INCREMENT = 0.001;

};

int main(){ 
    DefaultWindow.Max = Cartesian(10,10);
    DefaultWindow.Min = Cartesian(-10,-10);
    DefaultWindow.plane.width=1500;
    DefaultWindow.plane.height=1500;

    // Set up x and y-axis
    vertexX[0] = Cartesian(-100,0);
    vertexX[1] = Cartesian(100, 0);
    vertexY[0] = Cartesian(0,-100);
    vertexY[1] = Cartesian(0,100);

    axis[0] = sf::Vertex(convertCartesiantoWindow(vertexX[0],DefaultWindow));
    axis[1] = sf::Vertex(convertCartesiantoWindow(vertexX[1],DefaultWindow));
    axis[2] = sf::Vertex(convertCartesiantoWindow(vertexY[0],DefaultWindow));
    axis[3] = sf::Vertex(convertCartesiantoWindow(vertexY[1],DefaultWindow));

    // Set up the window
    window.create(sf::VideoMode(1500, 1500), "Graphing calculator");

    // Input string
    string s = "sin(cos(tan(x)))";

    // Stack c contains all the Cartesian coordinate vertices
    // Cartesian is a struct which contains x and y coordinates
    Stack<Cartesian> c;

    sf::VertexArray v;

    // For a certain function in string s, I evaluate it 
    // and return the y_coordinate from the function EvaluateString (s, i)
    // Push each (x,y) evaluated in the Stack c
    for (double i = MIN; i <= MAX; i+= INCREMENT)
        c.Push(Cartesian(i,EvaluateString(s,i)));

    // v is VertexArray which contains all the vertices (x,y)
    v = plot(DefaultWindow, c);


    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {

            switch (event.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
            }
        }
    }

    // Draw the graph
    window.clear(sf::Color::Black);
    window.draw(axis,4,sf::Lines);
    window.draw(v);
    window.display();
}

最佳答案

作为@super suggest ,使用库将是一个不错的解决方案,而且肯定比我的更好,但为了满足您的需求,我实现了一个 super 基础 TextField 类。

它可能有很多错误,但它可以让您了解如何实现该功能。

TextField 只不过是一个包含文本矩形。因为它将有一个 sf::Text,所以它必须有一个 sf::Font。此外,我限制它将包含的字符数。为了让我们在 TextField 中书写,我们必须知道它是否被选中,即它是否具有 焦点。因此,第一种方法可能是:

class TextField : public sf::Transformable, public sf::Drawable{
    private:
        unsigned int m_size;
        sf::Font m_font;
        std::string m_text;
        sf::RectangleShape m_rect;
        bool m_hasfocus;
};

我们需要这个类的构造函数:

class TextField : public sf::Transformable, public sf::Drawable{
    public:
        TextField(unsigned int maxChars) :
            m_size(maxChars),
            m_rect(sf::Vector2f(15 * m_size, 20)), // 15 pixels per char, 20 pixels height, you can tweak
            m_hasfocus(false)
        {
            m_font.loadFromFile("C:/Windows/Fonts/Arial.ttf"); // I'm working on Windows, you can put your own font instead
            m_rect.setOutlineThickness(2);
            m_rect.setFillColor(sf::Color::White);
            m_rect.setOutlineColor(sf::Color(127,127,127));
            m_rect.setPosition(this->getPosition());
        }

    private:
        unsigned int m_size;
        sf::Font m_font;
        std::string m_text;
        sf::RectangleShape m_rect;
        bool m_hasfocus;
};

我们还需要一些基本的方法,我们要获取里面的文字:

const std::string sf::TextField::getText() const{
    return m_text;
}

然后移动它,把它放在我们窗口的某个地方:

void sf::TextField::setPosition(float x, float y){
    sf::Transformable::setPosition(x, y);
    m_rect.setPosition(x, y);
}

这是一个棘手的问题。我们正在覆盖 setPosition method sf::Transformable 因为我们需要更新我们自己的 m_rect

此外,我们需要知道一个点是否在框内:

bool sf::TextField::contains(sf::Vector2f point) const{
    return m_rect.getGlobalBounds().contains(point);
}

很简单,我们使用sf::RectangleShapecointains方法,已经在中了.

设置(或取消设置)焦点在 TextField 上:

void sf::TextField::setFocus(bool focus){
    m_hasfocus = focus;
    if (focus){
        m_rect.setOutlineColor(sf::Color::Blue);
    }
    else{
        m_rect.setOutlineColor(sf::Color(127, 127, 127)); // Gray color
    }
}

简单的。为了美观,我们还更改了聚焦时框的轮廓颜色。

最后但同样重要的是,我们的 TextField 必须在接收到输入(又名 sf::Event)时以某种方式表现:

void sf::TextField::handleInput(sf::Event e){
    if (!m_hasfocus || e.type != sf::Event::TextEntered)
        return;

    if (e.text.unicode == 8){   // Delete key
        m_text = m_text.substr(0, m_text.size() - 1);
    }
    else if (m_text.size() < m_size){
        m_text += e.text.unicode;
    }
}

删除键检查有点脏,我知道。也许您可以找到更好的解决方案。

就是这样!现在 main 看起来像:

int main()
{
    RenderWindow window({ 500, 500 }, "SFML", Style::Close);

    sf::TextField tf(20);
    tf.setPosition(30, 30);

    while (window.isOpen())
    {
        for (Event event; window.pollEvent(event);)
            if (event.type == Event::Closed)
                window.close();
            else if (event.type == Event::MouseButtonReleased){
                auto pos = sf::Mouse::getPosition(window);
                tf.setFocus(false);
                if (tf.contains(sf::Vector2f(pos))){
                    tf.setFocus(true);
                }
            }
            else{
                tf.handleInput(event);
            }

            window.clear();
            window.draw(tf);
            window.display();
    }
    return 0;
}

概念验证:

enter image description here

关于c++ - 在屏幕上用 SFML 编写文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734084/

相关文章:

c++ - 如何访问 SFML 中的形状位置?

c++ - 游戏内 UI 的设计模式

c++ - 使用 Clang、libc++ 和 c++11 制作 SFML2 应用程序。对 SFML 库的 undefined reference

c++ - WASAPI + windows store 应用程序初始化

c++ - 用于更改静态成员初始化行为的简洁语法

c++ - boost 日期时间转换错误

c++ - 如何让我的圈子用键移动

c++ - 长数组性能问题

c++ - 从 Math C++ 获取美分

c++ - 什么时候应该在 SFML 中使用事件?