Lab4

Dependencies:   SeeedStudioTFTv2 TFT_fonts mbed

Fork of Seeed_TFT_Touch_Shield by Shields

Revision:
6:ebffa73d4f95
Parent:
4:ebcf8d366b91
--- a/Panel.cpp	Fri Sep 26 12:42:10 2014 +0000
+++ b/Panel.cpp	Fri Sep 26 15:33:34 2014 +0000
@@ -1,4 +1,5 @@
 #include "Panel.h"
+#include "Arial12x12.h"
 
 Panel::Panel(int x_0, int y_0, int x_1, int y_1, int p_color, int bd_color, int r = 1, int c = 1)
 {
@@ -7,7 +8,7 @@
     x1 = x_1;
     y1 = y_1 ;
     init(p_color, bd_color, r, c);
-        //calculate pixels per row/col
+    //calculate pixels per row/col
     pp_row = TFT.height() / (rows+0.0) ;
     pp_col = TFT.width() / (cols+0.0) ;
     redraw();
@@ -41,6 +42,11 @@
     //state info
     state_changed = true ;
     push_children = true ;
+    draw_text = false;
+    cursor_x0 = x0;
+    cursor_y0 = y0 ;
+    next_cursor_x0 = cursor_x0 ;
+    next_cursor_y0 = cursor_y0;
 }
 
 void Panel::paint()
@@ -48,17 +54,23 @@
     TFT.rect(x0,y0,x1,y1,border_color);
     //paint fill
     TFT.fillrect(x0+2,y0+2,x1-2,y1-2,back_color);
+    if(draw_text) {
+        TFT.set_font((unsigned char*) Arial12x12);
+        TFT.locate(cursor_x0 ,cursor_y0);
+        TFT.printf(input_string);
+        draw_text = false;
+    }
 }
 
 bool Panel::addWidget(AbstractWidget *p)
 {
     //include as a child
-    if(push_children){
+    if(push_children) {
         children.push_back(p);
     }
-    
+
     //adjust pixels per row/col values for panel 'p'
-    //this is [number of pixels taken by p] / [row or col divisions] 
+    //this is [number of pixels taken by p] / [row or col divisions]
     p->pp_row = (pp_row*p->height)/p->rows;
     p->pp_col = (pp_col*p->width)/p->cols;
     //re-adjust absolute cordinates of Panel 'p'
@@ -78,14 +90,14 @@
                 p->x0 = x0 + pp_col*curr_col ;
                 p->y0 = y0 + pp_row*curr_row ;
                 p->x1 = p->x0 + pp_col*p->width;
-                if(p->x1 + pp_col*1 > x1){
+                if(p->x1 + pp_col*1 > x1) {
                     //lookahead if the panel that is added now is in the last coloumn adjust it x corner to boundary
-                    p->x1 = x1;    
+                    p->x1 = x1;
                 }
                 p->y1 = p->y0 + pp_row*p->height;
-                if(p->y1 + pp_row*1 > y1){
+                if(p->y1 + pp_row*1 > y1) {
                     //if the panel that is added is in the last row then adjust its x corner to boundary
-                    p->y1 = y1;    
+                    p->y1 = y1;
                 }
                 //keep previous row,col for updates to this widget
                 prev_col = curr_col;
@@ -113,10 +125,36 @@
             curr_col = 0 ;
         }
     }
-    
+
     pc.printf("c_row: %d c_col: %d --- child x0,y0 : %d,%d  x1,y1 : %d,%d --- pp_row, pp_col : %f,%f \r\n", curr_row, curr_col,
-                                p->x0,p->y0,p->x1,p->y1,p->pp_row ,p->pp_col);
+              p->x0,p->y0,p->x1,p->y1,p->pp_row ,p->pp_col);
 
     return true;
 
 }
+
+void Panel::trigger_action(ActionType type, ActionEvent evnt, void* target)
+{
+    //do something
+    if(type == CORD_STR) {
+        char* input_str = evnt.str;
+        int len = strlen(input_str);
+
+        int x_pixels = len*12;
+        cursor_x0 = next_cursor_x0 ;
+        cursor_y0 = next_cursor_y0 ;
+
+        if(cursor_x0 < 240 && cursor_y0 < 320 ) {
+            if(cursor_x0 + x_pixels >= 240) {
+                next_cursor_y0 = cursor_y0 + 14;
+                next_cursor_x0 = x0 ;
+            } else {
+                next_cursor_x0 = (cursor_x0 + x_pixels);
+            }
+            input_string = input_str;
+            draw_text = true;
+            state_changed = true;
+        }
+
+    }
+}