[Tutorial] Clickable textdraws
#1

Just a quick tutorial on how to interact with textdraws. Questions can go in the comments section.

Note:This tutorial assumes you already know how to create textdraws

Getting started
The possibility of clicking textdraws is introduced at 0.3e RC4. That means you'll need SA-MP 0.3e RC4 or higher to get this working (both client and server).

In this tutorial we'll be recreating this simple teleport menu I made with Zamaroht's textdraw editor:
[ame]http://www.youtube.com/watch?v=aqVS-unQO6M[/ame]
I'm not going to tell you how to create these textdraws, that's up to you.


The script
We'll start by initializing our textdraws and setting them up. This is just a copy n' paste from the textdraw editor, nothing to explain here.
pawn Код:
#include <a_samp>
new Text:teleportBox;
new Text:teleportLS;
new Text:teleportSF;
new Text:teleportLV;
public OnFilterScriptInit()
{
    print("\n--------------------------------------");
    print(" Textdraw teleporting - tut for showing\n  how textdraw selecting works");
    print("--------------------------------------\n");
   
    teleportBox = TextDrawCreate(320.000000, 143.000000, "~n~Teleport menu~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~~n~_");
    TextDrawAlignment(teleportBox, 2);
    TextDrawBackgroundColor(teleportBox, 255);
    TextDrawFont(teleportBox, 2);
    TextDrawLetterSize(teleportBox, 0.500000, 1.000000);
    TextDrawColor(teleportBox, -1);
    TextDrawSetOutline(teleportBox, 0);
    TextDrawSetProportional(teleportBox, 1);
    TextDrawSetShadow(teleportBox, 1);
    TextDrawUseBox(teleportBox, 1);
    TextDrawBoxColor(teleportBox, 255);
    TextDrawTextSize(teleportBox, 45.000000, 115.000000);

    teleportLS = TextDrawCreate(320.000000, 180.000000, "Los Santos~n~Airport");
    TextDrawAlignment(teleportLS, 2);
    TextDrawBackgroundColor(teleportLS, 255);
    TextDrawFont(teleportLS, 2);
    TextDrawLetterSize(teleportLS, 0.260000, 0.799999);
    TextDrawColor(teleportLS, -1);
    TextDrawSetOutline(teleportLS, 0);
    TextDrawSetProportional(teleportLS, 1);
    TextDrawSetShadow(teleportLS, 1);

    teleportSF = TextDrawCreate(320.000000, 205.000000, "San Fierro~n~Airport");
    TextDrawAlignment(teleportSF, 2);
    TextDrawBackgroundColor(teleportSF, 255);
    TextDrawFont(teleportSF, 2);
    TextDrawLetterSize(teleportSF, 0.260000, 0.799999);
    TextDrawColor(teleportSF, -1);
    TextDrawSetOutline(teleportSF, 0);
    TextDrawSetProportional(teleportSF, 1);
    TextDrawSetShadow(teleportSF, 1);

    teleportLV = TextDrawCreate(320.000000, 230.000000, "Las Venturas~n~Airport");
    TextDrawAlignment(teleportLV, 2);
    TextDrawBackgroundColor(teleportLV, 255);
    TextDrawFont(teleportLV, 2);
    TextDrawLetterSize(teleportLV, 0.260000, 0.799999);
    TextDrawColor(teleportLV, -1);
    TextDrawSetOutline(teleportLV, 0);
    TextDrawSetProportional(teleportLV, 1);
    TextDrawSetShadow(teleportLV, 1);
    return 1;
}
In order to be able to make textdraws clickable, we'll need to use TextDrawSetSelectAble. This code will go at the bottom of OnGameModeInit (Check the source script if you've got problems with it):
pawn Код:
TextDrawSetSelectable(teleportBox, false);
TextDrawSetSelectable(teleportLS, true);
TextDrawSetSelectable(teleportSF, true);
TextDrawSetSelectable(teleportLV, true);
I'm using a filterscript for this tutorial, so I need to destroy the textdraws when I'm unloading the script.
pawn Код:
public OnFilterScriptExit()
{
    TextDrawDestroy(teleportBox);
    TextDrawDestroy(teleportLS);
    TextDrawDestroy(teleportSF);
    TextDrawDestroy(teleportLV);
    return 1;
}
I'm using a command to fetch the teleport menu. I know I'm using strcmp, but I'm not going to use ZCMD for just one simple command.

In this command we'll be showing the textdraws to the player and allowing him to select the textdraws:
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (!strcmp("/teleport", cmdtext, true))
    {
        TextDrawShowForPlayer(playerid, teleportBox); // Show the box
        TextDrawShowForPlayer(playerid, teleportLS);  // Show the LS option
        TextDrawShowForPlayer(playerid, teleportSF);  // Show the SF option
        TextDrawShowForPlayer(playerid, teleportLV);  // Show the LV option

        SelectTextDraw(playerid, 0xA3B4C5FF); // Allow the player to select textdraws.
            // The second parameter is the colour to which the textdraw turns when you hover over it.
            //  You can use stuff like COLOR_RED as well, that's your choice.
        return 1;
    }
    return 0;
}
I hope you've understood everything so far. If not, just leave a comment.

The player is now able to select textdraws (Or at least, the textdraws we've allowed him to select using TextDrawSetSelectable). After he has selected and clicked on a textdraw we need to process his 'click'.

I'll post the basic code:
pawn Код:
public OnPlayerClickTextDraw(playerid, Text:clickedid)
{
    if(clickedid == teleportLS) // If the player clicked on the teleportLS textdraw, teleport him there!
    {
        SetPlayerPos(playerid, 1630.2030,-2328.9673,13.5469);
        SetPlayerFacingAngle(playerid, 0.9392);
    }
    else if(clickedid == teleportSF) // Same as above, but for SF
    {
        SetPlayerPos(playerid, -1424.6083, -290.9622, 14.1484);
        SetPlayerFacingAngle(playerid, 134.9570);
    }
    else if(clickedid == teleportLV) // Same, but for LV
    {
        SetPlayerPos(playerid, 1688.7990, 1447.7753, 10.7675);
        SetPlayerFacingAngle(playerid, 267.3902);
    }

                     // Hide the textdraws after the player has teleport himself.
    TextDrawHideForPlayer(playerid, teleportBox);
    TextDrawHideForPlayer(playerid, teleportLS);
    TextDrawHideForPlayer(playerid, teleportSF);
    TextDrawHideForPlayer(playerid, teleportLV);

    CancelSelectTextDraw(playerid); // Everything worked out perfectly, now stop the player to be able to select shizzle.
    return 1;
}
'Done', you might say. However, there's a glitch when using CancelSelectTextDraw: It calls OnPlayerClickTextDraw. This code will result in an eternal loop. Click here for more information on this glitch.

We need to fix this code. CancelSelectTextDraw calls OnPlayerClickTextDraw with INVALID_TEXT_DRAW, so the workaround is easy:
pawn Код:
public OnPlayerClickTextDraw(playerid, Text:clickedid)
{
    if(_:clickedid != INVALID_TEXT_DRAW) // If the player clicked a valid textdraw, continue with the coding. (_:var removes the Text: tag, to avoid tag mismatch)
    {
        if(clickedid == teleportLS)
        {
            SetPlayerPos(playerid, 1630.2030,-2328.9673,13.5469);
            SetPlayerFacingAngle(playerid, 0.9392);
        }
        else if(clickedid == teleportSF)
        {
            SetPlayerPos(playerid, -1424.6083, -290.9622, 14.1484);
            SetPlayerFacingAngle(playerid, 134.9570);
        }
        else if(clickedid == teleportLV)
        {
            SetPlayerPos(playerid, 1688.7990, 1447.7753, 10.7675);
            SetPlayerFacingAngle(playerid, 267.3902);
        }

        TextDrawHideForPlayer(playerid, teleportBox);
        TextDrawHideForPlayer(playerid, teleportLS);
        TextDrawHideForPlayer(playerid, teleportSF);
        TextDrawHideForPlayer(playerid, teleportLV);
        CancelSelectTextDraw(playerid); // This will indeed call OnPlayerClickTextDraw again, but with an ID of 65535. The code above stops it from resulting in an eternal loop.
    }
    return 1;
}
I hope you've learned something from this tutorial. If you've still got questions, please post them in this thread (Do not PM me).

A link to the complete script can be found here on Pastebin.

Good luck!
Reply


Messages In This Thread
Clickable textdraws - by Hiddos - 24.03.2012, 11:40
Re: Clickable textdraws - by FireCat - 24.03.2012, 11:41
Re: Clickable textdraws - by Hiddos - 24.03.2012, 11:48
Re: Clickable textdraws - by T0pAz - 24.03.2012, 11:49
Re: Clickable textdraws - by vassilis - 24.03.2012, 12:01
Re: Clickable textdraws - by Hiddos - 24.03.2012, 12:03
Re: Clickable textdraws - by vassilis - 24.03.2012, 12:06
Re: Clickable textdraws - by Max_Coldheart - 24.03.2012, 12:07
Re: Clickable textdraws - by T0pAz - 24.03.2012, 12:12
Re: Clickable textdraws - by vassilis - 24.03.2012, 12:14
Re: Clickable textdraws - by Hiddos - 24.03.2012, 12:30
AW: Clickable textdraws - by AttachObject(Oo) - 24.03.2012, 19:20
Re: Clickable textdraws - by DarkScripter - 24.03.2012, 19:27
Re: Clickable textdraws - by Snipa - 24.03.2012, 19:41
Re: Clickable textdraws - by Michael@Belgium - 26.03.2012, 17:10
Re: Clickable textdraws - by Nicholas. - 27.03.2012, 14:48
Re: Clickable textdraws - by robintjeh - 27.03.2012, 17:18
Re: Clickable textdraws - by System64 - 27.03.2012, 19:50
Re: Clickable textdraws - by davve95 - 28.03.2012, 18:00
Re: Clickable textdraws - by Niko_boy - 30.03.2012, 17:59
Re: Clickable textdraws - by iNorton - 30.03.2012, 19:27
Re: Clickable textdraws - by Aleluja - 03.04.2012, 14:47
Re: Clickable textdraws - by |-Ruben-| - 07.04.2012, 17:27
Re: Clickable textdraws - by Guest9328472398472 - 15.04.2012, 03:56
Re: Clickable textdraws - by Dripac - 11.05.2012, 16:59
Re: Clickable textdraws - by Hiddos - 11.05.2012, 20:31
Re: Clickable textdraws - by $$inSane - 14.05.2012, 11:10
Re: Clickable textdraws - by DaRealShazz - 25.05.2012, 21:02
Re: Clickable textdraws - by Hiddos - 26.05.2012, 09:02
Re: Clickable textdraws - by SnG.Scot_MisCuDI - 29.05.2012, 05:25
Re: Clickable textdraws - by k3nw00d_kri5ty - 29.05.2012, 12:27
Re: Clickable textdraws - by necrobg3 - 02.06.2012, 19:50
Re: Clickable textdraws - by SkL_MD - 02.06.2012, 19:55
Re: Clickable textdraws - by [MM]18240[FMB] - 18.07.2012, 03:38
Re: Clickable textdraws - by Dark Killer - 07.12.2012, 11:47
Re: Clickable textdraws - by Maraudeur - 09.12.2012, 21:10
Re: Clickable textdraws - by Extasy4 - 09.08.2013, 15:15
Re: Clickable textdraws - by PT - 09.08.2013, 16:06
Re: Clickable textdraws - by *Reserv - 10.08.2013, 19:50
Re: Clickable textdraws - by Hargrave - 16.08.2013, 16:22
Re: Clickable textdraws - by (ETR)Geto244 - 06.02.2014, 13:36
Re: Clickable textdraws - by TheFlyer - 06.02.2014, 20:25
Re: Clickable textdraws - by Sanady - 26.04.2014, 09:42
Re: Clickable textdraws - by tboysamp - 26.04.2014, 17:50
Re: Clickable textdraws - by Hiddos - 30.04.2014, 11:31
Re: Clickable textdraws - by Lumixa - 02.08.2014, 09:36
Re: Clickable textdraws - by Steel_ - 02.08.2014, 09:37
Re: Clickable textdraws - by Lumixa - 02.08.2014, 09:49
Re: Clickable textdraws - by Venice - 05.08.2014, 08:17
Re: Clickable textdraws - by Nightangle - 05.08.2014, 11:55
Re: Clickable textdraws - by Ryz - 24.10.2014, 12:10
Re: Clickable textdraws - by M0HAMMAD - 12.03.2015, 14:35
Respuesta: Clickable textdraws - by [DOG]irinel1996 - 03.07.2015, 00:54
Re: Clickable textdraws - by ItzRbj - 03.07.2015, 01:35
Re: Clickable textdraws - by NotYoMama - 10.07.2015, 16:33
Re: Clickable textdraws - by xXRevolverXx - 13.04.2016, 13:30
Re: Clickable textdraws - by minhducc - 24.07.2016, 20:42
Re: Clickable textdraws - by ivndosos - 16.02.2018, 18:00

Forum Jump:


Users browsing this thread: 1 Guest(s)