OSDN Git Service

オブジェクトの削除についても記録に残すようにした。
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / holders / MeMoMaObjectHolder.java
1 package jp.sourceforge.gokigen.memoma.holders;
2
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5 import android.content.Context;
6 import android.graphics.Color;
7 import android.graphics.Paint;
8 import android.graphics.RectF;
9 import android.util.Log;
10 import android.widget.Toast;
11
12 import jp.sourceforge.gokigen.memoma.Main;
13 import jp.sourceforge.gokigen.memoma.R;
14
15 /**
16  *   表示オブジェクトの情報を保持するクラス
17  * 
18  * @author MRSa
19  *
20  */
21 public class MeMoMaObjectHolder
22 {
23         public static final int ID_NOTSPECIFY = -1;
24         
25     public static final int DRAWSTYLE_RECTANGLE = 0;
26     public static final int DRAWSTYLE_ROUNDRECT = 1;
27     public static final int DRAWSTYLE_OVAL = 2;
28     public static final int DRAWSTYLE_DIAMOND = 3;
29     public static final int DRAWSTYLE_HEXAGONAL = 4;
30     public static final int DRAWSTYLE_PARALLELOGRAM = 5;
31     public static final int DRAWSTYLE_KEYBOARD = 6;
32     public static final int DRAWSTYLE_PAPER = 7;
33     public static final int DRAWSTYLE_DRUM = 8;
34     public static final int DRAWSTYLE_CIRCLE = 9;
35     public static final int DRAWSTYLE_NO_REGION = 10;
36     
37     public static final int DRAWSTYLE_LOOP_START = 11;
38     public static final int DRAWSTYLE_LOOP_END = 12;
39     public static final int DRAWSTYLE_LEFT_ARROW = 13;
40     public static final int DRAWSTYLE_DOWN_ARROW = 14;
41     public static final int DRAWSTYLE_UP_ARROW = 15;
42     public static final int DRAWSTYLE_RIGHT_ARROW = 16;
43
44     public static final float ROUNDRECT_CORNER_RX = 8;
45     public static final float ROUNDRECT_CORNER_RY = 8;
46     
47     public static final float STOROKE_BOLD_WIDTH = 3.5f;
48     public static final float STOROKE_NORMAL_WIDTH = 0.0f;
49     
50     public static final float DUPLICATEPOSITION_MARGIN = 15.0f;
51
52     public static final float OBJECTSIZE_DEFAULT_X = 144.0f;
53         public static final float OBJECTSIZE_DEFAULT_Y = (OBJECTSIZE_DEFAULT_X / 16.0f * 9.0f);
54
55         public static final float OBJECTSIZE_MINIMUM_X = 48.0f;
56         public static final float OBJECTSIZE_MINIMUM_Y = (OBJECTSIZE_MINIMUM_X / 16.0f * 9.0f);
57         
58         public static final float OBJECTSIZE_MAXIMUM_X = 14400.0f;
59         public static final float OBJECTSIZE_MAXIMUM_Y =  (OBJECTSIZE_MAXIMUM_X / 16.0f * 9.0f);
60
61         public static final float OBJECTSIZE_STEP_X = OBJECTSIZE_MINIMUM_X * 1.0f;
62         public static final float OBJECTSIZE_STEP_Y = OBJECTSIZE_MINIMUM_Y * 1.0f;
63         
64         public static final float FONTSIZE_DEFAULT = 12.0f;
65         
66     private MeMoMaConnectLineHolder connectLineHolder = null;
67     
68         private Hashtable<Integer, PositionObject> objectPoints = null;
69         private Integer serialNumber = 1;
70         private String  dataTitle = "";
71         private String  background = "";
72         private Context parent;
73         private final IOperationHistoryHolder historyHolder;
74
75     public MeMoMaObjectHolder(Context context, MeMoMaConnectLineHolder lineHolder, IOperationHistoryHolder historyHolder)
76     {
77                   objectPoints = new Hashtable<>();
78                   connectLineHolder = lineHolder;
79                   parent = context;
80                   this.historyHolder = historyHolder;
81     }
82
83     /**
84      *    データの有無を見る (true の場合、データはない。)
85      * 
86      *
87      */
88     public boolean isEmpty()
89     {
90                 return (((connectLineHolder == null)||(objectPoints == null))||(objectPoints.isEmpty()));
91     }
92     
93     public MeMoMaConnectLineHolder getConnectLineHolder()
94     {
95         return (connectLineHolder);
96     }
97     
98     public void setDataTitle(String title)
99     {
100         dataTitle = title;
101     }
102     
103     public String getDataTitle()
104     {
105         return (dataTitle);
106     }
107
108     public void setBackground(String data)
109     {
110         background = data;
111     }
112     
113     public String getBackground()
114     {
115         return (background);
116     }
117
118     public int getCount()
119     {
120         return (objectPoints.size());
121     }
122
123     public Enumeration<Integer> getObjectKeys()
124     {
125         return (objectPoints.keys());
126     }
127
128     public PositionObject getPosition(Integer key)
129     {
130         return  (objectPoints.get(key));
131     }
132
133     public boolean removePosition(Integer key)
134     {
135         PositionObject removeTarget = objectPoints.remove(key);
136         if (removeTarget != null)
137         {
138             historyHolder.addHistory(key, IOperationHistoryHolder.ChangeKind.DELETE_OBJECT, removeTarget);
139         }
140         Log.v(Main.APP_IDENTIFIER, "REMOVE : " + key);
141         return (true);
142     }
143     
144     public void removeAllPositions()
145     {
146         objectPoints.clear();
147         serialNumber = 1;
148     }
149
150     public void setSerialNumber(int id)
151     {
152         serialNumber = (id == ID_NOTSPECIFY) ? ++serialNumber : id;
153     }
154     
155     public int getSerialNumber()
156     {
157         return (serialNumber);
158     }
159
160     public void dumpPositionObject(PositionObject position)
161     {
162         if (position == null)
163         {
164                 return;
165         }
166         RectF posRect = position.getRect();
167         Log.v(Main.APP_IDENTIFIER, "[" + posRect.left + "," + posRect.top + "][" + posRect.right + "," + posRect.bottom + "] " + "label : " + position.getLabel() + " detail : " + position.getDetail());
168     }
169     
170     
171     /**
172      *   オブジェクトを複製する。
173      * 
174      *
175      *
176      */
177     public PositionObject duplicatePosition(int key)
178     {
179         PositionObject orgPosition = objectPoints.get(key);
180         if (orgPosition == null)
181         {
182                 // 元のオブジェクトが見つからなかったので、何もせずに戻る
183                 return (null);
184         }
185         RectF orgRect = orgPosition.getRect();
186         PositionObject position = new PositionObject(serialNumber,
187                 new RectF(orgRect.left + DUPLICATEPOSITION_MARGIN, orgRect.top + DUPLICATEPOSITION_MARGIN, orgRect.right + DUPLICATEPOSITION_MARGIN, orgRect.bottom + DUPLICATEPOSITION_MARGIN),
188                 orgPosition.getDrawStyle(),
189                 orgPosition.getIcon(),
190                 orgPosition.getLabel(),
191                 orgPosition.getDetail(),
192                 orgPosition.getUserChecked(),
193                 orgPosition.getLabelColor(),
194                 orgPosition.getObjectColor(),
195                 orgPosition.getPaintStyle(),
196                 orgPosition.getstrokeWidth(),
197                 orgPosition.getFontSize(),
198                 historyHolder);
199                 objectPoints.put(serialNumber, position);
200                 serialNumber++;
201                 return (position);
202     }
203
204     public PositionObject createPosition(int id)
205     {
206         PositionObject position = new PositionObject(id,
207                 new RectF(0, 0, OBJECTSIZE_DEFAULT_X, OBJECTSIZE_DEFAULT_Y),
208                 MeMoMaObjectHolder.DRAWSTYLE_RECTANGLE,
209                 0,
210                 "",
211                 "",
212                 false,
213                 Color.WHITE,
214                 Color.WHITE,
215                 Paint.Style.STROKE.toString(),
216                 STOROKE_NORMAL_WIDTH,
217                 FONTSIZE_DEFAULT,
218                 historyHolder);
219                 objectPoints.put(id, position);
220                 return (position);      
221     }
222     
223     public PositionObject createPosition(float x, float y, int drawStyle)
224     {
225         PositionObject position = createPosition(serialNumber);
226         RectF posRect = position.getRect();
227         position.setRectLeft(posRect.left + x);
228         position.setRectRight(posRect.right + x);
229         position.setRectTop(posRect.top + y);
230         position.setRectBottom(posRect.bottom + y);
231         position.setDrawStyle(drawStyle);
232                 serialNumber++;
233                 return (position);
234     }
235
236     /**
237      *   オブジェクトのサイズを拡大する
238      *
239      */
240     public void expandObjectSize(Integer key)
241     {
242         PositionObject position = objectPoints.get(key);
243         if (position == null)
244         {
245                 // 元のオブジェクトが見つからなかったので、何もせずに戻る
246                 return;
247         }
248         RectF posRect = position.getRect();
249         float width = posRect.right - posRect.left;
250         float height = posRect.bottom - posRect.top;
251         if (((width + (OBJECTSIZE_STEP_X * 2.0f)) > OBJECTSIZE_MAXIMUM_X)||((height + (OBJECTSIZE_STEP_Y * 2.0f)) > OBJECTSIZE_MAXIMUM_Y))
252         {
253             // 拡大リミットだった。。。拡大しない
254                 String outputMessage = parent.getString(R.string.object_bigger_limit) + " ";
255             Toast.makeText(parent, outputMessage, Toast.LENGTH_SHORT).show();
256             return;
257         }
258         position.setRectLeft(posRect.left - OBJECTSIZE_STEP_X);
259         position.setRectRight(posRect.right + OBJECTSIZE_STEP_X);
260         position.setRectTop(posRect.top - OBJECTSIZE_STEP_Y);
261         position.setRectBottom(posRect.bottom + OBJECTSIZE_STEP_Y);
262     }
263
264     /**
265      *   オブジェクトのサイズを縮小する
266      * 
267      *
268      */
269     public void shrinkObjectSize(Integer key)
270     {
271         PositionObject position = objectPoints.get(key);
272         if (position == null)
273         {
274                 // 元のオブジェクトが見つからなかったので、何もせずに戻る
275                 return;
276         }
277         RectF posRect = position.getRect();
278         float width = posRect.right - posRect.left;
279         float height = posRect.bottom - posRect.top;
280         if (((width - (OBJECTSIZE_STEP_X * 2.0f)) < OBJECTSIZE_MINIMUM_X)||((height - (OBJECTSIZE_STEP_Y * 2.0f)) < OBJECTSIZE_MINIMUM_Y))
281         {
282             // 縮小リミットだった。。。縮小しない
283                 String outputMessage = parent.getString(R.string.object_small_limit) + " ";
284             Toast.makeText(parent, outputMessage, Toast.LENGTH_SHORT).show();
285             return;
286         }
287         position.setRectLeft(posRect.left + OBJECTSIZE_STEP_X);
288         position.setRectRight(posRect.right - OBJECTSIZE_STEP_X);
289         position.setRectTop(posRect.top + OBJECTSIZE_STEP_Y);
290         position.setRectBottom(posRect.bottom - OBJECTSIZE_STEP_Y);
291     }
292
293     public MeMoMaConnectLineHolder getLineHolder()
294         {
295                 return (connectLineHolder);
296         }
297         
298         static public int getObjectDrawStyleIcon(int drawStyle)
299         {
300                 int icon = 0;
301         if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_RECTANGLE)
302         {
303                 icon  = R.drawable.btn_rectangle;
304         }
305         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_ROUNDRECT)
306         {
307                 icon = R.drawable.btn_roundrect;
308         }
309         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_OVAL)
310         {
311                 icon = R.drawable.btn_oval;
312         }
313         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DIAMOND)
314         {
315                 icon = R.drawable.btn_diamond;
316         }
317         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_HEXAGONAL)
318         {
319                 icon = R.drawable.btn_hexagonal;
320         }
321         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_PARALLELOGRAM)
322         {
323                 icon = R.drawable.btn_parallelogram;
324         }
325         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_KEYBOARD)
326         {
327                 icon = R.drawable.btn_keyboard;
328         }
329         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_PAPER)
330         {
331                 icon = R.drawable.btn_paper;
332         }
333         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DRUM)
334         {
335                 icon = R.drawable.btn_drum;
336         }
337         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_CIRCLE)
338         {
339                 icon = R.drawable.btn_circle;
340         }
341         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_NO_REGION)
342         {
343                 icon = R.drawable.btn_noregion;
344         }
345         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LOOP_START)
346         {
347                 icon = R.drawable.btn_trapezoidy_up;
348         }
349         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LOOP_END)
350         {
351                 icon = R.drawable.btn_trapezoidy_down;
352         }
353         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_LEFT_ARROW)
354         {
355                 icon = R.drawable.btn_arrow_left;
356         }
357         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_DOWN_ARROW)
358         {
359                 icon = R.drawable.btn_arrow_down;
360         }
361         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_UP_ARROW)
362         {
363                 icon = R.drawable.btn_arrow_up;
364         }
365         else if (drawStyle == MeMoMaObjectHolder.DRAWSTYLE_RIGHT_ARROW)
366         {
367                 icon = R.drawable.btn_arrow_right;
368         }
369         return (icon);
370         }
371 }