From f754c6286a49ad76be1f2b8ad0c47b45f17829a1 Mon Sep 17 00:00:00 2001
From: Charan DSouza <godwincharan@gmail.com>
Date: Sat, 2 Sep 2017 04:41:30 +0530
Subject: [PATCH] Fixed the bug #65. Analysis: This bug is there for following
 reasons. 1.ElectrodeButton for channels is directly added to the
 ChannelMappingEditor component. 2.When we drag the button we set the
 visibility of the lastHoverButton which makes it visible and shows on top of
 other components. 3. Also finding the correct button Logic is not proper as
 we are not considering the buttons which are scrolled up and invisible.

Solution:
1. Create Viewport for holding the ElectrodeButton for channels.
2. Also create a Holder Component to hold all ElectrodeButton.
3. Add the ElectrodeButton to Holder Component and set the size of the Holder Component to fit all buttons.
4. Set setViewPosition for Viewport based on the scroll and drag.

More Improvement:
1. We can remove upButton and downButton.
2. Viewport has its own scrolling which can be used for scrolling. Currently its been hidden using code "electrodeButtonViewport->setScrollBarsShown(false,false,true,true);"
---
 .../ChannelMappingEditor.cpp                  | 56 +++++++++++++------
 .../ChannelMappingNode/ChannelMappingEditor.h |  2 +
 2 files changed, 40 insertions(+), 18 deletions(-)
 mode change 100644 => 100755 Source/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp
 mode change 100644 => 100755 Source/Plugins/ChannelMappingNode/ChannelMappingEditor.h

diff --git a/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp b/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp
old mode 100644
new mode 100755
index 5f334b650..3c312b6a4
--- a/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp
+++ b/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.cpp
@@ -67,6 +67,13 @@ ChannelMappingEditor::ChannelMappingEditor(GenericProcessor* parentNode, bool us
     downButton->setBounds(320,45,10,8);
     addAndMakeVisible(downButton);
     downButton->setVisible(false);
+    
+    addAndMakeVisible(electrodeButtonViewport = new Viewport());
+    electrodeButtonViewport->setBounds(10,30,330,70);
+    electrodeButtonViewport->setScrollBarsShown(false,false,true,true);
+    electrodeButtonHolder = new Component();
+    electrodeButtonViewport->setViewedComponent(electrodeButtonHolder,false);
+    
 
     loadButton = new LoadButton();
     loadButton->addListener(this);
@@ -169,7 +176,7 @@ void ChannelMappingEditor::createElectrodeButtons(int numNeeded, bool clearPrevi
         // if (!getCollapsedState())
         //     addAndMakeVisible(button);
         // else
-        addChildComponent(button); // determine visibility in refreshButtonLocations()
+        electrodeButtonHolder->addAndMakeVisible(button); // determine visibility in refreshButtonLocations()
 
         button->addListener(this);
         if (reorderActive)
@@ -223,29 +230,28 @@ void ChannelMappingEditor::createElectrodeButtons(int numNeeded, bool clearPrevi
 
 void ChannelMappingEditor::refreshButtonLocations()
 {
+    electrodeButtonViewport->setVisible(!getCollapsedState());
+    
+    electrodeButtonViewport->setViewPosition( 0,scrollDistance);
     int width = 19;
     int height = 15;
-    int row = (int) -scrollDistance;
+    int row = 0;
     int column = 0;
-
+    int totalWidth = 0;
+    int totalHeight = 0;
     for (int i = 0; i < electrodeButtons.size(); i++)
     {
-
         ElectrodeButton* button = electrodeButtons[i];
-
-        button->setBounds(10+(column++)*(width), 30+row*(height), width, 15);
-
-        if (row <= 4 && row >= 0 && !getCollapsedState())
-            button->setVisible(true);
-        else
-            button->setVisible(false);
-
+        button->setBounds(column*width, row*height, width, height);
+        totalWidth =  jmax(totalWidth, ++column*width);
+        
         if (column % 16 == 0)
         {
-            column = 0;
-            row++;
+            totalHeight =  jmax(totalHeight, ++row*height);
+            column = 0;            
         }
     }
+    electrodeButtonHolder->setSize(totalWidth,totalHeight);
 }
 
 void ChannelMappingEditor::collapsedStateChanged()
@@ -767,12 +773,26 @@ void ChannelMappingEditor::mouseDrag(const MouseEvent& e)
         else if (isDragging)
         {
             MouseEvent ev = e.getEventRelativeTo(this);
-
-            int col = ((ev.x-5) / 20);
+            int mouseDownY = ev.getMouseDownY()-30;
+            int mouseDownX = ev.getMouseDownX()-10;
+            Point<int> viewPosition =electrodeButtonViewport->getViewPosition();
+            
+            int distanceY = ev.getDistanceFromDragStartY();
+            int distanceX = ev.getDistanceFromDragStartX();
+            
+            int newPosY = viewPosition.getY()+ mouseDownY + distanceY;
+            int newPosX = viewPosition.getX()+ mouseDownX + distanceX;
+            if ( mouseDownY + distanceY > 70){
+                electrodeButtonViewport->setViewPosition(viewPosition.getX(),newPosY);
+            }else if( mouseDownY + distanceY < 0 ){
+                electrodeButtonViewport->setViewPosition(viewPosition.getX(),newPosY);
+            }
+            
+            
+            int col = (newPosX / 19);
             if (col < 0) col = 0;
             else if (col > 16) col = 16;
-
-            int row = ((ev.y-30) / 15);
+            int row = (newPosY / 15);
             if (row < 0) row = 0;
 
             int hoverButton = row*16+col;
diff --git a/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.h b/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.h
old mode 100644
new mode 100755
index e2b2408b4..d134b1164
--- a/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.h
+++ b/Source/Plugins/ChannelMappingNode/ChannelMappingEditor.h
@@ -90,6 +90,8 @@ private:
     ScopedPointer<TriangleButton> downButton;
     ScopedPointer<LoadButton> loadButton;
     ScopedPointer<SaveButton> saveButton;
+    ScopedPointer<Viewport> electrodeButtonViewport;
+    ScopedPointer<Component> electrodeButtonHolder;
 
     Array<int> channelArray;
     Array<int> referenceArray;
-- 
GitLab