MCHLV
Member
Hello,
How would you go in GMS2 about finding the closest button above another one ? (I found other approaches based on data structures or with "up" variable manually set to store the target) but did not find the answer for my case). I gave it a try below, maybe there are better ways...
I have a simple menu system, where all my buttons are children to a oGUI_Button_Par.
Everything is managed by a controller oGUI_Controller_Menu. It moves the selection, store the selected button (selected_button_id) and trigger the action.
So,when I press the up_key, I want my controller to find the button just above. I think I will use the following code, this would be run in the step event of my controller object when up_key is pressed.
Thank you for your time and comments !
M.
How would you go in GMS2 about finding the closest button above another one ? (I found other approaches based on data structures or with "up" variable manually set to store the target) but did not find the answer for my case). I gave it a try below, maybe there are better ways...
I have a simple menu system, where all my buttons are children to a oGUI_Button_Par.
Everything is managed by a controller oGUI_Controller_Menu. It moves the selection, store the selected button (selected_button_id) and trigger the action.
So,when I press the up_key, I want my controller to find the button just above. I think I will use the following code, this would be run in the step event of my controller object when up_key is pressed.
Code:
// 1.check if a button is selected. Select one if not
if ( selected_button_id = noone ) { selected_button_id = instance_nearest(x, y, oGUI_Button_Par); }
if ( selected_button_id = noone ) { exit; }
// 2.find the closest button above by looping in all candidates and finding the best one
// 2.1.setting temp variables to default value
var _top_nearest_button = noone;
var _distance_to_top_nearest_button = display_get_gui_height();
// 2.2.looping in candidates,(=buttons a-other than the selection, b-above, c-closest)
with (oGUI_Button_Par)
{
if ( id != selected_button_id ) // this button is not the selection
{
if ( bbox_bottom < other.bbox_top ) // this button is above me
{
if ( ( point_distance(x,y, other.x, other.y) < _distance_to_top_nearest_button ) // best candidate so far (closer one)
{
_top_nearest_button = id;
_distance_top_nearest_button
}
}
}
}
// 3.do something to _top_nearest_button like giving the focus
selected_button_id = _top_nearest_button ;
with (selected_button_id)
{
// bla bla
}
M.