]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/item/listbox.qc
Listbox: highlight item under the cursor
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / listbox.qc
index b92beb3d1f7b954f6cf72193a2aef44b7f3ab70b..558c8a4ea7d92176d3cd40b1b31442aeed6e5694 100644 (file)
@@ -4,12 +4,16 @@ CLASS(ListBox) EXTENDS(Item)
        METHOD(ListBox, configureListBox, void(entity, float, float))
        METHOD(ListBox, draw, void(entity))
        METHOD(ListBox, keyDown, float(entity, float, float, float))
+       METHOD(ListBox, mouseMove, float(entity, vector))
        METHOD(ListBox, mousePress, float(entity, vector))
        METHOD(ListBox, mouseDrag, float(entity, vector))
        METHOD(ListBox, mouseRelease, float(entity, vector))
        METHOD(ListBox, focusLeave, void(entity))
        ATTRIB(ListBox, focusable, float, 1)
-       ATTRIB(ListBox, selectedItem, float, 0)
+       ATTRIB(ListBox, highlightedItem, int, -1)
+       ATTRIB(ListBox, highlightedItemTime, float, 0)
+       ATTRIB(ListBox, allowFocusSound, float, 1)
+       ATTRIB(ListBox, selectedItem, int, 0)
        ATTRIB(ListBox, size, vector, '0 0 0')
        ATTRIB(ListBox, origin, vector, '0 0 0')
        ATTRIB(ListBox, scrollPos, float, 0) // measured in window heights, fixed when needed
@@ -39,7 +43,7 @@ CLASS(ListBox) EXTENDS(Item)
        ATTRIB(ListBox, lastClickedItem, float, -1)
        ATTRIB(ListBox, lastClickedTime, float, 0)
 
-       METHOD(ListBox, drawListBoxItem, void(entity, float, vector, float)) // item number, width/height, selected
+       METHOD(ListBox, drawListBoxItem, void(entity, int, vector, bool, float)) // item number, width/height, isSelected, highlightedTime
        METHOD(ListBox, clickListBoxItem, void(entity, float, vector)) // item number, relative clickpos
        METHOD(ListBox, doubleClickListBoxItem, void(entity, float, vector)) // item number, relative clickpos
        METHOD(ListBox, setSelected, void(entity, float))
@@ -81,7 +85,7 @@ void ListBox_setSelected(entity me, float i)
 void ListBox_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
 {
        SUPER(ListBox).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
-       me.controlWidth = me.scrollbarWidth / absSize_x;
+       me.controlWidth = me.scrollbarWidth / absSize.x;
 }
 void ListBox_configureListBox(entity me, float theScrollbarWidth, float theItemHeight)
 {
@@ -131,7 +135,7 @@ float ListBox_keyDown(entity me, float key, float ascii, float shift)
        {
                float i = me.selectedItem;
                float a = me.getItemHeight(me, i);
-               for(0;;)
+               for (;;)
                {
                        --i;
                        if (i < 0)
@@ -146,7 +150,7 @@ float ListBox_keyDown(entity me, float key, float ascii, float shift)
        {
                float i = me.selectedItem;
                float a = me.getItemHeight(me, i);
-               for(0;;)
+               for (;;)
                {
                        ++i;
                        if (i >= me.nItems)
@@ -175,6 +179,22 @@ float ListBox_keyDown(entity me, float key, float ascii, float shift)
                return 0;
        return 1;
 }
+float ListBox_mouseMove(entity me, vector pos)
+{
+       if(pos_x < 0) return 0;
+       if(pos_y < 0) return 0;
+       if(pos_x >= 1) return 0;
+       if(pos_y >= 1) return 0;
+       if(pos_x < 1 - me.controlWidth)
+       {
+               float x;
+               x = me.highlightedItem;
+               me.highlightedItem = me.getItemAtPos(me, me.scrollPos + pos.y);
+               if(x != me.highlightedItem)
+                       me.highlightedItemTime = time;
+       }
+       return 1;
+}
 float ListBox_mouseDrag(entity me, vector pos)
 {
        float hit;
@@ -184,15 +204,15 @@ float ListBox_mouseDrag(entity me, vector pos)
        if(me.pressed == 1)
        {
                hit = 1;
-               if(pos_x < 1 - me.controlWidth - me.tolerance_y * me.controlWidth) hit = 0;
-               if(pos_y < 0 - me.tolerance_x) hit = 0;
-               if(pos_x >= 1 + me.tolerance_y * me.controlWidth) hit = 0;
-               if(pos_y >= 1 + me.tolerance_x) hit = 0;
+               if(pos.x < 1 - me.controlWidth - me.tolerance.y * me.controlWidth) hit = 0;
+               if(pos.y < 0 - me.tolerance.x) hit = 0;
+               if(pos.x >= 1 + me.tolerance.y * me.controlWidth) hit = 0;
+               if(pos.y >= 1 + me.tolerance.x) hit = 0;
                if(hit)
                {
                        // calculate new pos to v
                        float d;
-                       d = (pos_y - me.pressOffset) / (1 - (me.controlBottom - me.controlTop)) * (me.getTotalHeight(me) - 1);
+                       d = (pos.y - me.pressOffset) / (1 - (me.controlBottom - me.controlTop)) * (me.getTotalHeight(me) - 1);
                        me.scrollPos = me.previousValue + d;
                }
                else
@@ -205,29 +225,29 @@ float ListBox_mouseDrag(entity me, vector pos)
        }
        else if(me.pressed == 2)
        {
-               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos_y));
+               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos.y));
        }
        return 1;
 }
 float ListBox_mousePress(entity me, vector pos)
 {
-       if(pos_x < 0) return 0;
-       if(pos_y < 0) return 0;
-       if(pos_x >= 1) return 0;
-       if(pos_y >= 1) return 0;
+       if(pos.x < 0) return 0;
+       if(pos.y < 0) return 0;
+       if(pos.x >= 1) return 0;
+       if(pos.y >= 1) return 0;
        me.dragScrollPos = pos;
        me.updateControlTopBottom(me);
        me.dragScrollTimer = time;
-       if(pos_x >= 1 - me.controlWidth)
+       if(pos.x >= 1 - me.controlWidth)
        {
                // if hit, set me.pressed, otherwise scroll by one page
-               if(pos_y < me.controlTop)
+               if(pos.y < me.controlTop)
                {
                        // page up
                        me.scrollPos = max(me.scrollPos - 1, 0);
                        me.setSelected(me, min(me.selectedItem, ListBox_getLastFullyVisibleItemAtScrollPos(me, me.scrollPos)));
                }
-               else if(pos_y > me.controlBottom)
+               else if(pos.y > me.controlBottom)
                {
                        // page down
                        me.scrollPos = min(me.scrollPos + 1, me.getTotalHeight(me) - 1);
@@ -236,7 +256,7 @@ float ListBox_mousePress(entity me, vector pos)
                else
                {
                        me.pressed = 1;
-                       me.pressOffset = pos_y;
+                       me.pressOffset = pos.y;
                        me.previousValue = me.scrollPos;
                }
        }
@@ -245,7 +265,7 @@ float ListBox_mousePress(entity me, vector pos)
                // continue doing that while dragging (even when dragging outside). When releasing, forward the click to the then selected item.
                me.pressed = 2;
                // an item has been clicked. Select it, ...
-               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos_y));
+               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos.y));
        }
        return 1;
 }
@@ -261,7 +281,7 @@ float ListBox_mouseRelease(entity me, vector pos)
                me.pressed = 3; // do that here, so setSelected can know the mouse has been released
                // item dragging mode
                // select current one one last time...
-               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos_y));
+               me.setSelected(me, me.getItemAtPos(me, me.scrollPos + pos.y));
                // and give it a nice click event
                if(me.nItems > 0)
                {
@@ -285,6 +305,7 @@ void ListBox_focusLeave(entity me)
        // by a mouse click on an item of the list
        // for example showing a dialog on right click
        me.pressed = 0;
+       me.highlightedItem = -1;
 }
 void ListBox_updateControlTopBottom(entity me)
 {
@@ -322,7 +343,7 @@ void ListBox_updateControlTopBottom(entity me)
                me.controlBottom = min((me.scrollPos + 1) / me.getTotalHeight(me), 1);
 
                float minfactor;
-               minfactor = 2 * me.controlWidth / me.size_y * me.size_x;
+               minfactor = 2 * me.controlWidth / me.size.y * me.size.x;
                f = me.controlBottom - me.controlTop;
                if(f < minfactor) // FIXME good default?
                {
@@ -344,7 +365,7 @@ void ListBox_draw(entity me)
        if(me.pressed == 2)
                me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
        me.updateControlTopBottom(me);
-       fillSize_x = (1 - me.controlWidth);
+       fillSize.x = (1 - me.controlWidth);
        if(me.alphaBG)
                draw_Fill('0 0 0', '0 1 0' + fillSize, me.colorBG, me.alphaBG);
        if(me.controlWidth)
@@ -366,17 +387,18 @@ void ListBox_draw(entity me)
        draw_SetClip();
        oldshift = draw_shift;
        oldscale = draw_scale;
+
        float y;
        i = me.getItemAtPos(me, me.scrollPos);
        y = me.getItemStart(me, i) - me.scrollPos;
-       for(0; i < me.nItems && y < 1; ++i)
+       for (; i < me.nItems && y < 1; ++i)
        {
                draw_shift = boxToGlobal(eY * y, oldshift, oldscale);
                vector relSize = eX * (1 - me.controlWidth) + eY * me.getItemHeight(me, i);
                absSize = boxToGlobalSize(relSize, me.size);
                draw_scale = boxToGlobalSize(relSize, oldscale);
-               me.drawListBoxItem(me, i, absSize, (me.selectedItem == i));
-               y += relSize_y;
+               me.drawListBoxItem(me, i, absSize, (me.selectedItem == i), (me.highlightedItem == i) ? me.highlightedItemTime : 0);
+               y += relSize.y;
        }
        draw_ClearClip();
 
@@ -395,8 +417,8 @@ void ListBox_doubleClickListBoxItem(entity me, float i, vector where)
        // template method
 }
 
-void ListBox_drawListBoxItem(entity me, float i, vector absSize, float selected)
+void ListBox_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, float highlightedTime)
 {
-       draw_Text('0 0 0', sprintf(_("Item %d"), i), eX * (8 / absSize_x) + eY * (8 / absSize_y), (selected ? '0 1 0' : '1 1 1'), 1, 0);
+       draw_Text('0 0 0', sprintf(_("Item %d"), i), eX * (8 / absSize.x) + eY * (8 / absSize.y), (isSelected ? '0 1 0' : '1 1 1'), 1, 0);
 }
 #endif