gegl_drawelement.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. {
  2. * Copyright (c) 2021 SSW
  3. *
  4. * This software is provided 'as-is', without any express or
  5. * implied warranty. In no event will the authors be held
  6. * liable for any damages arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented;
  13. * you must not claim that you wrote the original software.
  14. * If you use this software in a product, an acknowledgment
  15. * in the product documentation would be appreciated but
  16. * is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such,
  19. * and must not be misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any
  22. * source distribution.
  23. }
  24. unit gegl_drawElement;
  25. {$I zgl_config.cfg}
  26. interface
  27. uses
  28. zgl_font,
  29. zgl_types,
  30. {$IfDef USE_GLES}
  31. zgl_opengles_all,
  32. {$Else}
  33. zgl_opengl_all,
  34. {$EndIf}
  35. zgl_gltypeconst,
  36. zgl_window,
  37. gegl_Types,
  38. gegl_VElements,
  39. gegl_color,
  40. zgl_fx,
  41. zgl_primitives_2d,
  42. zgl_render_2d;
  43. // подготовка к выводу текста поля ввода, а так же созданной пользователем процедуры вывода окантовки поля ввода
  44. procedure EditDraw(num: Word);
  45. // вывод текста поля ввода, при выходе за пределы поля, производится обрезание текста.
  46. procedure DrawTextEdit(Edit: geglPEdit);
  47. implementation
  48. procedure EditDraw(num: Word);
  49. var
  50. UseText: geglPEdit;
  51. begin
  52. UseText := managerSetOfTools.SetOfTools[num];
  53. (* В данном случае надо обратить внимание, что все трансформации должны быть произведены "по умолчанию" -
  54. ротация, шкала, трансляция.
  55. Получается, для поля ввода, должнен быть определён фонт, который не должен меняться, но пользователь
  56. может сам выбирать этот фонт, либо просто переименовывать свой фонт, в нужное название.
  57. На данное время, работа с разными фонтами, даже менеджера не будет реализовано. Возможно в дальнейшем
  58. будет реализовано. *)
  59. // batch2d_Begin;
  60. (* при включении batch2d получаем "весёлый" эффект, когда поле ввода может быть в другом месте, и в нужном. И перемигиваться этими полями. *)
  61. glPushMatrix;
  62. // устанавливаем центр вращения
  63. glTranslatef(UseText^.RotatePoint.X, UseText^.RotatePoint.Y, 0);
  64. // поворачиваем
  65. glRotatef(UseText^.Rotate, 0, 0, 1);
  66. // и возвращаем на начальное значение рисования, только уже повернули
  67. glTranslatef(UseText^.Rect.X - UseText^.RotatePoint.x, UseText^.Rect.Y - UseText^.RotatePoint.Y, 0);
  68. // функция вывода окантовки, если её задали
  69. if Assigned(UseText^.procDraw) then
  70. UseText^.procDraw;
  71. // трансляция для вывода текста
  72. glTranslatef(- UseText^.translateX, 0, 0);
  73. //--------------------------------------------------------------
  74. if UseText^.EditString.UseLen > 0 then
  75. begin
  76. Set_numColor(UseText^.ColorText);
  77. batch2d_Begin;
  78. DrawTextEdit(UseText);
  79. batch2d_End;
  80. end;
  81. // рисуем курсор, только если этот элемент активирован
  82. if managerSetOfTools.ActiveElement = num then
  83. if UseText^.Cursor.NSleep > 0 then
  84. begin
  85. if UseText^.Cursor.Flags then // проверяем что прорисовывать (флаг мигания)
  86. begin // и для знака подчёркивания
  87. pr2d_Rect(UseText^.Cursor.curRect.X, UseText^.Cursor.curRect.Y, UseText^.Cursor.curRect.W,
  88. UseText^.Cursor.curRect.H, {$IfDef OLD_METHODS}$FFFFFF, 160{$Else}UseText^.ColorCursor{$EndIf}, PR2D_FILL);
  89. end;
  90. dec(UseText^.Cursor.NSleep);
  91. end
  92. else begin
  93. UseText^.Cursor.NSleep := 15;
  94. UseText^.Cursor.Flags := not (UseText^.Cursor.Flags);
  95. end;
  96. //--------------------------------------------------------------
  97. glPopMatrix;
  98. // batch2d_End;
  99. UseText := nil;
  100. end;
  101. // в данном варианте так, но надо всё будет переделать - размер шрифта идёт по умолчанию
  102. // задача: производить вывод только тех символов, которые попадают в поле ввода
  103. // подзадача: произвести текстурную выборку для символов, которые должны "резаться" - которые попадают на стык поля ввода
  104. procedure DrawTextEdit(Edit: geglPEdit);
  105. var
  106. i: LongWord;
  107. charDesc: zglPCharDesc;
  108. lastPage: Integer;
  109. XLoop, YLoop: Single;
  110. xx1, xx2, yy1, yy2: Single;
  111. xTex1, yTex1, xTex2, yTex2: Single;
  112. useFont: zglPFont;
  113. mode: LongWord;
  114. label
  115. StartLoop, EndLoop, NextLoop;
  116. begin
  117. if managerSetOfTools.count = 0 then
  118. exit;
  119. useFont := managerFont.Font[Edit^.font];
  120. i := 0;
  121. lastPage := -1;
  122. XLoop := 0;
  123. YLoop := - useFont^.MaxShiftY * useFont^.Scale;
  124. // надо определиться, нужен этот код или нет
  125. // charDesc := managerFont.Font[Edit^.font].CharDesc[Edit^.EditString.LineString[i]^.CharSymb];
  126. { charDesc := useFont^.CharDesc[Edit^.EditString.CharSymb[i]];
  127. if not b2dStarted Then // этот код нужен, когда мы закончили рисовать через glEnd, а bSize не обнулился
  128. begin // и в следующий момент, мы можем получить наложение двух разных элементов.
  129. if Assigned(charDesc) Then // это надо проверить при выводе текста. Сработало обнуление или нет.
  130. begin
  131. lastPage := charDesc^.Page;
  132. batch2d_Check(GL_QUADS, FX_BLEND, managerFont.Font[Edit^.font].Pages[lastPage]);
  133. glEnable(GL_BLEND);
  134. glEnable(GL_TEXTURE_2D);
  135. glBindTexture(GL_TEXTURE_2D, managerFont.Font[Edit^.font].Pages[lastPage]^.ID);
  136. glBegin(GL_QUADS);
  137. end else
  138. begin
  139. glEnable(GL_BLEND);
  140. glEnable(GL_TEXTURE_2D);
  141. glBegin(GL_QUADS);
  142. end;
  143. end; }
  144. StartLoop:
  145. if Edit^.EditString.CharSymb[i] = 0 then
  146. goto EndLoop;
  147. charDesc := useFont^.CharDesc[Edit^.EditString.CharSymb[i]];
  148. if not Assigned(charDesc) Then
  149. charDesc := useFont^.CharDesc[63];
  150. XLoop := Edit^.EditString.posX[i];
  151. xx1 := XLoop + charDesc^.xx1;
  152. xx2 := XLoop + charDesc^.xx2;
  153. xTex1 := charDesc^.TexCoords[0].X;
  154. xTex2 := charDesc^.TexCoords[1].X;
  155. if xx1 > Edit^.translateX + Edit^.Rect.W then
  156. goto EndLoop;
  157. if xx2 < Edit^.translateX then
  158. goto NextLoop;
  159. if xx1 < Edit^.translateX then
  160. begin
  161. xTex1 := xTex2 - (xx2 - Edit^.translateX) * (xTex2 - xTex1) / (xx2 - xx1);
  162. xx1 := Edit^.translateX;
  163. end;
  164. if xx2 > Edit^.translateX + Edit^.Rect.W then
  165. begin
  166. xTex2 := xTex2 - (xx2 - Edit^.translateX - Edit^.Rect.W) * (xTex2 - xTex1) / (xx2 - xx1);
  167. xx2 := Edit^.translateX + Edit^.Rect.W;
  168. end;
  169. if lastPage <> charDesc^.Page Then
  170. begin
  171. lastPage := charDesc^.Page;
  172. if (not b2dStarted) Then
  173. begin
  174. glEnd();
  175. glEnable(GL_BLEND);
  176. glEnable(GL_TEXTURE_2D);
  177. glBindTexture(GL_TEXTURE_2D, useFont^.Pages[lastPage]^.ID);
  178. glBegin(GL_QUADS);
  179. end else
  180. if batch2d_Check(GL_QUADS, FX_BLEND, useFont^.Pages[lastPage]) Then
  181. begin
  182. glEnable(GL_BLEND);
  183. glEnable(GL_TEXTURE_2D);
  184. glBindTexture(GL_TEXTURE_2D, useFont^.Pages[lastPage]^.ID);
  185. glBegin(GL_QUADS);
  186. end;
  187. end;
  188. yTex1 := charDesc^.TexCoords[2].Y;
  189. yTex2 := charDesc^.TexCoords[0].Y;
  190. yy1 := YLoop + charDesc^.yy1;
  191. yy2 := YLoop + charDesc^.yy2;
  192. glTexCoord2f(xTex1, yTex2);
  193. glVertex2f(xx1, yy1);
  194. glTexCoord2f(xTex2, yTex2);
  195. glVertex2f(xx2, yy1);
  196. glTexCoord2f(xTex2, yTex1);
  197. glVertex2f(xx2, yy2);
  198. glTexCoord2f(xTex1, yTex1);
  199. glVertex2f(xx1, yy2);
  200. NextLoop:
  201. inc(i);
  202. if i < MAX_SYMBOL_LINE then
  203. goto StartLoop;
  204. EndLoop:
  205. if not b2dStarted Then
  206. begin
  207. glEnd();
  208. glDisable(GL_TEXTURE_2D);
  209. glDisable(GL_BLEND);
  210. end;
  211. end;
  212. end.