📄 guieditctrl.cc
字号:
//-----------------------------------------------------------------------------
// 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/editor/guiEditCtrl.h"
#include "platform/event.h"
#include "core/fileStream.h"
#include "core/memStream.h"
#include "gui/containers/guiScrollCtrl.h"
IMPLEMENT_CONOBJECT(GuiEditCtrl);
GuiEditCtrl::GuiEditCtrl()
{
VECTOR_SET_ASSOCIATION(mSelectedControls);
mActive = true;
mCurrentAddSet = NULL;
mGridSnap.set(0, 0);
mDragBeginPoint.set( -1,-1 );
mDragBeginPoints.clear();
mDefaultCursor = NULL;
mLeftRightCursor = NULL;
mUpDownCursor = NULL;
mNWSECursor = NULL;
mNESWCursor = NULL;
mMoveCursor = NULL;
#ifdef TGE_RPG
m_maskColor.set(200,200,200,128);
m_outlineColor.set(255,200,200,200);
m_bDropCopyed = false;
#endif
}
ConsoleMethod( GuiEditCtrl, setRoot, void, 3, 3, "(GuiControl root)")
{
GuiControl *ctrl;
if(!Sim::findObject(argv[2], ctrl))
return;
object->setRoot(ctrl);
}
ConsoleMethod( GuiEditCtrl, addNewCtrl, void, 3, 3, "(GuiControl ctrl)")
{
GuiControl *ctrl;
if(!Sim::findObject(argv[2], ctrl))
return;
object->addNewControl(ctrl);
}
ConsoleMethod( GuiEditCtrl, addSelection, void, 3, 3, "selects a control.")
{
S32 id = dAtoi(argv[2]);
object->addSelection(id);
}
ConsoleMethod( GuiEditCtrl, removeSelection, void, 3, 3, "deselects a control.")
{
S32 id = dAtoi(argv[2]);
object->removeSelection(id);
}
ConsoleMethod( GuiEditCtrl, clearSelection, void, 2, 2, "Clear selected controls list.")
{
object->clearSelection();
}
ConsoleMethod( GuiEditCtrl, select, void, 3, 3, "(GuiControl ctrl)")
{
GuiControl *ctrl;
if(!Sim::findObject(argv[2], ctrl))
return;
object->setSelection(ctrl, false);
}
ConsoleMethod( GuiEditCtrl, setCurrentAddSet, void, 3, 3, "(GuiControl ctrl)")
{
GuiControl *addSet;
if (!Sim::findObject(argv[2], addSet))
{
Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
return;
}
object->setCurrentAddSet(addSet);
}
ConsoleMethod( GuiEditCtrl, toggle, void, 2, 2, "Toggle activation.")
{
object->setEditMode(! object->mActive);
}
ConsoleMethod( GuiEditCtrl, justify, void, 3, 3, "(int mode)" )
{
object->justifySelection((GuiEditCtrl::Justification)dAtoi(argv[2]));
}
ConsoleMethod( GuiEditCtrl, bringToFront, void, 2, 2, "")
{
object->bringToFront();
}
ConsoleMethod( GuiEditCtrl, pushToBack, void, 2, 2, "")
{
object->pushToBack();
}
ConsoleMethod( GuiEditCtrl, deleteSelection, void, 2, 2, "Delete the selected text.")
{
object->deleteSelection();
}
ConsoleMethod( GuiEditCtrl, moveSelection, void, 4, 4, "(int deltax, int deltay)")
{
object->moveSelection(Point2I(dAtoi(argv[2]), dAtoi(argv[3])));
}
ConsoleMethod( GuiEditCtrl, saveSelection, void, 3, 3, "(string fileName)")
{
object->saveSelection(argv[2]);
//object->copySelection();
}
ConsoleMethod( GuiEditCtrl, loadSelection, void, 3, 3, "(string fileName)")
{
object->loadSelection(argv[2]);
//object->parseSelection();
}
ConsoleMethod( GuiEditCtrl, selectAll, void, 2, 2, "()")
{
object->selectAll();
}
#ifdef TGE_RPG
ConsoleMethod( GuiEditCtrl, copySelection, void, 2, 2, "()")
{
object->copySelection();
}
ConsoleMethod( GuiEditCtrl, parseSelection, void, 2, 2, "()")
{
object->parseSelection();
}
ConsoleMethod( GuiEditCtrl, toggleToolBarVisible, void, 2, 2, "()")
{
object->toggleToolBarVisible();
}
#endif
ConsoleMethod( GuiEditCtrl, getSelected, const char *, 2, 2, "() - Gets the GUI control(s) the editor is currently selecting" )
{
char *returnText = Con::getReturnBuffer(128);
dStrcpy( returnText, "" );
const Vector<GuiControl *> *selected = object->getSelected();
for( Vector<GuiControl *>::const_iterator i = selected->begin(); i != selected->end(); i++ )
{
char temp[8];
dSprintf( temp, 8, "%d ", (*i)->getId() );
dStrcat( returnText, temp );
}
return returnText;
}
bool GuiEditCtrl::onWake()
{
if (! Parent::onWake())
return false;
// Set GUI Controls to DesignTime mode
GuiControl::smDesignTime = true;
GuiControl::smEditorHandle = this;
setEditMode(true);
return true;
}
void GuiEditCtrl::onSleep()
{
// Set GUI Controls to run time mode
GuiControl::smDesignTime = false;
GuiControl::smEditorHandle = NULL;
Parent::onSleep();
}
void GuiEditCtrl::setRoot(GuiControl *root)
{
mContentControl = root;
mCurrentAddSet = mContentControl;
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
}
enum GuiEditConstants {
GUI_BLACK = 0,
GUI_WHITE = 255,
NUT_SIZE = 3
};
// Sizing Cursors
bool GuiEditCtrl::initCursors()
{
if (mMoveCursor == NULL || mUpDownCursor == NULL || mLeftRightCursor == NULL || mDefaultCursor == NULL || mNWSECursor == NULL || mNESWCursor == NULL)
{
SimObject *obj;
obj = Sim::findObject("MoveCursor");
mMoveCursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("UpDownCursor");
mUpDownCursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("LeftRightCursor");
mLeftRightCursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("DefaultCursor");
mDefaultCursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("NESWCursor");
mNESWCursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("NWSECursor");
mNWSECursor = dynamic_cast<GuiCursor*>(obj);
obj = Sim::findObject("MoveCursor");
mMoveCursor = dynamic_cast<GuiCursor*>(obj);
return(mMoveCursor != NULL && mUpDownCursor != NULL && mLeftRightCursor != NULL && mDefaultCursor != NULL && mNWSECursor != NULL && mNESWCursor != NULL && mMoveCursor != NULL);
}
else
return(true);
}
void GuiEditCtrl::setEditMode(bool value)
{
mActive = value;
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
if (mActive && mAwake)
mCurrentAddSet = NULL;
}
void GuiEditCtrl::setCurrentAddSet(GuiControl *ctrl)
{
if (ctrl != mCurrentAddSet)
{
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
mCurrentAddSet = ctrl;
}
}
void GuiEditCtrl::clearSelection(void)
{
mSelectedControls.clear();
}
void GuiEditCtrl::setSelection(GuiControl *ctrl, bool inclusive)
{
//sanity check
if (! ctrl)
return;
if(mContentControl == ctrl)
{
mCurrentAddSet = ctrl;
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
}
else
{
// otherwise, we hit a new control...
GuiControl *newAddSet = ctrl->getParent();
//see if we should clear the old selection set
if (newAddSet != mCurrentAddSet || (! inclusive)) {
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
}
//set the selection
mCurrentAddSet = newAddSet;
if (!(ctrl->isLocked())) {
mSelectedControls.push_back(ctrl);
// Con::executef(this, 2, "onAddSelected", avar("%d", ctrl->getId()));
}
}
}
void GuiEditCtrl::addNewControl(GuiControl *ctrl)
{
if (! mCurrentAddSet)
mCurrentAddSet = mContentControl;
mCurrentAddSet->addObject(ctrl);
Con::executef(this, 1, "onClearSelected");
mSelectedControls.clear();
if (!(ctrl->isLocked())) {
mSelectedControls.push_back(ctrl);
Con::executef(this, 2, "onAddSelected", avar("%d", ctrl->getId()));
}
}
void GuiEditCtrl::drawNut(const Point2I &nut, ColorI &outlineColor, ColorI &nutColor)
{
RectI r(nut.x - NUT_SIZE, nut.y - NUT_SIZE, 2 * NUT_SIZE + 1, 2 * NUT_SIZE + 1);
r.inset(1, 1);
dglDrawRectFill(r, nutColor);
r.inset(-1, -1);
dglDrawRect(r, outlineColor);
}
static inline bool inNut(const Point2I &pt, S32 x, S32 y)
{
S32 dx = pt.x - x;
S32 dy = pt.y - y;
return dx <= NUT_SIZE && dx >= -NUT_SIZE && dy <= NUT_SIZE && dy >= -NUT_SIZE;
}
S32 GuiEditCtrl::getSizingHitKnobs(const Point2I &pt, const RectI &box)
{
S32 lx = box.point.x, rx = box.point.x + box.extent.x - 1;
S32 cx = (lx + rx) >> 1;
S32 ty = box.point.y, by = box.point.y + box.extent.y - 1;
S32 cy = (ty + by) >> 1;
if (inNut(pt, lx, ty))
return sizingLeft | sizingTop;
if (inNut(pt, cx, ty))
return sizingTop;
if (inNut(pt, rx, ty))
return sizingRight | sizingTop;
if (inNut(pt, lx, by))
return sizingLeft | sizingBottom;
if (inNut(pt, cx, by))
return sizingBottom;
if (inNut(pt, rx, by))
return sizingRight | sizingBottom;
if (inNut(pt, lx, cy))
return sizingLeft;
if (inNut(pt, rx, cy))
return sizingRight;
return sizingNone;
}
void GuiEditCtrl::drawNuts(RectI &box, ColorI &outlineColor, ColorI &nutColor)
{
S32 lx = box.point.x, rx = box.point.x + box.extent.x - 1;
S32 cx = (lx + rx) >> 1;
S32 ty = box.point.y, by = box.point.y + box.extent.y - 1;
S32 cy = (ty + by) >> 1;
ColorF greenLine(0,1,0,0.6);
ColorF lightGreenLine(0,1,0,0.3);
if(lx > 0 && ty > 0)
{
dglDrawLine(0, ty, lx, ty, greenLine);
dglDrawLine(lx, 0, lx, ty, greenLine);
}
if(lx > 0 && by > 0)
dglDrawLine(0, by, lx, by, greenLine);
if(rx > 0 && ty > 0)
dglDrawLine(rx, 0, rx, ty, greenLine);
Point2I extent = localToGlobalCoord(mBounds.extent);
if(lx < extent.x && by < extent.y)
dglDrawLine(lx, by, lx, extent.y, lightGreenLine);
if(rx < extent.x && by < extent.y)
{
dglDrawLine(rx, by, rx, extent.y, lightGreenLine);
dglDrawLine(rx, by, extent.x, by, lightGreenLine);
}
if(rx < extent.x && ty < extent.y)
dglDrawLine(rx, ty, extent.x, ty, lightGreenLine);
drawNut(Point2I(lx, ty), outlineColor, nutColor);
drawNut(Point2I(lx, cy), outlineColor, nutColor);
drawNut(Point2I(lx, by), outlineColor, nutColor);
drawNut(Point2I(rx, ty), outlineColor, nutColor);
drawNut(Point2I(rx, cy), outlineColor, nutColor);
drawNut(Point2I(rx, by), outlineColor, nutColor);
drawNut(Point2I(cx, ty), outlineColor, nutColor);
drawNut(Point2I(cx, by), outlineColor, nutColor);
}
void GuiEditCtrl::getDragRect(RectI &box)
{
box.point.x = getMin(mLastMousePos.x, mSelectionAnchor.x);
box.extent.x = getMax(mLastMousePos.x, mSelectionAnchor.x) - box.point.x + 1;
box.point.y = getMin(mLastMousePos.y, mSelectionAnchor.y);
box.extent.y = getMax(mLastMousePos.y, mSelectionAnchor.y) - box.point.y + 1;
}
void GuiEditCtrl::onPreRender()
{
setUpdate();
}
void GuiEditCtrl::onRender(Point2I offset, const RectI &updateRect)
{
Point2I ctOffset;
Point2I cext;
bool keyFocused = isFirstResponder();
if (mActive)
{
if (mCurrentAddSet)
{
// draw a white frame inset around the current add set.
cext = mCurrentAddSet->getExtent();
ctOffset = mCurrentAddSet->localToGlobalCoord(Point2I(0,0));
#ifdef TGE_RPG
//建立当前控件外的灰色掩蔽
Point2I pt(ctOffset.x + cext.x,ctOffset.y + cext.y);
RectI box(0,0, mBounds.extent.x, ctOffset.y);
//box.set(0,0, mBounds.extent.x, ctOffset.y);
dglDrawRectFill(box,m_maskColor);
box.set(0,pt.y, mBounds.extent.x, mBounds.extent.y - pt.y);
dglDrawRectFill(box,m_maskColor);
box.set(0,ctOffset.y, ctOffset.x, cext.y);
dglDrawRectFill(box,m_maskColor);
box.set(pt.x,ctOffset.y, mBounds.extent.x - pt.x, cext.y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -