]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/item/button.qc
Merge branch 'master' into DefaultUser/func_button_relay
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / button.qc
index 934e8cd3426e0579ff5066fea55349e2c3760dd8..77e4ccad264fee8f912abdb8573ba154af71289c 100644 (file)
-#ifndef ITEM_BUTTON_H
-#define ITEM_BUTTON_H
-#include "label.qc"
-CLASS(Button, Label)
-       METHOD(Button, configureButton, void(entity, string, float, string));
-       METHOD(Button, draw, void(entity));
-       METHOD(Button, showNotify, void(entity));
-       METHOD(Button, resizeNotify, void(entity, vector, vector, vector, vector));
-       METHOD(Button, keyDown, float(entity, float, float, float));
-       METHOD(Button, mousePress, float(entity, vector));
-       METHOD(Button, mouseDrag, float(entity, vector));
-       METHOD(Button, mouseRelease, float(entity, vector));
-       METHOD(Button, playClickSound, void(entity));
-       ATTRIB(Button, onClick, void(entity, entity), func_null)
-       ATTRIB(Button, onClickEntity, entity, NULL)
-       ATTRIB(Button, src, string, string_null)
-       ATTRIB(Button, srcSuffix, string, string_null)
-       ATTRIB(Button, src2, string, string_null) // is centered, same aspect, and stretched to label size
-       ATTRIB(Button, src2scale, float, 1)
-       ATTRIB(Button, srcMulti, float, 1) // 0: button square left, text right; 1: button stretched, text over it
-       ATTRIB(Button, buttonLeftOfText, float, 0)
-       ATTRIB(Button, focusable, float, 1)
-       ATTRIB(Button, allowFocusSound, float, 1)
-       ATTRIB(Button, pressed, float, 0)
-       ATTRIB(Button, clickTime, float, 0)
-       ATTRIB(Button, disabled, float, 0)
-       ATTRIB(Button, disabledAlpha, float, 0.3)
-       ATTRIB(Button, forcePressed, float, 0)
-       ATTRIB(Button, color, vector, '1 1 1')
-       ATTRIB(Button, colorC, vector, '1 1 1')
-       ATTRIB(Button, colorF, vector, '1 1 1')
-       ATTRIB(Button, colorD, vector, '1 1 1')
-       ATTRIB(Button, color2, vector, '1 1 1')
-       ATTRIB(Button, alpha2, float, 1)
+#include "button.qh"
 
-       ATTRIB(Button, origin, vector, '0 0 0')
-       ATTRIB(Button, size, vector, '0 0 0')
-ENDCLASS(Button)
-#endif
+       void Button_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
+       {
+               if (me.srcMulti) me.keepspaceLeft = 0;
+               else me.keepspaceLeft = min(0.8, absSize.x == 0 ? 0 : (absSize.y / absSize.x));
+               SUPER(Button).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
 
-#ifdef IMPLEMENTATION
-void Button_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
-{
-       if(me.srcMulti)
-               me.keepspaceLeft = 0;
-       else
-               me.keepspaceLeft = min(0.8, absSize.x == 0 ? 0 : (absSize.y / absSize.x));
-       SUPER(Button).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
-}
-void Button_configureButton(entity me, string txt, float sz, string gfx)
-{
-       SUPER(Button).configureLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
-       me.src = gfx;
-}
-float Button_keyDown(entity me, float key, float ascii, float shift)
-{
-       if(key == K_ENTER || key == K_SPACE || key == K_KP_ENTER)
+               if(me.disableOnClick)
+                       me.disabled = true; // initially disabled
+       }
+       void Button_configureButton(entity me, string txt, float sz, string gfx)
        {
-               me.playClickSound(me);
-               me.clickTime = 0.1; // delayed for effect
+               SUPER(Button).configureLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
+               me.src = gfx;
+       }
+       float Button_keyDown(entity me, float key, float ascii, float shift)
+       {
+               if (key == K_ENTER || key == K_SPACE || key == K_KP_ENTER)
+               {
+                       if(!me.disabled)
+                       {
+                               me.playClickSound(me);
+                               me.clickTime = 0.1;  // delayed for effect
+                       }
+                       return 1;
+               }
+               return 0;
+       }
+       float Button_mouseDrag(entity me, vector pos)
+       {
+               me.pressed = 1;
+               if (pos.x < 0) me.pressed = 0;
+               if (pos.y < 0) me.pressed = 0;
+               if (pos.x >= 1) me.pressed = 0;
+               if (pos.y >= 1) me.pressed = 0;
                return 1;
        }
-       return 0;
-}
-float Button_mouseDrag(entity me, vector pos)
-{
-       me.pressed = 1;
-       if(pos.x < 0) me.pressed = 0;
-       if(pos.y < 0) me.pressed = 0;
-       if(pos.x >= 1) me.pressed = 0;
-       if(pos.y >= 1) me.pressed = 0;
-       return 1;
-}
-float Button_mousePress(entity me, vector pos)
-{
-       me.mouseDrag(me, pos); // verify coordinates
-       return 1;
-}
-float Button_mouseRelease(entity me, vector pos)
-{
-       me.mouseDrag(me, pos); // verify coordinates
-       if(me.pressed)
+       METHOD(Button, mousePress, bool(Button this, vector pos))
+       {
+               this.mouseDrag(this, pos);  // verify coordinates
+               return true;
+       }
+       float Button_mouseRelease(entity me, vector pos)
        {
-               if (!me.disabled)
+               me.mouseDrag(me, pos);  // verify coordinates
+               if (me.pressed)
                {
-                       me.playClickSound(me);
-                       if(me.onClick)
-                               me.onClick(me, me.onClickEntity);
+                       if (!me.disabled)
+                       {
+                               me.playClickSound(me);
+                               if (me.onClick)
+                               {
+                                       if(me.applyButton)
+                                               me.applyButton.disabled = false;
+                                       me.onClick(me, me.onClickEntity);
+                                       if(me.disableOnClick)
+                                               me.disabled = true;
+                               }
+                       }
+                       me.pressed = 0;
                }
-               me.pressed = 0;
+               return 1;
+       }
+       void Button_showNotify(entity me)
+       {
+               me.focusable = !me.disabled;
        }
-       return 1;
-}
-void Button_showNotify(entity me)
-{
-       me.focusable = !me.disabled;
-}
-void Button_draw(entity me)
-{
-       vector bOrigin, bSize;
-       float save;
+       void Button_draw(entity me)
+       {
+               vector bOrigin, bSize;
+               float save;
 
-       me.focusable = !me.disabled;
+               me.focusable = !me.disabled;
 
-       save = draw_alpha;
-       if(me.disabled)
-               draw_alpha *= me.disabledAlpha;
+               save = draw_alpha;
+               if (me.disabled) draw_alpha *= me.disabledAlpha;
 
-       if(me.src)
-       {
-               if(me.srcMulti)
-               {
-                       bOrigin = '0 0 0';
-                       bSize = '1 1 0';
-                       if(me.disabled)
-                               draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
-                       else if(me.forcePressed || me.pressed || me.clickTime > 0)
-                               draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
-                       else if(me.focused)
-                               draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
-                       else
-                               draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
-               }
-               else
+               if (me.src)
                {
-                       if(me.realFontSize_y == 0)
+                       if (me.srcMulti)
                        {
                                bOrigin = '0 0 0';
                                bSize = '1 1 0';
+                               if (me.disabled) draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
+                               else if (me.forcePressed || me.pressed || me.clickTime > 0) draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
+                               else if (me.focused) draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
+                               else draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
                        }
                        else
                        {
-                               bOrigin = eY * (0.5 * (1 - me.realFontSize.y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize.x));
-                               bSize = me.realFontSize;
+                               if (me.realFontSize_y == 0)
+                               {
+                                       bOrigin = '0 0 0';
+                                       bSize = '1 1 0';
+                               }
+                               else
+                               {
+                                       bOrigin = eY * (0.5 * (1 - me.realFontSize.y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize.x));
+                                       bSize = me.realFontSize;
+                               }
+                               if (me.disabled) draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
+                               else if (me.forcePressed || me.pressed || me.clickTime > 0) draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
+                               else if (me.focused) draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
+                               else draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
                        }
-                       if(me.disabled)
-                               draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
-                       else if(me.forcePressed || me.pressed || me.clickTime > 0)
-                               draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
-                       else if(me.focused)
-                               draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
-                       else
-                               draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
                }
-       }
-       if(me.src2)
-       {
-               bOrigin = me.keepspaceLeft * eX;
-               bSize = eY + eX * (1 - me.keepspaceLeft);
+               if (me.src2)
+               {
+                       bOrigin = me.keepspaceLeft * eX;
+                       bSize = eY + eX * (1 - me.keepspaceLeft);
 
-               bOrigin += bSize * (0.5 - 0.5 * me.src2scale);
-               bSize = bSize * me.src2scale;
+                       bOrigin += bSize * (0.5 - 0.5 * me.src2scale);
+                       bSize = bSize * me.src2scale;
 
-               draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
-       }
+                       draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
+               }
+
+               draw_alpha = save;
 
-       draw_alpha = save;
+               if (me.clickTime > 0 && me.clickTime <= frametime)
+               {
+                       // keyboard click timer expired? Fire the event then.
+                       if (!me.disabled)
+                               if (me.onClick) me.onClick(me, me.onClickEntity);
+               }
+               me.clickTime -= frametime;
 
-       if(me.clickTime > 0 && me.clickTime <= frametime)
+               SUPER(Button).draw(me);
+       }
+       void Dialog_Close(entity button, entity me);
+       void Button_playClickSound(entity me)
        {
-               // keyboard click timer expired? Fire the event then.
-               if (!me.disabled)
-                       if(me.onClick)
-                               me.onClick(me, me.onClickEntity);
+               if (me.onClick == DialogOpenButton_Click) m_play_click_sound(MENU_SOUND_OPEN);
+               else if (me.onClick == Dialog_Close) m_play_click_sound(MENU_SOUND_CLOSE);
+               else m_play_click_sound(MENU_SOUND_EXECUTE);
        }
-       me.clickTime -= frametime;
-
-       SUPER(Button).draw(me);
-}
-void Dialog_Close(entity button, entity me);
-void Button_playClickSound(entity me)
-{
-       if(me.onClick == DialogOpenButton_Click)
-               m_play_click_sound(MENU_SOUND_OPEN);
-       else if(me.onClick == Dialog_Close)
-               m_play_click_sound(MENU_SOUND_CLOSE);
-       else
-               m_play_click_sound(MENU_SOUND_EXECUTE);
-}
-#endif