Subversion Repositories Kolibri OS

Rev

Rev 7422 | Rev 7447 | 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
7187 leency 3
 * Authors: Leency, Nicolas
7089 leency 4
 * Licence: GPL v2
5
*/
6
 
7267 leency 7
#define MEMSIZE 4096*500
7089 leency 8
 
9
#include "../lib/gui.h"
7151 leency 10
#include "../lib/random.h"
11
#include "../lib/mem.h"
7262 leency 12
#include "../lib/cursor.h"
7255 leency 13
 
7096 leency 14
#include "../lib/obj/libimg.h"
7255 leency 15
#include "../lib/obj/box_lib.h"
16
 
7089 leency 17
#include "../lib/patterns/rgb.h"
7253 leency 18
#include "../lib/patterns/libimg_load_skin.h"
7089 leency 19
 
7151 leency 20
#include "colors_mas.h"
21
 
7359 leency 22
#ifndef AUTOBUILD
7358 leency 23
#include "lang.h--"
7359 leency 24
#endif
7358 leency 25
 
7089 leency 26
//===================================================//
27
//                                                   //
28
//                       DATA                        //
29
//                                                   //
30
//===================================================//
31
 
7358 leency 32
#ifdef LANG_RUS
33
#define T_NOTIFY_OPEN "'IconEdit
7422 leency 34
‚ ¤ ­­ë© ¬®¬¥­â IconEdit ¬®¦¥â ®âªà뢠âì ⮫쪮 ¨ª®­ª¨, ᮧ¤ ­­ë¥ ¢ á ¬®¬ । ªâ®à¥.
35
…᫨ ­ã¦­® ®âªàëâì ¤à㣮¥ ¨§®¡à ¦¥­¨¥, ¢®á¯®«ì§ã©â¥áì ¨­áâà㬥­â®¬ <”®â® ¯¯ à â>
36
¤«ï § å¢ â  ª à⨭ª¨ á íªà ­ .' -Wt"
7358 leency 37
#else
38
#define T_NOTIFY_OPEN "'IconEdit
39
You can open only files created in IconEdit for now!
40
In other case please use  tool to get an image from screen.' -Wt"
41
#endif
42
 
43
 
7444 leency 44
#define T_TITLE "Icon Editor 0.58 Alpha"
7096 leency 45
 
7271 leency 46
#define TOPBAR_H    24+8
47
#define LEFTBAR_W 16+5+5+3+3
7096 leency 48
#define PALLETE_SIZE 116
7089 leency 49
 
7194 leency 50
#define PAL_ITEMS_X_COUNT 13
51
#define COLSIZE 18
7200 leency 52
#define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
7187 leency 53
 
7253 leency 54
#define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
55
#define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
56
 
7200 leency 57
block canvas = { NULL, NULL, NULL, NULL };
7271 leency 58
block wrapper = { LEFTBAR_W, TOPBAR_H, NULL, NULL };
59
block right_bar = { NULL, 10+TOPBAR_H, RIGHT_BAR_W+10, NULL };
7200 leency 60
 
7422 leency 61
dword linear_gradient[RIGHT_BAR_W];
62
block b_color_gradient = {NULL, 40+TOPBAR_H, RIGHT_BAR_W, 25};
63
//block b_opacity_gradient = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, 15};
64
block b_last_colors = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, COLSIZE};
65
block b_default_palette = {NULL, COLSIZE+10+75+TOPBAR_H, RIGHT_BAR_W, COLSIZE*9};
7200 leency 66
 
7422 leency 67
dword transparent = 0xBFCAD2;
7207 leency 68
dword color1 = 0x000000;
7255 leency 69
dword color2 = 0xBFCAD2;
7207 leency 70
dword tool_color;
7089 leency 71
 
7257 leency 72
signed hoverX;
73
signed hoverY;
74
signed priorHoverX;
75
signed priorHoverY;
76
bool canvasMouseMoved = false;
77
 
7089 leency 78
enum {
7096 leency 79
	BTN_NEW = 40,
80
	BTN_OPEN,
81
	BTN_SAVE,
82
	BTN_MOVE_LEFT,
83
	BTN_MOVE_RIGHT,
84
	BTN_MOVE_UP,
85
	BTN_MOVE_DOWN,
86
	BTN_FLIP_HOR,
87
	BTN_FLIP_VER,
88
	BTN_ROTATE_LEFT,
89
	BTN_ROTATE_RIGHT,
7265 leency 90
	BTN_TEST_ICON,
7187 leency 91
	BTN_PENCIL,
7096 leency 92
	BTN_PICK,
7156 leency 93
	BTN_FILL,
7186 leency 94
	BTN_LINE,
95
	BTN_RECT,
7259 leency 96
	BTN_BAR,
7253 leency 97
	BTN_SELECT,
7260 leency 98
	BTN_SCREEN_COPY,
7253 leency 99
	BTN_ZOOM_IN,
100
	BTN_ZOOM_OUT,
7274 leency 101
	BTN_CANVAS_RESIZE,
7275 leency 102
	BTN_CROP,
7155 leency 103
	BTNS_PALETTE_COLOR_MAS = 100,
104
	BTNS_LAST_USED_COLORS = 400
7089 leency 105
};
106
 
107
proc_info Form;
7255 leency 108
dword semi_white;
7422 leency 109
bool bg_dark=false;
7089 leency 110
 
7243 leency 111
more_less_box zoom = { 11, 1, 40, "Zoom" };
7150 leency 112
 
7152 leency 113
dword default_palette[] = {
7190 leency 114
0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
115
0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
116
0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
117
0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
118
0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
119
0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
120
0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
121
0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
122
0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
123
0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
124
0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
125
0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF
7089 leency 126
};
7422 leency 127
 
128
#define LAST_USED_MAX 13
129
dword last_used_colors[LAST_USED_MAX] = {
7190 leency 130
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
7422 leency 131
0xFFFFFF,0xFFFFFF,0xFFFFFF
7155 leency 132
};
7089 leency 133
 
7262 leency 134
CustomCursor Cursor;
135
dword CursorBar = FROM "cursors/bar.cur";
136
dword CursorFill = FROM "cursors/fill.cur";
137
dword CursorLine = FROM "cursors/line.cur";
138
dword CursorPencil = FROM "cursors/pencil.cur";
139
dword CursorPipette = FROM "cursors/pipette.cur";
140
dword CursorRectangle = FROM "cursors/rectangle.cur";
141
dword CursorSelect = FROM "cursors/select.cur";
142
 
7155 leency 143
_image image;
7148 leency 144
 
7204 leency 145
#include "actions_history.h"
146
 
147
_ActionsHistory actionsHistory;
7151 leency 148
 
7254 leency 149
#include "tools.h"
7274 leency 150
#include "canvas_resize.h"
7156 leency 151
 
7089 leency 152
//===================================================//
153
//                                                   //
154
//                       CODE                        //
155
//                                                   //
156
//===================================================//
157
 
7266 leency 158
libimg_image top_icons;
159
libimg_image left_icons;
160
 
7089 leency 161
void main()
162
{
163
	word btn;
7257 leency 164
	libimg_image open_image;
7444 leency 165
	dword bg_col;
7089 leency 166
 
7096 leency 167
	load_dll(libio,  #libio_init,  1);
168
	load_dll(libimg, #libimg_init, 1);
7255 leency 169
	load_dll(boxlib, #box_lib_init,0);
170
 
7266 leency 171
	Libimg_LoadImage(#top_icons, "/sys/icons16.png");
172
	Libimg_LoadImage(#left_icons, "/sys/icons16.png");
173
 
7255 leency 174
	system.color.get();
7444 leency 175
	bg_col = system.color.work;
176
	if (GrayScaleImage(#bg_col,1,1)<65) bg_dark=true; else bg_dark=false;
7422 leency 177
 
7444 leency 178
	semi_white = MixColors(system.color.work, 0xFFFfff, bg_dark*90 + 96);
7266 leency 179
	Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffFFFfff, semi_white);
180
	Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffCACBD6, MixColors(semi_white, 0, 220));
7089 leency 181
 
7266 leency 182
	Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffFFFfff, system.color.work);
183
	Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffCACBD6, MixColors(system.color.work, 0, 200));
184
 
7422 leency 185
	//fix line and rectandle color for dark skins
186
	if (bg_dark) Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xff545454, 0xffD3D3D4);
187
 
188
	EventSetActiveColor(1, color1);
189
 
7254 leency 190
	if (!param[0]) {
191
		image.create(32, 32);
192
	}
193
	else
194
	{
7151 leency 195
		Libimg_LoadImage(#open_image, #param);
7254 leency 196
 
197
		if (open_image.w*open_image.h>MAX_CELL_SIZE*MAX_CELL_SIZE) {
198
			notify("'Hey, this is just an icon editor,\nselected image is too big to open!' -E");
199
			ExitProcess();
7151 leency 200
		}
201
		else {
7422 leency 202
			image.create(open_image.h, open_image.w);
7254 leency 203
			image.set_image(open_image.imgsrc);
7151 leency 204
		}
205
	}
206
 
7204 leency 207
	actionsHistory.init();
208
 
7186 leency 209
	initTools();
210
	setCurrentTool(TOOL_PENCIL);
211
 
7096 leency 212
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
213
 
7089 leency 214
	loop() switch(WaitEvent())
215
	{
7096 leency 216
		case evMouse:
7274 leency 217
			if (Window_CanvasReSize.thread_exists()) break;
7096 leency 218
			mouse.get();
7186 leency 219
 
7207 leency 220
			if (mouse.lkm) tool_color = color1;
221
			if (mouse.pkm) tool_color = color2;
222
			if (mouse.mkm) break;
223
 
7257 leency 224
			hoverX = mouse.x - canvas.x / zoom.value;
225
			hoverY = mouse.y - canvas.y / zoom.value;
226
			if (hoverX<0) hoverX = 0;
227
			if (hoverY<0) hoverY = 0;
228
			if (hoverX>image.columns-1) hoverX = image.columns-1;
229
			if (hoverY>image.rows-1) hoverY = image.rows-1;
230
			canvasMouseMoved = false;
231
			if (priorHoverX != hoverX) canvasMouseMoved = true;
232
			if (priorHoverY != hoverY) canvasMouseMoved = true;
233
			priorHoverX = hoverX;
234
			priorHoverY = hoverY;
235
			//DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff);
236
			//WriteText(Form.cwidth-100, 3, 0x80, 0x000000,
237
			//	sprintf(#param, "%i %i", hoverX, hoverY));
238
 
7186 leency 239
			if (currentTool != TOOL_NONE)
240
				tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
241
 
7156 leency 242
			if (mouse.vert) {
7243 leency 243
				if (mouse.vert==65535) zoom.inc();
244
				if (mouse.vert==1) zoom.dec();
7152 leency 245
				DrawEditArea();
246
			}
7186 leency 247
 
7262 leency 248
			if (wrapper.hovered()) SetCursor();
249
			else Cursor.Restore();
250
 
7200 leency 251
			if (mouse.down) {
252
				if (b_color_gradient.hovered())
253
				|| (b_last_colors.hovered())
254
				|| (b_default_palette.hovered()) {
255
					if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
256
					if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
257
				}
258
			}
259
 
7148 leency 260
			break;
7096 leency 261
 
7089 leency 262
		case evButton:
7274 leency 263
			if (Window_CanvasReSize.thread_exists()) break;
7148 leency 264
			btn = GetButtonID();
7253 leency 265
 
7255 leency 266
			if (zoom.click(btn)) DrawEditArea();
267
 
7096 leency 268
			switch(btn)
269
			{
7151 leency 270
				case BTN_NEW:
7274 leency 271
					EventCreateNewIcon();
7151 leency 272
					break;
273
				case BTN_OPEN:
7422 leency 274
					EventOpenIcon();
7151 leency 275
					break;
276
				case BTN_SAVE:
7257 leency 277
					EventSaveIconToFile();
7151 leency 278
					break;
7096 leency 279
				case BTN_MOVE_LEFT:
7267 leency 280
					EventMove(MOVE_LEFT);
7096 leency 281
					break;
282
				case BTN_MOVE_RIGHT:
7267 leency 283
					EventMove(MOVE_RIGHT);
7096 leency 284
					break;
7147 leency 285
				case BTN_MOVE_UP:
7267 leency 286
					EventMove(MOVE_UP);
7147 leency 287
					break;
288
				case BTN_MOVE_DOWN:
7267 leency 289
					EventMove(MOVE_DOWN);
7147 leency 290
					break;
7151 leency 291
				case BTN_FLIP_VER:
7267 leency 292
					EventMove(FLIP_VER);
7151 leency 293
					break;
294
				case BTN_FLIP_HOR:
7267 leency 295
					EventMove(FLIP_HOR);
7151 leency 296
					break;
7444 leency 297
				case BTN_ROTATE_LEFT:
298
					EventMove(ROTATE_LEFT);
299
					break;
300
				case BTN_ROTATE_RIGHT:
301
					EventMove(ROTATE_RIGHT);
302
					break;
7265 leency 303
				case BTN_TEST_ICON:
304
					EventTestIcon();
305
					break;
7187 leency 306
				case BTN_PENCIL:
307
					setCurrentTool(TOOL_PENCIL);
308
					break;
7096 leency 309
				case BTN_PICK:
7186 leency 310
					setCurrentTool(TOOL_PIPETTE);
7096 leency 311
					break;
7156 leency 312
				case BTN_FILL:
7186 leency 313
					setCurrentTool(TOOL_FILL);
7156 leency 314
					break;
7186 leency 315
				case BTN_LINE:
316
					setCurrentTool(TOOL_LINE);
317
					break;
318
				case BTN_RECT:
319
					setCurrentTool(TOOL_RECT);
320
					break;
7259 leency 321
				case BTN_BAR:
322
					setCurrentTool(TOOL_BAR);
323
					break;
7253 leency 324
				case BTN_SELECT:
325
					setCurrentTool(TOOL_SELECT);
326
					break;
7260 leency 327
				case BTN_SCREEN_COPY:
328
					setCurrentTool(TOOL_SCREEN_COPY);
329
					break;
7274 leency 330
				case BTN_CANVAS_RESIZE:
331
					notify("Sorry, not implemented yet.");
332
					break;
7275 leency 333
				case BTN_CROP:
334
					EventCrop();
335
					break;
7152 leency 336
				case CLOSE_BTN:
7257 leency 337
					EventExitIconEdit();
7152 leency 338
					break;
7089 leency 339
			}
340
			break;
341
 
342
		case evKey:
343
			GetKeys();
7253 leency 344
 
7422 leency 345
			if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL)
346
			{
347
				switch(key_scancode)
348
				{
349
					case SCAN_CODE_KEY_S:
350
						EventSaveIconToFile();
351
						break;
352
					case SCAN_CODE_KEY_O:
353
						EventOpenIcon();
354
						break;
7444 leency 355
					case SCAN_CODE_LEFT:
356
						EventMove(MOVE_LEFT);
357
						break;
358
					case SCAN_CODE_RIGHT:
359
						EventMove(MOVE_RIGHT);
360
						break;
361
					case SCAN_CODE_UP:
362
						EventMove(MOVE_UP);
363
						break;
364
					case SCAN_CODE_DOWN:
365
						EventMove(MOVE_DOWN);
366
						break;
367
					case SCAN_CODE_KEY_R:
368
						EventMove(BTN_ROTATE_RIGHT);
369
						break;
370
					case SCAN_CODE_KEY_L:
371
						EventMove(BTN_ROTATE_LEFT);
372
						break;
7422 leency 373
				}
374
			}
375
 
376
			if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
377
				if (key_scancode == SCAN_CODE_DEL) EventCleanCanvas();
378
			}
379
 
7253 leency 380
			if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
381
				tools[currentTool].onKeyEvent(key_scancode);
382
 
7187 leency 383
			if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
7186 leency 384
			if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
7187 leency 385
			if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL);
386
			if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE);
387
			if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT);
7260 leency 388
			if (key_scancode == SCAN_CODE_KEY_B) setCurrentTool(TOOL_BAR);
389
			if (key_scancode == SCAN_CODE_KEY_S) setCurrentTool(TOOL_SELECT);
7204 leency 390
 
7265 leency 391
			if (key_scancode == SCAN_CODE_KEY_T) EventTestIcon();
392
 
7267 leency 393
			if (key_scancode == SCAN_CODE_KEY_Z) actionsHistory.undoLastAction();
394
			if (key_scancode == SCAN_CODE_KEY_Y) actionsHistory.redoLastAction();
7204 leency 395
 
7253 leency 396
			if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
7274 leency 397
			if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc(); DrawEditArea();}
7253 leency 398
 
7089 leency 399
			break;
400
 
401
		case evReDraw:
7274 leency 402
			Window_CanvasReSize.thread_exists();
7275 leency 403
			DrawWindow();
7089 leency 404
			break;
405
	}
406
}
407
 
7266 leency 408
void DrawTopPanelButton(dword _id, _x, _icon_n)
7096 leency 409
{
7255 leency 410
	DrawWideRectangle(_x, 4, 22, 22, 3, semi_white);
7271 leency 411
	PutPixel(_x,4,system.color.work);
412
	PutPixel(_x,4+21,system.color.work);
413
	PutPixel(_x+21,4,system.color.work);
414
	PutPixel(_x+21,4+21,system.color.work);
7152 leency 415
	DefineHiddenButton(_x, 4, 21, 21, _id);
7266 leency 416
	img_draw stdcall(top_icons.image, _x+3, 7, 16, 16, 0, _icon_n*16);
7096 leency 417
}
418
 
7187 leency 419
void DrawLeftPanelButton(dword _id, _y, _icon_n)
420
{
421
	int x = 5;
7266 leency 422
	DrawRectangle(x, _y, 22-1, 22-1, system.color.work);
7187 leency 423
	DefineHiddenButton(x, _y, 21, 21, _id);
7266 leency 424
	img_draw stdcall(left_icons.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
7187 leency 425
}
426
 
7148 leency 427
void DrawStatusBar()
428
{
7243 leency 429
	zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
7150 leency 430
 
7275 leency 431
	sprintf(#param,"%i x %i", image.columns, image.rows);
7274 leency 432
	DrawCaptButton(
7275 leency 433
		wrapper.x+wrapper.w-calc(strlen(#param)*8) -6 - 1,
7274 leency 434
		zoom.y,
7275 leency 435
		calc(strlen(#param)*8)+6,
7274 leency 436
		18,
437
		BTN_CANVAS_RESIZE,
438
		system.color.work_button,
439
		system.color.work_button_text,
7275 leency 440
		#param
7274 leency 441
		);
7148 leency 442
}
443
 
7275 leency 444
void DrawWindow()
7089 leency 445
{
7266 leency 446
	#define GAP 27
447
	#define BLOCK_SPACE 10
7096 leency 448
	incn tx;
7089 leency 449
	system.color.get();
7271 leency 450
	DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x73, NULL, T_TITLE, 0);
7089 leency 451
	GetProcessInfo(#Form, SelfInfo);
7152 leency 452
	if (Form.status_window>2) return;
453
	if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
454
	if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
455
 
7150 leency 456
	right_bar.x = Form.cwidth - right_bar.w;
7200 leency 457
	b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
7271 leency 458
	DrawBar(0, 0, Form.cwidth, TOPBAR_H-1, system.color.work);
459
	DrawBar(0, TOPBAR_H-1, Form.cwidth, 1, system.color.work_graph);
7089 leency 460
 
7266 leency 461
	tx.n = 5-GAP;
7444 leency 462
	DrawTopPanelButton(BTN_NEW,    tx.inc(GAP), 2);
7266 leency 463
	DrawTopPanelButton(BTN_OPEN,   tx.inc(GAP), 0); //not implemented
464
	DrawTopPanelButton(BTN_SAVE,   tx.inc(GAP), 5);
465
	DrawTopPanelButton(BTN_MOVE_LEFT,  tx.inc(GAP+BLOCK_SPACE), 30);
466
	DrawTopPanelButton(BTN_MOVE_RIGHT, tx.inc(GAP),   31);
467
	DrawTopPanelButton(BTN_MOVE_UP,    tx.inc(GAP),   32);
468
	DrawTopPanelButton(BTN_MOVE_DOWN,  tx.inc(GAP),   33);
7147 leency 469
 
7266 leency 470
	DrawTopPanelButton(BTN_FLIP_HOR,   tx.inc(GAP+BLOCK_SPACE), 34);
471
	DrawTopPanelButton(BTN_FLIP_VER,   tx.inc(GAP),   35);
7444 leency 472
	DrawTopPanelButton(BTN_ROTATE_LEFT,   tx.inc(GAP), 37);
473
	DrawTopPanelButton(BTN_ROTATE_RIGHT,  tx.inc(GAP), 36);
7265 leency 474
 
7266 leency 475
	DrawTopPanelButton(BTN_TEST_ICON,  tx.inc(GAP+BLOCK_SPACE), 12);
7275 leency 476
 
477
	DrawTopPanelButton(BTN_CROP,  tx.inc(GAP+BLOCK_SPACE), 46);
7186 leency 478
 
7150 leency 479
	DrawEditArea();
7096 leency 480
 
7271 leency 481
	DrawBar(0, TOPBAR_H, LEFTBAR_W-1, Form.cheight - TOPBAR_H, system.color.work);
7274 leency 482
	DrawLeftPanel();
483
 
7271 leency 484
	DrawBar(wrapper.x+wrapper.w, TOPBAR_H, Form.cwidth-wrapper.x-wrapper.w,
485
		Form.cheight - TOPBAR_H, system.color.work);
7274 leency 486
	DrawActiveColor(right_bar.y);
487
	DrawColorPallets();
488
	DrawPreview();
489
 
7271 leency 490
	DrawBar(LEFTBAR_W-1, wrapper.y + wrapper.h, wrapper.w+1,
491
		Form.cheight - wrapper.y - wrapper.h, system.color.work);
7148 leency 492
	DrawStatusBar();
7089 leency 493
}
494
 
7187 leency 495
void DrawLeftPanel()
496
{
7266 leency 497
	#define GAP 28
7187 leency 498
	incn ty;
7266 leency 499
	ty.n = right_bar.y - GAP - 2;
500
	DrawLeftPanelButton(BTN_PENCIL, ty.inc(GAP), 38);
501
	DrawLeftPanelButton(BTN_PICK,   ty.inc(GAP), 39);
502
	DrawLeftPanelButton(BTN_FILL,   ty.inc(GAP), 40);
503
	DrawLeftPanelButton(BTN_LINE,   ty.inc(GAP), 41);
504
	DrawLeftPanelButton(BTN_RECT,   ty.inc(GAP), 42);
505
	DrawLeftPanelButton(BTN_BAR,    ty.inc(GAP), 43);
506
	DrawLeftPanelButton(BTN_SELECT, ty.inc(GAP), 44);
507
	DrawLeftPanelButton(BTN_SCREEN_COPY, ty.inc(GAP), 45);
508
	DrawRectangle3D(5, currentTool*GAP+right_bar.y-2, 16+3+2, 16+3+2, 0x333333, 0x777777);
7187 leency 509
}
510
 
7151 leency 511
void DrawEditArea()
7089 leency 512
{
7150 leency 513
	dword color1=0xC0C0C0;
7155 leency 514
	int top_side;
515
	int left_side;
7089 leency 516
 
7194 leency 517
	wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
7271 leency 518
	wrapper.h = Form.cheight - TOPBAR_H - 35;
7148 leency 519
 
7150 leency 520
	//canvas{
7151 leency 521
	canvas.w = image.columns * zoom.value;
522
	canvas.h = image.rows * zoom.value;
523
	if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
7254 leency 524
		zoom.value--;
7422 leency 525
		if (zoom.x) zoom.redraw();
7151 leency 526
		DrawEditArea();
527
		return;
528
	}
529
	canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
530
	canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
531
	DrawCanvas();
7150 leency 532
	//}
7089 leency 533
 
7155 leency 534
	left_side = canvas.x-wrapper.x-1;
535
	top_side = canvas.y-wrapper.y-1;
7148 leency 536
 
7155 leency 537
	DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
7150 leency 538
 
7155 leency 539
	if (left_side>0)
7150 leency 540
	{
7155 leency 541
		DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
542
		DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
7150 leency 543
	}
7155 leency 544
	if (top_side>0)
7150 leency 545
	{
7190 leency 546
		//left
547
		DrawBar(wrapper.x, wrapper.y+top_side, left_side,
548
			wrapper.h-top_side-top_side, color1);
549
		//right
550
		DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
551
			wrapper.h-top_side-top_side, color1);
7150 leency 552
	}
7194 leency 553
	DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
7089 leency 554
}
555
 
556
void DrawActiveColor(dword iny)
557
{
7255 leency 558
	#define CELL 20
7089 leency 559
	static dword outy;
560
	if (iny != NULL) outy = iny;
7255 leency 561
	DrawFrame(right_bar.x, outy, CELL, CELL, NULL);
562
	DrawBar(right_bar.x+2, outy+2, CELL-4, CELL-4, color1);
7151 leency 563
 
7422 leency 564
	DrawFrame(right_bar.x+CELL+5, outy, CELL, CELL, NULL);
565
	DrawBar(right_bar.x+CELL+5+2, outy+2, CELL-4, CELL-4, color2);
566
 
567
	//sprintf(#param, "%A", color1);
568
	//WriteTextWithBg(right_bar.x+30, outy+3, 0xD0, system.color.work_text, #param+4, system.color.work);
569
	DrawCurrentColorGradient();
7089 leency 570
}
571
 
7422 leency 572
int lmax;
573
void GenerateCurrentColorGradient()
7194 leency 574
{
7422 leency 575
	int i, avg, rmax;
576
 
577
	rgb.DwordToRgb(color1);
578
	avg = 255 - calc(rgb.r + rgb.g + rgb.b / 3);
579
 
580
	lmax = b_color_gradient.w *avg/255 | 1;
581
	rmax = b_color_gradient.w - lmax | 1;
582
	if (lmax == 0) lmax=1;
583
	if (rmax == 0) rmax=1;
584
 
585
	for (i=0; i
586
		linear_gradient[i] = MixColors(color1,0xFFFfff,255*i/lmax);
587
	}
588
 
589
	for (i=0 ; i<=rmax; i++) {
590
		linear_gradient[lmax+rmax - i] = MixColors(color1,0x000000,255*i/rmax);
591
	}
592
}
593
 
7444 leency 594
int DrawGradientMarker(dword marker_x, marker_color)
595
{
596
	if (marker_x > b_color_gradient.w - 1) marker_x = b_color_gradient.w - 1;
597
	DrawBar(b_color_gradient.x + marker_x-2, b_color_gradient.y-3, 5, 1, marker_color);
598
	DrawBar(b_color_gradient.x + marker_x-1, b_color_gradient.y-2, 3, 1, marker_color);
599
	PutPixel(b_color_gradient.x + marker_x, b_color_gradient.y-1, marker_color);
600
	return marker_x;
601
}
602
 
603
int old_marker_pos;
7422 leency 604
void DrawCurrentColorGradient()
605
{
7194 leency 606
	int i;
7422 leency 607
	for (i=0 ; i
608
		DrawBar(b_color_gradient.x+i, b_color_gradient.y, 1, b_color_gradient.h, linear_gradient[i]);
609
	}
7444 leency 610
	DrawGradientMarker(old_marker_pos, system.color.work);
611
	old_marker_pos = DrawGradientMarker(lmax, 0xFFFfff * bg_dark);
7194 leency 612
}
613
 
7200 leency 614
void DrawColorPallets()
7089 leency 615
{
7155 leency 616
	int r, c, i=0;
617
	//Last used colors
7422 leency 618
	for (r = 0; r < LAST_USED_MAX/PAL_ITEMS_X_COUNT; r++)
7155 leency 619
	{
7194 leency 620
		for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
7155 leency 621
		{
7200 leency 622
			DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
623
				COLSIZE, COLSIZE, last_used_colors[i]);
7155 leency 624
		}
625
	}
626
	i=0;
627
	//Default colors
7089 leency 628
	for (r = 0; r < 9; r++)
629
	{
7194 leency 630
		for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
7089 leency 631
		{
7200 leency 632
			DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
633
				COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
7089 leency 634
		}
635
	}
636
}
637
 
7257 leency 638
void DrawCanvasPixel(dword _r,_c,_color)
639
{
640
	DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y,
641
	zoom.value, zoom.value, _color);
642
}
643
 
7155 leency 644
void DrawCanvas()
645
{
646
	int r, c;
7257 leency 647
	dword color;
648
 
649
	if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
650
	{
651
		tools[currentTool].onCanvasDraw();
652
	}
653
 
7155 leency 654
	for (r = 0; r < image.rows; r++)
655
	{
656
		for (c = 0; c < image.columns; c++)
657
		{
7257 leency 658
			if (image.pixel_state.is_drawable(r,c))
659
				DrawCanvasPixel(r, c, image.get_pixel(r,c));
7155 leency 660
		}
661
	}
7257 leency 662
	image.pixel_state.reset_and_set_all_drawable();
7186 leency 663
 
7200 leency 664
	DrawPreview();
7155 leency 665
}
666
 
7200 leency 667
void DrawPreview()
668
{
669
	int x = right_bar.x;
7444 leency 670
	int y = b_default_palette.y + b_default_palette.h + 6;
671
	int preview_h = Form.cheight - y;
672
 
673
	if (image.columns > right_bar.w) return;
674
	if (image.rows > preview_h) return;
675
 
676
	_PutImage(right_bar.w - image.columns / 2 + x - 3,
677
		preview_h - image.rows / 2 + y,
678
		image.columns, image.rows, image.get_image()
679
		);
7200 leency 680
}
7155 leency 681
 
7200 leency 682
dword GetPixelUnderMouse()
683
{
684
	return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
685
}
686
 
7265 leency 687
int preview_size = 128;
688
void DrawImageWithBg(dword _x, _y, _col_to)
689
{
690
	_x *= preview_size;
691
	_y *= preview_size;
692
	DrawWideRectangle(_x,_y, preview_size, preview_size, preview_size-image.columns/2, _col_to);
693
	_PutImage(preview_size - image.columns / 2 + _x, preview_size - image.rows / 2 + _y,
694
		image.columns, image.rows, image.get_image_with_replaced_color(color2, _col_to));
695
}
696
 
697
void ShowWindow_TestIcon()
698
{
7422 leency 699
	if (image.rows>=preview_size) || (image.columns>=preview_size) {
700
		notify("'IconEdit\nImage is too big for preview!' -tE");
701
		return;
702
	}
7265 leency 703
	loop() switch(WaitEvent())
704
	{
705
		case evButton:
706
			if (GetButtonID()) ExitProcess();
707
			break;
708
 
709
		case evKey:
710
			GetKeys();
711
			if (key_scancode == SCAN_CODE_ESC) ExitProcess();
712
			break;
713
 
714
		case evReDraw:
715
			DefineAndDrawWindow(Form.left+100, Form.top+100, preview_size*2+9,
716
				preview_size*2+skin_height+4, 0x74, NULL, "Test Icon", 0);
717
			DrawImageWithBg(0, 0, 0x000000);
718
			DrawImageWithBg(1, 0, 0xFFFfff);
719
			DrawImageWithBg(0, 1, GetPixelColorFromScreen(0, 0));
720
			DrawImageWithBg(1, 1, system.color.work);
721
			break;
722
	}
723
}
724
 
7089 leency 725
//===================================================//
726
//                                                   //
727
//                      EVENTS                       //
728
//                                                   //
729
//===================================================//
730
 
7274 leency 731
void EventCreateNewIcon()
732
{
733
	EventSaveIconToFile();
734
	Window_CanvasReSize.create();
735
}
736
 
7422 leency 737
void EventOpenIcon()
738
{
739
	//notify(T_NOTIFY_OPEN);
740
	RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
741
}
742
 
7257 leency 743
void EventSaveIconToFile()
7150 leency 744
{
7257 leency 745
	int i=0;
746
	char save_file_name[4096];
747
	char save_path_stable[4096];
748
	strcpy(#save_path_stable, "/tmp0/1");
749
	do {
750
		i++;
751
		sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i);
752
	} while (file_exists(#save_file_name));
753
	save_image(image.get_image(), image.columns, image.rows, #save_file_name);
7150 leency 754
}
7151 leency 755
 
7257 leency 756
void EventCleanCanvas()
757
{
7274 leency 758
	image.create(image.rows, image.columns);
759
	actionsHistory.saveCurrentState();
7257 leency 760
	DrawCanvas();
761
}
762
 
763
void EventExitIconEdit()
764
{
765
	EventSaveIconToFile();
766
	ExitProcess();
767
}
768
 
7155 leency 769
void EventSetActiveColor(int _number, _color)
7151 leency 770
{
771
	int i;
7422 leency 772
	if (last_used_colors[0] == _color) return;
773
	for (i=LAST_USED_MAX-1; i>0; i--) {
7155 leency 774
		last_used_colors[i] = last_used_colors[i-1];
775
	}
776
	last_used_colors[0] = _color;
7151 leency 777
 
7207 leency 778
	if (_number == 1) color1 = _color;
779
	if (_number == 2) color2 = _color;
7155 leency 780
 
7422 leency 781
	if (b_color_gradient.hovered()) {
782
		lmax = mouse.x - b_color_gradient.x;
783
	}
784
	else {
785
		GenerateCurrentColorGradient();
786
	}
7155 leency 787
	DrawActiveColor(NULL);
7200 leency 788
	DrawColorPallets();
7156 leency 789
}
7257 leency 790
 
7265 leency 791
void EventTestIcon()
792
{
793
	CreateThread(#ShowWindow_TestIcon, #test_icon_stak+4092);
794
}
795
 
7267 leency 796
void EventMove(dword _action)
797
{
7273 leency 798
	if (selection.state) {
799
		selection.buf.move(_action);
7271 leency 800
		SelectTool_onCanvasDraw();
7267 leency 801
	}
802
	else {
803
		image.move(_action);
804
		DrawCanvas();
7271 leency 805
	}
806
	actionsHistory.saveCurrentState();
7267 leency 807
}
808
 
7275 leency 809
void EventCrop()
810
{
811
	if (selection.state) {
812
		EventSaveIconToFile();
813
		image.create(selection.buf.rows, selection.buf.columns);
814
		selection.move_to_point(0,0);
815
		selection.apply_to_image();
816
		selection.reset();
817
		actionsHistory.init();
818
		DrawWindow();
819
	}
820
	else {
821
		notify("'You need to select something before usnig crop tool.' -W");
822
	}
823
}
824
 
7265 leency 825
stop:
826
 
827
char test_icon_stak[4096];