Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created September 18, 2013 14:37
Show Gist options
  • Save ynonp/6610132 to your computer and use it in GitHub Desktop.
Save ynonp/6610132 to your computer and use it in GitHub Desktop.
#include "typer.h"
#include <QDebug>
Typer::Typer(QObject *parent) :
QObject(parent)
{
m_text = "The quick brown fox jumps over the lazy dog";
m_idx = 0;
}
void Typer::letterPressed(QString letter)
{
if ( letter.isEmpty() ) return;
if ( m_idx >= m_text.size() ) return;
qDebug() << m_text;
QString rpl;
if ( m_text.at(m_idx) == letter )
{
rpl = QString("<font color=\"green\">%1</font>").arg(letter);
}
else
{
rpl = QString("<font color=\"red\">%1</font>").arg(m_text.at(m_idx));
}
m_text.replace(m_idx, 1, rpl);
qDebug() << m_text;
m_idx += rpl.size();
}
QString Typer::getText()
{
return m_text;
}
#ifndef TYPER_H
#define TYPER_H
#include <QObject>
#include <QString>
class Typer : public QObject
{
Q_OBJECT
public:
explicit Typer(QObject *parent = 0);
Q_INVOKABLE QString getText();
signals:
void gameEnd();
public slots:
void letterPressed(QString letter);
private:
QString m_text;
int m_idx;
};
#endif // TYPER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment