Skip to content

Commit

Permalink
Merge pull request #5 from lxgr-linux/resize_methods
Browse files Browse the repository at this point in the history
Resize methods
  • Loading branch information
lxgr-linux authored Sep 7, 2021
2 parents 2af884c + ab70509 commit 9640edb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 34 deletions.
12 changes: 11 additions & 1 deletion docs/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ Changes char for the character of the rectangle.
#### Method ```scrap_engine.Frame.remove(self)```
Removes the frame from the map.

#### Method ```scrap_engine.Frame.resize(self, height, width)```
Resizes the frame.
- height:```int``` Height of the frame
- width:```int``` Width of the frame

---

### scrap_engine.Box
Expand Down Expand Up @@ -276,6 +281,11 @@ Sets an object to another coordinate in the box.
#### Method ```scrap_engine.Box.remove(self)```
Removes the box from the map.

#### Method ```scrap_engine.Box.resize(self, height, width)```
Resizes the box.
- height:```int``` Height of the box
- width:```int``` Width of the box

---

### scrap_engine.Circle
Expand Down Expand Up @@ -451,4 +461,4 @@ text1.add(map, 0, 0) # Those two steps can even be switched

#>>> Hey You!

```
```
123 changes: 90 additions & 33 deletions scrap_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Feel free to contribute what ever you want to this engine
# You can contribute here: https://github.com/lxgr-linux/scrap_engine

import time, os, threading, math
import os, threading, math

width, height = os.get_terminal_size()

Expand All @@ -14,16 +14,19 @@ def __init__(self, ob, map, x, y):
self.x = x
self.y = y
self.map = map
super().__init__(f"The {ob}s coordinate ({x}|{y}) is not in {map.width-1}x{map.height-1}")
super().__init__(f"The {ob}s coordinate ({x}|{y}) is \
not in {map.width-1}x{map.height-1}")


class Map():
def __init__(self, height=height-1, width=width, background="#", dynfps=True):
def __init__(self, height=height-1, width=width, background="#",
dynfps=True):
self.height = height
self.width = width
self.dynfps = dynfps
self.background = background
self.map = [[self.background for j in range(width)] for i in range(height)]
self.map = [[self.background for j in range(width)]
for i in range(height)]
self.obmap = [[[] for j in range(width)] for i in range(height)]
self.obs = []
self.out_old = ""
Expand All @@ -32,7 +35,9 @@ def blur_in(self, blurmap, esccode="\033[37m"):
for l in range(self.height):
for i in range(self.width):
if blurmap.map[l][i] != " ":
self.map[l][i] = esccode+blurmap.map[l][i].replace("\033[0m", "")[-1]+"\033[0m"
self.map[l][i] = (esccode +
blurmap.map[l][i].replace("\033[0m", "")[-1] +
"\033[0m")
else:
self.map[l][i] = " "
for ob in self.obs:
Expand All @@ -51,8 +56,12 @@ def show(self, init=False):

def resize(self, height, width, background="#"):
self.background = background
self.map = [[self.background for j in range(width)] for i in range(height)]
self.obmap = [[[] for j in range(width if width > self.width else self.width)] for i in range(height if height > self.height else self.height)]
self.map = [[self.background for j in range(width)]
for i in range(height)]
self.obmap = [[[] for j in range(width
if width > self.width else self.width)]
for i in range(height
if height > self.height else self.height)]
self.width = width
self.height = height
for ob in self.obs:
Expand All @@ -71,16 +80,20 @@ def __init__(self, bmap, x, y, height=height-1, width=width, dynfps=True):
self.x = x
self.dynfps = dynfps
self.bmap = bmap
self.map = [[self.bmap.background for j in range(width)] for i in range(height)]
self.map = [[self.bmap.background for j in range(width)]
for i in range(height)]
self.obmap = [[[] for j in range(width)] for i in range(height)]
self.obs = []
self.out_old = ""
self.remap()

def remap(self):
self.map = [[self.bmap.background for j in range(self.width)] for i in range(self.height)]
for sy, y in zip(range(0, self.height), range(self.y, self.y+self.height)):
for sx, x in zip(range(0, self.width), range(self.x, self.x+self.width)):
self.map = [[self.bmap.background for j in range(self.width)]
for i in range(self.height)]
for sy, y in zip(range(0, self.height),
range(self.y, self.y+self.height)):
for sx, x in zip(range(0, self.width),
range(self.x, self.x+self.width)):
try:
self.map[sy][sx] = self.bmap.map[y][x]
except:
Expand All @@ -89,7 +102,7 @@ def remap(self):
ob.redraw()

def set(self, x, y):
if x < 0 or y < 0: #or x+self.width>self.bmap.width or y+self.height>self.bmap.height:
if x < 0 or y < 0:
return 1
self.x = x
self.y = y
Expand All @@ -106,7 +119,8 @@ def __init__(self, char, state="solid", arg_proto={}):
self.char = char
self.state = state
self.added = False
self.arg_proto = arg_proto # This was added to enable more than the default args for custom objects in Text and Square
self.arg_proto = arg_proto # This was added to enable more than the
# default args for custom objects in Text and Square

def add(self, map, x, y):
if not (0 <= x < map.width) or not (0 <= y < map.height):
Expand Down Expand Up @@ -164,7 +178,8 @@ def redraw(self):
return 0

def __backup_setter(self):
if len(self.map.obmap[self.y][self.x]) > self.map.obmap[self.y][self.x].index(self)+1:
if (len(self.map.obmap[self.y][self.x])
> self.map.obmap[self.y][self.x].index(self)+1):
self.map.obmap[self.y][self.x][self.map.obmap[self.y][self.x].index(self)+1].backup = self.backup
else:
self.map.map[self.y][self.x] = self.backup
Expand Down Expand Up @@ -253,7 +268,8 @@ def set_state(self, state):


class Text(ObjectGroup):
def __init__(self, text, state="solid", esccode="", ob_class=Object, ob_args={}, ignore=""):
def __init__(self, text, state="solid", esccode="", ob_class=Object,
ob_args={}, ignore=""):
self.obs = []
self.ob_class = ob_class
self.added = False
Expand All @@ -280,7 +296,8 @@ def __texter(self, text):
for i, char in enumerate(text):
if self.esccode != "":
char = self.esccode+char+"\033[0m"
self.obs.append(self.ob_class(char, self.state, arg_proto=self.ob_args))
self.obs.append(self.ob_class(char, self.state,
arg_proto=self.ob_args))
for ob in self.obs:
ob.group = self

Expand Down Expand Up @@ -314,7 +331,8 @@ def rechar(self, text, esccode=""):


class Square(ObjectGroup):
def __init__(self, char, width, height, state="solid", ob_class=Object, ob_args={}, threads=False):
def __init__(self, char, width, height, state="solid", ob_class=Object,
ob_args={}, threads=False):
self.obs = []
self.ob_class = ob_class
self.width = width
Expand All @@ -332,26 +350,30 @@ def __init__(self, char, width, height, state="solid", ob_class=Object, ob_args=
def __create(self):
for l in range(self.height):
if self.threads:
threading.Thread(target=self.__one_line_create, args=(l,), daemon=True).start()
threading.Thread(target=self.__one_line_create,
args=(l,), daemon=True).start()
else:
self.__one_line_create(l)

def __one_line_create(self, l):
for i in range(self.width):
exec("self.ob_"+str(i)+"_"+str(l)+" = self.ob_class(self.char, self.state, arg_proto=self.ob_args)")
exec("self.obs.append(self.ob_"+str(i)+"_"+str(l)+")")
exec(f"self.ob_{i}_{l} = self.ob_class(self.char, self.state,\
arg_proto=self.ob_args)")
exec(f"self.obs.append(self.ob_{i}_{l})")

def __one_line_add(self, l):
for i in range(self.width):
exec("self.exits.append(self.ob_"+str(i)+"_"+str(l)+".add(self.map, self.x+i, self.y+l))")
exec(f"self.exits.append(self.ob_{i}_{l}.add(self.map, self.x+i,\
self.y+l))")

def add(self, map, x, y):
self.x = x
self.y = y
self.map = map
for l in range(self.height):
if self.threads:
threading.Thread(target=self.__one_line_add, args=(l,), daemon=True).start()
threading.Thread(target=self.__one_line_add, args=(l,),
daemon=True).start()
else:
self.__one_line_add(l)
self.added = True
Expand Down Expand Up @@ -382,19 +404,31 @@ def resize(self, width, height):


class Frame(ObjectGroup):
def __init__(self, height, width, corner_chars=["+", "+", "+", "+"], horizontal_chars=["-", "-"], vertical_chars=["|", "|"], state="solid", ob_class=Object, ob_args={}):
def __init__(self, height, width, corner_chars=["+", "+", "+", "+"],
horizontal_chars=["-", "-"], vertical_chars=["|", "|"],
state="solid", ob_class=Object, ob_args={}):
self.height = height
self.width = width
self.ob_class = ob_class
self.ob_args = ob_args
self.added = False
self.state = state
self.corners = [self.ob_class(i, arg_proto=self.ob_args, state=self.state) for i, j in zip(corner_chars, range(4))]
self.horizontals = [Square(char=i, width=self.width-2, height=1, state=self.state, ob_class=Object, ob_args={}) for i, j in zip(horizontal_chars, range(2))]
self.verticals = [Square(char=i, width=1, height=self.height-2, state=self.state, ob_class=Object, ob_args={}) for i, j in zip(vertical_chars, range(2))]
self.corner_chars = corner_chars
self.horizontal_chars = horizontal_chars
self.vertical_chars = vertical_chars
self.corners = [self.ob_class(i, arg_proto=self.ob_args,
state=self.state)
for i, j in zip(corner_chars, range(4))]
self.horizontals = [Square(char=i, width=self.width-2, height=1,
state=self.state, ob_class=Object, ob_args={})
for i, j in zip(horizontal_chars, range(2))]
self.verticals = [Square(char=i, width=1, height=self.height-2,
state=self.state, ob_class=Object, ob_args={})
for i, j in zip(vertical_chars, range(2))]

def __add_obs(self):
for ob, rx, ry in zip(self.corners, [0, self.width-1, 0, self.width-1], [0, 0, self.height-1, self.height-1]):
for ob, rx, ry in zip(self.corners, [0, self.width-1, 0, self.width-1],
[0, 0, self.height-1, self.height-1]):
ob.add(self.map, self.x+rx, self.y+ry)
for ob, rx, ry in zip(self.horizontals, [1, 1], [0, self.height-1]):
ob.add(self.map, self.x+rx, self.y+ry)
Expand All @@ -415,7 +449,8 @@ def set(self, x, y):
ob.remove()
self.__add_obs()

def rechar(self, corner_chars=["+", "+", "+", "+"], horizontal_char="-", vertical_char="|"):
def rechar(self, corner_chars=["+", "+", "+", "+"], horizontal_char="-",
vertical_char="|"):
for ob, c in zip(self.corners, corner_chars):
ob.rechar(c)
for ob in self.horizontals:
Expand All @@ -428,6 +463,17 @@ def remove(self):
ob.remove()
self.added = False

def resize(self, height, width):
added = self.added
if added:
self.remove()
self.__init__(height, width, corner_chars=self.corner_chars,
horizontal_chars=self.horizontal_chars,
vertical_chars=self.vertical_chars, state=self.state,
ob_class=self.ob_class, ob_args=self.ob_args)
if added:
self.add(self.map, self.x, self.y)


class Box(ObjectGroup):
def __init__(self, height, width):
Expand Down Expand Up @@ -462,9 +508,14 @@ def remove(self):
ob.remove()
self.added = False

def resize(self, height, width):
self.heigth = height
self.width = width


class Circle(Box):
def __init__(self, char, radius, state="solid", ob_class=Object, ob_args={}):
def __init__(self, char, radius, state="solid", ob_class=Object,
ob_args={}):
super().__init__(0, 0)
self.char = char
self.ob_class = ob_class
Expand All @@ -477,7 +528,8 @@ def __gen(self, radius):
for i in range(-(int(radius)+1), int(radius+1)+1):
for j in range(-(int(radius)+1), int(radius+1)+1):
if math.sqrt((i)**2+(j)**2) <= radius:
self.add_ob(self.ob_class(self.char, state=self.state, arg_proto=self.ob_args), i, j)
self.add_ob(self.ob_class(self.char, state=self.state,
arg_proto=self.ob_args), i, j)

def rechar(self, char):
self.char = char
Expand All @@ -496,7 +548,8 @@ def resize(self, radius):


class Line(Box):
def __init__(self, char, cx, cy, type="straight", state="solid", ob_class=Object, ob_args={}):
def __init__(self, char, cx, cy, type="straight", state="solid",
ob_class=Object, ob_args={}):
super().__init__(0, 0)
self.char = char
self.ob_class = ob_class
Expand All @@ -512,12 +565,16 @@ def __gen(self, cx, cy):
for i in range(int(math.sqrt(cx**2))):
i = int(cx/math.sqrt(cx**2)*i)
j = {"straight": int, "crippled": round}[self.type](cy*i/cx)
self.add_ob(self.ob_class(self.char, state=self.state, arg_proto={**self.ob_args, **{"x": i, "y": cy*i/cx}}), i, j)
self.add_ob(self.ob_class(self.char, state=self.state,
arg_proto={**self.ob_args, **{"x": i, "y": cy*i/cx}}),
i, j)
else:
for j in range(int(math.sqrt(cy**2))):
j = int(cy/math.sqrt(cy**2)*j)
i = {"straight": int, "crippled": round}[self.type](cx*j/cy)
self.add_ob(self.ob_class(self.char, state=self.state, arg_proto={**self.ob_args, **{"x": cx*j/cy, "y": j}}), i, j)
self.add_ob(self.ob_class(self.char, state=self.state,
arg_proto={**self.ob_args, **{"x": cx*j/cy, "y": j}}),
i, j)

def rechar(self, char):
self.char = char
Expand Down

0 comments on commit 9640edb

Please sign in to comment.