Added "Brush Factory", allowing you to run a lua script to generate (or alter, untested yet) a brush.

Added a simple (and ugly) sample script.
This needs updates to the makefile for platforms other than linux.
TODO: add a listbox to select the script you want to run. Do we have a simple API to handle filelists yet ?


git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1094 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Adrien Destugues 2009-10-29 15:22:45 +00:00
parent 4d20be0053
commit 1934425972
12 changed files with 208 additions and 35 deletions

View File

@ -227,8 +227,8 @@ else
# Compiles a regular linux exectutable for the native platform
BIN = grafx2
COPT = -W -Wall -Wdeclaration-after-statement -std=c99 -c -g `sdl-config --cflags` $(TTFCOPT)
LOPT = `sdl-config --libs` -lSDL_image $(TTFLOPT) -lpng
COPT = -W -Wall -Wdeclaration-after-statement -std=c99 -c -g `sdl-config --cflags` $(TTFCOPT) $(LUACOPT)
LOPT = `sdl-config --libs` -lSDL_image $(TTFLOPT) -lpng $(LUALOPT)
# Use gcc for compiling. Use ncc to build a callgraph and analyze the code.
CC = gcc
#CC = nccgen -ncgcc -ncld -ncfabs
@ -253,6 +253,17 @@ else
TTFLABEL =
endif
#Lua scripting is optional too
ifeq ($(NOLUA),1)
LUACOPT =
LUALOPT =
LUALABEL = -nolua
else
LUACOPT = -D__ENABLE_LUA__
LUALOPT = -llua5.1
LUALABEL =
endif
#To disable Joystick emulation of cursor, make NOJOY=1 (for input.o)
#This can be necessary to test keyboard cursor code, because an existing
#joystick will keep reporting a contradicting position.
@ -267,7 +278,7 @@ endif
.PHONY : all debug release clean depend zip version force install uninstall
# This is the list of the objects we want to build. Dependancies are built by "make depend" automatically.
OBJ = $(OBJDIR)/main.o $(OBJDIR)/init.o $(OBJDIR)/graph.o $(OBJDIR)/sdlscreen.o $(OBJDIR)/misc.o $(OBJDIR)/special.o $(OBJDIR)/buttons.o $(OBJDIR)/palette.o $(OBJDIR)/help.o $(OBJDIR)/operatio.o $(OBJDIR)/pages.o $(OBJDIR)/loadsave.o $(OBJDIR)/readline.o $(OBJDIR)/engine.o $(OBJDIR)/filesel.o $(OBJDIR)/op_c.o $(OBJDIR)/readini.o $(OBJDIR)/saveini.o $(OBJDIR)/shade.o $(OBJDIR)/keyboard.o $(OBJDIR)/io.o $(OBJDIR)/version.o $(OBJDIR)/text.o $(OBJDIR)/SFont.o $(OBJDIR)/setup.o $(OBJDIR)/pxsimple.o $(OBJDIR)/pxtall.o $(OBJDIR)/pxwide.o $(OBJDIR)/pxdouble.o $(OBJDIR)/pxtriple.o $(OBJDIR)/pxtall2.o $(OBJDIR)/pxwide2.o $(OBJDIR)/pxquad.o $(OBJDIR)/windows.o $(OBJDIR)/brush.o $(OBJDIR)/realpath.o $(OBJDIR)/mountlist.o $(OBJDIR)/input.o $(OBJDIR)/hotkeys.o $(OBJDIR)/transform.o $(OBJDIR)/pversion.o $(PLATFORMOBJ)
OBJ = $(OBJDIR)/main.o $(OBJDIR)/init.o $(OBJDIR)/graph.o $(OBJDIR)/sdlscreen.o $(OBJDIR)/misc.o $(OBJDIR)/special.o $(OBJDIR)/buttons.o $(OBJDIR)/palette.o $(OBJDIR)/help.o $(OBJDIR)/operatio.o $(OBJDIR)/pages.o $(OBJDIR)/loadsave.o $(OBJDIR)/readline.o $(OBJDIR)/engine.o $(OBJDIR)/filesel.o $(OBJDIR)/op_c.o $(OBJDIR)/readini.o $(OBJDIR)/saveini.o $(OBJDIR)/shade.o $(OBJDIR)/keyboard.o $(OBJDIR)/io.o $(OBJDIR)/version.o $(OBJDIR)/text.o $(OBJDIR)/SFont.o $(OBJDIR)/setup.o $(OBJDIR)/pxsimple.o $(OBJDIR)/pxtall.o $(OBJDIR)/pxwide.o $(OBJDIR)/pxdouble.o $(OBJDIR)/pxtriple.o $(OBJDIR)/pxtall2.o $(OBJDIR)/pxwide2.o $(OBJDIR)/pxquad.o $(OBJDIR)/windows.o $(OBJDIR)/brush.o $(OBJDIR)/realpath.o $(OBJDIR)/mountlist.o $(OBJDIR)/input.o $(OBJDIR)/hotkeys.o $(OBJDIR)/transform.o $(OBJDIR)/pversion.o $(OBJDIR)/factory.o $(PLATFORMOBJ)
SKIN_FILES = skins/skin_classic.png skins/skin_modern.png skins/font_Classic.png skins/font_Fun.png

View File

@ -25,6 +25,8 @@
#ifndef __BRUSH_H_
#define __BRUSH_H_
#include "struct.h"
/*!
Gets the brush from the picture.
@param start_x left edge coordinate in the picture

View File

@ -17,10 +17,10 @@
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
*/
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///@file buttons.h
/// Almost all the editor actions that are called by the menu are here.
//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
#ifndef __BOUTONS_H_
#define __BOUTONS_H_
@ -28,7 +28,8 @@
#include "struct.h"
/*!
Displays an error message when there is no more memory for the requested operation.
Displays an error message when there is no more memory for the requested
operation.
*/
void Message_out_of_memory(void);
@ -39,7 +40,8 @@ void Button_Message_initial(void);
/*!
Changes brush shape.
This function saves the current brush shape and swith to the default one (single pixel brush) for the filler and the color picker.
This function saves the current brush shape and swith to the default one
(single pixel brush) for the filler and the color picker.
These functions don't need (and will not work with) a custom brush.
*/
void Change_paintbrush_shape(byte shape);

View File

@ -79,7 +79,11 @@ char * Menu_tooltip[NB_BUTTONS]=
"Grad. spheres / ellipses",
"Brush grab. / Restore ",
"Lasso / Restore brush ",
#ifdef __ENABLE_LUA__
"Brush effects / factory ",
#else
"Brush effects ",
#endif
"Drawing modes (effects) ",
"Text ",
"Magnify mode / Menu ",

View File

@ -86,7 +86,7 @@ void Window_dropdown_clear_items(T_Dropdown_button * dropdown);
T_List_button * Window_set_list_button(T_Special_button * entry_button,
T_Scroller_button * scroller, Func_draw_list_item draw_list_item);
void Window_redraw_list(T_List_button * list);
byte Window_click_in_rectangle(short start_x,short start_y,short end_x,
byte Window_click_in_rectangle(short start_x, short start_y, short end_x,
short end_y);
short Wait_click_in_palette(T_Palette_button * button);
void Get_color_behind_window(byte * color, byte * click);

148
factory.c Normal file
View File

@ -0,0 +1,148 @@
/* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2009 Adrien Destugues
Grafx2 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; version 2
of the License.
Grafx2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
*/
/*! \file factory.c
* \brief Brush factory - generates brush from lua scripts
*
* The brush factory allows you to generate brushes with Lua code.
*/
#include "brush.h"
#include "buttons.h"
#include "engine.h"
#include "global.h"
#include "misc.h"
#include "readline.h"
#include "sdlscreen.h"
#include "windows.h"
#ifdef __ENABLE_LUA__
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
// Wrapper functions to call C from Lua
int L_PutPixel(lua_State* L)
{
Pixel_in_brush(lua_tonumber(L, 1), lua_tonumber(L, 2), lua_tonumber(L, 3));
return 0; // no values returned for lua
}
int L_GetPixel(lua_State* L)
{
uint8_t c = Read_pixel_from_brush(lua_tonumber(L, 1), lua_tonumber(L, 2));
lua_pushinteger(L, c);
return 1;
}
void Button_Brush_Factory(void)
{
short clicked_button;
word height;
word width;
char str[5];
T_Special_button* width_button;
T_Special_button* height_button;
Open_window(154, 162, "Brush Factory");
Window_set_normal_button(77, 141, 67, 14, "Cancel", 0, 1, KEY_ESC); // 1
Window_set_normal_button(10, 141, 67, 14, "Run", 0, 1, 0); // 2
Print_in_window(10, 17, "Width:", MC_Black, MC_Light);
width_button = Window_set_input_button(64, 15, 4); // 3
Print_in_window(10, 30, "Height:", MC_Black, MC_Light);
height_button = Window_set_input_button(64, 28, 4); // 4
width = Paintbrush_width;
Num2str(width, str, 4);
Window_input_content(width_button, str);
height = Paintbrush_height;
Num2str(height, str, 4);
Window_input_content(height_button, str);
Update_window_area(0, 0, Window_width, Window_height);
Display_cursor();
do {
clicked_button = Window_clicked_button();
switch (clicked_button)
{
case 3 : // Largeur
Num2str(width, str, 4);
Readline(65, 16, str, 4, 1);
width = atoi(str);
// On corrige les dimensions
if (width == 0)
{
width = 1;
Num2str(width, str, 4);
Window_input_content(width_button, str);
}
Display_cursor();
break;
case 4 : // Height
Num2str(height, str, 4);
Readline(65, 29, str, 4, 1);
height = atoi(str);
// On corrige les dimensions
if (height == 0)
{
height = 1;
Num2str(height, str, 4);
Window_input_content(height_button, str);
}
Display_cursor();
break;
default:
break;
}
} while (clicked_button <= 0 || clicked_button >= 3);
if (clicked_button == 2) // Run the script
{
lua_State* L = lua_open();
Realloc_brush(width, height);
lua_register(L,"putpixel",L_PutPixel);
lua_register(L,"getpixel",L_GetPixel);
if (luaL_loadfile(L,"./test.lua") != 0)
Verbose_error_message(lua_tostring(L, 1));
lua_pushinteger(L, width);
lua_pushinteger(L, height);
if (lua_pcall(L, 2, 0, 0) != 0)
Verbose_error_message(lua_tostring(L, 1));
lua_close(L);
Change_paintbrush_shape(PAINTBRUSH_SHAPE_COLOR_BRUSH);
}
Close_window();
Display_cursor();
}
#endif

1
factory.h Normal file
View File

@ -0,0 +1 @@
void Button_Brush_Factory(void);

41
init.c
View File

@ -48,26 +48,27 @@
#include <signal.h>
#endif
#include "buttons.h"
#include "const.h"
#include "struct.h"
#include "errors.h"
#include "global.h"
#include "graph.h"
#include "buttons.h"
#include "palette.h"
#include "help.h"
#include "operatio.h"
#include "misc.h"
#include "errors.h"
#include "keyboard.h"
#include "io.h"
#include "hotkeys.h"
#include "setup.h"
#include "windows.h"
#include "sdlscreen.h"
#include "mountlist.h" // read_file_system_list
#include "loadsave.h" // Image_emergency_backup
#include "init.h"
#include "io.h"
#include "factory.h"
#include "help.h"
#include "hotkeys.h"
#include "keyboard.h"
#include "loadsave.h" // Image_emergency_backup
#include "misc.h"
#include "mountlist.h" // read_file_system_list
#include "operatio.h"
#include "palette.h"
#include "sdlscreen.h"
#include "setup.h"
#include "struct.h"
#include "transform.h"
#include "windows.h"
char Gui_loading_error_message[512];
@ -1058,10 +1059,14 @@ void Init_buttons(void)
FAMILY_INTERRUPTION);
Init_button(BUTTON_BRUSH_EFFECTS,
106,18,
16,16,
106, 18,
16, 16,
BUTTON_SHAPE_RECTANGLE,
Button_Brush_FX,Button_Brush_FX,
#ifdef __ENABLE_LUA__
Button_Brush_FX, Button_Brush_Factory,
#else
Button_Brush_FX, Button_Brush_FX,
#endif
Do_nothing,
FAMILY_INSTANT);

2
main.c
View File

@ -1,4 +1,4 @@
/* Grafx1 - The Ultimate 256-color bitmap paint program
/* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2008 Peter Gordon
Copyright 2008 Franck Charlet

6
misc.c
View File

@ -206,12 +206,12 @@ void Init_chrono(dword delay)
return;
}
void Pixel_in_brush (word x,word y,byte color)
void Pixel_in_brush (word x, word y, byte color)
{
*(Brush+y*Brush_width+x)=color;
*(Brush + y * Brush_width + x)=color;
}
byte Read_pixel_from_brush (word x,word y)
byte Read_pixel_from_brush (word x, word y)
{
return *(Brush + y * Brush_width + x);
}

View File

@ -908,7 +908,7 @@ void Warning_message(char * message)
}
/// Window that shows a big message, and waits for a click on OK
void Verbose_error_message(char * message)
void Verbose_error_message(const char * message)
{
short clicked_button;
int line;

View File

@ -70,7 +70,7 @@ void Print_counter(short x,short y,const char * str,byte text_color,byte backgro
byte Confirmation_box(char * message);
void Warning_message(char * message);
void Verbose_error_message(char * message);
void Verbose_error_message(const char * message);
int Requester_window(char* message, int initial_value);
void Display_image_limits(void);