Subversion Repositories Kolibri OS

Rev

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