Subversion Repositories Kolibri OS

Rev

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