text-box cursor rendering/improvment
in
Programming Questions
•
1 year ago
For one of my programs I need a text-box to enter names/descriptions and so I wrote a quick sketch to test a text-box class so this is what I wrote:
- PFont defaultFont;
- textBox textbox = new textBox();
- void setup(){
- defaultFont = loadFont("Courier-16.vlw");
- textFont(defaultFont,16);
- size(500,500);
- }
- void draw(){
- background(255);
- textbox.render(50,50,400,400,0,0,0,128,128,0,0,0,0);
- println(key);
- }
- class textBox{
- String localdata;
- String defaultdata;
- boolean pressed;
- char pKey;
- int localtimer;
- int blinktimer;
- void textBox(){
- }
- void render(int x,int y,int w,int h,int rb,int gb,int bb,int ra,int ga,int ba,int rf,int gf,int bf){
- noFill();
- if(pressed){
- stroke(ra,ga,ba);
- }else{
- stroke(rb,gb,bb);
- }
- rect(x,y,w,h);
- if(mousePressed){
- if(mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
- pressed = true;
- }else{
- pressed = false;
- }
- }
- fill(rf,gf,bf);
- if(localdata != null){
- if(blinktimer < 20 && pressed){
- text(localdata + "|",x+2,y,w,h);
- }else{
- text(localdata,x+2,y,w,h);
- }
- }else if(blinktimer < 20 && pressed){
- text("|",x+2,y,w,h);
- }
- if(blinktimer <= 40){
- blinktimer++;
- }else{
- blinktimer = 0;
- }
- if(pressed){
- if(keyPressed){
- if(key >= 32 && key <= 126 && keyCode != CONTROL && key != CODED){
- if(key != pKey){
- localtimer =0;
- }
- pKey = key;
- if(localtimer == 0 || localtimer > 20){
- if(localdata != null){
- localdata = localdata + Character.toString(key);
- }else{
- localdata = Character.toString(key);
- }
- }
- if(localtimer <100){
- localtimer++;
- }
- }
- }else{
- localtimer = 0;
- }
- if(keyCode == BACKSPACE && localdata.length() > 0){
- if(localtimer == 0 || localtimer > 20){
- String temp_localdatadub = localdata;
- localdata = temp_localdatadub.substring(0,temp_localdatadub.length()-1);
- keyCode = 0;
- }
- }else if(keyCode == ENTER || keyCode == RETURN){
- if(localtimer == 0 || localtimer > 20){
- if(localdata != null){
- localdata = localdata + Character.toString('\n');
- }else{
- localdata = Character.toString('\n');
- }
- keyCode = 0;
- }
- if(localtimer <100){
- localtimer++;
- }
- }
- }
- }
- }
Now I am not that good at dealing with keyboard inputs or text-cursors so I just made it render with an extra "|" half the time to show where the end of the text is. Does anyone know how I could improve on this?
Note: I also know about the
controlP5 library but I am trying to avoid libraries as I intend to port my code to android (libraries + androids != good experiance for me) later and
controlP5 doesn't support exactly what I am looking to do anyways
1