菜单导航的设计模式(c/c++ 中)

发布于 2024-12-16 00:47:08 字数 1204 浏览 2 评论 0原文

您能推荐一种设计模式或策略来优雅地处理基于屏幕的菜单导航吗?

其中“基于屏幕”是指一系列互连的全屏“页面”,其中带有链接到其他“屏幕”的按钮 - 例如在游戏的用户界面中。

我已经实现了基于状态的设计模式,但我不推荐这种模式。
代码很快就会变得混乱,并且容易出现各种基于状态的错误,这些错误变得越来越难以测试。

例如:

void Update(float dt)
{
    switch(curState)
    {
        case kScreenA:
            ScreenA.Update(dt);
            if(ScreenA.IsDone())
                curState = kScreenB;
        break;
        etc...
}

使用这种方法,您最终需要处理返回条件:

void Update(float dt)
{
    switch(curState)
    {
       case kScreenA:
           ScreenA.Update(dt);
           if(ScreenA.IsDone())
           {
               if(ScreenA.ReturnState == 1)
                   curState = kScreenB;
               if(ScreenA.ReturnState == 2)
                   curState = kScreenC;
               etc...
            }
     }
}

同样,使用这种方法,您最终可能需要处理进入条件:

void InitState()
{
    switch(nextState)
    {
        case kScreenC:
            if(curState == kScreenA)
                ScreenC.InitFromA();
            if(curState == kScreenB)
                ScreenC.InitFromB();
            etc...
     }
}

所以必须有更好的方法来设计它,您能描述一种更好的方法吗?

干杯,
泥鸭

Can you recommend a Design Pattern or strategy for elegantly handling screen based menu navigation?

Where "Screen Based" means a series of interconnected full screen "pages" with buttons that link to other "Screens" -Like for example in a game's user interface.

I've implemented a State based design pattern, which I do not recommend.
The code gets messy fast and becomes prone to all sorts of state based bugs which become increasingly hard to test for.

Eg:

void Update(float dt)
{
    switch(curState)
    {
        case kScreenA:
            ScreenA.Update(dt);
            if(ScreenA.IsDone())
                curState = kScreenB;
        break;
        etc...
}

With this approach you end up needing to handle return conditions:

void Update(float dt)
{
    switch(curState)
    {
       case kScreenA:
           ScreenA.Update(dt);
           if(ScreenA.IsDone())
           {
               if(ScreenA.ReturnState == 1)
                   curState = kScreenB;
               if(ScreenA.ReturnState == 2)
                   curState = kScreenC;
               etc...
            }
     }
}

Also with this approach you may end up needing to handle entry conditions:

void InitState()
{
    switch(nextState)
    {
        case kScreenC:
            if(curState == kScreenA)
                ScreenC.InitFromA();
            if(curState == kScreenB)
                ScreenC.InitFromB();
            etc...
     }
}

So there must be a better way to design this, can you describe a better way?

Cheers,
slushduck

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

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

发布评论

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

评论(2

此岸叶落 2024-12-23 00:47:08

对于简单的菜单系统,您可以使用状态设计模式。
创建一个 Menu 接口并使用具体类(FirstMenu、SecondMenu 等)实现它。创建一个绘制菜单并具有 currentSelected Menu 的引用的 MenuContext。通过使用此 currentSelected 菜单,ContextMenu 知道将调用委托给正确的菜单。您还可以使用状态模式拥有一个优雅的菜单树。
https://en.wikipedia.org/wiki/State_pattern

For a simple menu system you can use the State Design Pattern.
Create an interface Menu and implement it with concrete classes (FirstMenu, SecondMenu, etc). Create a MenuContext that draws the menus and has a reference of the currentSelected Menu. By using this currentSelected Menu, the ContextMenu knows to delegate the calls to the proper Menu. You can have an elegant Menu Tree as well using the State Pattern.
https://en.wikipedia.org/wiki/State_pattern

小伙你站住 2024-12-23 00:47:08

实现 GUI 的正常方法是使用 MVP演示者优先。如果您的菜单很复杂,有多个屏幕,那么您需要几个 MVP 三人组。

Normal way of implementing GUIs is using MVP or presenter first. If your menu is complex with several screens, then you need several MVP trios.

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