MCPcopy Create free account
hub / github.com/ddnet/ddnet / DoKeyReader

Method DoKeyReader

src/game/client/components/key_binder.cpp:34–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32}
33
34CKeyBinder::CKeyReaderResult CKeyBinder::DoKeyReader(CButtonContainer *pReaderButton, CButtonContainer *pClearButton, const CUIRect *pRect, const CBindSlot &CurrentBind, bool Activate)
35{
36 CKeyReaderResult Result = {CurrentBind, false};
37
38 CUIRect KeyReaderButton, ClearButton;
39 pRect->VSplitRight(pRect->h, &KeyReaderButton, &ClearButton);
40
41 const int ClearButtonResult = Ui()->DoButton_FontIcon(
42 pClearButton, FontIcon::TRASH,
43 Result.m_Bind == CBindSlot(KEY_UNKNOWN, KeyModifier::NONE) ? 1 : 0,
44 &ClearButton, BUTTONFLAG_LEFT, IGraphics::CORNER_R);
45
46 const int ButtonResult = Ui()->DoButtonLogic(pReaderButton, 0, &KeyReaderButton, BUTTONFLAG_LEFT | BUTTONFLAG_RIGHT);
47 if(ButtonResult == 1 || Activate)
48 {
49 m_pKeyReaderId = pReaderButton;
50 m_TakeKey = true;
51 m_Key = std::nullopt;
52 }
53 else if(ButtonResult == 2 || ClearButtonResult != 0)
54 {
55 Result.m_Bind = CBindSlot(KEY_UNKNOWN, KeyModifier::NONE);
56 }
57
58 if(m_pKeyReaderId == pReaderButton && m_Key.has_value())
59 {
60 if(m_Key.value().m_Key == KEY_ESCAPE)
61 {
62 Result.m_Aborted = true;
63 }
64 else
65 {
66 Result.m_Bind = m_Key.value();
67 }
68 m_pKeyReaderId = nullptr;
69 m_Key = std::nullopt;
70 Ui()->SetActiveItem(nullptr);
71 }
72
73 char aBuf[64];
74 if(m_pKeyReaderId == pReaderButton && m_TakeKey)
75 {
76 str_copy(aBuf, Localize("Press a key…"));
77 }
78 else if(Result.m_Bind.m_Key == KEY_UNKNOWN)
79 {
80 aBuf[0] = '\0';
81 }
82 else
83 {
84 GameClient()->m_Binds.GetKeyBindName(Result.m_Bind.m_Key, Result.m_Bind.m_ModifierMask, aBuf, sizeof(aBuf));
85 }
86
87 const ColorRGBA Color = m_pKeyReaderId == pReaderButton && m_TakeKey ? ColorRGBA(0.0f, 1.0f, 0.0f, 0.4f) : ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f * Ui()->ButtonColorMul(pReaderButton));
88 KeyReaderButton.Draw(Color, IGraphics::CORNER_L, 5.0f);
89 CUIRect Label;
90 KeyReaderButton.HMargin(1.0f, &Label);
91 Ui()->DoLabel(&Label, aBuf, Label.h * CUi::ms_FontmodHeight, TEXTALIGN_MC);

Callers 1

RenderSettingsBindsMethod · 0.80

Calls 14

UiFunction · 0.85
CBindSlotClass · 0.85
LocalizeFunction · 0.85
VSplitRightMethod · 0.80
SetActiveItemMethod · 0.80
GetKeyBindNameMethod · 0.80
ButtonColorMulMethod · 0.80
HMarginMethod · 0.80
DoLabelMethod · 0.80
ColorRGBAClass · 0.70
str_copyFunction · 0.50
DoButton_FontIconMethod · 0.45

Tested by

no test coverage detected