Subversion Repositories Kolibri OS

Rev

Rev 7151 | Rev 7154 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7089 leency 1
/*
2
 * BACKGEN - Background generator
3
 * Author: Leency
4
 * Licence: GPL v2
5
*/
6
 
7151 leency 7
/*
8
TODO/BUGS
9
Flip first pixel doesn't work well
10
Open with param
11
*/
12
 
7096 leency 13
#define MEMSIZE 4096*40
7089 leency 14
 
15
#include "../lib/gui.h"
7151 leency 16
#include "../lib/random.h"
17
#include "../lib/mem.h"
7096 leency 18
#include "../lib/obj/libimg.h"
7089 leency 19
#include "../lib/patterns/rgb.h"
7096 leency 20
#include "../lib/patterns/libimg_load_skin.h"
7089 leency 21
 
7151 leency 22
#include "colors_mas.h"
23
 
7089 leency 24
//===================================================//
25
//                                                   //
26
//                       DATA                        //
27
//                                                   //
28
//===================================================//
29
 
7152 leency 30
#define T_TITLE "Icon Editor 0.09"
7096 leency 31
 
7152 leency 32
#define TOOLBAR_H    24+8
7096 leency 33
#define PALLETE_SIZE 116
7089 leency 34
 
7151 leency 35
rect wrapper = { 10, TOOLBAR_H, NULL, NULL };
36
rect right_bar = { NULL, TOOLBAR_H, 280, NULL };
37
rect canvas = { NULL, NULL, NULL, NULL };
7089 leency 38
 
7150 leency 39
dword active_color_1 = 0x000000;
40
dword active_color_2 = 0xFFFfff;
7089 leency 41
 
42
enum {
7096 leency 43
	BTN_NEW = 40,
44
	BTN_OPEN,
45
	BTN_SAVE,
46
	BTN_MOVE_LEFT,
47
	BTN_MOVE_RIGHT,
48
	BTN_MOVE_UP,
49
	BTN_MOVE_DOWN,
50
	BTN_FLIP_HOR,
51
	BTN_FLIP_VER,
52
	BTN_ROTATE_LEFT,
53
	BTN_ROTATE_RIGHT,
54
	BTN_PICK,
7152 leency 55
	BTN_ZOOM_IN,
56
	BTN_ZOOM_OUT,
7096 leency 57
	BTN_PALETTE_COLOR_MAS = 100,
7089 leency 58
};
59
 
60
proc_info Form;
61
 
7096 leency 62
bool pick_active = false;
63
 
7152 leency 64
more_less_box zoom = { NULL, NULL, 11, 1, 40, BTN_ZOOM_IN, BTN_ZOOM_OUT, "Zoom" };
7150 leency 65
 
7152 leency 66
dword default_palette[] = {
7089 leency 67
0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,0x330033,0x330019,0x000000,
68
0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,
69
0x990000,0x994C00,0x999900,0x4C9900,0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,
70
0xCC0000,0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,0xCC0066,0x606060,
71
0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,
72
0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,
73
0xFF6666,0xFFB266,0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,0xC0C0C0,
74
0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,
75
0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF
76
};
77
 
7151 leency 78
_colors image;
7148 leency 79
 
7151 leency 80
libimg_image open_image;
81
 
7089 leency 82
//===================================================//
83
//                                                   //
84
//                       CODE                        //
85
//                                                   //
86
//===================================================//
87
 
88
void main()
89
{
90
	word btn;
91
 
7096 leency 92
	load_dll(libio,  #libio_init,  1);
93
	load_dll(libimg, #libimg_init, 1);
94
	Libimg_LoadImage(#skin, "/sys/icons16.png");
7152 leency 95
	//Libimg_ReplaceColor(skin.image, skin.w, skin.h, 0xFFfffFFF, 0xff808080);
7096 leency 96
 
7151 leency 97
	CreateCanvas();
7089 leency 98
 
7151 leency 99
	/*
100
	if (param) {
101
		Libimg_LoadImage(#open_image, #param);
102
		if (open_image.w==32) && (open_image.h==32) {
103
			memmov(#image.mas, open_image.image, 32*32*3);
104
		}
105
		else {
106
			notify("'Error: image format is unacceptable.\nOnly images created in IconEditor (BMP, 32x32x16b) can be opened!' -E");
107
		}
108
	}
109
	*/
110
 
7096 leency 111
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
112
 
7089 leency 113
	loop() switch(WaitEvent())
114
	{
7096 leency 115
		case evMouse:
116
			mouse.get();
7150 leency 117
			if (pick_active) EventPickColor(mouse.lkm, mouse.pkm);
7152 leency 118
			else if (mouse.vert) {
119
				if (mouse.vert==65535) zoom.click(BTN_ZOOM_IN);
120
				if (mouse.vert==1) zoom.click(BTN_ZOOM_OUT);
121
				DrawEditArea();
122
			}
7148 leency 123
			else {
7151 leency 124
				if (mouse.x>canvas.x) && (mouse.y>canvas.y)
7150 leency 125
				&& (mouse.y
7148 leency 126
				{
7151 leency 127
					if (mouse.lkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
128
						mouse.x-canvas.x/zoom.value, active_color_1);
129
					if (mouse.pkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
130
						mouse.x-canvas.x/zoom.value, active_color_2);
131
					DrawCanvas();
7148 leency 132
				}
133
			}
134
			break;
7096 leency 135
 
7089 leency 136
		case evButton:
7148 leency 137
			btn = GetButtonID();
7096 leency 138
			switch(btn)
139
			{
7151 leency 140
				case BTN_NEW:
141
					CreateCanvas();
142
					DrawCanvas();
143
					break;
144
				case BTN_OPEN:
145
					RunProgram("/sys/lod", sprintf(#param, "*bmp* %s",#program_path));
146
					break;
147
				case BTN_SAVE:
148
					EventSave();
149
					break;
7096 leency 150
				case BTN_MOVE_LEFT:
7151 leency 151
					image.move(MOVE_LEFT);
152
					DrawCanvas();
7096 leency 153
					break;
154
				case BTN_MOVE_RIGHT:
7151 leency 155
					image.move(MOVE_RIGHT);
156
					DrawCanvas();
7096 leency 157
					break;
7147 leency 158
				case BTN_MOVE_UP:
7151 leency 159
					image.move(MOVE_UP);
160
					DrawCanvas();
7147 leency 161
					break;
162
				case BTN_MOVE_DOWN:
7151 leency 163
					image.move(MOVE_DOWN);
164
					DrawCanvas();
7147 leency 165
					break;
7151 leency 166
				case BTN_FLIP_VER:
167
					image.move(FLIP_VER);
168
					DrawCanvas();
169
					break;
170
				case BTN_FLIP_HOR:
171
					image.move(FLIP_HOR);
172
					DrawCanvas();
173
					break;
7096 leency 174
				case BTN_PICK:
175
					EventPickActivate();
176
					break;
7152 leency 177
				case BTN_ZOOM_IN:
178
				case BTN_ZOOM_OUT:
179
					zoom.click(btn);
180
					DrawEditArea();
181
					break;
182
				case CLOSE_BTN:
183
					ExitProcess();
184
					break;
7096 leency 185
			}
186
			if (btn >= BTN_PALETTE_COLOR_MAS) && (btn < BTN_PALETTE_COLOR_MAS+PALLETE_SIZE)
7089 leency 187
			{
7152 leency 188
				if (mouse.lkm) active_color_1 = default_palette[btn-BTN_PALETTE_COLOR_MAS];
189
				if (mouse.pkm) active_color_2 = default_palette[btn-BTN_PALETTE_COLOR_MAS];
7089 leency 190
				DrawActiveColor(NULL);
191
			}
192
			break;
193
 
194
		case evKey:
195
			GetKeys();
7151 leency 196
			if (key_scancode == SCAN_CODE_ESC) pick_active=false;
7096 leency 197
			if (key_scancode == SCAN_CODE_KEY_I) EventPickActivate();
7152 leency 198
			if (key_scancode == SCAN_CODE_MINUS) {zoom.click(BTN_ZOOM_OUT); DrawEditArea();}
199
			if (key_scancode == SCAN_CODE_PLUS)  {zoom.click(BTN_ZOOM_IN);  DrawEditArea();}
7089 leency 200
			break;
201
 
202
		case evReDraw:
203
			draw_window();
204
			break;
205
	}
206
}
207
 
7096 leency 208
void DrawToolbarButton(dword _id, _x, _icon_n)
209
{
7152 leency 210
	DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff);
211
	DefineHiddenButton(_x, 4, 21, 21, _id);
212
	img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16);
7096 leency 213
}
214
 
7148 leency 215
void DrawStatusBar()
216
{
7150 leency 217
	zoom.y = wrapper.y + wrapper.h + 6;
218
	zoom.x = wrapper.x;
219
	zoom.draw();
220
 
7151 leency 221
	sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
7150 leency 222
	WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
7148 leency 223
}
224
 
7089 leency 225
void draw_window()
226
{
7096 leency 227
	#define TB_ICON_PADDING 26
228
	incn tx;
7089 leency 229
	system.color.get();
7151 leency 230
	DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
7089 leency 231
	GetProcessInfo(#Form, SelfInfo);
7152 leency 232
	if (Form.status_window>2) return;
233
	if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
234
	if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
235
 
7150 leency 236
	right_bar.x = Form.cwidth - right_bar.w;
7089 leency 237
 
7150 leency 238
	tx.n = wrapper.x - TB_ICON_PADDING;
7148 leency 239
	DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
240
	DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
7151 leency 241
	DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
7148 leency 242
	DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8),   30);
7096 leency 243
	DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
7147 leency 244
	DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
245
	DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
246
 
7148 leency 247
	DrawToolbarButton(BTN_FLIP_HOR,   tx.inc(TB_ICON_PADDING+8), 34); //not implemented
248
	DrawToolbarButton(BTN_FLIP_VER,   tx.inc(TB_ICON_PADDING),   35); //not implemented
249
	DrawToolbarButton(BTN_ROTATE_LEFT,   tx.inc(TB_ICON_PADDING), 36); //not implemented
250
	DrawToolbarButton(BTN_ROTATE_RIGHT,  tx.inc(TB_ICON_PADDING), 37); //not implemented
251
 
7147 leency 252
	DrawToolbarButton(BTN_PICK,   tx.inc(TB_ICON_PADDING+8), 38);
7096 leency 253
 
7150 leency 254
	DrawEditArea();
7096 leency 255
 
7150 leency 256
	DrawDefaultColors(right_bar.x, right_bar.y);
7151 leency 257
	DrawActiveColor(right_bar.y + 200);
7089 leency 258
 
7148 leency 259
	DrawStatusBar();
7089 leency 260
}
261
 
7151 leency 262
void DrawEditArea()
7089 leency 263
{
7150 leency 264
	dword color1=0xC0C0C0;
265
	int left_padding;
266
	int top_padding;
7089 leency 267
 
7150 leency 268
	wrapper.w = Form.cwidth - right_bar.w - 30;
269
	wrapper.h = Form.cheight - TOOLBAR_H - 35;
7148 leency 270
 
7150 leency 271
	//canvas{
7151 leency 272
	canvas.w = image.columns * zoom.value;
273
	canvas.h = image.rows * zoom.value;
274
	if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
275
		zoom.value--;
276
		DrawEditArea();
277
		return;
278
	}
279
	canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
280
	canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
7150 leency 281
	DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
7151 leency 282
	DrawCanvas();
7150 leency 283
	//}
7089 leency 284
 
7150 leency 285
	left_padding = canvas.x-wrapper.x-1;
286
	top_padding = canvas.y-wrapper.y-1;
7148 leency 287
 
7150 leency 288
	DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w+1, wrapper.h+1, system.color.work_graph);
289
 
290
	if (left_padding>0)
291
	{
7151 leency 292
		DrawBar(wrapper.x, wrapper.y, wrapper.w, top_padding, color1); //top
293
		DrawBar(wrapper.x, wrapper.y+wrapper.h-top_padding, wrapper.w, top_padding, color1); //bottom
7150 leency 294
	}
295
	if (top_padding>0)
296
	{
7151 leency 297
		DrawBar(wrapper.x, wrapper.y+top_padding, left_padding, wrapper.h-top_padding-top_padding, color1); //left
298
		DrawBar(wrapper.x+wrapper.w-left_padding, wrapper.y+top_padding, left_padding, wrapper.h-top_padding-top_padding, color1); //right
7150 leency 299
	}
7089 leency 300
}
301
 
302
void DrawActiveColor(dword iny)
303
{
304
	static dword outy;
305
	if (iny != NULL) outy = iny;
7150 leency 306
	DrawBar(right_bar.x, outy, 20, 20, active_color_1);
307
	sprintf(#param, "%A", active_color_1);
7089 leency 308
	EDI = system.color.work;
7148 leency 309
	WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7151 leency 310
 
311
	DrawBar(right_bar.x+110, outy, 20, 20, active_color_2);
312
	sprintf(#param, "%A", active_color_2);
313
	EDI = system.color.work;
314
	WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7089 leency 315
}
316
 
317
void DrawDefaultColors(dword _x, _y)
318
{
319
	int r, c, i;
7148 leency 320
	int cellw = 20;
321
 
7089 leency 322
	i = 0;
323
	for (r = 0; r < 9; r++)
324
	{
325
		for (c = 0; c < 13; c++)
326
		{
7152 leency 327
			DrawBar(c*cellw + _x, r*cellw + _y, cellw, cellw, default_palette[PALLETE_SIZE-i]);
7148 leency 328
			DefineHiddenButton(c*cellw + _x, r*cellw + _y, cellw-1, cellw-1, BTN_PALETTE_COLOR_MAS+PALLETE_SIZE-i);
7089 leency 329
			i++;
330
		}
331
	}
7148 leency 332
	DrawRectangle(_x-1, _y-1, c*cellw+1, r*cellw+1, system.color.work_light);
333
	DrawRectangle(_x-2, _y-2, c*cellw+3, r*cellw+3, system.color.work_dark);
7089 leency 334
}
335
 
336
 
337
//===================================================//
338
//                                                   //
339
//                      EVENTS                       //
340
//                                                   //
341
//===================================================//
342
 
7096 leency 343
void EventPickActivate()
344
{
345
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE);
346
	pick_active = true;
347
}
348
 
7150 leency 349
void EventPickColor(dword lkm_status, pkm_status)
7096 leency 350
{
7150 leency 351
	active_color_1 = GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
7096 leency 352
	DrawActiveColor(NULL);
353
	if (mouse.down) && (mouse.key&MOUSE_LEFT) {
354
		pick_active = false;
355
		SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
356
	}
357
}
7150 leency 358
 
7151 leency 359
dword bmp_32x32x16_header[] = FROM "bmp32x32header";
360
void EventSave()
7150 leency 361
{
7151 leency 362
	char save_buf[3126];
363
	memmov(#save_buf, #bmp_32x32x16_header, sizeof(bmp_32x32x16_header));
7152 leency 364
	memmov(#save_buf+sizeof(bmp_32x32x16_header), image.get_image(), sizeof(save_buf)-sizeof(bmp_32x32x16_header));
7151 leency 365
	if (WriteFile(sizeof(save_buf), #save_buf, "/rd/1/saved_image.bmp")==0)
366
	{
367
		notify("'File saved as /rd/1/saved_image.bmp' -O");
7150 leency 368
	}
7151 leency 369
	else {
370
		notify("'Error saving BPM file, probably not enought space on ramdisk!' -E");
371
	}
7150 leency 372
}
7151 leency 373
 
374
void CreateCanvas()
375
{
376
	int i;
377
	image.columns = 32;
378
	image.rows = 32;
379
	for (i = 0; i < image.columns*image.rows; i++) image.mas[i]=0xBFCAD2;
380
}
381
 
382
void DrawCanvas()
383
{
384
	int r, c;
385
	for (r = 0; r < image.rows; r++)
386
	{
387
		for (c = 0; c < image.columns; c++)
388
		{
389
			DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
390
				zoom.value, zoom.value, image.get_pixel(r, c));
391
		}
392
	}
393
}