A few weeks ago, I had started to write a text-editor in C++ using SFML library. The work is now paused pretty much because I am a lazy freak. So, I thought I might use this passive time to get reviews on what I have done so far.
As of now, I want to get reviews on the cursor module (for displaying a blinking cursor), I wrote for the text-editor. I am posting the code as it was written and it was written without many comments. (I am not sure if I am supposed to add comments before putting it up for review on the site.)
Cursor.h:
#ifndef CURSOR_H_INCLUDED
#define CURSOR_H_INCLUDED
#include<iostream>
#include<SFML/Graphics.hpp>
class Cursor{
private:
sf::Color color;
sf::RectangleShape cursorLine;
sf::Clock clock;
int col, line;
int txtX, txtY;
int maxCol;
float thickness, length;
int blinkPeriod, lastTime;
bool visible;
public:
float getCol();
float getLine();
float getLength();
float getThickness();
void move(int, int);
void setPosition(int,int);
void setDefaultValues();
Cursor();
Cursor(float, float, float, float, int, int, int);
void draw(sf::RenderWindow *);
void update();
};
#endif // CURSOR_H_INCLUDED
Cursor.cpp:
#include "Cursor.h"
void Cursor::setDefaultValues(){
col = 1;
line = 1;
lastTime = 0;
thickness = 2;
length = 20;
blinkPeriod = 400;
visible = true;
color = sf::Color(0,255,0);
}
Cursor::Cursor(){
setDefaultValues();
}
Cursor::Cursor(float x, float y, float len, float thick, int txX, int txY, int maxCol){
setDefaultValues();
this->maxCol = maxCol;
txtX = txX;
txtY = txY;
col = x;
line = y;
length = len;
thickness = thick;
}
float Cursor::getCol(){
return col;
}
float Cursor::getLine(){
return line;
}
float Cursor::getLength(){
return length;
}
float Cursor::getThickness(){
return thickness;
}
void Cursor::move(int dx, int dy){
if(dx>0){
if(col+dx<=maxCol)col += dx;
else{
col = 2;
line += 1;
}
}
else{
if(col+dx>0) col +=dx;
else{
if(line>1){
line -= 1;
col = maxCol-1;
}
}
}
if(line+dy>0)line+= dy;
}
void Cursor::setPosition(int dx, int dy){
col = dx;
line= dy;
}
void Cursor::update(){
cursorLine.setFillColor(color);
cursorLine.setSize(sf::Vector2f(thickness, length));
if((clock.getElapsedTime().asMilliseconds()-lastTime)>blinkPeriod){ visible = !visible;
lastTime = clock.getElapsedTime().asMilliseconds();
}
cursorLine.setPosition((col-1)*(length*0.6)+txtX, (line-1)*(length*1.2)+txtY);
}
void Cursor::draw(sf::RenderWindow *screen){
if(visible) screen->draw(cursorLine);
}