File: LineEdit.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (155 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "LineEdit.h"

#include "Gui.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/Fonts/glFont.h"
#include "System/Misc/SpringTime.h"


namespace agui
{

LineEdit::LineEdit(GuiElement* parent)
	: GuiElement(parent)
	, hasFocus(false)
	, crypt(false)
	, cursorPos(0)
{
}

void LineEdit::SetFocus(bool focus)
{
	hasFocus = focus;
}

void LineEdit::SetCrypt(bool focus)
{
	crypt = focus;
}

void LineEdit::SetContent(const std::string& line, bool moveCursor)
{
	content = line;
	if (!moveCursor)
		return;
	cursorPos = content.size();
}

void LineEdit::DrawSelf()
{
	gui->SetDrawMode(Gui::DrawMode::COLOR);
	const float opacity = Opacity();
	gui->SetColor(1.0f, 1.0f, 1.0f, opacity);
	DrawBox();

	if (hasFocus) {
		gui->SetColor(0.0f, 0.0f, 0.0f, opacity);
		DrawOutline();
	} else {
		gui->SetColor(0.5f, 0.5f, 0.5f, opacity);
		DrawOutline();
	}

	std::string tempText;
	if (crypt) {
		tempText.resize(content.size(), '*');
	} else {
		tempText = content;
	}

	const float textCenter = pos[1] + size[1]/2;
	if (hasFocus) {
		// draw the caret
		const std::string caretStr = tempText.substr(0, cursorPos);
		float caretWidth = font->GetSize() * font->GetTextWidth(caretStr) / float(screensize[0]);

		char c = tempText[cursorPos];
		if (c == 0) { c = ' '; }

		float cursorHeight = font->GetSize() * font->GetLineHeight() / float(screensize[1]);
		float cw = font->GetSize() * font->GetCharacterWidth(c) /float(screensize[0]);
		float csx = pos[0] + 0.01 + caretWidth;
		float f = 0.5f * (1.0f + fastmath::sin(spring_now().toMilliSecsf() * 0.015f));
		gui->SetColor(f, f, f, opacity);
		glRectf(csx, textCenter + cursorHeight/2, csx + cw, textCenter - cursorHeight/2);
		gui->SetColor(0.0f, 0.0f, 0.0f, 1.0f); // black
	}

	gui->SetDrawMode(Gui::DrawMode::FONT);
	font->SetTextColor(); //default
	font->SetTextDepth(depth);
	font->glPrint(pos[0] + 0.01, textCenter, 1.0, FONT_VCENTER | FONT_SCALE | FONT_NORM | FONT_BUFFERED, tempText);
	font->DrawBufferedGL4(gui->GetShader());
}

bool LineEdit::HandleEventSelf(const SDL_Event& ev)
{
	switch (ev.type) {
		case SDL_MOUSEBUTTONDOWN: {
			if (MouseOver(ev.button.x, ev.button.y)) {
				hasFocus = true;
			} else {
				hasFocus = false;
			}
			break;
		}
		case SDL_TEXTINPUT: {
			content.insert(cursorPos, ev.text.text);
			cursorPos+=strlen(ev.text.text);
		} break;
		case SDL_KEYDOWN: {
			if (!hasFocus) {
				break;
			}
			switch(ev.key.keysym.sym)
			{
				case SDLK_BACKSPACE: {
					if (cursorPos > 0) {
						content.erase(--cursorPos, 1);
					}
					break;
				}
				case SDLK_DELETE: {
					if (cursorPos < content.size()) {
						content.erase(cursorPos, 1);
					}
					break;
				}
				case SDLK_ESCAPE: {
					hasFocus = false;
					break;
				}
				case SDLK_RIGHT: {
					if (cursorPos < content.size()) {
						++cursorPos;
					}
					break;
				}
				case SDLK_LEFT: {
					if (cursorPos > 0) {
						--cursorPos;
					}
					break;
				}
				case SDLK_HOME: {
					cursorPos = 0;
					break;
				}
				case SDLK_END: {
					cursorPos = content.size();
					break;
				}
				case SDLK_RETURN: {
					DefaultAction.emit();
					return true;
				}
			}
			break;
		}
	}
	return false;
}

} // namespace agui