]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/colorpicker.qc
Menu, player name editing: make changing an existing color much easier by detecting...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / colorpicker.qc
1 #include "colorpicker.qh"
2
3 #include "inputbox.qh"
4
5 entity makeXonoticColorpicker(entity theTextbox)
6 {
7         entity me;
8         me = NEW(XonoticColorpicker);
9         me.configureXonoticColorpicker(me, theTextbox);
10         return me;
11 }
12
13 void XonoticColorpicker_configureXonoticColorpicker(entity me, entity theTextbox)
14 {
15         me.controlledTextbox = theTextbox;
16         me.configureImage(me, me.image);
17 }
18
19 METHOD(XonoticColorpicker, mousePress, bool(XonoticColorpicker this, vector pos))
20 {
21         this.mouseDrag(this, pos);
22         return true;
23 }
24
25 // must match hslimage.c
26 vector hslimage_color(vector v, vector margin)
27 {
28     v_x = (v.x - margin.x) / (1 - 2 * margin.x);
29     v_y = (v.y - margin.y) / (1 - 2 * margin.y);
30     if(v.x < 0) v_x = 0;
31     if(v.y < 0) v_y = 0;
32     if(v.x > 1) v_x = 1;
33     if(v.y > 1) v_y = 1;
34     if(v.y > 0.875) // grey bar
35         return hsl_to_rgb(eZ * v.x);
36     else
37         return hsl_to_rgb(v.x * 6 * eX + eY + v.y / 0.875 * eZ);
38 }
39
40 vector color_hslimage(vector v, vector margin)
41 {
42         vector pos = '0 0 0';
43         v = rgb_to_hsl(v);
44         if (v.y)
45         {
46                 pos_x = v.x / 6;
47                 pos_y = v.z * 0.875;
48         }
49         else // grey scale
50         {
51                 pos_x = v.z;
52                 pos_y = 0.875 + 0.07;
53         }
54         pos_x = margin.x + pos.x * (1 - 2 * margin.x);
55         pos_y = margin.y + pos.y * (1 - 2 * margin.y);
56         return pos;
57 }
58
59 float XonoticColorpicker_mouseDrag(entity me, vector coords)
60 {
61         int i;
62         for (;;)
63         {
64                 i = me.controlledTextbox.cursorPos;
65
66                 int res = checkColorCode(me.controlledTextbox.text, i);
67                 if (res)
68                 {
69                         int tag_length = floor(res / 10);
70                         int ofs = res % 10;
71                         for (int j = tag_length - ofs; j > 0; j--)
72                                 me.controlledTextbox.keyDown(me.controlledTextbox, K_RIGHTARROW, 8, 0);
73                         for (int j = tag_length; j > 0; j--)
74                                 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
75                         continue;
76                 }
77
78                 break;
79         }
80
81         if(substring(me.controlledTextbox.text, i-1, 1) == "^")
82         {
83                 if(!isCaretEscaped(me.controlledTextbox.text, i-1))
84                         me.controlledTextbox.enterText(me.controlledTextbox, "^"); // escape previous caret
85         }
86
87         vector margin;
88         margin = me.imagemargin;
89         if(coords.x >= margin.x)
90         if(coords.y >= margin.y)
91         if(coords.x <= 1 - margin.x)
92         if(coords.y <= 1 - margin.y)
93                 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
94
95         return 1;
96 }
97
98 float XonoticColorpicker_mouseRelease(entity me, vector coords)
99 {
100         m_play_click_sound(MENU_SOUND_SLIDE);
101         me.mouseDrag(me, coords);
102         return 1;
103 }
104
105 void XonoticColorpicker_focusLeave(entity me)
106 {
107         me.controlledTextbox.saveCvars(me.controlledTextbox);
108 }
109 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
110 {
111         return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
112 }
113 void XonoticColorpicker_draw(entity me)
114 {
115         SUPER(XonoticColorpicker).draw(me);
116
117         float B, C, aC;
118         C = cvar("r_textcontrast");
119         B = cvar("r_textbrightness");
120
121         // for this to work, C/(1-B) must be in 0..1
122         // B must be < 1
123         // C must be < 1-B
124
125         B = bound(0, B, 1);
126         C = bound(0, C, 1-B);
127
128         aC = 1 - C / (1 - B);
129
130         draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
131         draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);
132 }