styles.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**--------------------------------------------------------------------------**\
  2. =================================
  3. y_styles - Text style handling.
  4. =================================
  5. Description:
  6. Determines how a piece of text should be shown.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI utils include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.1
  43. Changelog:
  44. 25/02/12:
  45. First version.
  46. Functions:
  47. Stock:
  48. -
  49. Inline:
  50. -
  51. Variables:
  52. Global:
  53. -
  54. </remarks>
  55. \**--------------------------------------------------------------------------**/
  56. // Load the styles for the current item. I have decided to completely switch
  57. // over to XML as there are just too many options now to make INI files worth
  58. // the effort.
  59. enum e_STYLE_TYPE (+= 0x10000000) // ALWAYS ADD!
  60. {
  61. e_STYLE_TYPE_GT_0 = 0x10000000, // ALWAYS FIRST!
  62. e_STYLE_TYPE_GT_1,
  63. e_STYLE_TYPE_GT_2,
  64. e_STYLE_TYPE_GT_3,
  65. e_STYLE_TYPE_GT_4,
  66. e_STYLE_TYPE_GT_5,
  67. e_STYLE_TYPE_GT_6,
  68. e_STYLE_TYPE_TD,
  69. e_STYLE_TYPE_3D,
  70. // This is the point after which ~n~ is not used for new lines.
  71. e_STYLE_TYPE_CLIENT,
  72. e_STYLE_TYPE_PLAYER,
  73. e_STYLE_TYPE_OTHER,
  74. e_STYLE_TYPE_DIALOG,
  75. e_STYLE_TYPE_MASK = 0xF0000000,
  76. e_STYLE_TYPE_SHIFT = 28
  77. }
  78. enum E_STYLE_DATA
  79. {
  80. e_STYLE_TYPE:E_STYLE_DATA_TYPE = 0,
  81. E_STYLE_DATA_COLOUR = 0, // SCM colour (RRGGBBA (only 1 A)).
  82. E_STYLE_DATA_STYLE = 0, // GT style (set later).
  83. E_STYLE_DATA_TIME = 1, // GT time.
  84. Style:E_STYLE_DATA_TD_ID = 1, // TD style to show this as.
  85. E_STYLE_DATA_3D_ID = 1 // 3D style to show this as.
  86. }
  87. enum e_3D_FLAGS
  88. {
  89. e_3D_FLAGS_VW = 0x0003FFFF, // Max VWs of 262144 (>500 per player).
  90. e_3D_FLAGS_DD = 0x7FFC0000, // Max draw distance of 8192 (1.3x SA).
  91. e_3D_FLAGS_LOS = 0x80000000,
  92. e_3D_FLAGS_VW_SHIFT = 0,
  93. e_3D_FLAGS_DD_SHIFT = 18
  94. }
  95. enum E_3D_DATA
  96. {
  97. Float:E_3D_DATA_X,
  98. Float:E_3D_DATA_Y,
  99. Float:E_3D_DATA_Z,
  100. E_3D_DATA_ATTACH, // -1 = None, < 500 = player, >= 500 = vehicle + 500.
  101. e_3D_FLAGS:E_3D_DATA_FLAGS
  102. }
  103. #if !defined MAX_STYLES
  104. #define MAX_STYLES (MAX_TEXT_ENTRIES)
  105. #endif
  106. #if !defined MAX_3D_STYLES
  107. #define MAX_3D_STYLES (MAX_STYLES / 4)
  108. #endif
  109. #define STYLES_FORMAT_FILE "YSI/%s_LANG_DATA.yml"
  110. static stock
  111. YSI_g_sStyleData[MAX_STYLES][E_STYLE_DATA],
  112. YSI_g_s3DLabelData[MAX_3D_STYLES][E_3D_DATA],
  113. //YSI_g_s3DBlankData[E_3D_DATA],
  114. YSI_g_sStyleBlankData[E_STYLE_DATA] = {e_STYLE_TYPE_CLIENT | e_STYLE_TYPE:0x0FF0000A, -1},
  115. XML:YSI_g_sXMLRules = NO_XML_FILE,
  116. YSI_g_sCurFile[32] = "\1\0",
  117. YSI_g_sCur3D;
  118. stock _Style_Init(id)
  119. {
  120. YSI_g_sStyleData[id] = YSI_g_sStyleBlankData;
  121. }
  122. forward _Styles_SpecialInit();
  123. stock Styles_GetData(index, style[E_STYLE_DATA], label[E_3D_DATA])
  124. {
  125. if (0 <= index < MAX_STYLES)
  126. {
  127. style = YSI_g_sStyleData[index];
  128. if ((style[E_STYLE_DATA_TYPE] & e_STYLE_TYPE_MASK) == e_STYLE_TYPE_3D)
  129. {
  130. label = YSI_g_s3DLabelData[style[E_STYLE_DATA_3D_ID]];
  131. }
  132. }
  133. else
  134. {
  135. style = YSI_g_sStyleBlankData;
  136. }
  137. }
  138. /**--------------------------------------------------------------------------**\
  139. <summary>Styles_EntryTag</summary>
  140. <returns>
  141. -
  142. </returns>
  143. <remarks>
  144. Called to load "<entry>" tags from XML style files.
  145. </remarks>
  146. \**--------------------------------------------------------------------------**/
  147. forward Styles_EntryTag();
  148. public Styles_EntryTag()
  149. {
  150. P:2("Styles_EntryTag called");
  151. static
  152. name[MAX_XML_ENTRY_NAME],
  153. val[MAX_XML_ENTRY_TEXT],
  154. e_STYLE_TYPE:type = e_STYLE_TYPE_OTHER,
  155. data,
  156. colour,
  157. id,
  158. time;
  159. if (!XML_GetParentValue("name", val))
  160. {
  161. return 0;
  162. }
  163. // "_Text_CheckOwnership" MUST be called before "_Text_LookupName".
  164. if (!_Text_CheckOwnership(YSI_g_sCurFile, val))
  165. {
  166. P:5("Styles_EntryTag: Don't own %s", val);
  167. // This script doesn't own this text group.
  168. return 0;
  169. }
  170. // Got the name of this group that this text entries is in.
  171. while (XML_GetKeyValue(name, val))
  172. {
  173. if (!strcmp(name, "name", true))
  174. {
  175. // Use the parent name to help look this up. Also check if we are
  176. // the owner of this group. Actually, use internal text functions
  177. // to do this given that it will know what file it is currently
  178. // loading and has better knowledge of its own internal structure.
  179. // This means we MUST call a style file parse function from inside
  180. // y_textint.
  181. id = _Text_LookupName(val);
  182. P:5("Styles_EntryTag: %s = %d", val, id);
  183. }
  184. else if (!strcmp(name, "type", true) || !strcmp(name, "style", true))
  185. {
  186. // The searched for strings are very lenient.
  187. if (strfind(val, "Client", true) != -1)
  188. {
  189. type = e_STYLE_TYPE_CLIENT;
  190. }
  191. else if (strfind(val, "Player", true) != -1)
  192. {
  193. type = e_STYLE_TYPE_PLAYER;
  194. }
  195. else if (strfind(val, "Draw", true) != -1)
  196. {
  197. type = e_STYLE_TYPE_TD;
  198. }
  199. else if (strfind(val, "3D", true) != -1)
  200. {
  201. type = e_STYLE_TYPE_3D;
  202. }
  203. else
  204. {
  205. // Better "strval" for short numbers.
  206. new
  207. tmp = (val[0] - '0') + 1;
  208. if (1 <= tmp <= 7)
  209. {
  210. type = e_STYLE_TYPE_GT_0 * e_STYLE_TYPE:tmp;
  211. }
  212. }
  213. }
  214. else if (!strcmp(name, "3d", true))
  215. {
  216. data = strval(val);
  217. type = e_STYLE_TYPE_3D;
  218. }
  219. else if (!strcmp(name, "textdraw", true))
  220. {
  221. type = e_STYLE_TYPE_TD;
  222. if (isnumeric(val))
  223. {
  224. data = strval(val);
  225. }
  226. else
  227. {
  228. data = _:TD_GetNamed(val);
  229. }
  230. }
  231. else if (!strcmp(name, "time", true))
  232. {
  233. time = strval(val);
  234. }
  235. else if (!strcmp(name, "colour", true) || !strcmp(name, "color", true))
  236. {
  237. #if defined _inc_sscanf2 || defined unformat
  238. if (unformat(val, "n", colour))
  239. #else
  240. if (ishex(val)) colour = hexstr(val);
  241. else if (isnumeric(val)) colour = strval(val);
  242. else
  243. #endif
  244. colour = GetColour(val);
  245. }
  246. }
  247. if (0 <= id < MAX_STYLES)
  248. {
  249. //printf("SAVING %d", id);
  250. switch (type)
  251. {
  252. case e_STYLE_TYPE_GT_0, e_STYLE_TYPE_GT_1, e_STYLE_TYPE_GT_2, e_STYLE_TYPE_GT_3, e_STYLE_TYPE_GT_4, e_STYLE_TYPE_GT_5, e_STYLE_TYPE_GT_6:
  253. // This doesn't seem to want to work...
  254. //case e_STYLE_TYPE_GT_0 .. e_STYLE_TYPE_GT_6:
  255. {
  256. YSI_g_sStyleData[id][E_STYLE_DATA_TIME] = time;
  257. // Don't set ANY colours here!
  258. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = type;
  259. }
  260. case e_STYLE_TYPE_TD:
  261. {
  262. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = type;
  263. YSI_g_sStyleData[id][E_STYLE_DATA_TD_ID] = Style:data;
  264. }
  265. default:
  266. {
  267. //printf("SAVING %d", id);
  268. YSI_g_sStyleData[id][E_STYLE_DATA_3D_ID] = -1;
  269. //printf("SAVING %d", id);
  270. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = e_STYLE_TYPE:(colour >>> 4 ) | type;
  271. //printf("SAVING %d", id);
  272. }
  273. /*case e_STYLE_TYPE_PLAYER:
  274. {
  275. }
  276. case e_STYLE_TYPE_TD:
  277. {
  278. }
  279. case e_STYLE_TYPE_3D:
  280. {
  281. }
  282. case e_STYLE_TYPE_OTHER:
  283. {
  284. YSI_g_sStyleData[id] =
  285. }*/
  286. }
  287. }
  288. //printf("SAVING %d", id);
  289. // Return value is not important here.
  290. return 1;
  291. }
  292. forward Styles_3DTag();
  293. public Styles_3DTag()
  294. {
  295. P:2("Styles_EntryTag called");
  296. static
  297. name[MAX_XML_ENTRY_NAME],
  298. val[MAX_XML_ENTRY_TEXT],
  299. #if !defined _inc_sscanf2 && !defined unformat
  300. tmp,
  301. #endif
  302. Float:x,
  303. Float:y,
  304. Float:z,
  305. e_3D_FLAGS:flags;
  306. if (YSI_g_sCur3D == MAX_3D_STYLES)
  307. {
  308. return -1;
  309. }
  310. while (XML_GetKeyValue(name, val))
  311. {
  312. if (!strcmp(name, "x", true))
  313. {
  314. x = floatstr(val);
  315. }
  316. else if (!strcmp(name, "y", true))
  317. {
  318. y = floatstr(val);
  319. }
  320. else if (!strcmp(name, "z", true))
  321. {
  322. z = floatstr(val);
  323. }
  324. else if (!strcmp(name, "pos", true))
  325. {
  326. // A good example of the advantages of sscanf.
  327. #if defined _inc_sscanf2 || defined unformat
  328. unformat(val, "p<,>fff", x, y, z);
  329. #else
  330. x = floatstr(val);
  331. tmp = strfind(val, ",") + 1;
  332. if (tmp)
  333. {
  334. y = floatstr(val[tmp]);
  335. tmp = strfind(val, ",") + 1;
  336. if (tmp)
  337. {
  338. z = floatstr(val[tmp]);
  339. }
  340. }
  341. #endif
  342. }
  343. else if (!strcmp(name, "vw", true) || !strcmp(name, "virtualworld", true))
  344. {
  345. flags = (flags & ~e_3D_FLAGS_VW) | ((e_3D_FLAGS:strval(val)) & e_3D_FLAGS_VW);
  346. }
  347. else if (!strcmp(name, "los", true) || !strcmp(name, "lineofsight", true))
  348. {
  349. if (!strcmp(val, "true", true) || strval(val))
  350. {
  351. flags |= e_3D_FLAGS_LOS;
  352. }
  353. else
  354. {
  355. flags &= ~e_3D_FLAGS_LOS;
  356. }
  357. }
  358. else if (!strcmp(name, "drawdistance", true))
  359. {
  360. flags = (flags & ~e_3D_FLAGS_DD) | ((e_3D_FLAGS:strval(val) << e_3D_FLAGS_DD_SHIFT) & e_3D_FLAGS_DD);
  361. }
  362. }
  363. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_X] = x;
  364. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_Y] = y;
  365. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_Z] = z;
  366. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_FLAGS] = flags;
  367. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_ATTACH] = -1;
  368. return YSI_g_sCur3D++;
  369. }
  370. /**--------------------------------------------------------------------------**\
  371. <summary>Styles_OnScriptInit</summary>
  372. <returns>
  373. -
  374. </returns>
  375. <remarks>
  376. Loads all the style files relevant to this script. Filters by text group in
  377. the XML callback. Loops through all publics, then loops through them all
  378. again to check that the current file hasn't already been loaded. Despite
  379. the filtering, it is done this way to ensure that all relevant text draw
  380. styles are loaded for when we are displaying text.
  381. </remarks>
  382. \**--------------------------------------------------------------------------**/
  383. //hook OnScriptInit()
  384. public _Styles_SpecialInit()
  385. {
  386. if (YSI_g_sXMLRules == NO_XML_FILE)
  387. {
  388. YSI_g_sXMLRules = XML_New();
  389. if (YSI_g_sXMLRules != NO_XML_FILE)
  390. {
  391. // This is copied directly from y_td.
  392. XML_AddHandler(YSI_g_sXMLRules, "color", "TD_LoadColour");
  393. XML_AddHandler(YSI_g_sXMLRules, "colour", "TD_LoadColour");
  394. XML_AddHandler(YSI_g_sXMLRules, "textdraw", "TD_Textdraw");
  395. //XML_AddHandler(YSI_g_sXMLRules, "box", "TD_Box");
  396. //XML_AddHandler(YSI_g_sXMLRules, "background", "TD_Background");
  397. //XML_AddHandler(YSI_g_sXMLRules, "style", "TD_Style");
  398. XML_AddHandler(YSI_g_sXMLRules, "entry", "Styles_EntryTag");
  399. XML_AddHandler(YSI_g_sXMLRules, "3d", "Styles_3DTag");
  400. XML_AddHandler(YSI_g_sXMLRules, "group", NULL);
  401. }
  402. }
  403. YSI_g_sCur3D = 0;
  404. new
  405. idx = 0,
  406. buffer[32],
  407. b2[32],
  408. pos;
  409. // Load the data for ALL the files this script uses (to get textdraw data).
  410. while ((idx = AMX_GetPublicNameSuffix(idx, buffer, _A<@yX_>)))
  411. {
  412. if ((pos = strfind(buffer, "@")) != -1)
  413. {
  414. buffer{pos} = '\0';
  415. if (strcmp(buffer, YSI_g_sCurFile))
  416. {
  417. // Different - check for more matches.
  418. new
  419. i2 = 0;
  420. for ( ; ; )
  421. {
  422. i2 = AMX_GetPublicNameSuffix(i2, b2, _A<@yX_>);
  423. if (i2 == idx)
  424. {
  425. // Unique entry found. Load the file.
  426. strunpack(YSI_g_sCurFile, buffer);
  427. format(b2, sizeof (b2), STYLES_FORMAT_FILE, YSI_g_sCurFile);
  428. XML_Parse(YSI_g_sXMLRules, b2);
  429. break;
  430. }
  431. else if (!strcmp(b2, buffer))
  432. {
  433. break;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. /*stock _Styles_ParseOne(file[])
  441. {
  442. new
  443. fname[64];
  444. format(fname, sizeof (fname), STYLES_LANG_DATA, file);
  445. XML_Parse(YSI_g_sXMLRules, fname);
  446. }*/
  447. /**--------------------------------------------------------------------------**\
  448. <summary>Styles_Reload</summary>
  449. <returns>
  450. -
  451. </returns>
  452. <remarks>
  453. Reloads all the string styles in all scripts. Use sparingly!
  454. </remarks>
  455. \**--------------------------------------------------------------------------**/
  456. stock Styles_Reload()
  457. {
  458. CallRemoteFunction("_Styles_SpecialInit", "");
  459. }