Guest User

Untitled

a guest
Nov 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. void UI_Trace(uint fpar, uint numb, uint* tar) {
  2. byte p = 0;
  3. uint oldebx = 0;
  4. uint par;
  5. __asm {
  6. mov oldebx, ebp
  7. }
  8. for (uint a = 0; a < numb; a++) { //skip numb frames
  9. __asm {
  10. //go up 1 ebx
  11. mov ebx, oldebx
  12. mov ebx, [ebx]
  13. mov oldebx, ebx
  14. }
  15. }
  16. while (p < UI_MAX_EDIT_TEXT_FRAMES) {
  17. __asm {
  18. mov ebx, oldebx
  19. mov ebx, [ebx + 4]
  20. mov par, ebx
  21. }
  22. tar[p] = par;
  23. if (par == fpar) return;
  24. __asm {
  25. mov ebx, oldebx
  26. mov ebx, [ebx]
  27. mov oldebx, ebx
  28. }
  29. p++;
  30. }
  31. }
  32.  
  33. bool UI_Same_Id(uint* left, uint* right) {
  34. for (byte a = 0; a < UI_MAX_EDIT_TEXT_FRAMES; a++) {
  35. if (left[a] != right[a]) return false;
  36. }
  37. return true;
  38. }
  39.  
  40. void UI::GetEditTextId() {
  41. SecureZeroMemory(_activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  42. UI_Trace(drawFuncLoc, 2, _activeEditText);
  43. if (UI_Same_Id(_activeEditText, _lastEditText)) _activeEditTextId++;
  44. else _activeEditTextId = 0;
  45.  
  46. memcpy(_lastEditText, _activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  47. }
  48.  
  49. void UI::PreLoop() {
  50. SecureZeroMemory(_lastEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  51. _activeEditTextId = 0;
  52. }
  53.  
  54. #define _checkdraw assert(UI::CanDraw() && "UI functions can only be called from the Overlay function!");
  55.  
  56. string UI::EditText(float x, float y, float w, float h, float s, Vec4 bcol, string str, Font* font, bool delayed, bool* changed, Vec4 fcol, Vec4 hcol, Vec4 acol) {
  57. _checkdraw;
  58. GetEditTextId();
  59. bool isActive = (UI_Same_Id(_activeEditText, _editingEditText) && (_activeEditTextId == _editingEditTextId));
  60.  
  61. if (changed) *changed = false;
  62.  
  63. if (isActive) {
  64. auto al = font->alignment;
  65. font->Align(ALIGN_MIDLEFT);
  66. if (Editor::onFocus) {
  67. if ((Input::mouse0State == MOUSE_UP && !Rect(x, y, w, h).Inside(Input::mousePos)) || Input::KeyDown(Key_Enter)) {
  68. SecureZeroMemory(_editingEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  69. _activeEditTextId = 0;
  70. if (changed && delayed) *changed = true;
  71. return delayed ? _editTextString : str;
  72. }
  73. if (Input::KeyDown(Key_Escape)) {
  74. SecureZeroMemory(_editingEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  75. _activeEditTextId = 0;
  76. return str;
  77. }
  78. if (!delayed) _editTextString = str;
  79. _editTextCursorPos -= Input::KeyDown(Key_LeftArrow);
  80. _editTextCursorPos += Input::KeyDown(Key_RightArrow);
  81. _editTextCursorPos = clamp(_editTextCursorPos, 0, _editTextString.size());
  82. if (!Input::KeyHold(Key_Shift) && (Input::KeyDown(Key_LeftArrow) || Input::KeyDown(Key_RightArrow))) {
  83. _editTextCursorPos2 = _editTextCursorPos;
  84. _editTextBlinkTime = 0;
  85. }
  86. auto ssz = Input::inputString.size();
  87. if (ssz) {
  88. if (_editTextCursorPos == _editTextCursorPos2) {
  89. _editTextString = _editTextString.substr(0, _editTextCursorPos) + Input::inputString + _editTextString.substr(_editTextCursorPos);
  90. _editTextCursorPos += ssz;
  91. _editTextCursorPos2 += ssz;
  92. }
  93. else {
  94. _editTextString = _editTextString.substr(0, min(_editTextCursorPos, _editTextCursorPos2)) + Input::inputString + _editTextString.substr(max(_editTextCursorPos, _editTextCursorPos2));
  95. _editTextCursorPos = min(_editTextCursorPos, _editTextCursorPos2) + ssz;
  96. _editTextCursorPos2 = _editTextCursorPos;
  97. }
  98. if (changed) *changed = true;
  99. _editTextBlinkTime = 0;
  100. }
  101. if (Input::KeyDown(Key_Backspace)) {
  102. if (_editTextCursorPos == _editTextCursorPos2) {
  103. _editTextString = _editTextString.substr(0, _editTextCursorPos - 1) + _editTextString.substr(_editTextCursorPos);
  104. if (!!_editTextCursorPos) {
  105. _editTextCursorPos--;
  106. _editTextCursorPos2--;
  107. }
  108. }
  109. else {
  110. _editTextString = _editTextString.substr(0, min(_editTextCursorPos, _editTextCursorPos2)) + _editTextString.substr(max(_editTextCursorPos, _editTextCursorPos2));
  111. _editTextCursorPos = min(_editTextCursorPos, _editTextCursorPos2);
  112. _editTextCursorPos2 = _editTextCursorPos;
  113. }
  114. if (changed) *changed = true;
  115. _editTextBlinkTime = 0;
  116. }
  117. if (Input::KeyDown(Key_Delete) && _editTextCursorPos < _editTextString.size()) {
  118. _editTextString = _editTextString.substr(0, _editTextCursorPos) + _editTextString.substr(_editTextCursorPos + 1);
  119. if (changed) *changed = true;
  120. _editTextBlinkTime = 0;
  121. }
  122. }
  123. Engine::DrawQuad(x, y, w, h, black());
  124. Engine::DrawQuad(x + 1, y + 1, w - 2, h - 2, white());
  125. UI::Label(x + 2, y + 0.4f*h, s, _editTextString, font);
  126. float xp;
  127. if (!_editTextCursorPos) xp = x + 2;
  128. else xp = font->poss[_editTextCursorPos * 4].x*Display::width;
  129. float xp2;
  130. if (!_editTextCursorPos2) xp2 = x + 2;
  131. else xp2 = font->poss[_editTextCursorPos2 * 4].x*Display::width;
  132. if (_editTextCursorPos != _editTextCursorPos2) {
  133. Engine::DrawQuad(xp, y + 2, xp2 - xp, h - 4, hcol);
  134. UI::Label(min(xp, xp2), y + 0.4f*h, s, _editTextString.substr(min(_editTextCursorPos, _editTextCursorPos2), abs(_editTextCursorPos - _editTextCursorPos2)), font, acol);
  135. }
  136. _editTextBlinkTime += Time::delta;
  137. if (fmod(_editTextBlinkTime, 1) < 0.5f) Engine::DrawLine(Vec2(xp, y + 2), Vec2(xp, y + h - 2), (_editTextCursorPos == _editTextCursorPos2)? black() : white(), 1);
  138. font->Align(al);
  139. return delayed ? str : _editTextString;
  140. }
  141. else if(Engine::Button(x, y, w, h, bcol, str, s, font, fcol, false) == MOUSE_RELEASE) {
  142. memcpy(_editingEditText, _activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
  143. _editingEditTextId = _activeEditTextId;
  144. _editTextCursorPos = str.size();
  145. _editTextCursorPos2 = 0;
  146. _editTextBlinkTime = 0;
  147. if (delayed) _editTextString = str;
  148. }
  149. return str;
  150. }
Add Comment
Please, Sign In to add comment