Ogre SdkTrays UI c++ code override with c# (.net2.0)

andyhebear1

11-04-2014 04:18:36

c++ snap code

/*
-----------------------------------------------------------------------------
This source file is part of OGRE
-----------------------------------------------------------------------------
*/
#ifndef __SdkTrays_H__
#define __SdkTrays_H__

#include "Ogre.h"
#include "OgreFontManager.h"
#include "OgreBorderPanelOverlayElement.h"
#include "OgreTextAreaOverlayElement.h"
#include <math.h>

#if OGRE_COMPILER == OGRE_COMPILER_MSVC
// TODO - remove this
# pragma warning (disable : 4244)
#endif

namespace OgreBites
{
enum TrayLocation // enumerator values for widget tray anchoring locations
{
TL_TOPLEFT,
TL_TOP,
TL_TOPRIGHT,
TL_LEFT,
TL_CENTER,
TL_RIGHT,
TL_BOTTOMLEFT,
TL_BOTTOM,
TL_BOTTOMRIGHT,
TL_NONE
};

enum ButtonState // enumerator values for button states
{
BS_UP,
BS_OVER,
BS_DOWN
};

// forward widget class declarations
class Widget;
class Button;
class SelectMenu;
class Label;
class Slider;
class CheckBox;

/*=============================================================================
| Listener class for responding to tray events.
=============================================================================*/


/*=============================================================================
| Abstract base class for all widgets.
=============================================================================*/
class Widget
{
public:

Widget()
{
mTrayLoc = TL_NONE;
mElement = 0;
mListener = 0;
}

virtual ~Widget() {}

void cleanup()
{
if (mElement) nukeOverlayElement(mElement);
mElement = 0;
}

/*-----------------------------------------------------------------------------
| Static utility method to recursively delete an overlay element plus
| all of its children from the system.
-----------------------------------------------------------------------------*/
static void nukeOverlayElement(Ogre::OverlayElement* element)
{
Ogre::OverlayContainer* container = dynamic_cast<Ogre::OverlayContainer*>(element);
if (container)
{
std::vector<Ogre::OverlayElement*> toDelete;

Ogre::OverlayContainer::ChildIterator children = container->getChildIterator();
while (children.hasMoreElements())
{
toDelete.push_back(children.getNext());
}

for (unsigned int i = 0; i < toDelete.size(); i++)
{
nukeOverlayElement(toDelete);
}
}
if (element)
{
Ogre::OverlayContainer* parent = element->getParent();
if (parent) parent->removeChild(element->getName());
Ogre::OverlayManager::getSingleton().destroyOverlayElement(element);
}
}

/*-----------------------------------------------------------------------------
| Static utility method to check if the cursor is over an overlay element.
-----------------------------------------------------------------------------*/
static bool isCursorOver(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos, Ogre::Real voidBorder = 0)
{
Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();
Ogre::Real l = element->_getDerivedLeft() * om.getViewportWidth();
Ogre::Real t = element->_getDerivedTop() * om.getViewportHeight();
Ogre::Real r = l + element->getWidth();
Ogre::Real b = t + element->getHeight();

return (cursorPos.x >= l + voidBorder && cursorPos.x <= r - voidBorder &&
cursorPos.y >= t + voidBorder && cursorPos.y <= b - voidBorder);
}

/*-----------------------------------------------------------------------------
| Static utility method used to get the cursor's offset from the center
| of an overlay element in pixels.
-----------------------------------------------------------------------------*/
static Ogre::Vector2 cursorOffset(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos)
{
Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();
return Ogre::Vector2(cursorPos.x - (element->_getDerivedLeft() * om.getViewportWidth() + element->getWidth() / 2),
cursorPos.y - (element->_getDerivedTop() * om.getViewportHeight() + element->getHeight() / 2));
}


void resourceGroupLoadStarted(const Ogre::String& groupName, size_t resourceCount)
{
mLoadInc = mGroupLoadProportion / resourceCount;
mLoadBar->setCaption("Loading...");
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
mWindow->update();
#endif
}

void resourceLoadStarted(const Ogre::ResourcePtr& resource)
{
mLoadBar->setComment(resource->getName());
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
mWindow->update();
#endif
}

void resourceLoadEnded()
{
mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
mWindow->update();
#endif
}

void worldGeometryStageStarted(const Ogre::String& description)
{
mLoadBar->setComment(description);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
mWindow->update();
#endif
}

void worldGeometryStageEnded()
{
mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
mWindow->update();
#endif
}

void resourceGroupLoadEnded(const Ogre::String& groupName) {}

/*-----------------------------------------------------------------------------
| Toggles visibility of advanced statistics.
-----------------------------------------------------------------------------*/
void labelHit(Label* label)
{
if (mStatsPanel->getOverlayElement()->isVisible())
{
mStatsPanel->getOverlayElement()->hide();
mFpsLabel->getOverlayElement()->setWidth(150);
removeWidgetFromTray(mStatsPanel);
}
else
{
mStatsPanel->getOverlayElement()->show();
mFpsLabel->getOverlayElement()->setWidth(180);
moveWidgetToTray(mStatsPanel, mFpsLabel->getTrayLocation(), locateWidgetInTray(mFpsLabel) + 1);
}
}

/*-----------------------------------------------------------------------------
| Destroys dialog widgets, notifies listener, and ends high priority session.
-----------------------------------------------------------------------------*/
void buttonHit(Button* button)
{
if (mListener)
{
if (button == mOk) mListener->okDialogClosed(mDialog->getText());
else mListener->yesNoDialogClosed(mDialog->getText(), button == mYes);
}
closeDialog();
}

/*-----------------------------------------------------------------------------
| Processes mouse button down events. Returns true if the event was
| consumed and should not be passed on to other handlers.
-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
bool injectMouseDown(const OIS::MultiTouchEvent& evt)
#else
bool injectMouseDown(const OIS::MouseEvent& evt, OIS::MouseButtonID id)
#endif
{
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
// only process left button when stuff is visible
if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false;
#else
// only process touches when stuff is visible
if (!mCursorLayer->isVisible()) return false;
#endif
Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop());

mTrayDrag = false;

if (mExpandedMenu) // only check top priority widget until it passes on
{
mExpandedMenu->_cursorPressed(cursorPos);
if (!mExpandedMenu->isExpanded()) setExpandedMenu(0);
return true;
}

if (mDialog) // only check top priority widget until it passes on
{
mDialog->_cursorPressed(cursorPos);
if (mOk) mOk->_cursorPressed(cursorPos);
else
{
mYes->_cursorPressed(cursorPos);
mNo->_cursorPressed(cursorPos);
}
return true;
}

for (unsigned int i = 0; i < 9; i++) // check if mouse is over a non-null tray
{
if (mTrays->isVisible() && Widget::isCursorOver(mTrays, cursorPos, 2))
{
mTrayDrag = true; // initiate a drag that originates in a tray
break;
}
}

for (unsigned int i = 0; i < mWidgets[9].size(); i++) // check if mouse is over a non-null tray's widgets
{
if (mWidgets[9]->getOverlayElement()->isVisible() &&
Widget::isCursorOver(mWidgets[9]->getOverlayElement(), cursorPos))
{
mTrayDrag = true; // initiate a drag that originates in a tray
break;
}
}

if (!mTrayDrag) return false; // don't process if mouse press is not in tray

for (unsigned int i = 0; i < 10; i++)
{
if (!mTrays->isVisible()) continue;

for (unsigned int j = 0; j < mWidgets.size(); j++)
{
Widget* w = mWidgets[j];
if (!w->getOverlayElement()->isVisible()) continue;
w->_cursorPressed(cursorPos); // send event to widget

SelectMenu* m = dynamic_cast<SelectMenu*>(w);
if (m && m->isExpanded()) // a menu has begun a top priority session
{
setExpandedMenu(m);
return true;
}
}
}

return true; // a tray click is not to be handled by another party
}

/*-----------------------------------------------------------------------------
| Processes mouse button up events. Returns true if the event was
| consumed and should not be passed on to other handlers.
-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
bool injectMouseUp(const OIS::MultiTouchEvent& evt)
#else
bool injectMouseUp(const OIS::MouseEvent& evt, OIS::MouseButtonID id)
#endif
{
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
// only process left button when stuff is visible
if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false;
#else
// only process touches when stuff is visible
if (!mCursorLayer->isVisible()) return false;
#endif
Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop());

if (mExpandedMenu) // only check top priority widget until it passes on
{
mExpandedMenu->_cursorReleased(cursorPos);
return true;
}

if (mDialog) // only check top priority widget until it passes on
{
mDialog->_cursorReleased(cursorPos);
if (mOk) mOk->_cursorReleased(cursorPos);
else
{
mYes->_cursorReleased(cursorPos);
// very important to check if second button still exists, because first button could've closed the popup
if (mNo) mNo->_cursorReleased(cursorPos);
}
return true;
}

if (!mTrayDrag) return false; // this click did not originate in a tray, so don't process

Widget* w;

for (unsigned int i = 0; i < 10; i++)
{
if (!mTrays->isVisible()) continue;

for (unsigned int j = 0; j < mWidgets.size(); j++)
{
w = mWidgets[j];
if (!w->getOverlayElement()->isVisible()) continue;
w->_cursorReleased(cursorPos); // send event to widget
}
}

mTrayDrag = false; // stop this drag
return true; // this click did originate in this tray, so don't pass it on
}

/*-----------------------------------------------------------------------------
| Updates cursor position. Returns true if the event was
| consumed and should not be passed on to other handlers.
-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
bool injectMouseMove(const OIS::MultiTouchEvent& evt)
#else
bool injectMouseMove(const OIS::MouseEvent& evt)
#endif
{
if (!mCursorLayer->isVisible()) return false; // don't process if cursor layer is invisible

Ogre::Vector2 cursorPos(evt.state.X.abs, evt.state.Y.abs);
mCursor->setPosition(cursorPos.x, cursorPos.y);

if (mExpandedMenu) // only check top priority widget until it passes on
{
mExpandedMenu->_cursorMoved(cursorPos);
return true;
}

if (mDialog) // only check top priority widget until it passes on
{
mDialog->_cursorMoved(cursorPos);
if (mOk) mOk->_cursorMoved(cursorPos);
else
{
mYes->_cursorMoved(cursorPos);
mNo->_cursorMoved(cursorPos);
}
return true;
}

Widget* w;

for (unsigned int i = 0; i < 10; i++)
{
if (!mTrays->isVisible()) continue;

for (unsigned int j = 0; j < mWidgets.size(); j++)
{
w = mWidgets[j];
if (!w->getOverlayElement()->isVisible()) continue;
w->_cursorMoved(cursorPos); // send event to widget
}
}

if (mTrayDrag) return true; // don't pass this event on if we're in the middle of a drag
return false;
}

protected:

/*-----------------------------------------------------------------------------
| Internal method to prioritise / deprioritise expanded menus.
-----------------------------------------------------------------------------*/
void setExpandedMenu(SelectMenu* m)
{
if (!mExpandedMenu && m)
{
Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)m->getOverlayElement();
Ogre::OverlayContainer* eb = (Ogre::OverlayContainer*)c->getChild(m->getName() + "/MenuExpandedBox");
eb->_update();
eb->setPosition
((unsigned int)(eb->_getDerivedLeft() * Ogre::OverlayManager::getSingleton().getViewportWidth()),
(unsigned int)(eb->_getDerivedTop() * Ogre::OverlayManager::getSingleton().getViewportHeight()));
c->removeChild(eb->getName());
mPriorityLayer->add2D(eb);
}
else if(mExpandedMenu && !m)
{
Ogre::OverlayContainer* eb = mPriorityLayer->getChild(mExpandedMenu->getName() + "/MenuExpandedBox");
mPriorityLayer->remove2D(eb);
((Ogre::OverlayContainer*)mExpandedMenu->getOverlayElement())->addChild(eb);
}

mExpandedMenu = m;
}

Ogre::String mName; // name of this tray system
Ogre::RenderWindow* mWindow; // render window
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
OIS::MultiTouch* mMouse; // multitouch device
#else
OIS::Mouse* mMouse; // mouse device
#endif
Ogre::Overlay* mBackdropLayer; // backdrop layer
Ogre::Overlay* mTraysLayer; // widget layer
Ogre::Overlay* mPriorityLayer; // top priority layer
Ogre::Overlay* mCursorLayer; // cursor layer
Ogre::OverlayContainer* mBackdrop; // backdrop
Ogre::OverlayContainer* mTrays[10]; // widget trays
WidgetList mWidgets[10]; // widgets
WidgetList mWidgetDeathRow; // widget queue for deletion
Ogre::OverlayContainer* mCursor; // cursor
SdkTrayListener* mListener; // tray listener
Ogre::Real mWidgetPadding; // widget padding
Ogre::Real mWidgetSpacing; // widget spacing
Ogre::Real mTrayPadding; // tray padding
bool mTrayDrag; // a mouse press was initiated on a tray
SelectMenu* mExpandedMenu; // top priority expanded menu widget
TextBox* mDialog; // top priority dialog widget
Ogre::OverlayContainer* mDialogShade; // top priority dialog shade
Button* mOk; // top priority OK button
Button* mYes; // top priority Yes button
Button* mNo; // top priority No button
bool mCursorWasVisible; // cursor state before showing dialog
Label* mFpsLabel; // FPS label
ParamsPanel* mStatsPanel; // frame stats panel
DecorWidget* mLogo; // logo
ProgressBar* mLoadBar; // loading bar
Ogre::Real mGroupInitProportion; // proportion of load job assigned to initialising one resource group
Ogre::Real mGroupLoadProportion; // proportion of load job assigned to loading one resource group
Ogre::Real mLoadInc; // loading increment
Ogre::GuiHorizontalAlignment mTrayWidgetAlign[10]; // tray widget alignments
};
}

#endif


i override this c++ code by c# (.net2.0)
csharp snap code

namespace Mogre_Procedural.MogreBites
{
using System;
using Mogre;
using System.Collections.Generic;
using Math = System.Math;
using InputContext = MOIS.Mouse;

public enum TrayLocation : int // enumerator values for widget tray anchoring locations
{
TL_TOPLEFT,
TL_TOP,
TL_TOPRIGHT,
TL_LEFT,
TL_CENTER,
TL_RIGHT,
TL_BOTTOMLEFT,
TL_BOTTOM,
TL_BOTTOMRIGHT,
TL_NONE
}

public enum ButtonState : int // enumerator values for button states
{
BS_UP,
BS_OVER,
BS_DOWN
}



// =============================================================================
// | Listener class for responding to tray events.
// =============================================================================
public class SdkTrayListener
{

public virtual void Dispose() {
}
public virtual void buttonHit(Button button) {
}
public virtual void itemSelected(SelectMenu menu) {
}
public virtual void labelHit(Label label) {
}
public virtual void sliderMoved(Slider slider) {
}
public virtual void checkBoxToggled(CheckBox box) {
}
public virtual void okDialogClosed(string message) {
}
public virtual void yesNoDialogClosed(string question, bool yesHit) {
}
}
....
public class FontDefault
{
public const string Default = "FONT.DEFAULT";
public const string DefaultBold = "FONT.DEFAULT.BOLD";
public const string Torchlight = "FONT.TORCHLIGHT";
public const string Consolas = "FONT.CONSOLAS";
public static void Load(string fontFileName, string fontRef, int size) {
// Create the font resources
//ResourceGroupManager.Singleton.AddResourceLocation("Media/fonts", "FileSystem");
//Load("Default.ttf", Font.Default, 26);
//Load("DefaultBold.ttf", Font.DefaultBold, 28);
//Load("Torchlight.ttf", Font.Torchlight, 36);
//Load("Consolas.ttf", Font.Consolas, 26);

ResourcePtr font = FontManager.Singleton.Create(fontRef, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
font.SetParameter("type", "truetype");
font.SetParameter("source", fontFileName);
font.SetParameter("size", size.ToString());
font.SetParameter("resolution", "96");
font.Load();
}
}

// =============================================================================
// | Abstract base class for all widgets.
// =============================================================================
public class Widget : IDisposable
{

public Widget() {
mTrayLoc = TrayLocation.TL_NONE;
mElement = null;
mListener = null;
}
/// <summary>
/// dispose this widget
/// </summary>
public virtual void Dispose() {
}

public void cleanup() {
if (mElement != null)
nukeOverlayElement(mElement);
mElement = null;
}

// -----------------------------------------------------------------------------
// | Static utility method to recursively delete an overlay element plus
// | all of its children from the system.
// -----------------------------------------------------------------------------
public static void nukeOverlayElement(OverlayElement element) {
Mogre.OverlayContainer container = element as Mogre.OverlayContainer;
if (container != null) {
List<Mogre.OverlayElement> toDelete = new List<Mogre.OverlayElement>();

Mogre.OverlayContainer.ChildIterator children = container.GetChildIterator();
while (children.MoveNext()) {
toDelete.Add(children.Current);
}

for (int i = 0; i < toDelete.Count; i++) {
nukeOverlayElement(toDelete);
}
}
if (element != null) {
Mogre.OverlayContainer parent = element.Parent;
if (parent != null)
parent.RemoveChild(element.Name);
Mogre.OverlayManager.Singleton.DestroyOverlayElement(element);
}
}

// -----------------------------------------------------------------------------
// | Static utility method to check if the cursor is over an overlay element.
// -----------------------------------------------------------------------------
public static bool isCursorOver(Mogre.OverlayElement element, Mogre.Vector2 cursorPos) {
return isCursorOver(element, cursorPos, 0f);
}
//C++ TO C# CONVERTER NOTE: Overloaded method(s) are created above to convert the following method having default parameters:
//ORIGINAL LINE: static bool isCursorOver(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos, Ogre::Real voidBorder = 0)
public static bool isCursorOver(Mogre.OverlayElement element, Mogre.Vector2 cursorPos, float voidBorder) {
Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;
float l = element._getDerivedLeft() * om.ViewportWidth;
float t = element._getDerivedTop() * om.ViewportHeight;
float r = l + element.Width;
float b = t + element.Height;

return (cursorPos.x >= l + voidBorder && cursorPos.x <= r - voidBorder && cursorPos.y >= t + voidBorder && cursorPos.y <= b - voidBorder);
}

// -----------------------------------------------------------------------------
// | Static utility method used to get the cursor's offset from the center
// | of an overlay element in pixels.
// -----------------------------------------------------------------------------
public static Mogre.Vector2 cursorOffset(Mogre.OverlayElement element, Mogre.Vector2 cursorPos) {
Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;
return new Mogre.Vector2(cursorPos.x - (element._getDerivedLeft() * om.ViewportWidth + element.Width / 2), cursorPos.y - (element._getDerivedTop() * om.ViewportHeight + element.Height / 2f));
}
//public static Vector2 cursorOffset(Mogre.OverlayContainer containerElement,Vector2 cursorPos)
//{
// Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;
// return new Mogre.Vector2(cursorPos.x - (containerElement._getDerivedLeft() * om.ViewportWidth + containerElement.Width / 2), cursorPos.y - (containerElement._getDerivedTop() * om.ViewportHeight + containerElement.Height / 2f));
//}
// -----------------------------------------------------------------------------
// | Static utility method used to get the width of a caption in a text area.
// -----------------------------------------------------------------------------
public static float getCaptionWidth(string caption, ref Mogre.TextAreaOverlayElement area) {
Mogre.FontPtr font = null;
if (Mogre.FontManager.Singleton.ResourceExists(area.FontName)) {
font = (Mogre.FontPtr)Mogre.FontManager.Singleton.GetByName(area.FontName);
if (!font.IsLoaded) {
font.Load();
}
}
else {
OGRE_EXCEPT("this font:", area.FontName, "is not exist");
}
//Font font = new Font(ft.Creator, ft.Name, ft.Handle, ft.Group, ft.IsManuallyLoaded);
string current = DISPLAY_STRING_TO_STRING(caption);
float lineWidth = 0f;

for (int i = 0; i < current.Length; i++) {
// be sure to provide a line width in the text area
if (current == ' ') {
if (area.SpaceWidth != 0)
lineWidth += area.SpaceWidth;
else
lineWidth += font.GetGlyphAspectRatio(' ') * area.CharHeight;
}
else if (current == '\n')
break;
// use glyph information to calculate line width
else
lineWidth += font.GetGlyphAspectRatio(current) * area.CharHeight;
}

return (uint)lineWidth;
}

protected static void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p + "_" + p_2 + "_" + p_3);
}

protected static string DISPLAY_STRING_TO_STRING(string caption) {
#if DISPLAY_STRING_TO_STRING_AlternateDefinition1
string current = (caption.asUTF8_c_str());
#elif DISPLAY_STRING_TO_STRING_AlternateDefinition2
string current = (caption.asUTF8());
#elif DISPLAY_STRING_TO_STRING_AlternateDefinition3
string current = (caption);
#endif
return caption;
}

// -----------------------------------------------------------------------------
// | Static utility method to cut off a string to fit in a text area.
// -----------------------------------------------------------------------------
public static void fitCaptionToArea(string caption, ref Mogre.TextAreaOverlayElement area, float maxWidth) {
Mogre.FontPtr font = null;
if (Mogre.FontManager.Singleton.ResourceExists(area.FontName)) {
font = (Mogre.FontPtr)Mogre.FontManager.Singleton.GetByName(area.FontName);
if (!font.IsLoaded) {
font.Load();
}
}
else {
OGRE_EXCEPT("this font:", area.FontName, "is not exist");
}
Mogre.FontPtr f = font;
string s = DISPLAY_STRING_TO_STRING(caption);
//int nl = s.find('\n');
//if (nl != string.npos)
// s = s.substr(0, nl);
int nl = s.IndexOf('\n');
if (nl != -1) s = s.Substring(0, nl);

float width = 0;

for (int i = 0; i < s.Length; i++) {
if (s == ' ' && area.SpaceWidth != 0)
width += area.SpaceWidth;
else
width += f.GetGlyphAspectRatio(s) * area.CharHeight;
if (width > maxWidth) {
s = s.Substring(0, i);
break;
}
}

area.Caption = (s);
}

public Mogre.OverlayElement getOverlayElement() {
return mElement;
}

public string getName() {
return mElement.Name;
}

public TrayLocation getTrayLocation() {
return mTrayLoc;
}

public void hide() {
mElement.Hide();
}

public void show() {
mElement.Show();
}

public bool isVisible() {
return mElement.IsVisible;
}

// callbacks

public virtual void _cursorPressed(Mogre.Vector2 cursorPos) {
}
public virtual void _cursorReleased(Mogre.Vector2 cursorPos) {
}
public virtual void _cursorMoved(Mogre.Vector2 cursorPos) {
}
public virtual void _focusLost() {
}

// internal methods used by SdkTrayManager. do not call directly.

public void _assignToTray(TrayLocation trayLoc) {
mTrayLoc = trayLoc;
}
public void _assignListener(SdkTrayListener listener) {
mListener = listener;
}


protected Mogre.OverlayElement mElement;
protected TrayLocation mTrayLoc;
protected SdkTrayListener mListener;



}

....


// =============================================================================
// | Main class to manage a cursor, backdrop, trays and widgets.
// =============================================================================
public class SdkTrayManager : SdkTrayListener
/*, Mogre.ResourceGroupListener*/, IDisposable
{
#region resource load event
void _HookResourceGroupLoadEvent() {
_UnHookResourceGroupLoadEvent();
if (_hasHookResLoad) return;
_hasHookResLoad = true;
ResourceGroupManager.Singleton.ResourceGroupScriptingStarted += _ResourceGroupScriptingStarted;
ResourceGroupManager.Singleton.ScriptParseStarted += _ScriptParseStarted;
ResourceGroupManager.Singleton.ScriptParseEnded += _ScriptParseEnded;
ResourceGroupManager.Singleton.ResourceGroupLoadStarted += _ResourceGroupLoadStarted;
ResourceGroupManager.Singleton.ResourceLoadStarted += _ResourceLoadStarted;
ResourceGroupManager.Singleton.WorldGeometryStageStarted += _WorldGeometryStageStarted;
ResourceGroupManager.Singleton.WorldGeometryStageEnded += _WorldGeometryStageEnded;

}
bool _hasHookResLoad = false;
void _UnHookResourceGroupLoadEvent() {
if (!_hasHookResLoad) return;
_hasHookResLoad = false;
ResourceGroupManager.Singleton.WorldGeometryStageEnded -= _WorldGeometryStageEnded;
ResourceGroupManager.Singleton.WorldGeometryStageStarted -= _WorldGeometryStageStarted;
ResourceGroupManager.Singleton.ResourceLoadStarted -= _ResourceLoadStarted;
ResourceGroupManager.Singleton.ResourceGroupLoadStarted -= _ResourceGroupLoadStarted;
ResourceGroupManager.Singleton.ScriptParseEnded -= _ScriptParseEnded;
ResourceGroupManager.Singleton.ScriptParseStarted -= _ScriptParseStarted;
ResourceGroupManager.Singleton.ResourceGroupScriptingStarted -= _ResourceGroupScriptingStarted;

}

void _ResourceGroupScriptingStarted(string groupName, uint scriptCount) {
//Debug.Assert(numGroupsInit > 0, "You stated you were not going to init any groups, but you did! Divide by zero would follow...");
//progressBarInc = progressBarMaxSize * initProportion / (float)scriptCount;
//progressBarInc /= numGroupsInit;
//loadingDescriptionElement.Caption = "Parsing scripts...";
//window.Update();
resourceGroupScriptingStarted(groupName,(int)scriptCount);
}

void _ScriptParseStarted(string scriptName, out bool skipThisScript) {
//loadingCommentElement.Caption = scriptName;
//window.Update();
skipThisScript = false;
scriptParseStarted(scriptName, skipThisScript);
}

void _ScriptParseEnded(string scriptName, bool skipped) {
//loadingBarElement.Width += progressBarInc;
//window.Update();
scriptParseEnded(scriptName, skipped);
}

void _ResourceGroupLoadStarted(string groupName, uint resourceCount) {
//Debug.Assert(numGroupsLoad > 0, "You stated you were not going to load any groups, but you did! Divide by zero would follow...");
//progressBarInc = progressBarMaxSize * (1 - initProportion) / (float)resourceCount;
//progressBarInc /= numGroupsLoad;
//loadingDescriptionElement.Caption = "Loading resources...";
//window.Update();
resourceGroupLoadStarted(groupName, (int)resourceCount);
}

void _ResourceLoadStarted(ResourcePtr resource) {
//loadingCommentElement.Caption = resource.Name;
//window.Update();
resourceLoadStarted(resource);
}

void _WorldGeometryStageStarted(string description) {
//loadingCommentElement.Caption = description;
//window.Update();
worldGeometryStageStarted(description);
}

void _WorldGeometryStageEnded() {
//loadingBarElement.Width += progressBarInc;
//window.Update();
worldGeometryStageEnded();
}


public void resourceGroupScriptingStarted(string groupName, int scriptCount) {
mLoadInc = mGroupInitProportion / scriptCount;
mLoadBar.setCaption("Parsing...");
windowUpdate();
}

public void scriptParseStarted(string scriptName, bool skipThisScript) {
mLoadBar.setComment(scriptName);
windowUpdate();
}

public void scriptParseEnded(string scriptName, bool skipped) {
mLoadBar.setProgress(mLoadBar.getProgress() + mLoadInc);
windowUpdate();
}

public void resourceGroupScriptingEnded(string groupName) {
}

public void resourceGroupLoadStarted(string groupName, int resourceCount) {
mLoadInc = mGroupLoadProportion / resourceCount;
mLoadBar.setCaption("Loading...");
windowUpdate();
}

public void resourceLoadStarted(Mogre.ResourcePtr resource) {
mLoadBar.setComment(resource.Name);
windowUpdate();
}

public void resourceLoadEnded() {
mLoadBar.setProgress(mLoadBar.getProgress() + mLoadInc);
windowUpdate();
}

public void worldGeometryStageStarted(string description) {
mLoadBar.setComment(description);
windowUpdate();
}

public void worldGeometryStageEnded() {
mLoadBar.setProgress(mLoadBar.getProgress() + mLoadInc);
windowUpdate();
}

public void resourceGroupLoadEnded(string groupName) {
}
public void windowUpdate() {

//#if OGRE_PLATFORM != OGRE_PLATFORM_APPLE_IOS && OGRE_PLATFORM != OGRE_PLATFORM_NACL
mWindow.Update();
//#endif
}
#endregion
#region mouse event
internal void _UnHookMouseEvent() {
mInputContext.MousePressed -= (mInputContext_MousePressed);
mInputContext.MouseReleased -= (mInputContext_MouseReleased);
mInputContext.MouseMoved -= (mInputContext_MouseMoved);
}
internal void _HookMouseEvent() {
_UnHookMouseEvent();
mInputContext.MousePressed += (mInputContext_MousePressed);
mInputContext.MouseReleased += (mInputContext_MouseReleased);
mInputContext.MouseMoved += (mInputContext_MouseMoved);
}

bool mInputContext_MouseMoved(MOIS.MouseEvent arg) {
this.injectMouseMove(arg);
return true;
}

bool mInputContext_MouseReleased(MOIS.MouseEvent arg, MOIS.MouseButtonID id) {
this.injectMouseUp(arg, id);
return true;
}

bool mInputContext_MousePressed(MOIS.MouseEvent arg, MOIS.MouseButtonID id) {
this.injectMouseDown(arg, id);
return true;
}

#endregion
// -----------------------------------------------------------------------------
// | Creates backdrop, cursor, and trays.
// -----------------------------------------------------------------------------
public SdkTrayManager(string name, Mogre.RenderWindow window, InputContext inputContext)
: this(name, window, inputContext, null) {
}
//ORIGINAL LINE: SdkTrayManager(const Ogre::String& name, Ogre::RenderWindow* window, InputContext inputContext, SdkTrayListener* listener = 0) : mName(name), mWindow(window), mInputContext(inputContext), mWidgetDeathRow(), mListener(listener), mWidgetPadding(8), mWidgetSpacing(2), mTrayPadding(0), mTrayDrag(false), mExpandedMenu(0), mDialog(0), mOk(0), mYes(0), mNo(0), mCursorWasVisible(false), mFpsLabel(0), mStatsPanel(0), mLogo(0), mLoadBar(0), mGroupInitProportion(0.0f), mGroupLoadProportion(0.0f), mLoadInc(0.0f)
public SdkTrayManager(string name, Mogre.RenderWindow window, InputContext inputContext, SdkTrayListener listener)
: base() {
for (int i = 0; i < mWidgets.Length; i++) {
mWidgets = new List<Widget>();
}
mName = name;
mWindow = window;
mInputContext = inputContext;
mListener = listener;
mWidgetPadding = 8f;
mWidgetSpacing = 2f;
mTrayPadding = 0f;
mTrayDrag = false;
mExpandedMenu = null;
mDialog = null;
mOk = null;
mYes = null;
mNo = null;
mCursorWasVisible = false;
mFpsLabel = null;
mStatsPanel = null;
mLogo = null;
mLoadBar = null;
mGroupInitProportion = 0.0f;
mGroupLoadProportion = 0.0f;
mLoadInc = 0.0f;
mTimer = Mogre.Root.Singleton.Timer;
mLastStatUpdateTime = 0;

Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;

string nameBase = mName + "/";
//std.replace(nameBase.begin(), nameBase.end(), ' ', '_');
nameBase = nameBase.Replace(' ', '_');
// create overlay layers for everything
mBackdropLayer = om.Create(nameBase + "BackdropLayer");
mTraysLayer = om.Create(nameBase + "WidgetsLayer");
mPriorityLayer = om.Create(nameBase + "PriorityLayer");
mCursorLayer = om.Create(nameBase + "CursorLayer");
mBackdropLayer.ZOrder = (100);
mTraysLayer.ZOrder = (200);
mPriorityLayer.ZOrder = (300);
mCursorLayer.ZOrder = (400);

// make backdrop and cursor overlay containers
mCursor = (Mogre.OverlayContainer)om.CreateOverlayElementFromTemplate("SdkTrays/Cursor", "Panel", nameBase + "Cursor");
mCursorLayer.Add2D(mCursor);
mBackdrop = (Mogre.OverlayContainer)om.CreateOverlayElement("Panel", nameBase + "Backdrop");
mBackdropLayer.Add2D(mBackdrop);
mDialogShade = (Mogre.OverlayContainer)om.CreateOverlayElement("Panel", nameBase + "DialogShade");
mDialogShade.MaterialName = ("SdkTrays/Shade");
mDialogShade.Hide();
mPriorityLayer.Add2D(mDialogShade);

string[] trayNames = { "TopLeft", "Top", "TopRight", "Left", "Center", "Right", "BottomLeft", "Bottom", "BottomRight" };

for (uint i = 0; i < 9; i++) // make the real trays
{
mTrays = (Mogre.OverlayContainer)om.CreateOverlayElementFromTemplate("SdkTrays/Tray", "BorderPanel", nameBase + trayNames + "Tray");
mTraysLayer.Add2D(mTrays);

mTrayWidgetAlign = GuiHorizontalAlignment.GHA_CENTER;

// align trays based on location
if (i == (int)TrayLocation.TL_TOP || i == (int)TrayLocation.TL_CENTER || i == (int)TrayLocation.TL_BOTTOM)
mTrays.HorizontalAlignment = (GuiHorizontalAlignment.GHA_CENTER);
if (i == (int)TrayLocation.TL_LEFT || i == (int)TrayLocation.TL_CENTER || i == (int)TrayLocation.TL_RIGHT)
mTrays.VerticalAlignment = (GuiVerticalAlignment.GVA_CENTER);
if (i == (int)TrayLocation.TL_TOPRIGHT || i == (int)TrayLocation.TL_RIGHT || i == (int)TrayLocation.TL_BOTTOMRIGHT)
mTrays.HorizontalAlignment = (GuiHorizontalAlignment.GHA_RIGHT);
if (i == (int)TrayLocation.TL_BOTTOMLEFT || i == (int)TrayLocation.TL_BOTTOM || i == (int)TrayLocation.TL_BOTTOMRIGHT)
mTrays.VerticalAlignment = (GuiVerticalAlignment.GVA_BOTTOM);
}

// create the null tray for free-floating widgets
mTrays[9] = (Mogre.OverlayContainer)om.CreateOverlayElement("Panel", nameBase + "NullTray");
mTrayWidgetAlign[9] = GuiHorizontalAlignment.GHA_LEFT;
mTraysLayer.Add2D(mTrays[9]);
adjustTrays();

showTrays();
showCursor();
}

// -----------------------------------------------------------------------------
// | Destroys background, cursor, widgets, and trays.
// -----------------------------------------------------------------------------
public override void Dispose() {
Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;

destroyAllWidgets();//clean up ok

for (int i = 0; i < mWidgetDeathRow.Count; i++) // delete widgets queued for destruction
{
//delete mWidgetDeathRow;
mWidgetDeathRow.Dispose();//?is there need?
mWidgetDeathRow = null;
}
mWidgetDeathRow.Clear();

om.Destroy(mBackdropLayer);
om.Destroy(mTraysLayer);
om.Destroy(mPriorityLayer);
om.Destroy(mCursorLayer);

closeDialog();
hideLoadingBar();

Widget.nukeOverlayElement(mBackdrop);
Widget.nukeOverlayElement(mCursor);
Widget.nukeOverlayElement(mDialogShade);

for (int i = 0; i < 10; i++) {
Widget.nukeOverlayElement(mTrays);
}
base.Dispose();
}

// -----------------------------------------------------------------------------
// | Converts a 2D screen coordinate (in pixels) to a 3D ray into the scene.
// -----------------------------------------------------------------------------
public static Mogre.Ray screenToScene(Mogre.Camera cam, Mogre.Vector2 pt) {
return cam.GetCameraToViewportRay(pt.x, pt.y);
....
}


the full code with c#,you can see:https://code.google.com/p/mogre-procedural/source/browse/trunk/Demo.MogreProcedural/SdkTrayUI/SdkTrayGUI.cs

andyhebear1

11-04-2014 04:28:17

use this SdkTrays UI like after:

this.mTrayMgr = new SdkTrayManager("trayMgr", this.mWindow, this.mInput.Mouse, this);
this.mTrayMgr.showFrameStats(TrayLocation.TL_BOTTOMLEFT);
this.mTrayMgr.hideCursor();
//this.mTrayMgr.hideFrameStats();
//this.mTrayMgr.showFrameStats(TrayLocation.TL_BOTTOMLEFT);
//this.mTrayMgr.showLogo(TrayLocation.TL_BOTTOMLEFT);
//this.mTrayMgr.showOkDialog("context...", "this is a test");
//this.mTrayMgr.showLoadingBar();


this.mPanel = this.mTrayMgr.createParamsPanel(TrayLocation.TL_NONE, "DetailsPanel", 200, new string[] { "cam.pX", "cam.pY", "cam.pZ", "cam.oW", "cam.oX", "cam.oY", "cam.oZ", "Filtering", "Poly Mode" });
this.mPanel.setParamValue(7, "Bilinear");
this.mPanel.setParamValue(8, "Solid");
this.mPanel.hide();


on ogre frame render,you need add:

private bool OnFrameRendering(FrameEvent evt) {
if (this.mWindow.IsClosed || this.mIsShutDownRequested) { return false; }
try {
this.ProcessInput();
this.Update(evt);
this.mTrayMgr.frameRenderingQueued(evt);
....
}


the end you destroy this :

if (this.mTrayMgr != null) {
this.mTrayMgr.destroyAllWidgets();
//this.mTrayMgr.closeDialog();
//this.mTrayMgr.clearAllTrays();
this.mTrayMgr = null;
}