diff --git a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java index f3af186b..17912488 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/CalculatorFragment.java @@ -83,4 +83,8 @@ public abstract class CalculatorFragment extends SherlockFragment { public void onDetach() { super.onDetach(); } + + public boolean isPaneFragment() { + return fragmentHelper.isPane(this); + } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java index b0b00cbb..dbf90e6a 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java @@ -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); diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java index b4e19133..c0e98ac9 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java @@ -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); } } diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/Graph2dDimensions.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/Graph2dDimensions.java index 655580b1..b0e41999 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/Graph2dDimensions.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/plot/Graph2dDimensions.java @@ -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; } } diff --git a/core/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotterImpl.java b/core/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotterImpl.java index 51a155d9..10ca012e 100644 --- a/core/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotterImpl.java +++ b/core/src/main/java/org/solovyev/android/calculator/plot/CalculatorPlotterImpl.java @@ -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 diff --git a/core/src/main/java/org/solovyev/android/calculator/plot/MapPlotResourceManager.java b/core/src/main/java/org/solovyev/android/calculator/plot/MapPlotResourceManager.java index 4b7d0ae9..adb89e53 100644 --- a/core/src/main/java/org/solovyev/android/calculator/plot/MapPlotResourceManager.java +++ b/core/src/main/java/org/solovyev/android/calculator/plot/MapPlotResourceManager.java @@ -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 counters = new HashMap(); + private Map> registeredLineDefsMap = new HashMap>(); @NotNull private final List preparedLineDefs = new ArrayList(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 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 registeredLineDefs = registeredLineDefsMap.get(toBeAdded); + if ( registeredLineDefs == null ) { + registeredLineDefs = new ArrayList(); + registeredLineDefsMap.put(toBeAdded, registeredLineDefs); } - counter++; - counters.put(lineDef, counter); + + try { + Iterables.find(registeredLineDefs, new Predicate() { + @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 registeredLineDefs = registeredLineDefsMap.get(toBeRemoved); + + if (registeredLineDefs != null) { + Iterables.removeIf(registeredLineDefs, new Predicate() { + @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(); } } }