以正确的顺序进行位块传输 - 访问者问题

发布于 2024-09-27 09:52:04 字数 649 浏览 5 评论 0原文

我正在设计一个简单的 GUI。我有小部件,它有孩子和一个父母。每个 Widget 都是一个 Composite 对象,具有 WidgetComposite 对象的向量。其中一个 WidgetComposites 是 PaintingBehaviour,但 Widget 本身并不知道它。

为了显示我的窗口,我使用一个称为 ScreenVisitor 的访问者。当调用 Visitor 时,会发生以下情况:

第一个小部件 WidgetScene 迭代其每个小部件并调用“accept(Visitor* v)”方法。然后,每个小部件接受访问者,然后迭代其子部件。

例如,这是访问者必须接受的对象列表(按照发生的顺序)。

root
    child1
       child2
         child3
       child4
    child5
       child6
    child7

现在,我的问题很简单:我希望每个 Widget 都绘制在其父级上。你将如何进行?我尝试过使用一棵树,但总是遇到同样的问题:当我必须在层次结构中向上时(例如,在显示 child3 后,当我必须显示 child4 时),我不知道如何获取正确的父母。

我正在用 C++ 编码,但这个问题不是特定于语言的。

你有什么想法吗?提前致谢 !

I'm designing a simple GUI. I have Widgets, which have children and one parent. Each Widget is a Composite object, with a vector of WidgetComposite objects. One of those WidgetComposites is a PaintingBehaviour, but the Widget doesn't know it as such.

To display my window, I use a Visitor, called the ScreenVisitor. When the Visitor is called, this is what happens :

The first widget, the WidgetScene, iterates on each of its Widgets and calls the "accept(Visitor* v)" method. Then, each widget accepts the visitor, then iterates on its children.

For instance, this is a list of the objects (in the order it's going to happen) the visitor will have to accept.

root
    child1
       child2
         child3
       child4
    child5
       child6
    child7

Now, my problem is simple : I want each Widget to be painted on its parent. How would you proceed ? I've tried with a tree, but I always have the same problem : when I have to go up in the hierarchy (for instance, after having displayed child3, when I have to display child4) I don't know how to get the right parent.

I'm coding in C++ but this problem is not language specific.

Do you have any ideas ? Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个问题对我来说不是很清楚。如果您只需要按照正确的顺序绘制它们。在子节点之前创建父节点,这非常简单:

accept(Visitor v)
  paint() //paint parent first
  v.visit()
  foreach child
    child.accept(v) //then paint children

如果您需要父节点(为什么?),您可以更改接受方法以(可选)获取父节点。

accept(Visitor v,Element parent = null)
  paint()
  v.visit()
  parent.foo()
  foreach child
    child.accept(v,this)

The question is not very clear to me. If you just need to get them to be painted in the correct order. Parent before children, that would be quite easy:

accept(Visitor v)
  paint() //paint parent first
  v.visit()
  foreach child
    child.accept(v) //then paint children

If you need the parent (why?), you could change the accept method to (optionally) take a parent node.

accept(Visitor v,Element parent = null)
  paint()
  v.visit()
  parent.foo()
  foreach child
    child.accept(v,this)
扮仙女 2024-10-04 09:52:04

好吧,由于我找不到其他任何东西,我尝试了这个解决方案:

访问者创建一个“DisplayNodes”对象树,这些对象基本上是一个由指针组成的类。以下是该类的属性:

class DisplayNode
{
   private:
     Widget* myWidget;
     PaintingBehaviour* myPB;
     DisplayNode* myParent;
     vector <DisplayNode*>myChildren;
};

每个 DisplayNode 都存储在我的访问者的向量中。

然后,对于每个 PaintingBehaviour,我测试所连接的 Widget 的父级,并检查我的 Node 的“myWidget”之一是否是该父级。这样我就可以找到所有东西都在哪里。之后,就很简单了,在 DisplayNode 中使用一个简单的绘制方法,递归调用子级,使用场景的第一个 DisplayNode 进行调用...

由于它有点重,所以我只创建了一次 DisplayNode 树(尽管我这样做了)如果添加/删除 PaintBehaviour 或小部件,则再次进行)。如果树已经存在,我就直接进行递归绘画。

我承认,它有点扭曲(并且可能没有完全优化),但它有效!但如果有人有更好的解决方案,我会非常高兴听到。

All right, since I could not find anything else, I tried this solution :

The visitor create a tree of "DisplayNodes" objects, who are basically a class made essentially of pointers. Here are the attributes of the class :

class DisplayNode
{
   private:
     Widget* myWidget;
     PaintingBehaviour* myPB;
     DisplayNode* myParent;
     vector <DisplayNode*>myChildren;
};

Every DisplayNode is stocked in a vector in my visitor.

Then, for each PaintingBehaviour, I test the parent of the Widget connected, and check if one of my Node's "myWidget" is this parent. So I can find back where is everything. After that, it's quite easy, a simple paint method in DisplayNode with recursive calls to the children, called with the first DisplayNode of the scene...

Since it's a bit heavy, I create the tree of DisplayNode only once (though I do it again if PaintingBehaviour or widgets are added / removed). If the tree already exists, I go directly to the recursive painting.

It's a bit twisted (and probably not quite optimized), I'll admit, but it works ! But if anybody has a better solution, I'll be more than happy to hear it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文