y_styles.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. \*----------------------------------------------------------------------------*/
  55. // Load the styles for the current item. I have decided to completely switch
  56. // over to XML as there are just too many options now to make INI files worth
  57. // the effort.
  58. #include "y_version"
  59. #include "..\y_xml"
  60. #include "..\y_td"
  61. #include "..\y_colours"
  62. //#include "..\y_hooks"
  63. #include "y_natives"
  64. enum e_STYLE_TYPE (+= 0x10000000) // ALWAYS ADD!
  65. {
  66. e_STYLE_TYPE_GT_0 = 0x10000000, // ALWAYS FIRST!
  67. e_STYLE_TYPE_GT_1,
  68. e_STYLE_TYPE_GT_2,
  69. e_STYLE_TYPE_GT_3,
  70. e_STYLE_TYPE_GT_4,
  71. e_STYLE_TYPE_GT_5,
  72. e_STYLE_TYPE_GT_6,
  73. e_STYLE_TYPE_TD,
  74. e_STYLE_TYPE_3D,
  75. // This is the point after which ~n~ is not used for new lines.
  76. e_STYLE_TYPE_CLIENT,
  77. e_STYLE_TYPE_PLAYER,
  78. e_STYLE_TYPE_OTHER,
  79. e_STYLE_TYPE_DIALOG,
  80. e_STYLE_TYPE_MASK = 0xF0000000,
  81. e_STYLE_TYPE_SHIFT = 28
  82. }
  83. enum E_STYLE_DATA
  84. {
  85. e_STYLE_TYPE:E_STYLE_DATA_TYPE = 0,
  86. E_STYLE_DATA_COLOUR = 0, // SCM colour (RRGGBBA (only 1 A)).
  87. E_STYLE_DATA_STYLE = 0, // GT style (set later).
  88. E_STYLE_DATA_TIME = 1, // GT time.
  89. Style:E_STYLE_DATA_TD_ID = 1, // TD style to show this as.
  90. E_STYLE_DATA_3D_ID = 1 // 3D style to show this as.
  91. }
  92. enum e_3D_FLAGS
  93. {
  94. e_3D_FLAGS_VW = 0x0003FFFF, // Max VWs of 262144 (>500 per player).
  95. e_3D_FLAGS_DD = 0x7FFC0000, // Max draw distance of 8192 (1.3x SA).
  96. e_3D_FLAGS_LOS = 0x80000000,
  97. e_3D_FLAGS_VW_SHIFT = 0,
  98. e_3D_FLAGS_DD_SHIFT = 18
  99. }
  100. enum E_3D_DATA
  101. {
  102. Float:E_3D_DATA_X,
  103. Float:E_3D_DATA_Y,
  104. Float:E_3D_DATA_Z,
  105. E_3D_DATA_ATTACH, // -1 = None, < 500 = player, >= 500 = vehicle + 500.
  106. e_3D_FLAGS:E_3D_DATA_FLAGS
  107. }
  108. #if !defined MAX_STYLES
  109. #define MAX_STYLES (MAX_TEXT_ENTRIES)
  110. #endif
  111. #if !defined MAX_3D_STYLES
  112. #define MAX_3D_STYLES (MAX_STYLES / 4)
  113. #endif
  114. #define STYLES_FORMAT_FILE "YSI/%s_LANG_DATA.yml"
  115. static stock
  116. YSI_g_sStyleData[MAX_STYLES][E_STYLE_DATA],
  117. YSI_g_s3DLabelData[MAX_3D_STYLES][E_3D_DATA],
  118. //YSI_g_s3DBlankData[E_3D_DATA],
  119. YSI_g_sStyleBlankData[E_STYLE_DATA] = {e_STYLE_TYPE_CLIENT | e_STYLE_TYPE:0x0FF0000A, -1},
  120. XML:YSI_g_sXMLRules = NO_XML_FILE,
  121. YSI_g_sCurFile[32] = "\1\0",
  122. YSI_g_sCur3D;
  123. stock _Style_Init(id)
  124. {
  125. YSI_g_sStyleData[id] = YSI_g_sStyleBlankData;
  126. }
  127. forward _Styles_SpecialInit();
  128. stock Styles_GetData(index, style[E_STYLE_DATA], label[E_3D_DATA])
  129. {
  130. if (0 <= index < MAX_STYLES)
  131. {
  132. style = YSI_g_sStyleData[index];
  133. if ((style[E_STYLE_DATA_TYPE] & e_STYLE_TYPE_MASK) == e_STYLE_TYPE_3D)
  134. {
  135. label = YSI_g_s3DLabelData[style[E_STYLE_DATA_3D_ID]];
  136. }
  137. }
  138. else
  139. {
  140. style = YSI_g_sStyleBlankData;
  141. }
  142. }
  143. /*----------------------------------------------------------------------------*\
  144. Function:
  145. Styles_EntryTag
  146. Params:
  147. -
  148. Return:
  149. -
  150. Notes:
  151. Called to load "<entry>" tags from XML style files.
  152. \*----------------------------------------------------------------------------*/
  153. forward Styles_EntryTag();
  154. public Styles_EntryTag()
  155. {
  156. P:2("Styles_EntryTag called");
  157. static
  158. name[MAX_XML_ENTRY_NAME],
  159. val[MAX_XML_ENTRY_TEXT],
  160. e_STYLE_TYPE:type = e_STYLE_TYPE_OTHER,
  161. data,
  162. colour,
  163. id,
  164. time;
  165. if (!XML_GetParentValue("name", val))
  166. {
  167. return 0;
  168. }
  169. // "_Text_CheckOwnership" MUST be called before "_Text_LookupName".
  170. if (!_Text_CheckOwnership(YSI_g_sCurFile, val))
  171. {
  172. P:5("Styles_EntryTag: Don't own %s", val);
  173. // This script doesn't own this text group.
  174. return 0;
  175. }
  176. // Got the name of this group that this text entries is in.
  177. while (XML_GetKeyValue(name, val))
  178. {
  179. if (!strcmp(name, "name", true))
  180. {
  181. // Use the parent name to help look this up. Also check if we are
  182. // the owner of this group. Actually, use internal text functions
  183. // to do this given that it will know what file it is currently
  184. // loading and has better knowledge of its own internal structure.
  185. // This means we MUST call a style file parse function from inside
  186. // y_textint.
  187. id = _Text_LookupName(val);
  188. P:5("Styles_EntryTag: %s = %d", val, id);
  189. }
  190. else if (!strcmp(name, "type", true) || !strcmp(name, "style", true))
  191. {
  192. // The searched for strings are very lenient.
  193. if (strfind(val, "Client", true) != -1)
  194. {
  195. type = e_STYLE_TYPE_CLIENT;
  196. }
  197. else if (strfind(val, "Player", true) != -1)
  198. {
  199. type = e_STYLE_TYPE_PLAYER;
  200. }
  201. else if (strfind(val, "Draw", true) != -1)
  202. {
  203. type = e_STYLE_TYPE_TD;
  204. }
  205. else if (strfind(val, "3D", true) != -1)
  206. {
  207. type = e_STYLE_TYPE_3D;
  208. }
  209. else
  210. {
  211. // Better "strval" for short numbers.
  212. new
  213. tmp = val[0] - '0';
  214. if (0 <= tmp <= 6)
  215. {
  216. type = e_STYLE_TYPE_GT_0 * e_STYLE_TYPE:tmp;
  217. }
  218. }
  219. }
  220. else if (!strcmp(name, "3d", true))
  221. {
  222. data = strval(val);
  223. type = e_STYLE_TYPE_3D;
  224. }
  225. else if (!strcmp(name, "textdraw", true))
  226. {
  227. type = e_STYLE_TYPE_TD;
  228. if (isnumeric(val))
  229. {
  230. data = strval(val);
  231. }
  232. else
  233. {
  234. data = _:TD_GetNamed(val);
  235. }
  236. }
  237. else if (!strcmp(name, "time", true))
  238. {
  239. time = strval(val);
  240. }
  241. else if (!strcmp(name, "colour", true) || !strcmp(name, "color", true))
  242. {
  243. #if defined _inc_sscanf2 || defined unformat
  244. if (unformat(val, "n", colour))
  245. #else
  246. if (ishex(val)) colour = hexstr(val);
  247. else if (isnumeric(val)) colour = strval(val);
  248. else
  249. #endif
  250. colour = GetColour(val);
  251. }
  252. }
  253. if (0 <= id < MAX_STYLES)
  254. {
  255. //printf("SAVING %d", id);
  256. switch (type)
  257. {
  258. 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:
  259. // This doesn't seem to want to work...
  260. //case e_STYLE_TYPE_GT_0 .. e_STYLE_TYPE_GT_6:
  261. {
  262. YSI_g_sStyleData[id][E_STYLE_DATA_TIME] = time;
  263. // Don't set ANY colours here!
  264. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = type;
  265. }
  266. case e_STYLE_TYPE_TD:
  267. {
  268. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = type;
  269. YSI_g_sStyleData[id][E_STYLE_DATA_TD_ID] = Style:data;
  270. }
  271. default:
  272. {
  273. //printf("SAVING %d", id);
  274. YSI_g_sStyleData[id][E_STYLE_DATA_3D_ID] = -1;
  275. //printf("SAVING %d", id);
  276. YSI_g_sStyleData[id][E_STYLE_DATA_TYPE] = e_STYLE_TYPE:(colour >>> 4 ) | type;
  277. //printf("SAVING %d", id);
  278. }
  279. /*case e_STYLE_TYPE_PLAYER:
  280. {
  281. }
  282. case e_STYLE_TYPE_TD:
  283. {
  284. }
  285. case e_STYLE_TYPE_3D:
  286. {
  287. }
  288. case e_STYLE_TYPE_OTHER:
  289. {
  290. YSI_g_sStyleData[id] =
  291. }*/
  292. }
  293. }
  294. //printf("SAVING %d", id);
  295. // Return value is not important here.
  296. return 1;
  297. }
  298. forward Styles_3DTag();
  299. public Styles_3DTag()
  300. {
  301. P:2("Styles_EntryTag called");
  302. static
  303. name[MAX_XML_ENTRY_NAME],
  304. val[MAX_XML_ENTRY_TEXT],
  305. #if !defined _inc_sscanf2 && !defined unformat
  306. tmp,
  307. #endif
  308. Float:x,
  309. Float:y,
  310. Float:z,
  311. e_3D_FLAGS:flags;
  312. if (YSI_g_sCur3D == MAX_3D_STYLES)
  313. {
  314. return -1;
  315. }
  316. while (XML_GetKeyValue(name, val))
  317. {
  318. if (!strcmp(name, "x", true))
  319. {
  320. x = floatstr(val);
  321. }
  322. else if (!strcmp(name, "y", true))
  323. {
  324. y = floatstr(val);
  325. }
  326. else if (!strcmp(name, "z", true))
  327. {
  328. z = floatstr(val);
  329. }
  330. else if (!strcmp(name, "pos", true))
  331. {
  332. // A good example of the advantages of sscanf.
  333. #if defined _inc_sscanf2 || defined unformat
  334. unformat(val, "p<,>fff", x, y, z);
  335. #else
  336. x = floatstr(val);
  337. tmp = strfind(val, ",") + 1;
  338. if (tmp)
  339. {
  340. y = floatstr(val[tmp]);
  341. tmp = strfind(val, ",") + 1;
  342. if (tmp)
  343. {
  344. z = floatstr(val[tmp]);
  345. }
  346. }
  347. #endif
  348. }
  349. else if (!strcmp(name, "vw", true) || !strcmp(name, "virtualworld", true))
  350. {
  351. flags = (flags & ~e_3D_FLAGS_VW) | ((e_3D_FLAGS:strval(val)) & e_3D_FLAGS_VW);
  352. }
  353. else if (!strcmp(name, "los", true) || !strcmp(name, "lineofsight", true))
  354. {
  355. if (!strcmp(val, "true", true) || strval(val))
  356. {
  357. flags |= e_3D_FLAGS_LOS;
  358. }
  359. else
  360. {
  361. flags &= ~e_3D_FLAGS_LOS;
  362. }
  363. }
  364. else if (!strcmp(name, "drawdistance", true))
  365. {
  366. flags = (flags & ~e_3D_FLAGS_DD) | ((e_3D_FLAGS:strval(val) << e_3D_FLAGS_DD_SHIFT) & e_3D_FLAGS_DD);
  367. }
  368. }
  369. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_X] = x;
  370. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_Y] = y;
  371. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_Z] = z;
  372. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_FLAGS] = flags;
  373. YSI_g_s3DLabelData[YSI_g_sCur3D][E_3D_DATA_ATTACH] = -1;
  374. return YSI_g_sCur3D++;
  375. }
  376. /*----------------------------------------------------------------------------*\
  377. Function:
  378. Styles_OnScriptInit
  379. Params:
  380. -
  381. Return:
  382. -
  383. Notes:
  384. Loads all the style files relevant to this script. Filters by text group in
  385. the XML callback. Loops through all publics, then loops through them all
  386. again to check that the current file hasn't already been loaded. Despite
  387. the filtering, it is done this way to ensure that all relevant text draw
  388. styles are loaded for when we are displaying text.
  389. \*----------------------------------------------------------------------------*/
  390. //hook OnScriptInit()
  391. public _Styles_SpecialInit()
  392. {
  393. if (YSI_g_sXMLRules == NO_XML_FILE)
  394. {
  395. YSI_g_sXMLRules = XML_New();
  396. if (YSI_g_sXMLRules != NO_XML_FILE)
  397. {
  398. // This is copied directly from y_td.
  399. XML_AddHandler(YSI_g_sXMLRules, "color", "TD_LoadColour");
  400. XML_AddHandler(YSI_g_sXMLRules, "colour", "TD_LoadColour");
  401. XML_AddHandler(YSI_g_sXMLRules, "textdraw", "TD_Textdraw");
  402. //XML_AddHandler(YSI_g_sXMLRules, "box", "TD_Box");
  403. //XML_AddHandler(YSI_g_sXMLRules, "background", "TD_Background");
  404. //XML_AddHandler(YSI_g_sXMLRules, "style", "TD_Style");
  405. XML_AddHandler(YSI_g_sXMLRules, "entry", "Styles_EntryTag");
  406. XML_AddHandler(YSI_g_sXMLRules, "3d", "Styles_3DTag");
  407. XML_AddHandler(YSI_g_sXMLRules, "group", NULL);
  408. }
  409. }
  410. YSI_g_sCur3D = 0;
  411. new
  412. idx = 0,
  413. buffer[32],
  414. b2[32],
  415. pos;
  416. // Load the data for ALL the files this script uses (to get textdraw data).
  417. while ((idx = AMX_GetPublicNameSuffix(idx, buffer, _A<@yX_>)))
  418. {
  419. if ((pos = strfind(buffer, "@")) != -1)
  420. {
  421. buffer{pos} = '\0';
  422. if (strcmp(buffer, YSI_g_sCurFile))
  423. {
  424. // Different - check for more matches.
  425. new
  426. i2 = 0;
  427. for ( ; ; )
  428. {
  429. i2 = AMX_GetPublicNameSuffix(i2, b2, _A<@yX_>);
  430. if (i2 == idx)
  431. {
  432. // Unique entry found. Load the file.
  433. strunpack(YSI_g_sCurFile, buffer);
  434. format(b2, sizeof (b2), STYLES_FORMAT_FILE, YSI_g_sCurFile);
  435. XML_Parse(YSI_g_sXMLRules, b2);
  436. break;
  437. }
  438. else if (!strcmp(b2, buffer))
  439. {
  440. break;
  441. }
  442. }
  443. }
  444. }
  445. }
  446. }
  447. /*stock _Styles_ParseOne(file[])
  448. {
  449. new
  450. fname[64];
  451. format(fname, sizeof (fname), STYLES_LANG_DATA, file);
  452. XML_Parse(YSI_g_sXMLRules, fname);
  453. }*/
  454. /*----------------------------------------------------------------------------*\
  455. Function:
  456. Styles_Reload
  457. Params:
  458. -
  459. Return:
  460. -
  461. Notes:
  462. Reloads all the string styles in all scripts. Use sparingly!
  463. \*----------------------------------------------------------------------------*/
  464. stock Styles_Reload()
  465. {
  466. CallRemoteFunction("_Styles_SpecialInit", "");
  467. }