OSDN Git Service

お気に入り設定の読み出しと展開・仮実装。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / liveview / dialog / FavoriteSettingSelectionDialog.java
1 package jp.sfjp.gokigen.a01c.liveview.dialog;
2
3 import android.content.Context;
4 import android.graphics.Canvas;
5 import android.graphics.Color;
6 import android.graphics.Paint;
7 import android.graphics.RectF;
8 import android.util.Log;
9
10 import java.util.Locale;
11
12 import jp.sfjp.gokigen.a01c.olycamerawrapper.property.ICameraPropertyLoadSaveOperations;
13
14
15 /**
16  *   お気に入り設定のダイアログ
17  *
18  *
19  */
20 public class FavoriteSettingSelectionDialog implements IDialogDrawer
21 {
22     private final String TAG = toString();
23     private final ICameraPropertyLoadSaveOperations propertyOperation;
24     private final IDialogDismissedNotifier dismissNotifier;
25     private final Context context;
26     private final float WIDE_MARGIN = 10.0f;
27     private final float HEIGHT_MARGIN = 27.0f;
28     private int selectedId = 0;
29     private boolean isSaveOperation = false;  // loadはfalse, saveがtrue
30
31
32
33     /**
34      *   コンストラクタ
35      *
36      */
37     public FavoriteSettingSelectionDialog(Context context, ICameraPropertyLoadSaveOperations operation, IDialogDismissedNotifier dismissNotifier)
38     {
39         this.context = context;
40         this.propertyOperation = operation;
41         this.dismissNotifier = dismissNotifier;
42     }
43
44
45     /**
46      *   画面上にダイアログを表示する
47      *
48      *
49      */
50     @Override
51     public void drawDialog(Canvas canvas)
52     {
53
54         final float STROKE_WIDTH = 2.0f;
55
56         float width = canvas.getWidth();
57         float height = canvas.getHeight();
58
59         float height_unit = (height - 2.0f * (HEIGHT_MARGIN)) / 20.0f;
60         float width_unit = (width - 2.0f * (WIDE_MARGIN)) / 21.0f;
61
62         Paint paint = new Paint();
63         paint.setAntiAlias(true);
64
65         // 背景を消す
66         paint.setColor(Color.BLACK);
67         paint.setStyle(Paint.Style.FILL);
68         canvas.drawRect(WIDE_MARGIN, HEIGHT_MARGIN, (width - WIDE_MARGIN), (height - HEIGHT_MARGIN), paint);
69
70         // 外枠を引く
71         paint.setColor(Color.WHITE);
72         paint.setStyle(Paint.Style.STROKE);
73         paint.setStrokeWidth(STROKE_WIDTH);
74         canvas.drawRect(WIDE_MARGIN, HEIGHT_MARGIN, (width - WIDE_MARGIN), (height - HEIGHT_MARGIN), paint);
75         canvas.drawLine(WIDE_MARGIN, ((height - HEIGHT_MARGIN) - (height_unit * 4.0f)), (width - WIDE_MARGIN),((height - HEIGHT_MARGIN) - (height_unit * 4.0f)), paint);
76         canvas.drawLine((width / 2.0f), ((height - HEIGHT_MARGIN) - (height_unit * 4.0f)), (width / 2.0f), (height - HEIGHT_MARGIN), paint);
77         canvas.drawLine(WIDE_MARGIN, (HEIGHT_MARGIN + (height_unit * 3.0f)), (width - WIDE_MARGIN),  (HEIGHT_MARGIN + (height_unit * 3.0f)), paint);
78
79         Paint.Style style;
80
81         // Load
82         style = (isSaveOperation) ? Paint.Style.STROKE : Paint.Style.FILL;
83         paint.setStyle(style);
84         canvas.drawRect((WIDE_MARGIN + (width_unit * 1.0f)), HEIGHT_MARGIN + 4.0f * height_unit, (WIDE_MARGIN + width_unit * (1.0f + 9.0f)), HEIGHT_MARGIN + 8.0f * height_unit, paint);
85
86         // Save
87         style = (!isSaveOperation) ? Paint.Style.STROKE : Paint.Style.FILL;
88         paint.setStyle(style);
89         canvas.drawRect((WIDE_MARGIN + (width_unit * 11.0f)), HEIGHT_MARGIN + 4.0f * height_unit, (WIDE_MARGIN + width_unit * (11.0f + 9.0f)), HEIGHT_MARGIN + 8.0f * height_unit, paint);
90
91         // ボタン0
92         style = (selectedId != 0) ? Paint.Style.STROKE : Paint.Style.FILL;
93         paint.setStyle(style);
94         canvas.drawRect((WIDE_MARGIN + (width_unit * 1.0f)), HEIGHT_MARGIN + 9.0f * height_unit, (WIDE_MARGIN + width_unit * (1.0f + 3.0f)), HEIGHT_MARGIN + 15.0f * height_unit, paint);
95
96         // ボタン1
97         style = (selectedId != 1) ? Paint.Style.STROKE : Paint.Style.FILL;
98         paint.setStyle(style);
99         canvas.drawRect((WIDE_MARGIN + (width_unit * 5.0f)), HEIGHT_MARGIN + 9.0f * height_unit, (WIDE_MARGIN + width_unit * (5.0f + 3.0f)), HEIGHT_MARGIN + 15.0f * height_unit, paint);
100
101         // ボタン2
102         style = (selectedId != 2) ? Paint.Style.STROKE : Paint.Style.FILL;
103         paint.setStyle(style);
104         canvas.drawRect((WIDE_MARGIN + (width_unit * 9.0f)), HEIGHT_MARGIN + 9.0f * height_unit, (WIDE_MARGIN + width_unit * (9.0f + 3.0f)), HEIGHT_MARGIN + 15.0f * height_unit, paint);
105
106         // ボタン3
107         style = (selectedId != 3) ? Paint.Style.STROKE : Paint.Style.FILL;
108         paint.setStyle(style);
109         canvas.drawRect((WIDE_MARGIN + (width_unit * 13.0f)), HEIGHT_MARGIN + 9.0f * height_unit, (WIDE_MARGIN + width_unit * (13.0f + 3.0f)), HEIGHT_MARGIN + 15.0f * height_unit, paint);
110
111         // ボタン4
112         style = (selectedId != 4) ? Paint.Style.STROKE : Paint.Style.FILL;
113         paint.setStyle(style);
114         canvas.drawRect((WIDE_MARGIN + (width_unit * 17.0f)), HEIGHT_MARGIN + 9.0f * height_unit, (WIDE_MARGIN + width_unit * (17.0f + 3.0f)), HEIGHT_MARGIN + 15.0f * height_unit, paint);
115
116     }
117
118
119     /**
120      *   画面でボタンが押された(押された位置 0.0f ~ 1.0f)
121      *
122      * @param posX  X座標 (左~右)
123      * @param posY  Y座標(上~下)
124      *
125      * @return  true : ボタンなどがあるエリアだった / false : 何もしなかった
126      */
127     @Override
128     public boolean touchedPosition(float posX, float posY)
129     {
130         Log.v(TAG, " FavoriteSettingSelectionDialog::touchedPosition()  [" + posX + "," + posY + "]");
131
132         //  押された場所をチェックする
133         if (posY > (16.0f / 20.0f))
134         {
135             // 画面下部のOK or Cancelが押された
136             dismiss(posX);
137             return (true);
138         }
139         if ((posY >= (4.0f / 20.0f))&&(posY <= (8.0f / 20.0f)))
140         {
141             // Load/Saveボタンの領域が押された
142             isSaveOperation = (posX >= (10.0f / 21.0f));
143             return (true);
144         }
145         if ((posY >= (9.0f / 20.0f))&&(posY <= (15.0f / 20.0f)))
146         {
147             // IDボタンの領域が押された
148             selectedIdArea(posX);
149             return (true);
150         }
151         return (false);
152     }
153
154     /**
155      *  選択したIDボタン...
156      *
157      */
158     private void selectedIdArea(float posX)
159     {
160         if (posX <= (4.0f / 21.0f))
161         {
162             selectedId = 0;
163         }
164         else if (posX <= ((4.0f * 2.0f) / 21.0f))
165         {
166             selectedId = 1;
167         }
168         else if (posX <= ((4.0f * 3.0f) / 21.0f))
169         {
170             selectedId = 2;
171         }
172         else if (posX <= ((4.0f * 4.0f) / 21.0f))
173         {
174             selectedId = 3;
175         }
176         else //if (posX <= ((4.0f * 5.0f) / 21.0f))
177         {
178             selectedId = 4;
179         }
180     }
181
182
183     /**
184      *   プロパティの保存処理をする
185      *
186      * @param id  選択した番号
187      */
188     private void saveProperties(int id)
189     {
190         try
191         {
192             propertyOperation.saveProperties(String.format(Locale.ENGLISH, "%03d", id), "a01c:" + id);
193         }
194         catch (Exception e)
195         {
196             e.printStackTrace();
197         }
198     }
199
200     /**
201      *   プロパティのロード処理をする
202      *
203      * @param id  選択した番号
204      */
205     private void loadProperties(int id)
206     {
207         try
208         {
209             propertyOperation.loadProperties(String.format(Locale.ENGLISH, "%03d", id), "a01c:" + id);
210         }
211         catch (Exception e)
212         {
213             e.printStackTrace();
214         }
215     }
216
217     /**
218      *   ダイアログを閉じる
219      *
220      * @param posX : ボタンが押された位置
221      */
222     private void dismiss(float posX)
223     {
224         boolean isExecute = (posX > 0.5f);
225         if (isExecute)
226         {
227             Log.v(TAG, "  EXECUTE OPERATION  : " + selectedId + " " + isSaveOperation);
228 /**/
229             // コマンドを実行する
230             if (isSaveOperation)
231             {
232                 // プロパティの保存
233                 saveProperties(selectedId + 1);
234             }
235             else
236             {
237                 // プロパティの読み出し
238                 loadProperties(selectedId + 1);
239             }
240 /**/
241         }
242         if (dismissNotifier != null)
243         {
244             dismissNotifier.dialogDismissed(isExecute);
245         }
246     }
247 }