XO ImageQuiz/Plugins

From OLPC
< XO ImageQuiz
Revision as of 18:42, 27 November 2007 by Crazy-chris (talk | contribs) (How to write a ImageQuiz-Plugin)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This page is all about how to write Plugins for the XO ImageQuiz. You can get the latest source code here:

git clone git://dev.laptop.org/projects/xo-quiz

Services

Overview

Here's an overview of all services:

/*** Styles and Layout: Use with Layout. ***\

- Have a look at layout.py. Very self-explanatory. Any wishes?
/*** DB Services: Use with db. ***\

- query(q) ................................................... Returns result of db-query in a list
- commit(q) .................................................. Executes query and following commit()
/*** Frontend Services: Use with frontend. ***\

/* Menu */

- add_menu_dir (path, caption, part = 3) ..................... Add menu directory
- add_menu_item(path, caption, onclick, part = 3) ............ Add menu item
- change_dir(dir) ............................................ Change menu directory and update menu

/* Interaction */

- add_event_hook(event, onclick) ............................. Hook a Plugin's function to an event (here def onclick())
- add_react(x, y, width, height, onclick, display = False) ... Create a React and hook in Plugin's function. Returns react's id
- del_react(id) .............................................. Remove React

/* Question Frame */

- question_display() ......................................... Display last Question
- question_frame_clear() ..................................... Clear Question Frame
- question_frame_show_image(imgfn, x, y) ..................... Show Image in Question-Frame
- question_frame_show_surface(surface, x, y).................. Show Surface in Question-Frame
- question_frame_show_text(text, x, y) ....................... Show Text in Question-Frame

/* PyGame & Other Tools */

- image_load(img_fn) ......................................... Load and convert() image. Return image, image_rect
- sound_load(snd_fn) ......................................... Loads a sound, return sound object
- point_inside_polygon(x, y, poly) ........................... True if x and y inside the polygon

Events

A Plugin can attach functions to some events. Currently supported events (list is growing):

* onclick

How to use services

Services can be accessed via the object __SERVICES__

print __SERVICES__.db.query('SELECT * FROM xoquiz WHERE 1')

Reacts - Reacting Rectangles

Rect.jpg
Reacts are reacting, invisible or visible, rectangular areas on the screen. Created by the Plugins, they start a given function if clicked on. A found react (currently!) stops searching for other clickable objects below. A react can be build with this command:

react_id = __SERVICES__.frontend.add_react(x, y, width, height, start_function, display)
  • start_function: a function of your plugin
  • display: default is False. If you pass True, a visible rectangle will be created

Reacts can be removed like this:

__SERVICES__.frontend.del_react(react_id) 

Put together it looks like that:

def test():
	pass

react_id = __SERVICES__.frontend.add_react(100, 100, 400, 200, test)
// do stuff
__SERVICES__.frontend.del_react(react_id)

Examples

git clone git://dev.laptop.org/projects/xo-quiz

Then change to the directory xo-quiz/ImageQuiz.activity/imagequiz.

  • Plugins are .py files saved in the directory 'plugins/'
  • A filename beginning with '_' prevents loading.

Minimum

This is the bare minimum of code, a plugin script requires:

__PLUGIN_NAME__ = 
def load():
	pass

Hello World

  • Creates a menu-item linked to the local function click1()
  • This button clears the Question Frame and prints 'Hello World' with an Image below
__PLUGIN_NAME__ = 'Demo Plugin'
def click1():
	__SERVICES__.frontend.question_frame_clear()
	__SERVICES__.frontend.question_frame_show_text('Hello World!', 0, 0)
	__SERVICES__.frontend.question_frame_show_image('../images/europe.gif', 0, 50)

def load():
 	__SERVICES__.frontend.add_menu_dir('/tools', 'Tools')
	__SERVICES__.frontend.add_menu_item('/tools', 'Hello', click1)

Hook to event 'onclick'

  • Extends the Hello-World example, that it reacts on a mouse click
__PLUGIN_NAME__ = 'Demo Plugin'
def event1(var):
 	x, y = var
	print 'click at ', x, '-', y

def click1():
	__SERVICES__.frontend.question_frame_clear()
	__SERVICES__.frontend.question_frame_show_text('Hello World!', 0, 0)
	__SERVICES__.frontend.question_frame_show_image('../images/europe.gif', 0, 50)

	__SERVICES__.frontend.add_event_hook('onclick', event1)

def load():
 	__SERVICES__.frontend.add_menu_dir('/tools', 'Tools')
	__SERVICES__.frontend.add_menu_item('/tools', 'Hello', click1)

Create a React and Toggle Fullscreen

  • Extend the Hello-World example, that uses a React, and toggles fullscreen
import pygame
__PLUGIN_NAME__ = 'Demo Plugin'

def clickReact(): print "Clicked on my first React"
def clickReact2(): print "Clicked on my second React"

def clickToggleFS():
	pygame.display.toggle_fullscreen()

def click1():
	__SERVICES__.frontend.question_frame_clear()
	__SERVICES__.frontend.question_frame_show_text('Hello World!', 0, 0)
	__SERVICES__.frontend.question_frame_show_image('../images/europe.gif', 0, 50)

def load():
 	__SERVICES__.frontend.add_menu_dir('/tools', 'Tools')
	__SERVICES__.frontend.add_menu_item('/tools', 'Hello', click1)
	__SERVICES__.frontend.add_menu_item('/tools', 'Toggle Fullscreen', clickToggleFS)

	__SERVICES__.frontend.add_react(0, 0, 100, 100, clickReact)
	__SERVICES__.frontend.add_react(100, 100, 200, 200, clickReact2, True)

The function takes this parameters:

  • add_react (x, y, width, height, function, display=False)

The parameter display is set to False by default, but if you explicitly say 'True', it will draw the rectangle on the screen. Mainly for development.