]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/inputbox.qc
eb07f5d18182f37da110945713cfb744d2abf612
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / inputbox.qc
1 #ifndef INPUTBOX_H
2 #define INPUTBOX_H
3 CLASS(XonoticInputBox, InputBox)
4         METHOD(XonoticInputBox, configureXonoticInputBox, void(entity, float, string))
5         METHOD(XonoticInputBox, focusLeave, void(entity))
6         METHOD(XonoticInputBox, setText, void(entity, string))
7         ATTRIB(XonoticInputBox, fontSize, float, SKINFONTSIZE_NORMAL)
8         ATTRIB(XonoticInputBox, image, string, SKINGFX_INPUTBOX)
9         ATTRIB(XonoticInputBox, onChange, void(entity, entity), func_null)
10         ATTRIB(XonoticInputBox, onChangeEntity, entity, NULL)
11         ATTRIB(XonoticInputBox, onEnter, void(entity, entity), func_null)
12         ATTRIB(XonoticInputBox, onEnterEntity, entity, NULL)
13         ATTRIB(XonoticInputBox, marginLeft, float, SKINMARGIN_INPUTBOX_CHARS)
14         ATTRIB(XonoticInputBox, marginRight, float, SKINMARGIN_INPUTBOX_CHARS)
15         ATTRIB(XonoticInputBox, color, vector, SKINCOLOR_INPUTBOX_N)
16         ATTRIB(XonoticInputBox, colorF, vector, SKINCOLOR_INPUTBOX_F)
17
18         ATTRIB(XonoticInputBox, alpha, float, SKINALPHA_TEXT)
19
20         // Clear button attributes
21         ATTRIB(XonoticInputBox, cb_offset, float, SKINOFFSET_CLEARBUTTON) // bound to range -1, 0
22         ATTRIB(XonoticInputBox, cb_src, string, SKINGFX_CLEARBUTTON)
23         ATTRIB(XonoticInputBox, cb_color, vector, SKINCOLOR_CLEARBUTTON_N)
24         ATTRIB(XonoticInputBox, cb_colorF, vector, SKINCOLOR_CLEARBUTTON_F)
25         ATTRIB(XonoticInputBox, cb_colorC, vector, SKINCOLOR_CLEARBUTTON_C)
26
27         ATTRIB(XonoticInputBox, cvarName, string, string_null)
28         METHOD(XonoticInputBox, loadCvars, void(entity))
29         METHOD(XonoticInputBox, saveCvars, void(entity))
30         ATTRIB(XonoticInputBox, sendCvars, float, 0)
31         METHOD(XonoticInputBox, keyDown, float(entity, float, float, float))
32
33         ATTRIB(XonoticInputBox, saveImmediately, float, 0)
34 ENDCLASS(XonoticInputBox)
35 entity makeXonoticInputBox(float, string);
36 #endif
37
38 #ifdef IMPLEMENTATION
39 entity makeXonoticInputBox(float doEditColorCodes, string theCvar)
40 {
41         entity me;
42         me = NEW(XonoticInputBox);
43         me.configureXonoticInputBox(me, doEditColorCodes, theCvar);
44         return me;
45 }
46 void XonoticInputBox_configureXonoticInputBox(entity me, float doEditColorCodes, string theCvar)
47 {
48         me.configureInputBox(me, "", 0, me.fontSize, me.image);
49         me.editColorCodes = doEditColorCodes;
50         if(theCvar)
51         {
52                 me.cvarName = theCvar;
53                 me.tooltip = getZonedTooltipForIdentifier(theCvar);
54                 me.loadCvars(me);
55         }
56         me.cursorPos = strlen(me.text);
57 }
58 void XonoticInputBox_focusLeave(entity me)
59 {
60         me.saveCvars(me);
61 }
62 void XonoticInputBox_setText(entity me, string val)
63 {
64         if(me.text != val)
65         {
66                 SUPER(XonoticInputBox).setText(me, val);
67                 if(me.onChange)
68                         me.onChange(me, me.onChangeEntity);
69                 if(me.saveImmediately)
70                         me.saveCvars(me);
71         }
72         else
73                 SUPER(XonoticInputBox).setText(me, val);
74 }
75 void XonoticInputBox_loadCvars(entity me)
76 {
77         if (!me.cvarName)
78                 return;
79         SUPER(XonoticInputBox).setText(me, cvar_string(me.cvarName));
80 }
81 void XonoticInputBox_saveCvars(entity me)
82 {
83         if (!me.cvarName)
84                 return;
85         cvar_set(me.cvarName, me.text);
86         CheckSendCvars(me, me.cvarName);
87 }
88 float XonoticInputBox_keyDown(entity me, float key, float ascii, float shift)
89 {
90         float r;
91         r = 0;
92         if(key == K_ENTER || key == K_KP_ENTER)
93         {
94                 if(me.cvarName)
95                 {
96                         me.saveCvars(me);
97                         r = 1;
98                 }
99                 if(me.onEnter)
100                         me.onEnter(me, me.onEnterEntity);
101         }
102         if(SUPER(XonoticInputBox).keyDown(me, key, ascii, shift))
103                 r = 1;
104         return r;
105 }
106 #endif