Mulithreaded redraw, reduce main function and remove unused imports

master v0.1.1
Marko Semet 2017-12-26 01:12:17 +01:00
parent 23d7feb971
commit 96cfbf9d17
7 changed files with 100 additions and 55 deletions

View File

@ -4,7 +4,7 @@
"homepage": "https://marko10000.tk/redirect/drawofpages",
"dependencies": {
"gtk-d": "~>3.7.0",
"structuresd": "~>1.0.0-beta2"
"structuresd": "~>1.0.0-beta3"
},
"license": "GPL-3.0",
"name": "drawofpages",

View File

@ -19,7 +19,6 @@ module drawofpages.elements.basetypes;
private
{
import drawofpages.draw;
import std.algorithm.comparison;
import structuresd.dimension;
}

View File

@ -15,14 +15,3 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module drawofpages.elements;
private
{
import drawofpages.draw;
import drawofpages.elements.basetypes;
import drawofpages.gui;
import structuresd.dimension;
import structuresd.dimension.rtree;
}

View File

@ -18,7 +18,9 @@ module drawofpages.gui.mainWindow;
private
{
import gtk.Window;
import drawofpages.gui.drawArea;
import drawofpages.tools.drawer;
import drawofpages.tools.interaction;
import gtk.MainWindow;
}
@ -27,10 +29,38 @@ private class Menu
}
public class DBMain : MainWindow
public class Tab
{
public this()
package string name;
package DrawElement drawElement;
private InteractionSafer interaction;
package this(DrawThread drawThread)
{
super("Drawbook");
this.drawElement = new DrawElement();
this.interaction = new InteractionSafer(drawThread, this.drawElement.getDrawHanlder());
this.interaction.currentTool = new DrawLine(this.drawElement.getDrawHanlder());
this.drawElement.getGuiInteraction() = this.interaction;
}
}
public class DOPMain : MainWindow
{
DrawThread drawThread;
Tab tab;
public this()
{
super("DrawOfPapers");
this.setDefaultSize(800, 600); // TODO: Load size from config
this.drawThread = new DrawThread();
this.tab = new Tab(this.drawThread);
this.add(this.tab.drawElement); // TODO: Tab support
this.showAll();
}
public void stop()
{
this.drawThread.stop = true;
}
}

View File

@ -16,28 +16,14 @@
*/
module drawofpages;
private import drawofpages.tools.drawer;
private import drawofpages.tools.interaction;
private import drawofpages.gui.drawArea;
private import drawofpages.draw;
private import gtk.MainWindow;
private import drawofpages.gui.mainWindow;
private import gtk.Main;
void main(string[] args)
{
Main.init(args);
MainWindow win = new MainWindow("Hello World");
win.setDefaultSize(800, 600);
DrawElement de = new DrawElement();
de.getDrawHanlder().drawLine(Point2D([50, 50]), Point2D([100, 100]), 5, Color.RED);
DrawThread dt = new DrawThread(de.getDrawHanlder());
InteractionSafer ints = new InteractionSafer(dt);
ints.currentTool = new DrawLine(de.getDrawHanlder());
de.getGuiInteraction() = ints;
win.add(de);
de.queueDraw();
win.showAll();
DOPMain mainWindow = new DOPMain();
Main.run();
dt.stop = true;
mainWindow.stop();
}

View File

@ -23,7 +23,6 @@ private
import drawofpages.gui;
import drawofpages.tools;
import structuresd.containers.queue;
import std.stdio;
}
package struct CursorInteraction
@ -33,27 +32,34 @@ package struct CursorInteraction
double pressure;
string id;
}
package struct RedrawInteraction
{
Square box;
Draw target;
}
public class DrawThread : Thread
{
private enum INTERACTION_TYPE
{
CURSOR_DOWN = 0,
CURSOR_CONTIN = 1,
CURSOR_UP = 2
REDRAW = 0,
CURSOR_DOWN = 1,
CURSOR_CONTIN = 2,
CURSOR_UP = 3
}
private static struct Interaction
{
public INTERACTION_TYPE itype;
public Document doc;
public Tool tool;
public Draw draw;
union
{
public CursorInteraction cursor;
public RedrawInteraction redraw;
}
}
private Draw draw;
private Queue!(Interaction, true) queue;
public bool stop;
@ -62,7 +68,7 @@ public class DrawThread : Thread
Interaction tmp;
while(!this.stop)
{
bool redraw = false;
bool[Draw] redraw;
while(this.queue.fetch(tmp))
{
if(tmp.tool is null)
@ -71,31 +77,34 @@ public class DrawThread : Thread
}
final switch(tmp.itype)
{
case INTERACTION_TYPE.REDRAW:
redraw[tmp.draw] = true;
tmp.doc.redraw(tmp.redraw.box, tmp.redraw.target);
break;
case INTERACTION_TYPE.CURSOR_DOWN:
redraw = true;
redraw[tmp.draw] = true;
tmp.tool.cursorDown(tmp.doc, tmp.cursor.pos, tmp.cursor.ctype, tmp.cursor.pressure, tmp.cursor.id);
break;
case INTERACTION_TYPE.CURSOR_CONTIN:
redraw = true;
redraw[tmp.draw] = true;
tmp.tool.cursorContin(tmp.doc, tmp.cursor.pos, tmp.cursor.ctype, tmp.cursor.pressure, tmp.cursor.id);
break;
case INTERACTION_TYPE.CURSOR_UP:
redraw = true;
redraw[tmp.draw] = true;
tmp.tool.cursorUp(tmp.doc, tmp.cursor.pos, tmp.cursor.ctype, tmp.cursor.pressure, tmp.cursor.id);
break;
}
}
if(redraw)
foreach(Draw draw; redraw.keys)
{
this.draw.redraw();
draw.redraw();
}
yield();
}
}
public this(Draw draw)
public this()
{
this.draw = draw;
this.queue = new Queue!(Interaction, true);
this.stop = false;
super(&this.run);
@ -103,7 +112,13 @@ public class DrawThread : Thread
}
pragma(inline, true)
public void add(string TYPE)(Document doc, Tool tool, CursorInteraction ci)
private void add(ref Interaction interact)
{
this.queue.insert(interact);
}
pragma(inline, true)
public void add(string TYPE)(Document doc, Draw draw, Tool tool, CursorInteraction ci)
{
Interaction tmp;
static if(TYPE == "DOWN")
@ -125,7 +140,20 @@ public class DrawThread : Thread
tmp.doc = doc;
tmp.tool = tool;
tmp.cursor = ci;
this.queue.insert(tmp);
tmp.draw = draw;
this.add(tmp);
}
pragma(inline, true)
public void add(Document doc, Draw draw, RedrawInteraction redraw)
{
Interaction tmp;
tmp.itype = INTERACTION_TYPE.REDRAW;
tmp.doc = doc;
tmp.tool = voidTool;
tmp.draw = draw;
tmp.redraw = redraw;
this.add(tmp);
}
};
@ -134,12 +162,14 @@ public class InteractionSafer : GuiInteraction
private Document document;
public Tool currentTool;
private DrawThread thread;
private Draw draw;
public this(DrawThread thread)
public this(DrawThread thread, Draw draw)
{
this.document = new Document();
this.currentTool = null;
this.thread = thread;
this.draw = draw;
}
public void down(Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID)
@ -149,7 +179,7 @@ public class InteractionSafer : GuiInteraction
ci.ctype = cursor;
ci.pressure = pressure;
ci.id = cursorID;
thread.add!"DOWN"(this.document, this.currentTool, ci);
thread.add!"DOWN"(this.document, this.draw, this.currentTool, ci);
}
public void contin(Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID)
{
@ -158,7 +188,7 @@ public class InteractionSafer : GuiInteraction
ci.ctype = cursor;
ci.pressure = pressure;
ci.id = cursorID;
thread.add!"CONTIN"(this.document, this.currentTool, ci);
thread.add!"CONTIN"(this.document, this.draw, this.currentTool, ci);
}
public void up(Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID)
{
@ -167,10 +197,13 @@ public class InteractionSafer : GuiInteraction
ci.ctype = cursor;
ci.pressure = pressure;
ci.id = cursorID;
thread.add!"UP"(this.document, this.currentTool, ci);
thread.add!"UP"(this.document, this.draw, this.currentTool, ci);
}
public void redraw(Square area, Draw target)
{
this.document.redraw(area, target);
RedrawInteraction ri;
ri.box = area;
ri.target = target;
this.thread.add(this.document, this.draw, ri);
}
}

View File

@ -21,7 +21,6 @@ private
import drawofpages.draw;
import drawofpages.elements.basetypes;
import drawofpages.gui;
import drawofpages.tools.drawer;
import structuresd.dimension;
import structuresd.dimension.rtree;
}
@ -51,6 +50,15 @@ public interface Tool
void cursorUp(Document doc, Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID);
}
private class VoidTool : Tool
{
void cursorDown(Document doc, Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID) {}
void cursorContin(Document doc, Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID) {}
void cursorUp(Document doc, Point2D point, CURSOR_TYPE cursor, double pressure, string cursorID) {}
}
public __gshared Tool voidTool = new VoidTool();
public mixin template CURSOR_FROM_TO()
{
private Point2D __cursorPosition;