]> 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 478a2063ff74f71c7921d505134e0a1578ca500c..558c8a4ea7d92176d3cd40b1b31442aeed6e5694 100644 (file)
@@ -4,11 +4,14 @@ 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, 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')
@@ -40,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))
@@ -176,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;
@@ -286,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)
 {
@@ -367,6 +387,7 @@ 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;
@@ -376,7 +397,7 @@ void ListBox_draw(entity me)
                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));
+               me.drawListBoxItem(me, i, absSize, (me.selectedItem == i), (me.highlightedItem == i) ? me.highlightedItemTime : 0);
                y += relSize.y;
        }
        draw_ClearClip();
@@ -396,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