在 Juce 库中的 FileBrowserComponent 上拦截鼠标事件
我最近开始使用 Juce 库。我通常在其论坛上发布与 Juce 相关的问题,但我已经困扰了很多天的问题,但我仍然没有收到答案。所以 stackoveflow 确实值得一试,即使这里似乎没有很多 Juce 的用户。
问题是:
我正在用 Juce 的组件进行一些实验。 我有以下类:
class MyComponent : public Component{
public :
MyComponent(Component * comp){
this->child = comp;
comp->setInterceptsMouseClicks(false, false);
}
void mouseDown (const MouseEvent &e){
//do stuff
Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
if (target != NULL && doIWantToForwardEventToTarget()){
MouseEvent newEvent = e.getEventRelativeTo(target);
target->mouseDown(newEvent);
}
}
void mouseMove (const MouseEvent &e);
void mouseEnter (const MouseEvent &e);
void mouseExit (const MouseEvent &e);
void mouseDrag (const MouseEvent &e);
void mouseUp (const MouseEvent &e);
void mouseDoubleClick (const MouseEvent &e);
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY);
private:
Component *child;
}
此类的目的是:
存储 a 的单个组件 组件层次结构(子级)
拦截所有相关的鼠标事件 到子级或其后代之一
- 做某事
- 最终将 MouseEvent 转发到 它所指向的组件 我尝试
将滑块组件作为子组件,甚至嵌套在其他组件中......一切正常。 现在我正在用 FileBrowserComponent 做一些实验,但它似乎无法正常工作。例如,当我单击按钮移动到上层目录时,它不会(按钮接收鼠标事件并被单击,但树视图中没有任何反应)。 从列表中选择项目也不起作用。
可能是什么问题? (我做了一些实验,似乎没有调用FileBrowserComponent中的方法buttonClicked,但我不知道为什么) 有什么建议吗?
我也尝试过这样修改代码:
void mouseDown (const MouseEvent &e){
//do stuff
Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
if (target != NULL && doIWantToForwardEventToTarget()){
target->setInterceptsMouseClicks(true, true);
MouseEvent newEvent = e.getEventRelativeTo(target);
target->mouseDown(newEvent);
target->setInterceptsMouseClicks(false, false);
}
}
还是不行。无论如何,我发现,如果我评论第二次调用 setInterceptMouseClicks(其中我在之后禁用鼠标单击)会使事情正常工作(即使这不是我想要获得的结果,因为我需要重新禁用该鼠标事件)成分)。
这些事实可以让我考虑两个因素:
- 组件需要拦截鼠标 即使鼠标事件发生,也会点击 手动传递给它的 mouseDown 方法(这是真的吗?我不是这样 确信这一点)
- 在鼠标事件处理之后 FileBrowserComponent 还有其他 使用以下信息的类 它拦截鼠标点击状态, 否则它会工作如果之后 target->mouseDown(newEvent),我会 再次禁用鼠标点击。任何 主意?
提前致谢
I recently begin to use Juce library. I usually post Juce related question on its forum, but I'm struggling with an issues from a lot of days, and I received still no answers. So stackoveflow does worth a try even if seems that there aren't a lot of Juce's users here.
Here's the question:
I'm making some experiments with Juce's Components.
I have the following class:
class MyComponent : public Component{
public :
MyComponent(Component * comp){
this->child = comp;
comp->setInterceptsMouseClicks(false, false);
}
void mouseDown (const MouseEvent &e){
//do stuff
Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
if (target != NULL && doIWantToForwardEventToTarget()){
MouseEvent newEvent = e.getEventRelativeTo(target);
target->mouseDown(newEvent);
}
}
void mouseMove (const MouseEvent &e);
void mouseEnter (const MouseEvent &e);
void mouseExit (const MouseEvent &e);
void mouseDrag (const MouseEvent &e);
void mouseUp (const MouseEvent &e);
void mouseDoubleClick (const MouseEvent &e);
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY);
private:
Component *child;
}
The purposes of this class are:
store a single Component of a
hierarchy of Components (child)intercept all mouse events related
to child or one of its descendant- do something
- eventually forward the MouseEvent to
the Component which it was directed
to
I tried this class with sliders components as children, even nested inside other components.. all works fine.
Now I was doing some experiments with FileBrowserComponent and it seems not to work properly. For example when I click on the button to move to the upside directory it doesn't (the button receives the mouse event and it's clicked but nothing happen in the tree view).
Also selecting items from the list doesn't work.
What could be the problem?
(I did some experiments and seems that the method buttonClicked in the FileBrowserComponent isn't called, but I dont't know why)
Any suggestion?
I also tried to modify the code this way:
void mouseDown (const MouseEvent &e){
//do stuff
Component *target = getTopChild(this->child, e.x, e.y); //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
if (target != NULL && doIWantToForwardEventToTarget()){
target->setInterceptsMouseClicks(true, true);
MouseEvent newEvent = e.getEventRelativeTo(target);
target->mouseDown(newEvent);
target->setInterceptsMouseClicks(false, false);
}
}
It still doesn't work. Anyway I found out that if I comment the second call to setInterceptMouseClicks( where I disable the mouse click after ) make the things work (even if this isn't the result I'd want to obtain because I need to redisable mouse events on that component).
These facts can led me to 2 considerations:
- A component need to intercept mouse
clicks even if mouse events are
manually passed to its mouseDown
method (is this true? I'm not so
sure about that) - After the mouse event handling in
FileBrowserComponent there are other
classes that uses the information of
its intercepting mouse click status,
otherwise it would work if after the
target->mouseDown(newEvent), I'll
disable mouse clicks again. Any
idea?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好从 Component 创建派生类,并在这些派生类中实现其所需的鼠标事件,当这些事件被触发时,您可以向父类发布消息,以便它可以执行其他操作。
You should better create derived classes from Component, and in those derived classes implement their required mouse events, and when those events are triggered, you can post a message to the parent class, so it can do other stuff.
您似乎正在单击 FileBrowserComponent 的子组件。并且子级的鼠标事件不会转发回父级(FileBrowserComponent)。这就是为什么当您单击内部时,父级将不会收到该事件。
所以不要去顶部 -->底部,你应该到底部->顶部。为此:您应该通过以下方法向子级添加鼠标侦听器:
因此,对于您显示的示例:
并且方法 mouseDown() 应该如下所示:
希望这会有所帮助。
It seems that you are clicking on a child component of FileBrowserComponent. and the mouse event of the child is not forwarded back to the parent(FileBrowserComponent). that's why, when you click inside, the parent will not receive the event.
so instead of going Top --> Bottom, you should go bottom --> top. To do this: you should add a mouse listener to the child by this method:
So for the example you're showing :
And the method mouseDown() should be like this :
Hope this help.