From 9115f9746406fe820e5afb68bff8f5dd242e6072 Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Tue, 24 Apr 2018 15:14:08 +0000 Subject: [PATCH] IconEdit 0.48: decrease redraw, improve file save, fix pencil undo, code clean git-svn-id: svn://kolibrios.org@7257 a494cfbc-eb01-0410-851d-a64ba20cac60 --- data/common/settings/assoc.ini | 2 +- programs/cmm/iconedit/colors_mas.h | 54 +- programs/cmm/iconedit/iconedit.c | 115 ++-- programs/cmm/iconedit/tools.h | 606 ++------------------ programs/cmm/iconedit/tools/fill.h | 10 + programs/cmm/iconedit/tools/pencil.h | 26 + programs/cmm/iconedit/tools/pipette.h | 19 + programs/cmm/iconedit/tools/selection.h | 319 +++++++++++ programs/cmm/iconedit/tools/simple_figure.h | 180 ++++++ 9 files changed, 727 insertions(+), 604 deletions(-) create mode 100644 programs/cmm/iconedit/tools/fill.h create mode 100644 programs/cmm/iconedit/tools/pencil.h create mode 100644 programs/cmm/iconedit/tools/pipette.h create mode 100644 programs/cmm/iconedit/tools/selection.h create mode 100644 programs/cmm/iconedit/tools/simple_figure.h diff --git a/data/common/settings/assoc.ini b/data/common/settings/assoc.ini index eb83a7b3d3..dd7c3b1e17 100644 --- a/data/common/settings/assoc.ini +++ b/data/common/settings/assoc.ini @@ -59,7 +59,7 @@ icon=22 [Shell] exec=/sys/shell -icon=2 +icon=1 next=$TinyPad [FASM] diff --git a/programs/cmm/iconedit/colors_mas.h b/programs/cmm/iconedit/colors_mas.h index 3c38fb9564..81efe0c926 100644 --- a/programs/cmm/iconedit/colors_mas.h +++ b/programs/cmm/iconedit/colors_mas.h @@ -1,10 +1,60 @@ #define MAX_CELL_SIZE 256 +////////////////////////////////////////////////////////////////////////////////////// +// // +// DRAW PIXEL // +// // +////////////////////////////////////////////////////////////////////////////////////// + +//The 'draw[]' in the array which holds the states should we draw a pixel or not. +//Is need to decrese redraw when using some tools like line, rectangle and selection. + +struct _pixel_state +{ + unsigned image_rows, image_columns; + bool draw[MAX_CELL_SIZE*MAX_CELL_SIZE]; + void set_drawable_state(); + bool is_drawable(); + void reset_and_set_all_drawable(); + void set_sizes(); +} pixel_state; + +void _pixel_state::set_drawable_state(int _r, _c, _state) +{ + draw[image_columns*_r + _c] = _state; +} + +bool _pixel_state::is_drawable(int _r, _c) +{ + return draw[image_columns*_r + _c]; +} + +void _pixel_state::reset_and_set_all_drawable() +{ + int i; + for (i = 0; i < image_columns*image_rows; i++) draw[i]=true; +} + +void _pixel_state::set_sizes(dword _r, _c) +{ + image_rows = _r; + image_columns = _c; +} + +////////////////////////////////////////////////////////////////////////////////////// +// // +// IMAGE // +// // +////////////////////////////////////////////////////////////////////////////////////// + +//This stucture determines basic actions that can be done with image (icon). + struct _image { unsigned rows, columns; dword mas[MAX_CELL_SIZE*MAX_CELL_SIZE]; dword img; + _pixel_state pixel_state; void create(); void set_pixel(); void draw_line(); @@ -21,6 +71,7 @@ void _image::create(int _rows, _columns) rows = _rows; columns = _columns; for (i = 0; i < columns*rows; i++) mas[i]=0xBFCAD2; + pixel_state.set_sizes(rows, columns); } void _image::set_pixel(int _r, _c, _color) @@ -209,6 +260,3 @@ void _image::move(int _direction) - - - diff --git a/programs/cmm/iconedit/iconedit.c b/programs/cmm/iconedit/iconedit.c index 55a27c7d1f..e58899c11d 100644 --- a/programs/cmm/iconedit/iconedit.c +++ b/programs/cmm/iconedit/iconedit.c @@ -11,7 +11,7 @@ enhance icon pipet aside color view */ -#define MEMSIZE 4096*100 +#define MEMSIZE 4096*200 #include "../lib/gui.h" #include "../lib/random.h" @@ -31,7 +31,7 @@ pipet aside color view // // //===================================================// -#define T_TITLE "Icon Editor 0.47 Alpha" +#define T_TITLE "Icon Editor 0.48 Alpha" #define TOOLBAR_H 24+8 #define PANEL_LEFT_W 16+5+5+3+3 @@ -57,6 +57,12 @@ dword color1 = 0x000000; dword color2 = 0xBFCAD2; dword tool_color; +signed hoverX; +signed hoverY; +signed priorHoverX; +signed priorHoverY; +bool canvasMouseMoved = false; + enum { BTN_NEW = 40, BTN_OPEN, @@ -110,7 +116,6 @@ _image image; #include "actions_history.h" -libimg_image open_image; _ActionsHistory actionsHistory; #include "tools.h" @@ -121,43 +126,10 @@ _ActionsHistory actionsHistory; // // //===================================================// - -void initTools() -{ - tools[0].id = TOOL_PENCIL; - tools[0].onMouseEvent = #PencilTool_onMouseEvent; - tools[0].deactivate = #PencilTool_reset; - - tools[1].id = TOOL_PIPETTE; - tools[1].activate = #PipetteTool_activate; - tools[1].onMouseEvent = #PipetteTool_onMouseEvent; - - tools[2].id = TOOL_FILL; - tools[2].onMouseEvent = #FillTool_onMouseEvent; - - tools[3].id = TOOL_LINE; - tools[3].activate = #SimpleFigureTool_Reset; - tools[3].deactivate = #SimpleFigureTool_Reset; - tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent; - tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; - - tools[4].id = TOOL_RECT; - tools[4].activate = #SimpleFigureTool_Reset; - tools[4].deactivate = #SimpleFigureTool_Reset; - tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent; - tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; - - tools[5].id = TOOL_SELECT; - tools[5].activate = #SelectTool_activate; - tools[5].deactivate = #SelectTool_deactivate; - tools[5].onMouseEvent = #SelectTool_onMouseEvent; - tools[5].onCanvasDraw = #SelectTool_onCanvasDraw; - tools[5].onKeyEvent = #SelectTool_onKeyEvent; -} - void main() { word btn; + libimg_image open_image; load_dll(libio, #libio_init, 1); load_dll(libimg, #libimg_init, 1); @@ -202,6 +174,21 @@ void main() if (mouse.pkm) tool_color = color2; if (mouse.mkm) break; + hoverX = mouse.x - canvas.x / zoom.value; + hoverY = mouse.y - canvas.y / zoom.value; + if (hoverX<0) hoverX = 0; + if (hoverY<0) hoverY = 0; + if (hoverX>image.columns-1) hoverX = image.columns-1; + if (hoverY>image.rows-1) hoverY = image.rows-1; + canvasMouseMoved = false; + if (priorHoverX != hoverX) canvasMouseMoved = true; + if (priorHoverY != hoverY) canvasMouseMoved = true; + priorHoverX = hoverX; + priorHoverY = hoverY; + //DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff); + //WriteText(Form.cwidth-100, 3, 0x80, 0x000000, + // sprintf(#param, "%i %i", hoverX, hoverY)); + if (currentTool != TOOL_NONE) tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm); @@ -230,14 +217,13 @@ void main() switch(btn) { case BTN_NEW: - image.create(32, 32); - DrawCanvas(); + EventCleanCanvas(); break; case BTN_OPEN: RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path)); break; case BTN_SAVE: - EventSave(); + EventSaveIconToFile(); break; case BTN_MOVE_LEFT: image.move(MOVE_LEFT); @@ -284,7 +270,7 @@ void main() setCurrentTool(TOOL_SELECT); break; case CLOSE_BTN: - ExitProcess(); + EventExitIconEdit(); break; } break; @@ -489,20 +475,31 @@ void DrawColorPallets() } } +void DrawCanvasPixel(dword _r,_c,_color) +{ + DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y, + zoom.value, zoom.value, _color); +} + void DrawCanvas() { int r, c; + dword color; + + if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0)) + { + tools[currentTool].onCanvasDraw(); + } + for (r = 0; r < image.rows; r++) { for (c = 0; c < image.columns; c++) { - DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, - zoom.value, zoom.value, image.get_pixel(r, c)); + if (image.pixel_state.is_drawable(r,c)) + DrawCanvasPixel(r, c, image.get_pixel(r,c)); } } - - if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0)) - tools[currentTool].onCanvasDraw(); + image.pixel_state.reset_and_set_all_drawable(); DrawPreview(); } @@ -526,9 +523,30 @@ dword GetPixelUnderMouse() // // //===================================================// -void EventSave() +void EventSaveIconToFile() { - save_image(image.get_image(), image.columns, image.rows, "/rd/1/saved_image.png"); + int i=0; + char save_file_name[4096]; + char save_path_stable[4096]; + strcpy(#save_path_stable, "/tmp0/1"); + do { + i++; + sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i); + } while (file_exists(#save_file_name)); + save_image(image.get_image(), image.columns, image.rows, #save_file_name); +} + +void EventCleanCanvas() +{ + EventSaveIconToFile(); + image.create(32, 32); + DrawCanvas(); +} + +void EventExitIconEdit() +{ + EventSaveIconToFile(); + ExitProcess(); } void EventSetActiveColor(int _number, _color) @@ -545,3 +563,4 @@ void EventSetActiveColor(int _number, _color) DrawActiveColor(NULL); DrawColorPallets(); } + diff --git a/programs/cmm/iconedit/tools.h b/programs/cmm/iconedit/tools.h index cb59699b38..cff9e35342 100644 --- a/programs/cmm/iconedit/tools.h +++ b/programs/cmm/iconedit/tools.h @@ -1,3 +1,20 @@ +#define TOIMAGE 1 +#define TOCANVAS 2 + + +struct Tool { + int id; + void (*activate)(); + void (*deactivate)(); + void (*onMouseEvent)(int x, int y, int lkm, int pkm); + void (*onKeyEvent)(dword keycode); + void (*onCanvasDraw)(); +}; + +int previousTool = -1; +int currentTool = -1; +Tool tools[6]; + enum { TOOL_NONE = -1, TOOL_PENCIL, @@ -7,30 +24,56 @@ enum { TOOL_RECT, TOOL_SELECT }; +#include "tools/pencil.h"; +#include "tools/pipette.h"; +#include "tools/fill.h"; +#include "tools/selection.h"; +#include "tools/simple_figure.h"; -struct Tool { - int id; + +void initTools() +{ + tools[0].id = TOOL_PENCIL; + tools[0].onMouseEvent = #PencilTool_onMouseEvent; + tools[0].deactivate = #PencilTool_reset; - void (*activate)(); - void (*deactivate)(); - void (*onMouseEvent)(int x, int y, int lkm, int pkm); - void (*onKeyEvent)(dword keycode); - void (*onCanvasDraw)(); -}; + tools[1].id = TOOL_PIPETTE; + tools[1].activate = #PipetteTool_activate; + tools[1].onMouseEvent = #PipetteTool_onMouseEvent; + + tools[2].id = TOOL_FILL; + tools[2].onMouseEvent = #FillTool_onMouseEvent; + + tools[3].id = TOOL_LINE; + tools[3].activate = #SimpleFigureTool_Reset; + tools[3].deactivate = #SimpleFigureTool_Reset; + tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent; + tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; + + tools[4].id = TOOL_RECT; + tools[4].activate = #SimpleFigureTool_Reset; + tools[4].deactivate = #SimpleFigureTool_Reset; + tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent; + tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; -int currentTool = -1; -Tool tools[6]; + tools[5].id = TOOL_SELECT; + tools[5].activate = #SelectTool_activate; + tools[5].deactivate = #SelectTool_deactivate; + tools[5].onMouseEvent = #SelectTool_onMouseEvent; + tools[5].onCanvasDraw = #SelectTool_onCanvasDraw; + tools[5].onKeyEvent = #SelectTool_onKeyEvent; +} void resetCurrentTool() { if ((currentTool != TOOL_NONE) && (tools[currentTool].deactivate != 0)) { tools[currentTool].deactivate(); } - currentTool = TOOL_NONE; } void setCurrentTool(int index) { + previousTool = currentTool; resetCurrentTool(); currentTool = index; @@ -48,544 +91,3 @@ void setCurrentTool(int index) { // // //===================================================// -void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { - if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up) - { - image.fill(mouseY-canvas.y/zoom.value, - mouseX-canvas.x/zoom.value, tool_color); - actionsHistory.saveCurrentState(); - DrawCanvas(); - } -} - -void PipetteTool_activate() { - SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE); -} - -void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { - tool_color = GetPixelUnderMouse(); - DrawBar(Form.cwidth-30, 5, 20, 20, tool_color); - - if (mouse.down) { - DrawBar(Form.cwidth-30, 5, 20, 20, system.color.work); - SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); - if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color); - if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color); - - setCurrentTool(TOOL_PENCIL); - } -} - -bool PencilTool_Drawing = false; - -void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { - if (canvas.hovered()) - { - if ((PencilTool_Drawing == true) && (!mouse.key)) { - actionsHistory.saveCurrentState(); - PencilTool_Drawing = false; - } - - if (mouse.key) { - image.set_pixel(mouseY-canvas.y/zoom.value, - mouseX-canvas.x/zoom.value, tool_color); - PencilTool_Drawing = true; - } - DrawCanvas(); - } -} - -void PencilTool_reset() { - PencilTool_Drawing = false; -} - -// Line tool -struct SimpleFigureTool_State { - int startX, startY; - int lastTempPosX, lastTempPosY; -}; - -enum { - TOOL_LINE_STATE, - TOOL_RECT_STATE -}; - -dword currentFigToolState = -1; -SimpleFigureTool_State figTool_States[2]; - -void SimpleFigureTool_Reset() { - if (currentTool == TOOL_LINE) - currentFigToolState = TOOL_LINE_STATE; - else if (currentTool == TOOL_RECT) - currentFigToolState = TOOL_RECT_STATE; - - figTool_States[currentFigToolState].startX = -1; - figTool_States[currentFigToolState].startY = -1; - figTool_States[currentFigToolState].lastTempPosX = -1; - figTool_States[currentFigToolState].lastTempPosY = -1; -} - -int mouseX_last; -int mouseY_last; -bool first_click_in_canvas = false; - -void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { - if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true; - if (first_click_in_canvas) - { - if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; - if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; - if (mouseX= 0) - && (figTool_States[currentFigToolState].startY >= 0)) { - // Draw line from start position to current position - if (currentTool == TOOL_LINE) { - DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, - figTool_States[currentFigToolState].startY - canvas.y/zoom.value, - mouseX - canvas.x/zoom.value, - mouseY - canvas.y/zoom.value, - tool_color, - 1); - } - else if (currentTool == TOOL_RECT) { - DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, - figTool_States[currentFigToolState].startY - canvas.y/zoom.value, - mouseX - canvas.x/zoom.value, - mouseY - canvas.y/zoom.value, tool_color, 1); - } - - DrawCanvas(); - - actionsHistory.saveCurrentState(); - - // Reset start position - figTool_States[currentFigToolState].startX = -1; - figTool_States[currentFigToolState].startY = -1; - - first_click_in_canvas = false; - } - } - } -} - -void SimpleFigureTool_onCanvasDraw() { - if ((figTool_States[currentFigToolState].startX >= 0) - && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) { - if (currentTool == TOOL_LINE) { - DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, - figTool_States[currentFigToolState].startY - canvas.y/zoom.value, - mouseX_last - canvas.x/zoom.value, - mouseY_last - canvas.y/zoom.value, - tool_color, - 2); - } - else if (currentTool == TOOL_RECT) { - DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, - figTool_States[currentFigToolState].startY - canvas.y/zoom.value, - mouseX_last - canvas.x/zoom.value, - mouseY_last - canvas.y/zoom.value, - tool_color, - 2); - } - - figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value; - figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value; - } -} - -// Selection -int selection_start_x = -1; -int selection_start_y = -1; -int selection_end_x = -1; -int selection_end_y = -1; -bool selection_active = false; - -dword SelectionTool_buffer = 0; -dword SelectionTool_buffer_r = 0; -dword SelectionTool_buffer_c = 0; - -bool selection_moving_started = false; -int selection_pivot_x = -1; -int selection_pivot_y = -1; - -void SelectTool_normalizeSelection() { - int t; - - // Restructuring of the selection coordinates - if (selection_end_x < selection_start_x) { - t = selection_start_x; - selection_start_x = selection_end_x; - selection_end_x = t; - } - - if (selection_end_y < selection_start_y) { - t = selection_end_y; - selection_end_y = selection_start_y; - selection_start_y = t; - } -} - -void reset_selection_moving() { - if (selection_moving_started) { - SelectTool_drawBuffer(selection_start_x, selection_start_y, 1); - - selection_pivot_x = -1; - selection_pivot_y = -1; - - selection_moving_started = false; - - actionsHistory.saveCurrentState(); - DrawCanvas(); - } -} - -bool is_selection_moving() { - return selection_moving_started; -} - -void reset_selection() { - reset_selection_moving(); - - selection_start_x = -1; - selection_start_y = -1; - selection_end_x = -1; - selection_end_y = -1; -} - -void SelectTool_activate() { - reset_selection(); - - selection_active = false; -} - -void SelectTool_deactivate() { - reset_selection_moving(); -} - -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() { - dword offset, r, c; - - if (SelectionTool_buffer != 0) - free(SelectionTool_buffer); - - SelectionTool_buffer_r = selection_end_y - selection_start_y + 1; - SelectionTool_buffer_c = selection_end_x - selection_start_x + 1; - SelectionTool_buffer = malloc(SelectionTool_buffer_r * SelectionTool_buffer_c * 4); - - for (r = selection_start_y; r <= selection_end_y; r++) { - for (c = selection_start_x; c <= selection_end_x; c++) { - offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; - - ESDWORD[SelectionTool_buffer + offset] = image.get_pixel(r, c); - } - } -} - -void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { - int click_x, click_y, dx, dy, m_x, m_y, r, c, color; - dword pixel; - - m_x = TO_CANVAS_X(mouseX); - m_y = TO_CANVAS_Y(mouseY); - - if (mouse.down) && (canvas.hovered()) && (!selection_active) { - 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(); - - if (!selection_moving_started) && ( !(key_modifier&KEY_LSHIFT) ) { - for (r = selection_start_y; r <= selection_end_y; r++) - for (c = selection_start_x; c <= selection_end_x; c++) { - image.set_pixel(r, c, color2); - } - } - - selection_moving_started = true; - } - } - else { - - reset_selection(); - selection_active = true; - } - } - - 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(); - } - - if (selection_active) - { - if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; - if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; - - if (mouseX= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) { - DrawSelection(selection_start_x, selection_start_y, selection_end_x, selection_end_y); - } -} - -void SelectTool_drawBuffer(int insert_x, int insert_y, int target) { - dword color; - dword offset, r, c; - dword insert_to_x, insert_to_y; - - if (SelectionTool_buffer != 0) { - insert_to_x = insert_x + SelectionTool_buffer_c - 1; - - if (insert_to_x >= image.columns) - insert_to_x = image.columns-1; - - insert_to_y = insert_y + SelectionTool_buffer_r - 1; - - if (insert_to_y >= image.rows) - insert_to_y = image.rows-1; - - for (r = insert_y; r <= insert_to_y; r++) { - for (c = insert_x; c <= insert_to_x; c++) { - offset = calc(SelectionTool_buffer_c * calc(r - insert_y) + calc(c - insert_x)) * 4; - - color = ESDWORD[SelectionTool_buffer + offset]; - - if (target == 1) - image.set_pixel(r, c, color); - else - DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, - zoom.value, zoom.value, color); - } - } - } -} - -void SelectTool_onKeyEvent(dword keycode) { - dword offset, r, c; - dword insert_x, insert_y, insert_to_x, insert_to_y; - - if (keycode == SCAN_CODE_KEY_V) { - if (SelectionTool_buffer != 0) { - reset_selection(); - - selection_moving_started = true; - selection_start_x = 0; - selection_end_x = SelectionTool_buffer_c - 1; - - selection_start_y = 0; - selection_end_y = SelectionTool_buffer_r - 1; - - DrawCanvas(); - - } - } -} - - -//===================================================// -// // -// DRAWs // -// // -//===================================================// - - - -// target - image (1) or canvas (2) -void DrawLine(int x1, int y1, int x2, int y2, dword color, int target) { - int dx, dy, signX, signY, error, error2; - - if (1==target) { - image.draw_line(x1, y1, x2, y2, color); - return; - } - - dx = x2 - x1; - - if (dx < 0) - dx = -dx; - - dy = y2 - y1; - - if (dy < 0) - dy = -dy; - - if (x1 < x2) - signX = 1; - else - signX = -1; - - if (y1 < y2) - signY = 1; - else - signY = -1; - - error = dx - dy; - - DrawBar(x2*zoom.value + canvas.x, y2*zoom.value + canvas.y, - zoom.value, zoom.value, color); - - while((x1 != x2) || (y1 != y2)) - { - DrawBar(x1*zoom.value + canvas.x, y1*zoom.value + canvas.y, - zoom.value, zoom.value, color); - - error2 = error * 2; - - if(error2 > calc(-dy)) - { - error -= dy; - x1 += signX; - } - - if(error2 < dx) - { - error += dx; - y1 += signY; - } - } -} - -void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) { - DrawLine(x1, y1, x2, y1, color, target); - DrawLine(x2, y1, x2, y2, color, target); - DrawLine(x2, y2, x1, y2, color, target); - DrawLine(x1, y2, x1, y1, color, target); -} - -#define SELECTION_COLOR 0xAAE5EF - -void DrawSelection(int x1, int y1, int x2, int y2) { - int p1x, p1y, p2x, p2y, r, c, old_color, new_color; - dword offset; - - if (x1 <= x2) { - p1x = x1; - p2x = x2; - } - else { - p1x = x2; - p2x = x1; - } - - if (y1 <= y2) { - p2y = y1; - p1y = y2; - } - else { - p2y = y2; - p1y = y1; - } - - for (r = p1y; r >= p2y; r--) { - for (c = p1x; c <= p2x; c++) { - - if (selection_moving_started) && (SelectTool_pointInSelection(c, r)) { - offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; - old_color = ESDWORD[SelectionTool_buffer + offset]; - } - else { - old_color = image.get_pixel(r, c); - } - - new_color = MixColors(old_color, SELECTION_COLOR, 64); - - DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, - zoom.value, zoom.value, new_color); - - } - } -} \ No newline at end of file diff --git a/programs/cmm/iconedit/tools/fill.h b/programs/cmm/iconedit/tools/fill.h new file mode 100644 index 0000000000..91ef8e7e3e --- /dev/null +++ b/programs/cmm/iconedit/tools/fill.h @@ -0,0 +1,10 @@ + +void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { + if (canvas.hovered()) && (mouse.up) + { + image.fill(mouseY-canvas.y/zoom.value, + mouseX-canvas.x/zoom.value, tool_color); + actionsHistory.saveCurrentState(); + DrawCanvas(); + } +} diff --git a/programs/cmm/iconedit/tools/pencil.h b/programs/cmm/iconedit/tools/pencil.h new file mode 100644 index 0000000000..5fb9f72d51 --- /dev/null +++ b/programs/cmm/iconedit/tools/pencil.h @@ -0,0 +1,26 @@ + +bool PencilTool_Drawing = false; + +void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { + if (canvas.hovered()) + { + if ((PencilTool_Drawing == true) && (!mouse.key)) { + actionsHistory.saveCurrentState(); + PencilTool_Drawing = false; + } + + if (mouse.key) { + image.set_pixel(hoverY, hoverX, tool_color); + DrawCanvasPixel(hoverY, hoverX, tool_color); + PencilTool_Drawing = true; + } + if (mouse.up) { + DrawPreview(); + actionsHistory.saveCurrentState(); + } + } +} + +void PencilTool_reset() { + PencilTool_Drawing = false; +} diff --git a/programs/cmm/iconedit/tools/pipette.h b/programs/cmm/iconedit/tools/pipette.h new file mode 100644 index 0000000000..b605ee5914 --- /dev/null +++ b/programs/cmm/iconedit/tools/pipette.h @@ -0,0 +1,19 @@ + +void PipetteTool_activate() { + SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE); +} + +void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { + //if (!canvas.hovered()) return; //TODO: option "Restrict pipette to canvas area" + tool_color = GetPixelUnderMouse(); + DrawBar(Form.cwidth-30, 5, 20, 20, tool_color); + + if (mouse.down) { + DrawBar(Form.cwidth-30, 5, 20, 20, system.color.work); + SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); + if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color); + if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color); + + setCurrentTool(previousTool); + } +} diff --git a/programs/cmm/iconedit/tools/selection.h b/programs/cmm/iconedit/tools/selection.h new file mode 100644 index 0000000000..070231c347 --- /dev/null +++ b/programs/cmm/iconedit/tools/selection.h @@ -0,0 +1,319 @@ +// Selection +int selection_start_x = -1; +int selection_start_y = -1; +int selection_end_x = -1; +int selection_end_y = -1; +bool selection_active = false; + +dword SelectionTool_buffer = 0; +dword SelectionTool_buffer_r = 0; +dword SelectionTool_buffer_c = 0; + +bool selection_moving_started = false; +int selection_pivot_x = -1; +int selection_pivot_y = -1; + +void SelectTool_normalizeSelection() { + int t; + + // Restructuring of the selection coordinates + if (selection_end_x < selection_start_x) { + t = selection_start_x; + selection_start_x = selection_end_x; + selection_end_x = t; + } + + if (selection_end_y < selection_start_y) { + t = selection_end_y; + selection_end_y = selection_start_y; + selection_start_y = t; + } +} + +void reset_selection_moving() { + if (selection_moving_started) { + SelectTool_drawBuffer(selection_start_x, selection_start_y, 1); + + selection_pivot_x = -1; + selection_pivot_y = -1; + + selection_moving_started = false; + + actionsHistory.saveCurrentState(); + DrawCanvas(); + } +} + +bool is_selection_moving() { + return selection_moving_started; +} + +void reset_selection() { + reset_selection_moving(); + + selection_start_x = -1; + selection_start_y = -1; + selection_end_x = -1; + selection_end_y = -1; +} + +void SelectTool_activate() { + reset_selection(); + + selection_active = false; +} + +void SelectTool_deactivate() { + reset_selection_moving(); +} + +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() { + dword offset, r, c; + + if (SelectionTool_buffer != 0) + free(SelectionTool_buffer); + + SelectionTool_buffer_r = selection_end_y - selection_start_y + 1; + SelectionTool_buffer_c = selection_end_x - selection_start_x + 1; + SelectionTool_buffer = malloc(SelectionTool_buffer_r * SelectionTool_buffer_c * 4); + + for (r = selection_start_y; r <= selection_end_y; r++) { + for (c = selection_start_x; c <= selection_end_x; c++) { + offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; + + ESDWORD[SelectionTool_buffer + offset] = image.get_pixel(r, c); + } + } +} + +void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { + int click_x, click_y, dx, dy, m_x, m_y, r, c, color; + dword pixel; + + m_x = TO_CANVAS_X(mouseX); + m_y = TO_CANVAS_Y(mouseY); + + if (mouse.down) && (canvas.hovered()) && (!selection_active) { + 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(); + + if (!selection_moving_started) && ( !(key_modifier&KEY_LSHIFT) ) { + for (r = selection_start_y; r <= selection_end_y; r++) + for (c = selection_start_x; c <= selection_end_x; c++) { + image.set_pixel(r, c, color2); + } + } + + selection_moving_started = true; + } + } + else { + + reset_selection(); + selection_active = true; + } + } + + 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(); + } + + if (selection_active) + { + if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; + if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; + + if (mouseX= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) { + DrawSelection(selection_start_x, selection_start_y, selection_end_x, selection_end_y); + } +} + +void SelectTool_drawBuffer(int insert_x, int insert_y, int target) { + dword color; + dword offset, r, c; + dword insert_to_x, insert_to_y; + + if (SelectionTool_buffer != 0) { + insert_to_x = insert_x + SelectionTool_buffer_c - 1; + + if (insert_to_x >= image.columns) + insert_to_x = image.columns-1; + + insert_to_y = insert_y + SelectionTool_buffer_r - 1; + + if (insert_to_y >= image.rows) + insert_to_y = image.rows-1; + + for (r = insert_y; r <= insert_to_y; r++) { + for (c = insert_x; c <= insert_to_x; c++) { + offset = calc(SelectionTool_buffer_c * calc(r - insert_y) + calc(c - insert_x)) * 4; + + color = ESDWORD[SelectionTool_buffer + offset]; + + if (TOIMAGE == target) + image.set_pixel(r, c, color); + else + DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, + zoom.value, zoom.value, color); + } + } + } +} + +void SelectTool_onKeyEvent(dword keycode) { + dword offset, r, c; + dword insert_x, insert_y, insert_to_x, insert_to_y; + + if (keycode == SCAN_CODE_KEY_V) { + if (SelectionTool_buffer != 0) { + reset_selection(); + + selection_moving_started = true; + selection_start_x = 0; + selection_end_x = SelectionTool_buffer_c - 1; + + selection_start_y = 0; + selection_end_y = SelectionTool_buffer_r - 1; + + DrawCanvas(); + + } + } +} + +void DrawSelection(int x1, int y1, int x2, int y2) { + #define SELECTION_COLOR 0xAAE5EF + int p1x, p1y, p2x, p2y, r, c, old_color, new_color; + dword offset; + + if (x1 <= x2) { + p1x = x1; + p2x = x2; + } + else { + p1x = x2; + p2x = x1; + } + + if (y1 <= y2) { + p2y = y1; + p1y = y2; + } + else { + p2y = y2; + p1y = y1; + } + + /* + if (selection_moving_started) { + SelectTool_drawBuffer(selection_start_x, selection_start_y, 2); + } + + DrawRectangle( + selection_start_x*zoom.value+canvas.x, + selection_start_y*zoom.value+canvas.y, + selection_end_x-selection_start_x+1*zoom.value-1, + selection_end_y-selection_start_y+1*zoom.value-1, + 0); + + for (r = p1y; r >= p2y; r--) + for (c = p1x; c <= p2x; c++) { + image.pixel_state.set_drawable_state(r, c, false); + } + */ + + for (r = p1y; r >= p2y; r--) { + for (c = p1x; c <= p2x; c++) { + image.pixel_state.set_drawable_state(r, c, false); + + if (selection_moving_started) && (SelectTool_pointInSelection(c, r)) { + offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; + old_color = ESDWORD[SelectionTool_buffer + offset]; + } + else { + old_color = image.get_pixel(r, c); + } + + new_color = MixColors(old_color, SELECTION_COLOR, 64); + + DrawCanvasPixel(r, c, new_color); + } + } +} \ No newline at end of file diff --git a/programs/cmm/iconedit/tools/simple_figure.h b/programs/cmm/iconedit/tools/simple_figure.h new file mode 100644 index 0000000000..7d74b04825 --- /dev/null +++ b/programs/cmm/iconedit/tools/simple_figure.h @@ -0,0 +1,180 @@ +// Line tool +struct SimpleFigureTool_State { + int startX, startY; + int lastTempPosX, lastTempPosY; +}; + +enum { + TOOL_LINE_STATE, + TOOL_RECT_STATE +}; + +SimpleFigureTool_State figTool; + +void SimpleFigureTool_Reset() { + figTool.startX = -1; + figTool.startY = -1; + figTool.lastTempPosX = -1; + figTool.lastTempPosY = -1; +} + +int mouseX_last; +int mouseY_last; +bool first_click_in_canvas = false; + +void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { + if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true; + if (first_click_in_canvas) + { + if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; + if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; + if (mouseX= 0) + && (figTool.startY >= 0)) { + // Draw line from start position to current position + if (currentTool == TOOL_LINE) { + DrawLineIcon(figTool.startX - canvas.x/zoom.value, + figTool.startY - canvas.y/zoom.value, + mouseX - canvas.x/zoom.value, + mouseY - canvas.y/zoom.value, + tool_color, + TOIMAGE); + } + else if (currentTool == TOOL_RECT) { + DrawRectangleIcon(figTool.startX - canvas.x/zoom.value, + figTool.startY - canvas.y/zoom.value, + mouseX - canvas.x/zoom.value, + mouseY - canvas.y/zoom.value, tool_color, + TOIMAGE); + } + + DrawCanvas(); + + actionsHistory.saveCurrentState(); + + // Reset start position + figTool.startX = -1; + figTool.startY = -1; + + first_click_in_canvas = false; + } + } + } +} + +void SimpleFigureTool_onCanvasDraw() { + if ((figTool.startX >= 0) + && (figTool.startY >= 0) && (mouse.key)) { + if (currentTool == TOOL_LINE) { + DrawLineIcon(figTool.startX - canvas.x/zoom.value, + figTool.startY - canvas.y/zoom.value, + mouseX_last - canvas.x/zoom.value, + mouseY_last - canvas.y/zoom.value, + tool_color, + TOCANVAS); + } + else if (currentTool == TOOL_RECT) { + DrawRectangleIcon(figTool.startX - canvas.x/zoom.value, + figTool.startY - canvas.y/zoom.value, + mouseX_last - canvas.x/zoom.value, + mouseY_last - canvas.y/zoom.value, + tool_color, + TOCANVAS); + } + + figTool.lastTempPosX = mouseX_last - canvas.x/zoom.value; + figTool.lastTempPosY = mouseY_last - canvas.y/zoom.value; + } +} + + +//===================================================// +// // +// DRAWs // +// // +//===================================================// + + + +// target - TOMAGE, TOCANVAS +void DrawLineIcon(int x1, int y1, int x2, int y2, dword color, int target) { + int dx, dy, signX, signY, error, error2; + + if (TOIMAGE == target) { + image.draw_line(x1, y1, x2, y2, color); + return; + } + + dx = x2 - x1; + + if (dx < 0) + dx = -dx; + + dy = y2 - y1; + + if (dy < 0) + dy = -dy; + + if (x1 < x2) + signX = 1; + else + signX = -1; + + if (y1 < y2) + signY = 1; + else + signY = -1; + + error = dx - dy; + + DrawCanvasPixel(y2, x2, color); + image.pixel_state.set_drawable_state(y2, x2, false); + + while((x1 != x2) || (y1 != y2)) + { + DrawCanvasPixel(y1, x1, color); + + image.pixel_state.set_drawable_state(y1, x1, false); + + error2 = error * 2; + + if(error2 > calc(-dy)) + { + error -= dy; + x1 += signX; + } + + if(error2 < dx) + { + error += dx; + y1 += signY; + } + } +} + +void DrawRectangleIcon(int x1, int y1, int x2, int y2, dword color, int target) { + DrawLineIcon(x1, y1, x2, y1, color, target); + DrawLineIcon(x2, y1, x2, y2, color, target); + DrawLineIcon(x2, y2, x1, y2, color, target); + DrawLineIcon(x1, y2, x1, y1, color, target); +} +