OSDN Git Service

draw operable rects
[sharp4k/CUTEn.git] / ECC_evenodd / src / jp / ac / titech / sharp4k / cuten / sampletask / MatrixView.java
1 package jp.ac.titech.sharp4k.cuten.sampletask;
2
3 import android.content.Context;
4 import android.content.res.Resources;
5 import android.content.res.TypedArray;
6 import android.graphics.Canvas;
7 import android.graphics.Color;
8 import android.graphics.Paint;
9 import android.graphics.Paint.FontMetrics;
10 import android.graphics.Paint.Style;
11 import android.view.MotionEvent;
12 import android.view.View;
13
14 public class MatrixView extends View {
15         private Resources res;
16         private float width, height;
17         private int row, col;
18         private int[] opcol;
19         private String[][] bits, cAnswer;
20         private TypedArray question;
21
22         public MatrixView(Context ctx, Resources res, int questionId) {
23                 super(ctx);
24                 setFocusable(true);
25                 setFocusableInTouchMode(true);
26                 this.res = res;
27                 question = res.obtainTypedArray(questionId);
28                 row = question.getInteger(0, -1);
29                 col = question.getInteger(1, -1);
30                 opcol = res.getIntArray(question.getResourceId(2, -1));
31                 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
32                 bits = new String[code.length()][];
33                 for (int i = 0; i < code.length(); i++) {
34                         bits[i] = res.getStringArray(code.getResourceId(i, -1));
35                 }
36                 cAnswer = new String[opcol.length - 1][];
37                 TypedArray answer = res.obtainTypedArray(question.getResourceId(4, -1));
38                 for (int i = 0; i < answer.length(); i++) {
39                         cAnswer[i] = res.getStringArray(answer.getResourceId(i, -1));
40                 }
41         }
42
43         @Override
44         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
45                 width = w / (col + 2);
46                 height = h / (row + 2);
47                 super.onSizeChanged(w, h, oldw, oldh);
48         }
49
50         @Override
51         protected void onDraw(Canvas canvas) {
52                 // draw operable Rects
53                 Paint opRect = new Paint();
54                 opRect.setARGB(50, 240, 248, 255);
55                 for (int i = 0; i < opcol.length; i++) {
56                         if (i == opcol.length - 1) {
57                                 canvas.drawRect(width * opcol[i], height, width
58                                                 * (opcol[i] + 1), height * (row + 1), opRect);
59                         } else {
60                                 canvas.drawRect(width * opcol[i], height, width
61                                                 * (opcol[i] + 1), height * row, opRect);
62                         }
63                 }
64
65                 // draw matrix line
66                 Paint line = new Paint();
67                 line.setColor(Color.GRAY);
68                 for (int i = 1; i < row + 2; i++) {
69                         canvas.drawLine(width, i * height, width * (col + 1), i * height,
70                                         line);
71                 }
72                 for (int i = 1; i < col + 2; i++) {
73                         canvas.drawLine(i * width, height, i * width, height * (row + 1),
74                                         line);
75                 }
76
77                 // draw cross in unused box
78                 canvas.drawLine(width * (col - 1), height * row, width * col, height
79                                 * (row + 1), line);
80                 canvas.drawLine(width * col, height * row, width * (col - 1), height
81                                 * (row + 1), line);
82
83                 // draw boundary between code and extra row
84                 Paint boundary = new Paint();
85                 boundary.setColor(Color.BLUE);
86                 canvas.drawLine(width, height * row, width * (col + 1), height * row,
87                                 boundary);
88
89                 // draw checksum line
90                 Paint cline = new Paint();
91                 cline.setColor(Color.RED);
92                 canvas.drawLine(width * (col - 1), height, width * (col - 1), height
93                                 * row - 2, cline);
94                 canvas.drawLine(width * col, height, width * col, height * row - 2,
95                                 cline);
96                 canvas.drawLine(width * (col - 1), height, width * col, height, cline);
97                 canvas.drawLine(width * (col - 1), height * row - 2, width * col,
98                                 height * row - 2, cline);
99                 canvas.drawLine(width * col, height, width * col, height * (row + 1),
100                                 cline);
101                 canvas.drawLine(width * (col + 1), height, width * (col + 1), height
102                                 * (row + 1), cline);
103                 canvas.drawLine(width * col, height, width * (col + 1), height, cline);
104                 canvas.drawLine(width * col, height * (row + 1), width * (col + 1),
105                                 height * (row + 1), cline);
106
107                 /*
108                  * // selRects Paint selected = new Paint();
109                  * selected.setColor(Color.CYAN); for (Pair<Integer, Integer> i :
110                  * uAnswer) { canvas.drawRect(i.second * width, i.first * height,
111                  * i.second width + width, i.first * height + height, selected); }
112                  */
113
114                 // bits
115                 Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
116                 foreground.setColor(Color.GRAY);
117                 foreground.setStyle(Style.FILL);
118                 foreground.setTextSize(height * 0.75f);
119                 foreground.setTextScaleX(width / height);
120                 foreground.setTextAlign(Paint.Align.CENTER);
121                 FontMetrics fm = foreground.getFontMetrics();
122                 float x = width / 2;
123                 float y = height / 2 - (fm.ascent + fm.descent) / 2;
124                 for (int i = 1; i < row + 1; i++) {
125                         for (int j = 1; j < col + 1; j++) {
126                                 canvas.drawText(bits[i - 1][j - 1], j * width + x, i * height
127                                                 + y, foreground);
128                         }
129                 }
130         }
131
132         @Override
133         public boolean onTouchEvent(MotionEvent event) {
134                 if (event.getAction() != MotionEvent.ACTION_DOWN) {
135                         return super.onTouchEvent(event);
136                 } else {
137                         // position
138                         int selX, selY;
139                         selX = (int) (event.getX() / width);
140                         selY = (int) (event.getY() / height);
141                         if (selX <= 0 || selY <= 0 || selX > col || selY > row
142                                         || selX < col && selY == row) {
143                                 return true;
144                         }
145                         int check;
146                         for (check = 0; check < opcol.length; check++) {
147                                 if (selX == opcol[check]) {
148                                         break;
149                                 }
150                         }
151                         if (check == opcol.length) {
152                                 return true;
153                         }
154                         /*
155                          * // add user answer and inverse bit Pair<Integer, Integer> tmpAns
156                          * = new Pair<Integer, Integer>(selY, selX); if
157                          * (!uAnswer.remove(tmpAns)) { uAnswer.add(tmpAns); }
158                          */
159                         if (bits[selY - 1][selX - 1].equals("0")) {
160                                 bits[selY - 1][selX - 1] = "1";
161                         } else {
162                                 bits[selY - 1][selX - 1] = "0";
163                         }
164                         invalidate();
165                 }
166                 return true;
167         }
168
169         public void reset() {
170                 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
171                 bits = new String[code.length()][];
172                 for (int i = 0; i < code.length(); i++) {
173                         bits[i] = res.getStringArray(code.getResourceId(i, -1));
174                 }
175                 invalidate();
176         }
177
178         public int mark() {
179                 for (int i = 0; i < opcol.length - 1; i++) {
180                         for (int j = 0; j < row - 1; j++) {
181                                 if (!bits[j][opcol[i] - 1].equals(cAnswer[i][j])) {
182                                         return 0;
183                                 }
184                         }
185                 }
186                 return 100;
187         }
188 }