[dasher: 31/38] Reinterpret click safety: center 'safe' viewport around click



commit ed9685e22a62809faa5a0a7e32f5ee273ac1540e
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Mon Nov 7 15:28:21 2011 +0000

    Reinterpret click safety: center 'safe' viewport around click
    
    Refactor ZoomAdjuster: prefs passed as params, simplifies inheritance
    
    TODO does this feel right???

 Src/DasherCore/ClickFilter.cpp  |   24 +++++++-----------------
 Src/DasherCore/ClickFilter.h    |   16 ++++++++--------
 Src/DasherCore/StylusFilter.cpp |    2 +-
 Src/DasherCore/StylusFilter.h   |    2 +-
 4 files changed, 17 insertions(+), 27 deletions(-)
---
diff --git a/Src/DasherCore/ClickFilter.cpp b/Src/DasherCore/ClickFilter.cpp
index b78df9d..a66a276 100644
--- a/Src/DasherCore/ClickFilter.cpp
+++ b/Src/DasherCore/ClickFilter.cpp
@@ -24,7 +24,7 @@ bool CClickFilter::DecorateView(CDasherView *pView, CDasherInput *pInput) {
   if (GetBoolParameter(BP_DRAW_MOUSE_LINE)) {
     myint mouseX, mouseY;
     pInput->GetDasherCoords(mouseX, mouseY, pView);
-    AdjustZoomCoords(mouseX, mouseY, pView);
+    AdjustZoomX(mouseX, pView, GetLongParameter(LP_S), GetLongParameter(LP_MAXZOOM));
     if (m_iLastX != mouseX || m_iLastY != mouseY) {
       bChanged = true;
       m_iLastX = mouseX; m_iLastY = mouseY;
@@ -58,28 +58,18 @@ bool CClickFilter::DecorateView(CDasherView *pView, CDasherInput *pInput) {
   return bChanged;
 }
 
-CZoomAdjuster::CZoomAdjuster(CSettingsUser *pCreateFrom) : CSettingsUser(pCreateFrom) {
-}
-
-void CZoomAdjuster::AdjustZoomCoords(myint &iDasherX, myint &iDasherY, CDasherView *pView) {
+void CZoomAdjuster::AdjustZoomX(myint &iDasherX, CDasherView *pView, myint safety, myint maxZoom) {
   //these equations don't work well for iDasherX just slightly over ORIGIN_X;
   // this is probably due to rounding error, but the "safety margin" doesn't
   // really seem helpful when zooming out (or translating) anyway...
   if (iDasherX >= CDasherModel::ORIGIN_X) return;
-  const myint safety(GetLongParameter(LP_S));
+
   //safety param. Used to be just added onto DasherX,
   // but comments suggested should be interpreted as a fraction. Hence...
   myint iNewDasherX = (iDasherX*1024 + CDasherModel::ORIGIN_X*safety) / (1024+safety);
 
-  //max zoom parameter...
-  iNewDasherX = std::max(CDasherModel::ORIGIN_X/GetLongParameter(LP_MAXZOOM),iNewDasherX);
-  //force x>=2 (what's wrong with x==1?)
-  if (iNewDasherX<2) iNewDasherX=2;
-  if (iNewDasherX != iDasherX) {
-    //compute new dasher y to keep centre of expansion in same place...
-    myint iNewDasherY = CDasherModel::ORIGIN_Y + ((CDasherModel::ORIGIN_X-iNewDasherX) * (iDasherY-CDasherModel::ORIGIN_Y))/(CDasherModel::ORIGIN_X-iDasherX);
-    iDasherX = iNewDasherX; iDasherY = iNewDasherY;
-  }
+  //max zoom parameter...also force x>=2 (what's wrong with x==1?)
+  iDasherX = std::max(std::max(myint(2),CDasherModel::ORIGIN_X/maxZoom),iNewDasherX);  
 }
 
 void CClickFilter::KeyDown(unsigned long iTime, int iId, CDasherView *pView, CDasherInput *pInput, CDasherModel *pModel) {
@@ -90,8 +80,8 @@ void CClickFilter::KeyDown(unsigned long iTime, int iId, CDasherView *pView, CDa
       myint iDasherY;
 
       pInput->GetDasherCoords(iDasherX, iDasherY, pView);
-      AdjustZoomCoords(iDasherX, iDasherY, pView);
-      ScheduleZoom(pModel,iDasherY-iDasherX, iDasherY+iDasherX);
+      AdjustZoomX(iDasherX, pView, GetLongParameter(LP_S), GetLongParameter(LP_MAXZOOM));
+      ScheduleZoom(pModel, iDasherY-iDasherX, iDasherY+iDasherX);
     }
     break;
   default:
diff --git a/Src/DasherCore/ClickFilter.h b/Src/DasherCore/ClickFilter.h
index 36231fd..76b86cf 100644
--- a/Src/DasherCore/ClickFilter.h
+++ b/Src/DasherCore/ClickFilter.h
@@ -7,20 +7,20 @@
 /// @{
 namespace Dasher {
   ///Utility class for transforming co-ordinates of a mouse click to zoom target
-  class CZoomAdjuster : protected CSettingsUser {
+  class CZoomAdjuster {
   public:
-    //Everything public so we can use composition as well as inheritance
-    CZoomAdjuster(CSettingsUser *pCreator);
     /// Adjust co-ordinates of mouse click into coordinates for zoom target.
-    /// Adds a safety margin according to LP_S, checks we don't exceed the
-    /// zoom factor given by LP_MAXZOOM, and ensures x>=2.
-    void AdjustZoomCoords(myint &iDasherX, myint &iDasherY, CDasherView *comp);
+    /// \param safety Fraction (/1024) by which not to zoom in (e.g. 25 = 
+    /// zoom in ~~2.5% less than suggested)
+    /// \param maxZoom maximum factor by which to zoom in; note that
+    /// regardless of this parameter, we never zoom in to x<2.
+    void AdjustZoomX(myint &iDasherX, CDasherView *comp, myint safety, myint maxZoom);
   };
   
-class CClickFilter : public CStaticFilter, private CZoomAdjuster {
+class CClickFilter : public CStaticFilter, protected CSettingsUser, private CZoomAdjuster {
  public:
   CClickFilter(CSettingsUser *pCreator, CDasherInterfaceBase *pInterface)
-    : CStaticFilter(pInterface, 7, _("Click Mode")), CZoomAdjuster(pCreator) { };
+    : CStaticFilter(pInterface, 7, _("Click Mode")), CSettingsUser(pCreator) { };
 
   virtual bool DecorateView(CDasherView *pView, CDasherInput *pInput);
   virtual void KeyDown(unsigned long iTime, int iId, CDasherView *pView, CDasherInput *pInput, CDasherModel *pModel);
diff --git a/Src/DasherCore/StylusFilter.cpp b/Src/DasherCore/StylusFilter.cpp
index a8adda9..2caa57b 100644
--- a/Src/DasherCore/StylusFilter.cpp
+++ b/Src/DasherCore/StylusFilter.cpp
@@ -39,7 +39,7 @@ void CStylusFilter::KeyUp(unsigned long iTime, int iId, CDasherView *pView, CDas
 }
 
 void CStylusFilter::ApplyClickTransform(myint &iDasherX, myint &iDasherY, CDasherView *pView) {
-  CZoomAdjuster(this).AdjustZoomCoords(iDasherX, iDasherY, pView);
+  AdjustZoomX(iDasherX, pView, GetLongParameter(LP_S), GetLongParameter(LP_MAXZOOM));
 }
 
 CStartHandler *CStylusFilter::MakeStartHandler() {
diff --git a/Src/DasherCore/StylusFilter.h b/Src/DasherCore/StylusFilter.h
index 7e6e4dd..670f1ce 100644
--- a/Src/DasherCore/StylusFilter.h
+++ b/Src/DasherCore/StylusFilter.h
@@ -7,7 +7,7 @@
 /// \ingroup InputFilter
 /// @{
 namespace Dasher {
-class CStylusFilter : public CDefaultFilter {
+class CStylusFilter : public CDefaultFilter, protected CZoomAdjuster {
  public:
   CStylusFilter(CSettingsUser *pCreator, CDasherInterfaceBase *pInterface, CFrameRate *pFramerate, ModuleID_t iID=15, const char *szName=_("Stylus Control"));
   ///Override DefaultFilter (which supports pause), as we don't



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]