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() {
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) {
super.onCreate(in);
// todo serso: init variable properly
boolean paneFragment = true;
if (paneFragment) {
if (isPaneFragment()) {
this.bgColor = getResources().getColor(R.color.cpp_pane_background);
} else {
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) {
paint.setColor(graphViewHelper.getPlotViewDef().getGridColor());
paint.setAlpha(100);
canvas.drawLine(lastTouchXPxs, 0, lastTouchXPxs, heightPxs, 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 String touchLabel = "[" + formatTick(lastTouch.getX(), tickDigits + 1) + ", " + formatTick(lastTouch.getY(), tickDigits + 1) + "]";
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 {
// |<--------------gWidth-------------->|
// xMin xMax
// -------------------|------------------------------------|--------------------
// |<-------------vWidthPs------------->|
//
/*
*
*
* 0------------------------------------|--> xPxs
* |
* |
* | y
* | ^
* | |
* | |
* | |
* |------------------0-----------------|--> x
* | |
* | |
* | |
* | |
* | |
* v
* yPxs
*
* */
@NotNull
private GraphView graphView;
@ -39,7 +67,11 @@ public class Graph2dDimensions {
@NotNull
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
@ -82,9 +114,9 @@ public class Graph2dDimensions {
float getGraphToViewRatio() {
if (vWidthPxs != 0) {
return gWidth / vWidthPxs;
return gWidth / ((float)vWidthPxs);
} else {
return 0;
return 0f;
}
}
@ -92,7 +124,7 @@ public class Graph2dDimensions {
if (vWidthPxs != 0) {
return ((float) vHeightPxs) / vWidthPxs;
} else {
return 0;
return 0f;
}
}

View File

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

View File

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