]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/colorpicker.c
Merge branch 'master' into mirceakitsune/nex_reticle
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / colorpicker.c
1 #ifdef INTERFACE
2 CLASS(XonoticColorpicker) EXTENDS(Image)
3         METHOD(XonoticColorpicker, configureXonoticColorpicker, void(entity, entity))
4         METHOD(XonoticColorpicker, mousePress, float(entity, vector))
5         METHOD(XonoticColorpicker, mouseRelease, float(entity, vector))
6         METHOD(XonoticColorpicker, mouseDrag, float(entity, vector))
7         ATTRIB(XonoticColorpicker, controlledTextbox, entity, NULL)
8         ATTRIB(XonoticColorpicker, image, string, SKINGFX_COLORPICKER)
9         ATTRIB(XonoticColorpicker, imagemargin, vector, SKINMARGIN_COLORPICKER)
10         ATTRIB(XonoticColorpicker, focusable, float, 1)
11         METHOD(XonoticColorpicker, focusLeave, void(entity))
12         METHOD(XonoticColorpicker, keyDown, float(entity, float, float, float))
13         METHOD(XonoticColorpicker, draw, void(entity))
14 ENDCLASS(XonoticColorpicker)
15 entity makeXonoticColorpicker(entity theTextbox);
16 #endif
17
18 #ifdef IMPLEMENTATION
19 entity makeXonoticColorpicker(entity theTextbox)
20 {
21         entity me;
22         me = spawnXonoticColorpicker();
23         me.configureXonoticColorpicker(me, theTextbox);
24         return me;
25 }
26
27 void XonoticColorpicker_configureXonoticColorpicker(entity me, entity theTextbox)
28 {
29         me.controlledTextbox = theTextbox;
30         me.configureImage(me, me.image);
31 }
32
33 float XonoticColorpicker_mousePress(entity me, vector coords)
34 {
35         me.mouseDrag(me, coords);
36         return 1;
37 }
38
39 // must match hslimage.c
40 vector hslimage_color(vector v, vector margin)
41 {
42     v_x = (v_x - margin_x) / (1 - 2 * margin_x);
43     v_y = (v_y - margin_y) / (1 - 2 * margin_y);
44     if(v_x < 0) v_x = 0;
45     if(v_y < 0) v_y = 0;
46     if(v_x > 1) v_x = 1;
47     if(v_y > 1) v_y = 1;
48     if(v_y > 0.875) // grey bar
49         return hsl_to_rgb(eZ * v_x);
50     else
51         return hsl_to_rgb(v_x * 6 * eX + eY + v_y / 0.875 * eZ);
52 }
53
54 float XonoticColorpicker_mouseDrag(entity me, vector coords)
55 {
56         float i;
57         for(;;)
58         {
59                 i = me.controlledTextbox.cursorPos;
60                 if(i >= 2)
61                 {
62                         if(substring(me.controlledTextbox.text, i-2, 1) == "^")
63                                 if(strstrofs("0123456789", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
64                                 {
65                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
66                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
67                                         continue;
68                                 }
69                 }
70
71                 if(i >= 5)
72                 {
73                         if(substring(me.controlledTextbox.text, i-5, 2) == "^x")
74                                 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-3, 1), 0) >= 0)
75                                         if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-2, 1), 0) >= 0)
76                                                 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
77                                                 {
78                                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
79                                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
80                                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
81                                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
82                                                         me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
83                                                         continue;
84                                                 }
85                 }
86                 break;
87         }
88
89         vector margin;
90         margin = me.imagemargin;
91         if(coords_x >= margin_x)
92         if(coords_y >= margin_y)
93         if(coords_x <= 1 - margin_x)
94         if(coords_y <= 1 - margin_y)
95                 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
96
97         return 1;
98 }
99
100 float XonoticColorpicker_mouseRelease(entity me, vector coords)
101 {
102         me.mouseDrag(me, coords);
103         return 1;
104 }
105
106 void XonoticColorpicker_focusLeave(entity me)
107 {
108         me.controlledTextbox.saveCvars(me.controlledTextbox);
109 }
110 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
111 {
112         return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
113 }
114 void XonoticColorpicker_draw(entity me)
115 {
116         SUPER(XonoticColorpicker).draw(me);
117
118         float B, C, aC;
119         C = cvar("r_textcontrast");
120         B = cvar("r_textbrightness");
121
122         // for this to work, C/(1-B) must be in 0..1
123         // B must be < 1
124         // C must be < 1-B
125         
126         B = bound(0, B, 1);
127         C = bound(0, C, 1-B);
128
129         aC = 1 - C / (1 - B);
130
131         draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
132         draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);
133 }
134 #endif