demo03.dpr 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. program demo03;
  2. {$I zglCustomConfig.cfg}
  3. {$I zgl_config.cfg}
  4. // RU: Âêëþ÷èòå KEYBOARD_OLD_FUNCTION â zgl_config.cfg, åñëè õîòèòå èñïîëüçîâàòü ñòàðûå ôóíêöèè êëàâèàòóðû.
  5. // Ñ ââåäåíèåì Green Engine - îíè ñòàëè íå àêòóàëüíû, íî ÿ èõ ïîêà îñòàâëÿþ.
  6. // EN: Enable KEYBOARD_OLD_FUNCTION in zgl_config.cfg if you want to use the old keyboard functions.
  7. // With the introduction of the Green Engine - they are no longer relevant, but I leave them for now.
  8. {$R *.res}
  9. uses
  10. zgl_screen,
  11. zgl_window,
  12. zgl_timers,
  13. zgl_mouse,
  14. zgl_keyboard,
  15. zgl_joystick,
  16. zgl_primitives_2d,
  17. zgl_render_2d,
  18. zgl_font,
  19. zgl_text,
  20. zgl_textures_png,
  21. zgl_types,
  22. {$IfDef KEYBOARD_OLD_FUNCTION}
  23. zgl_collision_2d,
  24. {$Else}
  25. gegl_VElements,
  26. gegl_utils,
  27. // Rus: Âêëþ÷èòå Define USE_VKEYBOARD â zgl_config.cfg åñëè õîòèòå ïîïðîáîâàòü èñïîëüçîâàòü âèðòóàëüíóþ êëàâèàòóðó.
  28. // Eng: Enable Define USE_VKEYBOARD in zgl_config.cfg if you want to try using the virtual keyboard.
  29. {$IfDef USE_VKEYBOARD}
  30. gegl_draw_gui,
  31. gegl_menu_gui,
  32. {$EndIf}
  33. {$EndIf}
  34. gegl_color,
  35. zgl_utils
  36. ;
  37. var
  38. dirRes : UTF8String {$IFNDEF MACOSX} = '../data/' {$ENDIF};
  39. joyCount : Integer;
  40. // RU: ñòðîêà äëÿ ïîëó÷åíèÿ çíà÷åíèÿ èç ïîëÿ ââîäà
  41. // EN: string to get value from input field
  42. userInput : UTF8String;
  43. {$IfDef KEYBOARD_OLD_FUNCTION}
  44. trackInput : Boolean;
  45. inputRect : zglTRect;
  46. lineAlpha : LongWord;
  47. TimeStart : LongWord;
  48. {$Else}
  49. // Rus: íîìåðà øðèôòîâ. Âñÿ ðàáîòà ñî øðèôòàìè ïðîèñõîäèò èìåííî îò ýòèõ íîìåðîâ.
  50. // Eng: font numbers. All work with fonts comes from these numbers.
  51. fntMain, fntEdit: LongWord;
  52. // Rus: íîìåð öâåòà. Ðàáîòà ñ öâåòîì ïðîèñõîäèò èìåííî îò ýòîãî íîìåðà.
  53. // Eng: color number. Work with color comes from this number.
  54. EditColor: LongWord;
  55. // RU: ïðÿìîóãîëüíèê îïèñûâàþùèé ïîëå ââîäà
  56. // EN: rectangle describing the input field
  57. myRect: zglTRect2D;
  58. // "ïåðåïèñü" ïîëåé ââîäà äëÿ òîãî, ÷òîá çíàòü ñ êàêèì ïîëåì ðàáîòàåì.
  59. // RU: îáúÿâëÿåì ïåðåìåííóþ äëÿ ðàáîòû ñ ïîëåì ââîäà
  60. // EN: we declare a variable to work with the input field
  61. myEdit, myEdit2: LongWord;
  62. // RU: ïðîðèñîâûâàåì îñíîâàíèå ïîëÿ ââîäà. Âñ¸ îãðàíè÷åíî òîëüêî âàøèì âîîáðàæåíèåì. )))
  63. // EN: draw the base of the input field. Everything is limited only by your imagination. )))
  64. procedure EditCont;
  65. begin
  66. // RU: ïðè ïðîðèñîâêå ïîëÿ ââîäà, ñìåøåíèå è ïîâîðîò óæå áóäóò ñäåëàíû. ß ïîêàçûâàþ êàê íàðèñîâàòü ðàìêó.
  67. // Òåêñò áóäåò âûâåäåí ïîâåðõ òîãî, ÷òî âû çäåñü íàðèñóåòå.
  68. // EN: displacement and rotation will be done prior to performing the procedure. I am showing you how to draw a frame.
  69. // The text will be drawn on top of what you draw here.
  70. pr2d_Rect(- 2, - 1, myRect.W + 5, myRect.H, cl_White, PR2D_FILL);
  71. end;
  72. {$EndIf}
  73. procedure Init;
  74. {$IfNDef KEYBOARD_OLD_FUNCTION}
  75. var
  76. EScale: LongWord;
  77. {$EndIf}
  78. begin
  79. fntMain := font_LoadFromFile(dirRes + 'font.zfi');
  80. {$IfNDef KEYBOARD_OLD_FUNCTION}
  81. // RU: Çàãðóæàåì äàííûå î øðèôòå.
  82. // EN: Load the font.
  83. fntEdit := font_LoadFromFile( dirRes + 'CalibriBold50pt.zfi');
  84. {$IfDef USE_VKEYBOARD}
  85. //------------------------------------------------------------------------------
  86. // RU: Äàííûå äëÿ âèðòóàëüíîé êëàâèàòóðû. Ðàñêîììåíòèðóéòå, åñëè áóäåòå èñïîëüçîâàòü âèðòóàëüíóþ êëàâèàòóðó äëÿ ÏÊ.
  87. // EN: Data for the virtual keyboard. Uncomment if you will use the virtual keyboard for PC.
  88. // îáÿçàòåëüíûé êîä! Äàííûå äëÿ îòîáðàæåíèÿ êëàâèàòóðû.
  89. // RU: Çàãðóæàåì äàííûå î øðèôòå.
  90. // EN: Load the font.
  91. fontUse := font_LoadFromFile(dirRes + 'CalibriBold50pt.zfi');
  92. JoyArrow := tex_LoadFromFile(dirRes + 'arrow.png'); // çàãðóçèëè òåêñòóðó
  93. tex_SetFrameSize(JoyArrow, 64, 64); // è ðàçáèëè å¸ íà ÷àñòè, íî â çàïèñÿõ íå áóäåò óêàçàíî êîëè÷åñòâî ïîëó÷åííûõ òåêñòóð
  94. // RU: Äàííûå äëÿ âèðòóàëüíîé êëàâèàòóðû.
  95. // EN: Data for the virtual keyboard.
  96. txt_LoadFromFile(dirRes + 'Rus.txt', LoadText);
  97. // RU: Ñîçäà¸ì âèðòóàëüíóþ êëàâèàòóðó. Äëÿ ìîáèëüíûõ ñèñòåì ýòî áóäåò îáÿçàòåëüíûì êîäîì â äàëüíåéøåì.
  98. // EN: We create a virtual keyboard. For mobile systems, this will be a mandatory code in the future.
  99. CreateTouchKeyboard;
  100. // RU: çäåñü äàííûå äëÿ âèðòóàëüíîé êëàâèàòóðû çàêàí÷èâàþòñÿ.
  101. // EN: here the data for the virtual keyboard ends.
  102. //------------------------------------------------------------------------------
  103. {$EndIf}
  104. // RU: óêàçûâàåì ðàçìåðû øðèôòîâ
  105. // EN: set font sizes
  106. setFontTextScale(15, fntMain);
  107. setFontTextScale(20, fntEdit);
  108. // RU: ðàçìåð øðèôòà ïîëÿ ââîäà (äëÿ ïîíèìàíèÿ ÷òî ïðîèñõîäèò). Èçìåíÿÿ ðàçìåð øðèôòà, ìû äîëæíû ìåíÿòü è
  109. // ðàçìåðû ïîëÿ ââîäà - myRect â äàííîì ñëó÷àå. Ñàìè îíè íå èçìåíÿòñÿ.
  110. // EN: the font size of the input field (to understand what's going on). By changing the font size,
  111. // we must also change the size of the input field - myRect in this case. They themselves will not change.
  112. EScale := 20;
  113. setFontTextScale(EScale, fntEdit);
  114. // RU: óêàçûâàåì ðàçìåðû ïîëÿ ââîäà
  115. // EN: specify the size of the input field
  116. myRect.X := 200;
  117. myRect.Y := 150;
  118. myRect.W := 200;
  119. myRect.H := 33;
  120. // RU: óêàçûâàåì òî÷êó âðàùåíèÿ, â äàííîì ñëó÷àå öåíòð ïîëÿ ââîäà(ïî íåîáõîäèìîñòè) è óãîë ïîâîðîòà(íàïðèìåð 45)
  121. // EN: specify the point of rotation, in this case the center of the input field (if necessary) and the angle of rotation (for example 45)
  122. SetOfRotateAngleAndPoint(myRect.x + myRect.W / 2, myRect.y + myRect.H / 2, 30);
  123. // RU: óêàçûâàåì öâåò òåêñòà (äîáàâëÿåì íîâûé íîìåð öâåòà, õîòÿ äàííàÿ ôóíêöèÿ âàì âîçâðàòèò öâåò, åñëè îí óæå áûë ïðîïèñàí).
  124. // EN: specify the color of the text (we add a new color number, although this function will return the color to you if it
  125. // has already been assigned).
  126. EditColor := Color_FindOrAdd($208055FF);
  127. // Ru: óñòàíàâëèâàåì öâåòà ïî óìîë÷àíèþ äëÿ âñåõ ýëåìåíòîâ API. Ýòè öâåòà áóäóò çàäåéñòâîâàíû òîëüêî ïðè ñîçäàíèè
  128. // îïðåäåë¸ííîãî ýëåìåíòà. Äëÿ èçìåíåíèÿ öâåòà â ñàìîì (óæå ñîçäàííîì) ýëåìåíòå, íè ÷åãî íå ïðèëàãàåòñÿ.
  129. // Äàëüíåéøèå èçìåíèíÿ ýòèõ çíà÷åíèé öâåòà, íè êàê íå ñêàæåòñÿ íà óæå ñîçäàííûõ ýëåìåíòàõ.
  130. // En: set default colors for all API elements. These colors will only be used when creating a specific element.
  131. // To change the color in the (already created) element itself, nothing is attached. Further changes to these
  132. // color values will not affect the already created elements in any way.
  133. SetDefColor(EditColor, cl_Green, cl_Black);
  134. // RU: ñîçäà¸ì ñàìî ïîëå ââîäà ñ äàííûìè óêàçàííûìè âûøå è ïåðåäàâàåìûìè äàííûìè
  135. // EN: create the input field itself with the data specified above and the data that needs to be transferred
  136. myEdit := CreateEdit(myRect, fntEdit, EScale, @EditCont);
  137. // RU: êîððåêòèðóåì êóðñîð.
  138. // EN: adjust the cursor.
  139. CorrectEditCursor(myEdit, 3);
  140. // RU: çàäà¸ì î÷èñòêó ýêðàíà çàäàííûì öâåòîì
  141. // EN: set the screen to clear with a specified color
  142. scr_SetClearColor(true, $7090af);
  143. {$Else}
  144. inputRect.X := 400 - 192;
  145. inputRect.Y := 300 - 100 - 32;
  146. inputRect.W := 384;
  147. inputRect.H := 96;
  148. setFontTextScale(15, fntMain);
  149. {$EndIf}
  150. // RU: Èíèöèàëèçèðóåì îáðàáîòêó ââîäà äæîéñòèêîâ è ïîëó÷àåì êîëè÷åñòâî ïîäêëþ÷åííûõ äæîéñòèêîâ.
  151. // EN: Initialize processing joystick input and get count of plugged joysticks.
  152. joyCount := joy_Init();
  153. end;
  154. procedure Draw;
  155. {$IfDef KEYBOARD_OLD_FUNCTION}
  156. var
  157. w : Single;
  158. {$EndIf}
  159. begin
  160. batch2d_Begin;
  161. // Ru: áàëóåìñÿ öâåòîì øðèôòà.
  162. // En: indulge in the color of the font.
  163. setTextColor(Get_Color(cl_Blue));
  164. text_Draw(fntMain, 0, 0, 'Escape - Exit');
  165. setTextColor(Get_Color(cl_White));
  166. // RU: Êîîðäèíàòû ìûøè ìîæíî ïîëó÷èòü ïðè ïîìîùè ôóíêöèé mouse_X è mouse_Y.
  167. // EN: Mouse coordinates can be got using functions mouse_X and mouse_Y.
  168. text_Draw(fntMain, 0, 16, 'Mouse X, Y: ' + u_IntToStr(mouseX) + '; ' + u_IntToStr(mouseY));
  169. {$IfDef KEYBOARD_OLD_FUNCTION}
  170. // RU: Âûâîäèì ââåä¸ííûé ïîëüçîâàòåëåì òåêñò.
  171. // EN: Show the inputted text.
  172. pr2d_Rect(inputRect.X, inputRect.Y, inputRect.W, inputRect.H, $FFFFFF, 255);
  173. if trackInput Then
  174. begin
  175. text_Draw(fntMain, 400, 300 - 100, 'Press Enter to stop track text input:', TEXT_HALIGN_CENTER);
  176. w := text_GetWidth( fntMain, userInput);
  177. pr2d_Rect(400 + w / 2 + 2, 300 - 70, 10, 20, $FFFFFF, lineAlpha, PR2D_FILL);
  178. end else
  179. text_Draw(fntMain, 400, 300 - 100, 'Click here to enter text(maximum - 24 symbols):', TEXT_HALIGN_CENTER);
  180. text_Draw(fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER);
  181. {$Else}
  182. text_Draw(fntMain, 0, 36, 'Press F5 to copy from Edit and draw'); // êàêîé ÿ íàôèã àíãëè÷àíèí? ))))
  183. text_Draw(fntMain, 0, 54, 'Press F12 - Rus/Eng');
  184. if userInput <> '' then
  185. text_Draw(fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER);
  186. {$EndIf}
  187. // RU: Âûâîä ñîñòîÿíèÿ îñåé è êíîïîê ïåðâîãî äæîéñòèêà â ñèñòåìå.
  188. // EN: Show the state of axes and buttons of first joystick in the system.
  189. text_Draw(fntMain, 400, 360, 'JOYSTICK ( Found: ' + u_IntToStr(joyCount) + ' )', TEXT_HALIGN_CENTER);
  190. setTextColor(Get_Color(cl_Black));
  191. text_Draw( fntMain, 100, 400, 'Axis X: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_X ) ) );
  192. text_Draw( fntMain, 100, 420, 'Axis Y: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Y ) ) );
  193. setTextColor(Get_Color(cl_Black05));
  194. text_Draw( fntMain, 100, 440, 'Axis Z: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Z ) ) );
  195. setTextColor(Get_Color(cl_Green));
  196. text_Draw( fntMain, 100, 460, 'Axis R: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_R ) ) );
  197. text_Draw( fntMain, 100, 480, 'Axis U: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_U ) ) );
  198. setTextColor(Get_Color(cl_Green05));
  199. text_Draw( fntMain, 100, 500, 'Axis V: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_V ) ) );
  200. text_Draw( fntMain, 100, 520, 'POVX: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVX ) ) );
  201. text_Draw( fntMain, 100, 540, 'POVY: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVY ) ) );
  202. setTextColor(Get_Color(cl_Red05));
  203. text_Draw(fntMain, 400, 400, 'Button1: ' + u_BoolToStr(joy_Down(0, 0)));
  204. text_Draw(fntMain, 400, 420, 'Button2: ' + u_BoolToStr(joy_Down(0, 1)));
  205. text_Draw(fntMain, 400, 440, 'Button3: ' + u_BoolToStr(joy_Down(0, 2)));
  206. text_Draw(fntMain, 400, 460, 'Button4: ' + u_BoolToStr(joy_Down(0, 3)));
  207. text_Draw(fntMain, 400, 480, 'Button5: ' + u_BoolToStr(joy_Down(0, 4)));
  208. text_Draw(fntMain, 400, 500, 'Button6: ' + u_BoolToStr(joy_Down(0, 5)));
  209. text_Draw(fntMain, 400, 520, 'Button7: ' + u_BoolToStr(joy_Down(0, 6)));
  210. text_Draw(fntMain, 400, 540, 'Button8: ' + u_BoolToStr(joy_Down(0, 7)));
  211. text_Draw(fntMain, 550, 400, 'Button9: ' + u_BoolToStr(joy_Down(0, 8)));
  212. text_Draw(fntMain, 550, 420, 'Button10: ' + u_BoolToStr(joy_Down(0, 9)));
  213. text_Draw(fntMain, 550, 440, 'Button11: ' + u_BoolToStr(joy_Down(0, 10)));
  214. text_Draw(fntMain, 550, 460, 'Button12: ' + u_BoolToStr(joy_Down(0, 11)));
  215. text_Draw(fntMain, 550, 480, 'Button13: ' + u_BoolToStr(joy_Down(0, 12)));
  216. text_Draw(fntMain, 550, 500, 'Button14: ' + u_BoolToStr(joy_Down(0, 13)));
  217. text_Draw(fntMain, 550, 520, 'Button15: ' + u_BoolToStr(joy_Down(0, 14)));
  218. text_Draw(fntMain, 550, 540, 'Button16: ' + u_BoolToStr(joy_Down(0, 15)));
  219. batch2d_End;
  220. end;
  221. {$IfDef KEYBOARD_OLD_FUNCTION}
  222. procedure Timer;
  223. begin
  224. if lineAlpha > 5 Then
  225. DEC(lineAlpha, 10)
  226. else
  227. lineAlpha := 255;
  228. end;
  229. {$EndIf}
  230. procedure KeyMouseEvent;
  231. begin
  232. {$IfDef KEYBOARD_OLD_FUNCTION}
  233. // RU: Ïðîâåðèòü íàæàòà ëè ëåâàÿ êíîïêà ìûøè â ïðåäåëàõ inputRect è íà÷àòü îòñëåæèâàòü ââîä òåêñòà.
  234. // EN: Check if left mouse button was pressed inside inputRect and start to track text input.
  235. if mouseBClick(M_BLEFT) and col2d_PointInRect(mouseX, mouseY, inputRect) Then
  236. begin
  237. trackInput := TRUE;
  238. key_BeginReadText(userInput, 24);
  239. end;
  240. // RU: Åñëè áûë íàæàò Enter ïðåêðàùàåì îòñëåæèâàòü ââîä òåêñòà.
  241. // EN: Finish to track text input if Enter was pressed.
  242. if key_Press(K_ENTER) Then
  243. begin
  244. trackInput := FALSE;
  245. key_EndReadText();
  246. end;
  247. // RU: Ïîëó÷àåì ââåä¸ííûé ïîëüçîâàòåëåì òåêñò.
  248. // EN: Get inputted by user text.
  249. if trackInput Then
  250. userInput := key_GetText();
  251. {$Else}
  252. // RU: ïî íàæàòèþ F5 êîïèðóåì òî, ÷òî íàïèñàíî â ïîëå ââîäà
  253. // EN: by pressing F5, copy what is written in the input field
  254. if keysDown[K_F5] then
  255. begin
  256. userInput := GetEditToText(myEdit);
  257. end;
  258. {$EndIf}
  259. end;
  260. Begin
  261. {$IfDef KEYBOARD_OLD_FUNCTION}
  262. TimeStart := timer_Add(@Timer, 16, t_Start);
  263. {$EndIf}
  264. zgl_Reg(SYS_EVENTS, @KeyMouseEvent);
  265. zgl_Reg(SYS_LOAD, @Init);
  266. zgl_Reg(SYS_DRAW, @Draw);
  267. wnd_SetCaption(utf8_Copy('03 - Input'));
  268. zgl_Init();
  269. End.