Commits

blipi committed f65776b

v0.6
Added much more features
Fixed some bugs
R2 bugged?

  • Participants
  • Parent commits 1b8fe35

Comments (0)

Files changed (3)

 #include <sys/stat.h>
 #include <sysutil/events.h>
 #include <io/msg.h>
-#include <io/pad.h> 
+#include <io/pad.h> 
 #include <io/mouse.h> 
 #include <sysmodule/sysmodule.h>
 

File oskdialog.cpp

 };
 
 block_data lblock = {0}, rblock = {0}, ublock = {0}, dblock = {0};
-block_data xblock = {0};
-
-
+block_data l1block = {0}, r1block = {0}, r2block = {0};
+block_data xblock = {0}, tblock = {0};
 
 void TTF_to_Bitmap(u8 chr, u8 * bitmap, short *w, short *h, short *y_correction){
 	FT_Set_Pixel_Sizes(face, (*w), (*h));
 	*h = slot->bitmap.rows;
 }
 
+void TTF_get_Size(u8 chr, u8 *w, u8 *h){    
+	if(!face)
+		return;
+
+	FT_GlyphSlot slot = face->glyph;
+	if(!slot)
+		return;
+
+	if(FT_Load_Char(face, (char) chr, FT_LOAD_RENDER ))
+		return;
+
+	*w = (slot->advance.x >> 6) + slot->bitmap_left;
+	*h = (slot->advance.x >> 6) + ((slot->bitmap_left < 0) ? -slot->bitmap_left : 0);
+}
+
 void Debug(char *format, ...){
 	char str[1024];
 	char filename[1024];
 		sprintf(path, "%s/BTN.PNG", usrdir);
 		this->btn = this->png_loader_f(path, this->texture_pointer);
 		this->texture_pointer = this->btn->texture_pointer;
-
-		sprintf(path, "%s/SPACE.PNG", usrdir);
-		this->space = this->png_loader_f(path, this->texture_pointer);		
-		this->texture_pointer = this->space->texture_pointer;
-
+		
 		sprintf(path, "%s/DEL.PNG", usrdir);
 		this->del = this->png_loader_f(path, this->texture_pointer);
 		this->del->width = 35.3333 + 35.3333/2;
 		
 		sprintf(path, "%s/BTN.PNG", usrdir);
 		this->btn = (*this->png_loader)(path);
-
-		sprintf(path, "%s/SPACE.PNG", usrdir);
-		this->space = (*this->png_loader)(path);
-		
+				
 		sprintf(path, "%s/DEL.PNG", usrdir);
 		this->del = (*this->png_loader)(path);
 		this->del->width = 35.3333 + 35.3333/2;
     if(FT_New_Face(freetype, path, 0, &face)) return NULL;
 	
 	this->texture_pointer = (u32 *) AddFontFromTTF((u8 *) texture_pointer, 32, 255, 32, 32, TTF_to_Bitmap);
-
-	FT_Done_FreeType(freetype);
-	ttf_inited = 0;
-
+	
 	return this->texture_pointer;
 }
 
 		SetFontSize(18, 20);
 		SetFontColor(0xFFFFFFFF, 0x0);
 		
-		DrawString(x + w/2 + 15, y + h/2 - 20/2 - 5, this->buffer);
+		DrawString(x + w/2 + 15, y + h/2 - 20/2 - 5, "");
+		u16 len = strlen(this->buffer);
+		if(len > 0){
+			for(int i = 0; i < len; i++){
+				DrawFormatString(GetFontX(), y + h/2 - 20/2 - 5, "%c", this->buffer[i]);
+			
+				if(i+1 == cursor_pos && show_cursor){
+					float tx = GetFontX();
+					DrawString(GetFontX()-0.5f, y + h/2 - 20/2 - 5, "|");
+					DrawString(tx, 0, "");
+				}
+			}
+		}
+		
+		if(show_cursor && (len == 0 || cursor_pos == 0))
+			DrawString(x + w/2 + 15 - 0.5f, y + h/2 - 20/2 - 5, "|");
+
+		timeval time_now;
+		gettimeofday(&time_now, NULL);
+		long cur_time = time_now.tv_sec * 1000 + time_now.tv_usec/1000;
+		long old_time = old_cursor.tv_sec * 1000 + old_cursor.tv_usec/1000;
+		
+		if(cur_time - old_time >= 400){
+			show_cursor = !show_cursor;
+			gettimeofday(&old_cursor, NULL);
+		}
 	}
 
 	this->btn->width = w;
 
 				if(this->font_slot >= 0)
 					DrawFormatString(x + w/2 - 18/2 + 3, y + h/2 - 20/2 - 5, "%c", key_array[this->mode][i][n]);
-			
+
 				x += w + 5;
 
 				if(i == 0 && n == 9){
 						this->DrawBorders2D(0xFFFFFFFF, x+5, y, this->del->width-1, this->del->height, 3);
 				}
 			}else{
-				//SPECIAL KEYS
+				//SPECIAL KEYS			
 
+				// ---- "<-" ---- //
+				this->btn->x = this->pos.x;
+				this->btn->y = y;
+				this->btn->width = w;
+				
+				if(this->png_displayer != NULL)
+					(*this->png_displayer)(this->btn);
+				else
+					this->png_displayer_f(this->btn);
+
+				DrawString(this->btn->x + w/2 - 18 + 13, y + h/2 - 20/2 - 5, "<-");
+				
+				if(this->sel_row == 4 && this->sel_num == 0)
+					this->DrawBorders2D(0xFFFFFFFF, this->btn->x, this->btn->y, this->btn->width-1, this->btn->height, 3);
+
+				// ---- "->" ---- //
+				this->btn->x += w + 5;
+				if(this->png_displayer != NULL)
+					(*this->png_displayer)(this->btn);
+				else
+					this->png_displayer_f(this->btn);
+
+				DrawString(this->btn->x + w/2 - 18 + 13, y + h/2 - 20/2 - 5, "->");
+				
+				if(this->sel_row == 4 && this->sel_num == 1)
+					this->DrawBorders2D(0xFFFFFFFF, this->btn->x, this->btn->y, this->btn->width-1, this->btn->height, 3);
+
+				// ---- MODE ---- //
+
+				// ---- SPACER ---- //
+				this->btn->x = x + 2*w + w/2;
+				this->btn->width = (w+5)*5;
+
+				if(this->png_displayer != NULL)
+					(*this->png_displayer)(this->btn);
+				else
+					this->png_displayer_f(this->btn);
+
+				if(this->sel_row == 4 && this->sel_num == 2)
+					this->DrawBorders2D(0xFFFFFFFF, this->btn->x, this->btn->y, this->btn->width-1, this->btn->height, 3);
 			}
 		}
 
 	return 0;
 }
 
+void OSK::delAtPos(){
+	if(this->cursor_pos <= 0)
+		return;
+
+	u8 len = strlen(this->buffer);
+
+	for(int i = cursor_pos-1; i < len-1; i++)
+		this->buffer[i] = this->buffer[i+1];
+			
+	this->buffer[len-1] = '\0';
+
+	cursor_pos--;
+}
+
+void OSK::handleInput(){
+	u8 len = strlen(this->buffer);
+
+	if(this->sel_num >= 0 && this->sel_num <= 9 && this->sel_row >= 0 && this->sel_row <= 3){		
+		if(len >= 255)
+			return;
+		
+		if(cursor_pos != len){
+			for(int i = len; i > this->cursor_pos; i--)
+				this->buffer[i] = this->buffer[i-1];
+		}
+
+		this->buffer[cursor_pos] = key_array[this->mode][this->sel_row][this->sel_num];
+		
+		cursor_pos++;
+	}else{
+		//Special cases
+		if(this->sel_row == 0 && this->sel_num == 10){ //DEL
+			delAtPos();
+		}
+		else if(this->sel_row == 4 && this->sel_num == 0){ //<-
+			if(cursor_pos > 0)
+				cursor_pos--;
+		}else if(this->sel_row == 4 && this->sel_num == 1){ //->
+			if(cursor_pos < len)
+				this->cursor_pos++;
+		}
+
+		if(strlen(this->buffer) >= 255)
+			return;
+
+		if(this->sel_row == 4 && this->sel_num == 2){ //SPACE
+			if(cursor_pos != len){
+				for(int i = len; i > this->cursor_pos; i--)
+					this->buffer[i] = this->buffer[i-1];
+			}
+
+			this->buffer[cursor_pos] = key_array[this->mode][this->sel_row][this->sel_num];
+
+			cursor_pos++;
+		}
+	}
+}
+
 u8 OSK::handlePad(){
 	if(this->status != OSK_RUNNING)
 		return -1;
 			if(paddata.BTN_CROSS){
 				if(!xblock.blocked){
 					
-					if(this->sel_num >= 0 && this->sel_num <= 9 && this->sel_row >= 0 && this->sel_row <= 3){
-						if(strlen(this->buffer) >= 255)
-							continue;
-
-						char buf[5];
-						sprintf(buf, "%c", key_array[this->mode][this->sel_row][this->sel_num]);
-						strcat(this->buffer, buf);
-					}else{
-						//Special cases
-						if(this->sel_row == 0 && this->sel_num == 10) //DEL
-							this->buffer[strlen(this->buffer)-1] = '\0';
-						
-					}
+					this->handleInput();
 
 					xblock.blocked = 1;
 					xblock.delay = 1000;
 					long elapsed = ((cur_time-xblock.uval > 0)?cur_time-xblock.uval:xblock.uval-cur_time);
 					if(elapsed >= xblock.delay){
 						
-						if(this->sel_num >= 0 && this->sel_num <= 9 && this->sel_row >= 0 && this->sel_row <= 3){
-							if(strlen(this->buffer) >= 255)
-								continue;
-
-							char buf[5];
-							sprintf(buf, "%c", key_array[this->mode][this->sel_row][this->sel_num]);
-							strcat(this->buffer, buf);
-						}else{
-							//Special cases
-							if(this->sel_row == 0 && this->sel_num == 10) //DEL
-								this->buffer[strlen(this->buffer)-1] = '\0';
-						
-						}
+						this->handleInput();
 
 						xblock.uval = cur_time;
 						xblock.delay = 200;
 			}else
 				xblock.blocked = 0;
 
+			if(paddata.BTN_TRIANGLE){
+				if(!tblock.blocked){
+					
+					this->delAtPos();
+
+					tblock.blocked = 1;
+					tblock.delay = 1000;
+					tblock.uval = cur_time;
+				}else{
+					long elapsed = ((cur_time-tblock.uval > 0)?cur_time-tblock.uval:tblock.uval-cur_time);
+					if(elapsed >= tblock.delay){
+						
+						this->delAtPos();
+
+						tblock.uval = cur_time;
+						tblock.delay = 200;
+					}
+				}
+			}else
+				tblock.blocked = 0;
+
+			if(paddata.BTN_L1){
+				if(!l1block.blocked){
+					
+					if(this->cursor_pos > 0)
+						this->cursor_pos--;
+
+					l1block.blocked = 1;
+					l1block.delay = 1000;
+					l1block.uval = cur_time;
+				}else{
+					long elapsed = ((cur_time-l1block.uval > 0)?cur_time-l1block.uval:l1block.uval-cur_time);
+					if(elapsed >= l1block.delay){
+						
+						if(this->cursor_pos > 0)
+							this->cursor_pos--;
+
+						l1block.uval = cur_time;
+						l1block.delay = 200;
+					}
+				}
+			}else
+				l1block.blocked = 0;
+
+			if(paddata.BTN_R1){
+				if(!r1block.blocked){
+					
+					if(cursor_pos < strlen(this->buffer))
+						this->cursor_pos++;
+
+					r1block.blocked = 1;
+					r1block.delay = 1000;
+					r1block.uval = cur_time;
+				}else{
+					long elapsed = ((cur_time-r1block.uval > 0)?cur_time-r1block.uval:r1block.uval-cur_time);
+					if(elapsed >= r1block.delay){
+						
+						if(cursor_pos < strlen(this->buffer))
+							this->cursor_pos++;
+
+						r1block.uval = cur_time;
+						r1block.delay = 200;
+					}
+				}
+			}else
+				r1block.blocked = 0;
+
+			if(paddata.BTN_R2 && !r2block.blocked){
+				if(this->mode == 0)
+					this->mode = 1;
+				else
+					this->mode = 0;
+
+				r2block.blocked = 1;
+			}else
+				r2block.blocked = 0;
+
 			if(paddata.BTN_START){
 				this->status = OSK_RETURN;
 				//Callback
 				this->sel_row += plusr;
 
 				if(this->sel_row < 0)
-					this->sel_row = 3;
-				else if(this->sel_row >= 4)
+					this->sel_row = 4;
+				else if(this->sel_row >= 5)
 					this->sel_row = 0;
+
+				if(this->sel_row == 4 && this->sel_num > 3)
+					this->sel_num = 2;
 			}
 
 			if(plusn != 0){
 				this->sel_num += plusn;
+				
+				u8 max_num = 10;
+				if(this->sel_row == 0)
+					max_num = 11;
+				else if(this->sel_row == 4)
+					max_num = 3;
 
 				if(this->sel_num < 0)
-					this->sel_num = 9;
-				else{
-					u8 max_num = ((this->sel_row==0)?11:10);
-
-					if(this->sel_num >= max_num)
-						this->sel_num = 0;
-				}
+					this->sel_num = max_num-1;
+				else if(this->sel_num >= max_num)
+					this->sel_num = 0;
+				
 			}
 
 			break; //Solo queremos el PAD 0
 	s8 sel_num;
 	s8 sel_row;
 
+	u8 cursor_pos;
+	u8 show_cursor;
+	timeval old_cursor;
+
 	char buffer[256];
 
+	void delAtPos();
+	void handleInput();
+
 	static _png_loader* png_loader_f(char * path, u32 *texture_pointer){
 		_png_loader *png = (_png_loader*)malloc(sizeof(_png_loader));
 		memset(png, 0, sizeof(_png_loader));
 	OSK(u32 *texture_pointer, char *usrdir);
 	OSK(method_a *png_loader, method_b *png_displayer, char *usrdir);
 
+	~OSK(){ FT_Done_FreeType(freetype); };
+
 	u32* loadFont(u8 slot, char *usrdir, u32 *texture_pointer);
 
 	u8 draw();