Skip to content

Commit

Permalink
Added a rendered box around imported structures
Browse files Browse the repository at this point in the history
This makes it easier to see what is actually selected
  • Loading branch information
gentlegiantJGC committed May 22, 2020
1 parent bbaf589 commit b2d064d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
78 changes: 46 additions & 32 deletions amulet_map_editor/opengl/mesh/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,61 @@
from amulet.api.data_types import BlockCoordinatesAny, BlockCoordinatesNDArray, PointCoordinatesAny


class RenderSelectionGroup(Drawable):
class RenderSelectionGroupStatic(Drawable):
def __init__(self,
context_identifier: str,
texture_bounds: Dict[Any, Tuple[float, float, float, float]],
texture: int
texture: int,
selection: SelectionGroup = None
):
self._context_identifier = context_identifier
self._texture_bounds = texture_bounds
self._texture = texture
self._boxes: List[RenderSelection] = []
if selection:
for box in selection.selection_boxes:
render_box = self._new_render_selection()
render_box.point1 = numpy.array(box.min) - selection.min
render_box.point2 = numpy.array(box.max) - selection.min - 1
self._boxes.append(render_box)

def _new_render_selection(self):
return RenderSelection(self._context_identifier, self._texture_bounds, self._texture)

def __iter__(self):
for box in self._boxes:
if box.is_static:
yield box

def __contains__(self, position: BlockCoordinatesAny):
return any(position in box for box in self._boxes)

def __getitem__(self, index: int) -> "RenderSelection":
return self._boxes[index]

def create_selection_group(self) -> SelectionGroup:
return SelectionGroup([
SelectionBox(box.min, box.max) for box in self._boxes if box.is_static
])

def draw(self, transformation_matrix: numpy.ndarray, camera_position: PointCoordinatesAny = None):
for index, box in enumerate(self._boxes):
box.draw(transformation_matrix, camera_position)


class RenderSelectionGroup(RenderSelectionGroupStatic):
def __init__(self,
context_identifier: str,
texture_bounds: Dict[Any, Tuple[float, float, float, float]],
texture: int
):
super().__init__(context_identifier, texture_bounds, texture)
self._active: Optional[int] = None
self._temp_box = self._new_render_selection()


self._last_position = numpy.array([0, 0, 0], dtype=numpy.int)
self._hover_box: Optional[int] = None

def _new_render_selection(self):
return RenderSelection(self._context_identifier, self._texture_bounds, self._texture)

def _add_render_selection(self):
self._boxes.append(
self._new_render_selection()
)

def _merge_temp_box(self):
"""
Duplicate the temporary selection, append and activate it.
Expand Down Expand Up @@ -88,27 +118,16 @@ def box_click(self, add_modifier: bool = False) -> Optional[BlockCoordinatesNDAr
else:
active_selection.lock()

def __iter__(self):
for box in self._boxes:
if box.is_static:
yield box

def __contains__(self, position: BlockCoordinatesAny):
return any(position in box for box in self._boxes)

def __getitem__(self, index: int) -> "RenderSelection":
return self._boxes[index]

@property
def active_selection(self) -> Optional["RenderSelection"]:
if self._active is not None:
return self._boxes[self._active]

def draw(self, transformation_matrix: numpy.ndarray, active: bool, camera_position: PointCoordinatesAny):
def draw(self, transformation_matrix: numpy.ndarray, camera_position: PointCoordinatesAny = None, active: bool = False):
for index, box in enumerate(self._boxes):
box.draw(transformation_matrix, active and index == self._active, camera_position)
box.draw(transformation_matrix, camera_position, active and index == self._active)
if self.active_selection is None or self.active_selection.is_static:
self._temp_box.draw(transformation_matrix, False, camera_position=camera_position)
self._temp_box.draw(transformation_matrix, camera_position)

def closest_intersection(self, origin: PointCoordinatesAny, vector: PointCoordinatesAny) -> Tuple[Optional[int], Optional["RenderSelection"]]:
"""
Expand All @@ -129,11 +148,6 @@ def closest_intersection(self, origin: PointCoordinatesAny, vector: PointCoordin
box_return = box
return index_return, box_return

def create_selection_group(self) -> SelectionGroup:
return SelectionGroup([
SelectionBox(box.min, box.max) for box in self._boxes if box.is_static
])


class RenderSelection(TriMesh):
def __init__(self,
Expand Down Expand Up @@ -340,12 +354,12 @@ def _create_geometry(self):
self._volume = numpy.product(self.max - self.min)
self._rebuild = False

def draw(self, transformation_matrix: numpy.ndarray, active=False, camera_position: PointCoordinatesAny = None):
def draw(self, transformation_matrix: numpy.ndarray, camera_position: PointCoordinatesAny = None, active=False):
"""
Draw the selection box
:param transformation_matrix: 4x4 transformation matrix for the camera
:param active: If the selection box is the active selection (draw corner boxes)
:param camera_position: The position of the camera. Used to flip draw direction if camera inside box.
:param active: If the selection box is the active selection (draw corner boxes)
:return:
"""
self._setup()
Expand Down
3 changes: 3 additions & 0 deletions amulet_map_editor/opengl/mesh/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from amulet_map_editor.opengl.mesh.base.chunk_builder import RenderChunkBuilder
from amulet_map_editor.opengl.resource_pack import ResourcePackManager
from amulet_map_editor.opengl.mesh.selection import RenderSelectionGroupStatic


class SubRenderStructure(RenderChunkBuilder):
Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(
super().__init__(context_identifier, resource_pack, texture, texture_bounds, translator)
self._structure = structure
self._sub_structures = []
self._selection = RenderSelectionGroupStatic(context_identifier, texture_bounds, texture, structure.selection)
self._create_geometry() # TODO: move this to a different thread

@property
Expand All @@ -89,3 +91,4 @@ def draw(self, transformation_matrix: numpy.ndarray, cam_cx, cam_cz):
reverse=True
):
chunk.draw(transformation_matrix)
self._selection.draw(transformation_matrix)
2 changes: 1 addition & 1 deletion amulet_map_editor/programs/edit/canvas/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def draw(self):
for location in self.structure_locations:
transform[3, 0:3] = location
self._structure.draw(numpy.matmul(transform, self.transformation_matrix), 0, 0)
self._selection_group.draw(self.transformation_matrix, self._select_mode == MODE_NORMAL, tuple(self.camera_location))
self._selection_group.draw(self.transformation_matrix, tuple(self.camera_location), self._select_mode == MODE_NORMAL)
self.SwapBuffers()

def _gc(self, event):
Expand Down

0 comments on commit b2d064d

Please sign in to comment.