Subversion Repositories Kolibri OS

Rev

Rev 7207 | Rev 7229 | 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
 
7151 leency 7
/*
7156 leency 8
TODO:
9
window colors
10
enhance icon
7194 leency 11
pipet aside color view
7151 leency 12
*/
13
 
7096 leency 14
#define MEMSIZE 4096*40
7089 leency 15
 
16
#include "../lib/gui.h"
7151 leency 17
#include "../lib/random.h"
18
#include "../lib/mem.h"
7096 leency 19
#include "../lib/obj/libimg.h"
7089 leency 20
#include "../lib/patterns/rgb.h"
7096 leency 21
#include "../lib/patterns/libimg_load_skin.h"
7089 leency 22
 
7151 leency 23
#include "colors_mas.h"
24
 
7089 leency 25
//===================================================//
26
//                                                   //
27
//                       DATA                        //
28
//                                                   //
29
//===================================================//
30
 
7207 leency 31
#define T_TITLE "Icon Editor 0.39"
7096 leency 32
 
7152 leency 33
#define TOOLBAR_H    24+8
7187 leency 34
#define PANEL_LEFT_W 16+5+5+3+3
7096 leency 35
#define PALLETE_SIZE 116
7187 leency 36
#define TB_ICON_PADDING 26
7089 leency 37
 
7194 leency 38
#define PAL_ITEMS_X_COUNT 13
39
#define COLSIZE 18
7200 leency 40
#define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
7187 leency 41
 
7200 leency 42
block canvas = { NULL, NULL, NULL, NULL };
43
block wrapper = { PANEL_LEFT_W, TOOLBAR_H, NULL, NULL };
44
block right_bar = { NULL, TOOLBAR_H, RIGHT_BAR_W+10, NULL };
45
 
46
block b_color_gradient = {NULL, 30+TOOLBAR_H, RIGHT_BAR_W, 30};
47
block b_last_colors = {NULL, 70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*2};
48
block b_default_palette = {NULL, COLSIZE*2+10+70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*9};
49
 
7207 leency 50
dword color1 = 0x000000;
51
dword color2 = 0xFFFfff;
52
dword tool_color;
7089 leency 53
 
54
enum {
7096 leency 55
	BTN_NEW = 40,
56
	BTN_OPEN,
57
	BTN_SAVE,
58
	BTN_MOVE_LEFT,
59
	BTN_MOVE_RIGHT,
60
	BTN_MOVE_UP,
61
	BTN_MOVE_DOWN,
62
	BTN_FLIP_HOR,
63
	BTN_FLIP_VER,
64
	BTN_ROTATE_LEFT,
65
	BTN_ROTATE_RIGHT,
7187 leency 66
	BTN_PENCIL,
7096 leency 67
	BTN_PICK,
7156 leency 68
	BTN_FILL,
7186 leency 69
	BTN_LINE,
70
	BTN_RECT,
7152 leency 71
	BTN_ZOOM_IN,
72
	BTN_ZOOM_OUT,
7155 leency 73
	BTNS_PALETTE_COLOR_MAS = 100,
74
	BTNS_LAST_USED_COLORS = 400
7089 leency 75
};
76
 
77
proc_info Form;
78
 
7187 leency 79
more_less_box zoom = { PANEL_LEFT_W, -100, 11, 1, 40, BTN_ZOOM_IN, BTN_ZOOM_OUT, "Zoom" };
7150 leency 80
 
7152 leency 81
dword default_palette[] = {
7190 leency 82
0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
83
0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
84
0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
85
0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
86
0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
87
0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
88
0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
89
0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
90
0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
91
0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
92
0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
93
0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF
7089 leency 94
};
7155 leency 95
dword last_used_colors[13*2] = {
7190 leency 96
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
97
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
98
0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF
7155 leency 99
};
7089 leency 100
 
7155 leency 101
_image image;
7148 leency 102
 
7204 leency 103
#include "actions_history.h"
104
 
7151 leency 105
libimg_image open_image;
7204 leency 106
_ActionsHistory actionsHistory;
7151 leency 107
 
7156 leency 108
enum {
7186 leency 109
	TOOL_NONE = -1,
7187 leency 110
	TOOL_PENCIL,
111
	TOOL_PIPETTE,
112
	TOOL_FILL,
113
	TOOL_LINE,
114
	TOOL_RECT,
7156 leency 115
};
116
 
7186 leency 117
struct Tool {
118
	int id;
119
 
120
	void (*activate)();
121
	void (*deactivate)();
122
	void (*onMouseEvent)(int x, int y, int lkm, int pkm);
123
	void (*onCanvasDraw)();
124
};
125
 
126
Tool tools[5];
127
int currentTool = -1;
128
 
129
void resetCurrentTool() {
130
	if ((currentTool != TOOL_NONE) && (tools[currentTool].deactivate != 0)) {
131
		tools[currentTool].deactivate();
132
	}
133
 
134
	currentTool = TOOL_NONE;
135
}
136
 
137
void setCurrentTool(int index) {
138
	resetCurrentTool();
7204 leency 139
 
140
	currentTool = index;
7186 leency 141
 
142
	if ((index != TOOL_NONE) && (tools[index].activate != 0))
143
		tools[index].activate();
7187 leency 144
 
145
	DrawLeftPanel();
7186 leency 146
}
147
 
7089 leency 148
//===================================================//
149
//                                                   //
150
//                       CODE                        //
151
//                                                   //
152
//===================================================//
153
 
7186 leency 154
void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7207 leency 155
	if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up)
7186 leency 156
	{
7207 leency 157
		EventFill(mouseY-canvas.y/zoom.value,
158
				mouseX-canvas.x/zoom.value, tool_color);
159
		actionsHistory.saveCurrentState();
7186 leency 160
		DrawCanvas();
161
	}
162
}
163
 
164
void PipetteTool_activate() {
165
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE);
166
}
167
 
168
void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7207 leency 169
	tool_color = GetPixelUnderMouse();
170
	DrawBar(Form.cwidth-30, 5, 20, 20, tool_color);
7186 leency 171
 
7207 leency 172
	if (mouse.down) {
7186 leency 173
		SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
7207 leency 174
		if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color);
175
		if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color);
7186 leency 176
 
177
		setCurrentTool(TOOL_PENCIL);
178
	}
179
}
180
 
7204 leency 181
bool PencilTool_Drawing = false;
182
 
7186 leency 183
void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7200 leency 184
	if (canvas.hovered())
7186 leency 185
	{
7207 leency 186
		if ((PencilTool_Drawing == true) && (!mouse.key)) {
7204 leency 187
			actionsHistory.saveCurrentState();
188
			PencilTool_Drawing = false;
189
		}
190
 
7207 leency 191
		if (mouse.key) {
7186 leency 192
			image.set_pixel(mouseY-canvas.y/zoom.value,
7207 leency 193
				mouseX-canvas.x/zoom.value, tool_color);
7204 leency 194
			PencilTool_Drawing = true;
7207 leency 195
		}
7186 leency 196
		DrawCanvas();
197
	}
198
}
199
 
7204 leency 200
void PencilTool_reset() {
201
	PencilTool_Drawing = false;
202
}
203
 
7186 leency 204
// Line tool
7204 leency 205
struct SimpleFigureTool_State {
206
	int startX, startY;
207
	int lastTempPosX, lastTempPosY;
208
};
7186 leency 209
 
7204 leency 210
enum {
211
	TOOL_LINE_STATE,
212
	TOOL_RECT_STATE
213
};
7186 leency 214
 
7204 leency 215
dword currentFigToolState = -1;
216
SimpleFigureTool_State figTool_States[2];
217
 
218
void SimpleFigureTool_Reset() {
219
	if (currentTool == TOOL_LINE)
220
		currentFigToolState = TOOL_LINE_STATE;
221
	else if (currentTool == TOOL_RECT)
222
		currentFigToolState = TOOL_RECT_STATE;
223
 
224
	figTool_States[currentFigToolState].startX = -1;
225
	figTool_States[currentFigToolState].startY = -1;
226
	figTool_States[currentFigToolState].lastTempPosX = -1;
227
	figTool_States[currentFigToolState].lastTempPosY = -1;
7186 leency 228
}
229
 
7206 leency 230
int mouseX_last;
231
int mouseY_last;
232
bool first_click_in_canvas = false;
233
 
7204 leency 234
void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7206 leency 235
	if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true;
236
	if (first_click_in_canvas)
7186 leency 237
	{
7206 leency 238
		if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
239
		if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
240
		if (mouseX
241
		if (mouseY
242
 
7207 leency 243
		if (mouse.key) {
7204 leency 244
			if ((figTool_States[currentFigToolState].startX < 0) || (figTool_States[currentFigToolState].startY < 0)) {
245
				figTool_States[currentFigToolState].startX = mouseX;
246
				figTool_States[currentFigToolState].startY = mouseY;
7186 leency 247
			}
248
			else {
7204 leency 249
				if ((calc(mouseX - canvas.x/zoom.value) != figTool_States[currentFigToolState].lastTempPosX)
250
					|| (calc(mouseY - canvas.y/zoom.value) != figTool_States[currentFigToolState].lastTempPosY))
251
				{
252
					DrawCanvas();
253
				}
7186 leency 254
			}
7206 leency 255
			mouseX_last = mouseX;
256
			mouseY_last = mouseY;
7186 leency 257
		}
7206 leency 258
		if (mouse.up) {
7204 leency 259
			if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0)) {
260
				// Draw line from start position to current position
261
				if (currentTool == TOOL_LINE) {
262
					DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
263
						figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
264
						mouseX - canvas.x/zoom.value,
265
						mouseY - canvas.y/zoom.value,
7207 leency 266
						tool_color,
7204 leency 267
						1);
268
				}
269
				else if (currentTool == TOOL_RECT) {
270
					DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
271
						figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
272
						mouseX - canvas.x/zoom.value,
7207 leency 273
						mouseY - canvas.y/zoom.value, tool_color, 1);
7204 leency 274
				}
275
 
7186 leency 276
				DrawCanvas();
277
 
7204 leency 278
				actionsHistory.saveCurrentState();
7186 leency 279
 
280
				// Reset start position
7204 leency 281
				figTool_States[currentFigToolState].startX = -1;
282
				figTool_States[currentFigToolState].startY = -1;
7206 leency 283
 
284
				first_click_in_canvas = false;
7186 leency 285
			}
286
		}
287
	}
288
}
289
 
7204 leency 290
void SimpleFigureTool_onCanvasDraw() {
7207 leency 291
	if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) {
7204 leency 292
		if (currentTool == TOOL_LINE) {
293
			DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
294
				figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
7206 leency 295
				mouseX_last - canvas.x/zoom.value,
296
				mouseY_last - canvas.y/zoom.value,
7207 leency 297
				tool_color,
7204 leency 298
				2);
299
		}
300
		else if (currentTool == TOOL_RECT) {
301
			DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
302
				figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
7206 leency 303
				mouseX_last - canvas.x/zoom.value,
304
				mouseY_last - canvas.y/zoom.value,
7207 leency 305
				tool_color,
7206 leency 306
				2);
7204 leency 307
		}
308
 
7206 leency 309
		figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value;
310
		figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value;
7186 leency 311
	}
312
}
313
 
314
void initTools()
315
{
7187 leency 316
	tools[0].id = TOOL_PENCIL;
317
	tools[0].onMouseEvent = #PencilTool_onMouseEvent;
7204 leency 318
	tools[0].deactivate = #PencilTool_reset;
7186 leency 319
 
320
	tools[1].id = TOOL_PIPETTE;
321
	tools[1].activate = #PipetteTool_activate;
322
	tools[1].onMouseEvent = #PipetteTool_onMouseEvent;
323
 
7187 leency 324
	tools[2].id = TOOL_FILL;
325
	tools[2].onMouseEvent = #FillTool_onMouseEvent;
7186 leency 326
 
7204 leency 327
	tools[3].id = TOOL_LINE;
328
	tools[3].activate = #SimpleFigureTool_Reset;
329
	tools[3].deactivate = #SimpleFigureTool_Reset;
330
	tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent;
331
	tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw;
7186 leency 332
 
333
	tools[4].id = TOOL_RECT;
7204 leency 334
	tools[4].activate = #SimpleFigureTool_Reset;
335
	tools[4].deactivate = #SimpleFigureTool_Reset;
336
	tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent;
337
	tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw;
7186 leency 338
}
339
 
7089 leency 340
void main()
341
{
342
	word btn;
343
 
7096 leency 344
	load_dll(libio,  #libio_init,  1);
345
	load_dll(libimg, #libimg_init, 1);
346
	Libimg_LoadImage(#skin, "/sys/icons16.png");
7156 leency 347
	//system.color.get();
7187 leency 348
	//Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work);
7186 leency 349
 
7155 leency 350
	image.create(32, 32);
7089 leency 351
 
7156 leency 352
	if (param[0]) {
7151 leency 353
		Libimg_LoadImage(#open_image, #param);
354
		if (open_image.w==32) && (open_image.h==32) {
7156 leency 355
			image.set_image(open_image.imgsrc);
7151 leency 356
		}
357
		else {
7190 leency 358
			notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E");
7151 leency 359
		}
360
	}
361
 
7204 leency 362
	actionsHistory.init();
363
 
7186 leency 364
	initTools();
365
	setCurrentTool(TOOL_PENCIL);
366
 
7096 leency 367
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
368
 
7089 leency 369
	loop() switch(WaitEvent())
370
	{
7096 leency 371
		case evMouse:
372
			mouse.get();
7186 leency 373
 
7207 leency 374
			if (mouse.lkm) tool_color = color1;
375
			if (mouse.pkm) tool_color = color2;
376
			if (mouse.mkm) break;
377
 
7186 leency 378
			if (currentTool != TOOL_NONE)
379
				tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
380
 
7156 leency 381
			if (mouse.vert) {
7152 leency 382
				if (mouse.vert==65535) zoom.click(BTN_ZOOM_IN);
383
				if (mouse.vert==1) zoom.click(BTN_ZOOM_OUT);
384
				DrawEditArea();
385
			}
7186 leency 386
 
7200 leency 387
			if (mouse.down) {
388
				if (b_color_gradient.hovered())
389
				|| (b_last_colors.hovered())
390
				|| (b_default_palette.hovered()) {
391
					if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
392
					if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
393
				}
394
			}
395
 
7148 leency 396
			break;
7096 leency 397
 
7089 leency 398
		case evButton:
7148 leency 399
			btn = GetButtonID();
7096 leency 400
			switch(btn)
401
			{
7151 leency 402
				case BTN_NEW:
7155 leency 403
					image.create(32, 32);
7151 leency 404
					DrawCanvas();
405
					break;
406
				case BTN_OPEN:
7190 leency 407
					RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
7151 leency 408
					break;
409
				case BTN_SAVE:
410
					EventSave();
411
					break;
7096 leency 412
				case BTN_MOVE_LEFT:
7151 leency 413
					image.move(MOVE_LEFT);
414
					DrawCanvas();
7096 leency 415
					break;
416
				case BTN_MOVE_RIGHT:
7151 leency 417
					image.move(MOVE_RIGHT);
418
					DrawCanvas();
7096 leency 419
					break;
7147 leency 420
				case BTN_MOVE_UP:
7151 leency 421
					image.move(MOVE_UP);
422
					DrawCanvas();
7147 leency 423
					break;
424
				case BTN_MOVE_DOWN:
7151 leency 425
					image.move(MOVE_DOWN);
426
					DrawCanvas();
7147 leency 427
					break;
7151 leency 428
				case BTN_FLIP_VER:
429
					image.move(FLIP_VER);
430
					DrawCanvas();
431
					break;
432
				case BTN_FLIP_HOR:
433
					image.move(FLIP_HOR);
434
					DrawCanvas();
435
					break;
7187 leency 436
				case BTN_PENCIL:
437
					setCurrentTool(TOOL_PENCIL);
438
					break;
7096 leency 439
				case BTN_PICK:
7186 leency 440
					setCurrentTool(TOOL_PIPETTE);
441
					//EventPickActivate();
7096 leency 442
					break;
7156 leency 443
				case BTN_FILL:
7186 leency 444
					setCurrentTool(TOOL_FILL);
445
					//EventFillActivate();
7156 leency 446
					break;
7186 leency 447
				case BTN_LINE:
448
					setCurrentTool(TOOL_LINE);
449
					break;
450
				case BTN_RECT:
451
					setCurrentTool(TOOL_RECT);
452
					break;
7152 leency 453
				case BTN_ZOOM_IN:
7186 leency 454
					zoom.click(BTN_ZOOM_IN);
455
					DrawEditArea();
456
					break;
7152 leency 457
				case BTN_ZOOM_OUT:
7186 leency 458
					zoom.click(BTN_ZOOM_OUT);
7152 leency 459
					DrawEditArea();
460
					break;
461
				case CLOSE_BTN:
462
					ExitProcess();
463
					break;
7089 leency 464
			}
465
			break;
466
 
467
		case evKey:
468
			GetKeys();
7186 leency 469
			if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL);
7187 leency 470
			if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
7186 leency 471
			if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
7187 leency 472
			if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL);
473
			if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE);
474
			if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT);
7204 leency 475
 
476
			if (key_scancode == SCAN_CODE_KEY_S) actionsHistory.undoLastAction();
477
			if (key_scancode == SCAN_CODE_KEY_C) actionsHistory.redoLastAction();
478
 
7152 leency 479
			if (key_scancode == SCAN_CODE_MINUS) {zoom.click(BTN_ZOOM_OUT); DrawEditArea();}
480
			if (key_scancode == SCAN_CODE_PLUS)  {zoom.click(BTN_ZOOM_IN);  DrawEditArea();}
7089 leency 481
			break;
482
 
483
		case evReDraw:
484
			draw_window();
485
			break;
486
	}
487
}
488
 
7096 leency 489
void DrawToolbarButton(dword _id, _x, _icon_n)
490
{
7152 leency 491
	DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff);
492
	DefineHiddenButton(_x, 4, 21, 21, _id);
493
	img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16);
7096 leency 494
}
495
 
7187 leency 496
void DrawLeftPanelButton(dword _id, _y, _icon_n)
497
{
498
	int x = 5;
499
	DrawWideRectangle(x, _y, 22, 22, 3, 0xFFFfff);
500
	DefineHiddenButton(x, _y, 21, 21, _id);
501
	img_draw stdcall(skin.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
502
}
503
 
7148 leency 504
void DrawStatusBar()
505
{
7150 leency 506
	zoom.y = wrapper.y + wrapper.h + 6;
507
	zoom.x = wrapper.x;
508
	zoom.draw();
509
 
7151 leency 510
	sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
7150 leency 511
	WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
7148 leency 512
}
513
 
7089 leency 514
void draw_window()
515
{
7096 leency 516
	incn tx;
7089 leency 517
	system.color.get();
7151 leency 518
	DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
7089 leency 519
	GetProcessInfo(#Form, SelfInfo);
7152 leency 520
	if (Form.status_window>2) return;
521
	if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
522
	if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
523
 
7150 leency 524
	right_bar.x = Form.cwidth - right_bar.w;
7200 leency 525
	b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
7089 leency 526
 
7155 leency 527
	tx.n = 10-TB_ICON_PADDING;
7148 leency 528
	DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
529
	DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
7151 leency 530
	DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
7207 leency 531
	DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8), 30);
7096 leency 532
	DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
7147 leency 533
	DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
534
	DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
535
 
7155 leency 536
	DrawToolbarButton(BTN_FLIP_HOR,   tx.inc(TB_ICON_PADDING+8), 34);
537
	DrawToolbarButton(BTN_FLIP_VER,   tx.inc(TB_ICON_PADDING),   35);
538
	// DrawToolbarButton(BTN_ROTATE_LEFT,   tx.inc(TB_ICON_PADDING), 36); //not implemented
539
	// DrawToolbarButton(BTN_ROTATE_RIGHT,  tx.inc(TB_ICON_PADDING), 37); //not implemented
7148 leency 540
 
7187 leency 541
	DrawLeftPanel();
7186 leency 542
 
7150 leency 543
	DrawEditArea();
7096 leency 544
 
7155 leency 545
	DrawActiveColor(right_bar.y);
7200 leency 546
	DrawColorPallets();
7089 leency 547
 
7148 leency 548
	DrawStatusBar();
7089 leency 549
}
550
 
7187 leency 551
void DrawLeftPanel()
552
{
553
	incn ty;
554
	ty.n = TOOLBAR_H-TB_ICON_PADDING;
555
	DrawLeftPanelButton(BTN_PENCIL, ty.inc(TB_ICON_PADDING), 38);
556
	DrawLeftPanelButton(BTN_PICK,   ty.inc(TB_ICON_PADDING), 39);
557
	DrawLeftPanelButton(BTN_FILL,   ty.inc(TB_ICON_PADDING), 40);
558
	DrawLeftPanelButton(BTN_LINE,   ty.inc(TB_ICON_PADDING), 41);
559
	DrawLeftPanelButton(BTN_RECT,   ty.inc(TB_ICON_PADDING), 42);
560
	DrawRectangle3D(5, currentTool*TB_ICON_PADDING+TOOLBAR_H, 16+3+2, 16+3+2, 0x333333, 0x777777);
561
}
562
 
7151 leency 563
void DrawEditArea()
7089 leency 564
{
7150 leency 565
	dword color1=0xC0C0C0;
7155 leency 566
	int top_side;
567
	int left_side;
7089 leency 568
 
7194 leency 569
	wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
7150 leency 570
	wrapper.h = Form.cheight - TOOLBAR_H - 35;
7148 leency 571
 
7150 leency 572
	//canvas{
7151 leency 573
	canvas.w = image.columns * zoom.value;
574
	canvas.h = image.rows * zoom.value;
575
	if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
7154 leency 576
		zoom.click(BTN_ZOOM_OUT);
7151 leency 577
		DrawEditArea();
578
		return;
579
	}
580
	canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
581
	canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
582
	DrawCanvas();
7150 leency 583
	//}
7089 leency 584
 
7155 leency 585
	left_side = canvas.x-wrapper.x-1;
586
	top_side = canvas.y-wrapper.y-1;
7148 leency 587
 
7155 leency 588
	DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
7150 leency 589
 
7155 leency 590
	if (left_side>0)
7150 leency 591
	{
7155 leency 592
		DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
593
		DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
7150 leency 594
	}
7155 leency 595
	if (top_side>0)
7150 leency 596
	{
7190 leency 597
		//left
598
		DrawBar(wrapper.x, wrapper.y+top_side, left_side,
599
			wrapper.h-top_side-top_side, color1);
600
		//right
601
		DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
602
			wrapper.h-top_side-top_side, color1);
7150 leency 603
	}
7194 leency 604
	DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
7089 leency 605
}
606
 
607
void DrawActiveColor(dword iny)
608
{
609
	static dword outy;
610
	if (iny != NULL) outy = iny;
7207 leency 611
	DrawBar(right_bar.x, outy, 20, 20, color1);
612
	sprintf(#param, "%A", color1);
7089 leency 613
	EDI = system.color.work;
7148 leency 614
	WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7151 leency 615
 
7207 leency 616
	DrawBar(right_bar.x+110, outy, 20, 20, color2);
617
	sprintf(#param, "%A", color2);
7151 leency 618
	EDI = system.color.work;
619
	WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
7200 leency 620
	DrawCurrentColorGradientByLightness();
7089 leency 621
}
622
 
7200 leency 623
void DrawCurrentColorGradientByLightness()
7194 leency 624
{
625
	int i;
626
	int w = right_bar.w-10/2;
627
	for (i=0; i
7200 leency 628
		DrawBar(b_color_gradient.x+i, b_color_gradient.y,
7207 leency 629
			1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w));
7194 leency 630
	for (i=0 ; i<=w; i++)
7200 leency 631
		DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y,
7207 leency 632
			1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w));
7194 leency 633
}
634
 
7200 leency 635
void DrawColorPallets()
7089 leency 636
{
7155 leency 637
	int r, c, i=0;
638
	//Last used colors
639
	for (r = 0; r < 2; r++)
640
	{
7194 leency 641
		for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
7155 leency 642
		{
7200 leency 643
			DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
644
				COLSIZE, COLSIZE, last_used_colors[i]);
7155 leency 645
		}
646
	}
647
	i=0;
648
	//Default colors
7089 leency 649
	for (r = 0; r < 9; r++)
650
	{
7194 leency 651
		for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
7089 leency 652
		{
7200 leency 653
			DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
654
				COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
7089 leency 655
		}
656
	}
657
}
658
 
7155 leency 659
void DrawCanvas()
660
{
661
	int r, c;
662
	for (r = 0; r < image.rows; r++)
663
	{
664
		for (c = 0; c < image.columns; c++)
665
		{
666
			DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
667
				zoom.value, zoom.value, image.get_pixel(r, c));
668
		}
669
	}
7186 leency 670
 
671
	if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
672
		tools[currentTool].onCanvasDraw();
673
 
7200 leency 674
	DrawPreview();
7155 leency 675
}
676
 
7200 leency 677
void DrawPreview()
678
{
679
	int x = right_bar.x;
680
	int y = wrapper.y + wrapper.h - image.rows-2;
681
	DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph);
682
	_PutImage(x+1,y+1, image.columns, image.rows, image.get_image());
683
}
7155 leency 684
 
7200 leency 685
dword GetPixelUnderMouse()
686
{
687
	return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
688
}
689
 
7089 leency 690
//===================================================//
691
//                                                   //
692
//                      EVENTS                       //
693
//                                                   //
694
//===================================================//
695
 
7151 leency 696
void EventSave()
7150 leency 697
{
7190 leency 698
	dword encoded_data=0;
699
	dword encoded_size=0;
700
	dword image_ptr = 0;
701
 
702
	image_ptr = create_image(Image_bpp24, 32, 32);
703
 
704
	if (image_ptr == 0) {
705
		notify("'Error saving file, probably not enought memory!' -E");
7150 leency 706
	}
7151 leency 707
	else {
7190 leency 708
		EDI = image_ptr;
709
		memmov(EDI._Image.Data, image.get_image(), image.rows * image.columns * 3);
710
 
711
		encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
712
 
713
		img_destroy stdcall(image_ptr);
714
 
715
		if(encoded_data == 0) {
716
			notify("'Error saving file, incorrect data!' -E");
717
		}
718
		else {
7227 leency 719
			if (CreateFile(encoded_size, encoded_data, "/rd/1/saved_image.png") == 0) {
7190 leency 720
				notify("'File saved as /rd/1/saved_image.png' -O");
721
			}
722
			else {
723
				notify("'Error saving file, probably not enought space on ramdisk!' -E");
724
			}
725
		}
7151 leency 726
	}
7150 leency 727
}
7151 leency 728
 
7155 leency 729
void EventSetActiveColor(int _number, _color)
7151 leency 730
{
731
	int i;
7155 leency 732
	for (i=13*2-1; i>0; i--) {
733
		last_used_colors[i] = last_used_colors[i-1];
734
	}
735
	last_used_colors[0] = _color;
7151 leency 736
 
7207 leency 737
	if (_number == 1) color1 = _color;
738
	if (_number == 2) color2 = _color;
7155 leency 739
 
740
	DrawActiveColor(NULL);
7200 leency 741
	DrawColorPallets();
7156 leency 742
}
743
 
744
void EventFill(dword _r, _c, _color)
745
{
746
	#define MARKED 6
747
	int r, c, i, restart;
748
 
749
	dword old_color = image.get_pixel(_r, _c);
750
	image.set_pixel(_r, _c, MARKED);
751
 
752
	do {
753
		restart=false;
754
		for (r = 0; r < image.rows; r++)
755
			for (c = 0; c < image.columns; c++)
756
			{
757
				IF (image.get_pixel(r,c) != old_color) continue;
758
				IF (image.get_pixel(r,c) == MARKED) continue;
759
 
7194 leency 760
				IF (c>0)               && (image.get_pixel(r,c-1) == MARKED) image.set_pixel(r,c,MARKED);
761
				IF (r>0)               && (image.get_pixel(r-1,c) == MARKED) image.set_pixel(r,c,MARKED);
762
				IF (c
763
				IF (r
7156 leency 764
 
7194 leency 765
				IF (image.get_pixel(r,c)==MARKED) restart=true;
7156 leency 766
			}
767
	}while(restart);
768
 
769
	for (i=0; i
770
			IF (image.mas[i]==MARKED) image.mas[i] = _color;
771
}
772
 
7186 leency 773
// target - image (1) or canvas (2)
774
void DrawLine(int x1, int y1, int x2, int y2, dword color, int target) {
775
	int dx, dy, signX, signY, error, error2;
776
 
777
   dx = x2 - x1;
778
 
779
   if (dx < 0)
780
	   dx = -dx;
781
 
782
   dy = y2 - y1;
783
 
784
   if (dy < 0)
785
	   dy = -dy;
786
 
787
   if (x1 < x2)
788
	   signX = 1;
789
   else
790
	   signX = -1;
791
 
792
   if (y1 < y2)
793
	   signY = 1;
794
   else
795
	   signY = -1;
796
 
797
   error = dx - dy;
798
 
799
	if (target == 1)
800
		image.set_pixel(y2, x2, color);
801
	else
802
		DrawBar(x2*zoom.value + canvas.x, y2*zoom.value + canvas.y,
803
				zoom.value, zoom.value, color);
804
 
805
   while((x1 != x2) || (y1 != y2))
806
  {
807
		if (target == 1)
808
			image.set_pixel(y1, x1, color);
809
		else
810
			DrawBar(x1*zoom.value + canvas.x, y1*zoom.value + canvas.y,
811
				zoom.value, zoom.value, color);
812
 
813
	   error2 = error * 2;
814
 
815
       if(error2 > calc(-dy))
816
       {
817
           error -= dy;
818
           x1 += signX;
819
       }
820
 
821
       if(error2 < dx)
822
       {
823
           error += dx;
824
           y1 += signY;
825
       }
826
   }
827
 
828
}
829
 
830
void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) {
831
	DrawLine(x1, y1, x2, y1, color, target);
832
	DrawLine(x2, y1, x2, y2, color, target);
833
	DrawLine(x2, y2, x1, y2, color, target);
834
	DrawLine(x1, y2, x1, y1, color, target);
835
}