make context menu with cegui[resolved]

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

silenttree
Not too shy to talk
Not too shy to talk
Posts: 30
Joined: Wed Nov 01, 2006 05:59
Contact:

make context menu with cegui[resolved]

Postby silenttree » Thu Nov 02, 2006 13:08

How can i make a context menu with cegui ?



----------------------------------
ITS RESOLVED, THANKS FOR CE'S HELP!
Last edited by silenttree on Thu Nov 02, 2006 15:02, edited 2 times in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Thu Nov 02, 2006 14:31

Ok, this is a complete example, but be aware that for real world usage, there are various things that you'd need to add to make it more robust and/or usable

Consider this initialisation code:

Code: Select all

void initialise()
{
    using namespace CEGUI;

    // load some of the base files.
    SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
    System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
    FontManager::getSingleton().createFont("Commonwealth-10.font");

    WindowManager& winMgr = WindowManager::getSingleton();

    // create the root window and set it as active sheet.
    DefaultWindow* root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "root");
    System::getSingleton().setGUISheet(root);

    // create a frame window and add it to the active sheet.
    FrameWindow* wnd = (FrameWindow*)winMgr.createWindow("TaharezLook/FrameWindow", "frame_window");
    wnd->setPosition(UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));
    wnd->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.5f)));
    root->addChildWindow(wnd);
    // set a title for the window.
    wnd->setText("Context Menu Example");

    // load a menu via a layout and add it to the frame window.
    PopupMenu* ctxMenu = (PopupMenu*)winMgr.loadWindowLayout("context_menu.layout");
    wnd->addChildWindow(ctxMenu);

    // attach handler to process the mouse button up events.
    wnd->subscribeEvent( Window::EventMouseButtonUp, &myMouseHandler);
}


The handler for the mouse button is this, though would normally have some checks to make sure the window is the one with the context menu (you could set a user-string on the Window with the name of the context menu window and handle things that way).

Code: Select all

bool myMouseHandler(const CEGUI::EventArgs& args)
{
    using namespace CEGUI;

    // convert event to MouseEventArgs
    const MouseEventArgs& ma =
        static_cast<const MouseEventArgs&>(args);

    // was it the right mouse button?
    if (ma.button == RightButton)
    {
        UVector2 popup_pos;

        // get x position of mouse as UDim absolute position within the
        // window
        popup_pos.d_x = cegui_absdim(
            CoordConverter::screenToWindowX(*ma.window, ma.position.d_x));

        // get y position of mouse as UDim absolute position within the
        // window
        popup_pos.d_y = cegui_absdim(
            CoordConverter::screenToWindowY(*ma.window, ma.position.d_y));

        // get pointer to the context menu window.
        Window* ctxmenu = WindowManager::getSingleton().getWindow("ctxmenu");

        // set position of menu so it's around where the mouse was clicked.
        ctxmenu->setPosition( popup_pos );

        // show the menu.
        ctxmenu->show();

        // event was handled.
        return true;
    }

    // event was not handled.
    return false;
}


The is the layout (context_menu.layout) - though this could easily be created in code.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<GUILayout>
    <Window Type="TaharezLook/PopupMenu" Name="ctxmenu">
        <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{0,128},{0,300}}" />
        <Window Type="TaharezLook/MenuItem" >
            <Property Name="Text" Value="Cut" />
        </Window>
        <Window Type="TaharezLook/MenuItem" >
            <Property Name="Text" Value="Copy" />
        </Window>
        <Window Type="TaharezLook/MenuItem" >
            <Property Name="Text" Value="Paste" />
        </Window>
        <Window Type="TaharezLook/MenuItem" >
            <Property Name="Text" Value="Select All" />
        </Window>
    </Window>
</GUILayout>


HTH

CE.

silenttree
Not too shy to talk
Not too shy to talk
Posts: 30
Joined: Wed Nov 01, 2006 05:59
Contact:

Postby silenttree » Thu Nov 02, 2006 14:40

just read it complete!

practise right now!

silenttree
Not too shy to talk
Not too shy to talk
Posts: 30
Joined: Wed Nov 01, 2006 05:59
Contact:

Postby silenttree » Thu Nov 02, 2006 15:01

GREAT WORK!

JUST WANT I WANT!

i try this by add PopupMenu & MenuItem in c++ before i ask here, but doesnt work. :oops:

I'll try again later!


Return to “Help”

Who is online

Users browsing this forum: No registered users and 33 guests