Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple planner buttons #4868

Merged

Conversation

dodomorandi
Copy link
Contributor

@dodomorandi dodomorandi commented Apr 26, 2020

Current behavior

Orbit planner buttons changes the value all together when using left and right arrow buttons. This is obviously unintended.

Desired behavior

  • Thrust controls must be independent one each other, and should continuously increase/decrease the value until mouse button is released
  • Time factor control must be independent from other controls, but a step-on-click behavior is expected -- i.e. the factor is increased/decreased once per click

Reason of the issue

PiGui::ButtonImageSized uses the texture id as the button id. However, left and right arrow buttons share the same texture, leading to this issue. This maybe could be happening with other buttons as well.
@Gliese852 clearly showed that the issue was related to the ID of the tooltip, nothing related to PiGui::ButtonImageSized.

I am not sure this issue have been already reported, I am not able to find it. If someone can give me some feedback, it can be opened or we can just solve the whole thing in this PR.

## Status of the PR

The think that the best way to handle this situation is have an optional string that can be passed to the function in order to generate a different ID for the ImGui Item. I followed the typical hashing approach to combine hash together in order to mix the texture id with passed string.

In order to pass an optional string to the function I needed to use an alternative version of LuaPull, and instead of doing that just for const char*, I decided to implement a poor man's optional type and create a generic LuaPullOpt function. Side note: I did not create the function for boolean types because, by default, lua_toboolean returns false in case the value was nil. Maybe it should be implemented anyway, in order to distinguish nil from false, let me know what you think.

In any case, I am still struggling with the buttons, probably because of my inexperience with ImGui. I am not able to increment/decrement while keeping the mouse pressed. I changed a bit the return value of PiGui::ButtonImageSized in order to return pressed, hovered and held properties, but I am still struggling to obtain the desired behavior. Moreover, using only pressed value, the factor buttons don't work at all (why???). Maybe it is just something silly that I am missing, so I decided to create this PR in draft mode and, hopefully, get some feedback and help.

Oh, don't worry: if it is possible to make things work without using hovered and held properties, I will be very happy to restore the return value of PiGui::ButtonImageSized and the related functions -- I was just trying to find something that could work 😞

EDIT: the issue is solvable in a much simpler way than I thought, thank you @Gliese852.

@Gliese852
Copy link
Contributor

@dodomorandi I remember exactly that everything worked as it should, when I checked this a couple of weeks ago.

@Gliese852
Copy link
Contributor

Gliese852 commented Apr 27, 2020

@dodomorandi it worked well when every button had unique tooltip, i.e. it was:

showDvLine(icons.orbit_reduce, icons.orbit_prograde, icons.orbit_increase, "prograde", ui.Format.Speed, "Thrust retrograde", "Reset prograde thrust", "Thrust prograde")
showDvLine(icons.orbit_reduce, icons.orbit_normal, icons.orbit_increase, "normal", ui.Format.Speed, "Thrust antinormal", "Reset normal thrust", "Thrust normal")
showDvLine(icons.orbit_reduce, icons.orbit_radial, icons.orbit_increase, "radial", ui.Format.Speed, "Thrust radially out", "Reset radial thrust", "Thrust radially in")

Then I renamed all the tooltips to

showDvLine(icons.decrease, icons.orbit_prograde, icons.increase, "prograde", ui.Format.Speed, luc.DECREASE, lc.PLANNER_RESET_PROGRADE, luc.INCREASE)
showDvLine(icons.decrease, icons.orbit_normal, icons.increase, "normal", ui.Format.Speed, luc.DECREASE, lc.PLANNER_RESET_NORMAL, luc.INCREASE)
showDvLine(icons.decrease, icons.orbit_radial, icons.increase, "radial", ui.Format.Speed, luc.DECREASE, lc.PLANNER_RESET_RADIAL, luc.INCREASE)

And everything became bad.

@fluffyfreak
Copy link
Contributor

@Gliese852 is this something that's already being refactored in your other PR?

@Gliese852
Copy link
Contributor

@fluffyfreak no

@dodomorandi
Copy link
Contributor Author

A couple of discoveries.

There was an issue related to clipping rectangle that sometimes made ImGui::ItemAdd in PiGui::ButtonImageSized return false. I am now trying to push/pop the correct clipping rect in order to make the call always work -- maybe this is not correct but it helps me to fix the issue, just let me know if the previous behavior was correct.

The second thing is that I was trying to use ImGuiButtonFlags_Repeat flag on ImGui::ButtonBehavior, without any particular success. What is happening, and I really don't understand why, is that the button being pressed is not active anymore after some frames (it is set to 0), therefore the repeat behavior is not triggered. It looks like a ClearActiveID is triggered somewhere, but where and why (and if it makes sense) I don't know. 😞

@Gliese852
Copy link
Contributor

Gliese852 commented Apr 27, 2020

@dodomorandi

left and right arrow buttons share the same texture

are they? IIRC icons.decrease != icons.increase

@dodomorandi
Copy link
Contributor Author

Yes, Sorry, I was not clear enough: all decrease icons share the same texture, and all the increase icons do the same.

@Gliese852
Copy link
Contributor

@dodomorandi what if we do like this:

--- a/data/pigui/modules/system-view-ui.lua
+++ b/data/pigui/modules/system-view-ui.lua
@@ -75,18 +75,18 @@ local function showDvLine(leftIcon, resetIcon, rightIcon, key, Formatter, leftTo
             end
         end
     end
-    local press = ui.coloredSelectedIconButton(leftIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, leftTooltip)
+    local press = ui.coloredSelectedIconButton(leftIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, leftTooltip, Vector2(0,0), key)
     if press or (key ~= "factor" and ui.isItemActive()) then
         systemView:TransferPlannerAdd(key, -10)
     end
     wheel()
     ui.sameLine()
-    if ui.coloredSelectedIconButton(resetIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, resetTooltip) then
+    if ui.coloredSelectedIconButton(resetIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, resetTooltip, Vector2(0,0), key) then
         systemView:TransferPlannerReset(key)
     end
     wheel()
     ui.sameLine()
-    press = ui.coloredSelectedIconButton(rightIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, rightTooltip)
+    press = ui.coloredSelectedIconButton(rightIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_ACTIVE, svColor.BUTTON_INK, rightTooltip, Vector2(0,0), key)
     if press or (key ~= "factor" and ui.isItemActive()) then
         systemView:TransferPlannerAdd(key, 10)
     end



--- a/data/pigui/pigui.lua
+++ b/data/pigui/pigui.lua
@@ -813,7 +813,7 @@ ui.coloredSelectedButton = function(label, thesize, is_selected, bg_color, toolt
     end
     return res
 end
-ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size)
+ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size, extraID)
     if is_selected then
         pigui.PushStyleColor("Button", bg_color)
         pigui.PushStyleColor("ButtonHovered", bg_color:tint(0.1))
@@ -824,7 +824,7 @@ ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_paddin
         pigui.PushStyleColor("ButtonActive", bg_color:shade(0.2))
     end
     local uv0,uv1 = get_icon_tex_coords(icon)
-    pigui.PushID(tooltip)
+    pigui.PushID(tooltip .. (extraID or ""))
     local res = pigui.ButtonImageSized(ui.icons_texture, thesize, img_size or Vector2(0,0), uv0, uv1, frame_padding, ui.theme.colors.lightBlueBackground, fg_color)
     pigui.PopID()
     pigui.PopStyleColor(3)

these changes seem to solve the problem?

@dodomorandi
Copy link
Contributor Author

Holy cow, you are totally right!

This demonstrates how I am unable to get how ImGui works... Thank you so much, you easily solved what was driving me nuts. I will update the description of the PR, force push with your changes and, obviously, credit you 😉

@dodomorandi dodomorandi force-pushed the decouple-planner-buttons branch from 51656f0 to e559ef0 Compare April 27, 2020 20:19
@dodomorandi dodomorandi marked this pull request as ready for review April 27, 2020 20:19
Patch kindly suggested by Gliese852
@dodomorandi dodomorandi force-pushed the decouple-planner-buttons branch from e559ef0 to 7e11dbb Compare April 27, 2020 20:23
@Gliese852
Copy link
Contributor

Very glad I could help!

@sturnclaw sturnclaw merged commit 54b9706 into pioneerspacesim:master Apr 28, 2020
@dodomorandi dodomorandi deleted the decouple-planner-buttons branch April 28, 2020 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants