Reverse items and your values of a popup (LUA)

I have a request for LUA a little strange, but necessary.

I need to invert the table that defines the items of a popup, so that the first value is the last one. The popup should return everything in reverse, not only the item, but also the value.

Can anyone do some LUA magic for this?

the table

pht_chd_root = { --99
  " C-0"," C#0"," D-0"," D#0"," E-0"," F-0"," F#0"," G-0"," G#0"," A-0"," A#0"," B-0",
  " C-1"," C#1"," D-1"," D#1"," E-1"," F-1"," F#1"," G-1"," G#1"," A-1"," A#1"," B-1",
  " C-2"," C#2"," D-2"," D#2"," E-2"," F-2"," F#2"," G-2"," G#2"," A-2"," A#2"," B-2",
  " C-3"," C#3"," D-3"," D#3"," E-3"," F-3"," F#3"," G-3"," G#3"," A-3"," A#3"," B-3",
  " C-4"," C#4"," D-4"," D#4"," E-4"," F-4"," F#4"," G-4"," G#4"," A-4"," A#4"," B-4",
  " C-5"," C#5"," D-5"," D#5"," E-5"," F-5"," F#5"," G-5"," G#5"," A-5"," A#5"," B-5",
  " C-6"," C#6"," D-6"," D#6"," E-6"," F-6"," F#6"," G-6"," G#6"," A-6"," A#6"," B-6",
  " C-7"," C#7"," D-7"," D#7"," E-7"," F-7"," F#7"," G-7"," G#7"," A-7"," A#7"," B-7",
  " C-8"," C#8"," D-8"
}

The popup

vb:popup {
      id = "PHT_PP_ROOT_1",
      height = 25,
      width = 60,
      value = 49,
      items = pht_chd_root,
      --notifier = ,
      --midi_mapping = ,
      --tooltip = 
    },

The result I want is exactly that the mouse wheel behaves exactly the other way around with this popup. When rolling up, start at the end of the table and vice versa, Thus, the behavior with the mouse wheel will be identical to a valuebox.

Any ideas? Thanks!

Edit: The problem is that as it is now, when rolling the wheel up, the values of the notes decrease and vice versa. And I want it the other way around.

Edit: Sorry, I misread you so I removed my example.

Edit: Sorry, I misread you so I removed my example.

I think it is possible to solve it in another way.

I do not really care about changing the order of the table. The problem is that the popup returns the original value, which starts with 1 and ends with the total number of elements in the table.

If the table has 99 elements, that number 1 should convert it to 99, 2 to 98, 3 to 97… and so on …

It would be to create a function that inverts a range of ordered numbers:

  • From 1 to 99, convert to 99 to 1.For example, upon receiving the value 4, the function should return 96.
    • 99 to 1
    • 98 to 2
    • 97 to 3
    • 94 to 4
    • 4 to 96
    • 3 to 97
    • 2 to 98
    • 1 to 99

If you only want to sort a table, there should be a lot of information on how to do this available by searching on google.

But from what I understand, you want to change the behavior of the popup so that the list is in order (when clicked normally) but the mouse scrolling is ‘inverted’? That’s not possible.

PS. Already thought about your idea about ‘inverting the result’, but that would break compatibility with normal popup+mouse clicking behavior. No go… VB can’t tell the difference of selecting the value by scrolling or clicking.

Not even some fancy overlay hack could help here, since anything overlaid would block catching the scroll wheel.

If you only want to sort a table, there should be a lot of information on how to do this available by searching on google.

But from what I understand, you want to change the behavior of the popup so that the list is in order (when clicked normally) but the mouse scrolling is ‘inverted’? That’s not possible.

PS. Already thought about your idea about ‘inverting the result’, but that would break compatibility with normal popup+mouse clicking behavior. No go… VB can’t tell the difference of selecting the value by scrolling or clicking.

I think I have the solution. The behavior of the popup will be the same, but the value sent by its notifier will be inverted through a simple function. Thus, it is possible to order the table upside down and it will be displayed just the other way around. I believe. I’m going to try it.

Actually, this is a management problem. The normal thing is that the user wants to turn the wheel upwards and that the value grows, not the other way around.

This is a specific case. Normally, when you display the list, the first element is at the top, and I want it just the other way around.

Like i said. If you invert anything, or if you rely on quickly changing the value - the normal popup list will in effect be sorted wrongly. But if that’s not undesirable, sure.

I’m pretty sure that’s undesirable, otherwise you could use a valuebox right from the start.

And if you don’t care anything about the order being displayed in the popup, but only care about the mouse wheel, then just reverse the table.

Like i said. If you invert anything, or if you rely on quickly changing the value - the normal popup list will in effect be sorted wrongly. But if that’s not undesirable, sure.

I’m pretty sure that’s undesirable, otherwise you could use a valuebox right from the start.

And if you don’t care anything about the order being displayed in the popup, but only care about the mouse wheel, then just reverse the table.

Solved!

Here are two things:

  1. The order of the table that defines the items
  2. The value returned by the popup notifier

The solution is to invest both. Additionally, if I used midi_mapping , it is necessary to invert the value as well ( 99 - math.floor( message.int_value * 99/128 ) ). In this way, the drop-down list will be reversed, the D-8 will be seen above and the C-0 will be seen below. The mouse wheel when going up, will increase in note and vice versa. And the same will happen through MIDI Input. It is necessary to invest these 3 things, so everything will remain coherent.

In the end it has been easier to play with simple mathematical operations.

Thanks for the help!

PS. If you don’t want to hardcode a separate reversed table, you can use this.

local my_table = { "a", "b", "c", "d", "e" }

function table_reverse(tbl)
  local new_tbl = { }
  for _, v in ripairs(tbl) do
    new_tbl[#new_tbl+1] = v
  end
  return new_tbl
end

local a_reversed_table = table_reverse(my_table)

PS. If you don’t want to hardcode a separate reversed table, you can use this.

local my_table = { "a", "b", "c", "d", "e" }

function table_reverse(tbl)
local new_tbl = { }
for _, v in ripairs(tbl) do
new_tbl[#new_tbl+1] = v
end
return new_tbl
end

local a_reversed_table = table_reverse(my_table)

Thanks!In specific cases this function is very good, especially if you have a very large table, and manually investing it costs a lot of work.By the way, yes, I had searched in google for information on how to invest tables. I even provided it, but it was not the final solution to this issue. Here it was necessary to concentrate on the value returned by the popup notifier. By inverting this number you can reverse the order of the items that appear in the popup displayed, and the result is optimal in this specific case, just what I expected.