📄 guitextlistctrl.cc
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "console/consoleTypes.h"
#include "console/console.h"
#include "dgl/dgl.h"
#include "gui/controls/guiTextListCtrl.h"
#include "gui/containers/guiScrollCtrl.h"
#include "gui/core/guiDefaultControlRender.h"
IMPLEMENT_CONOBJECT(GuiTextListCtrl);
static int sortColumn;
static bool sIncreasing;
static const char *getColumn(const char *text)
{
int ct = sortColumn;
while(ct--)
{
text = dStrchr(text, '\t');
if(!text)
return "";
text++;
}
return text;
}
static S32 QSORT_CALLBACK textCompare( const void* a, const void* b )
{
GuiTextListCtrl::Entry *ea = (GuiTextListCtrl::Entry *) (a);
GuiTextListCtrl::Entry *eb = (GuiTextListCtrl::Entry *) (b);
S32 result = dStricmp( getColumn( ea->text ), getColumn( eb->text ) );
return ( sIncreasing ? result : -result );
}
static S32 QSORT_CALLBACK numCompare(const void *a,const void *b)
{
GuiTextListCtrl::Entry *ea = (GuiTextListCtrl::Entry *) (a);
GuiTextListCtrl::Entry *eb = (GuiTextListCtrl::Entry *) (b);
const char* aCol = getColumn( ea->text );
const char* bCol = getColumn( eb->text );
F32 result = dAtof(aCol) - dAtof(bCol);
S32 res = result < 0 ? -1 : (result > 0 ? 1 : 0);
return ( sIncreasing ? res : -res );
}
GuiTextListCtrl::GuiTextListCtrl()
{
VECTOR_SET_ASSOCIATION(mList);
VECTOR_SET_ASSOCIATION(mColumnOffsets);
mActive = true;
mEnumerate = false;
mResizeCell = true;
mSize.set(1, 0);
mColumnOffsets.push_back(0);
mFitParentWidth = true;
mClipColumnText = false;
}
void GuiTextListCtrl::initPersistFields()
{
Parent::initPersistFields();
addField("enumerate", TypeBool, Offset(mEnumerate, GuiTextListCtrl));
addField("resizeCell", TypeBool, Offset(mResizeCell, GuiTextListCtrl));
addField("columns", TypeS32Vector, Offset(mColumnOffsets, GuiTextListCtrl));
addField("fitParentWidth", TypeBool, Offset(mFitParentWidth, GuiTextListCtrl));
addField("clipColumnText", TypeBool, Offset(mClipColumnText, GuiTextListCtrl));
}
ConsoleMethod(GuiTextListCtrl, getSelectedId, S32, 2, 2, "Get the ID of the currently selected item.")
{
return object->getSelectedId();
}
ConsoleMethod( GuiTextListCtrl, setSelectedById, void, 3, 3, "(int id)"
"Finds the specified entry by id, then marks its row as selected.")
{
S32 index = object->findEntryById(dAtoi(argv[2]));
if(index < 0)
return ;
object->setSelectedCell(Point2I(0, index));
}
ConsoleMethod( GuiTextListCtrl, setSelectedRow, void, 3, 3, "(int rowNum)"
"Selects the specified row.")
{
object->setSelectedCell( Point2I( 0, dAtoi( argv[2] ) ) );
}
ConsoleMethod( GuiTextListCtrl, clearSelection, void, 2, 2, "Set the selection to nothing.")
{
object->setSelectedCell(Point2I(-1, -1));
}
ConsoleMethod(GuiTextListCtrl, addRow, S32, 4, 5, "(int id, string text, int index=0)"
"Returns row number of the new item.")
{
S32 ret = object->mList.size();
if(argc < 5)
object->addEntry(dAtoi(argv[2]), argv[3]);
else
object->insertEntry(dAtoi(argv[2]), argv[3], dAtoi(argv[4]));
return ret;
}
ConsoleMethod( GuiTextListCtrl, setRowById, void, 4, 4, "(int id, string text)")
{
object->setEntry(dAtoi(argv[2]), argv[3]);
}
ConsoleMethod( GuiTextListCtrl, sort, void, 3, 4, "(int columnID, bool increasing=false)"
"Performs a standard (alphabetical) sort on the values in the specified column.")
{
if ( argc == 3 )
object->sort(dAtoi(argv[2]));
else
object->sort( dAtoi( argv[2] ), dAtob( argv[3] ) );
}
ConsoleMethod(GuiTextListCtrl, sortNumerical, void, 3, 4, "(int columnID, bool increasing=false)"
"Perform a numerical sort on the values in the specified column.")
{
if ( argc == 3 )
object->sortNumerical( dAtoi( argv[2] ) );
else
object->sortNumerical( dAtoi( argv[2] ), dAtob( argv[3] ) );
}
ConsoleMethod( GuiTextListCtrl, clear, void, 2, 2, "Clear the list.")
{
object->clear();
}
ConsoleMethod( GuiTextListCtrl, rowCount, S32, 2, 2, "Get the number of rows.")
{
return object->getNumEntries();
}
ConsoleMethod( GuiTextListCtrl, getRowId, S32, 3, 3, "(int index)"
"Get the row ID for an index.")
{
U32 index = dAtoi(argv[2]);
if(index >= object->getNumEntries())
return -1;
return object->mList[index].id;
}
ConsoleMethod( GuiTextListCtrl, getRowTextById, const char*, 3, 3, "(int id)"
"Get the text of a row with the specified id.")
{
S32 index = object->findEntryById(dAtoi(argv[2]));
if(index < 0)
return "";
return object->mList[index].text;
}
ConsoleMethod( GuiTextListCtrl, getRowNumById, S32, 3, 3, "(int id)"
"Get the row number for a specified id.")
{
S32 index = object->findEntryById(dAtoi(argv[2]));
if(index < 0)
return -1;
return index;
}
ConsoleMethod( GuiTextListCtrl, getRowText, const char*, 3, 3, "(int index)"
"Get the text of the row with the specified index.")
{
U32 index = dAtoi(argv[2]);
if(index < 0 || index >= object->mList.size())
return "";
return object->mList[index].text;
}
ConsoleMethod( GuiTextListCtrl, removeRowById, void, 3, 3,"(int id)"
"Remove row with the specified id.")
{
object->removeEntry(dAtoi(argv[2]));
}
ConsoleMethod( GuiTextListCtrl, removeRow, void, 3, 3, "(int index)"
"Remove a row from the table, based on its index.")
{
U32 index = dAtoi(argv[2]);
object->removeEntryByIndex(index);
}
ConsoleMethod( GuiTextListCtrl, scrollVisible, void, 3, 3, "(int rowNum)"
"Scroll so the specified row is visible.")
{
object->scrollCellVisible(Point2I(0, dAtoi(argv[2])));
}
ConsoleMethod( GuiTextListCtrl, findTextIndex, S32, 3, 3, "(string needle)"
"Find needle in the list, and return the row number it was found in.")
{
return( object->findEntryByText( argv[2] ) );
}
ConsoleMethod( GuiTextListCtrl, setRowActive, void, 4, 4, "(int rowNum, bool active)"
"Mark a specified row as active/not.")
{
object->setEntryActive( U32( dAtoi( argv[2] ) ), dAtob( argv[3] ) );
}
ConsoleMethod( GuiTextListCtrl, isRowActive, bool, 3, 3, "(int rowNum)"
"Is the specified row currently active?")
{
return( object->isEntryActive( U32( dAtoi( argv[2] ) ) ) );
}
bool GuiTextListCtrl::onWake()
{
if(!Parent::onWake())
return false;
setSize(mSize);
return true;
}
U32 GuiTextListCtrl::getSelectedId()
{
if (mSelectedCell.y == -1)
return InvalidId;
return mList[mSelectedCell.y].id;
}
void GuiTextListCtrl::onCellSelected(Point2I cell)
{
Con::executef(this, 3, "onSelect", Con::getIntArg(mList[cell.y].id), mList[cell.y].text);
if (mConsoleCommand[0])
Con::evaluate(mConsoleCommand, false);
}
void GuiTextListCtrl::onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver)
{
if ( mList[cell.y].active )
{
#ifdef TGE_RPG_UI ///TGE_RPG_UI
if (selected)
{
renderFilledBorder(RectI(offset.x, offset.y, mCellSize.x, mCellSize.y), mProfile->mBorderColor,mProfile->mFillColorHL);
dglSetBitmapModulation(mProfile->mFontColorSEL);
}
else if((mProfile->mMouseOverSelected && mouseOver))
{
renderFilledBorder(RectI(offset.x, offset.y, mCellSize.x, mCellSize.y), mProfile->mBorderColorHL,mProfile->mFillColorHL);
dglSetBitmapModulation(mProfile->mFontColorHL);
}
else
dglSetBitmapModulation(mouseOver ? mProfile->mFontColorHL : mProfile->mFontColor);
#else
if (selected || (mProfile->mMouseOverSelected && mouseOver))
{
dglDrawRectFill(RectI(offset.x, offset.y, mCellSize.x, mCellSize.y), mProfile->mFillColorHL);
dglSetBitmapModulation(mProfile->mFontColorHL);
}
else
dglSetBitmapModulation(mouseOver ? mProfile->mFontColorHL : mProfile->mFontColor);
#endif
}
else
dglSetBitmapModulation( mProfile->mFontColorNA );
#ifdef TGE_RPG_UI ///TGE_RPG_UI 文字内容V居中
S32 yOffset = (mCellSize.y - mFont->getHeight()) / 2;
#endif
const char *text = mList[cell.y].text;
for(U32 index = 0; index < mColumnOffsets.size(); index++)
{
const char *nextCol = dStrchr(text, '\t');
if(mColumnOffsets[index] >= 0)
{
dsize_t slen;
if(nextCol)
slen = nextCol - text;
else
slen = dStrlen(text);
Point2I pos(offset.x + 4 + mColumnOffsets[index], offset.y);
RectI saveClipRect;
bool clipped = false;
if(mClipColumnText && (index != (mColumnOffsets.size() - 1)))
{
saveClipRect = dglGetClipRect();
RectI clipRect(pos, Point2I(mColumnOffsets[index+1] - mColumnOffsets[index] - 4, mCellSize.y));
if(clipRect.intersect(saveClipRect))
{
clipped = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -