[ RESOLVED ] GUI Button - Help

D

Diveyoc

Guest
Please help if you can offer some advice.
I can't figure out a way to do this. Maybe using the (switch) command ??
The display_mouse_get commands work fine, I tested them with some hover code. So there's no issue in using the point_in_rectangle code.

I have 4 buttons in total. Each button has an index of 0 (gray) and 1 (green).
All buttons could start off gray and only turn green if Mouse Left clicked.
However, I only want 1 button green at any time because I need to write code for this on/off action.
Therefore, if 1 button is on, the other 3 need to be off.

I need it so that later I can write other commands like:
if tier1_button = 1
{
// Code //
}

Code:
//====  TIER BUTTON CONTROL  ====\\

tier1_button = 0;      //  0 is OFF
tier2_button = 0;      //  0 is OFF

draw_sprite(spr_tier1_button,0,1412,12);
if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1404,32,1473,60)
    && mouse_check_button_pressed(mb_left)
    {
    draw_sprite(spr_tier1_button,1,1412,12);
    tier1_button = 1;
    }
    
draw_sprite(spr_tier2_button,0,1500,12);
if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1492,32,1560,60)
    && mouse_check_button_pressed(mb_left)
    {
    draw_sprite(spr_tier2_button,1,1500,12);
    tier2_button = 1;
    }
 
D

Diveyoc

Guest
I was thinking too, If I could create a var that just changed the sprite index between 1 and 0, it would cut back on writing the draw_sprite code multiple times.
 
D

Diveyoc

Guest
I've added hovering = 0 in the create event.

Seems like I'm making progress with this code. The buttons flash green but don't stay green.

Code:
draw_sprite(spr_tier1_button,0,1412,12);
draw_sprite(spr_tier2_button,0,1500,12);

if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1404,32,1473,60) 
        {
        hovering = 1;
        }
       
if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1492,32,1560,60)
        {
        hovering = 2;
        }

if mouse_check_button_pressed(mb_left) && hovering > 0
    {
    switch hovering
        {
        case 1: { draw_sprite(spr_tier1_button,1,1412,12); }
        case 2: { draw_sprite(spr_tier2_button,1,1500,12); }
        } 
    }
 

Zerb Games

Member
Here's a set of scripts

/// test(expression,valtrue,valfalse)

if (argument0)
return argument1
return argument2

/// mouse_box(x,y,w,h)

return (mouse_x>=argument0 && mouse_y>=argument1 && mouse_x<argument0+argument2 && mouse_y<argument1+argument3 /*&& window_busy="" && popup_ani_type=""*/)

/// mouse_box_relative(x,y,w,h)

return (mouse_x>=view_xview+argument0 && mouse_y>=view_yview+argument1 && mouse_x<view_xview+argument0+argument2 && mouse_y<view_yview+argument1+argument3)

/// draw_box(x,y,w,h,outline[,color,alpha])

var xx,yy,w,h,outline,oldcolor,oldalpha;
xx=argument[0]
yy=argument[1]
w=argument[2]
h=argument[3]
outline=argument[4]

if (argument_count>5) {
oldcolor=draw_get_color()
oldalpha=draw_get_alpha()
draw_set_color(argument[5])
draw_set_alpha(oldalpha*argument[6])
}

draw_rectangle(xx,yy,xx+w,yy+h,outline)

if (argument_count>5) {
draw_set_color(oldcolor)
draw_set_alpha(oldalpha)
}

/// draw_drop_shadow(x,y,width,height)

var xx,yy,w,h;
xx=argument0
yy=argument1
w=argument2
h=argument3

draw_gradient(xx+w,yy,shadow_size,shadow_size,c_black,0,0,0,shadow_alpha)
draw_gradient(xx+w,yy+shadow_size,shadow_size,h-shadow_size,c_black,shadow_alpha,0,0,shadow_alpha)
draw_gradient(xx+w,yy+h,shadow_size,shadow_size,c_black,shadow_alpha,0,0,0)
draw_gradient(xx+shadow_size,yy+h,w-shadow_size,shadow_size,c_black,shadow_alpha,shadow_alpha,0,0)
draw_gradient(xx,yy+h,shadow_size,shadow_size,c_black,0,shadow_alpha,0,0)

/// draw_gradient(x,y,width,height,color,alphalefttop,alpharighttop,alpharightbottom,alphaleftbottom)

var xx,yy,w,h,color,alphalefttop,alpharighttop,alpharightbot,alphaleftbot;
var alpha;
xx=argument0
yy=argument1
w=argument2
h=argument3
color=argument4
alphalefttop=argument5
alpharighttop=argument6
alpharightbot=argument7
alphaleftbot=argument8
alpha=draw_get_alpha()

draw_primitive_begin(pr_trianglestrip)

draw_vertex_color(xx,yy,color,alphalefttop*alpha)
draw_vertex_color(xx+w,yy,color,alpharighttop*alpha)
draw_vertex_color(xx,yy+h,color,alphaleftbot*alpha)
draw_vertex_color(xx+w,yy+h,color,alpharightbot*alpha)

draw_primitive_end()

so a good hover example and stuffs would be:

Code:
draw_drop_shadow(16,16,32,8)

if mouse_box(16,16,32,8) {
draw_box(16,16,32,8,false,test(mouse_check_button(mb_left)=1,c_lime,c_green),1)
if mouse_check_button(mb_left) {scr_action_restart();}
} else {
draw_box(16,16,32,8,false,c_grey,1)
}
The great thing about this is the fact that you can use the GUI Draw Event for all of this.

Hopefully this helps with the workflow!
 
Last edited:
D

Diveyoc

Guest
That seems like 5x the amount of code I already have. I have button sprites drawn, so I don't need to draw them with code.
I think what I have now would work, I just need to figure out how to lock in the image_index 1.
Right now when I left click the button, it just flashes green instead of staying green.

Code:
t1_index = 0;
t2_index = 0;

if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1404,32,1473,60)
    && mouse_check_button_pressed(mb_left)
        {
        t1_index = 1;
        t2_index = 0;
        }
        
if point_in_rectangle(display_mouse_get_x(), display_mouse_get_y(),1492,32,1560,60)
    && mouse_check_button_pressed(mb_left)
        {
        t1_index = 0;
        t2_index = 1;
        }

draw_sprite(spr_tier1_button,t1_index,1412,12);
draw_sprite(spr_tier2_button,t2_index,1500,12);
 

Mick

Member
Move:
Code:
t1_index = 0;
t2_index = 0;
to the create event. Now those variables will be reset every step and you don't want that.
 
C

Chubb1337

Guest
What happens is that you reset the t1_index and t2_index, ans then only check for a pressed button (so basicly, only for the moment you pressed the mouse down, not holding). So depending on what you want:

Want it to stay green until mouse button released? Use: mouse_check_button ()

Want it to stay green until the other button is pressed? Remove the "t1_index = 0; t2_index = 0; " from the start of the code.

EDIT: Mick was just a tad bit faster, but the solution still holds.
 
D

Diveyoc

Guest
Ahh thanks. I overlooked that. This works how I want it now.
 
Top