3 #include "../item/image.qc"
4 CLASS(XonoticColorpicker, Image)
5 METHOD(XonoticColorpicker, configureXonoticColorpicker, void(entity, entity));
6 METHOD(XonoticColorpicker, mousePress, float(entity, vector));
7 METHOD(XonoticColorpicker, mouseRelease, float(entity, vector));
8 METHOD(XonoticColorpicker, mouseDrag, float(entity, vector));
9 ATTRIB(XonoticColorpicker, controlledTextbox, entity, NULL)
10 ATTRIB(XonoticColorpicker, image, string, SKINGFX_COLORPICKER)
11 ATTRIB(XonoticColorpicker, imagemargin, vector, SKINMARGIN_COLORPICKER)
12 ATTRIB(XonoticColorpicker, focusable, float, 1)
13 METHOD(XonoticColorpicker, focusLeave, void(entity));
14 METHOD(XonoticColorpicker, keyDown, float(entity, float, float, float));
15 METHOD(XonoticColorpicker, draw, void(entity));
16 ENDCLASS(XonoticColorpicker)
17 entity makeXonoticColorpicker(entity theTextbox);
21 entity makeXonoticColorpicker(entity theTextbox)
24 me = NEW(XonoticColorpicker);
25 me.configureXonoticColorpicker(me, theTextbox);
29 void XonoticColorpicker_configureXonoticColorpicker(entity me, entity theTextbox)
31 me.controlledTextbox = theTextbox;
32 me.configureImage(me, me.image);
35 float XonoticColorpicker_mousePress(entity me, vector coords)
37 me.mouseDrag(me, coords);
41 // must match hslimage.c
42 vector hslimage_color(vector v, vector margin)
44 v_x = (v.x - margin.x) / (1 - 2 * margin.x);
45 v_y = (v.y - margin.y) / (1 - 2 * margin.y);
50 if(v.y > 0.875) // grey bar
51 return hsl_to_rgb(eZ * v.x);
53 return hsl_to_rgb(v.x * 6 * eX + eY + v.y / 0.875 * eZ);
56 vector color_hslimage(vector v, vector margin)
70 pos_x = margin.x + pos.x * (1 - 2 * margin.x);
71 pos_y = margin.y + pos.y * (1 - 2 * margin.y);
75 float XonoticColorpicker_mouseDrag(entity me, vector coords)
80 i = me.controlledTextbox.cursorPos;
83 if(substring(me.controlledTextbox.text, i-2, 1) == "^")
86 while (i - 2 - carets >= 0 && substring(me.controlledTextbox.text, i - 2 - carets, 1) == "^")
89 if(strstrofs("0123456789", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
91 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
92 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
100 if(substring(me.controlledTextbox.text, i-5, 2) == "^x")
103 while (i - 5 - carets >= 0 && substring(me.controlledTextbox.text, i - 5 - carets, 1) == "^")
106 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-3, 1), 0) >= 0)
107 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-2, 1), 0) >= 0)
108 if(strstrofs("0123456789abcdefABCDEF", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
110 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
111 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
112 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
113 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
114 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
122 if(substring(me.controlledTextbox.text, i-1, 1) == "^")
125 while (i - 1 - carets >= 0 && substring(me.controlledTextbox.text, i - 1 - carets, 1) == "^")
128 me.controlledTextbox.enterText(me.controlledTextbox, "^"); // escape previous caret
132 margin = me.imagemargin;
133 if(coords.x >= margin.x)
134 if(coords.y >= margin.y)
135 if(coords.x <= 1 - margin.x)
136 if(coords.y <= 1 - margin.y)
137 me.controlledTextbox.enterText(me.controlledTextbox, rgb_to_hexcolor(hslimage_color(coords, margin)));
142 float XonoticColorpicker_mouseRelease(entity me, vector coords)
144 m_play_click_sound(MENU_SOUND_SLIDE);
145 me.mouseDrag(me, coords);
149 void XonoticColorpicker_focusLeave(entity me)
151 me.controlledTextbox.saveCvars(me.controlledTextbox);
153 float XonoticColorpicker_keyDown(entity me, float key, float ascii, float shift)
155 return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
157 void XonoticColorpicker_draw(entity me)
159 SUPER(XonoticColorpicker).draw(me);
162 C = cvar("r_textcontrast");
163 B = cvar("r_textbrightness");
165 // for this to work, C/(1-B) must be in 0..1
170 C = bound(0, C, 1-B);
172 aC = 1 - C / (1 - B);
174 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, '0 0 0', aC);
175 draw_Picture(me.imgOrigin, strcat(me.src, "_m"), me.imgSize, me.color, B);