Skip to content
Snippets Groups Projects
Commit ad09ffa3 authored by raoul's avatar raoul
Browse files

Python 1st steps + main/controller refactoring

parent 7ad9349f
No related branches found
No related tags found
No related merge requests found
......@@ -6,3 +6,7 @@ All:
clean:
@echo "----------Cleaning project:[ openembroider - Debug ]----------"
@$(MAKE) -f "openembroider.mk" clean
swig:
@swig -python -Iinclude -c++ oe.i
@g++ -DSWINGING -ggdb -fPIC -shared -std=c++11 -Iinclude -I/usr/include/python3.5m main.cpp oe_wrap.cxx src/OE_editDisplay.cpp src/instructions/OE_instruction.cpp src/instructions/OE_waitcolor.cpp src/instructions/OE_start.cpp src/OE_svgParser.cpp src/OE_commandDisplay.cpp src/curves/OE_subcurve.cpp src/curves/OE_curve.cpp src/curves/OE_joincurve.cpp src/curves/OE_pointcurve.cpp src/OE_thread.cpp src/xml/tinyxml.cpp src/xml/tinystr.cpp src/xml/tinyxmlparser.cpp src/xml/tinyxmlerror.cpp src/stitchs/OE_birailstitch.cpp src/stitchs/OE_stitchs.cpp src/stitchs/OE_linkstitch.cpp src/stitchs/OE_linestitch.cpp src/OE_document.cpp src/OE_display.cpp src/OE_interfaceDisplay.cpp src/actions/OE_actionSelection.cpp src/actions/OE_actions.cpp src/actions/OE_actionsThreads.cpp src/actions/OE_actionsBirailStitchs.cpp src/actions/OE_actionsLineStitchs.cpp src/actions/OE_actionsCurves.cpp src/actions/OE_actionsStitchs.cpp src/OE_controller.cpp src/OE_base.cpp -rdynamic -lpython3.5m -lGLU -lGL -lglfw -lrt -lm -ldl -lXrandr -lXinerama -lXi -lXcursor -lXrender -lGL -lm -lpthread -ldl -ldrm -lXdamage -lXfixes -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-randr -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb-sync -lxshmfence -lXxf86vm -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp -lpthread -lm -lbluetooth -ldl -lXrandr -lXinerama -lXi -lXcursor -lXrender -lGL -lpthread -ldrm -lXdamage -lXfixes -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-randr -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb-sync -lxshmfence -lXxf86vm -lXext -lX11 -lxcb -lXau -lXdmcp -lpthread -lbluetooth -o _oe.so
#! /usr/bin/python -i
import sys
sys.path+=['.']
from oe import *
root = cvar.root
import os
os.chdir('build')
def changeDocument(doc): # DO NOT RUN, SEGFAULT
#if root.document:
# del root.document
#if root.document:
# del root.document
root.document = doc
root.editDisplay.setDocument(root.document)
root.commandDisplay.setDocument(root.document)
root.controller = OE_controller(root.display, root.document)
root.commandDisplay.setController(root.controller)
def addAction(newAct):
root.controller.addAction(newAct)
newAct.thisown = False
def getCurve(n):
return root.controller.getCurve(n)
def newSubCurve(ncurve, start, end, reverse):
addAction(OE_actionNewSubCurve(getCurve(ncurve), start, end, reverse))
def newBirailStitch(ncurve1, ncurve2, reverse1, reverse2, offset1, offset2, len_):
addAction(OE_actionNewBirailStitch(getCurve(ncurve1), getCurve(ncurve2), reverse1, reverse2, offset1, offset2, len_))
def testLogo2():
root.document.setZeroPoint(vector_2d(0,1050))
#newSubCurve(14, 3, 0, True)
#newSubCurve(14, 1, 2, False)
#newBirailStitch(-1, -2, False, False, -0.2, -0.2, 0.5)
root.controller.newSubCurve(14,3,0,True)
root.controller.newSubCurve(14,1,2,False)
root.controller.newBirailStitch(-1, -2, False, False, -0.2, -0.2, 0.5)
......@@ -27,14 +27,14 @@ protected:
fd = open (name, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
printf ("error %d opening %s: %s", errno, name, strerror (errno));
printf ("error %d opening %s: %s\n", errno, name, strerror (errno));
return;
}
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tcgetattr", errno);
printf ("error %d from tcgetattr\n", errno);
return;
}
......@@ -64,13 +64,13 @@ protected:
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf ("error %d from tcsetattr", errno);
printf ("error %d from tcsetattr\n", errno);
return;
}
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tggetattr", errno);
printf ("error %d from tggetattr\n", errno);
return;
}
......@@ -78,7 +78,7 @@ protected:
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf ("error %d setting term attributes", errno);
printf ("error %d setting term attributes\n", errno);
}
else
{ // try bluetooth open
......
......@@ -49,25 +49,36 @@ class OE_controller
/** Default destructor */
virtual ~OE_controller();
/** \brief to create a new curve. */
/** \brief to create a new curve. */
bool testLogo2();
bool testOpenEmbroider();
bool testStarTrek();
bool testRadio();
bool setDefault();
bool testStarTrek();
/** \brief Helpers */
void newSubCurve(OE_curve* curve, float curveStart, float curveEnd, bool reverse);
void newSubCurve(int ncurve, float curveStart, float curveEnd, bool reverse);
void newBirailStitch(OE_curve* curve1, OE_curve* curve2, bool reverse1, bool reverse2, float offset1, float offset2, float len);
void newBirailStitch(int ncurve1, int ncurve2, bool reverse1, bool reverse2, float offset1, float offset2, float len);
void newJoinCurve(OE_curve* curve1, OE_curve* curve2);
void newJoinCurve(int ncurve1, int ncurve2);
void newLineStitch(OE_curve* curve, float len, float width, unsigned motif, float offset);
void newLineStitch(int ncurve, float len, float width, unsigned motif, float offset);
void setStitchThread(OE_stitchs* stitch, OE_thread * thread);
void setStitchThread(int nstitch, int nthread);
bool setDefault();
OE_curve* getCurve (unsigned index);
OE_pointcurve* getPointcurve (unsigned index);
OE_subcurve* getSubcurve (unsigned index);
OE_joincurve* getJoincurve (unsigned index);
/** \brief return linestitch if the index is pointing on a linestitch, else return null */
OE_stitchs* getStitch (unsigned index);
OE_linestitch* getLineStitch (unsigned index);
OE_birailstitch* getBirailStitch (unsigned index);
OE_linkstitch* getLinkStitch (unsigned index);
OE_thread* getThread (unsigned index);
OE_curve* getCurve (int index);
OE_pointcurve* getPointcurve (unsigned index);
OE_subcurve* getSubcurve (unsigned index);
OE_joincurve* getJoincurve (unsigned index);
/** \brief return linestitch if the index is pointing on a linestitch, else return null */
OE_stitchs* getStitch (int index);
OE_linestitch* getLineStitch (unsigned index);
OE_birailstitch* getBirailStitch (unsigned index);
OE_linkstitch* getLinkStitch (unsigned index);
OE_thread* getThread (int index);
bool addAction(OE_actions * action);
bool undoAction();
......
......@@ -29,16 +29,12 @@
class OE_svgParser
{
public:
/** Default constructor */
OE_svgParser();
/** Default destructor */
virtual ~OE_svgParser();
/** Parse a svg file */
OE_document * ParseFile(const std::string filename, const char* units, float dpi);
static OE_document * fromFile(const std::string filename, const char* units, float dpi);
protected:
private:
/** Default constructor */
OE_svgParser();
char id[64]; // Optional 'id' attr of the curve or its group
float opacity; // Opacity of the curve.
......
#pragma once
#include "OE_document.h"
#include "OE_interfaceDisplay.h"
#include "OE_controller.h"
struct OE_root
{
OE_document* document;
OE_display* display;
OE_interfaceDisplay* interfaceDisplay;
OE_controller* controller;
OE_root() : document(0), display(0), interfaceDisplay(0), controller(0) {}
};
void startMain();
extern OE_root root;
//
// Copyright (c) 2013 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <GLFW/glfw3.h>
#include <algorithm>
#include "main.h"
#include "OE_document.h"
#include "OE_svgParser.h"
#include "OE_interfaceDisplay.h"
#include "OE_controller.h"
#include <iostream>
#include <chrono>
#include <thread>
// TODO: fix qwerty mapping
// TODO: handlers, qui a besoin de qui et quand, pas de redondance ? chemins les + simples ?
// TODO: lock pour la modif, histoire de péter tout le backend sans que l'ihm explose
//#include "nanosvg.h"
//NSVGimage* g_image = NULL;
OE_document* document = NULL;
OE_display* display = NULL;
OE_interfaceDisplay* interfaceDisplay = NULL;
OE_controller* controller = NULL;
OE_root root;
//static unsigned char bgColor[4] = {205,202,200,255};
......@@ -44,116 +31,154 @@ void drawframe(GLFWwindow* window)
//NSVGshape* shape;
//NSVGpath* path;
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
if (display)
if (root->display)
{
display->draw();
root->display->draw();
}
glfwSwapBuffers(window);
}
static void My_Mouse_Pos_Callback(GLFWwindow* window, double x, double y)
static void OE_Mouse_Pos_Callback(GLFWwindow* window, double x, double y)
{
if (display) display->mouse_Pos(x, y);
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
if (root->display)
root->display->mouse_Pos(x, y);
}
static void My_Mouse_Button_Callback(GLFWwindow* window, int button, int action, int mods)
static void OE_Mouse_Button_Callback(GLFWwindow* window, int button, int action, int mods)
{
if (display) display->mouse_Button(button, action, mods);
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
if (root->display)
root->display->mouse_Button(button, action, mods);
}
void My_Scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
static void OE_Mouse_Scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
if (display) display->scroll(xoffset, yoffset);
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
if (root->display)
root->display->scroll(xoffset, yoffset);
}
void My_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
static void OE_Key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (display) display->key(key, scancode, action, mods);
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
if (root->display)
root->display->key(key, scancode, action, mods);
}
void resizecb(GLFWwindow* window, int width, int height)
static void OE_Window_Resize_callback(GLFWwindow* window, int, int)
{
if (display)
OE_root* root = (OE_root*)glfwGetWindowUserPointer(window);
if (root->display)
{
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
display->resize(width, height);
root->display->resize(width, height);
drawframe(window);
}
}
void startMain()
{
#ifdef SWINGING
int mainIhm();
std::thread newThread(mainIhm);
newThread.detach();
#endif
}
#ifdef SWINGING
int mainIhm()
#else
int main()
#endif
{
GLFWwindow* window;
const GLFWvidmode* mode;
std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now();
#define SHOWTIME() printf("%u: %ldms\n",\
__LINE__,\
std::chrono::duration_cast<std::chrono::milliseconds>(\
std::chrono::high_resolution_clock::now()-t0).count())
if (!glfwInit())
return -1;
SHOWTIME();
mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Open embroider", NULL, NULL);
window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Open embroider", NULL, NULL);
if (!window)
{
printf("Could not open window\n");
printf("Could not open window : %s\n", strerror(errno));
glfwTerminate();
return -1;
}
glfwSetWindowUserPointer(window, &root);
glfwSetFramebufferSizeCallback(window, resizecb);
glfwSetCursorPosCallback(window, My_Mouse_Pos_Callback);
glfwSetMouseButtonCallback(window, My_Mouse_Button_Callback);
glfwSetScrollCallback(window, My_Scroll_callback);
glfwSetKeyCallback(window, My_key_callback);
SHOWTIME();
// Enable sticky keys
glfwSetInputMode(window, GLFW_STICKY_KEYS, true);
glfwSetFramebufferSizeCallback(window, OE_Window_Resize_callback);
glfwSetCursorPosCallback(window, OE_Mouse_Pos_Callback);
glfwSetMouseButtonCallback(window, OE_Mouse_Button_Callback);
glfwSetScrollCallback(window, OE_Mouse_Scroll_callback);
glfwSetKeyCallback(window, OE_Key_callback);
// Disable vertical sync (on cards that support it)
glfwSwapInterval( 1 );
SHOWTIME();
// Enable sticky keys
glfwSetInputMode(window, GLFW_STICKY_KEYS, true);
// Disable vertical sync (on cards that support it)
glfwSwapInterval( 1 );
glfwMakeContextCurrent(window);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
OE_svgParser * parser = new OE_svgParser();
document = parser->ParseFile("../motifs/logo3.svg", "px", 96.0f);
//document = parser->ParseFile("../motifs/open_embroider_em2.svg", "px", 96.0f);
//document = parser->ParseFile("../motifs/startrek_small.svg", "px", 96.0f);
delete parser;
if (document == NULL) {
printf("Could not open SVG image.\n");
glfwTerminate();
return -1;
}
interfaceDisplay = new OE_interfaceDisplay();
interfaceDisplay->setDocument(document);
display = interfaceDisplay;
controller = new OE_controller(display, document);
controller->testLogo2();
//controller->testOpenEmbroider();
//controller->testStarTrek();
if ((controller == NULL)||(display == NULL) ) {
glfwTerminate();
return -1;
}
display->setController(controller);
glEnable(GL_LINE_SMOOTH);
SHOWTIME();
root.document = OE_svgParser::fromFile("../motifs/logo3.svg", "px", 96.0f);
//root.document = OE_svgParser::fromFile("../motifs/open_embroider_em2.svg", "px", 96.0f);
//root.document = OE_svgParser::fromFile("../motifs/startrek_small.svg", "px", 96.0f);
if (root.document == NULL) {
printf("Could not open SVG image.\n");
glfwTerminate();
return -1;
}
root.interfaceDisplay = new OE_interfaceDisplay();
root.interfaceDisplay->setDocument(root.document);
root.display = root.interfaceDisplay;
root.controller = new OE_controller(root.display, root.document);
root.controller->testLogo2();
//root.controller->testOpenEmbroider();
//root.controller->testStarTrek();
if ((root.controller == NULL)||(root.display == NULL) ) {
glfwTerminate();
return -1;
}
root.display->setController(root.controller);
{
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
display->resize(width, height);
display->draw();
display->showAll();
root.display->resize(width, height);
root.display->draw();
root.display->showAll();
}
SHOWTIME();
while (!glfwWindowShouldClose(window))
{
......
oe.i 0 → 100644
%include "std_string.i"
%include "std_vector.i"
%include "std_list.i"
%module oe
%{
#include "include/OE_document.h"
#include "include/OE_utils.h"
#include "include/OE_base.h"
#include "include/OE_display.h"
#include "include/OE_commandDisplay.h"
#include "include/OE_interfaceDisplay.h"
#include "include/OE_editDisplay.h"
#include "include/OE_thread.h"
#include "include/OE_controller.h"
#include "include/OE_instruction.h"
#include "include/OE_svgParser.h"
#include "include/instructions/OE_instruction.h"
#include "include/instructions/OE_waitcolor.h"
#include "include/instructions/OE_start.h"
#include "include/curves/OE_curve.h"
#include "include/curves/OE_subcurve.h"
#include "include/curves/OE_pointcurve.h"
#include "include/curves/OE_joincurve.h"
#include "include/xml/tinyxml.h"
#include "include/xml/tinystr.h"
#include "include/stitchs/OE_stitchs.h"
#include "include/stitchs/OE_linkstitch.h"
#include "include/stitchs/OE_birailstitch.h"
#include "include/stitchs/OE_linestitch.h"
#include "include/actions/OE_actions.h"
#include "include/actions/OE_actionsThreads.h"
#include "include/actions/OE_actionsBirailStitchs.h"
#include "include/actions/OE_actionsCurves.h"
#include "include/actions/OE_actionsStitchs.h"
#include "include/actions/OE_actionsSelection.h"
#include "include/actions/OE_actionsLineStitchs.h"
#include "include/main.h"
%}
%include "include/OE_document.h"
%include "include/OE_utils.h"
%include "include/OE_base.h"
%include "include/OE_display.h"
%include "include/OE_commandDisplay.h"
%include "include/OE_interfaceDisplay.h"
%include "include/OE_editDisplay.h"
%include "include/OE_thread.h"
%include "include/OE_controller.h"
%include "include/OE_instruction.h"
%include "include/OE_svgParser.h"
%include "include/instructions/OE_instruction.h"
%include "include/instructions/OE_waitcolor.h"
%include "include/instructions/OE_start.h"
%include "include/curves/OE_curve.h"
%include "include/curves/OE_subcurve.h"
%include "include/curves/OE_pointcurve.h"
%include "include/curves/OE_joincurve.h"
%include "include/xml/tinyxml.h"
%include "include/xml/tinystr.h"
%include "include/stitchs/OE_stitchs.h"
%include "include/stitchs/OE_linkstitch.h"
%include "include/stitchs/OE_birailstitch.h"
%include "include/stitchs/OE_linestitch.h"
%include "include/actions/OE_actions.h"
%include "include/actions/OE_actionsThreads.h"
%include "include/actions/OE_actionsBirailStitchs.h"
%include "include/actions/OE_actionsCurves.h"
%include "include/actions/OE_actionsStitchs.h"
%include "include/actions/OE_actionsSelection.h"
%include "include/actions/OE_actionsLineStitchs.h"
%include "include/main.h"
/*
gcc -c oe.c oe_wrap.c -I/usr/local/include/python2.1
ld -shared oe.o eo_wrap.o -o _eo.so
import oe
swig -python -Iinclude -c++ oe.i
g++ -fPIC -shared -std=c++11 -Iinclude $(pkg-config --cflags python3) main.cpp oe_wrap.cxx $(find src -name "*.cpp") -rdynamic -lGLU -lGL -lglfw -lrt -lm -ldl -lXrandr -lXinerama -lXi -lXcursor -lXrender -lGL -lm -lpthread -ldl -ldrm -lXdamage -lXfixes -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-randr -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb-sync -lxshmfence -lXxf86vm -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp -lpthread -lm -lbluetooth -ldl -lXrandr -lXinerama -lXi -lXcursor -lXrender -lGL -lpthread -ldrm -lXdamage -lXfixes -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-randr -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb-sync -lxshmfence -lXxf86vm -lXext -lX11 -lxcb -lXau -lXdmcp -lpthread -lbluetooth -o _oe.so
*/
This diff is collapsed.
......@@ -37,9 +37,6 @@ OE_svgParser::OE_svgParser()
strokeLineCap = 0; // Stroke cap type.
}
OE_svgParser::~OE_svgParser()
{
}
float OE_svgParser::sqr(float x) { return x*x; }
float OE_svgParser::vmag(float x, float y) { return sqrtf(x*x + y*y); }
......@@ -461,8 +458,10 @@ int OE_svgParser::getArgsPerElement(char cmd)
return 0;
}
OE_document * OE_svgParser::ParseFile(const std::string filename, const char* units, float dpi)
OE_document * OE_svgParser::fromFile(const std::string filename, const char* units, float dpi)
{
OE_svgParser parser;
// Loading of the SVG file
TiXmlDocument doc(filename.c_str());
if ( ! doc.LoadFile() )
......@@ -476,11 +475,11 @@ OE_document * OE_svgParser::ParseFile(const std::string filename, const char* un
TiXmlElement *object = hdl.FirstChildElement("svg").Element();
document = new OE_document();
if (document)
parseSvg(object);
parser.document = new OE_document();
if (parser.document)
parser.parseSvg(object);
return document;
return parser.document;
}
bool OE_svgParser::parseSvg(TiXmlElement *input)
......@@ -659,4 +658,4 @@ bool OE_svgParser::pushCurve(OE_pointcurve * curve)
curve = nullptr;
return false;
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment