This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
forked from sknjpn/SyLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUITextBox.h
54 lines (41 loc) · 1.52 KB
/
GUITextBox.h
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
#pragma once
#include "EasyViewer.h"
#include "GUIText.h"
class GUITextBox : public EasyViewer {
size_t m_cursorPos;
bool m_isSelected;
std::function<void(const String&)> m_functionOnChanged;
String& getText() { return getChildViewer<GUIText>()->m_text; }
public:
// Set
std::shared_ptr<EasyViewer> setText(const String& text) {
getChildViewer<GUIText>()->m_text = text;
return shared_from_this();
}
std::shared_ptr<EasyViewer> setFunctionOnChanged(std::function<void(const String&)> functionOnChanged) {
m_functionOnChanged = functionOnChanged;
return shared_from_this();
}
std::shared_ptr<EasyViewer> setFont(const Font& font) {
getChildViewer<GUIText>()->m_font = font;
return shared_from_this();
}
// Get
const String& getText() const { return getChildViewer<GUIText>()->m_text; }
void init() override {
addChildViewer<GUIText>(U"", Font(16), GUIText::Mode::DrawLeftCenter);
}
void update() override {
RectF(getViewerSize()).draw(Palette::White).drawFrame(2.0, 0.0, Palette::Black);
// 枠が押されたら有効化
if (MouseL.down()) m_isSelected = isMouseover();
// 入力
if (m_isSelected) {
auto newCursorPos = TextInput::UpdateText(getText(), m_cursorPos, TextInputMode::AllowBackSpace);
if (newCursorPos != m_cursorPos && m_functionOnChanged) m_functionOnChanged(getText());
m_cursorPos = newCursorPos;
}
// 位置調整
getChildViewer<GUIText>()->setViewerRectInLocal(RectF(getViewerSize()).stretched(-2));
}
};