find column index when columns are of different widthes

Started by
3 comments, last by Alberth 6 years, 3 months ago

hello

 

let's say i have a table with columns, each are subdivided into 4 subcolumns.

i can get which subcolumn (the 1st, the 2nd, the 3rd...) is hovered by the mouse by doing :

int subcol = ((int)mouse.x / SUBCOL_WIDTH * SUBCOL_WIDTH ) % 4

however, if i have, let's say, the 4th column bigger than the 3 others. How do i modify this line of code to handle that ?

Of course i could do it with conditions, four IFs checking the mouse position. But i'm looking for an elegant solution. Any ideas ?

 

 

Advertisement

If the subcolumns are of different widths and there is no clean relationship between the widths such as 'subcolumn 4 is twice as wide as any other column' then you'd probably have to either do it with conditions or keep a list of where the columns start and end. Also would you mind explaining why the fourth subcolumn is wider than the others?

You iterate through all columns untill n column and you add all widths, this gives you exact posittion you can handle

If you use accumulated width (length of all columns upto the Nth one), you can use bisection to find the column quickly. For mouse interaction, it may not be needed though, as mouse is slow by nature.

This topic is closed to new replies.

Advertisement