Subversion Repositories Kolibri OS

Rev

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