]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/menu/xonotic/crosshairpicker.qc
Listbox / Picker: Implement item fading in a different way so that it gets influenced...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / crosshairpicker.qc
index 9236e61ebaaf5309d76b2895939f273a6651f0bf..1441ebcb31eb18ef6d43edd9cedd12fbbbf82dc4 100644 (file)
@@ -1,40 +1,32 @@
 #ifdef INTERFACE
-CLASS(XonoticCrosshairPicker) EXTENDS(Item)
+CLASS(XonoticCrosshairPicker) EXTENDS(XonoticPicker)
        METHOD(XonoticCrosshairPicker, configureXonoticCrosshairPicker, void(entity))
-       METHOD(XonoticCrosshairPicker, mousePress, float(entity, vector))
-       METHOD(XonoticCrosshairPicker, mouseRelease, float(entity, vector))
-       METHOD(XonoticCrosshairPicker, mouseMove, float(entity, vector))
-       METHOD(XonoticCrosshairPicker, mouseDrag, float(entity, vector))
-       METHOD(XonoticCrosshairPicker, keyDown, float(entity, float, float, float))
-       METHOD(XonoticCrosshairPicker, draw, void(entity))
-       ATTRIB(XonoticCrosshairPicker, focusable, float, 1)
-       ATTRIB(XonoticCrosshairPicker, disabled, float, 0)
-       ATTRIB(XonoticCrosshairPicker, alpha, float, 1)
-       ATTRIB(XonoticCrosshairPicker, disabledAlpha, float, SKINALPHA_DISABLED)
 
-       METHOD(XonoticCrosshairPicker, moveFocus, void(entity, vector, vector))
-       METHOD(XonoticCrosshairPicker, setCrosshair, void(entity))
-       ATTRIB(XonoticCrosshairPicker, realCellSize, vector, '0 0 0')
-       ATTRIB(XonoticCrosshairPicker, focusedCell, vector, '-1 -1 0')
-       ATTRIB(XonoticCrosshairPicker, focusedCellTime, float, 0)
-       ATTRIB(XonoticCrosshairPicker, pressedCell, vector, '-1 -1 0')
+       ATTRIB(XonoticCrosshairPicker, rows, float, 3)
+       ATTRIB(XonoticCrosshairPicker, columns, float, 12)
+
+       METHOD(XonoticCrosshairPicker, cellSelect, void(entity, vector))
+       METHOD(XonoticCrosshairPicker, cellIsValid, bool(entity, vector))
+       METHOD(XonoticCrosshairPicker, cellDraw, void(entity, vector, vector))
 ENDCLASS(XonoticCrosshairPicker)
 entity makeXonoticCrosshairPicker();
 #endif
 
 #ifdef IMPLEMENTATION
 
-const float CROSSHAIRPICKER_COLS = 12;
-const float CROSSHAIRPICKER_ROWS = 3;
-
-string crosshairpicker_cellToCrosshair(vector cell)
+string crosshairpicker_cellToCrosshair(entity me, vector cell)
 {
-       float crosshair = 31 + cell.y * CROSSHAIRPICKER_COLS + cell.x;
-
-       if (crosshair >= 31 && crosshair < 31 + CROSSHAIRPICKER_COLS * CROSSHAIRPICKER_ROWS)
-               return ftos(crosshair);
-       else
+       if(cell.x < 0 || cell.x >= me.columns || cell.y < 0 || cell.y >= me.rows)
                return "";
+       return ftos(31 + cell.y * me.columns + cell.x);
+}
+
+vector crosshairpicker_crosshairToCell(entity me, string crosshair_str)
+{
+       float crosshair = stof(crosshair_str) - 31;
+       if(crosshair - floor(crosshair) > 0)
+               return '-1 -1 0';
+       return mod(crosshair, me.columns) * eX + floor(crosshair / me.columns) * eY;
 }
 
 entity makeXonoticCrosshairPicker()
@@ -47,179 +39,41 @@ entity makeXonoticCrosshairPicker()
 
 void XonoticCrosshairPicker_configureXonoticCrosshairPicker(entity me)
 {
-       me.realCellSize = eX / CROSSHAIRPICKER_COLS + eY / CROSSHAIRPICKER_ROWS;
-}
-
-float XonoticCrosshairPicker_mouseMove(entity me, vector coords)
-{
-       vector prevFocusedCell = me.focusedCell;
-       me.focusedCell_x = floor(coords.x * CROSSHAIRPICKER_COLS);
-       me.focusedCell_y = floor(coords.y * CROSSHAIRPICKER_ROWS);
-
-       if(me.focusedCell.x < 0 || me.focusedCell.y < 0 ||
-          me.focusedCell.x >= CROSSHAIRPICKER_COLS || me.focusedCell.y >= CROSSHAIRPICKER_ROWS)
-       {
-               me.focusedCell = '-1 -1 0';
-               return 0;
-       }
-
-       if(me.focusedCell != prevFocusedCell)
-               me.focusedCellTime = time;
-
-       return 1;
-}
-
-float XonoticCrosshairPicker_mouseDrag(entity me, vector coords)
-{
-       return me.mouseMove(me, coords);
-}
-
-float XonoticCrosshairPicker_mousePress(entity me, vector coords)
-{
-       me.mouseMove(me, coords);
-
-       if(me.focusedCell.x >= 0)
-       {
-               me.pressed = 1;
-               me.pressedCell = me.focusedCell;
-       }
-
-       return 1;
-}
-
-float XonoticCrosshairPicker_mouseRelease(entity me, vector coords)
-{
-       if(!me.pressed)
-               return 0;
-
-       me.mouseMove(me, coords);
-
-       if(me.focusedCell == me.pressedCell)
-               me.setCrosshair(me);
-
-       me.pressed = 0;
-       return 1;
-}
-
-float XonoticCrosshairPicker_keyDown(entity me, float key, float ascii, float shift)
-{
-       switch(key)
-       {
-               case K_LEFTARROW:
-               case K_KP_LEFTARROW:
-                       me.moveFocus(me, me.focusedCell, '-1 0 0');
-                       return 1;
-               case K_RIGHTARROW:
-               case K_KP_RIGHTARROW:
-                       me.moveFocus(me, me.focusedCell, '1 0 0');
-                       return 1;
-               case K_UPARROW:
-               case K_KP_UPARROW:
-                       me.moveFocus(me, me.focusedCell, '0 -1 0');
-                       return 1;
-               case K_DOWNARROW:
-               case K_KP_DOWNARROW:
-                       me.moveFocus(me, me.focusedCell, '0 1 0');
-                       return 1;
-               case K_HOME:
-               case K_KP_HOME:
-                       me.focusedCell = '0 0 0';
-                       return 1;
-               case K_END:
-               case K_KP_END:
-                       me.focusedCell_x = CROSSHAIRPICKER_COLS - 1;
-                       me.focusedCell_y = CROSSHAIRPICKER_ROWS - 1;
-                       return 1;
-               case K_ENTER:
-               case K_KP_ENTER:
-               case K_INS:
-               case K_KP_INS:
-                       me.setCrosshair(me);
-                       return 1;
-       }
-       return 0;
+       me.configureXonoticPicker(me);
+       SUPER(XonoticCrosshairPicker).cellSelect(me, crosshairpicker_crosshairToCell(me, cvar_string("crosshair")));
 }
 
-void XonoticCrosshairPicker_moveFocus(entity me, vector initialCell, vector step)
+void XonoticCrosshairPicker_cellSelect(entity me, vector cell)
 {
-       me.focusedCell_x = mod(me.focusedCell.x + step.x + CROSSHAIRPICKER_COLS, CROSSHAIRPICKER_COLS);
-       me.focusedCell_y = mod(me.focusedCell.y + step.y + CROSSHAIRPICKER_ROWS, CROSSHAIRPICKER_ROWS);
-
-       if(me.focusedCell != initialCell) // Recursion break
-               if(crosshairpicker_cellToCrosshair(me.focusedCell) == "")
-                       me.moveFocus(me, initialCell, step);
+       cvar_set("crosshair", crosshairpicker_cellToCrosshair(me, me.focusedCell));
+       SUPER(XonoticCrosshairPicker).cellSelect(me, me.focusedCell);
 }
 
-void XonoticCrosshairPicker_setCrosshair(entity me)
+bool XonoticCrosshairPicker_cellIsValid(entity me, vector cell)
 {
-       cvar_set("crosshair", crosshairpicker_cellToCrosshair(me.focusedCell));
+       if(crosshairpicker_cellToCrosshair(me, cell) == "")
+               return false;
+       return true;
 }
 
-void XonoticCrosshairPicker_draw(entity me)
+void XonoticCrosshairPicker_cellDraw(entity me, vector cell, vector cellPos)
 {
        vector sz, rgb;
-       float save;
-
-       me.focusable = !me.disabled;
-
-       save = draw_alpha;
-       if(me.disabled)
-               draw_alpha *= me.disabledAlpha;
-
-       string crosshair;
-       vector cell, cellPos, crosshairPos;
-       cell = '0 0 0';
-       cellPos = '0 0 0';
-       crosshairPos = '0 0 0';
-
-
-       for(cell_y = 0; cell.y < CROSSHAIRPICKER_ROWS; ++cell.y)
-       {
-               crosshairPos_y = cell.y / CROSSHAIRPICKER_ROWS + 0.5 * me.realCellSize.y;
-               for(cell_x = 0; cell.x < CROSSHAIRPICKER_COLS; ++cell.x)
-               {
-                       crosshair = crosshairpicker_cellToCrosshair(cell);
-
-                       if(crosshair == "")
-                               continue;
-
-                       // Draw focused cell
-                       if(cell == me.focusedCell && me.focused)
-                       {
-                               if(!me.pressed || me.focusedCell == me.pressedCell)
-                               {
-                                       cellPos_x = mod(me.focusedCell.x, CROSSHAIRPICKER_COLS) / CROSSHAIRPICKER_COLS;
-                                       cellPos_y = mod(me.focusedCell.y, CROSSHAIRPICKER_ROWS) / CROSSHAIRPICKER_ROWS;
-                                       draw_Fill(cellPos, me.realCellSize, SKINCOLOR_LISTBOX_FOCUSED, getHighlightAlpha(SKINALPHA_LISTBOX_FOCUSED, SKINFADEALPHA_LISTBOX_FOCUSED, me.focusedCellTime));
-                               }
-                       }
-
-                       // Draw crosshair
-                       crosshairPos_x = cell.x / CROSSHAIRPICKER_COLS + 0.5 * me.realCellSize.x;
-                       string cross = strcat("/gfx/crosshair", crosshairpicker_cellToCrosshair(cell));
-                       sz = draw_PictureSize(cross);
-                       sz = globalToBoxSize(sz, me.size);
-
-                       float ar = sz.x / sz.y;
-                       sz.x = me.realCellSize.x;
-                       sz.y = sz.x / ar;
-
-                       sz = sz * 0.95;
+       string cross = strcat("/gfx/crosshair", crosshairpicker_cellToCrosshair(me, cell));
+       sz = draw_PictureSize(cross);
+       sz = globalToBoxSize(sz, me.size);
 
-                       rgb = '1 1 1';
-                       draw_Picture(crosshairPos - 0.5 * sz, cross, sz, rgb, me.alpha);
-                       if(cvar("crosshair_dot"))
-                       {
-                               if(cvar("crosshair_dot_color_custom") && (cvar_string("crosshair_dot_color") != "0"))
-                                       rgb = stov(cvar_string("crosshair_dot_color"));
+       float ar = sz.x / sz.y;
+       sz.x = me.realCellSize.x;
+       sz.y = sz.x / ar;
+       sz = sz * 0.95;
 
-                               draw_Picture(crosshairPos - 0.5 * sz * cvar("crosshair_dot_size"), "/gfx/crosshairdot", sz * cvar("crosshair_dot_size"), rgb, me.alpha);
-                       }
-               }
-       }
+       rgb = '1 1 1';
 
-       draw_alpha = save;
+       vector crosshairPos = cellPos + 0.5 * me.realCellSize;
+       draw_Picture(crosshairPos - 0.5 * sz, cross, sz, rgb, me.alpha);
 
-       SUPER(XonoticCrosshairPicker).draw(me);
+       if(cvar("crosshair_dot"))
+               draw_Picture(crosshairPos - 0.5 * sz * cvar("crosshair_dot_size"), "/gfx/crosshairdot", sz * cvar("crosshair_dot_size"), rgb, me.alpha);
 }
 #endif