Subversion Repositories Kolibri OS

Rev

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