demo03.dpr 10 KB

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