From: <at...@us...> - 2007-10-30 00:26:23
|
Revision: 521 http://cadcdev.svn.sourceforge.net/cadcdev/?rev=521&view=rev Author: atani Date: 2007-10-29 17:26:20 -0700 (Mon, 29 Oct 2007) Log Message: ----------- * added LightBarMenu drawable * added a couple examples using GenericMenu and the LightBarMenu Modified Paths: -------------- tiki/dc/src/platgl.cpp tiki/examples/Makefile tiki/include/Tiki/gl.h tiki/nds/src/platgl.cpp tiki/osx/src/platgl.cpp tiki/sdl/src/platgl.cpp tiki/win32/src/platgl.cpp Added Paths: ----------- tiki/examples/menu/ tiki/examples/menu/Makefile tiki/examples/menu/basic/ tiki/examples/menu/basic/Makefile tiki/examples/menu/basic/resources/ tiki/examples/menu/basic/resources/ca-logo.png tiki/examples/menu/basic/src/ tiki/examples/menu/basic/src/MenuBasic.cpp tiki/examples/menu/basic/src/main.cpp tiki/examples/menu/popup/ tiki/examples/menu/popup/Makefile tiki/examples/menu/popup/resources/ tiki/examples/menu/popup/resources/ca-logo.png tiki/examples/menu/popup/resources/typewriter.txf tiki/examples/menu/popup/src/ tiki/examples/menu/popup/src/MenuPopup.cpp tiki/examples/menu/popup/src/main.cpp tiki/include/Tiki/drawables/lightbarmenu.h tiki/src/gl/drawables/lightbarmenu.cpp Modified: tiki/dc/src/platgl.cpp =================================================================== --- tiki/dc/src/platgl.cpp 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/dc/src/platgl.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -31,9 +31,13 @@ return Vector( 640.0f, 480.0f, 0.0f ); } +Vector getScreenCenter() { + return Vector( 320.0f, 240.0f, 0.0f ); } + } } +} extern "C" { Modified: tiki/examples/Makefile =================================================================== --- tiki/examples/Makefile 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/examples/Makefile 2007-10-30 00:26:20 UTC (rev 521) @@ -1,5 +1,5 @@ -SUBDIRS = events console net nehe +SUBDIRS = events menu console net nehe TIKI_DIR ?= $(CURDIR)/../ include $(TIKI_DIR)$(TIKI_PLAT)/Makefile.rules Added: tiki/examples/menu/Makefile =================================================================== --- tiki/examples/menu/Makefile (rev 0) +++ tiki/examples/menu/Makefile 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,8 @@ + +SUBDIRS = basic popup + +TIKI_DIR ?= $(CURDIR)/../../ +include $(TIKI_DIR)$(TIKI_PLAT)/Makefile.rules + +all: subdirs +clean: clean_subdirs Added: tiki/examples/menu/basic/Makefile =================================================================== --- tiki/examples/menu/basic/Makefile (rev 0) +++ tiki/examples/menu/basic/Makefile 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,26 @@ + +CFLAGS=-I$(TIKI_DIR)$(TIKI_PLAT)/include -I$(TIKI_DIR)include +OBJS = $(patsubst %.cpp,%.o,$(wildcard src/*.cpp)) + +ifeq ($(TIKI_PLAT),nds) +NDS_CART_CODE ?= MENU +NDS_CART_ID ?= TK +NDS_CART_NAME ?= MenuBasic +NDS_CART_VERSION ?= 1 +endif + +all: menu_popup +menu_popup: $(OBJS) + $(build_romdisk) + $(CXX) $(LDFLAGS) -L$(TIKI_DIR)$(TIKI_PLAT) -L$(TIKI_DIR)$(TIKI_PLAT)/lib $(OBJS) $(TIKI_BASE_LIBS) -o menu_basic$(PLATFORM_BINARY_EXT) $(ROMDISK_OBJ) + $(post_build) + +clean: + -rm -f $(OBJS) menu_basic$(PLATFORM_BINARY_EXT) $(ROMDISK_OBJ) +ifeq ($(TIKI_PLAT),nds) + -rm -f menu_basic.nds menu_basic.ds.gba +endif + +TIKI_DIR ?= $(CURDIR)/../../../ +DEPSDIR=$(CURDIR) +include $(TIKI_DIR)$(TIKI_PLAT)/Makefile.rules Property changes on: tiki/examples/menu/basic/Makefile ___________________________________________________________________ Name: svn:executable + * Added: tiki/examples/menu/basic/resources/ca-logo.png =================================================================== (Binary files differ) Property changes on: tiki/examples/menu/basic/resources/ca-logo.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: tiki/examples/menu/basic/src/MenuBasic.cpp =================================================================== --- tiki/examples/menu/basic/src/MenuBasic.cpp (rev 0) +++ tiki/examples/menu/basic/src/MenuBasic.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,27 @@ +/* +* MenuBasic.cpp +* Basic menu example +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <pch.h> + +#if TIKI_PLAT == TIKI_WIN32 +#include <windows.h> + +static char szAppName[] = "MenuBasic"; +int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) +#else +extern "C" int tiki_main( int argc, char *argv[] ); +int main( int argc, char *argv[] ) +#endif +{ +#if TIKI_PLAT != TIKI_WIN32 + return tiki_main( argc, argv ); +#else + return Tiki::DoMain( szAppName, hInst, hPrevInstance, lpCmdLine, nCmdShow ); +#endif +} Added: tiki/examples/menu/basic/src/main.cpp =================================================================== --- tiki/examples/menu/basic/src/main.cpp (rev 0) +++ tiki/examples/menu/basic/src/main.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,60 @@ +/* +* main.cpp +* Basic menu example +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <Tiki/gl.h> +#include <Tiki/hid.h> +#include <Tiki/genmenu.h> +#include <Tiki/texture.h> +#include <Tiki/drawables/banner.h> + +using namespace Tiki; +using namespace Tiki::GL; + +class BasicMenuExample : public GenericMenu { + public: + BasicMenuExample() { +#if TIKI_PLAT == TIKI_DC + m_image = new Texture("/rd/ca-logo.png", true); +#else + m_image = new Texture("ca-logo.png", true); +#endif + Vector screenSize = Frame::getScreenExtents(); + m_scene->setTranslate(Frame::getScreenCenter() + Vector(0.0f, 0.0f, 10.0f)); + + m_background = new Banner(Drawable::Opaque, m_image); + m_background->setSize(screenSize.x, screenSize.y); + m_background->setTranslate(Vector(0.0f, 0.0f, 1.0f)); + m_scene->subAdd(m_background); + } + protected: + virtual void inputEvent(const Event & evt) + { + if ( evt.type == Hid::Event::EvtQuit ) { + startExit(); + } + else if (evt.type == Hid::Event::EvtKeypress && evt.key == Hid::Event::KeyEsc) { + startExit(); + } + } + + private: + RefPtr<Banner> m_background; + RefPtr<Texture> m_image; +}; + +extern "C" int tiki_main(int argc, char *argv[]) +{ + Tiki::init(argc, argv); + + BasicMenuExample example; + example.doMenu(); + + Tiki::shutdown(); + return 0; +} Added: tiki/examples/menu/popup/Makefile =================================================================== --- tiki/examples/menu/popup/Makefile (rev 0) +++ tiki/examples/menu/popup/Makefile 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,26 @@ + +CFLAGS=-I$(TIKI_DIR)$(TIKI_PLAT)/include -I$(TIKI_DIR)include +OBJS = $(patsubst %.cpp,%.o,$(wildcard src/*.cpp)) + +ifeq ($(TIKI_PLAT),nds) +NDS_CART_CODE ?= MENU +NDS_CART_ID ?= TK +NDS_CART_NAME ?= MenuPopup +NDS_CART_VERSION ?= 1 +endif + +all: menu_popup +menu_popup: $(OBJS) + $(build_romdisk) + $(CXX) $(LDFLAGS) -L$(TIKI_DIR)$(TIKI_PLAT) -L$(TIKI_DIR)$(TIKI_PLAT)/lib $(OBJS) $(TIKI_BASE_LIBS) -o menu_popup$(PLATFORM_BINARY_EXT) $(ROMDISK_OBJ) + $(post_build) + +clean: + -rm -f $(OBJS) menu_popup$(PLATFORM_BINARY_EXT) $(ROMDISK_OBJ) +ifeq ($(TIKI_PLAT),nds) + -rm -f menu_popup.nds menu_popup.ds.gba +endif + +TIKI_DIR ?= $(CURDIR)/../../../ +DEPSDIR=$(CURDIR) +include $(TIKI_DIR)$(TIKI_PLAT)/Makefile.rules Property changes on: tiki/examples/menu/popup/Makefile ___________________________________________________________________ Name: svn:executable + * Added: tiki/examples/menu/popup/resources/ca-logo.png =================================================================== (Binary files differ) Property changes on: tiki/examples/menu/popup/resources/ca-logo.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: tiki/examples/menu/popup/resources/typewriter.txf =================================================================== (Binary files differ) Property changes on: tiki/examples/menu/popup/resources/typewriter.txf ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: tiki/examples/menu/popup/src/MenuPopup.cpp =================================================================== --- tiki/examples/menu/popup/src/MenuPopup.cpp (rev 0) +++ tiki/examples/menu/popup/src/MenuPopup.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,27 @@ +/* +* MenuPopup.cpp +* Basic Popup menu +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <pch.h> + +#if TIKI_PLAT == TIKI_WIN32 +#include <windows.h> + +static char szAppName[] = "MenuPopup"; +int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) +#else +extern "C" int tiki_main( int argc, char *argv[] ); +int main( int argc, char *argv[] ) +#endif +{ +#if TIKI_PLAT != TIKI_WIN32 + return tiki_main( argc, argv ); +#else + return Tiki::DoMain( szAppName, hInst, hPrevInstance, lpCmdLine, nCmdShow ); +#endif +} Added: tiki/examples/menu/popup/src/main.cpp =================================================================== --- tiki/examples/menu/popup/src/main.cpp (rev 0) +++ tiki/examples/menu/popup/src/main.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,92 @@ +/* +* MenuPopup.cpp +* Basic Popup menu +* +* Copyright (C)2007 Atani Software +* +*/ + +#include <Tiki/tiki.h> +#include <Tiki/gl.h> +#include <Tiki/hid.h> +#include <Tiki/genmenu.h> +#include <Tiki/font.h> +#include <Tiki/texture.h> +#include <Tiki/drawables/banner.h> +#include <Tiki/drawables/lightbarmenu.h> + +using namespace Tiki; +using namespace Tiki::GL; + +class PopupMenuExample : public GenericMenu { + public: + PopupMenuExample() { +#if TIKI_PLAT == TIKI_DC + m_image = new Texture("/rd/ca-logo.png", true); + m_font = new Font("/rd/typewriter.txf"); +#else + m_image = new Texture("ca-logo.png", true); + m_font = new Font("typewriter.txf"); +#endif + Vector screenSize = Frame::getScreenExtents(); + m_scene->setTranslate(Frame::getScreenCenter() + Vector(0.0f, 0.0f, 10.0f)); + + std::list<std::string> options; + options.push_back("Option 1"); + options.push_back("Option 2"); + options.push_back("Option 3"); + options.push_back("Quit"); + m_menu = new LightBarMenu(options, m_font); + m_menu->setTranslate(Vector(0.0f, 0.0f, 2.0f)); + m_scene->subAdd(m_menu); + + m_background = new Banner(Drawable::Opaque, m_image); + m_background->setSize(screenSize.x, screenSize.y); + m_background->setTranslate(Vector(0.0f, 0.0f, 1.0f)); + m_scene->subAdd(m_background); + } + + int getResult() { + return m_menu->getResult(); + } + + protected: + virtual void inputEvent(const Event & evt) + { + // the lightbar menu drawable handles all events while it is active + if(m_menu->inputEvent(evt)) + { + startExit(); + } + } + private: + RefPtr<LightBarMenu> m_menu; + RefPtr<Banner> m_background; + RefPtr<Texture> m_image; + RefPtr<Font> m_font; +}; + +volatile bool g_quitting = false; +void tkCallback( const Hid::Event & evt, void * data ) { + if ( evt.type == Hid::Event::EvtQuit ) { + g_quitting = true; + } + else if (evt.type == Hid::Event::EvtKeypress && evt.key == Hid::Event::KeyEsc) { + g_quitting = true; + } +} + +extern "C" int tiki_main(int argc, char *argv[]) +{ + Tiki::init(argc, argv); + Hid::callbackReg( tkCallback, NULL ); + + PopupMenuExample example; + example.doMenu(); + + Debug::printf("You chose option %d\n", example.getResult()); + + Tiki::shutdown(); + + return 0; +} Added: tiki/include/Tiki/drawables/lightbarmenu.h =================================================================== --- tiki/include/Tiki/drawables/lightbarmenu.h (rev 0) +++ tiki/include/Tiki/drawables/lightbarmenu.h 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,55 @@ +/* + Tiki + + lightbarmenu.h + + Copyright (C)2007 Atani Software + */ +#ifndef __TIKI_DRW_LIGHTBAR_H +#define __TIKI_DRW_LIGHTBAR_H + +#include "Tiki/drawable.h" +#include "Tiki/drawables/label.h" +#include "Tiki/vector.h" +#include "Tiki/color.h" +#include "Tiki/hid.h" + +#include <list> + +namespace Tiki { + +namespace GL { + +class LightBarMenu : public Drawable +{ + public: + LightBarMenu(std::list<std::string> options, + Font *fh, + bool smearText = false, + bool needBox = true, + float borderSize = 3.0f, + const Color boxColor = Color(0.5f, 0.0f, 0.0f, 0.75f), + const Color borderColor = Color(0.5f, 0.75f, 0.0f, 0.0f)); + bool inputEvent(const Hid::Event &evt); + int getResult(); + + virtual void draw(ObjType t); + + private: + bool m_needBox; + Color m_boxColor; + Color m_borderColor; + float m_borderSize; + float m_width; + float m_height; + int m_cursel; + std::list< RefPtr<Label> > m_options; + std::list<Vector> m_optionsPosCache; + Color m_colorSelected; + Color m_colorDefault; +}; + +} // namespace GL +} // namespace Tiki + +#endif // __TIKI_DRW_LIGHTBAR_H Modified: tiki/include/Tiki/gl.h =================================================================== --- tiki/include/Tiki/gl.h 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/include/Tiki/gl.h 2007-10-30 00:26:20 UTC (rev 521) @@ -44,6 +44,9 @@ // defaults to window size for platform. Tiki::Math::Vector getScreenExtents(); +// Returns a suitable Vector pointing to the screen center +Tiki::Math::Vector getScreenCenter(); + } } } Modified: tiki/nds/src/platgl.cpp =================================================================== --- tiki/nds/src/platgl.cpp 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/nds/src/platgl.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -27,6 +27,10 @@ return Vector( 256.0f, 192.0f, 0.0f ); } +Vector getScreenCenter() { + return Vector( 128.0f, 96.0f, 0.0f ); +} + float getFrameRate() { uint64 cur = Tiki::Time::gettime(); Modified: tiki/osx/src/platgl.cpp =================================================================== --- tiki/osx/src/platgl.cpp 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/osx/src/platgl.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -74,6 +74,10 @@ return Vector( 640.0f, 480.0f, 0.0f ); } +Vector getScreenCenter() { + return Vector( 320.0f, 240.0f, 0.0f ); } + } } +} Modified: tiki/sdl/src/platgl.cpp =================================================================== --- tiki/sdl/src/platgl.cpp 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/sdl/src/platgl.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -48,9 +48,13 @@ return Vector( 640.0f, 480.0f, 0.0f ); } +Vector getScreenCenter() { + return Vector( 320.0f, 240.0f, 0.0f ); } + } } +} extern "C" { Added: tiki/src/gl/drawables/lightbarmenu.cpp =================================================================== --- tiki/src/gl/drawables/lightbarmenu.cpp (rev 0) +++ tiki/src/gl/drawables/lightbarmenu.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -0,0 +1,216 @@ +/* + Tiki + + lightbarmenu.cpp + + Copyright (C)2007 Atani Software + */ + +#include "pch.h" +#include "Tiki/drawables/lightbarmenu.h" + +using namespace Tiki::GL; +using namespace Tiki::Hid; + +LightBarMenu::LightBarMenu(std::list<std::string> options, + Font *fh, bool smearText, bool needBox, float borderSize, + Color boxColor, Color borderColor) +{ + m_needBox = needBox; + m_borderSize = borderSize; + m_borderColor = borderColor; + m_boxColor = boxColor; + m_colorSelected = Color(1, 1, 0, 1); + m_colorDefault = Color(1, 1, 1, 1); + m_cursel = 0; + m_height = 0.0f; + m_width = 0.0f; + + float y = 10.0f; + for(std::list<std::string>::const_iterator iter = options.begin(); iter != options.end(); iter++) + { + float h, w; + std::string str = *iter; + fh->getTextSize(str, w, h); + if(m_width < w) + { + m_width = w; + } + RefPtr<Label> tempLabel = new Label(fh, str, 24, true, true); + tempLabel->setTranslate(Vector(0.0f, y, 0.01f)); + Vector locTL = Vector(0.0f, y, 0.01f); + locTL.x -= w / 2; + locTL.y -= h / 2; + m_optionsPosCache.push_back(locTL); + Vector locBR = Vector(0.0f, y, 0.01f); + locBR.x += w / 2; + locBR.y += h / 2; + m_optionsPosCache.push_back(locBR); + m_options.push_back(tempLabel); + subAdd(tempLabel); + y += 25.0f; + } + m_height = (y - 10.0f) + (m_borderSize * 2); + m_width += (m_borderSize * 2); + + m_options.front()->setTint(m_colorSelected); +} + +bool LightBarMenu::inputEvent(const Event &evt) +{ + if(evt.dev == NULL || evt.type == Event::EvtQuit) + { + return true; + } + switch(evt.dev->getType()) + { + case Device::TypeKeyboard: + if(evt.type == Event::EvtKeyUp) + { + switch(evt.key) + { + case Event::KeyUp: + if(m_cursel > 0) + { + m_cursel--; + } + else + { + m_cursel = static_cast<int>(m_options.size() - 1); + } + break; + case Event::KeyDown: + if(static_cast<std::list< RefPtr<Label> >::size_type>(m_cursel) < + m_options.size() - 1) + { + m_cursel++; + } + else + { + m_cursel = 0; + } + break; + case 27: // ESCAPE KEY + m_cursel = -1; + case '\r': + case '\n': + return true; + } + } + break; + case Device::TypeJoystick: + switch(evt.btn) + { + case Event::BtnUp: + if(m_cursel > 0) + { + m_cursel--; + } + else + { + m_cursel = static_cast<int>(m_options.size() - 1); + } + break; + case Event::BtnDown: + if(static_cast<std::list< RefPtr<Label> >::size_type>(m_cursel) < + m_options.size() - 1) + { + m_cursel++; + } + else + { + m_cursel = 0; + } + break; + case Event::BtnB: + m_cursel = -1; + case Event::BtnA: + return true; + } + break; + case Device::TypeMouse: + { + int cnt = -1; + switch(evt.type) + { + case Event::EvtMouseMove: + for(std::list<Vector>::const_iterator iter = m_optionsPosCache.begin(); iter != m_optionsPosCache.end(); iter++) + { + cnt++; + const Vector &tl = *iter; + const Vector &br = *++iter; + + if( (evt.x >= tl.x && evt.x <= br.x) && + (evt.y >= tl.y && evt.y <= br.y)) + { + m_cursel = cnt; + } + } + break; + case Event::EvtBtnPress: + for(std::list<Vector>::const_iterator iter = m_optionsPosCache.begin(); iter != m_optionsPosCache.end(); iter++) + { + cnt++; + const Vector &tl = *iter; + const Vector &br = *++iter; + + if( (evt.x >= tl.x && evt.x <= br.x) && + (evt.y >= tl.y && evt.y <= br.y)) + { + m_cursel = cnt; + return true; + } + } + break; + } + } + break; + default: + Tiki::Debug::printf("Unknown Device: %d\n", evt.dev->getType()); + } + + int counter = 0; + for(std::list< RefPtr<Label> >::const_iterator iter = m_options.begin(); iter != m_options.end(); iter++) + { + Label *label = *iter; + label->setTint(m_colorDefault); + if(counter == m_cursel) + { + label->setTint(m_colorSelected); + } + counter++; + } + return false; +} + +int LightBarMenu::getResult() +{ + Tiki::Debug::printf("Result: %d\n", m_cursel); + return m_cursel; +} + +void LightBarMenu::draw(ObjType list) +{ + if(!m_needBox) + { + return; + } + else if(list==Trans) + { + const Vector & tv = getPosition(); + Texture::deselect(); + glBegin(GL_QUADS); + m_borderColor.select(); + glBegin(GL_QUADS); + glVertex3f(tv.x - m_width/2, tv.y, tv.z / 1000.0f); + glVertex3f(tv.x + m_width/2, tv.y, tv.z / 1000.0f); + glVertex3f(tv.x + m_width/2, tv.y + m_height, tv.z / 1000.0f); + glVertex3f(tv.x - m_width/2, tv.y + m_height, tv.z / 1000.0f); + m_boxColor.select(); + glVertex3f(tv.x - (m_width - m_borderSize)/2, tv.y + m_borderSize, tv.z / 1000.0f); + glVertex3f(tv.x + (m_width - m_borderSize)/2, tv.y + m_borderSize, tv.z / 1000.0f); + glVertex3f(tv.x + (m_width - m_borderSize)/2, tv.y + (m_height - m_borderSize), tv.z / 1000.0f); + glVertex3f(tv.x - (m_width - m_borderSize)/2, tv.y + (m_height - m_borderSize), tv.z / 1000.0f); + glEnd(); + } +} Modified: tiki/win32/src/platgl.cpp =================================================================== --- tiki/win32/src/platgl.cpp 2007-10-29 23:07:45 UTC (rev 520) +++ tiki/win32/src/platgl.cpp 2007-10-30 00:26:20 UTC (rev 521) @@ -324,9 +324,13 @@ return Vector( static_cast<float>(m_targetW), static_cast<float>(m_targetH), 0.0f ); } +Vector getScreenCenter() { + return Vector( static_cast<float>(m_targetW / 2), static_cast<float>(m_targetH / 2), 0.0f ); } + } } +} extern "C" void tiki_scene_begin_hook() { // Set up our GL context for painting, if needed. If this is another This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |