Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
guiButtonKey.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include "guiButton.h"
7#include "client/keycode.h"
8#include "util/string.h"
9#include "gettext.h"
10
11
12class GUIButtonKey : public GUIButton
13{
15
16public:
18 GUIButtonKey(gui::IGUIEnvironment *environment, gui::IGUIElement *parent,
19 s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
20 bool noclip = false)
21 : GUIButton(environment, parent, id, rectangle, tsrc, noclip) {}
22
24 virtual void setText(const wchar_t *text) override
25 {
26 setKey(wide_to_utf8(text));
27 }
28
30 virtual const wchar_t *getText() const override
31 {
32 return keysym.c_str();
33 }
34
36 static GUIButtonKey *addButton(gui::IGUIEnvironment *environment,
37 const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
38 IGUIElement *parent, s32 id, const wchar_t *text = L"",
39 const wchar_t *tooltiptext = L"");
40
42 virtual bool OnEvent(const SEvent &event) override;
43
44private:
45 void sendKey();
46
49 {
50 if (nostart) {
51 nostart = false;
52 return;
53 }
54 capturing = true;
55 super::setText(wstrgettext("Press Button").c_str());
56 }
57
59 // inhibit_restart: whether the next call to startCapture should be inhibited
60 void cancelCapture(bool inhibit_restart = false)
61 {
62 capturing = false;
63 nostart |= inhibit_restart;
65 }
66
68 void setKey(KeyPress key);
69
70 bool capturing = false;
71 bool nostart = false;
73 std::wstring keysym;
74};
Definition guiButtonKey.h:13
GUIButtonKey(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id, core::rect< s32 > rectangle, ISimpleTextureSource *tsrc, bool noclip=false)
Constructor.
Definition guiButtonKey.h:18
virtual const wchar_t * getText() const override
Gets the value for the key field.
Definition guiButtonKey.h:30
void startCapture()
Start key capture.
Definition guiButtonKey.h:48
virtual void setText(const wchar_t *text) override
Sets the text for the key field.
Definition guiButtonKey.h:24
virtual bool OnEvent(const SEvent &event) override
Called if an event happened.
Definition guiButtonKey.cpp:43
static GUIButtonKey * addButton(gui::IGUIEnvironment *environment, const core::rect< s32 > &rectangle, ISimpleTextureSource *tsrc, IGUIElement *parent, s32 id, const wchar_t *text=L"", const wchar_t *tooltiptext=L"")
Do not drop returned handle.
Definition guiButtonKey.cpp:7
void cancelCapture(bool inhibit_restart=false)
Cancel key capture.
Definition guiButtonKey.h:60
std::wstring keysym
Definition guiButtonKey.h:73
void setKey(KeyPress key)
Sets the captured key and stop capturing.
Definition guiButtonKey.cpp:24
bool nostart
Definition guiButtonKey.h:71
KeyPress key_value
Definition guiButtonKey.h:72
bool capturing
Definition guiButtonKey.h:70
void sendKey()
Definition guiButtonKey.cpp:31
Definition guiButton.h:19
virtual void setText(const wchar_t *text) override
Sets the text displayed by the button.
Definition guiButton.cpp:548
GUIButton(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, s32 id, core::rect< s32 > rectangle, ISimpleTextureSource *tsrc, bool noclip=false)
constructor
Definition guiButton.cpp:28
Definition texturesource.h:34
Definition keycode.h:17
std::string name() const
Definition keycode.cpp:322
std::wstring wstrgettext(const char *str)
Definition gettext.h:46
std::string wide_to_utf8(std::wstring_view input)
Definition string.cpp:115
std::wstring utf8_to_wide(std::string_view input)
Definition string.cpp:87