⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guitabbookctrl.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#include "console/console.h"
#include "console/consoleTypes.h"
#include "dgl/dgl.h"
#include "console/simBase.h"
#include "gui/core/guiCanvas.h"
#include "gui/containers/guiTabBookCtrl.h"
#include "platform/event.h"
#include "core/fileStream.h"
#include "gui/containers/guiScrollCtrl.h"
#include "gui/editor/guiEditCtrl.h"
#include "gui/controls/guiPopUpCtrl.h"


// So we can set tab alignment via gui editor
static EnumTable::Enums tabAlignEnums[] =
{
   { GuiTabBookCtrl::AlignTop,   "Top"    },
//   { GuiTabBookCtrl::AlignLeft,  "Left"   },
   { GuiTabBookCtrl::AlignBottom,"Bottom" },
//   { GuiTabBookCtrl::AlignRight,	"Right"  }
};
static EnumTable gTabAlignEnums(/*4*/2,&tabAlignEnums[0]);


IMPLEMENT_CONOBJECT(GuiTabBookCtrl);

GuiTabBookCtrl::GuiTabBookCtrl()
{
   VECTOR_SET_ASSOCIATION(mPages);
   mTabHeight = 24;
   mLastTabHeight = mTabHeight;
   mTabWidth = 64;
   mLastTabWidth = mTabWidth;
   mTabPosition = GuiTabBookCtrl::AlignTop;
   mLastTabPosition = mTabPosition;
   mActivePage = NULL;
   mHoverTab = NULL;
   mHasTexture = false;
   mBitmapBounds = NULL;
   mBounds.extent.set( 400, 300 );
   mPageRect = RectI(0,0,0,0);
   mTabRect = RectI(0,0,0,0);

	m_nDefaultPage = 0;
}

void GuiTabBookCtrl::initPersistFields()
{
   Parent::initPersistFields();
   addField("TabPosition",		TypeEnum,		Offset(mTabPosition,GuiTabBookCtrl), 1, &gTabAlignEnums );
   addField("TabHeight",		TypeS32,		Offset(mTabHeight,GuiTabBookCtrl) );
   addField("TabWidth",		TypeS32,		Offset(mTabWidth,GuiTabBookCtrl));

#ifdef TGE_RPG
   addField("defaultPage",		TypeS32,		Offset(m_nDefaultPage,GuiTabBookCtrl));
#endif

}


// Empty for now, will implement for handling design time context menu for manipulating pages
ConsoleMethod( GuiTabBookCtrl, addPage, void, 2, 2, "(no arguments expected)")
{
   object->addNewPage();
}

//ConsoleMethod( GuiTabBookCtrl, removePage, void, 2, 2, "()")
//{
//}


bool GuiTabBookCtrl::onAdd()
{
   Parent::onAdd();

   mTabRect = getTabBaseRect( Point2I(0,0) );
   mPageRect = getPageRect( mTabRect, Point2I(0,0) );

   return true;
}


void GuiTabBookCtrl::onRemove()
{
   Parent::onRemove();
}

void GuiTabBookCtrl::onChildRemoved( GuiControl* child )
{
   Vector<GuiTabPageCtrl*>::iterator i = mPages.begin();
   for( ; i != mPages.end(); i++)
   {
      GuiTabPageCtrl* tab = (*i);
      if( tab == child )
      {
         if( tab == mActivePage )
            mActivePage = NULL;
         mPages.erase( i );
         break;
      }
   }

   if( mPages.empty() )
      mActivePage = NULL;
   else if (mActivePage == NULL )
      mActivePage = (*mPages.begin());

}

void GuiTabBookCtrl::onChildAdded( GuiControl *child )
{
   GuiTabPageCtrl *page = dynamic_cast<GuiTabPageCtrl*>(child);
   if( !page )
   {
      Con::warnf("GuiTabBookCtrl::onChildAdded - attempting to add NON GuiTabPageCtrl as child page");
      SimObject *simObj = reinterpret_cast<SimObject*>(child);
      removeObject( simObj );
      if( mActivePage )
      {
         mActivePage->addObject( simObj );
      }
      else
      {
         Con::warnf("GuiTabBookCtrl::onChildAdded - unable to find active page to reassign ownership of new child control to, placing on parent");
         GuiControl *rent = getParent();
         if( rent )
            rent->addObject( simObj );
      }
      return;
   }
   child->resize( mPageRect.point, mPageRect.extent );
   mPages.push_back( page );

#ifndef TGE_RPG
//	if(child->isVisible())
//		selectPage(page);
//#else
   selectPage( page );
#endif
}


bool GuiTabBookCtrl::onWake()
{
   if (! Parent::onWake())
      return false;

   mHasTexture = mProfile->constructBitmapArray();
   if( mHasTexture )
      mBitmapBounds = mProfile->mBitmapArrayRects.address();

#ifdef TGE_RPG
	//if(mActivePage == NULL)
	if(m_nDefaultPage == -1)
		m_nDefaultPage = mPages.size()-1;
	if(m_nDefaultPage < 0 || m_nDefaultPage >= mPages.size())
		m_nDefaultPage = 0;

	selectPage(mPages[m_nDefaultPage]);
#endif

   return true;
}

void GuiTabBookCtrl::onSleep()
{
   Parent::onSleep();
}


void GuiTabBookCtrl::addNewPage()
{
   char textbuf[1024];

   GuiTabPageCtrl * page = new GuiTabPageCtrl();

   page->setField("profile", "GuiTabPageProfile");

   dSprintf(textbuf, sizeof(textbuf), "TabBookPage%d_%d", getId(), page->getId());
   page->registerObject(textbuf);

   this->addObject( page );
}

void GuiTabBookCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
{
   Parent::resize( newPosition, newExtent );

   mTabRect = getTabBaseRect( Point2I(0,0) );
   mPageRect = getPageRect( mTabRect, Point2I(0,0) );

   RectI pageRect = getPageRectChild();

   // Resize Children
   SimSet::iterator i;
   for(i = begin(); i != end(); i++)
   {
      GuiControl *ctrl = static_cast<GuiControl *>(*i);
      ctrl->mBounds = pageRect;
   }
}

void GuiTabBookCtrl::childResized(GuiControl *child)
{
   Parent::childResized( child );
   child->mBounds = getPageRectChild();
}

GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( const GuiEvent &event )
{
   return findHitTab( event.mousePoint );
}

GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( Point2I hitPoint )
{
   S32 hitTab = 0; // Which tab did we hit?

   hitPoint = globalToLocalCoord( hitPoint );

   switch( mTabPosition )
   {
   case AlignTop:
   case AlignBottom:
      hitTab = (S32)mFloor( (F32)hitPoint.x / (F32)mTabWidth );
      break;
   case AlignLeft:
   case AlignRight:
      hitTab = (S32)mFloor( (F32)hitPoint.y / (F32)mTabWidth );
      break;
   };

   if( mPages.size() > hitTab && hitTab >= 0 )
      return mPages[hitTab];

   return NULL;
}

RectI GuiTabBookCtrl::getHitRect( Point2I offset )
{
   // We can get our rect without a specified offset, but if we don't specify one
   // when rendering with GUI editor open, our offset is wrong because the editor
   // offsets the canvas to accomodate creation of it's own controls

   RectI Result = RectI( localToGlobalCoord( Point2I(0,0) ), mBounds.extent );

   if( ! offset.isZero() )
      Result.point -= offset;
   switch( mTabPosition )
   {
   case AlignTop:
      Result.extent.y = mTabHeight;
      break;
   case AlignBottom:
      Result.point.y = (Result.point.y + Result.extent.y) - mTabHeight;
      Result.extent.y = mTabHeight;
      break;
   case AlignLeft:
      Result.extent.x = mTabHeight;
      break;
   case AlignRight:
      Result.point.x = (Result.point.x + Result.extent.x) - mTabHeight;
      Result.extent.x = mTabHeight;
      break;
   }
   return Result;

}

RectI GuiTabBookCtrl::getTabBaseRect( Point2I offset = Point2I(0,0) )
{
   // We can get our rect without a specified offset, but if we don't specify one
   // when rendering with GUI editor open, our offset is wrong because the editor
   // offsets the canvas to accomodate creation of it's own controls

   RectI Result = RectI( offset.isZero() ? mBounds.point : offset, mBounds.extent );
   switch( mTabPosition )
   {
   case AlignTop:
      Result.extent.y = mTabHeight;
      break;
   case AlignBottom:
      Result.point.y = (Result.point.y + Result.extent.y) - mTabHeight;
      Result.extent.y = mTabHeight;
      break;
   case AlignLeft:
      Result.extent.x = mTabHeight;
      break;
   case AlignRight:
      Result.point.x = (Result.point.x + Result.extent.x) - mTabHeight;
      Result.extent.x = mTabHeight;
      break;
   }
   return Result;

}

RectI GuiTabBookCtrl::getPageRect( const RectI &tabBaseRect, Point2I offset )
{
   RectI Result;
   switch( mTabPosition )
   {
   case AlignTop:
      Result.point.x = tabBaseRect.point.x;
      Result.extent.x = tabBaseRect.extent.x;
      Result.point.y = tabBaseRect.point.y + tabBaseRect.extent.y;
      Result.extent.y = mBounds.extent.y - mTabHeight;
      break;
   case AlignBottom:
      Result.point.x = tabBaseRect.point.x;
      Result.extent.x = tabBaseRect.extent.x;
      Result.point.y = offset.y;
      Result.extent.y = mBounds.extent.y - mTabHeight;
      break;
   case AlignLeft:
      Result.point.x = offset.x + mTabHeight;
      Result.extent.x = mBounds.extent.x - mTabHeight;
      Result.point.y = offset.y;
      Result.extent.y = mBounds.extent.y;
      break;
   case AlignRight:
      Result.point.x = offset.x;
      Result.extent.x = mBounds.extent.x - mTabHeight;
      Result.point.y = offset.y;
      Result.extent.y = mBounds.extent.y;
      break;
   }

   return Result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -