App

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

BrushToolSettingsFragment.java
Go to the documentation of this file.
1 package mhr.app.fragments.toolbox;
2
3 import mhr.app.AppMainActivity;
4 import mhr.app.R;
5 import mhr.app.touchlisteners.BrushToolTouchListener;
6 import mhr.appandroid.views.brushpicker.RoundBrushPickerView;
7 import mhr.appandroid.views.brushpicker.RoundBrushPickerView.RoundBrushPickerEvent;
8 import mhr.appandroid.views.brushpicker.RoundBrushPickerView.RoundBrushPickerViewEventListener;
9 import mhr.appandroid.views.colorpicker.ColorPickerView;
10 import mhr.appandroid.views.colorpicker.ColorPickerView.OnColorSelectedListener;
11 import mhr.appandroid.views.pathpicker.PathPickerView;
12 import mhr.appandroid.views.pathpicker.PathPickerView.PathParams;
13 import mhr.appandroid.views.pathpicker.PathPickerView.PathPickerEvent;
14 import mhr.appandroid.views.pathpicker.PathPickerView.PathPickerViewEventListener;
15 import mhr.appcore.bitmap.NBitmap;
16 import mhr.appcore.blending.BlendMode;
17 import mhr.appcore.commands.toolcommands.SelectToolCommand;
18 import mhr.appcore.tools.brushlike.Brush;
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.view.ViewGroup;
26 import android.widget.ArrayAdapter;
27 import android.widget.Button;
28 import android.widget.Spinner;
29
30
34 public class BrushToolSettingsFragment extends Fragment implements OnClickListener, RoundBrushPickerViewEventListener, PathPickerViewEventListener, OnColorSelectedListener {
35
36 //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
37 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
38 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
39
40 //===== FIELDS ==============================================================================================================================//
41 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
42 protected View root;
43 protected AppMainActivity activity;
44
45 protected ColorPickerView clrPicker;
46 protected RoundBrushPickerView brushPicker;
47 protected PathPickerView pathPicker;
48 protected boolean pathLivePreviewOn;
49 protected Button selectBrushToolBtn;
50 protected Spinner blendModePicker;
51 protected boolean firstCreated = true;
52
53 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
54
55 //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
56 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
64 protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
65 View v = inflater.inflate(R.layout.fragment_brush_tool_settings, container, false);
66 root = v;
67
68 blendModePicker = (Spinner) root.findViewById(R.id.BrushBlendModeSp);
69 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, R.array.BlendModeNames, android.R.layout.simple_spinner_item);
70 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
71 blendModePicker.setAdapter(adapter);
72
73 clrPicker = (ColorPickerView) root.findViewById(R.id.BrushColorPicker);
74 clrPicker.setOnSelectedListener(this);
75
76 brushPicker = (RoundBrushPickerView) root.findViewById(R.id.BrushRoundBrushPicker);
77 brushPicker.setRoundBrushPickerListener(this);
78
79 pathPicker = (PathPickerView) root.findViewById(R.id.BrushPathPicker);
80 pathPicker.setPathPickerListener(this);
81
82 pathLivePreviewOn = pathPicker.isLivePreviewOn();
83 selectBrushToolBtn = (Button) root.findViewById(R.id.SelectBrushToolBtn);
84 selectBrushToolBtn.setOnClickListener(this);
85 firstCreated = false;
86 return root;
87 }
88
89 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
90
91 //===== METHODS =============================================================================================================================//
92 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
96 protected void setBrush() {
97 PathParams pp = pathPicker.getNewPathParams();
98 activity.addCommand(new SelectToolCommand(new Brush(brushPicker.getNewBrush(), BlendMode.fromValue(blendModePicker.getSelectedItemPosition()), pp.opacity / 100.0,
99 pp.flow / 100.0, clrPicker.getNativeNewColor(), brushPicker.getSize() * pp.spacing / 100.0)));
100 }
101
102 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
103
104 //===== CALLBACKS ===========================================================================================================================//
105 @Override
106 public void onClick(View v) {
107 int id = v.getId();
108 switch (id) {
109 case R.id.SelectBrushToolBtn:
110 PathParams pp = pathPicker.getNewPathParams();
111 activity.addCommand(new SelectToolCommand(new Brush(brushPicker.getNewBrush(), BlendMode.fromValue(blendModePicker.getSelectedItemPosition()), pp.opacity / 100.0,
112 pp.flow / 100.0, clrPicker.getNativeNewColor(), brushPicker.getSize() * pp.spacing / 100.0)));
113 break;
114 }
115 }
116
117 @Override
118 public void onColorSelected(ColorPickerView v) {
119 PathParams pp = pathPicker.getNewPathParams();
120 activity.addCommand(new SelectToolCommand(new Brush(brushPicker.getNewBrush(), BlendMode.fromValue(blendModePicker.getSelectedItemPosition()), pp.opacity / 100.0,
121 pp.flow / 100.0, clrPicker.getNativeNewColor(), brushPicker.getSize() * pp.spacing / 100.0)));
122 }
123
124 @Override
125 public void onRoundBrushPickerEvent(RoundBrushPickerEvent e, RoundBrushPickerView v) {
126 switch (e) {
127 case EVENT_SELECTED:
128 if (!pathLivePreviewOn) {
129 NBitmap tmp = v.getNewBrush();
130 pathPicker.setBrush(tmp);
131 tmp.dispose();
132 }
133 setBrush();
134 break;
135
136 case EVENT_CANCELED:
137 if (!pathLivePreviewOn) {
138 NBitmap tmp = v.getNewBrush();
139 pathPicker.setBrush(tmp);
140 tmp.dispose();
141 }
142
143 break;
144
145 case EVENT_CHANGED:
146 if (pathLivePreviewOn) {
147 NBitmap tmp = v.getNewBrush();
148 pathPicker.setBrush(tmp);
149 tmp.dispose();
150 }
151
152 break;
153
154 case EVENT_LIVE_PREVIEW_STATE_CHANGED:
155
156 break;
157
158 default:
159 break;
160 }
161
162 }
163
164 @Override
165 public void onPathPickerEvent(PathPickerEvent e, PathPickerView v) {
166 switch (e) {
167 case EVENT_SELECTED:
168 setBrush();
169 break;
170
171 case EVENT_LIVE_PREVIEW_STATE_CHANGED:
172 pathLivePreviewOn = v.isLivePreviewOn();
173 if (pathLivePreviewOn) {
174 NBitmap tmp = brushPicker.getNewBrush();
175 pathPicker.setBrush(tmp);
176 tmp.dispose();
177 }
178 break;
179 default:
180 break;
181 }
182
183 }
184
185 //----- LIFE CYCLE --------------------------------------------------------------------------------------------------------------------------//
186
187 @Override
188 public void onAttach(Activity activity) {
189 super.onAttach(activity);
190 /*
191 * called once the fragment is associated with its activity.
192 */
193 this.activity = (AppMainActivity) activity;
194 }
195
196 // @Override
197 // public void onCreate(Bundle savedInstanceState) {
198 // super.onCreate(savedInstanceState);
199 // /*
200 // * called to do initial creation of the fragment.
201 // *
202 // * The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want
203 // * to retain when the fragment is paused or stopped, then resumed.
204 // */
205 // }
206
207 @Override
208 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
209 //return super.onCreateView(inflater, container, savedInstanceState);
210 /*
211 * creates and returns the view hierarchy associated with the fragment.
212 *
213 * The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a
214 * View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
215 *
216 * Zde začíná životní cyklus při návratu z backstacku
217 */
218 if (firstCreated) {
219 firstCreated = false;
220 return initView(inflater, container, savedInstanceState);
221 } else {
222 return root;
223 }
224 }
225
226 // @Override
227 // public void onActivityCreated(Bundle savedInstanceState) {
228 // super.onActivityCreated(savedInstanceState);
229 // /*
230 // * tells the fragment that its activity has completed its own Activity.onCreate().
231 // */
232 // }
233
234 // @Override // Vyžaduje API 17
235 // public void onViewStateRestored(Bundle savedInstanceState) {
236 // super.onViewStateRestored(savedInstanceState);
237 // /*
238 // * tells the fragment that all of the saved state of its view hierarchy has been restored.
239 // */
240 // }
241
242 // @Override
243 // public void onStart() {
244 // super.onStart();
245 // /*
246 // * makes the fragment visible to the user (based on its containing activity being started).
247 // */
248 // }
249
250 @Override
251 public void onResume() {
252 super.onResume();
253 /*
254 * makes the fragment interacting with the user (based on its containing activity being resumed).
255 */
256 NBitmap brush = brushPicker.getNewBrush();
257 pathPicker.setBrush(brush);
258 PathParams pp = pathPicker.getNewPathParams();
259 activity.addCommand(new SelectToolCommand(new Brush(brush, BlendMode.fromValue(blendModePicker.getSelectedItemPosition()), pp.opacity / 100.0, pp.flow / 100.0,
260 clrPicker.getNativeNewColor(), brushPicker.getSize() * pp.spacing / 100.0)));
261 activity.setCanvasOnTouchListener(new BrushToolTouchListener(activity));
262 }
263
264 // @Override
265 // public void onPause() {
266 // super.onPause();
267 // /*
268 // * fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
269 // *
270 // * The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed).
271 // * This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
272 // */
273 // }
274
275 // @Override
276 // public void onStop() {
277 // super.onStop();
278 // /*
279 // * fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
280 // */
281 // }
282
283 // @Override
284 // public void onDestroyView() {
285 // super.onDestroyView();
286 // /*
287 // * allows the fragment to clean up resources associated with its View.
288 // *
289 // * Zde končí životní cyklus při umístění do backstacku
290 // */
291 // }
292
293 // @Override
294 // public void onDestroy() {
295 // super.onDestroy();
296 // /*
297 // * called to do final cleanup of the fragment's state.
298 // */
299 // }
300
301 // @Override
302 // public void onDetach() {
303 // super.onDetach();
304 // /*
305 // * called immediately prior to the fragment no longer being associated with its activity.
306 // */
307 // }
308 }