Guichan-xml: example how to use absolute touch coordinates instead of emulated mouse events in Guichan

This commit is contained in:
pelya
2013-12-01 00:48:59 +02:00
parent 5b8ced6c15
commit 7e12b8c268
5 changed files with 1298 additions and 1231 deletions

View File

@@ -75,6 +75,10 @@ CompatibilityHacksSlowCompatibleEventQueue=n
# Save and restore OpenGL state when drawing on-screen keyboard for apps that use SDL_OPENGL
CompatibilityHacksTouchscreenKeyboardSaveRestoreOpenGLState=
# Application uses SDL_UpdateRects() properly, and does not draw in any region outside those rects.
# This improves drawing speed, but I know only one application that does that, and it's written by me (y)/(n)
CompatibilityHacksProperUsageOfSDL_UpdateRects=
# Application uses mouse (y) or (n), this will show mouse emulation dialog to the user
AppUsesMouse=y
@@ -82,10 +86,13 @@ AppUsesMouse=y
AppNeedsTwoButtonMouse=y
# Show SDL mouse cursor, for applications that do not draw cursor at all (y) or (n)
ShowMouseCursor=n
ShowMouseCursor=y
# Generate more touch events, by default SDL generates one event per one video frame, this is useful for drawing apps (y) or (n)
GenerateSubframeTouchEvents=
# Force relative (laptop) mouse movement mode, useful when both on-screen keyboard and mouse are needed (y) or (n)
ForceRelativeMouseMode=n
ForceRelativeMouseMode=y
# Application needs arrow keys (y) or (n), will show on-screen dpad/joystick (y) or (n)
AppNeedsArrowKeys=n
@@ -106,7 +113,7 @@ AppUsesAccelerometer=n
AppUsesGyroscope=n
# Application uses multitouch (y) or (n), multitouch events are passed as SDL_JOYBALLMOTION events for the joystick 0
AppUsesMultitouch=n
AppUsesMultitouch=y
# Application records audio (it will use any available source, such a s microphone)
# API is defined in file SDL_android.h: int SDL_ANDROID_OpenAudioRecording(SDL_AudioSpec *spec); void SDL_ANDROID_CloseAudioRecording(void);
@@ -179,7 +186,7 @@ AppVersionName="1.01"
ResetSdlConfigForThisVersion=n
# Delete application data files when upgrading (specify file/dir paths separated by spaces)
DeleteFilesOnUpgrade=""
DeleteFilesOnUpgrade="%"
# Optional shared libraries to compile - removing some of them will save space
# MP3 support by libMAD is encumbered by patents and libMAD is GPL-ed

View File

@@ -1,208 +1,268 @@
/**
* SDL Hello World example for Guichan.
*/
// Include all necessary headers.
#include <iostream>
#include <guichan.hpp>
#include <guichan/sdl.hpp>
#include <SDL/SDL.h>
#include "xmlgui.h"
/*
* Common stuff we need
*/
bool running = true;
/*
* SDL Stuff we need
*/
SDL_Surface* screen;
SDL_Event event;
/*
* Guichan SDL stuff we need
*/
gcn::SDLInput* input; // Input driver
gcn::SDLGraphics* graphics; // Graphics driver
gcn::SDLImageLoader* imageLoader; // For loading images
/*
* Guichan stuff we need
*/
gcn::Gui* gui; // A Gui object - binds it all together
gcn::Container* top; // A top container
gcn::ImageFont* font; // A font
//XmlGui stuff
XmlGui *xmlgui = new XmlGui();
/**
* Initializes the Hello World
*/
void init()
{
/*
* Here we initialize SDL as we would do with any SDL application.
*/
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
// We want unicode
SDL_EnableUNICODE(1);
// We want to enable key repeat
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
/*
* Now it's time for Guichan SDL stuff
*/
imageLoader = new gcn::SDLImageLoader();
// The ImageLoader in use is static and must be set to be
// able to load images
gcn::Image::setImageLoader(imageLoader);
graphics = new gcn::SDLGraphics();
// Set the target for the graphics object to be the screen.
// In other words, we will draw to the screen.
// Note, any surface will do, it doesn't have to be the screen.
graphics->setTarget(screen);
input = new gcn::SDLInput();
/*
* Last but not least it's time to initialize and create the gui
* with Guichan stuff.
*/
gui = new gcn::Gui();
// Set gui to use the SDLGraphics object.
gui->setGraphics(graphics);
// Set gui to use the SDLInput object
gui->setInput(input);
// Set the top container
gui->setTop(top);
// Load the image font.
font = new gcn::ImageFont("fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
// The global font is static and must be set.
gcn::Widget::setGlobalFont(font);
xmlgui = new XmlGui();
xmlgui->parse("gui.xml");
gui->setTop(xmlgui->getWidget("top"));
}
/**
* Halts the application
*/
void halt()
{
/*
* Destroy Guichan stuff
*/
delete xmlgui;
delete font;
delete top;
delete gui;
/*
* Destroy Guichan SDL stuff
*/
delete input;
delete graphics;
delete imageLoader;
/*
* Destroy SDL stuff
*/
SDL_Quit();
}
/**
* Checks input. On escape halt the application.
*/
void checkInput()
{
/*
* Poll SDL events
*/
while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
if (event.key.keysym.sym == SDLK_f)
{
if (event.key.keysym.mod & KMOD_CTRL)
{
// Works with X11 only
SDL_WM_ToggleFullScreen(screen);
}
}
}
else if(event.type == SDL_QUIT)
{
running = false;
}
/*
* Now that we are done polling and using SDL events we pass
* the leftovers to the SDLInput object to later be handled by
* the Gui. (This example doesn't require us to do this 'cause a
* label doesn't use input. But will do it anyway to show how to
* set up an SDL application with Guichan.)
*/
input->pushInput(event);
}
}
/**
* Runs the application
*/
void run()
{
while(running)
{
// Poll input
checkInput();
// Let the gui perform it's logic (like handle input)
gui->logic();
// Draw the gui
gui->draw();
// Update the screen
SDL_Flip(screen);
}
}
int main(int argc, char **argv)
{
try
{
init();
run();
halt();
}
/*
* Catch all Guichan exceptions
*/
catch (gcn::Exception e)
{
std::cerr << e.getMessage() << std::endl;
return 1;
}
/*
* Catch all Std exceptions
*/
catch (std::exception e)
{
std::cerr << "Std exception: " << e.what() << std::endl;
return 1;
}
/*
* Catch all Unknown exceptions
*/
catch (...)
{
std::cerr << "Unknown exception" << std::endl;
return 1;
}
return 0;
}
/**
* SDL Hello World example for Guichan.
*/
// Include all necessary headers.
#include <iostream>
#include <guichan.hpp>
#include <guichan/sdl.hpp>
#include <SDL/SDL.h>
//#include <android/log.h>
#include "xmlgui.h"
/*
* Common stuff we need
*/
bool running = true;
/*
* SDL Stuff we need
*/
SDL_Surface* screen;
SDL_Event event;
/*
* Guichan SDL stuff we need
*/
gcn::SDLInput* input; // Input driver
gcn::SDLGraphics* graphics; // Graphics driver
gcn::SDLImageLoader* imageLoader; // For loading images
/*
* Guichan stuff we need
*/
gcn::Gui* gui; // A Gui object - binds it all together
gcn::Container* top; // A top container
gcn::ImageFont* font; // A font
//XmlGui stuff
XmlGui *xmlgui = new XmlGui();
/**
* Initializes the Hello World
*/
void init()
{
/*
* Here we initialize SDL as we would do with any SDL application.
*/
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
// We want unicode
SDL_EnableUNICODE(1);
// We want to enable key repeat
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
// Enable Android multitouch
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_JoystickOpen(0);
/*
* Now it's time for Guichan SDL stuff
*/
imageLoader = new gcn::SDLImageLoader();
// The ImageLoader in use is static and must be set to be
// able to load images
gcn::Image::setImageLoader(imageLoader);
graphics = new gcn::SDLGraphics();
// Set the target for the graphics object to be the screen.
// In other words, we will draw to the screen.
// Note, any surface will do, it doesn't have to be the screen.
graphics->setTarget(screen);
input = new gcn::SDLInput();
/*
* Last but not least it's time to initialize and create the gui
* with Guichan stuff.
*/
gui = new gcn::Gui();
// Set gui to use the SDLGraphics object.
gui->setGraphics(graphics);
// Set gui to use the SDLInput object
gui->setInput(input);
// Set the top container
gui->setTop(top);
// Load the image font.
font = new gcn::ImageFont("fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
// The global font is static and must be set.
gcn::Widget::setGlobalFont(font);
xmlgui = new XmlGui();
xmlgui->parse("gui.xml");
gui->setTop(xmlgui->getWidget("top"));
}
/**
* Halts the application
*/
void halt()
{
/*
* Destroy Guichan stuff
*/
delete xmlgui;
delete font;
delete top;
delete gui;
/*
* Destroy Guichan SDL stuff
*/
delete input;
delete graphics;
delete imageLoader;
/*
* Destroy SDL stuff
*/
SDL_Quit();
}
/**
* Checks input. On escape halt the application.
*/
void checkInput()
{
/*
* Poll SDL events
*/
while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
if (event.key.keysym.sym == SDLK_f)
{
if (event.key.keysym.mod & KMOD_CTRL)
{
// Works with X11 only
SDL_WM_ToggleFullScreen(screen);
}
}
}
else if(event.type == SDL_QUIT)
{
running = false;
}
/*
* Now that we are done polling and using SDL events we pass
* the leftovers to the SDLInput object to later be handled by
* the Gui. (This example doesn't require us to do this 'cause a
* label doesn't use input. But will do it anyway to show how to
* set up an SDL application with Guichan.)
*/
if (event.type == SDL_MOUSEMOTION ||
event.type == SDL_MOUSEBUTTONDOWN ||
event.type == SDL_MOUSEBUTTONUP)
{
// Filter emulated mouse events for Guichan, we wand absolute input
}
else
{
// Convert multitouch event to SDL mouse event
static int x = 0, y = 0, buttons = 0;
SDL_Event event2;
memcpy(&event2, &event, sizeof(event));
if (event.type == SDL_JOYBALLMOTION &&
event.jball.which == 0 &&
event.jball.ball == 0)
{
event2.type = SDL_MOUSEMOTION;
event2.motion.which = 0;
event2.motion.state = buttons;
event2.motion.xrel = event.jball.xrel - x;
event2.motion.yrel = event.jball.yrel - y;
x = event.jball.xrel;
y = event.jball.yrel;
event2.motion.x = x;
event2.motion.y = y;
//__android_log_print(ANDROID_LOG_INFO, "GUICHAN","Mouse motion %d %d btns %d", x, y, buttons);
if (buttons == 0)
{
// Push mouse motion event first, then button down event
input->pushInput(event2);
buttons = SDL_BUTTON_LMASK;
event2.type = SDL_MOUSEBUTTONDOWN;
event2.button.which = 0;
event2.button.button = SDL_BUTTON_LEFT;
event2.button.state = SDL_PRESSED;
event2.button.x = x;
event2.button.y = y;
//__android_log_print(ANDROID_LOG_INFO, "GUICHAN","Mouse button %d coords %d %d", buttons, x, y);
}
}
if (event.type == SDL_JOYBUTTONUP &&
event.jbutton.which == 0 &&
event.jbutton.button == 0)
{
// Do not push button down event here, because we need mouse motion event first
buttons = 0;
event2.type = SDL_MOUSEBUTTONUP;
event2.button.which = 0;
event2.button.button = SDL_BUTTON_LEFT;
event2.button.state = SDL_RELEASED;
event2.button.x = x;
event2.button.y = y;
//__android_log_print(ANDROID_LOG_INFO, "GUICHAN","Mouse button %d coords %d %d", buttons, x, y);
}
input->pushInput(event2);
}
}
}
/**
* Runs the application
*/
void run()
{
while(running)
{
// Poll input
checkInput();
// Let the gui perform it's logic (like handle input)
gui->logic();
// Draw the gui
gui->draw();
// Update the screen
SDL_Flip(screen);
}
}
int main(int argc, char **argv)
{
try
{
init();
run();
halt();
}
/*
* Catch all Guichan exceptions
*/
catch (gcn::Exception e)
{
std::cerr << e.getMessage() << std::endl;
return 1;
}
/*
* Catch all Std exceptions
*/
catch (std::exception e)
{
std::cerr << "Std exception: " << e.what() << std::endl;
return 1;
}
/*
* Catch all Unknown exceptions
*/
catch (...)
{
std::cerr << "Unknown exception" << std::endl;
return 1;
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,155 +1,155 @@
/* XmlGui - xml parsing class for guichan (http://guichan.sourceforge.net)
* author - quard (quard8@gmail.com)
* Changes:
* v. 0.4 - fixed bug in Visual Studio .NET 2003, when compiler option /GR is not enabled;
fixed widget allocation, when attribute "name" is't exists
deleted all "return false" after GCN_EXCEPTION, because they not used ever
* v. 0.3.5 - fixed stupid bug, when widgets not parsed if parent widget is NULL
* v. 0.3 - added parsing method for widget,
* now checking widget node is in one function
* v. 0.2 - small bug fix
* - added parsing widgets under 'container'
* - changed sscanf to atoi
* v. 0.1 - first public release
*/
#ifndef XML_GUI_H
#define XML_GUI_H
#include <guichan.hpp>
#include "tinyxml.h"
#include <map>
//!XmlGui class for loading gui widget from xml file
class XmlGui
{
public:
//!constructor
XmlGui();
//!parse xml file
//@param filename the file name to parse
//@return true if loaded ok
bool parse(const std::string &filename);
//@param name th widget name
//@return widget by name
gcn::Widget *getWidget(const std::string &name);
//!parse default parameters for all widgets
//@param element the xml element
//@param widget the current widget
void parseDefaults(TiXmlElement *element, gcn::Widget *widget);
//!parse container widget
//@param element the xml element
//@param widget the current widget
void parseContainer(TiXmlElement *element, gcn::Widget *parent);
//!parse label widget
//@param element the xml element
//@param widget the current widget
void parseLabel(TiXmlElement *element, gcn::Widget *parent);
//!parse button widget
//@param element the xml element
//@param widget the current widget
void parseButton(TiXmlElement *element, gcn::Widget *parent);
//!parse checkbox widget
//@param element the xml element
//@param widget the current widget
void parseCheckBox(TiXmlElement *element, gcn::Widget *parent);
//!parse radiobutton widget
//@param element the xml element
//@param widget the current widget
void parseRadioButton(TiXmlElement *element, gcn::Widget *parent);
//!parse icon widget
//@param element the xml element
//@param widget the current widget
void parseIcon(TiXmlElement *element, gcn::Widget *parent);
//!parse textbox widget
//@param element the xml element
//@param widget the current widget
void parseTextBox(TiXmlElement *element, gcn::Widget *parent);
//!parse textfield widget
//@param element the xml element
//@param widget the current widget
void parseTextField(TiXmlElement *element, gcn::Widget *parent);
//!parse slider widget
//@param element the xml element
//@param widget the current widget
void parseSlider(TiXmlElement *element, gcn::Widget *parent);
//!parse window widget
//@param element the xml element
//@param widget the current widget
void parseWindow(TiXmlElement *element, gcn::Widget *parent);
//!parse scrollarea widget
//@param element the xml element
//@param widget the current widget
void parseScrollArea(TiXmlElement *element,gcn::Widget *parent);
//!parse dropdown widget
//@param element the xml element
//@param widget the current widget
void parseDropdown(TiXmlElement *element,gcn::Widget *parent);
//!parse listbox widget
//@param element the xml element
//@param widget the current widget
void parseListbox(TiXmlElement *element,gcn::Widget *parent);
//!add actionlistener to array
//@param name the actionlistener name
//@param al the pointer to actionlistener class
void addActionListener(const std::string &name,gcn::ActionListener *al);
//!add font
//@param name the font name
//@param al the pointer to font class
void addFont(const std::string &name,gcn::Font *font);
private:
//!parse xml node
//@param element - xml element
//@param parent - the parent widget
void parseWidgets(TiXmlElement *element, gcn::Widget *parent);
//!adding widget to parent
//!parent widget can be Container,ScrollArea,Window. this function get class and set widget or add widget (for Container)
//@param widget our widget
//@param parent the parent widget
void addToParent(gcn::Widget *widget, gcn::Widget *parent);
//!check string value for boolean value
//@param value the string value
//@return true if value are "1" or "true"
bool checkBool(const std::string &value);
std::map<std::string,gcn::Widget*> widgets;
std::map<std::string,gcn::ActionListener*> actions;
std::map<std::string,gcn::Font*> fonts;
//temporary xml document (need by some functions)
TiXmlDocument *doc;
};
class XmlListModel : public gcn::ListModel
{
public:
virtual int getNumberOfElements();
virtual std::string getElementAt(int i);
std::vector<std::string> items;
};
/* XmlGui - xml parsing class for guichan (http://guichan.sourceforge.net)
* author - quard (quard8@gmail.com)
* Changes:
* v. 0.4 - fixed bug in Visual Studio .NET 2003, when compiler option /GR is not enabled;
fixed widget allocation, when attribute "name" is't exists
deleted all "return false" after GCN_EXCEPTION, because they not used ever
* v. 0.3.5 - fixed stupid bug, when widgets not parsed if parent widget is NULL
* v. 0.3 - added parsing method for widget,
* now checking widget node is in one function
* v. 0.2 - small bug fix
* - added parsing widgets under 'container'
* - changed sscanf to atoi
* v. 0.1 - first public release
*/
#ifndef XML_GUI_H
#define XML_GUI_H
#include <guichan.hpp>
#include "tinyxml.h"
#include <map>
//!XmlGui class for loading gui widget from xml file
class XmlGui
{
public:
//!constructor
XmlGui();
//!parse xml file
//@param filename the file name to parse
//@return true if loaded ok
bool parse(const std::string &filename);
//@param name th widget name
//@return widget by name
gcn::Widget *getWidget(const std::string &name);
//!parse default parameters for all widgets
//@param element the xml element
//@param widget the current widget
void parseDefaults(TiXmlElement *element, gcn::Widget *widget);
//!parse container widget
//@param element the xml element
//@param widget the current widget
void parseContainer(TiXmlElement *element, gcn::Widget *parent);
//!parse label widget
//@param element the xml element
//@param widget the current widget
void parseLabel(TiXmlElement *element, gcn::Widget *parent);
//!parse button widget
//@param element the xml element
//@param widget the current widget
void parseButton(TiXmlElement *element, gcn::Widget *parent);
//!parse checkbox widget
//@param element the xml element
//@param widget the current widget
void parseCheckBox(TiXmlElement *element, gcn::Widget *parent);
//!parse radiobutton widget
//@param element the xml element
//@param widget the current widget
void parseRadioButton(TiXmlElement *element, gcn::Widget *parent);
//!parse icon widget
//@param element the xml element
//@param widget the current widget
void parseIcon(TiXmlElement *element, gcn::Widget *parent);
//!parse textbox widget
//@param element the xml element
//@param widget the current widget
void parseTextBox(TiXmlElement *element, gcn::Widget *parent);
//!parse textfield widget
//@param element the xml element
//@param widget the current widget
void parseTextField(TiXmlElement *element, gcn::Widget *parent);
//!parse slider widget
//@param element the xml element
//@param widget the current widget
void parseSlider(TiXmlElement *element, gcn::Widget *parent);
//!parse window widget
//@param element the xml element
//@param widget the current widget
void parseWindow(TiXmlElement *element, gcn::Widget *parent);
//!parse scrollarea widget
//@param element the xml element
//@param widget the current widget
void parseScrollArea(TiXmlElement *element,gcn::Widget *parent);
//!parse dropdown widget
//@param element the xml element
//@param widget the current widget
void parseDropdown(TiXmlElement *element,gcn::Widget *parent);
//!parse listbox widget
//@param element the xml element
//@param widget the current widget
void parseListbox(TiXmlElement *element,gcn::Widget *parent);
//!add actionlistener to array
//@param name the actionlistener name
//@param al the pointer to actionlistener class
void addActionListener(const std::string &name,gcn::ActionListener *al);
//!add font
//@param name the font name
//@param al the pointer to font class
void addFont(const std::string &name,gcn::Font *font);
private:
//!parse xml node
//@param element - xml element
//@param parent - the parent widget
void parseWidgets(TiXmlElement *element, gcn::Widget *parent);
//!adding widget to parent
//!parent widget can be Container,ScrollArea,Window. this function get class and set widget or add widget (for Container)
//@param widget our widget
//@param parent the parent widget
void addToParent(gcn::Widget *widget, gcn::Widget *parent);
//!check string value for boolean value
//@param value the string value
//@return true if value are "1" or "true"
bool checkBool(const std::string &value);
std::map<std::string,gcn::Widget*> widgets;
std::map<std::string,gcn::ActionListener*> actions;
std::map<std::string,gcn::Font*> fonts;
//temporary xml document (need by some functions)
TiXmlDocument *doc;
};
class XmlListModel : public gcn::ListModel
{
public:
virtual int getNumberOfElements();
virtual std::string getElementAt(int i);
std::vector<std::string> items;
};
#endif