Permalink
Browse files

Rotation

  • Loading branch information...
feragon committed Jun 28, 2016
1 parent afff78a commit b87fb040d1471cad5764d96c997b7c49ba4973bd
Binary file not shown.
View
@@ -16,5 +16,6 @@
<file alias="quit.svg">icons/quit.svg</file>
<file alias="polylines.svg">icons/polylines.svg</file>
<file alias="modify.svg">icons/modify.svg</file>
<file alias="modifyrotate.png">icons/modifyrotate.png</file>
</qresource>
</RCC>
@@ -12,6 +12,7 @@ require 'actions.splineoperations'
require 'actions.lwpolylineoperations'
require 'actions.moveoperation'
require 'actions.rotateoperation'
Operations = {}
Operations.__index = Operations
@@ -0,0 +1,110 @@
RotateOperation = {}
RotateOperation.__index = RotateOperation
setmetatable(RotateOperation, {
__index = Operations,
__call = function (o, ...)
local self = setmetatable({}, o)
self:_init(...)
return self
end,
})
function RotateOperation:_init(id)
Operations._init(self, id)
self.selection = active_widget():selection()
message(tostring(#self.selection) .. " items selected")
if(#self.selection > 0) then
self.origin = nil
self.firstPoint = nil
self.secondPoint = nil
self.tempEntities = {}
event.register('point', self)
event.register('mouseMove', self)
message("Give origin point")
else
self.finished = true
event.trigger('operationFinished')
end
end
function RotateOperation:onEvent(eventName, ...)
if(Operations.forMe(self) == false) then
return
end
if(eventName == "point" or eventName == "number") then
self:newData(...)
elseif(eventName == "mouseMove") then
self:tempRotate(...)
end
end
function RotateOperation:newData(point)
if(self.origin == nil) then
self.origin = Operations:getCoordinate(point)
message("Give first point")
elseif(self.firstPoint == nil) then
self.firstPoint = Operations:getCoordinate(point)
message("Give second point")
elseif(Operations:getCoordinate(point) ~= nil) then
self.secondPoint = Operations:getCoordinate(point)
self:rotate()
end
end
function RotateOperation:tempRotate(point)
if(self.origin ~= nil and self.firstPoint ~= nil) then
for k, entity in pairs(self.tempEntities) do
active_widget():tempEntities():removeEntity(entity)
end
self.tempEntities = {}
local angle = self.origin:angleTo(point) - self.origin:angleTo(self.firstPoint)
for k, entity in pairs(self.selection) do
local newEntity = entity:rotate(self.origin, angle)
active_widget():tempEntities():addEntity(newEntity)
table.insert(self.tempEntities, newEntity)
end
end
end
function RotateOperation:rotate()
local angle = self.origin:angleTo(self.secondPoint) - self.origin:angleTo(self.firstPoint)
local b = Builder(active_widget():document())
for k, entity in pairs(self.selection) do
b:append(entity)
end
b:push()
b:rotate(self.origin, angle)
b:execute()
self:close()
end
function RotateOperation:close()
if(not self.finished) then
self.finished = true
for k, entity in pairs(self.tempEntities) do
active_widget():tempEntities():removeEntity(entity)
end
event.delete('mouseMove', self)
event.delete('point', self)
event.trigger('operationFinished')
end
end
@@ -65,6 +65,7 @@ function add_commandline()
add_command("SPLINE", create_spline)
add_command("MOVE", move_selected_entities)
add_command("ROTATE", rotate_selected_entities)
event.register("point", setLastPoint)
end
@@ -113,4 +113,9 @@ end
function move_selected_entities()
new_operation()
op[active_widget().id] = MoveOperation(active_widget().id)
end
function rotate_selected_entities()
new_operation()
op[active_widget().id] = RotateOperation(active_widget().id)
end
View
@@ -100,4 +100,8 @@ function add_toolbar()
local moveButton = create_button("", ":/icons/modify.svg")
quickAccess:addButton(modifyGroup, moveButton, 0, 0, 1, 1)
luaInterface:luaConnect(moveButton, "pressed()", move_selected_entities)
local rotateButton = create_button("", ":/icons/modifyrotate.png")
quickAccess:addButton(modifyGroup, rotateButton, 1, 0, 1, 1)
luaInterface:luaConnect(rotateButton, "pressed()", rotate_selected_entities)
end
@@ -175,6 +175,7 @@ void lua_openlckernel(lua_State* L) {
.endClass()
.beginExtendClass<entity::CADEntity, ID>("CADEntity")
.addFunction("move", &entity::CADEntity::move)
.addFunction("rotate", &entity::CADEntity::rotate)
.endClass()
.beginExtendClass<entity::Line, entity::CADEntity>("Line")
.addConstructor(LUA_SP(entity::Line_SPtr), LUA_ARGS(

0 comments on commit b87fb04

Please sign in to comment.