📄 luaconsoledlg.cpp
字号:
// LuaConsoleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "femmview.h"
#include "lua.h"
#include "LuaConsoleDlg.h"
extern lua_State *lua;
extern BOOL bLinehook;
extern BOOL lua_byebye;
CLuaConsoleDlg *pThis;
// CLuaConsoleDlg dialog
// IMPLEMENT_DYNAMIC(CLuaConsoleDlg, CResizableDialog)
CLuaConsoleDlg::CLuaConsoleDlg(CWnd* pParent /*=NULL*/)
: CResizableDialog(CLuaConsoleDlg::IDD, pParent)
, outbuffer(_T(""))
, inbuffer(_T(""))
{
}
CLuaConsoleDlg::~CLuaConsoleDlg()
{
}
void CLuaConsoleDlg::DoDataExchange(CDataExchange* pDX)
{
CResizableDialog::DoDataExchange(pDX);
// DDX_Text(pDX, IDC_LUA_OUTPUT, outbuffer);
DDX_Text(pDX, IDC_LUA_INPUT, inbuffer);
DDX_Control(pDX, IDC_LUA_INPUT, inbufferctrl);
DDX_Control(pDX, IDC_LUA_OUTPUT, ConsoleOutput);
}
BEGIN_MESSAGE_MAP(CLuaConsoleDlg, CResizableDialog)
ON_BN_CLICKED(IDC_CLEAR_INPUT, OnBnClickedClearInput)
ON_BN_CLICKED(IDC_CLEAR_OUTPUT, OnBnClickedClearOutput)
ON_BN_CLICKED(IDC_EVALUATE, OnBnClickedEvaluate)
END_MESSAGE_MAP()
// CLuaConsoleDlg message handlers
BOOL CLuaConsoleDlg::OnInitDialog()
{
CResizableDialog::OnInitDialog();
pThis=this;
// register lua extensions
lua_register(lua,"print",lua_Print);
// preset layout
AddAnchor(IDC_EVALUATE, BOTTOM_RIGHT);
AddAnchor(IDC_CLEAR_INPUT, BOTTOM_LEFT);
AddAnchor(IDC_CLEAR_OUTPUT, BOTTOM_LEFT);
AddAnchor(IDC_LUA_OUTPUT, TOP_LEFT, MIDDLE_RIGHT);
AddAnchor(IDC_LUA_INPUT, MIDDLE_LEFT,BOTTOM_RIGHT);
return TRUE;
}
void CLuaConsoleDlg::ToOutput(CString str)
{
int Length = ConsoleOutput.GetWindowTextLength();
ConsoleOutput.SetSel(Length, Length);
ConsoleOutput.ReplaceSel(str);
ConsoleOutput.LineScroll( ConsoleOutput.GetLineCount() );
}
int CLuaConsoleDlg::lua_Print(lua_State *L)
{
// Snatched this from the Lua source code,
// replacing writes to stdout with my custom
// output to the LuaConsole's edit box
int n = lua_gettop(L); // number of arguments
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++) {
CString s;
lua_pushvalue(L, -1); // function to be called
lua_pushvalue(L, i); // value to print
lua_rawcall(L, 1, 1);
s = lua_tostring(L, -1); // get result
if (i>1) pThis->ToOutput("\t");
else pThis->ToOutput("--> ");
pThis->ToOutput(s);
lua_pop(L, 1); // pop result
}
pThis->ToOutput("\r\n");
return 0;
}
void CLuaConsoleDlg::OnBnClickedClearInput()
{
if (bLinehook!=FALSE) return;
UpdateData(TRUE);
inbuffer="";
UpdateData(FALSE);
}
void CLuaConsoleDlg::OnBnClickedClearOutput()
{
if (bLinehook!=FALSE) return;
ConsoleOutput.SetWindowText("");
}
void CLuaConsoleDlg::OnBnClickedEvaluate()
{
if (bLinehook!=FALSE) return;
CString LuaCmd;
UpdateData();
ToOutput(inbuffer+"\r\n");
LuaCmd=inbuffer;
inbuffer="";
UpdateData(FALSE);
bLinehook=TRUE;
CStatusBar *StatBar=(CStatusBar *)((CFrameWnd*)AfxGetApp()->GetMainWnd())->GetMessageBar();
StatBar->SetPaneText(0,"EXECUTING LUASCRIPT -- HIT <BREAK> TO ABORT",TRUE);
int lua_error_code=lua_dostring(lua,LuaCmd);
if(lua_error_code!=FALSE){
if (lua_error_code==LUA_ERRRUN)
AfxMessageBox("Run Error");
// if (lua_error_code==LUA_ERRSYNTAX)
// AfxMessageBox("Syntax Error");
if (lua_error_code==LUA_ERRMEM)
AfxMessageBox("Lua memory Error");
if (lua_error_code==LUA_ERRERR)
AfxMessageBox("User error error");
if (lua_error_code==LUA_ERRFILE)
AfxMessageBox("File Error");
inbuffer=LuaCmd;
UpdateData(FALSE);
}
bLinehook=FALSE;
inbufferctrl.SetFocus();
StatBar->SetPaneText(0,"Ready",TRUE);
if(lua_byebye==TRUE){
ASSERT(AfxGetMainWnd() != NULL);
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -