]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/item/inputbox.c
Merge branch 'master' into terencehill/clear_button
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / inputbox.c
index cb1b32e8b1c9a4225e9e3bb09885b935e3945491..83bb916dad2363dc389fd8db05bb54a6565bdfa2 100644 (file)
@@ -5,10 +5,12 @@ CLASS(InputBox) EXTENDS(Label)
        METHOD(InputBox, setText, void(entity, string))
        METHOD(InputBox, enterText, void(entity, string))
        METHOD(InputBox, keyDown, float(entity, float, float, float))
+       METHOD(InputBox, mouseMove, float(entity, vector))
        METHOD(InputBox, mouseRelease, float(entity, vector))
        METHOD(InputBox, mousePress, float(entity, vector))
        METHOD(InputBox, mouseDrag, float(entity, vector))
        METHOD(InputBox, showNotify, void(entity))
+       METHOD(InputBox, resizeNotify, void(entity, vector, vector, vector, vector))
 
        ATTRIB(InputBox, src, string, string_null)
 
@@ -25,7 +27,13 @@ CLASS(InputBox) EXTENDS(Label)
        ATTRIB(InputBox, forbiddenCharacters, string, "")
        ATTRIB(InputBox, color, vector, '1 1 1')
        ATTRIB(InputBox, colorF, vector, '1 1 1')
-       ATTRIB(InputBox, maxLength, float, 255)
+       ATTRIB(InputBox, maxLength, float, 255) // if negative, it counts bytes, not chars
+
+       ATTRIB(InputBox, enableClearButton, float, 1)
+       ATTRIB(InputBox, clearButton, entity, NULL)
+       ATTRIB(InputBox, cb_width, float, 0)
+       ATTRIB(InputBox, cb_pressed, float, 0)
+       ATTRIB(InputBox, cb_focused, float, 0)
 ENDCLASS(InputBox)
 void InputBox_Clear_Click(entity btn, entity me);
 #endif
@@ -37,6 +45,16 @@ void InputBox_configureInputBox(entity me, string theText, float theCursorPos, f
        me.src = gfx;
        me.cursorPos = theCursorPos;
 }
+void InputBox_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
+{
+       SUPER(InputBox).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
+       if (me.enableClearButton)
+       {
+               me.cb_width = absSize_y / absSize_x;
+               me.cb_offset = bound(-1, me.cb_offset, 0) * me.cb_width; // bound to range -1, 0
+               me.keepspaceRight = me.keepspaceRight - me.cb_offset + me.cb_width;
+       }
+}
 
 void InputBox_setText(entity me, string txt)
 {
@@ -50,18 +68,60 @@ void InputBox_Clear_Click(entity btn, entity me)
        me.setText(me, "");
 }
 
+float over_ClearButton(entity me, vector pos)
+{
+       if (pos_x >= 1 + me.cb_offset - me.cb_width)
+       if (pos_x < 1 + me.cb_offset)
+       if (pos_y >= 0)
+       if (pos_y < 1)
+               return 1;
+       return 0;
+}
+
+float InputBox_mouseMove(entity me, vector pos)
+{
+       if (me.enableClearButton)
+       {
+               if (over_ClearButton(me, pos))
+               {
+                       me.cb_focused = 1;
+                       return 1;
+               }
+               me.cb_focused = 0;
+       }
+       return 1;
+}
+
 float InputBox_mouseDrag(entity me, vector pos)
 {
        float p;
-       me.dragScrollPos = pos;
-       p = me.scrollPos + pos_x - me.keepspaceLeft;
-       me.cursorPos = draw_TextLengthUpToWidth(me.text, p, 0, me.realFontSize);
-       me.lastChangeTime = time;
+       if(me.pressed)
+       {
+               me.dragScrollPos = pos;
+               p = me.scrollPos + pos_x - me.keepspaceLeft;
+               me.cursorPos = draw_TextLengthUpToWidth(me.text, p, 0, me.realFontSize);
+               me.lastChangeTime = time;
+       }
+       else if (me.enableClearButton)
+       {
+               if (over_ClearButton(me, pos))
+               {
+                       me.cb_pressed = 1;
+                       return 1;
+               }
+       }
+       me.cb_pressed = 0;
        return 1;
 }
 
 float InputBox_mousePress(entity me, vector pos)
 {
+       if (me.enableClearButton)
+       if (over_ClearButton(me, pos))
+       {
+               me.cb_pressed = 1;
+               return 1;
+       }
        me.dragScrollTimer = time;
        me.pressed = 1;
        return InputBox_mouseDrag(me, pos);
@@ -69,8 +129,19 @@ float InputBox_mousePress(entity me, vector pos)
 
 float InputBox_mouseRelease(entity me, vector pos)
 {
+       if(me.cb_pressed)
+       if (over_ClearButton(me, pos))
+       {
+               me.cb_pressed = 0;
+               InputBox_Clear_Click(world, me);
+               return 1;
+       }
+       float r = InputBox_mouseDrag(me, pos);
+       //reset cb_pressed after mouseDrag, mouseDrag could set cb_pressed in this case:
+       //mouse press out of the clear button, drag and then mouse release over the clear button
+       me.cb_pressed = 0;
        me.pressed = 0;
-       return InputBox_mouseDrag(me, pos);
+       return r;
 }
 
 void InputBox_enterText(entity me, string ch)
@@ -79,8 +150,16 @@ void InputBox_enterText(entity me, string ch)
        for(i = 0; i < strlen(ch); ++i)
                if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
                        return;
-       if(strlen(ch) + strlen(me.text) > me.maxLength)
-               return;
+       if(me.maxLength > 0)
+       {
+               if(strlen(ch) + strlen(me.text) > me.maxLength)
+                       return;
+       }
+       else if(me.maxLength < 0)
+       {
+               if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
+                       return;
+       }
        me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
        me.cursorPos += strlen(ch);
 }
@@ -96,15 +175,19 @@ float InputBox_keyDown(entity me, float key, float ascii, float shift)
        }
        switch(key)
        {
+               case K_KP_LEFTARROW:
                case K_LEFTARROW:
                        me.cursorPos -= 1;
                        return 1;
+               case K_KP_RIGHTARROW:
                case K_RIGHTARROW:
                        me.cursorPos += 1;
                        return 1;
+               case K_KP_HOME:
                case K_HOME:
                        me.cursorPos = 0;
                        return 1;
+               case K_KP_END:
                case K_END:
                        me.cursorPos = strlen(me.text);
                        return 1;
@@ -115,6 +198,7 @@ float InputBox_keyDown(entity me, float key, float ascii, float shift)
                                me.setText(me, strcat(substring(me.text, 0, me.cursorPos), substring(me.text, me.cursorPos + 1, strlen(me.text) - me.cursorPos - 1)));
                        }
                        return 1;
+               case K_KP_DEL:
                case K_DEL:
                        if(shift & S_CTRL)
                                me.setText(me, "");
@@ -259,35 +343,6 @@ void InputBox_draw(entity me)
                                                draw_Text(p, substring(me.text, i, 2), me.realFontSize, '1 1 1', theAlpha, 0);
                                        }
                                }
-                               /*else if(ch2 == "a") // ^a found
-                               {
-                                       draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
-                                       draw_Text(p, substring(me.text, i, 2), me.realFontSize, theColor, 0.8, 0);
-                                       
-                                       component = str2chr(me.text, i+2);
-                                       if (component >= '0' && component <= '9')
-                                               component = component - '0';
-                                       else if (component >= 'a' && component <= 'f')
-                                               component = component - 87;
-                                       else if (component >= 'A' && component <= 'F')
-                                               component = component - 55;
-                                       else
-                                               component = -1;
-                                       
-                                       if (component >= 0) // ^ah found
-                                       {
-                                               // FIX ME: overflow here
-                                               if (component == 20 && theVariableAlpha <= 0.97)
-                                                       theVariableAlpha = theVariableAlpha + 0.0625;
-                                               else if (component == 30 && theVariableAlpha >= 0.03)
-                                                       theVariableAlpha = theVariableAlpha - 0.0625;
-                                               else
-                                                       theVariableAlpha = component*0.0625;
-                                               
-                                               draw_Fill(p, eX * draw_TextWidth(substring(me.text, i, 3), 0, me.realFontSize) + eY * me.realFontSize_y, '0.8 0.8 0.8', 0.5);
-                                               draw_Text(p, strcat(ch, ch2), me.realFontSize, theColor, 0.8, 0);
-                                       }
-                               }*/
                                else
                                {
                                        draw_Fill(p, eX * w + eY * me.realFontSize_y, '1 1 1', 0.5);
@@ -303,11 +358,25 @@ void InputBox_draw(entity me)
        }
        else
                draw_Text(me.realOrigin - eX * me.scrollPos, me.text, me.realFontSize, '1 1 1', 1, 0);
-               // skipping SUPER(InputBox).draw(me);
+
        if(!me.focused || (time - me.lastChangeTime) < floor(time - me.lastChangeTime) + 0.5)
                draw_Text(me.realOrigin + eX * (cursorPosInWidths - me.scrollPos), CURSOR, me.realFontSize, '1 1 1', 1, 0);
 
        draw_ClearClip();
+
+       if (me.enableClearButton)
+       if (me.text != "")
+       {
+               if(me.focused && me.cb_pressed)
+                       draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_c"), eX * me.cb_width + eY, '1 1 1', 1);
+               else if(me.focused && me.cb_focused)
+                       draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_f"), eX * me.cb_width + eY, '1 1 1', 1);
+               else
+                       draw_Picture(eX * (1 + me.cb_offset - me.cb_width), strcat(me.cb_src, "_n"), eX * me.cb_width + eY, '1 1 1', 1);
+       }
+
+       // skipping SUPER(InputBox).draw(me);
+       Item_draw(me);
 }
 
 void InputBox_showNotify(entity me)