OSDN Git Service

e831f9fc7c0a2f722bb8ee3b87a540588863fed9
[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 matrix line
53                 Paint line = new Paint();
54                 line.setColor(Color.GRAY);
55                 for (int i = 1; i < row + 2; i++) {
56                         canvas.drawLine(width, i * height, width * (col + 1), i * height,
57                                         line);
58                 }
59                 for (int i = 1; i < col + 2; i++) {
60                         canvas.drawLine(i * width, height, i * width, height * (row + 1),
61                                         line);
62                 }
63
64                 // draw cross in unused box
65                 canvas.drawLine(width * (col - 1), height * row, width * col, height
66                                 * (row + 1), line);
67                 canvas.drawLine(width * col, height * row, width * (col - 1), height
68                                 * (row + 1), line);
69
70                 // draw boundary between code and extra row
71                 Paint boundary = new Paint();
72                 boundary.setColor(Color.BLUE);
73                 canvas.drawLine(width, height * row, width * (col + 1), height * row,
74                                 boundary);
75
76                 // draw checksum line
77                 Paint cline = new Paint();
78                 cline.setColor(Color.RED);
79                 canvas.drawLine(width * (col - 1), height, width * (col - 1), height
80                                 * row - 2, cline);
81                 canvas.drawLine(width * col, height, width * col, height * row - 2,
82                                 cline);
83                 canvas.drawLine(width * (col - 1), height, width * col, height, cline);
84                 canvas.drawLine(width * (col - 1), height * row - 2, width * col,
85                                 height * row - 2, cline);
86                 canvas.drawLine(width * col, height, width * col, height * (row + 1),
87                                 cline);
88                 canvas.drawLine(width * (col + 1), height, width * (col + 1), height
89                                 * (row + 1), cline);
90                 canvas.drawLine(width * col, height, width * (col + 1), height, cline);
91                 canvas.drawLine(width * col, height * (row + 1), width * (col + 1),
92                                 height * (row + 1), cline);
93
94                 /*
95                  * // selRects Paint selected = new Paint();
96                  * selected.setColor(Color.CYAN); for (Pair<Integer, Integer> i :
97                  * uAnswer) { canvas.drawRect(i.second * width, i.first * height,
98                  * i.second width + width, i.first * height + height, selected); }
99                  */
100
101                 // bits
102                 Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
103                 foreground.setColor(Color.GRAY);
104                 foreground.setStyle(Style.FILL);
105                 foreground.setTextSize(height * 0.75f);
106                 foreground.setTextScaleX(width / height);
107                 foreground.setTextAlign(Paint.Align.CENTER);
108                 FontMetrics fm = foreground.getFontMetrics();
109                 float x = width / 2;
110                 float y = height / 2 - (fm.ascent + fm.descent) / 2;
111                 for (int i = 1; i < row + 1; i++) {
112                         for (int j = 1; j < col + 1; j++) {
113                                 canvas.drawText(bits[i - 1][j - 1], j * width + x, i * height
114                                                 + y, foreground);
115                         }
116                 }
117         }
118
119         @Override
120         public boolean onTouchEvent(MotionEvent event) {
121                 if (event.getAction() != MotionEvent.ACTION_DOWN) {
122                         return super.onTouchEvent(event);
123                 } else {
124                         // position
125                         int selX, selY;
126                         selX = (int) (event.getX() / width);
127                         selY = (int) (event.getY() / height);
128                         if (selX <= 0 || selY <= 0 || selX > col || selY > row
129                                         || selX < col && selY == row) {
130                                 return true;
131                         }
132                         int check;
133                         for (check = 0; check < opcol.length; check++) {
134                                 if (selX == opcol[check]) {
135                                         break;
136                                 }
137                         }
138                         if (check == opcol.length) {
139                                 return true;
140                         }
141                         /*
142                          * // add user answer and inverse bit Pair<Integer, Integer> tmpAns
143                          * = new Pair<Integer, Integer>(selY, selX); if
144                          * (!uAnswer.remove(tmpAns)) { uAnswer.add(tmpAns); }
145                          */
146                         if (bits[selY - 1][selX - 1].equals("0")) {
147                                 bits[selY - 1][selX - 1] = "1";
148                         } else {
149                                 bits[selY - 1][selX - 1] = "0";
150                         }
151                         invalidate();
152                 }
153                 return true;
154         }
155
156         public void reset() {
157                 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
158                 bits = new String[code.length()][];
159                 for (int i = 0; i < code.length(); i++) {
160                         bits[i] = res.getStringArray(code.getResourceId(i, -1));
161                 }
162                 invalidate();
163         }
164
165         public int mark() {
166                 for (int i = 0; i < opcol.length - 1; i++) {
167                         for (int j = 0; j < row - 1; j++) {
168                                 if (!bits[j][opcol[i] - 1].equals(cAnswer[i][j])) {
169                                         return 0;
170                                 }
171                         }
172                 }
173                 return 100;
174         }
175 }