[Tutorial] Using LD_SPAC:white to create selectable, pixel perfect rectangles.
#1

Hi all,

I've been a lurker for a fair few years now, but I think it's time I contributed a little myself.

I've noticed a couple of others have found this trick, but couldn't find any formal explanation of why it works so I thought I'd be the first to document it.

I'll try to keep this as concise as people.


What is LD_SPAC:white and why is it useful?
LD_SPAC:white is a plain, square, white texture. These four attributes have the following implications:
  • Texture: Normal text-based textdraws (and rectangles drawn using usebox=1) measure their height in letterheight; which is a fairly arbitrary scale that is near impossible to get perfect and more frequently than not is displayed differently on different screens. Because LD_SPAC:white is a texture, it uses the normal canvas system and therefore measures its height in pixels** as you would expect - meaning a LD_SPAC:white texture with a width and height of 128 pixels will be exactly 128x128.
  • White: The fact that it is white means that it can be coloured using TextDrawColor without any unintended consequences.
  • Plain, Square: Finally, the fact that it is plain (has no detail) and square means that it scales and stretches perfectly regardless of size - it looks the same at 1x1 as it does at 640x480.
Another useful implication of this is that because textures use the forecolor to paint themselves, and because the hovercolor in SelectTextDraw also uses forecolor, this means that the entire box will be highlighted in hovercolor rather than just the text (assuming anything in the foreground is not selectable).



Notice the highlighted grey box in the centre: although you can't see it in this screenshot, it being hovered over with the mouse.


Code example
Draws a red rectangle 50% the size of the screen starting at (25%, 25%). When player 0 hovers over with the mouse, the entire box will turn green.
Code:
TextDraw:td = TextDrawCreate(640.0 * 0.25, 480.0 * 0.25, "_");
TextDrawTextSize(td, 640.0 * 0.5, 480 * 0.5);
TextDrawColor(td, 0xFF0000FF);
TextDrawFont(td, 4);
TextDrawSetSelectable(td, true);
SelectTextdraw(0, 0x00FF00FF);
** SA-MP uses a 640x480 canvas for textdraws irrespective of screen resolution, so when I say pixel I mean 1 pixel respective to the canvas.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)