started reworking image generation
This commit is contained in:
@@ -11,9 +11,16 @@ class Iinstruction:
|
||||
self.instruction_text = instruction_text
|
||||
|
||||
@abstractmethod
|
||||
def to_image(self, x:float, y:float, x_sz: float, y_sz: float) -> Iterable[float]:
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize()
|
||||
|
||||
def _getblksize(self) -> float:
|
||||
return cti.get_text_size(self.instruction_text)[1]
|
||||
|
||||
|
||||
class generic_instruction(Iinstruction):
|
||||
"""Any instruction that is not a control structure"""
|
||||
@@ -21,9 +28,8 @@ class generic_instruction(Iinstruction):
|
||||
def __init__(self, instruction_text: str) -> None:
|
||||
Iinstruction.__init__(self, instruction_text)
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int, y_sz: int) -> Iterable[float]:
|
||||
new_x, new_y = cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, y_sz)
|
||||
return new_x, new_y
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
|
||||
|
||||
|
||||
@@ -38,31 +44,40 @@ class if_instruction(Iinstruction):
|
||||
Iinstruction.__init__(self, instruction_text)
|
||||
self.true_case = true_case
|
||||
self.false_case = false_case
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int, y_sz: int) -> Iterable[float]:
|
||||
true_x, true_y, true_sz_x, true_sz_y, false_x, false_y, false_sz_x, false_sz_y = cti.draw_if_statement(
|
||||
self.instruction_text,
|
||||
x, y, x_sz, y_sz)
|
||||
|
||||
true_blk_sz = self.draw_true_case(true_x, true_y, true_sz_x, true_sz_y)
|
||||
false_blk_sz = self.draw_false_case(false_x, false_y, false_sz_x, false_sz_y)
|
||||
blk_sz = max(true_blk_sz, false_blk_sz)
|
||||
return x, true_y + blk_sz
|
||||
def get_truesize(self) -> float:
|
||||
sz = 0.0
|
||||
for inst in self.true_case:
|
||||
sz += inst.getblksize()
|
||||
return sz
|
||||
|
||||
def draw_true_case(self, x: float, y:float, x_sz:float, y_sz:float) -> float:
|
||||
start_y = y
|
||||
y_sz /= len(self.true_case)
|
||||
for instruction in self.true_case:
|
||||
x, y = instruction.to_image(x, y, x_sz, y_sz)
|
||||
return y - start_y
|
||||
|
||||
def draw_false_case(self, x: float, y:float, x_sz:float, y_sz:float) -> float:
|
||||
start_y = y
|
||||
def get_falsesize(self) -> float:
|
||||
sz = 0.0
|
||||
if self.false_case:
|
||||
for inst in self.false_case:
|
||||
sz += inst.getblksize()
|
||||
return sz
|
||||
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize() + max(self.get_truesize(), self.get_falsesize())
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
true_x, true_y, true_sz_x, _, false_x, false_y, false_sz_x, _ = cti.draw_if_statement(
|
||||
self.instruction_text, x, y, x_sz, self.getblksize()
|
||||
)
|
||||
self.draw_true_case(true_x, true_y, true_sz_x)
|
||||
self.draw_false_case(false_x, false_y, false_sz_x)
|
||||
blk_size = self.getblksize()
|
||||
return x, y + blk_size
|
||||
|
||||
def draw_true_case(self, x: float, y:float, x_sz:float):
|
||||
for instruction in self.true_case:
|
||||
x, y = instruction.to_image(x, y, x_sz)
|
||||
|
||||
def draw_false_case(self, x: float, y:float, x_sz:float):
|
||||
if self.false_case:
|
||||
y_sz /= len(self.false_case)
|
||||
for instruction in self.false_case:
|
||||
x, y = instruction.to_image(x, y, x_sz, y_sz)
|
||||
return y - start_y
|
||||
x, y = instruction.to_image(x, y, x_sz)
|
||||
|
||||
#TODO
|
||||
# class switch_instruction(Iinstruction):
|
||||
@@ -87,34 +102,41 @@ class while_instruction_front(Iinstruction):
|
||||
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None:
|
||||
Iinstruction.__init__(self, condition)
|
||||
self.child_instructions = instructions
|
||||
|
||||
def get_children_size(self) -> float:
|
||||
children_sz = 0
|
||||
for inst in self.child_instructions:
|
||||
children_sz += inst.getblksize()
|
||||
return children_sz
|
||||
|
||||
def getblksize(self) -> float:
|
||||
return self._getblksize() + self.get_children_size()
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int, y_sz: int):
|
||||
children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, y_sz)
|
||||
blk_size = self.draw_children(children_x, children_y, children_sz_x, children_sz_y)
|
||||
return x, y + blk_size
|
||||
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
|
||||
children_x, children_y, children_sz_x = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, self.getblksize())
|
||||
self.draw_children(children_x, children_y, children_sz_x)
|
||||
|
||||
def draw_children(self, x:float, y:float, x_sz:float, y_sz:float) -> float:
|
||||
y_sz /= len(self.child_instructions)
|
||||
for instruction in self.child_instructions:
|
||||
x, y = instruction.to_image(x, y, x_sz, y_sz)
|
||||
return y_sz
|
||||
return x, y + self.getblksize()
|
||||
|
||||
|
||||
def draw_children(self, x:float, y:float, x_sz:float):
|
||||
for inst in self.child_instructions:
|
||||
x, y = inst.to_image(x, y, x_sz)
|
||||
return self.get_children_size()
|
||||
|
||||
|
||||
class while_instruction_back(while_instruction_front):
|
||||
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None:
|
||||
while_instruction_front.__init__(self, condition, instructions)
|
||||
|
||||
def to_image(self, x:int, y:int, x_sz: int, y_sz: int):
|
||||
def to_image(self, x:int, y:int, x_sz: int):
|
||||
children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, y_sz)
|
||||
blk_size = self.draw_children(children_x, children_y, children_sz_x, children_sz_y)
|
||||
return x, y + blk_size
|
||||
blk_size = self.draw_children(children_x, children_y, children_sz_x)
|
||||
return x
|
||||
|
||||
def draw_children(self, x:float, y:float, x_sz:float, y_sz:float) -> float:
|
||||
y_sz /= len(self.child_instructions)
|
||||
def draw_children(self, x:float, y:float, x_sz:float):
|
||||
for instruction in self.child_instructions:
|
||||
x, y = instruction.to_image(x, y, x_sz, y_sz)
|
||||
return y_sz
|
||||
x, y = instruction.to_image(x, y, x_sz)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -130,5 +152,5 @@ if __name__ == "__main__":
|
||||
if_instruction("shouldNiet()", [ generic_instruction("hiet()") ], [generic_instruction("hiet()")]),
|
||||
])
|
||||
cti.NSD_init(500, 500)
|
||||
test.to_image(0, 0, 500, 500)
|
||||
test.to_image(0, 0, 500)
|
||||
cti.NSD_save("Iinstruction")
|
||||
Reference in New Issue
Block a user