1 package jp.sourceforge.gokigen.memoma.operations;
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.content.Context;
6 import android.content.DialogInterface;
7 import android.content.SharedPreferences;
8 import android.graphics.Color;
9 import android.graphics.Paint;
10 import android.graphics.PorterDuff.Mode;
11 import android.graphics.drawable.BitmapDrawable;
12 import android.graphics.drawable.GradientDrawable;
13 import android.preference.PreferenceManager;
14 import android.util.Log;
15 import android.util.TypedValue;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.widget.CheckBox;
19 import android.widget.EditText;
20 import android.widget.ImageButton;
21 import android.widget.SeekBar;
22 import android.widget.TextView;
23 import android.widget.CompoundButton;
25 import jp.sourceforge.gokigen.memoma.Main;
26 import jp.sourceforge.gokigen.memoma.R;
27 import jp.sourceforge.gokigen.memoma.drawers.MeMoMaCanvasDrawer;
28 import jp.sourceforge.gokigen.memoma.holders.MeMoMaObjectHolder;
31 * オブジェクトのデータを入力するダイアログを表示する
36 public class ObjectDataInputDialog implements SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener, ImageButton.OnClickListener
38 private final float FONTSIZE_SMALL = 4.0f;
39 private final float FONTSIZE_MIDDLE = 6.0f;
40 private final float FONTSIZE_LARGE = 10.0f;
42 private Context context = null;
43 private IResultReceiver resultReceiver = null;
44 private MeMoMaObjectHolder objectHolder = null;
45 private Integer key = 0;
47 private View dialogLayout = null;
48 private TextView colorBorderAreaView = null;
49 private SeekBar borderColorView = null;
50 private CheckBox fillObjectView = null;
51 private GradientDrawable backgroundShape = null;
52 private int backgroundColor = MeMoMaCanvasDrawer.BACKGROUND_COLOR_DEFAULT;
53 private int currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_RECTANGLE;
54 private float textFontSize = 6.0f;
56 public ObjectDataInputDialog(Context arg, MeMoMaObjectHolder holder)
59 objectHolder = holder;
62 public void setResultReceiver(IResultReceiver receiver)
64 resultReceiver = receiver;
71 public Dialog getDialog()
73 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
74 final View layout = inflater.inflate(R.layout.objectinput, null);
75 dialogLayout = layout;
77 AlertDialog.Builder builder = new AlertDialog.Builder(context);
79 // ダイアログで表示するデータを設定する場所
84 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
85 String colorString = preferences.getString("backgroundColor", "0xff004000");
86 backgroundColor = Color.parseColor(colorString);
90 backgroundColor = MeMoMaCanvasDrawer.BACKGROUND_COLOR_DEFAULT;
93 final TextView colorLabel = (TextView) layout.findViewById(R.id.setBorderColorLabel);
94 backgroundShape = (GradientDrawable)colorLabel.getBackground();
97 final EditText label = (EditText) layout.findViewById(R.id.labelInputArea);
98 // label.setTextColor(Color.LTGRAY);
100 final EditText detail = (EditText) layout.findViewById(R.id.descriptionInputArea);
101 //detail.setTextColor(Color.LTGRAY);
103 borderColorView = (SeekBar) layout.findViewById(R.id.borderColorSelectionBar);
104 borderColorView.setOnSeekBarChangeListener(this);
106 final CheckBox userCheckbox = (CheckBox) layout.findViewById(R.id.checkUserCheckbox);
107 final CheckBox boldText = (CheckBox) layout.findViewById(R.id.checkBoldText);
108 fillObjectView = (CheckBox) layout.findViewById(R.id.checkFillObject);
109 fillObjectView.setOnCheckedChangeListener(this);
111 colorBorderAreaView = (TextView) layout.findViewById(R.id.borderColorArea);
112 colorBorderAreaView.setOnClickListener(this);
114 final ImageButton rect = (ImageButton) layout.findViewById(R.id.btnObjectRectangle);
115 rect.setOnClickListener(this);
116 final ImageButton roundRect = (ImageButton) layout.findViewById(R.id.btnObjectRoundRect);
117 roundRect.setOnClickListener(this);
118 final ImageButton oval = (ImageButton) layout.findViewById(R.id.btnObjectOval);
119 oval.setOnClickListener(this);
120 final ImageButton diamond = (ImageButton) layout.findViewById(R.id.btnObjectDiamond);
121 diamond.setOnClickListener(this);
122 final ImageButton hexagonal = (ImageButton) layout.findViewById(R.id.btnObjectHexagonal);
123 hexagonal.setOnClickListener(this);
124 final ImageButton parallelogram = (ImageButton) layout.findViewById(R.id.btnObjectParallelogram);
125 parallelogram.setOnClickListener(this);
126 final ImageButton keyboard = (ImageButton) layout.findViewById(R.id.btnObjectKeyboard);
127 keyboard.setOnClickListener(this);
128 final ImageButton paper = (ImageButton) layout.findViewById(R.id.btnObjectPaper);
129 paper.setOnClickListener(this);
130 final ImageButton drum = (ImageButton) layout.findViewById(R.id.btnObjectDrum);
131 drum.setOnClickListener(this);
132 final ImageButton circle = (ImageButton) layout.findViewById(R.id.btnObjectCircle);
133 circle.setOnClickListener(this);
134 final ImageButton noregion = (ImageButton) layout.findViewById(R.id.btnObjectNoRegion);
135 noregion.setOnClickListener(this);
137 final ImageButton loopStart = (ImageButton) layout.findViewById(R.id.btnObjectLoopStart);
138 loopStart.setOnClickListener(this);
139 final ImageButton loopEnd = (ImageButton) layout.findViewById(R.id.btnObjectLoopEnd);
140 loopEnd.setOnClickListener(this);
141 final ImageButton leftArrow = (ImageButton) layout.findViewById(R.id.btnObjectLeftArrow);
142 leftArrow.setOnClickListener(this);
143 final ImageButton downArrow = (ImageButton) layout.findViewById(R.id.btnObjectDownArrow);
144 downArrow.setOnClickListener(this);
145 final ImageButton upArrow = (ImageButton) layout.findViewById(R.id.btnObjectUpArrow);
146 upArrow.setOnClickListener(this);
147 final ImageButton rightArrow = (ImageButton) layout.findViewById(R.id.btnObjectRightArrow);
148 rightArrow.setOnClickListener(this);
150 // 背景の色を調整(塗りつぶしの時はオブジェクトの色とする。)
151 int color = convertColor(borderColorView.getProgress());
152 colorBorderAreaView.setBackgroundColor((fillObjectView.isChecked() == true) ? color : backgroundColor);
153 //backgroundShape.setStroke(2, color);
155 if (fillObjectView.isChecked() == true)
158 color = (color ^ 0x00ffffff);
160 colorBorderAreaView.setTextColor(color);
161 colorBorderAreaView.setText(context.getString(R.string.labelTextColorSample));
163 builder.setView(layout);
164 builder.setCancelable(false);
165 builder.setPositiveButton(context.getString(R.string.confirmYes), new DialogInterface.OnClickListener()
167 public void onClick(DialogInterface dialog, int id)
170 boolean isUserCheck = false;
171 if (userCheckbox != null)
173 isUserCheck = userCheckbox.isChecked();
175 setObjectData(label.getText().toString(), detail.getText().toString(), borderColorView.getProgress(), boldText.isChecked(), fillObjectView.isChecked(), isUserCheck, currentObjectDrawStyle);
176 if (resultReceiver != null)
178 resultReceiver.finishObjectInput();
191 builder.setNegativeButton(context.getString(R.string.confirmNo), new DialogInterface.OnClickListener()
193 public void onClick(DialogInterface dialog, int id)
196 if (resultReceiver != null)
198 resultReceiver.cancelObjectInput();
211 return (builder.create());
215 * オブジェクト入力用ダイアログの表示を準備する
218 public void prepareObjectInputDialog(Dialog dialog, Integer objectKey)
220 MeMoMaObjectHolder.PositionObject position = objectHolder.getPosition(objectKey);
222 if (position != null)
225 final SeekBar borderColorProgess = (SeekBar) dialog.findViewById(R.id.borderColorSelectionBar);
226 borderColorProgess.setProgress(convertProgress(position.objectColor));
228 final CheckBox boldTextCheck = (CheckBox) dialog.findViewById(R.id.checkBoldText);
229 boldTextCheck.setChecked(position.strokeWidth == MeMoMaObjectHolder.STOROKE_BOLD_WIDTH);
231 final CheckBox fillObjectCheck = (CheckBox) dialog.findViewById(R.id.checkFillObject);
232 fillObjectCheck.setChecked(Paint.Style.valueOf(position.paintStyle) != Paint.Style.STROKE);
235 textFontSize = position.fontSize / 2.0f;
238 final EditText targetLabel = (EditText) dialog.findViewById(R.id.labelInputArea);
239 targetLabel.setText(position.label);
241 final EditText targetDetail = (EditText) dialog.findViewById(R.id.descriptionInputArea);
242 targetDetail.setText(position.detail);
244 // 設定に記録されているデータを画面に反映させる
245 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
246 String userCheckboxTitle = preferences.getString("userCheckboxString", "");
249 currentObjectDrawStyle = position.drawStyle;
250 updateObjectDrawStyleImageButton(currentObjectDrawStyle);
255 String colorString = preferences.getString("backgroundColor", "0xff004000");
256 backgroundColor = Color.parseColor(colorString);
260 backgroundColor = MeMoMaCanvasDrawer.BACKGROUND_COLOR_DEFAULT;
262 setTextColorSample(borderColorProgess.getProgress(), textFontSize, fillObjectView.isChecked());
264 final CheckBox userCheckbox = (CheckBox) dialog.findViewById(R.id.checkUserCheckbox);
265 userCheckbox.setEnabled(true);
266 userCheckbox.setText(userCheckboxTitle);
267 userCheckbox.setChecked(position.userChecked);
275 public void setObjectData(String label, String detail, int progress, boolean boldText, boolean fillObject, boolean userCheck, int drawStyle)
277 MeMoMaObjectHolder.PositionObject positionObject = objectHolder.getPosition(key);
278 if (positionObject != null)
280 positionObject.label = label;
281 positionObject.detail = detail;
282 int color = convertColor(progress);
283 positionObject.objectColor = color;
284 color = (color ^ 0x00ffffff);
285 positionObject.fontSize = textFontSize * 2.0f;
286 positionObject.labelColor = color;
287 positionObject.strokeWidth = (boldText == true) ? MeMoMaObjectHolder.STOROKE_BOLD_WIDTH : MeMoMaObjectHolder.STOROKE_NORMAL_WIDTH;
288 positionObject.paintStyle = ((fillObject == true) ? Paint.Style.FILL : Paint.Style.STROKE).toString();
289 positionObject.userChecked = userCheck;
291 if (positionObject.drawStyle != drawStyle)
293 if ((drawStyle == MeMoMaObjectHolder.DRAWSTYLE_CIRCLE)||
294 (drawStyle == MeMoMaObjectHolder. DRAWSTYLE_LEFT_ARROW)||
295 (drawStyle == MeMoMaObjectHolder. DRAWSTYLE_DOWN_ARROW)||
296 (drawStyle == MeMoMaObjectHolder. DRAWSTYLE_UP_ARROW)||
297 (drawStyle == MeMoMaObjectHolder. DRAWSTYLE_RIGHT_ARROW))
299 // (長方形の形状から)正方形の形状にする場合...
300 setRectToSquare(positionObject);
302 else if ((positionObject.drawStyle == MeMoMaObjectHolder.DRAWSTYLE_CIRCLE)||
303 (positionObject.drawStyle == MeMoMaObjectHolder. DRAWSTYLE_LEFT_ARROW)||
304 (positionObject.drawStyle == MeMoMaObjectHolder. DRAWSTYLE_DOWN_ARROW)||
305 (positionObject.drawStyle == MeMoMaObjectHolder. DRAWSTYLE_UP_ARROW)||
306 (positionObject.drawStyle == MeMoMaObjectHolder. DRAWSTYLE_RIGHT_ARROW))
308 // 正方形の形状から、長方形の形状にする場合...
309 setRectFromSquare(positionObject);
311 positionObject.drawStyle = drawStyle;
314 positionObject = null;
318 * オブジェクトの領域を長方形から正方形にする
320 private void setRectToSquare(MeMoMaObjectHolder.PositionObject positionObject)
322 float bandWidth = ((positionObject.rect.right - positionObject.rect.left)) / 2.0f;
323 float center = positionObject.rect.centerY();
325 positionObject.rect.top = center - bandWidth;
326 positionObject.rect.bottom = center + bandWidth;
330 * オブジェクトの領域を正方形から長方形にする
332 private void setRectFromSquare(MeMoMaObjectHolder.PositionObject positionObject)
334 float bandWidth = ((positionObject.rect.right - positionObject.rect.left) / 16.0f * 9.0f) / 2.0f;
335 float center = positionObject.rect.centerY();
337 positionObject.rect.top = center - bandWidth;
338 positionObject.rect.bottom = center + bandWidth;
341 private void setButtonBorder(int id, boolean isHighlight)
345 ImageButton button = (ImageButton) dialogLayout.findViewById(id);
346 //GradientDrawable btnBackgroundShape = (GradientDrawable)button.getBackground();
347 BitmapDrawable btnBackgroundShape = (BitmapDrawable)button.getBackground();
348 if (isHighlight == true)
350 // btnBackgroundShape.setColorFilter(Color.rgb(51, 181, 229), Mode.LIGHTEN);
351 btnBackgroundShape.setColorFilter(Color.BLUE, Mode.LIGHTEN);
355 btnBackgroundShape.setColorFilter(Color.BLACK, Mode.LIGHTEN);
361 Log.v(Main.APP_IDENTIFIER, "setButtonBorder(): " + ex.toString());
371 private void updateObjectDrawStyleImageButton(int drawStyle)
373 setButtonBorder(R.id.btnObjectRectangle, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_RECTANGLE));
374 setButtonBorder(R.id.btnObjectRoundRect, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_ROUNDRECT));
375 setButtonBorder(R.id.btnObjectOval, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_OVAL));
376 setButtonBorder(R.id.btnObjectDiamond, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DIAMOND));
377 setButtonBorder(R.id.btnObjectHexagonal, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_HEXAGONAL));
378 setButtonBorder(R.id.btnObjectParallelogram, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_PARALLELOGRAM));
379 setButtonBorder(R.id.btnObjectKeyboard, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_KEYBOARD));
380 setButtonBorder(R.id.btnObjectPaper, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_PAPER));
381 setButtonBorder(R.id.btnObjectDrum, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DRUM));
382 setButtonBorder(R.id.btnObjectCircle, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_CIRCLE));
383 setButtonBorder(R.id.btnObjectNoRegion, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_NO_REGION));
384 setButtonBorder(R.id.btnObjectLoopStart, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LOOP_START));
385 setButtonBorder(R.id.btnObjectLoopEnd, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LOOP_END));
386 setButtonBorder(R.id.btnObjectLeftArrow, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LEFT_ARROW));
387 setButtonBorder(R.id.btnObjectDownArrow, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DOWN_ARROW));
388 setButtonBorder(R.id.btnObjectUpArrow, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_UP_ARROW));
389 setButtonBorder(R.id.btnObjectRightArrow, (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_RIGHT_ARROW));
396 public void onClick(View v)
399 if (id == R.id.borderColorArea)
402 if (textFontSize == FONTSIZE_MIDDLE)
404 textFontSize = FONTSIZE_LARGE;
406 else if (textFontSize == FONTSIZE_LARGE)
408 textFontSize = FONTSIZE_SMALL;
410 else // if (textFontSize == FONTSIZE_SMALL)
412 textFontSize = FONTSIZE_MIDDLE;
414 setTextColorSample(borderColorView.getProgress(), textFontSize, fillObjectView.isChecked());
420 case R.id.btnObjectRoundRect:
421 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_ROUNDRECT;
423 case R.id.btnObjectOval:
424 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_OVAL;
426 case R.id.btnObjectDiamond:
427 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_DIAMOND;
429 case R.id.btnObjectHexagonal:
430 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_HEXAGONAL;
432 case R.id.btnObjectParallelogram:
433 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_PARALLELOGRAM;
435 case R.id.btnObjectKeyboard:
436 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_KEYBOARD;
438 case R.id.btnObjectPaper:
439 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_PAPER;
441 case R.id.btnObjectDrum:
442 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_DRUM;
444 case R.id.btnObjectNoRegion:
445 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_NO_REGION;
447 case R.id.btnObjectCircle:
448 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_CIRCLE;
450 case R.id.btnObjectLoopStart:
451 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_LOOP_START;
453 case R.id.btnObjectLoopEnd:
454 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_LOOP_END;
456 case R.id.btnObjectLeftArrow:
457 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_LEFT_ARROW;
459 case R.id.btnObjectDownArrow:
460 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_DOWN_ARROW;
462 case R.id.btnObjectUpArrow:
463 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_UP_ARROW;
465 case R.id.btnObjectRightArrow:
466 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_RIGHT_ARROW;
468 case R.id.btnObjectRectangle:
470 currentObjectDrawStyle = MeMoMaObjectHolder.DRAWSTYLE_RECTANGLE;
473 updateObjectDrawStyleImageButton(currentObjectDrawStyle);
477 * 0x00~0x3fの値(R, G, B, それぞれ2ビット)で色を変える
482 private int convertColor(int value)
485 int r = ((value >> 4 ) & 0x03) * 85;
486 int g = ((value >> 2) & 0x03) * 85;
487 int b = (value & 0x03) * 85;
488 color = Color.rgb(r, g, b);
489 //Log.v(Main.APP_IDENTIFIER, "convertColor() R : " + r + ", G : " + g + ", B : " + b + "color : " + color);
499 private int convertProgress(int color)
501 int r = Color.red(color) / 85;
502 int g = Color.green(color) / 85;
503 int b = Color.blue(color) / 85;
505 return ((r << 4) + (g << 2) + b);
515 private void setTextColorSample(int progress, float fontSize, boolean value)
517 if (colorBorderAreaView != null)
519 int color = convertColor(progress);
520 int backColor = (value == true) ? color : backgroundColor;
521 colorBorderAreaView.setBackgroundColor(backColor);
523 backgroundShape.setStroke(2, color);
528 color = (color ^ 0x00ffffff);
530 colorBorderAreaView.setTextSize(TypedValue.COMPLEX_UNIT_PT, fontSize);
531 colorBorderAreaView.setTextColor(color);
532 colorBorderAreaView.setText(context.getString(R.string.labelTextColorSample));
537 * チェックボックスの値が変更された時の処理
540 public void onCheckedChanged(CompoundButton view, boolean value)
542 int id = view.getId();
543 if ((id == R.id.checkFillObject)&&(borderColorView != null))
545 setTextColorSample(borderColorView.getProgress(), textFontSize, value);
549 * プログレスバーで値を変更された時の処理
552 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
554 int id = seekBar.getId();
555 //Log.v(Main.APP_IDENTIFIER, "onProgressChanged() : " + progress + " id : " + id);
557 if ((id == R.id.borderColorSelectionBar)&&(fillObjectView != null))
559 setTextColorSample(progress, textFontSize, fillObjectView.isChecked());
563 public void onStartTrackingTouch(SeekBar seekBar)
568 public void onStopTrackingTouch(SeekBar seekBar)
573 public interface IResultReceiver
575 public abstract void finishObjectInput();
576 public abstract void cancelObjectInput();