fixed picross sample

This commit is contained in:
Peter Schojer 2008-10-07 06:53:01 +00:00
parent c8f5d2ca64
commit a8bba08a18
7 changed files with 78 additions and 32 deletions

View File

@ -0,0 +1,3 @@
Picross.port = 9980
Picross.dataRoot = ${application.configDir}bin/data/
Picross.extjsRoot = ${application.configDir}../../testsuite/bin/

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

View File

@ -83,13 +83,14 @@ using namespace Poco::WebWidgets;
class WebAppHandlerFactory: public HTTPRequestHandlerFactory
{
public:
WebAppHandlerFactory(SharedPtr<WebApplication> pApp, const Poco::Path& dataPath, Poco::Logger* pLogger):
WebAppHandlerFactory(SharedPtr<WebApplication> pApp, const Poco::Path& dataPath, const Poco::Path& extJSPath, Poco::Logger* pLogger):
_pApp(pApp),
_dataRoot(dataPath),
_aliases(),
_pLogger(pLogger)
{
_aliases.insert(std::make_pair("", _dataRoot));
_aliases.insert(std::make_pair("extjs", extJSPath));
}
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
@ -168,29 +169,36 @@ protected:
{
if (!_helpRequested)
{
ResourceManager::Ptr pRM = new ResourceManager();
ExtJS::Utility::initialize(pRM, Poco::Path());
SharedPtr<WebApplication> pWebApp = new WebApplication(Poco::URI("/"), pRM);
LookAndFeel::Ptr laf(new LookAndFeel());
Poco::WebWidgets::ExtJS::Utility::initialize(laf);
pWebApp->setLookAndFeel(laf);
PicrossPage::Ptr ptr = new PicrossPage();
ptr->createComponents();
ptr->initComponents();
pWebApp->setCurrentPage(ptr);
unsigned short port = (unsigned short) config().getInt("Picross.port", 9980);
std::string data = config().getString("Picross.dataRoot", ".");
std::string extJSDir = config().getString("Picross.extjsRoot", ".");
Poco::Path aPath(data);
aPath.makeAbsolute();
aPath.makeDirectory();
Poco::File aFile(aPath);
if (!aFile.exists() || aFile.isFile())
throw Poco::Util::InvalidArgumentException("dataRoot is either a file or doesn't exist: must be directory!");
Poco::Path extJS(extJSDir);
extJS.makeAbsolute();
extJS.makeDirectory();
Poco::File extJSFile(extJS);
if (!extJSFile.exists() || extJSFile.isFile())
throw Poco::Util::InvalidArgumentException("extjsRoot is either a file or doesn't exist: must be directory!");
ResourceManager::Ptr pRM = new ResourceManager();
ExtJS::Utility::initialize(pRM, Poco::Path("/extjs"));
SharedPtr<WebApplication> pWebApp = new WebApplication(Poco::URI("/"), pRM);
LookAndFeel::Ptr laf(new LookAndFeel());
Poco::WebWidgets::ExtJS::Utility::initialize(laf);
pWebApp->setLookAndFeel(laf);
PicrossPage::Ptr ptr = new PicrossPage(aPath);
ptr->createComponents();
ptr->initComponents();
pWebApp->setCurrentPage(ptr);
ServerSocket svs(port);
HTTPServer srv(new WebAppHandlerFactory(pWebApp, aPath, &logger()), svs, new HTTPServerParams);
HTTPServer srv(new WebAppHandlerFactory(pWebApp, aPath, extJS, &logger()), svs, new HTTPServerParams);
srv.start();
waitForTerminationRequest();
srv.stop();

View File

@ -45,6 +45,47 @@ void PicrossFrame::initComponents()
void PicrossFrame::setupJavaScript(Page* pPage)
{
{
std::ostringstream str;
str << "function handleRightClick(elem, val) {" << std::endl;
str << "handleClick(elem, val, false);" << std::endl;
str << "}";
pPage->addDynamicFunction(str.str());
}
{
std::ostringstream str;
str << "function handleClick(elem, val, leftClick) {" << std::endl;
str << "if (val == 'O'){" << std::endl;
str << "elem.innerHTML = '<img src=\"ok.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" />';" << std::endl;
str << "}" << std::endl;
str << "else {" << std::endl;
str << "elem.innerHTML = '<img src=\"x.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" />';" << std::endl;
str << "if (leftClick){" << std::endl; // left mouse click means the user guesses there is an entry
str << "if (val != 'O') {" << std::endl;
str << "Ext.Msg.alert('Error', 'You guessed wrong');" << std::endl;
str << "}" << std::endl;
str << "}" << std::endl;
str << "else if (e.button == 2){" << std::endl;
str << "if (val == 'O') {" << std::endl;
str << "Ext.Msg.alert('Error', 'You guessed wrong');" << std::endl;
str << "}" << std::endl;
str << "}" << std::endl;
str << "}" << std::endl;
str << "}" << std::endl;
pPage->addDynamicFunction(str.str());
}
{
std::ostringstream str;
str << "function renderColumn(val){" << std::endl;
str << "var retVal = '<img src=\"default.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" ';"<< std::endl;
str << "retVal += '/>';" << std::endl;
str << "return retVal;" << std::endl;
str << "}"<< std::endl;
pPage->addDynamicFunction(str.str());
}
{
// setup cellclick event
//cellclick : ( Grid this, Number rowIndex, Number columnIndex, Ext.EventObject e )
@ -53,14 +94,7 @@ void PicrossFrame::setupJavaScript(Page* pPage)
str << "var rec = grid.store.getAt(row);" << std::endl;
str << "var val = rec.get(''+col);" << std::endl;
str << "var html = grid.getView().getCell(row,col);" << std::endl;
str << "if (val == 'O'){" << std::endl;
str << "html.firstChild.innerHTML = '<img src=\"ok.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" />';" << std::endl;
str << "}" << std::endl;
str << "else {" << std::endl;
str << "html.firstChild.innerHTML = '<img src=\"x.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" />';" << std::endl;
str << "}" << std::endl;
str << "handleClick(html.firstChild, val, (e.button == 0));" << std::endl;
str << "}" << std::endl;
_pGameTable->cellClicked.add(jsDelegate(str.str()));
}
@ -119,13 +153,7 @@ void PicrossFrame::createGameTable()
TableColumn::Ptr pCol = new TableColumn(new TextFieldCell(0), "", PI_CELLWIDTH, false);
pCol->resizable(false);
pCol->setEditable(false);
// return an image
std::ostringstream str;
str << "function(){";
str << "return '<img src=\"default.png\" width=\"";
str << "16\" height=\"13" << "\" border=\"0\" alt=\"\" />';";
str << "}";
pCol->setCustomRenderer(str.str());
pCol->setCustomRenderer("renderColumn");
tc.push_back(pCol);
}

View File

@ -1,7 +1,8 @@
#include "PicrossPage.h"
PicrossPage::PicrossPage()
PicrossPage::PicrossPage(const Poco::Path& dataRoot):
_dataRoot(dataRoot)
{
setWidth(800);
setHeight(600);
@ -10,7 +11,10 @@ PicrossPage::PicrossPage()
void PicrossPage::createComponents()
{
_pFrame = new PicrossFrame(Poco::File("x_16x16.raw"));
Poco::Path file(_dataRoot);
file.makeDirectory();
file.setFileName("x_16x16.raw");
_pFrame = new PicrossFrame(Poco::File(file));
add (_pFrame);
setupJavaScript();
}

View File

@ -7,13 +7,14 @@
#include "Poco/WebWidgets/Page.h"
#include "Poco/Path.h"
#include "PicrossFrame.h"
class PicrossPage: public Poco::WebWidgets::Page
{
public:
typedef Poco::AutoPtr<PicrossPage> Ptr;
PicrossPage();
PicrossPage(const Poco::Path& dataRoot);
void createComponents();
void initComponents();
@ -23,6 +24,7 @@ private:
private:
PicrossFrame::Ptr _pFrame;
Poco::Path _dataRoot;
};
#endif

View File

@ -60,6 +60,7 @@ public:
PeriodicUpdater(TableUpdater::Ptr pUpdater, Poco::UInt32 interValInSeconds);
/// Creates the PeriodicUpdater for the given table
/// You must call start manually to start it!
void update();
/// Updates the table from a given source