Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7089 leency 1
/*
7154 leency 2
 * Icon Editor for KolibriOS
7089 leency 3
 * Author: Leency
4
 * Licence: GPL v2
5
*/
6
 
7151 leency 7
/*
8
TODO/BUGS
9
Open with param
10
*/
11
 
7096 leency 12
#define MEMSIZE 4096*40
7089 leency 13
 
14
#include "../lib/gui.h"
7151 leency 15
#include "../lib/random.h"
16
#include "../lib/mem.h"
7096 leency 17
#include "../lib/obj/libimg.h"
7089 leency 18
#include "../lib/patterns/rgb.h"
7096 leency 19
#include "../lib/patterns/libimg_load_skin.h"
7089 leency 20
 
7151 leency 21
#include "colors_mas.h"
22
 
7089 leency 23
//===================================================//
24
//                                                   //
25
//                       DATA                        //
26
//                                                   //
27
//===================================================//
28
 
7155 leency 29
#define T_TITLE "Icon Editor 0.1"
7096 leency 30
 
7152 leency 31
#define TOOLBAR_H    24+8
7096 leency 32
#define PALLETE_SIZE 116
7089 leency 33
 
7151 leency 34
rect wrapper = { 10, TOOLBAR_H, NULL, NULL };
35
rect right_bar = { NULL, TOOLBAR_H, 280, NULL };
36
rect canvas = { NULL, NULL, NULL, NULL };
7089 leency 37
 
7150 leency 38
dword active_color_1 = 0x000000;
39
dword active_color_2 = 0xFFFfff;
7089 leency 40
 
41
enum {
7096 leency 42
	BTN_NEW = 40,
43
	BTN_OPEN,
44
	BTN_SAVE,
45
	BTN_MOVE_LEFT,
46
	BTN_MOVE_RIGHT,
47
	BTN_MOVE_UP,
48
	BTN_MOVE_DOWN,
49
	BTN_FLIP_HOR,
50
	BTN_FLIP_VER,
51
	BTN_ROTATE_LEFT,
52
	BTN_ROTATE_RIGHT,
53
	BTN_PICK,
7152 leency 54
	BTN_ZOOM_IN,
55
	BTN_ZOOM_OUT,
7155 leency 56
	BTNS_PALETTE_COLOR_MAS = 100,
57
	BTNS_LAST_USED_COLORS = 400
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
};
7155 leency 77
dword last_used_colors[13*2] = {
78
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
79
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF
80
};
7089 leency 81
 
7155 leency 82
_image image;
7148 leency 83
 
7151 leency 84
libimg_image open_image;
85
 
7089 leency 86
//===================================================//
87
//                                                   //
88
//                       CODE                        //
89
//                                                   //
90
//===================================================//
91
 
92
void main()
93
{
94
	word btn;
95
 
7096 leency 96
	load_dll(libio,  #libio_init,  1);
97
	load_dll(libimg, #libimg_init, 1);
98
	Libimg_LoadImage(#skin, "/sys/icons16.png");
7152 leency 99
	//Libimg_ReplaceColor(skin.image, skin.w, skin.h, 0xFFfffFFF, 0xff808080);
7096 leency 100
 
7155 leency 101
	image.create(32, 32);
7089 leency 102
 
7151 leency 103
	/*
104
	if (param) {
105
		Libimg_LoadImage(#open_image, #param);
106
		if (open_image.w==32) && (open_image.h==32) {
107
			memmov(#image.mas, open_image.image, 32*32*3);
108
		}
109
		else {
110
			notify("'Error: image format is unacceptable.\nOnly images created in IconEditor (BMP, 32x32x16b) can be opened!' -E");
111
		}
112
	}
113
	*/
114
 
7096 leency 115
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
116
 
7089 leency 117
	loop() switch(WaitEvent())
118
	{
7096 leency 119
		case evMouse:
120
			mouse.get();
7150 leency 121
			if (pick_active) EventPickColor(mouse.lkm, mouse.pkm);
7152 leency 122
			else if (mouse.vert) {
123
				if (mouse.vert==65535) zoom.click(BTN_ZOOM_IN);
124
				if (mouse.vert==1) zoom.click(BTN_ZOOM_OUT);
125
				DrawEditArea();
126
			}
7148 leency 127
			else {
7151 leency 128
				if (mouse.x>canvas.x) && (mouse.y>canvas.y)
7150 leency 129
				&& (mouse.y
7148 leency 130
				{
7151 leency 131
					if (mouse.lkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
132
						mouse.x-canvas.x/zoom.value, active_color_1);
133
					if (mouse.pkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
134
						mouse.x-canvas.x/zoom.value, active_color_2);
135
					DrawCanvas();
7148 leency 136
				}
137
			}
138
			break;
7096 leency 139
 
7089 leency 140
		case evButton:
7148 leency 141
			btn = GetButtonID();
7096 leency 142
			switch(btn)
143
			{
7151 leency 144
				case BTN_NEW:
7155 leency 145
					image.create(32, 32);
7151 leency 146
					DrawCanvas();
147
					break;
148
				case BTN_OPEN:
149
					RunProgram("/sys/lod", sprintf(#param, "*bmp* %s",#program_path));
150
					break;
151
				case BTN_SAVE:
152
					EventSave();
153
					break;
7096 leency 154
				case BTN_MOVE_LEFT:
7151 leency 155
					image.move(MOVE_LEFT);
156
					DrawCanvas();
7096 leency 157
					break;
158
				case BTN_MOVE_RIGHT:
7151 leency 159
					image.move(MOVE_RIGHT);
160
					DrawCanvas();
7096 leency 161
					break;
7147 leency 162
				case BTN_MOVE_UP:
7151 leency 163
					image.move(MOVE_UP);
164
					DrawCanvas();
7147 leency 165
					break;
166
				case BTN_MOVE_DOWN:
7151 leency 167
					image.move(MOVE_DOWN);
168
					DrawCanvas();
7147 leency 169
					break;
7151 leency 170
				case BTN_FLIP_VER:
171
					image.move(FLIP_VER);
172
					DrawCanvas();
173
					break;
174
				case BTN_FLIP_HOR:
175
					image.move(FLIP_HOR);
176
					DrawCanvas();
177
					break;
7096 leency 178
				case BTN_PICK:
179
					EventPickActivate();
180
					break;
7152 leency 181
				case BTN_ZOOM_IN:
182
				case BTN_ZOOM_OUT:
183
					zoom.click(btn);
184
					DrawEditArea();
185
					break;
186
				case CLOSE_BTN:
187
					ExitProcess();
188
					break;
7096 leency 189
			}
7155 leency 190
			if (btn >= BTNS_PALETTE_COLOR_MAS) && (btn < BTNS_PALETTE_COLOR_MAS+PALLETE_SIZE)
7089 leency 191
			{
7155 leency 192
				if (mouse.lkm) EventSetActiveColor(1, default_palette[btn - BTNS_PALETTE_COLOR_MAS]);
193
				if (mouse.pkm) EventSetActiveColor(2, default_palette[btn - BTNS_PALETTE_COLOR_MAS]);
7089 leency 194
			}
7155 leency 195
			if (btn >= BTNS_LAST_USED_COLORS) && (btn < sizeof(last_used_colors)*sizeof(dword))
196
			{
197
				if (mouse.lkm) active_color_1 = last_used_colors[btn - BTNS_LAST_USED_COLORS];
198
				if (mouse.pkm) active_color_2 = last_used_colors[btn - BTNS_LAST_USED_COLORS];
199
				DrawActiveColor(NULL);
200
			}
7089 leency 201
			break;
202
 
203
		case evKey:
204
			GetKeys();
7151 leency 205
			if (key_scancode == SCAN_CODE_ESC) pick_active=false;
7096 leency 206
			if (key_scancode == SCAN_CODE_KEY_I) EventPickActivate();
7152 leency 207
			if (key_scancode == SCAN_CODE_MINUS) {zoom.click(BTN_ZOOM_OUT); DrawEditArea();}
208
			if (key_scancode == SCAN_CODE_PLUS)  {zoom.click(BTN_ZOOM_IN);  DrawEditArea();}
7089 leency 209
			break;
210
 
211
		case evReDraw:
212
			draw_window();
213
			break;
214
	}
215
}
216
 
7096 leency 217
void DrawToolbarButton(dword _id, _x, _icon_n)
218
{
7152 leency 219
	DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff);
220
	DefineHiddenButton(_x, 4, 21, 21, _id);
221
	img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16);
7096 leency 222
}
223
 
7148 leency 224
void DrawStatusBar()
225
{
7150 leency 226
	zoom.y = wrapper.y + wrapper.h + 6;
227
	zoom.x = wrapper.x;
228
	zoom.draw();
229
 
7151 leency 230
	sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
7150 leency 231
	WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
7148 leency 232
}
233
 
7089 leency 234
void draw_window()
235
{
7096 leency 236
	#define TB_ICON_PADDING 26
237
	incn tx;
7089 leency 238
	system.color.get();
7151 leency 239
	DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
7089 leency 240
	GetProcessInfo(#Form, SelfInfo);
7152 leency 241
	if (Form.status_window>2) return;
242
	if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
243
	if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
244
 
7150 leency 245
	right_bar.x = Form.cwidth - right_bar.w;
7089 leency 246
 
7155 leency 247
	tx.n = 10-TB_ICON_PADDING;
7148 leency 248
	DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
249
	DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
7151 leency 250
	DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
7148 leency 251
	DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8),   30);
7096 leency 252
	DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
7147 leency 253
	DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
254
	DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
255
 
7155 leency 256
	DrawToolbarButton(BTN_FLIP_HOR,   tx.inc(TB_ICON_PADDING+8), 34);
257
	DrawToolbarButton(BTN_FLIP_VER,   tx.inc(TB_ICON_PADDING),   35);
258
	// DrawToolbarButton(BTN_ROTATE_LEFT,   tx.inc(TB_ICON_PADDING), 36); //not implemented
259
	// DrawToolbarButton(BTN_ROTATE_RIGHT,  tx.inc(TB_ICON_PADDING), 37); //not implemented
7148 leency 260
 
7147 leency 261
	DrawToolbarButton(BTN_PICK,   tx.inc(TB_ICON_PADDING+8), 38);
7096 leency 262
 
7150 leency 263
	DrawEditArea();
7096 leency 264
 
7155 leency 265
	DrawActiveColor(right_bar.y);
266
	DrawColorPallets(right_bar.x, right_bar.y + 30);
7089 leency 267
 
7148 leency 268
	DrawStatusBar();
7089 leency 269
}
270
 
7151 leency 271
void DrawEditArea()
7089 leency 272
{
7150 leency 273
	dword color1=0xC0C0C0;
7155 leency 274
	int top_side;
275
	int left_side;
7089 leency 276
 
7150 leency 277
	wrapper.w = Form.cwidth - right_bar.w - 30;
278
	wrapper.h = Form.cheight - TOOLBAR_H - 35;
7148 leency 279
 
7150 leency 280
	//canvas{
7151 leency 281
	canvas.w = image.columns * zoom.value;
282
	canvas.h = image.rows * zoom.value;
283
	if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
7154 leency 284
		zoom.click(BTN_ZOOM_OUT);
7151 leency 285
		DrawEditArea();
286
		return;
287
	}
288
	canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
289
	canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
7150 leency 290
	DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
7151 leency 291
	DrawCanvas();
7150 leency 292
	//}
7089 leency 293
 
7155 leency 294
	left_side = canvas.x-wrapper.x-1;
295
	top_side = canvas.y-wrapper.y-1;
7148 leency 296
 
7155 leency 297
	DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
7150 leency 298
 
7155 leency 299
	if (left_side>0)
7150 leency 300
	{
7155 leency 301
		DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
302
		DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
7150 leency 303
	}
7155 leency 304
	if (top_side>0)
7150 leency 305
	{
7155 leency 306
		DrawBar(wrapper.x, wrapper.y+top_side, left_side, wrapper.h-top_side-top_side, color1); //left
307
		DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side, wrapper.h-top_side-top_side, color1); //right
7150 leency 308
	}
7089 leency 309
}
310
 
311
void DrawActiveColor(dword iny)
312
{
313
	static dword outy;
314
	if (iny != NULL) outy = iny;
7150 leency 315
	DrawBar(right_bar.x, outy, 20, 20, active_color_1);
316
	sprintf(#param, "%A", active_color_1);
7089 leency 317
	EDI = system.color.work;
7148 leency 318
	WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7151 leency 319
 
320
	DrawBar(right_bar.x+110, outy, 20, 20, active_color_2);
321
	sprintf(#param, "%A", active_color_2);
322
	EDI = system.color.work;
323
	WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7089 leency 324
}
325
 
7155 leency 326
void DrawColorPallets(dword _x, _y)
7089 leency 327
{
7155 leency 328
	int r, c, i=0;
7148 leency 329
	int cellw = 20;
330
 
7155 leency 331
	//Last used colors
332
	for (r = 0; r < 2; r++)
333
	{
334
		for (c = 0; c < 13; c++, i++)
335
		{
336
			DrawBar(c*cellw + _x, r*cellw + _y, cellw, cellw, last_used_colors[i]);
337
			DefineHiddenButton(c*cellw + _x, r*cellw + _y, cellw-1, cellw-1, BTNS_LAST_USED_COLORS+i);
338
 
339
		}
340
	}
341
 
342
	_y += r*cellw + 10;
343
	i=0;
344
 
345
	//Default colors
7089 leency 346
	for (r = 0; r < 9; r++)
347
	{
7155 leency 348
		for (c = 0; c < 13; c++, i++)
7089 leency 349
		{
7152 leency 350
			DrawBar(c*cellw + _x, r*cellw + _y, cellw, cellw, default_palette[PALLETE_SIZE-i]);
7155 leency 351
			DefineHiddenButton(c*cellw + _x, r*cellw + _y, cellw-1, cellw-1, BTNS_PALETTE_COLOR_MAS+PALLETE_SIZE-i);
7089 leency 352
		}
353
	}
354
}
355
 
356
 
7155 leency 357
void DrawCanvas()
358
{
359
	int r, c;
360
	for (r = 0; r < image.rows; r++)
361
	{
362
		for (c = 0; c < image.columns; c++)
363
		{
364
			DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
365
				zoom.value, zoom.value, image.get_pixel(r, c));
366
		}
367
	}
368
}
369
 
370
 
7089 leency 371
//===================================================//
372
//                                                   //
373
//                      EVENTS                       //
374
//                                                   //
375
//===================================================//
376
 
7096 leency 377
void EventPickActivate()
378
{
379
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE);
380
	pick_active = true;
381
}
382
 
7150 leency 383
void EventPickColor(dword lkm_status, pkm_status)
7096 leency 384
{
7150 leency 385
	active_color_1 = GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
7096 leency 386
	DrawActiveColor(NULL);
387
	if (mouse.down) && (mouse.key&MOUSE_LEFT) {
388
		pick_active = false;
389
		SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
7155 leency 390
		EventSetActiveColor(1, active_color_1);
7096 leency 391
	}
392
}
7150 leency 393
 
7151 leency 394
dword bmp_32x32x16_header[] = FROM "bmp32x32header";
395
void EventSave()
7150 leency 396
{
7151 leency 397
	char save_buf[3126];
398
	memmov(#save_buf, #bmp_32x32x16_header, sizeof(bmp_32x32x16_header));
7152 leency 399
	memmov(#save_buf+sizeof(bmp_32x32x16_header), image.get_image(), sizeof(save_buf)-sizeof(bmp_32x32x16_header));
7151 leency 400
	if (WriteFile(sizeof(save_buf), #save_buf, "/rd/1/saved_image.bmp")==0)
401
	{
402
		notify("'File saved as /rd/1/saved_image.bmp' -O");
7150 leency 403
	}
7151 leency 404
	else {
405
		notify("'Error saving BPM file, probably not enought space on ramdisk!' -E");
406
	}
7150 leency 407
}
7151 leency 408
 
7155 leency 409
void EventSetActiveColor(int _number, _color)
7151 leency 410
{
411
	int i;
7155 leency 412
	for (i=13*2-1; i>0; i--) {
413
		last_used_colors[i] = last_used_colors[i-1];
414
	}
415
	last_used_colors[0] = _color;
7151 leency 416
 
7155 leency 417
	if (_number == 1) active_color_1 = _color;
418
	if (_number == 2) active_color_2 = _color;
419
 
420
	DrawActiveColor(NULL);
421
	DrawColorPallets(right_bar.x, right_bar.y + 30);
7151 leency 422
}