better Console support

* Arguments are passed as std::string
* Adds `resolution` and `projection` command
* `texture` and `fileutils` supports the `flush` argument
This commit is contained in:
Ricardo Quesada 2014-02-08 12:46:44 -08:00
parent af7a591483
commit 560abe77c8
7 changed files with 244 additions and 51 deletions

View File

@ -1,6 +1,7 @@
cocos2d-x-3.0rc0 Feb.?? 2014
[All]
[NEW] Adds Dutch Language support.
[NEW] Console: Added 'resolution', 'projection' commands. Improved API
[FIX] EGLView improvements: renamed to GLView, no longer a singleton, easier to customize
[FIX] Removes samples except testcpp|testjavascript|testlua. Moves sample games to `cocos2d/samples` repo.

View File

@ -151,7 +151,15 @@ void GLViewProtocol::setFrameSize(float width, float height)
_designResolutionSize = _screenSize = Size(width, height);
}
Size GLViewProtocol::getVisibleSize() const
Rect GLViewProtocol::getVisibleRect() const
{
Rect ret;
ret.size = getVisibleSize();
ret.origin = getVisibleOrigin();
return ret;
}
Size GLViewProtocol::getVisibleSize() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{

View File

@ -117,6 +117,11 @@ public:
*/
virtual Point getVisibleOrigin() const;
/**
* Get the visible rectangle of opengl viewport.
*/
virtual Rect getVisibleRect() const;
/**
* Set the design resolution size.
* @param width Design resolution width.
@ -177,6 +182,8 @@ public:
*/
float getScaleY() const;
/** returns the current Resolution policy */
ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; }
protected:
void handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, int ids[], float xs[], float ys[]);

View File

@ -26,6 +26,10 @@
#include <thread>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <sstream>
#include <stdio.h>
#include <time.h>
@ -53,10 +57,32 @@
#include "platform/CCFileUtils.h"
#include "CCConfiguration.h"
#include "CCTextureCache.h"
#include "CCGLView.h"
NS_CC_BEGIN
//
// Trimming functions were taken from: http://stackoverflow.com/a/217605
//
// trim from start
static std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
// helper free functions
// dprintf() is not defined in Android
@ -201,28 +227,31 @@ Console::Console()
{
// VS2012 doesn't support initializer list, so we create a new array and assign its elements to '_command'.
Command commands[] = {
{ "config", std::bind(&Console::commandConfig, this, std::placeholders::_1, std::placeholders::_2) },
{ "debug msg on", [&](int fd, const char* command) {
_sendDebugStrings = true;
{ "config", "prints the Configuration object", std::bind(&Console::commandConfig, this, std::placeholders::_1, std::placeholders::_2) },
{ "debug msg", "[on | off] Whether or not to forward the debug messages on the console", [&](int fd, const std::string& args) {
if( args.compare("on")==0 || args.compare("off")==0) {
_sendDebugStrings = (args.compare("on") == 0);
} else {
mydprintf(fd, "Supported arguments: 'on' or 'off'\n");
}
} },
{ "debug msg off", [&](int fd, const char* command) {
_sendDebugStrings = false;
{ "exit", "Close connection to the console", std::bind(&Console::commandExit, this, std::placeholders::_1, std::placeholders::_2) },
{ "fileutils", "[flush | ] Flush or print the FileUtils info", std::bind(&Console::commandFileUtils, this, std::placeholders::_1, std::placeholders::_2) },
{ "fps", "[on | off] Turn on|off the FPS", [](int fd, const std::string& args) {
if( args.compare("on")==0 || args.compare("off")==0) {
bool state = (args.compare("on") == 0);
Director *dir = Director::getInstance();
Scheduler *sched = dir->getScheduler();
sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, state));
} else {
mydprintf(fd, "Supported arguments: 'on' or 'off'\n");
}
} },
{ "exit", std::bind(&Console::commandExit, this, std::placeholders::_1, std::placeholders::_2) },
{ "fileutils dump", std::bind(&Console::commandFileUtilsDump, this, std::placeholders::_1, std::placeholders::_2) },
{ "fps on", [](int fd, const char* command) {
Director *dir = Director::getInstance();
Scheduler *sched = dir->getScheduler();
sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, true));
} },
{ "fps off", [](int fd, const char* command) {
Director *dir = Director::getInstance();
Scheduler *sched = dir->getScheduler();
sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, false));
} },
{ "help", std::bind(&Console::commandHelp, this, std::placeholders::_1, std::placeholders::_2) },
{ "scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) },
{ "textures", std::bind(&Console::commandTextures, this, std::placeholders::_1, std::placeholders::_2) },
{ "help", "Print this message", std::bind(&Console::commandHelp, this, std::placeholders::_1, std::placeholders::_2) },
{ "projection", "[2d | 3d] Changes or print the current projection", std::bind(&Console::commandProjection, this, std::placeholders::_1, std::placeholders::_2) },
{ "resolution", "[width height | ] Changes or print the window resolution", std::bind(&Console::commandResolution, this, std::placeholders::_1, std::placeholders::_2) },
{ "scene graph", "Print the scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) },
{ "texture", "[flush | ] Flush or print the TextureCache info", std::bind(&Console::commandTextures, this, std::placeholders::_1, std::placeholders::_2) },
};
_maxCommands = sizeof(commands)/sizeof(commands[0]);
@ -338,7 +367,7 @@ void Console::setUserCommands(Command *commands, int numberOfCommands)
// commands
//
void Console::commandHelp(int fd, const char* command)
void Console::commandHelp(int fd, const std::string &args)
{
const char help[] = "\nAvailable commands:\n";
write(fd, help, sizeof(help));
@ -357,26 +386,38 @@ void Console::commandHelp(int fd, const char* command)
}
void Console::commandExit(int fd, const char *command)
void Console::commandExit(int fd, const std::string &args)
{
FD_CLR(fd, &_read_set);
_fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
close(fd);
}
void Console::commandSceneGraph(int fd, const char *command)
void Console::commandSceneGraph(int fd, const std::string &args)
{
Scheduler *sched = Director::getInstance()->getScheduler();
sched->performFunctionInCocosThread( std::bind(&printSceneGraphBoot, fd) );
}
void Console::commandFileUtilsDump(int fd, const char *command)
void Console::commandFileUtils(int fd, const std::string &args)
{
Scheduler *sched = Director::getInstance()->getScheduler();
sched->performFunctionInCocosThread( std::bind(&printFileUtils, fd) );
if( args.compare("flush") == 0 )
{
FileUtils::getInstance()->purgeCachedEntries();
}
else if( args.length()==0)
{
sched->performFunctionInCocosThread( std::bind(&printFileUtils, fd) );
}
else
{
mydprintf(fd, "Unsupported argument: '%s'. Supported arguments: 'flush' or nothing", args.c_str());
}
}
void Console::commandConfig(int fd, const char *command)
void Console::commandConfig(int fd, const std::string& args)
{
Scheduler *sched = Director::getInstance()->getScheduler();
sched->performFunctionInCocosThread( [&](){
@ -385,13 +426,112 @@ void Console::commandConfig(int fd, const char *command)
);
}
void Console::commandTextures(int fd, const char *command)
void Console::commandResolution(int fd, const std::string& args)
{
if(args.length()==0) {
auto director = Director::getInstance();
Size points = director->getWinSize();
Size pixels = director->getWinSizeInPixels();
auto glview = director->getOpenGLView();
Size design = glview->getDesignResolutionSize();
ResolutionPolicy res = glview->getResolutionPolicy();
Rect visibleRect = glview->getVisibleRect();
mydprintf(fd, "Window Size:\n"
"\t%d x %d (points)\n"
"\t%d x %d (pixels)\n"
"\t%d x %d (design resolution)\n"
"Resolution Policy: %d\n"
"Visible Rect:\n"
"\torigin: %d x %d\n"
"\tsize: %d x %d",
(int)points.width, (int)points.height,
(int)pixels.width, (int)pixels.height,
(int)design.width, (int)design.height,
(int)res,
(int)visibleRect.origin.x, (int)visibleRect.origin.y,
(int)visibleRect.size.width, (int)visibleRect.size.height
);
} else {
int width, height, policy;
std::istringstream stream( args );
stream >> width >> height>> policy;
Scheduler *sched = Director::getInstance()->getScheduler();
sched->performFunctionInCocosThread( [&](){
Director::getInstance()->getOpenGLView()->setDesignResolutionSize(width, height, static_cast<ResolutionPolicy>(policy));
} );
}
}
void Console::commandProjection(int fd, const std::string& args)
{
auto director = Director::getInstance();
Scheduler *sched = director->getScheduler();
if(args.length()==0)
{
char buf[20];
auto proj = director->getProjection();
switch (proj) {
case cocos2d::Director::Projection::_2D:
sprintf(buf,"2d");
break;
case cocos2d::Director::Projection::_3D:
sprintf(buf,"3d");
break;
case cocos2d::Director::Projection::CUSTOM:
sprintf(buf,"custom");
break;
default:
sprintf(buf,"unknown");
break;
}
mydprintf(fd, "Current projection: %s", buf);
}
else if( args.compare("2d") == 0)
{
sched->performFunctionInCocosThread( [&](){
director->setProjection(Director::Projection::_2D);
} );
}
else if( args.compare("3d") == 0)
{
sched->performFunctionInCocosThread( [&](){
director->setProjection(Director::Projection::_3D);
} );
}
else
{
mydprintf(fd, "Unsupported argument: '%s'. Supported arguments: '2d' or '3d'", args.c_str());
}
}
void Console::commandTextures(int fd, const std::string& args)
{
Scheduler *sched = Director::getInstance()->getScheduler();
sched->performFunctionInCocosThread( [&](){
mydprintf(fd, "%s", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
if( args.compare("flush")== 0)
{
sched->performFunctionInCocosThread( [&](){
Director::getInstance()->getTextureCache()->removeAllTextures();
}
);
}
else if(args.length()==0)
{
sched->performFunctionInCocosThread( [&](){
mydprintf(fd, "%s", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
}
);
}
else
{
mydprintf(fd, "Unsupported argument: '%s'. Supported arguments: 'flush' or nothing", args.c_str());
}
);
}
@ -403,12 +543,19 @@ bool Console::parseCommand(int fd)
bool found=false;
for(int i=0; i < _maxCommands; ++i) {
if( strncmp(_buffer, _commands[i].name,strlen(_commands[i].name)) == 0 ) {
ssize_t commandLen = strlen(_commands[i].name);
if( strncmp(_buffer, _commands[i].name,commandLen) == 0 ) {
// XXX TODO FIXME
// Ideally this loop should execute the function in the cocos2d according to a variable
// But clang crashes in runtime when doing that (bug in clang, not in the code).
// So, unfortunately, the only way to fix it was to move that logic to the callback itself
_commands[i].callback(fd, _buffer);
std::string args;
if(strlen(_buffer) >= commandLen+2) {
args = std::string(&_buffer[commandLen]+1);
args = trim(args);
}
_commands[i].callback(fd, args);
found = true;
break;
}
@ -416,8 +563,14 @@ bool Console::parseCommand(int fd)
// user commands
for(int i=0; i < _maxUserCommands && !found; ++i) {
if( strncmp(_buffer, _userCommands[i].name,strlen(_userCommands[i].name)) == 0 ) {
_userCommands[i].callback(fd, _buffer);
ssize_t commandLen = strlen(_userCommands[i].name);
if( strncmp(_buffer, _userCommands[i].name,commandLen) == 0 ) {
std::string args;
if(strlen(_buffer) >= commandLen+2) {
args = std::string(&_buffer[commandLen]+1);
args = trim(args);
}
_userCommands[i].callback(fd, args);
found = true;
break;
}

View File

@ -73,7 +73,8 @@ class CC_DLL Console
public:
struct Command {
const char *name;
std::function<void(int, const char*)> callback;
const char *help;
std::function<void(int, const std::string&)> callback;
};
/** Constructor */
@ -105,12 +106,14 @@ protected:
void addClient();
// Add commands here
void commandHelp(int fd, const char *command);
void commandExit(int fd, const char *command);
void commandSceneGraph(int fd, const char *command);
void commandFileUtilsDump(int fd, const char *command);
void commandConfig(int fd, const char *command);
void commandTextures(int fd, const char *command);
void commandHelp(int fd, const std::string &args);
void commandExit(int fd, const std::string &args);
void commandSceneGraph(int fd, const std::string &args);
void commandFileUtils(int fd, const std::string &args);
void commandConfig(int fd, const std::string &args);
void commandTextures(int fd, const std::string &args);
void commandResolution(int fd, const std::string &args);
void commandProjection(int fd, const std::string &args);
// file descriptor: socket, console, etc.
int _listenfd;

View File

@ -168,10 +168,10 @@ ConsoleCustomCommand::ConsoleCustomCommand()
_console = Director::getInstance()->getConsole();
static struct Console::Command commands[] = {
{"hello", [](int fd, const char* command) {
const char msg[] = "how are you?\nYou typed: ";
{"hello", "help string goes here", [](int fd, const std::string &args) {
const char msg[] = "how are you?\nPassed arguments: ";
write(fd, msg, sizeof(msg));
write(fd, command, strlen(command));
write(fd, args.c_str(), args.length());
write(fd, "\n",1);
}},
};

View File

@ -1,15 +1,36 @@
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "VisibleRect.h"
Rect VisibleRect::s_visibleRect;
void VisibleRect::lazyInit()
{
if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f)
{
auto glView = Director::getInstance()->getOpenGLView();
s_visibleRect.origin = glView->getVisibleOrigin();
s_visibleRect.size = glView->getVisibleSize();
}
// no lazy init
// Useful if we change the resolution in runtime
s_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect();
}
Rect VisibleRect::getVisibleRect()