Project

General

Profile

Download (3.8 KB) Statistics
| Branch: | Revision:

sokoban / distorted-sokoban / src / main / java / org / distorted / keyboard / TextFieldArea.java @ c2d85688

1
package org.distorted.keyboard;
2

    
3
import android.graphics.Canvas;
4
import android.graphics.Paint;
5
import android.graphics.Paint.Style;
6

    
7
///////////////////////////////////////////////////////////////////
8

    
9
public class TextFieldArea 
10
    {
11
	//private static final String TAG_TEXT = "RRText";
12
	
13
    private StringBuffer text;
14
 
15
    private int textFieldBkgColor    = 0xFFEEEEEE;
16
    private int textFieldBorderColor = 0xFF000000;
17
    private int textFieldFontColor   = 0xFF000000;
18
   
19
    private int maxSize = -1;
20
    private int strLen;
21
    private Paint mPaint;
22
    
23
///////////////////////////////////////////////////////////////////
24
    
25
    public TextFieldArea() 
26
    {
27
        text   = new StringBuffer();
28
        strLen = 0;
29
        mPaint = new Paint();
30
        mPaint.setAntiAlias(true);
31
    }
32

    
33
///////////////////////////////////////////////////////////////////
34
    
35
    public void deleteLastChar() 
36
    {
37
        if (strLen > 0) 
38
        {
39
            text.deleteCharAt(strLen-1);
40
            strLen--;
41
        }
42
    }
43

    
44
///////////////////////////////////////////////////////////////////
45
    
46
    public void insertNewChar(char aChar) 
47
    {
48
        if ( ((maxSize > 0) && (text.length() < maxSize)) || (maxSize < 0)) 
49
        { 
50
            if (aChar != 0) 
51
            {
52
                text.insert(strLen, aChar);
53
                strLen++;
54
            }
55
        }
56
    }
57

    
58
///////////////////////////////////////////////////////////////////
59
    
60
    public String getText() 
61
    {
62
        return text.toString();
63
    }
64

    
65
///////////////////////////////////////////////////////////////////
66
    
67
    public void setText(String newText) 
68
    {
69
        text   = null;
70
        text   = new StringBuffer(newText);
71
        strLen = newText.length();
72
    }
73

    
74
///////////////////////////////////////////////////////////////////
75
   
76
    public void clearTextField() 
77
    {
78
        text = null;
79
        text = new StringBuffer();
80
        strLen = 0;
81
    }
82

    
83
///////////////////////////////////////////////////////////////////
84
    
85
    public void paint( Canvas canvas, int left, int top, int right, int bottom) 
86
    {
87
    	int fontH = (int)((bottom-top)*0.7);
88
    	
89
        mPaint.setColor(textFieldBkgColor);
90
        mPaint.setStyle(Style.FILL);
91
        canvas.drawRect(left, top, right, bottom, mPaint);
92
        mPaint.setColor(textFieldBorderColor);
93
        mPaint.setStyle(Style.STROKE);
94
        canvas.drawRect(left, top, right, bottom, mPaint);
95
        mPaint.setColor(textFieldFontColor);
96
        mPaint.setTextSize(fontH);   
97
        
98
        String t = text.toString();
99
        
100
        while( mPaint.measureText(t) > right-left-(bottom-top)/2 )
101
          {
102
          mPaint.setTextSize(--fontH);
103
          }
104
        
105
        canvas.drawText( t, left+(bottom-top)/4, (bottom+top+fontH)/2-fontH/8, mPaint);
106
    }
107

    
108
////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void setTextFieldBkgColor(int textFieldBkgColor) 
111
    {
112
        this.textFieldBkgColor = textFieldBkgColor;
113
    }
114

    
115
////////////////////////////////////////////////////////////////////////////////
116
    
117
    public void setTextFieldBorderColor(int textFieldBorderColor)
118
    {
119
        this.textFieldBorderColor = textFieldBorderColor;
120
    }
121

    
122
////////////////////////////////////////////////////////////////////////////////
123
 
124
    public void setTextFieldFontColor(int textFieldFontColor) 
125
    {
126
        this.textFieldFontColor = textFieldFontColor;
127
    }
128

    
129
////////////////////////////////////////////////////////////////////////////////
130

    
131
    public int getMaxSize() 
132
    {
133
        return maxSize;
134
    }
135

    
136
////////////////////////////////////////////////////////////////////////////////
137
  
138
    public void setMaxSize(int maxSize) 
139
    {
140
        this.maxSize = maxSize;
141
    }
142
////////////////////////////////////////////////////////////////////////////////
143
// end of TextFieldArea    
144
}
(2-2/3)