Search for a selectable item that is close to the touched XY coordinates
| 879 | |
| 880 | // Search for a selectable item that is close to the touched XY coordinates |
| 881 | void Menu::HandleTouch(PixelNumber x, PixelNumber y) noexcept |
| 882 | { |
| 883 | constexpr int MaxXerror = 8, MaxYerror = 8; // how far (in pixels) the X and Y coordinates of a touch event need to be to the outline of the item for us to disallow it |
| 884 | |
| 885 | if (displayingErrorMessage) |
| 886 | { |
| 887 | // Allow the message to be cancelled by a touch |
| 888 | TouchBeep(); |
| 889 | timeoutValue = 1; // cancel the timeout at the next tick |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | int bestError = MaxXerror + MaxYerror; |
| 894 | MenuItem *null best = nullptr; |
| 895 | for (MenuItem *p = selectableItems; p != nullptr; p = p->GetNext()) |
| 896 | { |
| 897 | if (p->IsVisible()) |
| 898 | { |
| 899 | const int xError = (x < p->GetMinX()) ? p->GetMinX() - x |
| 900 | : (x > p->GetMaxX()) ? x - p->GetMaxX() |
| 901 | : 0; |
| 902 | if (xError < MaxXerror) |
| 903 | { |
| 904 | const int yError = (y <p->GetMinY()) ? p->GetMinY() - y |
| 905 | : (y > p->GetMaxY()) ? y - p->GetMaxY() |
| 906 | : 0; |
| 907 | if (yError < MaxYerror && xError + yError < bestError) |
| 908 | { |
| 909 | bestError = xError + yError; |
| 910 | best = p; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (best != nullptr) |
| 917 | { |
| 918 | TouchBeep(); |
| 919 | if (itemIsSelected) // send the touch to the item itself |
| 920 | { |
| 921 | if (highlightedItem != nullptr && highlightedItem->IsVisible()) |
| 922 | { |
| 923 | //TODO |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | itemIsSelected = false; // should not get here |
| 928 | } |
| 929 | } |
| 930 | else // click without an item under selection |
| 931 | { |
| 932 | highlightedItem = best; |
| 933 | EncoderActionEnterItemHelper(); |
| 934 | } |
| 935 | |
| 936 | if (!displayingErrorMessage && !displayingMessageBox) // if the operation did not result in an error and we are not displaying a message box |
| 937 | { |
| 938 | lastActionTime = millis(); |