3 * This file is part of shape4a.
5 * shape4a is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * shape4a is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with shape4a. If not, see <http://www.gnu.org/licenses/>.
19 package org.ocera.orte.shape4a;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.content.DialogInterface;
24 import android.content.res.Configuration;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.support.v4.app.DialogFragment;
28 import android.support.v4.app.Fragment;
29 import android.view.LayoutInflater;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.LinearLayout;
36 import android.widget.SeekBar;
38 import org.ocera.orte.DomainApp;
41 * Fragment containing publishing workspace.
43 * @author jiri hubacek <jiri.hubacek@gmail.com>
47 public class PublisherFragment extends Fragment {
48 private static final int REDRAW_INTERVAL = 50;
49 private static final int STRENGTH_MAX = 5;
51 private View rootView;
52 private PublisherView publisherView;
54 private Handler handler = new Handler();
55 private Runnable redraw = new Runnable()
60 publisherView.countShapes();
61 publisherView.invalidate();
62 handler.postDelayed(this, REDRAW_INTERVAL);
66 NewDialogFragment newDialogFragment = new NewDialogFragment();
67 PublisherSettingsDialogFragment publisherSettingsDialogFragment = new PublisherSettingsDialogFragment();
69 private DomainApp appDomain;
72 * Constructor just stores appDomain variable.
77 public PublisherFragment(DomainApp appDomain)
79 this.appDomain = appDomain;
83 * When screen orientation is changed,
84 * scale recalculation is needed.
89 public void onConfigurationChanged(Configuration newConfig)
91 for (PublisherShape s : this.publisherView.shapes) {
92 s.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
97 * Setting up workspace, adding publisherView
98 * and timer (handler) for drawing objects.
103 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
105 this.rootView = inflater.inflate(R.layout.fragment_publisher, container, false);
106 this.publisherView = (PublisherView) this.rootView.findViewById(R.id.publisher_view);
107 this.handler.postDelayed(redraw, REDRAW_INTERVAL);
109 return this.rootView;
118 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
120 inflater.inflate(R.menu.publisher, menu);
124 * All created publishers are in menu.
129 public void onPrepareOptionsMenu(Menu menu)
131 for (PublisherShape shape : publisherView.shapes) {
132 menu.add(0, publisherView.shapes.indexOf(shape), 20 + publisherView.shapes.indexOf(shape), "#" + publisherView.shapes.indexOf(shape) + " " + shape.getColorName() + " " + shape.getShapeName());
137 * Select settings of publishers
138 * or add new publisher.
143 public boolean onOptionsItemSelected(MenuItem item)
145 // Handle action bar item clicks here. The action bar will
146 // automatically handle clicks on the Home/Up button, so long
147 // as you specify a parent activity in AndroidManifest.xml.
148 int id = item.getItemId();
150 if (id >= 0 && id < publisherView.shapes.size()) {
151 publisherSettingsDialogFragment.setId(id);
152 publisherSettingsDialogFragment.show(getFragmentManager(), "dialog");
155 case R.id.action_new:
156 newDialogFragment.show(getFragmentManager(), "dialog");
160 return super.onOptionsItemSelected(item);
164 * Dialog for adding new publisher
165 * throw action bar button +.
169 public class NewDialogFragment extends DialogFragment
172 * Selection of colors is badly solved,
173 * it should be altered..
176 private String[] colors = new String[5];
179 * In constructor, make array
184 public NewDialogFragment()
186 // Texts should be done better way,
187 // like centralized in strings.xml
193 colors[4] = "Yellow";
197 * Creating dialog for new publisher.
202 public Dialog onCreateDialog(Bundle savedInstanceState)
204 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
205 builder.setTitle(R.string.dialog_newPublisher)
206 .setItems(colors, new DialogInterface.OnClickListener()
209 public void onClick(DialogInterface dialog, int which)
213 publisherView.addShape(which, appDomain);
215 for (PublisherShape s : publisherView.shapes) {
216 s.setScale(getActivity().getWindowManager().getDefaultDisplay().getWidth(), getActivity().getWindowManager().getDefaultDisplay().getHeight());
219 getActivity().supportInvalidateOptionsMenu();
222 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
225 public void onClick(DialogInterface dialog, int which)
229 return builder.create();
235 * When click on shape in Action Bar menu.
239 public class PublisherSettingsDialogFragment extends DialogFragment
242 private int shapeStrength;
243 private SeekBar seekStrength;
246 * No constructor needed.
250 public PublisherSettingsDialogFragment()
254 * Set wanted publisher's dialog.
256 * @param i Index of publisher.
259 public void setId(int i)
262 this.shapeStrength = publisherView.shapes.get(this.shapeId).getStrength();
266 * Creating dialog for publisher's settings.
271 public Dialog onCreateDialog(Bundle savedInstanceState)
273 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
274 builder.setTitle("#" + this.shapeId + " " + publisherView.shapes.get(this.shapeId).getColorName() + " " + publisherView.shapes.get(this.shapeId).getShapeName() + " strength:")
276 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener()
279 public void onClick(DialogInterface dialog, int which)
281 publisherView.shapes.get(shapeId).setStrength(shapeStrength);
284 .setNeutralButton(R.string.dialog_delete, new DialogInterface.OnClickListener()
287 public void onClick(DialogInterface dialog, int which) {
288 publisherView.shapes.get(shapeId).killMe();
289 publisherView.shapes.remove(shapeId);
290 getActivity().supportInvalidateOptionsMenu();
293 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
296 public void onClick(DialogInterface dialog, int which)
300 return builder.create();
304 * SeekBar for setting-up strength of publishers.
306 * @return Layout of SeekBar.
309 private View seekBar(){
310 LinearLayout l = new LinearLayout(getActivity());
311 l.setOrientation(LinearLayout.VERTICAL);
313 seekStrength = new SeekBar(getActivity());
314 seekStrength.setMax(STRENGTH_MAX);
315 seekStrength.setProgress(shapeStrength);
316 seekStrength.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
319 public void onStopTrackingTouch(SeekBar seekBar) {
323 public void onStartTrackingTouch(SeekBar seekBar) {
327 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
329 shapeStrength = progress;
333 l.addView(seekStrength);