TheMagician
Member
I'm coding a UI module where I'm running into a problem with a circular dependency between the scrollfield and the scrollbar.
The scrollfield struct has a method set_y() with which you can set the y-offset of the content of the scrollfield.
The scrollbar struct has a method set_value() with which you can set the slider's offset in percent (0 to 1) along the scrollbar.
When the y-offset of the scrollfield is changed via set_y() (e.g. by scrolling the mouse wheel while hovering over the scrollfield) I call the scrollbar's set_value() method from within the scrollfield's set_y() method to update the scrollbar accordingly.
When the value of the scrollbar is changed via set_value() (e.g. by using the mouse to drag the slider up and down) I call the scrollfield's set_y() method from within the scrollbar's set_value() method to update the scrollfield offset accordingly.
This creates an endless loop where the two methods keep calling each other as soon as I either call set_y() on the scrollfield or set_value() on the scrollbar.
What would be an (elegant) solution to this problem?
Of course I could say that you just always have to call both functions separately, one after the other, if you want to update the scrollfield or the scrollbar. However, it feels to me like the scrollfield should automatically take care of its scrollbar and vice-versa - even if you only change one of the two.
The scrollfield struct has a method set_y() with which you can set the y-offset of the content of the scrollfield.
The scrollbar struct has a method set_value() with which you can set the slider's offset in percent (0 to 1) along the scrollbar.
When the y-offset of the scrollfield is changed via set_y() (e.g. by scrolling the mouse wheel while hovering over the scrollfield) I call the scrollbar's set_value() method from within the scrollfield's set_y() method to update the scrollbar accordingly.
When the value of the scrollbar is changed via set_value() (e.g. by using the mouse to drag the slider up and down) I call the scrollfield's set_y() method from within the scrollbar's set_value() method to update the scrollfield offset accordingly.
This creates an endless loop where the two methods keep calling each other as soon as I either call set_y() on the scrollfield or set_value() on the scrollbar.
What would be an (elegant) solution to this problem?
Of course I could say that you just always have to call both functions separately, one after the other, if you want to update the scrollfield or the scrollbar. However, it feels to me like the scrollfield should automatically take care of its scrollbar and vice-versa - even if you only change one of the two.