我最近开始使用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;
}
本课程的目的是:
存储组件层次结构的单个组件(子)
拦截与孩子或其后代之一相关的所有鼠标事件
- 做一点事
- 最终将 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) 之后,它会起作用,我将再次禁用鼠标单击。任何想法?
提前致谢