B4A Class Continuous knob

Discussion in 'Additional libraries, classes and official updates' started by pauleffect, Mar 9, 2014.

  1. pauleffect

    pauleffect Member Licensed User

    Hello.
    I searched the forum and gave it a long thought (actually closed this -posting- window several times throughout the past weeks) but haven't managed to figure it out.

    I'm trying to create a continuous rotary knob. It should return something like 1 when you rotate it to the right and -1 when you do the same, but to the left this time.

    Everything I've seen has min-max values. And when you would reach the max value it would reset to the min, thus interrupting the seeking process in a disturbing fashion.

    If anybody has a good solution, would you kindly point me in the right direction?

    Thank you!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The attached project should help you get started:

    upload_2014-3-9_17-42-3.png
     

    Attached Files:

    • Knob.zip
      File size:
      10.8 KB
      Views:
      137
    GabrielM and pauleffect like this.
  3. pauleffect

    pauleffect Member Licensed User

    You did this for me?

    I'm... very grateful and impressed. Thank you a lot!
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    For you... and for any developer who like to implement such a view.
     
    mesutaslan and hibrid0 like this.
  5. Informatix

    Informatix Expert Licensed User

    Did you look at the knobs provided at the end of my tutorial?
     
    wonder and Erel like this.
  6. Informatix

    Informatix Expert Licensed User

    By testing my old code for knobs, I noticed that it did not work with the obfuscation mode. This is now fixed.
     
  7. pauleffect

    pauleffect Member Licensed User

    I was just thinking about you, @Informatix.

    Specifically, the bitmap plus library.

    I'm trying to use Erel's code with bitmap plus. Once I'm satisfied with the outcome, I'll post the results.

    Thank you both for your time.
     
  8. Manuel Moreno-Eguilaz

    Manuel Moreno-Eguilaz New Member Licensed User

    Hi Erel,

    Thank you very much for your attached project.

    Is it possible to pass some parameters to the knob object when calling Initialize function?

    I would like to limitate the minimum and maximum values in one Knob by passing these values in the creation process of the object.

    Thank you. I love B4A.

    Manuel
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Yes. You can add parameters to the initialize sub.
     
  10. DT1111

    DT1111 Member Licensed User

    Hi Erel

    Thanks for the Knob example and I have modified the pnl_Touch sub so that the knob will only rotate between 0 and 360 deg.

    It worked when I rotate it slowly but when (my guess) I placed my finger close to the center plus various other point of contact combinations, and rotate, the 0 to 360 deg constraint fails.

    I don't understand and can you please enlighten. Thanks.

    Code:
    Sub pnl_Touch  (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
        
    If action = 0 Then 'ACTION_DOWN
            startAngle = ATan2(X - radius, Y - radius)
        
    Else If action = 2 Then 'ACTION_MOVE
            Dim angle As Float = ATan2(X - radius, Y - radius)
            currentAngle = currentAngle - angle + startAngle
            
    Dim delta As Int = (startAngle - angle) * 180 / cPI
            
    If delta > 180 Then
                delta = delta - 
    360
            
    Else If delta < -180 Then
                delta = delta + 
    360
            
    End If
           
            
    If (currentAngle * 180 / cPI < 0.0Then
                currentAngle = 
    0.0
            
    Else If (currentAngle * 180 / cPI > 360.0Then
                currentAngle = (
    360/180) * cPI
            
    End If
           
            CallSubDelayed2(CallBack, EventName & 
    "_AngleChanged", delta)
            startAngle = angle
            cvs.DrawBitmapRotated(bmp, 
    Null, destRect, currentAngle * 180 / cPI)
            pnl.Invalidate
        
    End If
        
    Return True
    End Sub
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    If you are only interested in the current value then you can use this code:
    Code:
    Sub pnl_Touch  (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
       
    If action = 0 Then 'ACTION_DOWN
         startAngle = ATan2(X - radius, Y - radius)
       
    Else If action = 2 Then 'ACTION_MOVE
         Dim angle As Float = ATan2(X - radius, Y - radius)
         currentAngle = currentAngle - angle + startAngle
         currentAngle = (currentAngle + 
    2 * cPImod (2 * cPI)
         CallSubDelayed2(CallBack, EventName & 
    "_AngleChanged", currentAngle * 180 / cPI)
         startAngle = angle
         cvs.DrawBitmapRotated(bmp, 
    Null, destRect, currentAngle * 180 / cPI)
         pnl.Invalidate
       
    End If
       
    Return True
    End Sub
    The event handler:
    Code:
    Sub Knob1_AngleChanged (CurrentAngleDegrees As Double)
       
    Log(CurrentAngleDegrees)
    End Sub
     
  12. DT1111

    DT1111 Member Licensed User

    Hi Erel

    That is a neat solution. Actually my attempt is to put a "hard" stop at zero deg (rotating anti-CW) and another one at 360 deg (rotating CW).

    At the moment my code above works intermittently. Don't know why.
     
  13. Informatix

    Informatix Expert Licensed User

    Look at post #5
     
  14. DT1111

    DT1111 Member Licensed User

    Hi Informatix, thanks for the link. It is exactly what I have in mind ie the rotary knob. VERY impressive. Not quite rocket science but close IMHO.

    It is not done via Designer and my preference is to use Designer as I have other things built around the knob. I am still interested how the same idea could be done using the above code though. Thanks again.
     
  15. Informatix

    Informatix Expert Licensed User

    Then replace the Erel's code by mine in the above class...
     
    DT1111 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice