Closed pozemka closed 11 months ago
Three comments:
out_clicked
, out_hovered
, out_held
to be consistent with ImGui::ButtonBehavior
DragRect
calls ImGui::ButtonBehavior
button behavior five times -- once for the center, and once for each corner. We need to handle this somehow. A couple options:
bool out_xxx[5]
ButtonBehavior
, e.g. *is_clicked = *is_clicked || clicked;
The first option feels heavy, the last one feels misleading. So perhaps the second option is the best generalization. Also, DragRect
might need to report "hovered" when the cursor is inside the rect (but not necessarily hovering one of the control points).
Thoughts?
DragRect
is tricky. 🤔 In my opinion most useful would be to know if it was clicked anywhere. Actually I can imagine scenarios when user would be interested in clicks on corner points or edges but then we could also imagine need for a RMB, Scroll etc. So I think it is better to provide facility to handle most common case. I've added handling of events on entire rectangle:
if (input) {
// Entire rectangle
ImGuiID r_id = id + 5;
ImGui::KeepAliveID(r_id);
bool is_hovered = false;
bool is_held = false;
bool is_clicked = ImGui::ButtonBehavior(rect, r_id, &is_hovered, &is_held);
if (out_clicked)
*out_clicked = is_clicked;
if (out_hovered)
*out_hovered = is_hovered;
if (out_held)
*out_held = is_held;
}
But I not sure about the r_id
value for this ButtonBehavior. In the loop earlier it had values id+1 .. id+4
so id + 5
seems correct?
Also I have updated example code to print info about state of draggable items (only one item per plot though):
@pozemka, I just pushed a few changes:
Let me know what you think and we'll merge this...
I've looked through your commit. Everything seems fine. My initial thought was thath this code
const bool mouse_inside = rect_grab.Contains(ImGui::GetMousePos());
const bool mouse_clicked = ImGui::IsMouseClicked(0);
if (input && mouse_inside) {
if (out_clicked) *out_clicked = *out_clicked || mouse_clicked;
if (out_hovered) *out_hovered = true;
}
Makes previous hover/click checks unnecessary. But parts of the grab points are outside of the rect so they should be handled too.
Only thing I could imagine as missing is that the rect itself has no held state (grab points have it). So I have added ImGui::IsMouseDown
.
So I have added ImGui::IsMouseDown.
Yea, I was on the fence about doing that. Seemed a little weird to say the rect is held when the user isn't necessarily holding a moveable element. But, for consistency, I think it's the right call.
Merging ...
Hello again! Following #506 PR.
To be able to query states of draggable items I've added new optional arguments:
When
clicked
,hovered
orheld
provided it will be set to results ofImGui::ButtonBehavior
function.Example code: