demo03.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. program demo03;
  2. {$I zglCustomConfig.cfg}
  3. {$I zgl_config.cfg}
  4. uses
  5. {$IFDEF UNIX}
  6. cthreads,
  7. {$ENDIF}
  8. zgl_screen,
  9. zgl_window,
  10. zgl_timers,
  11. zgl_mouse,
  12. zgl_keyboard,
  13. zgl_joystick,
  14. zgl_primitives_2d,
  15. zgl_font,
  16. zgl_text,
  17. zgl_textures_png,
  18. zgl_types,
  19. {$IfDef OLD_METHODS}
  20. zgl_collision_2d,
  21. {$Else}
  22. gegl_VElements,
  23. {$EndIf}
  24. zgl_utils
  25. ;
  26. var
  27. dirRes : UTF8String {$IFNDEF MACOSX} = '../data/' {$ENDIF};
  28. fntMain, fntEdit: Byte;
  29. joyCount : Integer;
  30. // RU: строка для получения значения из поля ввода
  31. // EN: string to get value from input field
  32. userInput : UTF8String;
  33. {$IfDef OLD_METHODS}
  34. trackInput : Boolean;
  35. inputRect : zglTRect;
  36. lineAlpha : Byte;
  37. TimeStart : Byte;
  38. {$Else}
  39. // RU: прямоугольник описывающий поле ввода
  40. // EN: rectangle describing the input field
  41. myRect: zglTRect;
  42. // "перепись" полей ввода для того, чтоб знать с каким полем работаем.
  43. // RU: объявляем переменную для работы с полем ввода
  44. // EN: we declare a variable to work with the input field
  45. myEdit, myEdit2: Word;
  46. // RU: прорисовываем основание поля ввода. Всё ограничено только вашим воображением. )))
  47. // EN: draw the base of the input field. Everything is limited only by your imagination. )))
  48. procedure EditCont;
  49. begin
  50. // RU: при прорисовке поля ввода, смешениt и поворот уже будут сделаны. Я показываю как нарисовать рамку.
  51. // Текст будет выведен поверх того, что вы здесь нарисуете.
  52. // EN: displacement and rotation will be done prior to performing the procedure. I am showing you how to draw a frame.
  53. // The text will be drawn on top of what you draw here.
  54. pr2d_Rect(- 2, - 1, myRect.W + 5, myRect.H, $FFFFFF, 128, PR2D_FILL);
  55. end;
  56. {$EndIf}
  57. procedure Init;
  58. {$IfNDef OLD_METHODS}
  59. var
  60. TextColor: zglTColor;
  61. {$EndIf}
  62. begin
  63. fntMain := font_LoadFromFile( dirRes + 'font.zfi' );
  64. {$IfNDef OLD_METHODS}
  65. // RU: Загружаем данные о шрифте.
  66. // EN: Load the font.
  67. fntEdit := font_LoadFromFile( dirRes + 'CalibriBold50pt.zfi');
  68. // RU: устанавливаем размеры шрифтов
  69. // EN: set font sizes
  70. setFontTextScale(15, fntMain);
  71. setFontTextScale(20, fntEdit);
  72. // RU: указываем размеры поля ввода
  73. // EN: specify the size of the input field
  74. myRect.X := 200;
  75. myRect.Y := 150;
  76. myRect.W := 200;
  77. myRect.H := 33;
  78. // RU: указываем точку вращения, в данном случае центр поля ввода (по необходимости) и угол поворота (например 45)
  79. // 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)
  80. SetOfRotateAngleAndPoint(myRect.x + myRect.W / 2, myRect.y + myRect.H / 2, 30);
  81. // RU: указываем цвет текста
  82. // EN: specify the color of the text
  83. TextColor.R := 0.1;
  84. TextColor.G := 0.5;
  85. TextColor.B := 0.3;
  86. TextColor.A := 1; // max = 1, min = 0
  87. // RU: передаём цвет в данные менеджера
  88. // EN: transfer the color to the manager data
  89. SetColorElementText(@TextColor);
  90. // RU: создаём само поле ввода с данными указанными выше
  91. // EN: create the input field itself with the data specified above
  92. myEdit := CreateEdit(myRect, fntEdit, 20, @EditCont);
  93. // RU: корректируем курсор
  94. // EN: adjust the cursor
  95. // CorrectEditCursor(myEdit, 3);
  96. // RU: задаём очистку экрана заданным цветом
  97. // EN: set the screen to clear with a specified color
  98. scr_SetClearColor(true, $7090af);
  99. {$Else}
  100. inputRect.X := 400 - 192;
  101. inputRect.Y := 300 - 100 - 32;
  102. inputRect.W := 384;
  103. inputRect.H := 96;
  104. {$EndIf}
  105. // RU: Инициализируем обработку ввода джойстиков и получаем количество подключенных джойстиков.
  106. // EN: Initialize processing joystick input and get count of plugged joysticks.
  107. joyCount := joy_Init();
  108. end;
  109. procedure Draw;
  110. {$IfDef OLD_METHODS}
  111. var
  112. w : Single;
  113. {$EndIf}
  114. begin
  115. text_Draw( fntMain, 0, 0, 'Escape - Exit' );
  116. // RU: Координаты мыши можно получить при помощи функций mouse_X и mouse_Y.
  117. // EN: Mouse coordinates can be got using functions mouse_X and mouse_Y.
  118. text_Draw( fntMain, 0, 18, 'Mouse X, Y: ' + u_IntToStr( mouseX ) + '; ' + u_IntToStr( mouseY ) );
  119. {$IfDef OLD_METHODS}
  120. // RU: Выводим введённый пользователем текст.
  121. // EN: Show the inputted text.
  122. pr2d_Rect( inputRect.X, inputRect.Y, inputRect.W, inputRect.H, $FFFFFF, 255 );
  123. if trackInput Then
  124. begin
  125. text_Draw( fntMain, 400, 300 - 100, 'Press Enter to stop track text input:', TEXT_HALIGN_CENTER );
  126. w := text_GetWidth( fntMain, userInput );
  127. pr2d_Rect( 400 + w / 2 + 2, 300 - 70, 10, 20, $FFFFFF, lineAlpha, PR2D_FILL );
  128. end else
  129. text_Draw( fntMain, 400, 300 - 100, 'Click here to enter text(maximum - 24 symbols):', TEXT_HALIGN_CENTER );
  130. text_Draw( fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER );
  131. {$Else}
  132. text_Draw(fntMain, 0, 36, 'Press F5 to copy from Edit and draw'); // какой я нафиг англичанин? ))))
  133. text_Draw(fntMain, 0, 54, 'Press F12 - Rus/Eng');
  134. if userInput <> '' then
  135. text_Draw(fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER);
  136. {$EndIf}
  137. // RU: Вывод состояния осей и кнопок первого джойстика в системе.
  138. // EN: Show the state of axes and buttons of first joystick in the system.
  139. text_Draw( fntMain, 400, 360, 'JOYSTICK ( Found: ' + u_IntToStr( joyCount ) + ' )', TEXT_HALIGN_CENTER );
  140. text_Draw( fntMain, 100, 400, 'Axis X: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_X ) ) );
  141. text_Draw( fntMain, 100, 420, 'Axis Y: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Y ) ) );
  142. text_Draw( fntMain, 100, 440, 'Axis Z: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Z ) ) );
  143. text_Draw( fntMain, 100, 460, 'Axis R: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_R ) ) );
  144. text_Draw( fntMain, 100, 480, 'Axis U: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_U ) ) );
  145. text_Draw( fntMain, 100, 500, 'Axis V: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_V ) ) );
  146. text_Draw( fntMain, 100, 520, 'POVX: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVX ) ) );
  147. text_Draw( fntMain, 100, 540, 'POVY: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVY ) ) );
  148. text_Draw( fntMain, 400, 400, 'Button1: ' + u_BoolToStr( joy_Down( 0, 0 ) ) );
  149. text_Draw( fntMain, 400, 420, 'Button2: ' + u_BoolToStr( joy_Down( 0, 1 ) ) );
  150. text_Draw( fntMain, 400, 440, 'Button3: ' + u_BoolToStr( joy_Down( 0, 2 ) ) );
  151. text_Draw( fntMain, 400, 460, 'Button4: ' + u_BoolToStr( joy_Down( 0, 3 ) ) );
  152. text_Draw( fntMain, 400, 480, 'Button5: ' + u_BoolToStr( joy_Down( 0, 4 ) ) );
  153. text_Draw( fntMain, 400, 500, 'Button6: ' + u_BoolToStr( joy_Down( 0, 5 ) ) );
  154. text_Draw( fntMain, 400, 520, 'Button7: ' + u_BoolToStr( joy_Down( 0, 6 ) ) );
  155. text_Draw( fntMain, 400, 540, 'Button8: ' + u_BoolToStr( joy_Down( 0, 7 ) ) );
  156. text_Draw( fntMain, 550, 400, 'Button9: ' + u_BoolToStr( joy_Down( 0, 8 ) ) );
  157. text_Draw( fntMain, 550, 420, 'Button10: ' + u_BoolToStr( joy_Down( 0, 9 ) ) );
  158. text_Draw( fntMain, 550, 440, 'Button11: ' + u_BoolToStr( joy_Down( 0, 10 ) ) );
  159. text_Draw( fntMain, 550, 460, 'Button12: ' + u_BoolToStr( joy_Down( 0, 11 ) ) );
  160. text_Draw( fntMain, 550, 480, 'Button13: ' + u_BoolToStr( joy_Down( 0, 12 ) ) );
  161. text_Draw( fntMain, 550, 500, 'Button14: ' + u_BoolToStr( joy_Down( 0, 13 ) ) );
  162. text_Draw( fntMain, 550, 520, 'Button15: ' + u_BoolToStr( joy_Down( 0, 14 ) ) );
  163. text_Draw( fntMain, 550, 540, 'Button16: ' + u_BoolToStr( joy_Down( 0, 15 ) ) );
  164. end;
  165. {$IfDef OLD_METHODS}
  166. procedure Timer;
  167. begin
  168. if lineAlpha > 5 Then
  169. DEC( lineAlpha, 10 )
  170. else
  171. lineAlpha := 255;
  172. end;
  173. {$EndIf}
  174. procedure KeyMouseEvent;
  175. begin
  176. {$IfDef OLD_METHODS}
  177. // RU: Проверить нажата ли левая кнопка мыши в пределах inputRect и начать отслеживать ввод текста.
  178. // EN: Check if left mouse button was pressed inside inputRect and start to track text input.
  179. if mBClickCanClick( M_BLEFT_CLICK ) and col2d_PointInRect( mouseX, mouseY, inputRect ) Then
  180. begin
  181. trackInput := TRUE;
  182. key_BeginReadText( userInput, 24 );
  183. end;
  184. // RU: Если был нажат Enter прекращаем отслеживать ввод текста.
  185. // EN: Finish to track text input if Enter was pressed.
  186. if key_Press( K_ENTER ) Then
  187. begin
  188. trackInput := FALSE;
  189. key_EndReadText();
  190. end;
  191. // RU: Получаем введённый пользователем текст.
  192. // EN: Get inputted by user text.
  193. if trackInput Then
  194. userInput := key_GetText();
  195. {$Else}
  196. // RU: по нажатию F5 копируем то, что написано в поле ввода
  197. // EN: by pressing F5, copy what is written in the input field
  198. if keysDown[K_F5] then
  199. begin
  200. userInput := GetEditToText(myEdit);
  201. end;
  202. {$EndIf}
  203. end;
  204. Begin
  205. {$IfDef OLD_METHODS}
  206. TimeStart := timer_Add( @Timer, 16, Start );
  207. {$EndIf}
  208. zgl_Reg(SYS_EVENTS, @KeyMouseEvent);
  209. zgl_Reg( SYS_LOAD, @Init );
  210. zgl_Reg( SYS_DRAW, @Draw );
  211. wnd_SetCaption(utf8_Copy('03 - Input'));
  212. zgl_Init();
  213. End.