Subversion Repositories Kolibri OS

Rev

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