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