2 CLASS(NexuizColorpicker) EXTENDS(Image)
3 METHOD(NexuizColorpicker, configureNexuizColorpicker, void(entity, entity))
4 METHOD(NexuizColorpicker, mousePress, float(entity, vector))
5 METHOD(NexuizColorpicker, mouseRelease, float(entity, vector))
6 METHOD(NexuizColorpicker, mouseDrag, float(entity, vector))
7 ATTRIB(NexuizColorpicker, controlledTextbox, entity, NULL)
8 ATTRIB(NexuizColorpicker, image, string, SKINGFX_COLORPICKER)
9 ATTRIB(NexuizColorpicker, imagemargin, vector, SKINMARGIN_COLORPICKER)
10 ATTRIB(NexuizColorpicker, focusable, float, 1)
11 METHOD(NexuizColorpicker, focusLeave, void(entity))
12 METHOD(NexuizColorpicker, keyDown, float(entity, float, float, float))
13 METHOD(NexuizColorpicker, draw, void(entity))
14 ENDCLASS(NexuizColorpicker)
15 entity makeNexuizColorpicker(entity theTextbox);
19 entity makeNexuizColorpicker(entity theTextbox)
22 me = spawnNexuizColorpicker();
23 me.configureNexuizColorpicker(me, theTextbox);
27 void configureNexuizColorpickerNexuizColorpicker(entity me, entity theTextbox)
29 me.controlledTextbox = theTextbox;
30 me.configureImage(me, me.image);
33 float mousePressNexuizColorpicker(entity me, vector coords)
35 me.mouseDrag(me, coords);
39 // must match hslimage.c
40 vector hslimage_color(vector v, vector margin)
42 v_x = (v_x - margin_x) / (1 - 2 * margin_x);
43 v_y = (v_y - margin_y) / (1 - 2 * margin_y);
48 if(v_y > 0.875) // grey bar
49 return hsl_to_rgb(eZ * v_x);
51 return hsl_to_rgb(v_x * 6 * eX + eY + v_y / 0.875 * eZ);
54 float mouseDragNexuizColorpicker(entity me, vector coords)
59 i = me.controlledTextbox.cursorPos;
62 if(substring(me.controlledTextbox.text, i-2, 1) == "^")
63 if(strstrofs("0123456789", substring(me.controlledTextbox.text, i-1, 1), 0) >= 0)
65 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
66 me.controlledTextbox.keyDown(me.controlledTextbox, K_BACKSPACE, 8, 0);
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)
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);
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)));
100 float mouseReleaseNexuizColorpicker(entity me, vector coords)
102 me.mouseDrag(me, coords);
106 void focusLeaveNexuizColorpicker(entity me)
108 me.controlledTextbox.saveCvars(me.controlledTextbox);
110 float keyDownNexuizColorpicker(entity me, float key, float ascii, float shift)
112 return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
114 void drawNexuizColorpicker(entity me)
119 C = cvar("r_textcontrast");
120 B = cvar("r_textbrightness");
122 // for this to work, C/(1-B) must be in 0..1
127 C = bound(0, C, 1-B);
129 aC = 1 - C / (1 - B);
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);