Subversion Repositories Kolibri OS

Rev

Rev 7229 | Rev 7253 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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