new plotter

This commit is contained in:
Sergey Solovyev 2013-01-19 16:02:25 +04:00
parent 0c39c155b6
commit 2ebf5841e1
6 changed files with 92 additions and 47 deletions

View File

@ -83,4 +83,8 @@ public abstract class CalculatorFragment extends SherlockFragment {
public void onDetach() { public void onDetach() {
super.onDetach(); super.onDetach();
} }
public boolean isPaneFragment() {
return fragmentHelper.isPane(this);
}
} }

View File

@ -106,9 +106,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
public void onCreate(Bundle in) { public void onCreate(Bundle in) {
super.onCreate(in); super.onCreate(in);
// todo serso: init variable properly if (isPaneFragment()) {
boolean paneFragment = true;
if (paneFragment) {
this.bgColor = getResources().getColor(R.color.cpp_pane_background); this.bgColor = getResources().getColor(R.color.cpp_pane_background);
} else { } else {
this.bgColor = getResources().getColor(android.R.color.transparent); this.bgColor = getResources().getColor(android.R.color.transparent);

View File

@ -243,7 +243,6 @@ public class CalculatorGraph2dView extends View implements GraphView {
if (lastTouchXPxs != NO_TOUCH && lastTouchYPxs != NO_TOUCH) { if (lastTouchXPxs != NO_TOUCH && lastTouchYPxs != NO_TOUCH) {
paint.setColor(graphViewHelper.getPlotViewDef().getGridColor()); paint.setColor(graphViewHelper.getPlotViewDef().getGridColor());
paint.setAlpha(100);
canvas.drawLine(lastTouchXPxs, 0, lastTouchXPxs, heightPxs, paint); canvas.drawLine(lastTouchXPxs, 0, lastTouchXPxs, heightPxs, paint);
canvas.drawLine(0, lastTouchYPxs, widthPxs, lastTouchYPxs, paint); canvas.drawLine(0, lastTouchYPxs, widthPxs, lastTouchYPxs, paint);
@ -251,9 +250,6 @@ public class CalculatorGraph2dView extends View implements GraphView {
final Point2d lastTouch = dimensions.toGraphCoordinates(lastTouchXPxs, lastTouchYPxs); final Point2d lastTouch = dimensions.toGraphCoordinates(lastTouchXPxs, lastTouchYPxs);
final String touchLabel = "[" + formatTick(lastTouch.getX(), tickDigits + 1) + ", " + formatTick(lastTouch.getY(), tickDigits + 1) + "]"; final String touchLabel = "[" + formatTick(lastTouch.getX(), tickDigits + 1) + ", " + formatTick(lastTouch.getY(), tickDigits + 1) + "]";
canvas.drawText(touchLabel, 0, touchLabel.length(), lastTouchXPxs - 40, lastTouchYPxs - 40, textPaint); canvas.drawText(touchLabel, 0, touchLabel.length(), lastTouchXPxs - 40, lastTouchYPxs - 40, textPaint);
// restore alpha
paint.setAlpha(255);
} }
} }

View File

@ -11,6 +11,34 @@ import org.solovyev.common.math.Point2d;
*/ */
public class Graph2dDimensions { public class Graph2dDimensions {
// |<--------------gWidth-------------->|
// xMin xMax
// -------------------|------------------------------------|--------------------
// |<-------------vWidthPs------------->|
//
/*
*
*
* 0------------------------------------|--> xPxs
* |
* |
* | y
* | ^
* | |
* | |
* | |
* |------------------0-----------------|--> x
* | |
* | |
* | |
* | |
* | |
* v
* yPxs
*
* */
@NotNull @NotNull
private GraphView graphView; private GraphView graphView;
@ -39,7 +67,11 @@ public class Graph2dDimensions {
@NotNull @NotNull
Point2d toGraphCoordinates(float xPxs, float yPxs) { Point2d toGraphCoordinates(float xPxs, float yPxs) {
return new Point2d(xPxs * getGraphToViewRatio() + getXMin(), - (yPxs * getGraphToViewRatio() + getYMin())); return new Point2d( scalePxs(xPxs) + getXMin(), (getGraphHeight() - scalePxs(yPxs)) + getYMin() );
}
private float scalePxs(float pxs) {
return pxs * getGraphToViewRatio();
} }
// X // X
@ -82,9 +114,9 @@ public class Graph2dDimensions {
float getGraphToViewRatio() { float getGraphToViewRatio() {
if (vWidthPxs != 0) { if (vWidthPxs != 0) {
return gWidth / vWidthPxs; return gWidth / ((float)vWidthPxs);
} else { } else {
return 0; return 0f;
} }
} }
@ -92,7 +124,7 @@ public class Graph2dDimensions {
if (vWidthPxs != 0) { if (vWidthPxs != 0) {
return ((float) vHeightPxs) / vWidthPxs; return ((float) vHeightPxs) / vWidthPxs;
} else { } else {
return 0; return 0f;
} }
} }

View File

@ -193,10 +193,10 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
synchronized (functions) { synchronized (functions) {
for (int i = 0; i < functions.size(); i++) { for (int i = 0; i < functions.size(); i++) {
final PlotFunction plotFunction = functions.get(i); final PlotFunction oldFunction = functions.get(i);
if (plotFunction.equals(newFunction)) { if (oldFunction.equals(newFunction)) {
resourceManager.unregister(plotFunction.getPlotLineDef()); resourceManager.unregister(oldFunction.getPlotLineDef());
resourceManager.register(newFunction.getPlotLineDef()); resourceManager.register(newFunction.getPlotLineDef());
// update old function // update old function

View File

@ -1,11 +1,11 @@
package org.solovyev.android.calculator.plot; package org.solovyev.android.calculator.plot;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import javax.annotation.Nullable;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
/** /**
* User: serso * User: serso
@ -15,7 +15,7 @@ import java.util.Map;
public class MapPlotResourceManager implements PlotResourceManager { public class MapPlotResourceManager implements PlotResourceManager {
@NotNull @NotNull
private Map<PlotLineDef, Integer> counters = new HashMap<PlotLineDef, Integer>(); private Map<PlotLineDef, List<PlotLineDef>> registeredLineDefsMap = new HashMap<PlotLineDef, List<PlotLineDef>>();
@NotNull @NotNull
private final List<PlotLineDef> preparedLineDefs = new ArrayList<PlotLineDef>(PlotLineStyle.values().length * PlotLineColor.values().length); private final List<PlotLineDef> preparedLineDefs = new ArrayList<PlotLineDef>(PlotLineStyle.values().length * PlotLineColor.values().length);
@ -33,9 +33,9 @@ public class MapPlotResourceManager implements PlotResourceManager {
public PlotLineDef generateAndRegister() { public PlotLineDef generateAndRegister() {
synchronized (this) { synchronized (this) {
for (PlotLineDef lineDef : preparedLineDefs) { for (PlotLineDef lineDef : preparedLineDefs) {
final Integer counter = counters.get(lineDef); final List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(lineDef);
if ( counter == null || counter.equals(0) ) { if ( registeredLineDefs == null || registeredLineDefs.isEmpty() ) {
register0(lineDef); register(lineDef);
return lineDef; return lineDef;
} }
} }
@ -44,56 +44,71 @@ public class MapPlotResourceManager implements PlotResourceManager {
} }
} }
private void increaseCounter(@NotNull PlotLineDef lineDef) { private void addLineDef(@NotNull final PlotLineDef toBeAdded) {
assert Thread.holdsLock(this); assert Thread.holdsLock(this);
Integer counter = counters.get(lineDef); List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeAdded);
if ( counter == null ) { if ( registeredLineDefs == null ) {
counter = 0; registeredLineDefs = new ArrayList<PlotLineDef>();
} registeredLineDefsMap.put(toBeAdded, registeredLineDefs);
counter++;
counters.put(lineDef, counter);
} }
private void decreaseCounter(@NotNull PlotLineDef lineDef) { try {
Iterables.find(registeredLineDefs, new Predicate<PlotLineDef>() {
@Override
public boolean apply(@Nullable PlotLineDef lineDef) {
return lineDef == toBeAdded;
}
});
// already added
} catch (NoSuchElementException e) {
registeredLineDefs.add(toBeAdded);
}
}
private void removeLineDef(@NotNull final PlotLineDef toBeRemoved) {
assert Thread.holdsLock(this); assert Thread.holdsLock(this);
Integer counter = counters.get(lineDef); List<PlotLineDef> registeredLineDefs = registeredLineDefsMap.get(toBeRemoved);
if (counter != null) {
counter--; if (registeredLineDefs != null) {
counters.put(lineDef, Math.max(counter, 0)); Iterables.removeIf(registeredLineDefs, new Predicate<PlotLineDef>() {
@Override
public boolean apply(@Nullable PlotLineDef lineDef) {
return lineDef == toBeRemoved;
}
});
if ( registeredLineDefs.isEmpty() ) {
registeredLineDefsMap.remove(toBeRemoved);
}
} else {
registeredLineDefsMap.remove(toBeRemoved);
} }
} }
@Override @Override
public void register(@NotNull PlotLineDef lineDef) { public void register(@NotNull PlotLineDef lineDef) {
synchronized (this) { synchronized (this) {
// we should check if specified line def is not ours, i.e. created by this class addLineDef(lineDef);
for (PlotLineDef preparedLineDef : preparedLineDefs) {
if ( preparedLineDef == lineDef ) {
return;
} }
} }
register0(lineDef);
}
}
private void register0(@NotNull PlotLineDef lineDef) {
increaseCounter(lineDef);
}
@Override @Override
public void unregister(@NotNull PlotLineDef lineDef) { public void unregister(@NotNull PlotLineDef lineDef) {
synchronized (this) { synchronized (this) {
decreaseCounter(lineDef); removeLineDef(lineDef);
} }
} }
@Override @Override
public void unregisterAll() { public void unregisterAll() {
synchronized (this) { synchronized (this) {
counters.clear(); registeredLineDefsMap.clear();
} }
} }
} }