2018-05-03 15:40:26 +02:00
|
|
|
//===================================================//
|
|
|
|
// //
|
|
|
|
// DATA //
|
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
_image selection;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
STATE_INACTIVE=0,
|
|
|
|
STATE_CHOSING=1,
|
2018-05-03 16:46:27 +02:00
|
|
|
STATE_SELECTED=2
|
2018-05-03 15:40:26 +02:00
|
|
|
};
|
|
|
|
int selection_state = STATE_INACTIVE;
|
|
|
|
|
2018-04-24 17:14:08 +02:00
|
|
|
int selection_start_x = -1;
|
|
|
|
int selection_start_y = -1;
|
|
|
|
int selection_end_x = -1;
|
|
|
|
int selection_end_y = -1;
|
|
|
|
|
|
|
|
int selection_pivot_x = -1;
|
|
|
|
int selection_pivot_y = -1;
|
|
|
|
|
2018-05-03 15:40:26 +02:00
|
|
|
//===================================================//
|
|
|
|
// //
|
|
|
|
// CODE //
|
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
2018-04-24 17:14:08 +02:00
|
|
|
void SelectTool_normalizeSelection() {
|
|
|
|
// Restructuring of the selection coordinates
|
|
|
|
if (selection_end_x < selection_start_x) {
|
2018-05-03 16:46:27 +02:00
|
|
|
selection_start_x >< selection_end_x;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
if (selection_end_y < selection_start_y) {
|
2018-05-03 16:46:27 +02:00
|
|
|
selection_end_y >< selection_start_y;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
void SelectTool_drawBufferToImage(int insert_x, int insert_y) {
|
|
|
|
dword r, c;
|
|
|
|
dword insert_to_x, insert_to_y;
|
|
|
|
|
|
|
|
insert_to_x = math.min(insert_x + selection.columns - 1, image.columns-1);
|
|
|
|
insert_to_y = math.min(insert_y + selection.rows - 1, image.rows-1);
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
for (r = insert_y; r <= insert_to_y; r++) {
|
|
|
|
for (c = insert_x; c <= insert_to_x; c++) {
|
|
|
|
image.set_pixel(r, c, selection.get_pixel(r - insert_y, c - insert_x) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplySelectionToImage() {
|
|
|
|
if (STATE_SELECTED != selection_state) return;
|
|
|
|
|
|
|
|
SelectTool_drawBufferToImage(selection_start_x, selection_start_y);
|
|
|
|
|
|
|
|
selection_pivot_x = -1;
|
|
|
|
selection_pivot_y = -1;
|
|
|
|
|
|
|
|
actionsHistory.saveCurrentState();
|
|
|
|
DrawCanvas();
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_selection_moving() {
|
2018-05-03 16:46:27 +02:00
|
|
|
if (STATE_SELECTED == selection_state) return true;
|
2018-05-03 15:40:26 +02:00
|
|
|
return false;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_selection() {
|
2018-05-06 17:02:12 +02:00
|
|
|
ApplySelectionToImage();
|
2018-04-24 17:14:08 +02:00
|
|
|
|
|
|
|
selection_start_x = -1;
|
|
|
|
selection_start_y = -1;
|
|
|
|
selection_end_x = -1;
|
|
|
|
selection_end_y = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectTool_activate() {
|
|
|
|
reset_selection();
|
2018-05-03 15:40:26 +02:00
|
|
|
selection_state = STATE_INACTIVE;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelectTool_deactivate() {
|
2018-05-06 17:02:12 +02:00
|
|
|
ApplySelectionToImage();
|
2018-05-03 15:40:26 +02:00
|
|
|
selection_state = STATE_INACTIVE;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SelectTool_pointInSelection(int x, int y) {
|
|
|
|
if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SelectTool_copyToBuffer() {
|
2018-05-03 15:40:26 +02:00
|
|
|
dword r, c;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-03 15:40:26 +02:00
|
|
|
selection_state = STATE_SELECTED;
|
|
|
|
selection.rows = selection_end_y - selection_start_y + 1;
|
|
|
|
selection.columns = selection_end_x - selection_start_x + 1;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-03 15:40:26 +02:00
|
|
|
for (r = selection_start_y; r <= selection_end_y; r++) {
|
|
|
|
for (c = selection_start_x; c <= selection_end_x; c++) {
|
|
|
|
selection.set_pixel(r - selection_start_y, c - selection_start_x, image.get_pixel(r, c) );
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
2018-05-03 15:40:26 +02:00
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
|
2018-05-03 16:46:27 +02:00
|
|
|
int dx, dy, m_x, m_y;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
|
|
|
m_x = TO_CANVAS_X(mouseX);
|
|
|
|
m_y = TO_CANVAS_Y(mouseY);
|
2018-05-03 15:40:26 +02:00
|
|
|
|
2018-05-03 16:46:27 +02:00
|
|
|
if (mouse.down) && (canvas.hovered())
|
2018-05-03 15:40:26 +02:00
|
|
|
{
|
2018-04-24 17:14:08 +02:00
|
|
|
if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) {
|
|
|
|
if (selection_pivot_x == -1) {
|
|
|
|
selection_pivot_x = m_x;
|
|
|
|
selection_pivot_y = m_y;
|
|
|
|
|
|
|
|
GetKeys();
|
2018-05-06 17:02:12 +02:00
|
|
|
if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
|
|
|
|
DrawBarIcon(selection_start_x, selection_start_y, selection_end_x,
|
|
|
|
selection_end_y, color2, TOIMAGE);
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
2018-05-03 15:40:26 +02:00
|
|
|
|
2018-05-03 16:46:27 +02:00
|
|
|
selection_state = STATE_SELECTED;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-03 15:40:26 +02:00
|
|
|
else {
|
2018-04-24 17:14:08 +02:00
|
|
|
reset_selection();
|
2018-05-03 15:40:26 +02:00
|
|
|
selection_state = STATE_CHOSING;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selection_pivot_x != -1) {
|
|
|
|
dx = m_x - selection_pivot_x;
|
|
|
|
dy = m_y - selection_pivot_y;
|
|
|
|
|
|
|
|
if (selection_start_x + dx < 0)
|
|
|
|
dx = selection_start_x;
|
|
|
|
|
|
|
|
if (selection_end_x + dx >= image.columns)
|
|
|
|
dx = image.columns-1 - selection_end_x;
|
|
|
|
|
|
|
|
if (selection_start_y + dy < 0)
|
|
|
|
dy = selection_start_y;
|
|
|
|
|
|
|
|
if (selection_end_y + dy >= image.rows)
|
|
|
|
dy = image.rows-1 - selection_end_y;
|
|
|
|
|
|
|
|
selection_start_x += dx;
|
|
|
|
selection_end_x += dx;
|
|
|
|
|
|
|
|
selection_start_y += dy;
|
|
|
|
selection_end_y += dy;
|
|
|
|
|
|
|
|
selection_pivot_x += dx;
|
|
|
|
selection_pivot_y += dy;
|
|
|
|
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:40:26 +02:00
|
|
|
if (STATE_CHOSING == selection_state)
|
2018-04-24 17:14:08 +02:00
|
|
|
{
|
2018-05-06 17:02:12 +02:00
|
|
|
mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value);
|
|
|
|
mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value);
|
2018-04-24 17:14:08 +02:00
|
|
|
|
|
|
|
if (mouse.key) {
|
|
|
|
selection_end_x = TO_CANVAS_X(mouseX);
|
|
|
|
selection_end_y = TO_CANVAS_Y(mouseY);
|
|
|
|
|
|
|
|
if ((selection_start_x < 0) || (selection_start_y < 0)) {
|
|
|
|
selection_start_x = TO_CANVAS_X(mouseX);
|
|
|
|
selection_start_y = TO_CANVAS_Y(mouseY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse.up) {
|
|
|
|
SelectTool_normalizeSelection();
|
|
|
|
SelectTool_copyToBuffer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mouse.up) {
|
|
|
|
if (selection_pivot_x != -1) {
|
|
|
|
selection_pivot_x = -1;
|
|
|
|
selection_pivot_y = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
void SelectTool_onKeyEvent(dword keycode) {
|
2018-05-03 15:40:26 +02:00
|
|
|
dword r, c;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
if (SCAN_CODE_DEL == keycode) {
|
|
|
|
selection_start_x = -1;
|
|
|
|
selection_start_y = -1;
|
|
|
|
selection_end_x = -1;
|
|
|
|
selection_end_y = -1;
|
|
|
|
selection_state = STATE_INACTIVE;
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
if (SCAN_CODE_ESC == keycode) {
|
|
|
|
reset_selection();
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
if (SCAN_CODE_KEY_V == keycode) {
|
2018-05-03 15:40:26 +02:00
|
|
|
if (STATE_SELECTED == selection_state) {
|
2018-05-06 17:02:12 +02:00
|
|
|
|
2018-04-24 17:14:08 +02:00
|
|
|
selection_start_x = 0;
|
|
|
|
selection_start_y = 0;
|
2018-05-06 17:02:12 +02:00
|
|
|
selection_end_x = selection.columns - 1;
|
2018-05-03 15:40:26 +02:00
|
|
|
selection_end_y = selection.rows - 1;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
void SelectTool_onCanvasDraw()
|
|
|
|
{
|
2018-04-24 17:14:08 +02:00
|
|
|
#define SELECTION_COLOR 0xAAE5EF
|
|
|
|
int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
|
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) {
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
p1x = math.min(selection_start_x, selection_end_x);
|
|
|
|
p2x = math.max(selection_start_x, selection_end_x);
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
p1y = math.min(selection_start_y, selection_end_y);
|
|
|
|
p2y = math.max(selection_start_y, selection_end_y);
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
for (r = p1y; r <= p2y; r++) {
|
|
|
|
for (c = p1x; c <= p2x; c++) {
|
|
|
|
image.pixel_state.set_drawable_state(r, c, false);
|
|
|
|
|
|
|
|
if (STATE_SELECTED == selection_state) && (SelectTool_pointInSelection(c, r)) {
|
|
|
|
old_color = selection.get_pixel(r - selection_start_y, c - selection_start_x);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
old_color = image.get_pixel(r, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_color = MixColors(old_color, SELECTION_COLOR, 64);
|
|
|
|
|
|
|
|
DrawCanvasPixel(r, c, new_color);
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
2018-05-06 17:02:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|