Lua: port viewports + add test_viewports.lua

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21076 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2009-05-25 14:21:17 +00:00
parent 88cf5b307a
commit 06ba3cceec
3 changed files with 189 additions and 2 deletions

View file

@ -181,6 +181,73 @@ static inline void rli_init(lua_State *L)
#define RB_WRAP(M) static int rock_##M(lua_State *L)
/* Helper function for opt_viewport */
static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val)
{
lua_pushstring(L, key); /* Push the key on the stack */
lua_gettable(L, tablepos); /* Find table[key] (pops key off the stack) */
if(!lua_isnoneornil(L, -1))
{
if(unsigned_val)
*(unsigned*)res = luaL_checkint(L, -1);
else
*(int*)res = luaL_checkint(L, -1);
}
lua_pop(L, 1); /* Pop the value off the stack */
}
static struct viewport* opt_viewport(lua_State *L, int narg, struct viewport* alt)
{
if(lua_isnoneornil(L, narg))
return alt;
int tablepos = lua_gettop(L);
lua_pushliteral(L, "vp"); /* push 'vp' on the stack */
struct viewport *vp = (struct viewport*)lua_newuserdata(L, sizeof(struct viewport)); /* allocate memory and push it as udata on the stack */
memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */
lua_settable(L, tablepos); /* table['vp'] = vp (pops key & value off the stack) */
luaL_checktype(L, narg, LUA_TTABLE);
check_tablevalue(L, "x", tablepos, &vp->x, false);
check_tablevalue(L, "y", tablepos, &vp->y, false);
check_tablevalue(L, "width", tablepos, &vp->width, false);
check_tablevalue(L, "height", tablepos, &vp->height, false);
#ifdef HAVE_LCD_BITMAP
check_tablevalue(L, "font", tablepos, &vp->font, false);
check_tablevalue(L, "drawmode", tablepos, &vp->drawmode, false);
#endif
#if LCD_DEPTH > 1
check_tablevalue(L, "fg_pattern", tablepos, &vp->fg_pattern, true);
check_tablevalue(L, "bg_pattern", tablepos, &vp->bg_pattern, true);
#ifdef HAVE_LCD_COLOR
check_tablevalue(L, "lss_pattern", tablepos, &vp->lss_pattern, true);
check_tablevalue(L, "lse_pattern", tablepos, &vp->lse_pattern, true);
check_tablevalue(L, "lst_pattern", tablepos, &vp->lse_pattern, true);
#endif
#endif
return vp;
}
RB_WRAP(set_viewport)
{
struct viewport *vp = opt_viewport(L, 1, NULL);
int screen = luaL_optint(L, 2, SCREEN_MAIN);
rb->screens[screen]->set_viewport(vp);
return 0;
}
RB_WRAP(clear_viewport)
{
int screen = luaL_optint(L, 2, SCREEN_MAIN);
rb->screens[screen]->clear_viewport();
return 0;
}
RB_WRAP(splash)
{
int ticks = luaL_checkint(L, 1);
@ -837,6 +904,8 @@ static const luaL_Reg rocklib[] =
R(font_getstringsize),
R(read_bmp_file),
R(set_viewport),
R(clear_viewport),
{"new_image", rli_new},
@ -867,8 +936,10 @@ LUALIB_API int luaopen_rock(lua_State *L)
RB_CONSTANT(SEEK_CUR);
RB_CONSTANT(SEEK_END);
RB_CONSTANT(FONT_SYSFIXED);
RB_CONSTANT(FONT_UI);
rli_init(L);
return 1;
}

View file

@ -5,7 +5,7 @@
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: helloworld.c 12807 2007-03-16 21:56:08Z amiconn $
* $Id$
*
* Copyright (C) 2007 Dave Chapman
*

View file

@ -0,0 +1,116 @@
--[[
__________ __ ___.
Open \______ \ ____ ____ | | _\_ |__ _______ ___
Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
\/ \/ \/ \/ \/
$Id$
Port of test_viewports.c to Lua
Copyright (C) 2009 by Maurus Cuelenaere
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
KIND, either express or implied.
]]--
-- TODO: outsource this
rb.DRMODE_SOLID = 3
rb.LCD_BLACK = rb.lcd_rgbpack(0, 0, 0)
rb.LCD_WHITE = rb.lcd_rgbpack(255, 255, 255)
rb.LCD_DEFAULT_FG = rb.LCD_WHITE
rb.LCD_DEFAULT_BG = rb.LCD_BLACK
BGCOLOR_1 = rb.lcd_rgbpack(255,255,0)
BGCOLOR_2 = rb.lcd_rgbpack(0,255,0)
FGCOLOR_1 = rb.lcd_rgbpack(0,0,255)
local vp0 =
{
x = 0,
y = 0,
width = rb.LCD_WIDTH,
height = 20,
font = rb.FONT_UI,
drawmode = rb.DRMODE_SOLID,
fg_pattern = rb.LCD_DEFAULT_FG,
bg_pattern = BGCOLOR_1
}
local vp1 =
{
x = rb.LCD_WIDTH / 10,
y = 20,
width = rb.LCD_WIDTH / 3,
height = rb.LCD_HEIGHT / 2,
font = rb.FONT_SYSFIXED,
drawmode = rb.DRMODE_SOLID,
fg_pattern = rb.LCD_DEFAULT_FG,
bg_pattern = rb.LCD_DEFAULT_BG
};
local vp2 =
{
x = rb.LCD_WIDTH / 2,
y = 40,
width = rb.LCD_WIDTH / 3,
height = (rb.LCD_HEIGHT / 2),
font = rb.FONT_UI,
drawmode = rb.DRMODE_SOLID,
fg_pattern = FGCOLOR_1,
bg_pattern = BGCOLOR_2
};
local vp3 =
{
x = rb.LCD_WIDTH / 4,
y = (5 * rb.LCD_HEIGHT) / 8,
width = rb.LCD_WIDTH / 2,
height = (rb.LCD_HEIGHT / 4),
font = rb.FONT_SYSFIXED,
drawmode = rb.DRMODE_SOLID,
fg_pattern = rb.LCD_BLACK,
bg_pattern = rb.LCD_WHITE
};
rb.set_viewport(vp0)
rb.clear_viewport()
rb.lcd_puts_scroll(0,0,"Viewport testing plugin - this is a scrolling title")
rb.set_viewport(vp1);
rb.clear_viewport();
for i = 0, 3 do
rb.lcd_puts_scroll(0,i,string.format("Left text, scrolling_line %d",i));
end
rb.set_viewport(vp2);
rb.clear_viewport();
for i = 0, 3 do
rb.lcd_puts_scroll(1,i,string.format("Right text, scrolling line %d",i));
end
local y = -10
for i = -10, vp2.width + 10, 5 do
rb.lcd_drawline(i, y, i, vp2.height - y);
end
rb.set_viewport(vp3);
rb.clear_viewport();
for i = 1, 2 do
rb.lcd_puts_scroll(2,i,string.format("Bottom text, a scrolling line %d",i));
end
rb.lcd_puts_scroll(4,3,"Short line")
rb.lcd_update()
rb.button_get(true)
rb.set_viewport()