Jump to content
Sign in to follow this  
Schatten

Getting listbox item index by coordinates

Recommended Posts

Hi!

I wrote a function to get listbox item index by coordinates. It works fine if listbox height is a multiple of row height, otherwise a small error appears, which is especially noticeable at the end of listbox. Could anyone help solve this issue?

 

Spoiler

private ["_index", "_itemsCount"];

_index = -1;

params [
    "_control",
    "_coordinates",
    ["_itemHeight", getNumber (configFile >> "ctrlListbox" >> "rowHeight")]
];

_itemsCount = lbSize _control;

if (_itemsCount == 0) exitWith { _index };

private ["_contentHeight", "_offset"];

(ctrlPosition _control) params ["", "_controlY", "", "_controlH"];
((ctrlScrollValues _control) apply { if (_x < 0) then { 0 } else { _x } }) params ["_scrollValueV"];

_coordinates params ["", "_y"];

_y = _y - _controlY;

_contentHeight = _itemsCount * _itemHeight;

_offset = if (_contentHeight > _controlH) then {
    _scrollValueV * (_contentHeight - (floor (_controlH / _itemHeight)) * _itemHeight)
} else { 0 };

if (_y <= (_contentHeight - _offset)) then {
    _index = floor ((_offset + _y) / _itemHeight);
};

hintSilent (format [
    "Control height: %1\nItem height: %2\nContent height: %3\nItems count: %4\ny: %5\nScroll values: %6\nOffset: %7\nIndex: %8",
    _controlH,
    _itemHeight,
    _contentHeight,
    _itemsCount,
    _y,
    ctrlScrollValues _control,
    _offset,
    _index
]);

_index

 

 

Spoiler

FgH0Dr7S_o.png

wzMFglt2_o.png

 

Share this post


Link to post
Share on other sites

ListBox's sizeEx will be rounded into pixel perfect. Which means if sizeEx has a modulo (sizeEx mod pixelH != 0) there can be a gap between your coordinates and real height.

sizeEx = rowHeight - (rowHeight mod pixelH) is a workaround.

  • Like 1

Share this post


Link to post
Share on other sites

So, you suggest changing sizeEx in my listbox config, right? If so, can I fix this gap in my function? I'm asking because I would like my dialogs to be as vanilla as possible, so I wouldn't like to change sizeEx. I also wouldn't like sizes of listboxes and, accordingly, the dialog to change when switching between tabs.

Share this post


Link to post
Share on other sites
2 hours ago, POLPOX said:

ListBox's sizeEx will be rounded into pixel perfect. Which means if sizeEx has a modulo (sizeEx mod pixelH != 0) there can be a gap between your coordinates and real height.

sizeEx = rowHeight - (rowHeight mod pixelH) is a workaround.

Unfortunately, this didn't help:

#define LISTBOX_ITEM_H 1.75 * 4.32 * pixelGrid * pixelScale / (getResolution select 3)

...

rowHeight = LISTBOX_ITEM_H;
sizeEx = __EVAL(format ["%1 - (%1 mod pixelH)", LISTBOX_ITEM_H]);
Spoiler

dWHimbgk_o.png

 

Share this post


Link to post
Share on other sites

If you don't want to make a new class, maybe you can use _itemHeight = _itemHeight - (_itemHeight mod pixelH)?

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

It works! I added your line just after

_y = _y - _controlY;

Thanks, @POLPOX!

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×