#include "paragui.h" #include #include "pgapplication.h" #include "pglistbox.h" #include "pglistboxitem.h" #include "pgeventsupplier.h" /** Some general remarks: * * Since ParaGUI > 1.1.8, it is possible to create custom input sources. The * general procedure is: * - derive from PG_EventSupplier and fill it with your custom input code * - set your own event supplier as standard event supplier via * PG_Application::SetEventSupplier. * * This example is very simplistic. It takes the normal SDL events and converts * left mouse clicks into key down and right mouse clicks into key up events * before passing it on to a simple application where a listbox is scrolled * up/down. I don't do fancy stuff like merging mouse motion events, properly * handling button up events etc. which would be interesting for serious users. */ class My_EventSupplier : public PG_EventSupplier { public: // all abstract functions have to be declared bool PollEvent(SDL_Event* ev); bool PeepEvent(SDL_Event* ev); int WaitEvent(SDL_Event* ev); int GetMouseState(int& x, int& y); private: void convertClicks(SDL_Event* ev); }; // The first functions are not really interesting, copy&paste from standard // event supplier, apart from them all calling convertClicks bool My_EventSupplier::PollEvent(SDL_Event* event) { bool eventAvail = SDL_PollEvent( event ); if ( eventAvail ) convertClicks( event ); return eventAvail; } bool My_EventSupplier::PeepEvent(SDL_Event* event) { bool avail = SDL_PeepEvents( event, 1, SDL_PEEKEVENT, 0xffffffff ); if (avail) convertClicks(event); return avail; } int My_EventSupplier::WaitEvent(SDL_Event* event) { int res = SDL_WaitEvent( event ); convertClicks( event ); return res; } int My_EventSupplier::GetMouseState(int& x, int&y) { // quick hack x = y = 10; return 0; } // Now the core: Look for left/right mouse clicks and create key press events // instead. For details see the SDL header files or manpages. void My_EventSupplier::convertClicks(SDL_Event* ev) { if (ev->type & !(SDL_MOUSEBUTTONDOWN | SDL_MOUSEBUTTONUP)) return; SDL_KeyboardEvent convert; if (ev->type == SDL_MOUSEBUTTONDOWN) convert.type = SDL_KEYDOWN; else convert.type = SDL_KEYUP; if (ev->button.button & SDL_BUTTON_LEFT) convert.keysym.sym = SDLK_DOWN; else convert.keysym.sym = SDLK_UP; ev->key = convert; } /* The rest of the test program. It just contains an adjusted listbox that * selects the next/previous item on key up or down. */ class My_ListBox : public PG_ListBox { public: My_ListBox(PG_Widget* parent, PG_Rect rect) :PG_ListBox(parent, rect) {} protected: bool eventKeyDown(const SDL_KeyboardEvent* key); }; bool My_ListBox::eventKeyDown(const SDL_KeyboardEvent* key) { switch (key->keysym.sym) { case SDLK_UP: SelectPrevItem(); break; case SDLK_DOWN: SelectNextItem(); break; default: return false; } return true; } int main( int argc, char **argv ) { PG_Application app; My_EventSupplier supp; app.LoadTheme( "default" ); app.InitScreen( 640, 480, 0 ); app.SetEmergencyQuit(true); // IMPORTANT! PG_Application::SetEventSupplier(&supp); My_ListBox listbox(NULL, PG_Rect(10,10,300,300)); PG_ListBoxItem* item; int i; for(i=0; i<10; i++) { item = new PG_ListBoxItem(&listbox, 25, ""); item->SetTextFormat("Item %i", i+1); } listbox.SelectFirstItem(); listbox.Show(); app.Run(); return 0; }