Refactor: Move Operation to its own file
This commit is contained in:
parent
dfab4a7af7
commit
a4e8a632fc
@ -21,6 +21,8 @@ import tempfile
|
|||||||
import itertools
|
import itertools
|
||||||
from toolbasics import *
|
from toolbasics import *
|
||||||
import logging
|
import logging
|
||||||
|
from operation import Operation
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
||||||
|
from operation import Operation
|
||||||
from pymclevel.box import Vector
|
from pymclevel.box import Vector
|
||||||
|
|
||||||
from toolbasics import *
|
from toolbasics import *
|
||||||
|
@ -11,6 +11,7 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
||||||
|
from operation import Operation
|
||||||
|
|
||||||
from toolbasics import *
|
from toolbasics import *
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
||||||
|
from operation import Operation
|
||||||
from toolbasics import *
|
from toolbasics import *
|
||||||
from albow.dialogs import wrapped_label
|
from albow.dialogs import wrapped_label
|
||||||
from albow import *
|
from albow import *
|
||||||
|
57
editortools/operation.py
Normal file
57
editortools/operation.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import atexit
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
import pymclevel
|
||||||
|
from mceutils import showProgress
|
||||||
|
|
||||||
|
class Operation(object):
|
||||||
|
changedLevel = True
|
||||||
|
undoLevel = None
|
||||||
|
|
||||||
|
def __init__(self, editor, level):
|
||||||
|
self.editor = editor
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
def extractUndo(self, level, box):
|
||||||
|
undoPath = tempfile.mkdtemp("mceditundo")
|
||||||
|
undoLevel = pymclevel.MCInfdevOldLevel(undoPath, create=True)
|
||||||
|
atexit.register(shutil.rmtree, undoPath, True)
|
||||||
|
|
||||||
|
def _extractUndo():
|
||||||
|
yield 0, 0, "Recording undo..."
|
||||||
|
for i, (cx, cz) in enumerate(box.chunkPositions):
|
||||||
|
undoLevel.copyChunkFrom(level, cx, cz)
|
||||||
|
yield i, box.chunkCount, "Copying chunk %s..." % ((cx, cz),)
|
||||||
|
undoLevel.saveInPlace()
|
||||||
|
|
||||||
|
showProgress("Recording undo...", _extractUndo())
|
||||||
|
|
||||||
|
return undoLevel
|
||||||
|
|
||||||
|
# represents a single undoable operation
|
||||||
|
def perform(self, recordUndo=True):
|
||||||
|
" Perform the operation. Record undo information if recordUndo"
|
||||||
|
|
||||||
|
def undo(self):
|
||||||
|
""" Undo the operation. Ought to leave the Operation in a state where it can be performed again.
|
||||||
|
Default implementation copies all chunks in undoLevel back into level. Non-chunk-based operations
|
||||||
|
should override this."""
|
||||||
|
|
||||||
|
if self.undoLevel:
|
||||||
|
|
||||||
|
def _undo():
|
||||||
|
yield 0, 0, "Undoing..."
|
||||||
|
for i, (cx, cz) in enumerate(self.undoLevel.allChunks):
|
||||||
|
self.level.copyChunkFrom(self.undoLevel, cx, cz)
|
||||||
|
yield i, self.undoLevel.chunkCount, "Copying chunk %s..." % ((cx, cz),)
|
||||||
|
|
||||||
|
|
||||||
|
showProgress("Undoing...", _undo())
|
||||||
|
self.editor.invalidateChunks(self.undoLevel.allChunks)
|
||||||
|
|
||||||
|
|
||||||
|
def dirtyBox(self):
|
||||||
|
""" The region modified by the operation.
|
||||||
|
Return None to indicate no blocks were changed.
|
||||||
|
"""
|
||||||
|
return None
|
@ -11,6 +11,7 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
||||||
|
from operation import Operation
|
||||||
|
|
||||||
from toolbasics import *
|
from toolbasics import *
|
||||||
import urllib
|
import urllib
|
||||||
|
@ -13,6 +13,7 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from operation import Operation
|
||||||
from pymclevel.box import Vector
|
from pymclevel.box import Vector
|
||||||
from fill import FillTool, BlockFillOperation
|
from fill import FillTool, BlockFillOperation
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -16,6 +16,7 @@ import shutil
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
|
from operation import Operation
|
||||||
from pymclevel import *
|
from pymclevel import *
|
||||||
import pymclevel
|
import pymclevel
|
||||||
|
|
||||||
@ -97,59 +98,6 @@ class NudgeButton(GLBackground):
|
|||||||
self.nudge(Vector(*right))
|
self.nudge(Vector(*right))
|
||||||
|
|
||||||
|
|
||||||
class Operation(object):
|
|
||||||
changedLevel = True
|
|
||||||
undoLevel = None
|
|
||||||
|
|
||||||
def __init__(self, editor, level):
|
|
||||||
self.editor = editor
|
|
||||||
self.level = level
|
|
||||||
|
|
||||||
def extractUndo(self, level, box):
|
|
||||||
undoPath = tempfile.mkdtemp("mceditundo")
|
|
||||||
undoLevel = MCInfdevOldLevel(undoPath, create=True)
|
|
||||||
atexit.register(shutil.rmtree, undoPath, True)
|
|
||||||
|
|
||||||
def _extractUndo():
|
|
||||||
yield 0, 0, "Recording undo..."
|
|
||||||
for i, (cx, cz) in enumerate(box.chunkPositions):
|
|
||||||
undoLevel.copyChunkFrom(level, cx, cz)
|
|
||||||
yield i, box.chunkCount, "Copying chunk %s..." % ((cx, cz),)
|
|
||||||
undoLevel.saveInPlace()
|
|
||||||
|
|
||||||
showProgress("Recording undo...", _extractUndo())
|
|
||||||
|
|
||||||
return undoLevel
|
|
||||||
|
|
||||||
# represents a single undoable operation
|
|
||||||
def perform(self, recordUndo=True):
|
|
||||||
" Perform the operation. Record undo information if recordUndo"
|
|
||||||
|
|
||||||
def undo(self):
|
|
||||||
""" Undo the operation. Ought to leave the Operation in a state where it can be performed again.
|
|
||||||
Default implementation copies all chunks in undoLevel back into level. Non-chunk-based operations
|
|
||||||
should override this."""
|
|
||||||
|
|
||||||
if self.undoLevel:
|
|
||||||
|
|
||||||
def _undo():
|
|
||||||
yield 0, 0, "Undoing..."
|
|
||||||
for i, (cx, cz) in enumerate(self.undoLevel.allChunks):
|
|
||||||
self.level.copyChunkFrom(self.undoLevel, cx, cz)
|
|
||||||
yield i, self.undoLevel.chunkCount, "Copying chunk %s..." % ((cx, cz),)
|
|
||||||
|
|
||||||
|
|
||||||
showProgress("Undoing...", _undo())
|
|
||||||
self.editor.invalidateChunks(self.undoLevel.allChunks)
|
|
||||||
|
|
||||||
|
|
||||||
def dirtyBox(self):
|
|
||||||
""" The region modified by the operation.
|
|
||||||
Return None to indicate no blocks were changed.
|
|
||||||
"""
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class ToolOptions(Panel):
|
class ToolOptions(Panel):
|
||||||
@property
|
@property
|
||||||
def editor(self):
|
def editor(self):
|
||||||
|
@ -58,8 +58,8 @@ from albow.openglwidgets import GLOrtho, GLViewport
|
|||||||
from pygame import display, event, key, KMOD_ALT, KMOD_CTRL, KMOD_LALT, KMOD_META, KMOD_RALT, KMOD_SHIFT, mouse, MOUSEMOTION
|
from pygame import display, event, key, KMOD_ALT, KMOD_CTRL, KMOD_LALT, KMOD_META, KMOD_RALT, KMOD_SHIFT, mouse, MOUSEMOTION
|
||||||
|
|
||||||
from depths import DepthOffset
|
from depths import DepthOffset
|
||||||
|
from editortools.operation import Operation
|
||||||
from editortools.chunk import GeneratorPanel
|
from editortools.chunk import GeneratorPanel
|
||||||
from editortools.toolbasics import Operation
|
|
||||||
from glbackground import GLBackground, Panel
|
from glbackground import GLBackground, Panel
|
||||||
from glutils import gl, Texture
|
from glutils import gl, Texture
|
||||||
from mcplatform import askSaveFile
|
from mcplatform import askSaveFile
|
||||||
|
Reference in New Issue
Block a user