Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void UI_Trace(uint fpar, uint numb, uint* tar) {
- byte p = 0;
- uint oldebx = 0;
- uint par;
- __asm {
- mov oldebx, ebp
- }
- for (uint a = 0; a < numb; a++) { //skip numb frames
- __asm {
- //go up 1 ebx
- mov ebx, oldebx
- mov ebx, [ebx]
- mov oldebx, ebx
- }
- }
- while (p < UI_MAX_EDIT_TEXT_FRAMES) {
- __asm {
- mov ebx, oldebx
- mov ebx, [ebx + 4]
- mov par, ebx
- }
- tar[p] = par;
- if (par == fpar) return;
- __asm {
- mov ebx, oldebx
- mov ebx, [ebx]
- mov oldebx, ebx
- }
- p++;
- }
- }
- bool UI_Same_Id(uint* left, uint* right) {
- for (byte a = 0; a < UI_MAX_EDIT_TEXT_FRAMES; a++) {
- if (left[a] != right[a]) return false;
- }
- return true;
- }
- void UI::GetEditTextId() {
- SecureZeroMemory(_activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- UI_Trace(drawFuncLoc, 2, _activeEditText);
- if (UI_Same_Id(_activeEditText, _lastEditText)) _activeEditTextId++;
- else _activeEditTextId = 0;
- memcpy(_lastEditText, _activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- }
- void UI::PreLoop() {
- SecureZeroMemory(_lastEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- _activeEditTextId = 0;
- }
- #define _checkdraw assert(UI::CanDraw() && "UI functions can only be called from the Overlay function!");
- 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) {
- _checkdraw;
- GetEditTextId();
- bool isActive = (UI_Same_Id(_activeEditText, _editingEditText) && (_activeEditTextId == _editingEditTextId));
- if (changed) *changed = false;
- if (isActive) {
- auto al = font->alignment;
- font->Align(ALIGN_MIDLEFT);
- if (Editor::onFocus) {
- if ((Input::mouse0State == MOUSE_UP && !Rect(x, y, w, h).Inside(Input::mousePos)) || Input::KeyDown(Key_Enter)) {
- SecureZeroMemory(_editingEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- _activeEditTextId = 0;
- if (changed && delayed) *changed = true;
- return delayed ? _editTextString : str;
- }
- if (Input::KeyDown(Key_Escape)) {
- SecureZeroMemory(_editingEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- _activeEditTextId = 0;
- return str;
- }
- if (!delayed) _editTextString = str;
- _editTextCursorPos -= Input::KeyDown(Key_LeftArrow);
- _editTextCursorPos += Input::KeyDown(Key_RightArrow);
- _editTextCursorPos = clamp(_editTextCursorPos, 0, _editTextString.size());
- if (!Input::KeyHold(Key_Shift) && (Input::KeyDown(Key_LeftArrow) || Input::KeyDown(Key_RightArrow))) {
- _editTextCursorPos2 = _editTextCursorPos;
- _editTextBlinkTime = 0;
- }
- auto ssz = Input::inputString.size();
- if (ssz) {
- if (_editTextCursorPos == _editTextCursorPos2) {
- _editTextString = _editTextString.substr(0, _editTextCursorPos) + Input::inputString + _editTextString.substr(_editTextCursorPos);
- _editTextCursorPos += ssz;
- _editTextCursorPos2 += ssz;
- }
- else {
- _editTextString = _editTextString.substr(0, min(_editTextCursorPos, _editTextCursorPos2)) + Input::inputString + _editTextString.substr(max(_editTextCursorPos, _editTextCursorPos2));
- _editTextCursorPos = min(_editTextCursorPos, _editTextCursorPos2) + ssz;
- _editTextCursorPos2 = _editTextCursorPos;
- }
- if (changed) *changed = true;
- _editTextBlinkTime = 0;
- }
- if (Input::KeyDown(Key_Backspace)) {
- if (_editTextCursorPos == _editTextCursorPos2) {
- _editTextString = _editTextString.substr(0, _editTextCursorPos - 1) + _editTextString.substr(_editTextCursorPos);
- if (!!_editTextCursorPos) {
- _editTextCursorPos--;
- _editTextCursorPos2--;
- }
- }
- else {
- _editTextString = _editTextString.substr(0, min(_editTextCursorPos, _editTextCursorPos2)) + _editTextString.substr(max(_editTextCursorPos, _editTextCursorPos2));
- _editTextCursorPos = min(_editTextCursorPos, _editTextCursorPos2);
- _editTextCursorPos2 = _editTextCursorPos;
- }
- if (changed) *changed = true;
- _editTextBlinkTime = 0;
- }
- if (Input::KeyDown(Key_Delete) && _editTextCursorPos < _editTextString.size()) {
- _editTextString = _editTextString.substr(0, _editTextCursorPos) + _editTextString.substr(_editTextCursorPos + 1);
- if (changed) *changed = true;
- _editTextBlinkTime = 0;
- }
- }
- Engine::DrawQuad(x, y, w, h, black());
- Engine::DrawQuad(x + 1, y + 1, w - 2, h - 2, white());
- UI::Label(x + 2, y + 0.4f*h, s, _editTextString, font);
- float xp;
- if (!_editTextCursorPos) xp = x + 2;
- else xp = font->poss[_editTextCursorPos * 4].x*Display::width;
- float xp2;
- if (!_editTextCursorPos2) xp2 = x + 2;
- else xp2 = font->poss[_editTextCursorPos2 * 4].x*Display::width;
- if (_editTextCursorPos != _editTextCursorPos2) {
- Engine::DrawQuad(xp, y + 2, xp2 - xp, h - 4, hcol);
- UI::Label(min(xp, xp2), y + 0.4f*h, s, _editTextString.substr(min(_editTextCursorPos, _editTextCursorPos2), abs(_editTextCursorPos - _editTextCursorPos2)), font, acol);
- }
- _editTextBlinkTime += Time::delta;
- if (fmod(_editTextBlinkTime, 1) < 0.5f) Engine::DrawLine(Vec2(xp, y + 2), Vec2(xp, y + h - 2), (_editTextCursorPos == _editTextCursorPos2)? black() : white(), 1);
- font->Align(al);
- return delayed ? str : _editTextString;
- }
- else if(Engine::Button(x, y, w, h, bcol, str, s, font, fcol, false) == MOUSE_RELEASE) {
- memcpy(_editingEditText, _activeEditText, UI_MAX_EDIT_TEXT_FRAMES * 4);
- _editingEditTextId = _activeEditTextId;
- _editTextCursorPos = str.size();
- _editTextCursorPos2 = 0;
- _editTextBlinkTime = 0;
- if (delayed) _editTextString = str;
- }
- return str;
- }
Add Comment
Please, Sign In to add comment