A Circular Slider

24 06 2009

Sliders are very nifty UI control tool, they provide some sort of natural feedback to the user visually when they interact with it. Especially in a touch user interface sliders are very useful. In PyMT we already have Horizontal and Vertical rectangular sliders.

PyMT rectangular sliders

PyMT rectangular sliders

But I needed something different, a circular slider for the next widget that I wanted to develop a Quarter Circle color picker. So I sat down with the sketchbook again and noted down the following pointers

  • The Slider must provide a way to limit the angle of the slider i.e not full 360 always, meaning it should provide me a way to select 60 Degree Circular sliders too.
  • Must be rotatable so that it can be set in any direction.
  • Must provide a way for customizations like color, size etc.
  • Must provide all features of a regular rectangular slider.

The three main things that i had to take care when designing the algorithm was that

  1. Drawing the Ring in Opengl
  2. Angle Calculation to fill in the slider as you touch it
  3. Collision detection, as the existing collision detection code was for rectangular widgets

Let start with each part one at a time

1. OPENGL Ring

I was wondering how I can make a ring like structure in opengl, I thought maybe make two circles one of the outer radius another of the inner radius, with the difference between the two being the thickness of the ring. But a bit of browsing on the internet revealed that there is a OPENGL call to draw the exact same thing, and its very customizable too.

 void gluPartialDisk( GLUquadric* quad,
			       GLdouble	inner,
			       GLdouble	outer,
			       GLint slices,
			       GLint loops,
			       GLdouble	start,
			       GLdouble	sweep )

you can read more about gluPartialDisk here. This really simplified the drawing part of the slider.

2. Angle Calculation

Slider Angle Calculation

Slider Angle Calculation

The angle calculation was easy once i found out how to do it,

  • First Make a vector of the one edge of the slider w.r.t the center.
  • Now as the touch is moved on the slider, take one more vector at the current touch location w.r.t to the center
  • Find the angle between the two vector, this is the fill angle for the slider

3.  Collision Detection

Next problem was collision detection algorithm, since the sliders where in a arc form, the collision detection has two boundary conditions and which itself formed the algorithm for collision detection.

  • The distance of touch from the center of the slider must be lesser than the outer radius and lesser than the outer radius-thickness of the slider
  • The Angle created by the current touch location and the vertical axis must be greater than the angle of the start edge of the slider with the vertical axis and lesser than the angle of the end edge of the slider with the vertical axis

Using the above two conditions I wrote the following code which works very nicely

def collide_point(self, x, y):
    #A algorithm to find the whether a touch is within a semi ring
    point_dist = Vector(self.pos).distance((x, y))
    point_angle = Vector(self.radius_line).angle((x - self.pos[0], y - self.pos[1]))
    if point_angle < 0:
       point_angle=360+point_angle
    if point_angle <= self.sweep_angle and point_angle >=0:
       return  point_dist<= self.radius and point_dist > self.radius-self.thickness

Here is a screenshot of the circular sliders, this was taken by my other mentor Thomas, see how they can be stacked over one another and still the collision detection algorithm can work flawlessly

Stacked Circular Sliders

Stacked Circular Sliders

You can read more about the widget here. In the next blog i’ll be posting about Color Wheel, and that involves alot of trigometric equations :) .





A Theory behind Circular Menu

22 06 2009

At first I thought it would be very simple to code a circular menu, as there was already a scatterwidget in pymt which would rotate on a gesture, I can shut of translation and scaling gesture and make a simple circular menu out of it, but then how does a rotation work ? well you need two points to rotate a scatter widget to find the angle of rotation, so using a scatterwidget was ruled out.

So i started with a sketch book, and started deciding how my Circular Menu should work and look. Here are the few pointers I noted down

  • One finger should be able to rotate the widget, like a swipe
  • How do i calculate the angle of rotation when only finger is used ?!!
  • Should be a Circular Design which is obvious :P
  • The icons/buttons should be arranged circularly around the circumference of the widget.

I started  with the angle calculation. I thought of the following logical algorithm

  1. Save first touch point coordinates
  2. For every movement of the same touch point location, find the angle between the first touch point coordinates and the current touch point coordinates with respect to the center of the circle.
  3. Add the angle calculated to the total rotation of the widget when you finish the gesture i.e when you remove your touch.
  4. Use this total angle calculated to rotate the widget using openGL transformations.
Angle Calculation

Angle Calculation

The angle calculation can be easily done using pymt’s awesome Vector class, tito showed me how I can use it to do alot of vector calculations which really simplies most of the things i want to do like angle calculation, before using Vector class I intially wrote my own angle calculation code using trignometrics :P .

Next problem was to arrange the widgets on the circumference. I remembered from my Highschool Trigonometrics that we can find the point on the circumference of a circle given a angle theta using the formula

P(x,y) = (radius*cos(theta), radius*sin(theta))

This worked brilliantly. It arranged the widgets around the Widget in a circular manner, and this would rotate the icons along with the widget. Here is the screenshot of the final widget and here is the final code.

Circular Menu

Circular Menu





What I’ve been uptop

22 06 2009

Lately I’ve been coding a lot, especially on NUIPaint, but I couldn’t blog because of my Final exams and plus blogging uses up too much of precious development time :P , so I tend to blog less. Anyways in the next few days I’ll be posting  about all the theory behind the new widgets that I developed. The Opengl Algorithms, Trigonometric equations and other stuff. Hopefully this will be useful for somebody, as i dint now find much information and had to code on my own. Following are the widgets of which I’ll be blogging about

  • A Circular Menu
  • A Circular Slider
  • A Quarter Circle Color Picker




NUIPaint Update 1

1 06 2009

So Last three days i’ve been working continously on the GSOC project. I Manage to achieve the following

  • A toolbar with dummy icons in place, but two of the buttons zoom and brush are activated. They are used to switch between the two modes of the canvas Widget
  • A Canvas Widgets, its a FBO based workarea/canvas, it has two modes of operation
    • Zoom mode: In this mode the canvas can be altered using multitouch gestures, it can be translated,rotated and scaled
    • Paint Mode: In this mode any touch on the canvas draws a circle using a default brush and color
  • I also developed a file browser widget, its a  file browser with kinetics and it generates appropriate icons for each file type. There is alot to improve on this widget, like provide filters, several buttons, and generate events on launch and exit  as suggested by my mentor Matheiu.

I’m getting the hang of this, several new kinds of widget requirements are coming into picture which is great :) . I have attached a screenshot of the work in progress

NUIPaint Screenshot 1

NUIPaint Screenshot 1





Let the Coding Begin!!

30 05 2009

Finally i’m over with the college project and now on i can concentrate on GSOCing, i’m relieved no more h/w work is involved. There are several updates which occured during the long absense of my posting on this blog.

  1. Mathieu Virbel a.k.a tito is also my GSOC mentor wohoo. So i got the two best Computer Graphics mentors a student can get. Now i call myself double lucky ;) . I have already know them very well, and i’ve been learning under them for nearly 3+ months now. Wohhoo
  2. Got a free gift from Google, a years subscriptions to ACM digital library. thank you google.

As for coding, The source code is here http://code.google.com/p/nuipaint/source/checkout, i’ll keep this blog updated regarding my endevours as usual now that my college project is done :P