2018-05-03 15:40:26 +02:00
|
|
|
//===================================================//
|
|
|
|
// //
|
2018-05-06 22:31:36 +02:00
|
|
|
// SELECTION //
|
2018-05-03 15:40:26 +02:00
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
struct _selection
|
|
|
|
{
|
|
|
|
_image buf;
|
|
|
|
int state;
|
|
|
|
|
|
|
|
int start_x;
|
|
|
|
int start_y;
|
|
|
|
int end_x;
|
|
|
|
int end_y;
|
|
|
|
|
|
|
|
int pivot_x;
|
|
|
|
int pivot_y;
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
bool is_point_in_selection();
|
|
|
|
void copy_to_buf();
|
|
|
|
void move_to_point();
|
|
|
|
void apply_to_image();
|
|
|
|
} selection;
|
|
|
|
|
|
|
|
void _selection::reset() {
|
|
|
|
start_x = -1;
|
|
|
|
start_y = -1;
|
|
|
|
end_x = -1;
|
|
|
|
end_y = -1;
|
|
|
|
pivot_x = -1;
|
|
|
|
pivot_y = -1;
|
|
|
|
state = STATE_INACTIVE;
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
bool _selection::is_point_in_selection(int x, int y) {
|
|
|
|
if (x >= start_x) && (x <= end_x)
|
|
|
|
&& (y >= start_y) && (y <= end_y)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
void _selection::copy_to_buf() {
|
|
|
|
dword r, c;
|
2018-05-03 15:40:26 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
// Normalize: Restructuring of the selection coordinates
|
|
|
|
if (end_x < start_x) {
|
|
|
|
start_x >< end_x;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
2018-05-06 22:31:36 +02:00
|
|
|
if (end_y < start_y) {
|
|
|
|
end_y >< start_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = STATE_SELECTED;
|
|
|
|
buf.rows = end_y - start_y + 1;
|
|
|
|
buf.columns = end_x - start_x + 1;
|
|
|
|
|
|
|
|
for (r = start_y; r <= end_y; r++) {
|
|
|
|
for (c = start_x; c <= end_x; c++) {
|
|
|
|
buf.set_pixel(r - start_y, c - start_x, image.get_pixel(r, c) );
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
void _selection::move_to_point(int _new_x, _new_y)
|
|
|
|
{
|
|
|
|
start_x = _new_x;
|
|
|
|
start_y = _new_y;
|
|
|
|
end_x = buf.columns - 1 + start_x;
|
|
|
|
end_y = buf.rows - 1 + start_y;
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
void _selection::apply_to_image() {
|
|
|
|
dword r, c;
|
|
|
|
for (r = 0; r <= buf.rows - 1; r++) {
|
|
|
|
for (c = 0; c <= buf.columns - 1; c++) {
|
|
|
|
image.set_pixel(r + start_y, c + start_x, buf.get_pixel(r, c) );
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 17:02:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
//===================================================//
|
|
|
|
// //
|
|
|
|
// EVENTS //
|
|
|
|
// //
|
|
|
|
//===================================================//
|
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
void ApplySelectionToImage() {
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
if (STATE_SELECTED != selection.state) return;
|
2018-05-06 17:02:12 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
selection.apply_to_image();
|
2018-05-08 20:35:13 +02:00
|
|
|
selection.reset();
|
2018-05-06 17:02:12 +02:00
|
|
|
|
|
|
|
actionsHistory.saveCurrentState();
|
|
|
|
DrawCanvas();
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_selection_moving() {
|
2018-05-06 22:31:36 +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 SelectTool_activate() {
|
2018-05-06 22:31:36 +02:00
|
|
|
ApplySelectionToImage();
|
|
|
|
selection.reset();
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelectTool_deactivate() {
|
2018-05-06 17:02:12 +02:00
|
|
|
ApplySelectionToImage();
|
2018-05-06 22:31:36 +02:00
|
|
|
selection.reset();
|
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-05-06 22:31:36 +02:00
|
|
|
if (selection.start_x != -1) && (selection.is_point_in_selection(m_x, m_y)) {
|
|
|
|
if (selection.pivot_x == -1) {
|
|
|
|
selection.pivot_x = m_x;
|
|
|
|
selection.pivot_y = m_y;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
|
|
|
GetKeys();
|
2018-05-06 17:02:12 +02:00
|
|
|
if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
|
2018-05-06 22:31:36 +02:00
|
|
|
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-06 22:31:36 +02:00
|
|
|
selection.state = STATE_SELECTED;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-03 15:40:26 +02:00
|
|
|
else {
|
2018-05-06 22:31:36 +02:00
|
|
|
ApplySelectionToImage();
|
|
|
|
selection.state = STATE_CHOSING;
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
//Drag selection
|
|
|
|
if (STATE_SELECTED == selection.state)
|
|
|
|
{
|
|
|
|
if (selection.pivot_x != -1) {
|
|
|
|
dx = m_x - selection.pivot_x;
|
|
|
|
dy = m_y - selection.pivot_y;
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
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.move_to_point(selection.start_x + dx, selection.start_y + dy);
|
|
|
|
|
|
|
|
selection.pivot_x += dx;
|
|
|
|
selection.pivot_y += dy;
|
|
|
|
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
|
|
|
if (mouse.up) {
|
|
|
|
if (selection.pivot_x != -1) {
|
|
|
|
selection.pivot_x = -1;
|
|
|
|
selection.pivot_y = -1;
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:31:36 +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) {
|
2018-05-06 22:31:36 +02:00
|
|
|
selection.end_x = TO_CANVAS_X(mouseX);
|
|
|
|
selection.end_y = TO_CANVAS_Y(mouseY);
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
if ((selection.start_x < 0) || (selection.start_y < 0)) {
|
|
|
|
selection.start_x = TO_CANVAS_X(mouseX);
|
|
|
|
selection.start_y = TO_CANVAS_Y(mouseY);
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
DrawCanvas();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
if (mouse.up) {
|
|
|
|
selection.copy_to_buf();
|
2018-04-24 17:14:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-10-01 18:41:14 +02:00
|
|
|
DrawBarIcon(selection.start_x, selection.start_y, selection.end_x, selection.end_y, color2, TOIMAGE);
|
2018-05-06 22:31:36 +02:00
|
|
|
selection.reset();
|
2018-05-06 17:02:12 +02:00
|
|
|
DrawCanvas();
|
|
|
|
}
|
2018-04-24 17:14:08 +02:00
|
|
|
|
2018-05-06 17:02:12 +02:00
|
|
|
if (SCAN_CODE_ESC == keycode) {
|
2018-05-06 22:31:36 +02:00
|
|
|
ApplySelectionToImage();
|
2018-05-06 17:02:12 +02:00
|
|
|
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-06 22:31:36 +02:00
|
|
|
if (STATE_SELECTED == selection.state)
|
|
|
|
{
|
|
|
|
selection.move_to_point(0, 0);
|
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 22:31:36 +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 22:31:36 +02:00
|
|
|
if (STATE_SELECTED == selection.state)
|
|
|
|
|| (STATE_CHOSING == selection.state)
|
|
|
|
{
|
|
|
|
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 22:31:36 +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);
|
|
|
|
|
2018-05-06 22:31:36 +02:00
|
|
|
if (STATE_SELECTED == selection.state) && (selection.is_point_in_selection(c, r)) {
|
|
|
|
old_color = selection.buf.get_pixel(r - selection.start_y, c - selection.start_x);
|
2018-05-06 17:02:12 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|