Skip to content

Commit

Permalink
Use Enum for cell type
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorgious56 committed Mar 23, 2021
1 parent f4dec43 commit 6498d33
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 50 deletions.
8 changes: 4 additions & 4 deletions Maze Generator/blender/drivers/objects/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
DriverProperties,
DriverVariable,
)
from ....maze_logic.cells import SQUARE, TRIANGLE, HEXAGON, OCTOGON
from ....maze_logic.cells import CellType


def setup_drivers(scene, props):
Expand All @@ -31,14 +31,14 @@ def setup_drivers(scene, props):
# Scale the cylinder and torus objects when scaling the size of the maze
for i, obj in enumerate((obj_cylinder, obj_torus)):
exp = 'var * 0.314'
if props.cell_type == SQUARE or props.cell_type == OCTOGON:
if props.is_cell_type(CellType.SQUARE) or props.is_cell_type(CellType.OCTOGON):
exp = 'var * 0.15915'
elif props.cell_type == TRIANGLE:
elif props.is_cell_type(CellType.TRIANGLE):
if i == 0:
exp = 'var * 0.07963'
else:
exp = 'var * 0.13791'
elif props.cell_type == HEXAGON:
elif props.is_cell_type(CellType.HEXAGON):
if i == 0:
exp = 'var * 0.2388'
else:
Expand Down
4 changes: 2 additions & 2 deletions Maze Generator/blender/drivers/objects/walls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Stores all the drivers used in the walls objects
Stores all the drivers used in the walls object
"""

from ..methods import (
Expand Down Expand Up @@ -48,6 +48,6 @@ def setup_drivers(scene, props):
setup_driver_from_addon_props(
obj_walls, names.weave_disp, 'strength', scene, "wall_height", '-var')
setup_driver_from_addon_props(
obj_walls, names.bevel, 'harden_normals', scene, "cell_use_smooth", 'var')
obj_walls, names.bevel, 'harden_normals', scene, "meshes_use_smooth", 'var')
setup_driver_from_addon_props(
obj_walls, names.screw, 'use_smooth_shade', scene, "wall_bevel", 'var > 0.005')
4 changes: 2 additions & 2 deletions Maze Generator/blender/modifiers/objects/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
from ..methods import add_modifier, ModifierCreator
from ... import meshes as cv
from ....maze_logic.cells import TRIANGLE
from ....maze_logic.cells import CellType


def setup_modifiers(scene, props) -> None:
Expand Down Expand Up @@ -106,7 +106,7 @@ def setup_modifiers(scene, props) -> None:
name=names.moebius,
properties={
"angle": 2 * math.pi +
(1 / 18 if props.cell_type == TRIANGLE else 0),
(1 / 18 if props.is_cell_type(CellType.TRIANGLE) else 0),
}
))
if ow or not obj_cells.modifiers.get(names.cylinder):
Expand Down
4 changes: 2 additions & 2 deletions Maze Generator/blender/modifiers/objects/walls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
from ..methods import add_modifier, ModifierCreator
from ... import meshes as cv
from ....maze_logic.cells import TRIANGLE
from ....maze_logic.cells import CellType


def setup_modifiers(scene, props) -> None:
Expand Down Expand Up @@ -134,7 +134,7 @@ def setup_modifiers(scene, props) -> None:
_type='SIMPLE_DEFORM',
name=names.moebius,
properties={
'angle': 2 * math.pi + (1 / 16 if props.cell_type == TRIANGLE else 0),
'angle': 2 * math.pi + (1 / 16 if props.is_cell_type(CellType.TRIANGLE) else 0),
}
),
ModifierCreator(
Expand Down
2 changes: 1 addition & 1 deletion Maze Generator/blender/operators/op_sample_mazes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sample(context):
(
mg_props,
'cell_type',
(cells.HEXAGON, cells.POLAR, cells.SQUARE, cells.TRIANGLE),
(cells.CellType.HEXAGON.value, cells.CellType.POLAR.value, cells.CellType.SQUARE.value, cells.CellType.TRIANGLE.value),
20, 17,
),
(
Expand Down
10 changes: 7 additions & 3 deletions Maze Generator/blender/properties/mg_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All the add-on properties and callbacks are stored here
"""

from enum import Enum
import bpy.ops
from bpy.types import PropertyGroup, Scene
from bpy.props import (
Expand Down Expand Up @@ -70,12 +71,12 @@ class SpaceRepsPropertyGroup(PropertyGroup):
def generate_space_rep_enum(self, context):
space_reps = self.space_reps
ret = [(space_reps.regular, 'Plane', '')]
if self.cell_type != POLAR:
if not self.is_cell_type(CellType.POLAR):
ret.extend((
(space_reps.cylinder, 'Cylinder', ''),
(space_reps.moebius, 'Moebius', ''),
(space_reps.torus, 'Torus', '')))
# (space_reps.box, 'Box', '')))
# (space_reps.box, 'Box', '')))
return ret


Expand Down Expand Up @@ -300,7 +301,7 @@ class MGProperties(PropertyGroup):
default=10,
min=2,
soft_max=100,
update=generate_maze
update=generate_maze,
)

maze_columns_gizmo: FloatProperty(
Expand Down Expand Up @@ -461,6 +462,9 @@ class MGProperties(PropertyGroup):
default=False,
)

def is_cell_type(self, cell_type: Enum) -> bool:
return self.cell_type == cell_type.value

def register():
Scene.mg_props = PointerProperty(type=MGProperties)

Expand Down
6 changes: 3 additions & 3 deletions Maze Generator/maze_logic/algorithms/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Algorithm manager methods and properties
"""

from ..cells import POLAR, SQUARE
from ..cells import CellType

# Algorithms :
from .maze_algorithm import MazeAlgorithm
Expand Down Expand Up @@ -47,13 +47,13 @@ def is_algo_incompatible(props):
return "Aldous-Broder can't solve a box representation"
if props.maze_space_dimension == props.space_reps.box and props.maze_weave:
return "Can't solve weaved maze for a box"
if algorithm_class_from_name(props.maze_algorithm) in (RecursiveDivision, VoronoiDivision) and props.cell_type == POLAR:
if algorithm_class_from_name(props.maze_algorithm) in (RecursiveDivision, VoronoiDivision) and props.is_cell_type(CellType.POLAR):
return "Can't solve this algorithm with Polar grid (yet)"
return False


def is_algo_weaved(props):
return props.cell_type == SQUARE and props.maze_algorithm in WEAVED_ALGORITHMS
return props.is_cell_type(CellType.SQUARE) and props.maze_algorithm in WEAVED_ALGORITHMS


def is_kruskal_random(algo_name):
Expand Down
28 changes: 15 additions & 13 deletions Maze Generator/maze_logic/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
"""

import random
from enum import Enum
from ..utils import event
from . import constants as cst


POLAR = '0'
TRIANGLE = '1'
SQUARE = '2'
HEXAGON = '3'
OCTOGON = '4'
DODECAGON = '5'
class CellType(Enum):
POLAR = '0'
TRIANGLE = '1'
SQUARE = '2'
HEXAGON = '3'
OCTOGON = '4'
DODECAGON = '5'

DEFAULT_CELL_TYPE = SQUARE
DEFAULT_CELL_TYPE = CellType.SQUARE.value


def generate_cell_type_enum():
return [(POLAR, 'Polar', ''),
(TRIANGLE, 'Triangle', ''),
(SQUARE, 'Square', ''),
(HEXAGON, 'Hexagon', ''),
(OCTOGON, 'Octogon', ''),
(DODECAGON, 'Dodecagon', ''),
return [(CellType.POLAR.value, 'Polar', ''),
(CellType.TRIANGLE.value, 'Triangle', ''),
(CellType.SQUARE.value, 'Square', ''),
(CellType.HEXAGON.value, 'Hexagon', ''),
(CellType.OCTOGON.value, 'Octogon', ''),
(CellType.DODECAGON.value, 'Dodecagon', ''),
]


Expand Down
12 changes: 6 additions & 6 deletions Maze Generator/maze_logic/grids/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


from .. import cells as ct
from ..cells import CellType as ct
from .polar import GridPolar
from .hex import GridHex
from .triangle import GridTriangle
Expand All @@ -21,21 +21,21 @@ def generate_grid(props) -> None:
space_reps = props.space_reps
warp_horiz = maze_dimension in (space_reps.cylinder, space_reps.moebius, space_reps.torus)
warp_vert = maze_dimension == space_reps.torus
if props.cell_type == ct.POLAR:
if props.is_cell_type(ct.POLAR):
return GridPolar(
rows=props.maze_rows_or_radius,
columns=0,
levels=props.maze_levels,
cell_size=1 - props.cell_inset,
branch_polar=props.maze_polar_branch)

if props.cell_type == ct.HEXAGON:
if props.is_cell_type(ct.HEXAGON):
grid = GridHex
elif props.cell_type == ct.TRIANGLE:
elif props.is_cell_type(ct.TRIANGLE):
grid = GridTriangle
elif props.cell_type == ct.OCTOGON:
elif props.is_cell_type(ct.OCTOGON):
grid = GridOctogon
elif props.cell_type == ct.DODECAGON:
elif props.is_cell_type(ct.DODECAGON):
grid = GridDodecagon
else:
if props.maze_weave:
Expand Down
25 changes: 11 additions & 14 deletions Maze Generator/ui/panels/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


import bpy
from ...maze_logic import cells as cell_mgr
from ...maze_logic.cells import CellType
from ...maze_logic.algorithms.manager import algorithm_class_from_name, KruskalRandom, is_algo_incompatible

from ...blender.operators.op_tweak_maze_size import MG_OT_TweakMazeSize
Expand All @@ -31,15 +31,12 @@ def draw(self, context):
mg_props = scene.mg_props
space_reps = mg_props.space_reps

cell_enum_icon = 'MESH_PLANE'
if mg_props.cell_type == cell_mgr.POLAR:
cell_enum_icon = 'MESH_CIRCLE'
elif mg_props.cell_type == cell_mgr.TRIANGLE:
cell_enum_icon = 'OUTLINER_OB_MESH'
elif mg_props.cell_type == cell_mgr.HEXAGON:
cell_enum_icon = 'SEQ_CHROMA_SCOPE'
elif mg_props.cell_type == cell_mgr.OCTOGON:
cell_enum_icon = 'MESH_ICOSPHERE'
cell_enum_icon = {
CellType.POLAR.value: 'MESH_CIRCLE',
CellType.TRIANGLE.value: 'OUTLINER_OB_MESH',
CellType.HEXAGON.value: 'SEQ_CHROMA_SCOPE',
CellType.OCTOGON.value: 'MESH_ICOSPHERE',
}.get(mg_props.cell_type, 'MESH_PLANE')

layout.prop_menu_enum(mg_props, 'cell_type', icon=cell_enum_icon)
layout.prop(mg_props, 'maze_algorithm', icon='HAND', text='Solver')
Expand All @@ -59,10 +56,10 @@ def draw(self, context):
box.prop(mg_props, 'maze_weave_toggle', toggle=True)
else:
box.prop(mg_props, setting)
if mg_props.cell_type == cell_mgr.POLAR and mg_props.maze_space_dimension in (space_reps.cylinder, space_reps.moebius, space_reps.torus, space_reps.moebius):
if mg_props.is_cell_type(CellType.POLAR) and mg_props.maze_space_dimension in (space_reps.cylinder, space_reps.moebius, space_reps.torus, space_reps.moebius):
layout.label(
text='Only Regular and Stairs for Polar cells', icon='ERROR')
elif mg_props.cell_type in (cell_mgr.TRIANGLE, cell_mgr.HEXAGON, cell_mgr.OCTOGON):
elif mg_props.is_cell_type(CellType.TRIANGLE) or mg_props.is_cell_type(CellType.HEXAGON) or mg_props.is_cell_type(CellType.OCTOGON):
if mg_props.maze_space_dimension in (space_reps.cylinder, space_reps.moebius, space_reps.torus):
layout.label(
text='Needs PAIR Columns (2, 4, 6, ...)', icon='ERROR')
Expand All @@ -89,11 +86,11 @@ def maze_size_ui(prop_name, decrease, increase, text):
sub.scale_x = 10.0

sub = row.row()
sub.operator('maze.tweak_maze_size', text='',
sub.operator(MG_OT_TweakMazeSize.bl_idname, text='',
icon='ADD').tweak_size = increase
return row

if mg_props.cell_type != cell_mgr.POLAR:
if mg_props.is_cell_type(CellType.POLAR):
maze_size_ui('maze_columns', [-1, 0, 0], [1, 0, 0], 'Columns')
else:
layout.prop(mg_props, 'maze_polar_branch', text='Branch amount')
Expand Down

0 comments on commit 6498d33

Please sign in to comment.