Subversion Repositories Kolibri OS

Rev

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