draw_button_plus

Publicado por dixon, Septiembre 07, 2021, 01:57:11 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

dixon

Nombre del autor : Reyes Dávila (Dixon CGM).
Breve descripción de su función : Sirve para crear varios botones de manera rápida y sin complicaciones, incluyendo las funciones.
Versión GM utilizada : Game maker 4.3 y Game Maker studio 1.4
Versión del script: 1

Vista previa html5:
Aquí podrán ver en funcionamiento el script.
https://dixonysusjuegoseducativos.000webhostapp.com/_practica_game/botones/ver_01/index.html
Recursos adjuntos:
Aquí podrán descargar el editable para game maker studio.
https://www.mediafire.com/file/65vh2fqnjguz5nf/botones.gmz/file

Código del script
//draw_button_plus
//Colocar en evento Create: draw_button_plus_create=1;
//Colocar en evento Draw: draw_button_plus("ver,5,5,ocultar,7,5");
//Ejemplo de uso: draw_button_plus("Abrir,5,5,Cerrar,7,5");


//Creando variables
if(draw_button_plus_create=1)
{
draw_button_plus_create=0;

//Estableciendo cuadriculas
cuadricula_w=room_width/20;
cuadricula_h=room_height/20;

boton_fuente=string_height("H");
cantidad_comas=string_count(",",argument0);
cantidad_botones=floor((1+cantidad_comas)/3);

string_ram=argument0;
largo_string=string_length(string_ram);
for(i=0;i<cantidad_botones;i+=1)
{
posx=string_pos(",",string_ram);
boton[i,0]=string_copy(string_ram,1,posx-1);
string_ram=string_copy(string_ram,posx+1,largo_string);
largo_string=string_length(string_ram);

posx=string_pos(",",string_ram);
boton_ram=string_copy(string_ram,1,posx-1);
boton[i,1]=real(boton_ram);
string_ram=string_copy(string_ram,posx+1,largo_string);
largo_string=string_length(string_ram);

c1=(string_count(",",string_ram));
if(c1)
{
    posx=string_pos(",",string_ram)
    boton_ram=string_copy(string_ram,1,posx-1);
}
else
{
    boton_ram=string_ram;
}
boton[i,2]=real(boton_ram);
string_ram=string_copy(string_ram,posx+1,largo_string);
largo_string=string_length(string_ram);

ancho_boton=string_width(boton[i,0]);
boton[i,3]=ancho_boton*1.5/cuadricula_w;//Alto del boton por cuadricula.
boton[i,4]=2;
}

//Ajustando medidas del boton a cuadricula
for(i=0;i<cantidad_botones;i+=1)
{
boton[i,1]=boton[i,1]*cuadricula_w;
boton[i,2]=boton[i,2]*cuadricula_h;
boton[i,3]=boton[i,3]*cuadricula_w;
boton[i,4]=boton[i,4]*cuadricula_h;
}

}//Fin if argument0="create";








////////////////////////Dibujando botones...
if(draw_button_plus_create=0)
{
//Dibujando botones
funcion_boton="";
for(i=0;i<cantidad_botones;i+=1)
{
for(i2=0;i2<=4;i2+=1)
{
boton_data[i2]=boton[i,i2];
}
x1=boton_data[1];y1=boton_data[2];
x2=boton_data[1]+boton_data[3];y2=boton_data[2]+boton_data[4];
boton_up=0;
c1=mouse_x>x1;c2=mouse_x<x2;
c3=mouse_y>y1;c4=mouse_y<y2;
c5=mouse_check_button(mb_left);
c6=(funcion_boton="");
if(c1 and c2 and c3 and c4 and c5 and c6)
{
funcion_boton=boton_data[0];
boton_up=1;
}

draw_button(x1,y1,x2,y2,boton_up);//Game maker 4

game_maker_studio=''
draw_set_colour(c_ltgray);//Game maker studio
draw_button(x1,y1,x2,y2,!boton_up);//Game maker studio
draw_set_color(c_black);//Game maker studio
//'

if(c1 and c2 and c3 and c4 and (c5=0))
{
draw_line(x1,y1,x2,y1);
draw_line(x1,y1,x1,y2);
draw_line(x2,y1,x2,y2);
draw_line(x1,y2,x2,y2);
}
boton_data[5]=string_width(boton_data[0]);//Ancho del texto.
x1=boton_data[1]+boton_data[3]/2-boton_data[5]/2;y1=boton_data[2]+boton_data[4]/2-boton_fuente/2;
draw_text(x1,y1,boton_data[0]);
}
}//Fin if argument0=draw


Modo de uso
1. Primero copian el código del script que les he dejado.
2.Crean un script en game maker y lo llaman "draw_button_plus".


3. Dentro de ese script pegan el código que copiaron peviamente.
4. En el evento CREATE de un objeto deberán colocar:
draw_button_plus_create=1;

5. Luego en el evento DRAW deberán colocar:
//Esto es un ejemplo:
draw_button_plus("Ver,1,10,Ocultar,4,10");

6. Finalmente debajo de ese código deberán colocar las funciones de cada botón muy fácilmente de la siguiente manera:
//Funciones de boton
if(funcion_boton="Ver")
{
direction=0;//Aquí va la función deseada
}

if(funcion_boton="Ocultar")
{
direction=1;//Aquí va la función deseada
}


El evento DRAW quedaría de la siguiente manera:
draw_button_plus("Ver,1,10,Ocultar,4,10");
//Funciones de boton
if(funcion_boton="Ver")
{
direction=0;//Aquí va la función deseada
}

if(funcion_boton="Ocultar")
{
direction=1;//Aquí va la función deseada
}


Segunda explicación:
1. Este script recibe argumentos en forma de string, los cuales deberán ser separados por una coma.
2. El primer espacio antes de la primera coma corresponde al nombre del primer botón a dibujar, el segundo argumeto es la posición x del botón y el tercer argumento es la posición y del botón.

3. Si queremos crear tres botones (Abrir, Cerrar y guardar), podemos hacer lo siguiente:
draw_button_plus("Abrir,0,0,Cerrar,3,0,Guardar,6,0");

4. En cuanto a las posiciones x, y estas se expresan en cuadriculas. El script divide la room en cuadriculas dividiendo el ancho y el alto en 20 partes, por tanto si queremos crear un botón en todo el centro de la pantalla podemos escribir:
draw_button_plus("Ver,10,10")


AlexWood


136 Visitantes, 0 Usuarios