IconEdit 0.54.2: completely rework selection

git-svn-id: svn://kolibrios.org@7273 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2018-05-06 20:31:36 +00:00
parent cd1cb39d43
commit 2f45ac2c8a
2 changed files with 160 additions and 157 deletions

View File

@ -32,7 +32,7 @@ pipet aside color view
// // // //
//===================================================// //===================================================//
#define T_TITLE "Icon Editor 0.54.1 Alpha" #define T_TITLE "Icon Editor 0.54.2 Alpha"
#define TOPBAR_H 24+8 #define TOPBAR_H 24+8
#define LEFTBAR_W 16+5+5+3+3 #define LEFTBAR_W 16+5+5+3+3
@ -656,8 +656,8 @@ void EventTestIcon()
void EventMove(dword _action) void EventMove(dword _action)
{ {
if (selection_state) { if (selection.state) {
selection.move(_action); selection.buf.move(_action);
SelectTool_onCanvasDraw(); SelectTool_onCanvasDraw();
} }
else { else {

View File

@ -1,114 +1,124 @@
//===================================================// //===================================================//
// // // //
// DATA // // SELECTION //
// // // //
//===================================================// //===================================================//
_image selection;
enum { enum {
STATE_INACTIVE=0, STATE_INACTIVE=0,
STATE_CHOSING=1, STATE_CHOSING=1,
STATE_SELECTED=2 STATE_SELECTED=2
}; };
int selection_state = STATE_INACTIVE;
int selection_start_x = -1; struct _selection
int selection_start_y = -1; {
int selection_end_x = -1; _image buf;
int selection_end_y = -1; int state;
int selection_pivot_x = -1; int start_x;
int selection_pivot_y = -1; int start_y;
int end_x;
int end_y;
//===================================================// int pivot_x;
// // int pivot_y;
// CODE //
// //
//===================================================//
void SelectTool_normalizeSelection() { void reset();
// Restructuring of the selection coordinates bool is_point_in_selection();
if (selection_end_x < selection_start_x) { void copy_to_buf();
selection_start_x >< selection_end_x; void move_to_point();
} void apply_to_image();
if (selection_end_y < selection_start_y) { } selection;
selection_end_y >< selection_start_y;
} void _selection::reset() {
start_x = -1;
start_y = -1;
end_x = -1;
end_y = -1;
pivot_x = -1;
pivot_y = -1;
state = STATE_INACTIVE;
} }
void SelectTool_drawBufferToImage(int insert_x, int insert_y) { bool _selection::is_point_in_selection(int x, int y) {
dword r, c; if (x >= start_x) && (x <= end_x)
dword insert_to_x, insert_to_y; && (y >= start_y) && (y <= end_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);
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();
}
bool is_selection_moving() {
if (STATE_SELECTED == selection_state) return true;
return false;
}
void reset_selection() {
ApplySelectionToImage();
selection_start_x = -1;
selection_start_y = -1;
selection_end_x = -1;
selection_end_y = -1;
}
void SelectTool_activate() {
reset_selection();
selection_state = STATE_INACTIVE;
}
void SelectTool_deactivate() {
ApplySelectionToImage();
selection_state = STATE_INACTIVE;
}
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; return true;
else else
return false; return false;
} }
void _selection::copy_to_buf() {
void SelectTool_copyToBuffer() {
dword r, c; dword r, c;
selection_state = STATE_SELECTED; // Normalize: Restructuring of the selection coordinates
selection.rows = selection_end_y - selection_start_y + 1; if (end_x < start_x) {
selection.columns = selection_end_x - selection_start_x + 1; start_x >< end_x;
}
if (end_y < start_y) {
end_y >< start_y;
}
for (r = selection_start_y; r <= selection_end_y; r++) { state = STATE_SELECTED;
for (c = selection_start_x; c <= selection_end_x; c++) { buf.rows = end_y - start_y + 1;
selection.set_pixel(r - selection_start_y, c - selection_start_x, image.get_pixel(r, c) ); 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) );
} }
} }
} }
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;
}
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) );
}
}
}
//===================================================//
// //
// EVENTS //
// //
//===================================================//
void ApplySelectionToImage() {
if (STATE_SELECTED != selection.state) return;
selection.apply_to_image();
actionsHistory.saveCurrentState();
DrawCanvas();
selection.reset();
}
bool is_selection_moving() {
if (STATE_SELECTED == selection.state) return true;
return false;
}
void SelectTool_activate() {
ApplySelectionToImage();
selection.reset();
}
void SelectTool_deactivate() {
ApplySelectionToImage();
selection.reset();
}
void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
int dx, dy, m_x, m_y; int dx, dy, m_x, m_y;
@ -117,83 +127,80 @@ void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
if (mouse.down) && (canvas.hovered()) if (mouse.down) && (canvas.hovered())
{ {
if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) { if (selection.start_x != -1) && (selection.is_point_in_selection(m_x, m_y)) {
if (selection_pivot_x == -1) { if (selection.pivot_x == -1) {
selection_pivot_x = m_x; selection.pivot_x = m_x;
selection_pivot_y = m_y; selection.pivot_y = m_y;
GetKeys(); GetKeys();
if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) { if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
DrawBarIcon(selection_start_x, selection_start_y, selection_end_x, DrawBarIcon(selection.start_x, selection.start_y, selection.end_x,
selection_end_y, color2, TOIMAGE); selection.end_y, color2, TOIMAGE);
} }
selection_state = STATE_SELECTED; selection.state = STATE_SELECTED;
} }
} }
else { else {
reset_selection(); ApplySelectionToImage();
selection_state = STATE_CHOSING; selection.state = STATE_CHOSING;
} }
} }
if (selection_pivot_x != -1) { //Drag selection
dx = m_x - selection_pivot_x; if (STATE_SELECTED == selection.state)
dy = m_y - selection_pivot_y; {
if (selection.pivot_x != -1) {
dx = m_x - selection.pivot_x;
dy = m_y - selection.pivot_y;
if (selection_start_x + dx < 0) if (selection.start_x + dx < 0)
dx = selection_start_x; dx = selection.start_x;
if (selection_end_x + dx >= image.columns) if (selection.end_x + dx >= image.columns)
dx = image.columns-1 - selection_end_x; dx = image.columns-1 - selection.end_x;
if (selection_start_y + dy < 0) if (selection.start_y + dy < 0)
dy = selection_start_y; dy = selection.start_y;
if (selection_end_y + dy >= image.rows) if (selection.end_y + dy >= image.rows)
dy = image.rows-1 - selection_end_y; dy = image.rows-1 - selection.end_y;
selection_start_x += dx; selection.move_to_point(selection.start_x + dx, selection.start_y + dy);
selection_end_x += dx;
selection_start_y += dy; selection.pivot_x += dx;
selection_end_y += dy; selection.pivot_y += dy;
selection_pivot_x += dx;
selection_pivot_y += dy;
DrawCanvas(); DrawCanvas();
} }
if (mouse.up) {
if (selection.pivot_x != -1) {
selection.pivot_x = -1;
selection.pivot_y = -1;
}
}
}
if (STATE_CHOSING == selection_state) if (STATE_CHOSING == selection.state)
{ {
mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value); mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value);
mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value); mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value);
if (mouse.key) { if (mouse.key) {
selection_end_x = TO_CANVAS_X(mouseX); selection.end_x = TO_CANVAS_X(mouseX);
selection_end_y = TO_CANVAS_Y(mouseY); selection.end_y = TO_CANVAS_Y(mouseY);
if ((selection_start_x < 0) || (selection_start_y < 0)) { if ((selection.start_x < 0) || (selection.start_y < 0)) {
selection_start_x = TO_CANVAS_X(mouseX); selection.start_x = TO_CANVAS_X(mouseX);
selection_start_y = TO_CANVAS_Y(mouseY); selection.start_y = TO_CANVAS_Y(mouseY);
} }
else { else {
DrawCanvas(); DrawCanvas();
} }
} }
if (mouse.up) { if (mouse.up) {
SelectTool_normalizeSelection(); selection.copy_to_buf();
SelectTool_copyToBuffer();
}
}
if (mouse.up) {
if (selection_pivot_x != -1) {
selection_pivot_x = -1;
selection_pivot_y = -1;
} }
} }
} }
@ -202,27 +209,19 @@ void SelectTool_onKeyEvent(dword keycode) {
dword r, c; dword r, c;
if (SCAN_CODE_DEL == keycode) { if (SCAN_CODE_DEL == keycode) {
selection_start_x = -1; selection.reset();
selection_start_y = -1;
selection_end_x = -1;
selection_end_y = -1;
selection_state = STATE_INACTIVE;
DrawCanvas(); DrawCanvas();
} }
if (SCAN_CODE_ESC == keycode) { if (SCAN_CODE_ESC == keycode) {
reset_selection(); ApplySelectionToImage();
DrawCanvas(); DrawCanvas();
} }
if (SCAN_CODE_KEY_V == keycode) { if (SCAN_CODE_KEY_V == keycode) {
if (STATE_SELECTED == selection_state) { if (STATE_SELECTED == selection.state)
{
selection_start_x = 0; selection.move_to_point(0, 0);
selection_start_y = 0;
selection_end_x = selection.columns - 1;
selection_end_y = selection.rows - 1;
DrawCanvas(); DrawCanvas();
} }
} }
@ -233,20 +232,24 @@ void SelectTool_onCanvasDraw()
#define SELECTION_COLOR 0xAAE5EF #define SELECTION_COLOR 0xAAE5EF
int p1x, p1y, p2x, p2y, r, c, old_color, new_color; int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) { //if ((selection.start_x >= 0) && (selection.start_y >= 0)
//&& (selection.end_x >= 0) && (selection.end_y >= 0)) {
p1x = math.min(selection_start_x, selection_end_x); if (STATE_SELECTED == selection.state)
p2x = math.max(selection_start_x, selection_end_x); || (STATE_CHOSING == selection.state)
{
p1x = math.min(selection.start_x, selection.end_x);
p2x = math.max(selection.start_x, selection.end_x);
p1y = math.min(selection_start_y, selection_end_y); p1y = math.min(selection.start_y, selection.end_y);
p2y = math.max(selection_start_y, selection_end_y); p2y = math.max(selection.start_y, selection.end_y);
for (r = p1y; r <= p2y; r++) { for (r = p1y; r <= p2y; r++) {
for (c = p1x; c <= p2x; c++) { for (c = p1x; c <= p2x; c++) {
image.pixel_state.set_drawable_state(r, c, false); image.pixel_state.set_drawable_state(r, c, false);
if (STATE_SELECTED == selection_state) && (SelectTool_pointInSelection(c, r)) { if (STATE_SELECTED == selection.state) && (selection.is_point_in_selection(c, r)) {
old_color = selection.get_pixel(r - selection_start_y, c - selection_start_x); old_color = selection.buf.get_pixel(r - selection.start_y, c - selection.start_x);
} }
else { else {
old_color = image.get_pixel(r, c); old_color = image.get_pixel(r, c);