Demo_ItemBox problem

haibo19981984

13-06-2010 10:38:46

I spent 3 days trying rebuild Demo_ItemBox in my project.
Today I build a frame image of demo finally.Just as imge "Demo_ItemBox frame.JPG".
It is not a simple task.
At first,I'm confused about the number of classes in demo.
In my mind,it should be a simple effect.
But the class is complex.
Second,I don't find the part that control the item's position in the demo.
In the other wards,whether can I set the item's position when item is initialized in a fixed area.
Maybe I don't say this problem distinctly.Just as imge "daoju.jpg".
I want item can fill in the grid.
Is it possible?

haibo19981984

14-06-2010 16:49:51

Maybe my problem is not right.
Ignore it.
Then begin to see the new problem.
I want to rebuild demo_ItemBox .
But I find the script must exist 1 ItemBox named "_Main",the key is in "BaseLayout.h".
Moreover,"BaseLayout.h" will read all script in "ItemBox.layout".
As result,my project will show 4 windows.If you move the window,you can see.
By all appearances,it is not rational.Because you can not demand user using a script which
exists a window named "_Main".
In general,my script include many widgets.
So whehter is there a method to instead of :

wraps::BaseLayout* t = new wraps::BaseLayout("ItemBox.layout");
t->assignBase(box1, "box_Items1");


All files in my project as follows:
Attribute.h,BaseCellView.h,BaseItemBox.h,BaseLayout.h,CellView.h,ItemBox.h,ItemData.h,
ItemDropInfo.h,ResourceItemInfo.h,ToolTip.h,WrapsAttribute.h,CellView.cpp,ItemBox.cpp,
myGUI_ItemBox.cpp,ResourceItemInfo.cpp,ToolTip.cpp.
note: I mended BaseLayout.h,instead "private" to "public".
I used ogre's excample frame instead of it.
my code(myGUI_ItemBox.cpp):

#include "ExampleApplication.h"
#include "MyGUI.h"
#include "MyGUI_OgrePlatform.h"
#include "ToolTip.h"
#include "ItemBox.h"
#include "ItemData.h"

using namespace Ogre;
class MyListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener
{
public:
MyListener(SceneManager *sceneMgr, RenderWindow* win, Camera* cam, MyGUI::Gui* pGUI)
: ExampleFrameListener(win, cam, true, true)
, mSceneMgr(sceneMgr)
, m_pGUI(pGUI)
, mContinue(true)
{
//
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
//
const OIS::MouseState &mouseState = mMouse->getMouseState();
mouseState.width = 1024;
mouseState.height = 768;

// load
MyGUI::LayoutManager::getInstance().load("ItemBox.layout");
MyGUI::FactoryManager::getInstance().registerFactory<demo::ResourceItemInfo>("Resource");
MyGUI::ResourceManager::getInstance().load("Resources.xml");
MyGUI::ResourceManager::getInstance().load("ItemBox_skin.xml");

MyGUI::ItemBoxPtr parentBox1 = m_pGUI->findWidget<MyGUI::ItemBox>("box_Items1");
demo::ItemBox * box1 = new demo::ItemBox(parentBox1);
box1->addItem(new demo::ItemData());
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item1", 5));
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item2", 5));
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item3", 5));
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item4", 5));
box1->eventStartDrag = MyGUI::newDelegate(this, &MyListener::notifyStartDrop);
box1->eventRequestDrop = MyGUI::newDelegate(this, &MyListener::notifyRequestDrop);
box1->eventDropResult = MyGUI::newDelegate(this, &MyListener::notifyEndDrop);
box1->eventChangeDDState = MyGUI::newDelegate(this, &MyListener::notifyDropState);
box1->eventNotifyItem = MyGUI::newDelegate(this, &MyListener::notifyNotifyItem);
box1->eventToolTip = MyGUI::newDelegate(this, &MyListener::notifyToolTip);

MyGUI::ItemBoxPtr parentBox2 = m_pGUI->findWidget<MyGUI::ItemBox>("box_Items2");
demo::ItemBox * box2 = new demo::ItemBox(parentBox2);
box2->addItem(new demo::ItemData());
box2->addItem(new demo::ItemData("info_Crystal_Clear_Item1", 5));
box2->addItem(new demo::ItemData("info_Crystal_Clear_Item2", 5));
box2->addItem(new demo::ItemData("info_Crystal_Clear_Item3", 5));
box2->addItem(new demo::ItemData("info_Crystal_Clear_Item4", 5));
box2->eventStartDrag = MyGUI::newDelegate(this, &MyListener::notifyStartDrop);
box2->eventRequestDrop = MyGUI::newDelegate(this, &MyListener::notifyRequestDrop);
box2->eventDropResult = MyGUI::newDelegate(this, &MyListener::notifyEndDrop);
box2->eventChangeDDState = MyGUI::newDelegate(this, &MyListener::notifyDropState);
box2->eventNotifyItem = MyGUI::newDelegate(this, &MyListener::notifyNotifyItem);
box2->eventToolTip = MyGUI::newDelegate(this, &MyListener::notifyToolTip);

//
m_ToolTip = new demo::ToolTip();
m_ToolTip->hide();
}
bool frameStarted(const FrameEvent& evt)
{
mKeyboard->capture();
mMouse->capture();

return mContinue;
}
//////////////////////////////////////////////////////////////////////////
bool mouseMoved( const OIS::MouseEvent &arg )
{
m_pGUI->injectMouseMove(arg.state.X.abs, arg.state.Y.abs, arg.state.Z.abs);
//...
return true;
}

bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
m_pGUI->injectMousePress(arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id));
//...
return true;
}

bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
m_pGUI->injectMouseRelease(arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id));
//...
return true;
}

bool keyPressed( const OIS::KeyEvent &arg )
{
if (arg.key == OIS::KC_ESCAPE || arg.key == OIS::KC_Q){
mContinue = false;
}

m_pGUI->injectKeyPress(MyGUI::KeyCode::Enum(arg.key), arg.text);
//...
return true;
}

bool keyReleased( const OIS::KeyEvent &arg )
{
m_pGUI->injectKeyRelease(MyGUI::KeyCode::Enum(arg.key));
//...
return true;
}
//////////////////////////////////////////////////////////////////////////
void notifyStartDrop(wraps::BaseLayout * _sender, wraps::DDItemInfo _info, bool & _result)
{
if (_info.sender_index != MyGUI::ITEM_NONE)
{
demo::ItemData * data = *static_cast<demo::ItemBox*>(_info.sender)->getItemDataAt<demo::ItemData*>(_info.sender_index);
_result = !data->isEmpty();
}
}

void notifyRequestDrop(wraps::BaseLayout * _sender, wraps::DDItemInfo _info, bool & _result)
{
//
if (_info.receiver_index == MyGUI::ITEM_NONE)
{
_result = false;
return;
}

//
if ((_info.sender == _info.receiver) && (_info.sender_index == _info.receiver_index))
{
_result = false;
return;
}

demo::ItemData * sender_data = *static_cast<demo::ItemBox*>(_info.sender)->getItemDataAt<demo::ItemData*>(_info.sender_index);
demo::ItemData * receiver_data = *static_cast<demo::ItemBox*>(_info.receiver)->getItemDataAt<demo::ItemData*>(_info.receiver_index);

_result = receiver_data->isEmpty() || receiver_data->compare(sender_data);
}

void notifyEndDrop(wraps::BaseLayout * _sender, wraps::DDItemInfo _info, bool _result)
{
if (_result)
{

demo::ItemData * sender_data = *static_cast<demo::ItemBox*>(_info.sender)->getItemDataAt<demo::ItemData*>(_info.sender_index);
demo::ItemData * receiver_data = *static_cast<demo::ItemBox*>(_info.receiver)->getItemDataAt<demo::ItemData*>(_info.receiver_index);

receiver_data->add(sender_data);
sender_data->clear();


static_cast<demo::ItemBox*>(_info.receiver)->setItemData(_info.receiver_index, receiver_data);
static_cast<demo::ItemBox*>(_info.sender)->setItemData(_info.sender_index, sender_data);
}

}
void notifyNotifyItem(wraps::BaseLayout * _sender, const MyGUI::IBNotifyItemData & _info)
{
}
void notifyDropState(wraps::BaseLayout * _sender, MyGUI::DDItemState _state)
{
}
void notifyToolTip(wraps::BaseLayout * _sender, const MyGUI::ToolTipInfo & _info, demo::ItemData * _data)
{
if (_info.type == MyGUI::ToolTipInfo::Show)
{
m_ToolTip->show(_data);
m_ToolTip->move(_info.point);
}
else if (_info.type == MyGUI::ToolTipInfo::Hide)
{
m_ToolTip->hide();
}
else if (_info.type == MyGUI::ToolTipInfo::Move)
{
m_ToolTip->move(_info.point);
}
}
public:
SceneManager* mSceneMgr;
// myGUI
MyGUI::Gui* m_pGUI;
//
bool mContinue;
//
demo::ToolTip* m_ToolTip;
};
class myGUI_ItemBox : public ExampleApplication
{
public:
myGUI_ItemBox()
{
}
~myGUI_ItemBox()
{
m_pGUI->shutdown();
delete m_pGUI;
m_pGUI = 0;
m_pPlatform->shutdown();
delete m_pPlatform;
m_pPlatform = 0;
}
void createScene(void)
{
//
mSceneMgr->setAmbientLight( ColourValue( 0.0, 0.0, 0.0) );
mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
// Shadow
mSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
mSceneMgr->setShadowTexturePixelFormat(PF_L8);
mSceneMgr->setShadowTextureCasterMaterial(Ogre::StringUtil::BLANK);
mSceneMgr->setShadowTextureReceiverMaterial(Ogre::StringUtil::BLANK);
mSceneMgr->setShadowTextureSelfShadow(false);
mSceneMgr->setShadowColour( ColourValue(0.3, 0.3, 0.3));
mSceneMgr->setShadowTextureSettings(2048,1);
mSceneMgr->setShadowFarDistance(600);
// Camera
mCamera->setPosition(Vector3(0,200,200));
mCamera->lookAt(Vector3(0,0,0));
// light
Vector3 lightdir(0, -1, -1);
lightdir.normalise();
Light* l = mSceneMgr->createLight("tstLight");
l->setType(Light::LT_DIRECTIONAL);
l->setDirection(lightdir);
l->setDiffuseColour(ColourValue::White);
l->setSpecularColour(ColourValue(0.1, 0.1, 0.1));
// ground
MeshManager::getSingleton().createPlane("ground", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Plane(Vector3::UNIT_Y, 0), 1000, 1000, 20, 20, true, 1, 6, 6, Vector3::UNIT_Z);
Entity* ground = mSceneMgr->createEntity("Ground", "ground");
ground->setMaterialName("Examples/Rockwall");
ground->setCastShadows(false);
mSceneMgr->getRootSceneNode()->attachObject(ground);
// myGUI
m_pPlatform = new MyGUI::OgrePlatform();
m_pPlatform->initialise(mWindow, mSceneMgr);
m_pGUI = new MyGUI::Gui();
m_pGUI->initialise();
}
void createFrameListener(void)
{
mFrameListener = new MyListener(mSceneMgr, mWindow, mCamera, m_pGUI);
mRoot->addFrameListener(mFrameListener);
}


public:
MyGUI::Gui* m_pGUI;
MyGUI::OgrePlatform* m_pPlatform;
};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
#else
int main(int argc, char *argv[])
#endif
{
try{
myGUI_ItemBox app;
app.go();
}catch (Ogre::Exception& e){
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occurred: " << e.getFullDescription().c_str() << std::endl;
#endif
}

return 0;
}

ItemBox.layout:

<?xml version="1.0" encoding="UTF-8"?>
<MyGUI type="Layout">
<Widget type="Window" skin="WindowCS" position="600 450 365 256" layer="Overlapped" name="_Main">
<Property key="Widget_Caption" value="drag and drop demo"/>
<Property key="Window_MinSize" value="170 100"/>
<Widget type="ItemBox" skin="ItemBoxH" position="0 0 356 220" align="Stretch" name="box_Items1">
<Property key="Widget_NeedToolTip" value="true"/>
</Widget>
</Widget>
<Widget type="Window" skin="WindowCS" position="50 100 365 256" layer="Overlapped" name="_Main2">
<Property key="Widget_Caption" value="drag and drop demo"/>
<Property key="Window_MinSize" value="170 100"/>
<Widget type="ItemBox" skin="ItemBoxV" position="0 0 356 220" align="Stretch" name="box_Items2">
<Property key="Widget_NeedToolTip" value="true"/>
</Widget>
</Widget>
</MyGUI>

I had copied other script:CellView.layout,ItemBox_skin.xml,Resources.xml,ToolTip.layout.
Except "ItemBoxH.layout" and "ItemBoxV.layout".

haibo19981984

17-06-2010 03:45:38

Hai,Altren.
I had mended my code,just as above post.
It can run.
But just as what I say above ,how can I call "demo::ItemBox" just as general widget?

haibo19981984

18-06-2010 06:20:26


demo::ItemBox * box1;
MyGUI::Widget* widget = m_pGUI->findWidgetT("box_Items1");
// this is only my dream
box1 = widget;

Whether is there a method to come true above code?

Altren

18-06-2010 14:16:10

It's not a widget - it is wrapper above ItemBox. So you can't find it through MyGUI::Gui. You should save pointer to demo::ItemBox when you create it and then use it wherever you need it.

haibo19981984

19-06-2010 04:17:05

At first,I find why there are additional 2 empty windows in my project finally.
Because my code has "MyGUI::LayoutManager::getInstance().load("ItemBox.layout");" and
"wraps::BaseLayout* t = new wraps::BaseLayout("ItemBox.layout");".
As result, "ItemBox.layout" is loaded repeatedly.
Second,thank for Altren's reply.
From what you say,I think demo::ItemBox should be created in code.
And attach it to parent widget when it be created.
Parent widget should be found by its name.
It may be a feasible method.
I am trying to come true it.
After several hours work,I find good method to create demo::ItemBox finally.
I had amended the second post.
The main modification is:
instead

ItemBox * box1;
wraps::BaseLayout* t = new wraps::BaseLayout("ItemBox.layout");
t->assignBase(box1, "box_Items1");
...
ItemBox * box2;
t->assignBase(box2, "box_Items2");

to

MyGUI::ItemBoxPtr parentBox1 = m_pGUI->findWidget<MyGUI::ItemBox>("box_Items1");
demo::ItemBox * box1 = new demo::ItemBox(parentBox1);
...
MyGUI::ItemBoxPtr parentBox2 = m_pGUI->findWidget<MyGUI::ItemBox>("box_Items2");
demo::ItemBox * box2 = new demo::ItemBox(parentBox2);

haibo19981984

19-06-2010 14:12:49

First problem:see "box1->addItem(new demo::ItemData("info_Crystal_Clear_Item1", 1)); // 1" and the image"ItemBox1.JPG" .
You can see the number "1" doesn't show.
If I use "box1->addItem(new demo::ItemData("info_Crystal_Clear_Item1", 2); // 1",it is normal.
Why?
Second problem:from image"ItemBox1.JPG",we can see the item images put in order by vertical.
Is there a method to make it by horizontal.

// ItemBox1
MyGUI::ItemBoxPtr parentBox1 = m_pGUI->findWidget<MyGUI::ItemBox>("box_Items1");
demo::ItemBox * box1 = new demo::ItemBox(parentBox1);
box1->addItem(new demo::ItemData()); // 0
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item1", 1)); // 1
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item2", 2)); // 2
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item3", 3)); // 3
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item4", 4)); // 4
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item5", 5)); // 5
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item6", 6)); // 6
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item7", 7)); // 7
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item8", 8)); // 8
box1->addItem(new demo::ItemData("info_Crystal_Clear_Item9", 9)); // 9
box1->addItem(new demo::ItemData()); // 10
box1->addItem(new demo::ItemData()); // 11
box1->addItem(new demo::ItemData()); // 12
box1->addItem(new demo::ItemData()); // 13
box1->addItem(new demo::ItemData()); // 14
box1->addItem(new demo::ItemData()); // 15
box1->addItem(new demo::ItemData()); // 16
box1->addItem(new demo::ItemData()); // 17
box1->addItem(new demo::ItemData()); // 18
box1->addItem(new demo::ItemData()); // 19
box1->addItem(new demo::ItemData()); // 20
box1->addItem(new demo::ItemData()); // 21
box1->addItem(new demo::ItemData()); // 22
box1->addItem(new demo::ItemData()); // 23
box1->addItem(new demo::ItemData()); // 24
box1->addItem(new demo::ItemData()); // 25
box1->addItem(new demo::ItemData()); // 26
box1->addItem(new demo::ItemData()); // 27

by the way, I use my custom ItemBox skin:

<Resource type="ResourceSkin" name = "Game/ItemBox" size = "11 11" texture = "#{MyGUI_Theme_Texture}">
<Property key="AlignVert" value = "false" />
<Property key="DragLayer" value = "DragAndDrop" />

<Child type="Widget" skin="Game/Transparent" offset = "0 0 15 15" align = "Stretch" name = "Client"/>
</Resource>

"Game/Transparent" is a transparent skin.

Altren

19-06-2010 15:32:59

You can't see "1" in count because it was made so - open CellView.cpp and replace
mTextBack->setCaption(((_data->getCount() > 1) && ( ! _info.drag)) ? MyGUI::utility::toString(_data->getCount()) : "");
mTextFront->setCaption(((_data->getCount() > 1) && ( ! _info.drag)) ? MyGUI::utility::toString(_data->getCount()) : "");
with mTextBack->setCaption((( ! _info.drag)) ? MyGUI::utility::toString(_data->getCount()) : "");
mTextFront->setCaption((( ! _info.drag)) ? MyGUI::utility::toString(_data->getCount()) : "");


About vertical align - try <Property key="AlignVert" value = "true" />I'm not sure, but as far as I remember it works so.

haibo19981984

21-06-2010 06:48:27

What you say is right.The problem is sovled.