(CEGUI)DragDropDemo例子的延伸(添加,删除,双击ITEM) -------制作背包系统

//例子的初始化

bool DragDropDemo::initialiseSample()
{
    using namespace CEGUI;

    // load windows look
    SchemeManager::getSingleton().create("WindowsLook.scheme");

    // load font and setup default if not loaded via scheme
    FontManager::getSingleton().create("DejaVuSans-10.font");

    // set up defaults
    System::getSingleton().setDefaultMouseCursor("WindowsLook", "MouseArrow");
    System::getSingleton().setDefaultFont("DejaVuSans-10");

    // load the drive icons imageset
    ImagesetManager::getSingleton().create("DriveIcons.imageset");

    // load the initial layout
    System::getSingleton().setGUISheet(
        WindowManager::getSingleton().loadWindowLayout("DragDropDemo.layout"));


 WindowManager& wmger = WindowManager::getSingleton();
 Window* root = wmger.getWindow("Root");

//打开CEGUI的Tooltip功能

 System::getSingleton ().setDefaultTooltip ("WindowsLook/Tooltip");
 // 显示是否有物品;
 PushButton* btn = static_cast<PushButton*>(wmger.createWindow("WindowsLook/Button","AddBtn"));
 btn->setPosition(UVector2(cegui_reldim(0.0f),cegui_reldim(0.0f)));
 btn->setSize(UVector2(cegui_reldim(0.12f),cegui_reldim(0.06f)));
 btn->setText("ShowItem");
 root->addChildWindow(btn);
 btn->subscribeEvent(PushButton::EventClicked,Event::Subscriber(&DragDropDemo::addClose,this));

 // 添加按钮
 PushButton* addItemBtn = static_cast<PushButton*>(wmger.createWindow("WindowsLook/Button","AddItemBtn"));
 root->addChildWindow(addItemBtn);
 addItemBtn->setPosition(UVector2(cegui_reldim(0.0f),cegui_reldim(0.2f)));
 addItemBtn->setSize(UVector2(cegui_reldim(0.12f),cegui_reldim(0.06f)));
 addItemBtn->setText("AddItemBtn");
 addItemBtn->subscribeEvent(PushButton::EventClicked,Event::Subscriber(&DragDropDemo::AddItem,this));
 // 删除按钮
 PushButton* delItemBtn = static_cast<PushButton*>(wmger.createWindow("WindowsLook/Button","DelItemBtn"));
 root->addChildWindow(delItemBtn);
 delItemBtn->setPosition(UVector2(cegui_reldim(0.0f),cegui_reldim(0.4f)));
 delItemBtn->setSize(UVector2(cegui_reldim(0.12f),cegui_reldim(0.06f)));
 delItemBtn->setText("DelItemBtn");
 delItemBtn->setProperty("Tooltip","Del a Item!/nIt's a button");
 delItemBtn->subscribeEvent(PushButton::EventClicked,Event::Subscriber(&DragDropDemo::DelItem,this));

 // setup events
    subscribeEvents();

    // success!
    return true;
}

//----------------------------------------------------------------------------//
void DragDropDemo::cleanupSample()
{
    // nothing doing in here!
}

//----------------------------------------------------------------------------//
void DragDropDemo::subscribeEvents()
{
    using namespace CEGUI;

    WindowManager& wmgr = WindowManager::getSingleton();

    /*
     * Subscribe handler to deal with user closing the frame window
     */
    CEGUI_TRY
    {
        Window* main_wnd = wmgr.getWindow("Root/MainWindow");
        main_wnd->subscribeEvent(
            FrameWindow::EventCloseClicked,
            Event::Subscriber(&DragDropDemo::handle_CloseButton, this));
    }
    // if something goes wrong, log the issue but do not bomb!
    CEGUI_CATCH(CEGUI::Exception&)
    {}

    /*
     * Subscribe the same handler to each of the twelve slots
     */
    String base_name = "Root/MainWindow/Slot";

    for (int i = 1; i <= 12; ++i)
    {
        CEGUI_TRY
        {
            // get the window pointer for this slot
            Window* wnd =
                wmgr.getWindow(base_name + PropertyHelper::intToString(i));

            // subscribe the handler.
            wnd->subscribeEvent(
                Window::EventDragDropItemDropped,
                Event::Subscriber(&DragDropDemo::handle_ItemDropped, this));
        }
        // if something goes wrong, log the issue but do not bomb!
        CEGUI_CATCH(CEGUI::Exception&)
        {}
    }
}

//----------------------------------------------------------------------------//
bool DragDropDemo::handle_ItemDropped(const CEGUI::EventArgs& args)
{
    using namespace CEGUI;

    // cast the args to the 'real' type so we can get access to extra fields
    const DragDropEventArgs& dd_args =
        static_cast<const DragDropEventArgs&>(args);

    //物品的移动

    if (!dd_args.window->getChildCount())
    {
        // add dragdrop item as child of target if target has no item already
        dd_args.window->addChildWindow(dd_args.dragDropItem);
        // Now we must reset the item position from it's 'dropped' location,
        // since we're now a child of an entirely different window
        dd_args.dragDropItem->setPosition(
            UVector2(UDim(0.05f, 0),UDim(0.05f, 0)));

  
    }
    return true;
}

//----------------------------------------------------------------------------//
bool DragDropDemo::handle_CloseButton(const CEGUI::EventArgs&)
{
    d_sampleApp->setQuitting();
    return true;
}

//----------------------------------------------------------------------------//
bool DragDropDemo::addClose(const CEGUI::EventArgs& agrs)
{
 using namespace CEGUI;

 string base_name = "Root/MainWindow/Slot";
 for(int i=0;i<12;i++)
 {
  Window* window = WindowManager::getSingleton().getWindow(base_name+PropertyHelper::intToString(i+1));
  //设置ID
  //window->setProperty("ID",PropertyHelper::intToString(i+1));
  size_t ishas = window->getChildCount();
  if (ishas>0)
  {
   Window* windowTemp = window->getChild(0);
   if (!windowTemp)
   {
    return false;
   }
   
   size_t aa = windowTemp->getChildCount();
   if (aa>0)
   {
     Window* tempWindow = window->getChild(0);
     //size_t bb = tempWindow->getChildCount();
     //if (bb>0)
    {
     char temp[128]={0};
     sprintf(temp,"%s",tempWindow->getChild(0)->getName().c_str());
     MessageBox(NULL,temp,"",0);
    }
    
   }
  }else
  {
   MessageBox(NULL,"NULL","",0);
  }
 } 
 return true;
}
/// 添加物品

bool DragDropDemo::AddItem(const CEGUI::EventArgs &args)
{
 using namespace CEGUI;
 WindowManager& winMger = WindowManager::getSingleton();
 Window* root = winMger.getWindow("Root");
 string base_name = "Root/MainWindow/Slot";

//为了防止创建同名的控件
 static int _num = 0;
 for(int i=0;i<12;i++)
 {
  Window* window = WindowManager::getSingleton().getWindow(base_name+PropertyHelper::intToString(i+1));
  if (!window->getChildCount())
  {
   //创建物品
   Window* statiImg = winMger.createWindow("WindowsLook/StaticImage","staticT"+PropertyHelper::intToString(_num));
   statiImg->setPosition(UVector2(cegui_reldim(0.0f),cegui_reldim(0.0f)));
   statiImg->setSize(UVector2(cegui_reldim(1.0f),cegui_reldim(1.0f)));
   statiImg->setProperty("Image","set:DriveIcons image:Blue");  
   statiImg->setProperty("MousePassThroughEnabled","True");
   //创建拖动框
   DragContainer* dContainer = static_cast<DragContainer*>(winMger.createWindow("DragContainer","DragContainerT"+PropertyHelper::intToString(_num)));
   window->addChildWindow(dContainer);
   dContainer->addChildWindow(statiImg);
   dContainer->setPosition(UVector2(cegui_reldim(0.05f),cegui_reldim(0.05f)));
   dContainer->setSize(UVector2(cegui_reldim(0.90f),cegui_reldim(0.90f)));

   //添加标签其实很简单,就2句代码(紫色部分,非注释)
   dContainer->setProperty("Tooltip","staticT"+PropertyHelper::intToString(_num));
   dContainer->subscribeEvent(DragContainer::EventMouseDoubleClick,Event::Subscriber(&DragDropDemo::DBClick,this));
   _num++;
   return true;
  }
  
 }
 
 return true;
}

//删除物品(丢弃物品)
bool DragDropDemo::DelItem(const CEGUI::EventArgs &args)
{
 using namespace CEGUI;
 WindowManager& winMger = WindowManager::getSingleton();
 Window* root = winMger.getWindow("Root");
 string base_name = "Root/MainWindow/Slot";
 for(int i=0;i<12;i++)
 {
  Window* window = WindowManager::getSingleton().getWindow(base_name+PropertyHelper::intToString(i+1));
  if (window->getChildCount()>0)
  {
   window->getChild(0)->destroy();
   //window->destroy();
   /*window->ChildList
   char temp[128]={0};
   sprintf(temp,"%s",window->getChild(0)->getName().c_str());
   MessageBox(NULL,temp,"",0);*/
   return true;
  }
 }
 return true;
}

 

//双击物品,如背包中的使用物品等功能

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

//对于CEGUI的EVENT还不是很懂,忘高手指点,或提供点资料
 const DragDropEventArgs& dd_args =
  static_cast<const DragDropEventArgs&>(args);

 if (dd_args.window->getChildCount()>0)
 {  
  dd_args.window->destroy();  
 }
 return true;
}

 

 

             以上为CEGUI例子中DragDropDemo的延伸,比较简单(高手绕行),这个例子应该主要就是用于背包的功能,所以延伸了下,未测试BUG,有BUG请告知

 

你可能感兴趣的:((CEGUI)DragDropDemo例子的延伸(添加,删除,双击ITEM) -------制作背包系统)