2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Typer.hpp"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace Utility;
|
|
|
|
|
|
|
|
Typer::Typer(const char *string, int delay, int frequency, Delegate *delegate) :
|
2016-06-19 17:46:53 +00:00
|
|
|
_counter(-delay), _frequency(frequency), _string(strdup(string)), _string_pointer(0), _delegate(delegate), _phase(0) {}
|
2016-06-19 17:10:52 +00:00
|
|
|
|
|
|
|
void Typer::update(int duration)
|
|
|
|
{
|
|
|
|
if(_string)
|
|
|
|
{
|
|
|
|
if(_counter < 0 && _counter + duration >= 0)
|
|
|
|
{
|
2016-06-19 17:46:53 +00:00
|
|
|
type_next_character();
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_counter += duration;
|
2016-07-07 01:39:09 +00:00
|
|
|
while(_string && _counter > _frequency)
|
2016-06-19 17:10:52 +00:00
|
|
|
{
|
|
|
|
_counter -= _frequency;
|
2016-06-19 17:46:53 +00:00
|
|
|
type_next_character();
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:33:24 +00:00
|
|
|
bool Typer::type_next_character()
|
2016-06-19 17:46:53 +00:00
|
|
|
{
|
2016-08-06 18:33:24 +00:00
|
|
|
if(_string == nullptr) return false;
|
|
|
|
|
2016-06-19 17:46:53 +00:00
|
|
|
if(_delegate->typer_set_next_character(this, _string[_string_pointer], _phase))
|
|
|
|
{
|
|
|
|
_phase = 0;
|
|
|
|
if(!_string[_string_pointer])
|
|
|
|
{
|
|
|
|
free(_string);
|
|
|
|
_string = nullptr;
|
2016-08-06 18:33:24 +00:00
|
|
|
return false;
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_string_pointer++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_phase++;
|
|
|
|
}
|
2016-08-06 18:33:24 +00:00
|
|
|
|
|
|
|
return true;
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 17:10:52 +00:00
|
|
|
Typer::~Typer()
|
|
|
|
{
|
|
|
|
free(_string);
|
|
|
|
}
|