erebus

diff src/console.cc @ 38:5e27c85e79ca

cursor handling in the console
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Jun 2014 18:40:30 +0300
parents db8a90307386
children 9d6368850fe1
line diff
     1.1 --- a/src/console.cc	Mon Jun 09 16:01:00 2014 +0300
     1.2 +++ b/src/console.cc	Mon Jun 09 18:40:30 2014 +0300
     1.3 @@ -135,7 +135,7 @@
     1.4  					--cursor;
     1.5  					must_redraw = true;
     1.6  				} else if(cursor > 0) {
     1.7 -					input.erase(cursor - 1);
     1.8 +					input.erase(cursor - 1, 1);
     1.9  					--cursor;
    1.10  					must_redraw = true;
    1.11  				}
    1.12 @@ -173,7 +173,11 @@
    1.13  
    1.14  		default:
    1.15  			if(c < 256 && isprint(c)) {
    1.16 -				input.push_back(c);
    1.17 +				if(cursor == (int)input.size()) {
    1.18 +					input.push_back(c);
    1.19 +				} else {
    1.20 +					input.insert(cursor, 1, c);
    1.21 +				}
    1.22  				++cursor;
    1.23  				must_redraw = true;
    1.24  			}
    1.25 @@ -215,7 +219,8 @@
    1.26  	}
    1.27  
    1.28  	// draw the output box
    1.29 -	float outbox_height = nlines * dtx_line_height();
    1.30 +	float line_height = dtx_line_height();
    1.31 +	float outbox_height = nlines * line_height;
    1.32  
    1.33  	glPushAttrib(GL_ENABLE_BIT);
    1.34  	glEnable(GL_BLEND);
    1.35 @@ -229,22 +234,34 @@
    1.36  	glVertex2f(0, 0);
    1.37  
    1.38  	glColor4f(0.5, 0.2, 0.1, 0.7);
    1.39 -	glVertex2f(0, -(outbox_height + dtx_line_height()));
    1.40 -	glVertex2f(max_width, -(outbox_height + dtx_line_height()));
    1.41 +	glVertex2f(0, -(outbox_height + line_height));
    1.42 +	glVertex2f(max_width, -(outbox_height + line_height));
    1.43  	glVertex2f(max_width, -outbox_height);
    1.44  	glVertex2f(0, -outbox_height);
    1.45  	glEnd();
    1.46  
    1.47  	// draw the output text
    1.48  	glPushMatrix();
    1.49 -	glTranslatef(0, -dtx_line_height(), 0);
    1.50 +	glTranslatef(0, -line_height, 0);
    1.51  	glColor4f(1, 1, 1, 0.7);
    1.52  	dtx_string(buflines.c_str());
    1.53  	glPopMatrix();
    1.54  
    1.55  	// draw the input line
    1.56 -	glTranslatef(0, -outbox_height - dtx_line_height(), 0);
    1.57 -	dtx_string((std::string("> ") + input).c_str());
    1.58 +	glTranslatef(0, -outbox_height - line_height, 0);
    1.59 +
    1.60 +	std::string inpline = std::string("> ") + input;
    1.61 +	dtx_string(inpline.c_str());
    1.62 +	glDisable(GL_TEXTURE_2D);
    1.63 +
    1.64 +	float cursor_x = dtx_char_pos(inpline.c_str(), cursor + 2);
    1.65 +	glBegin(GL_QUADS);
    1.66 +	glColor4f(1, 1, 1, 0.7);
    1.67 +	glVertex2f(cursor_x, 2);
    1.68 +	glVertex2f(cursor_x + 2, 2);
    1.69 +	glVertex2f(cursor_x + 2, line_height - 2);
    1.70 +	glVertex2f(cursor_x, line_height - 2);
    1.71 +	glEnd();
    1.72  
    1.73  	glPopAttrib();
    1.74