y_gui2.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //#define Y_GUI_Y_ADJUST (3.0476190476)
  2. //#define Y_GUI_Y_ADJUST (3.0)
  3. //#define Y_GUI_Y_ADJUST (3.1060948081)
  4. #include "y_iterate"
  5. #include "y_playerset"
  6. #include "y_hooks"
  7. #define MAX_GUI_TEXT_LENGTH (32)
  8. #define MAX_GUI_ELEMENTS (256)
  9. #define NO_GUI (GUI:cellmin)
  10. enum E_GUI_SLOT
  11. {
  12. Float:E_GUI_SLOT_X,
  13. Float:E_GUI_SLOT_Y,
  14. Float:E_GUI_SLOT_W,
  15. Float:E_GUI_SLOT_H,
  16. E_GUI_SLOT_TEXT[MAX_GUI_TEXT_LENGTH char],
  17. E_GUI_SLOT_COLOUR,
  18. E_GUI_SLOT_BORDER,
  19. Text:E_GUI_SLOT_TD,
  20. E_GUI_SLOT_NEXT
  21. }
  22. static stock
  23. YSI_gsFreeSlot = 0,
  24. YSI_gsGUIData[MAX_GUI_ELEMENTS][E_GUI_SLOT];
  25. hook OnScriptInit()
  26. {
  27. for (new i = 0; i != MAX_GUI_ELEMENTS; ++i)
  28. {
  29. YSI_gsGUIData[i][E_GUI_SLOT_NEXT] = i + 1;
  30. YSI_gsGUIData[i][E_GUI_SLOT_TD] = Text:-1;
  31. }
  32. YSI_gsGUIData[MAX_GUI_ELEMENTS - 1][E_GUI_SLOT_NEXT] = cellmin;
  33. }
  34. stock Text:GUI_DrawBox(Float:x, Float:y, Float:width, Float:height, colour, string:text[] = "", border = 0)
  35. {
  36. // Does the rendering for a part of a GUI. I spent a long time callibrating
  37. // these display modifications, so they should be pretty accurate. At least
  38. // they were tested at 1280 and 1680 (as that's as high as I can go).
  39. new
  40. //Text:hnd = TextDrawCreate(x, y, text);
  41. //Text:hnd = TextDrawCreate((x * 2.0 + width) / 2.0, 0.9308510638 * (y + 4.0), "_");
  42. Text:hnd = TextDrawCreate((x * 2.0 + width) / 2.0, (y + 3.4) / 1.0711904762 - 1.5, "_");
  43. //Text:hnd = TextDrawCreate((x * 2.0 + width) / 2.0, y, "_");
  44. TextDrawUseBox(hnd, true);
  45. // Subtract 8 to get the correct width (letter border maybe)...
  46. //TextDrawTextSize(hnd, 0.0, width - 8.0);
  47. // DON'T subtract when not on 640x480...
  48. // 4.0 works for 1280x960, 3.0485 works for 1280x960 AND 1680x1050, don't
  49. // know about larger but this I think is fairly well tweaked.
  50. TextDrawTextSize(hnd, 0.0, width - 3.0485); // - Y_GUI_Y_ADJUST);
  51. //TextDrawTextSize(hnd, x + width, height);
  52. //TextDrawTextSize(hnd, 0.1, 0.1);
  53. //TextDrawLetterSize(hnd, 1.0, height * 0.064);
  54. //TextDrawLetterSize(hnd, 1.0, height); // * 0.135);
  55. TextDrawLetterSize(hnd, 1.0, (height - 4.0) / 9.58); //10.29); // was 9.65
  56. TextDrawBoxColor(hnd, colour);
  57. TextDrawColor(hnd, 0x0000FFFF);
  58. TextDrawBackgroundColor(hnd, 0xFF0000FF);
  59. TextDrawSetOutline(hnd, 0);
  60. //TextDrawSetProportional(hnd, true);
  61. TextDrawAlignment(hnd, 2);
  62. return hnd;
  63. }
  64. stock GUI:GUI_Create(Float:x, Float:y, Float:width, Float:height, text[] = "", colour = 0xE4E4E4FF, border = 0)
  65. {
  66. // Get the next free ID.
  67. if (YSI_gsFreeSlot == cellmin)
  68. {
  69. return NO_GUI;
  70. }
  71. new
  72. slot = YSI_gsFreeSlot;
  73. YSI_gsFreeSlot = YSI_gsGUIData[slot][E_GUI_SLOT_NEXT];
  74. YSI_gsGUIData[slot][E_GUI_SLOT_NEXT] = cellmin;
  75. YSI_gsGUIData[slot][E_GUI_SLOT_X] = x;
  76. YSI_gsGUIData[slot][E_GUI_SLOT_Y] = y;
  77. YSI_gsGUIData[slot][E_GUI_SLOT_W] = width;
  78. YSI_gsGUIData[slot][E_GUI_SLOT_H] = height;
  79. YSI_gsGUIData[slot][E_GUI_SLOT_COLOUR] = colour;
  80. YSI_gsGUIData[slot][E_GUI_SLOT_BORDER] = border;
  81. strpack(YSI_gsGUIData[slot][E_GUI_SLOT_TEXT], text);
  82. return GUI:slot;
  83. }
  84. static stock bool:GUI_Link(GUI:x, GUI:parent)
  85. {
  86. if (x == NO_GUI || parent == NO_GUI)
  87. {
  88. return false;
  89. }
  90. new
  91. slot = YSI_gsGUIData[_:parent][E_GUI_SLOT_NEXT];
  92. YSI_gsGUIData[_:x][E_GUI_SLOT_NEXT] = slot;
  93. YSI_gsGUIData[_:parent][E_GUI_SLOT_NEXT] = _:x;
  94. return true;
  95. }
  96. stock GUI_Button(GUI:parent, Float:xOffset, Float:yOffset, Float:width, Float:height, text[])
  97. {
  98. xOffset += YSI_gsGUIData[_:parent][E_GUI_SLOT_X];
  99. yOffset += YSI_gsGUIData[_:parent][E_GUI_SLOT_Y];
  100. return GUI_Link(GUI_Create(xOffset, yOffset, width, height, text, 0xC0C0C0FF), parent);
  101. }
  102. static stock GUI_ShowOne(gui[E_GUI_SLOT])
  103. {
  104. if (gui[E_GUI_SLOT_TD] == -1)
  105. {
  106. gui[E_GUI_SLOT_TD] = GUI_DrawBox(gui[E_GUI_SLOT_X], gui[E_GUI_SLOT_Y], gui[E_GUI_SLOT_W], gui[E_GUI_SLOT_H], gui[E_GUI_SLOT_COLOUR], gui[E_GUI_SLOT_TEXT], gui[E_GUI_SLOT_BORDER]);
  107. }
  108. return gui[E_GUI_SLOT_NEXT];
  109. }
  110. #define GUI_Show PSF:_GUI_Show
  111. stock _GUI_Show(@PlayerSet:players, GUI:gui)
  112. {
  113. new
  114. cur = _:gui;
  115. while (cur != cellmin)
  116. {
  117. cur = GUI_ShowOne(YSI_gsGUIData[cur]);
  118. }
  119. foreach (new i : PS(players))
  120. {
  121. cur = _:gui;
  122. while (cur != cellmin)
  123. {
  124. TextDrawShowForPlayer(i, YSI_gsGUIData[cur][E_GUI_SLOT_TD]);
  125. cur = YSI_gsGUIData[cur][E_GUI_SLOT_NEXT];
  126. }
  127. }
  128. }
  129. stock GUI:GUI_Window(Float:x, Float:y, Float:width, Float:height, title[])
  130. {
  131. new
  132. GUI:base = GUI_Create(x, y, width, height, _, 0xE4E4E4FF),
  133. GUI:other;
  134. if (base == NO_GUI)
  135. {
  136. return NO_GUI;
  137. }
  138. // Do window elements in reverse Z-order.
  139. // Close button.
  140. other = GUI_Create(x + width - 8.5, y + 1.5, 7.0, 7.0, "x", 0xC0C0C0FF);
  141. if (other == NO_GUI)
  142. {
  143. // Free "base".
  144. return NO_GUI;
  145. }
  146. GUI_Link(other, base);
  147. // Title bar.
  148. other = GUI_Create(x, y, width, 10.0, title, 0x0000FFFF);
  149. if (other == NO_GUI)
  150. {
  151. // Free "base".
  152. return NO_GUI;
  153. }
  154. GUI_Link(other, base);
  155. return base;
  156. }