# Path: EnhancedWindow.py
# class EnhancedWindow:
# 	def __init__(self, x, y, width, height, title, minimizable = True):
# 		self.__x = x
# 		self.__y = y
# 		self.__width = width
# 		self.__height = height
# 		self.__heightNotMinimized = height
# 		self.__title = title
# 		self.__deltaY = 0
# 		self.__deltaX = 0
# 		self.__isMoving = False
# 		self.__minimized = False;
# 		self.__minimizable = minimizable
# 
# 	def begin(self, frame):
# 		mouseInsideTitleArea = cvui.mouse().inside(cvui.Rect(self.__x, self.__y, self.__width, 20))
# 		self.__height = 20 if self.__minimized else self.__heightNotMinimized
# 
# 		if self.__isMoving == False and cvui.mouse(cvui.DOWN) and mouseInsideTitleArea:
# 			self.__deltaX = cvui.mouse().x - self.__x
# 			self.__deltaY = cvui.mouse().y - self.__y
# 			self.__isMoving = True
# 
# 		elif self.__isMoving and cvui.mouse(cvui.IS_DOWN):
# 			self.__x = cvui.mouse().x - self.__deltaX
# 			self.__y = cvui.mouse().y - self.__deltaY
# 
# 		else:
# 			frameRows,frameCols,frameChannels = frame.shape
# 			self.__isMoving = False
# 			self.__x = max(0, self.__x)
# 			self.__y = max(0, self.__y)
# 			self.__x = min(frameCols - self.__width, self.__x)
# 			self.__y = min(frameRows - 20, self.__y)
# 
# 		cvui.window(frame, self.__x, self.__y, self.__width, self.__height, self.__title)
# 
# 		if self.__minimizable and cvui.button(frame, self.__x + self.__width - 20, self.__y + 1, 18, 18, '+' if self.__minimized else '-'):
# 			self.__minimized = not self.__minimized
# 
# 		cvui.beginRow(frame, self.__x + 10, self.__y + 30, self.__width - 20, self.__height - 20)
# 		cvui.beginColumn(self.__width - 10, self.__height - 20)
# 
# 
# 	def end(self):
# 		cvui.endColumn()
# 		cvui.endRow()
# 
# 	def width(self):
# 		return self.__width
# 
# 	def height(self):
# 		return self.__height
# 
# 	def setWidth(self, w):
# 		self.__width = w
# 
# 	def setHeight(self, h):
# 		self.__height = h
# 		self.__heightNotMinimized = h
# 
# 	def isMinimized(self):
# 		return self.__minimized


import numpy as np
import cv2
import cvui
from EnhancedWindow import EnhancedWindow

	while (True):
		# Should we apply Canny edge?
		if use_canny[0]:
			# Yes, we should apply it.
			frame = cv2.cvtColor(fruits, cv2.COLOR_BGR2GRAY)
			frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3)
			frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
		else: 
			# No, so just copy the original image to the displaying frame.
			frame[:] = fruits[:]

		# Render the settings window and its content, if it is not minimized.
		settings.begin(frame)
		if settings.isMinimized() == False:
			cvui.checkbox('Use Canny Edge', use_canny)
			cvui.trackbar(settings.width() - 20, low_threshold, 5, 150)
			cvui.trackbar(settings.width() - 20, high_threshold, 80, 300)
			cvui.space(20); # add 20px of empty space
			cvui.text('Drag and minimize this settings window', 0.4, 0xff0000)
		settings.end()

		# This function must be called *AFTER* all UI components. It does
		# all the behind the scenes magic to handle mouse clicks, etc.
		cvui.update()

		# Show everything on the screen
		cv2.imshow(WINDOW_NAME, frame)

		# Check if ESC key was pressed

# Path: EnhancedWindow.py
# class EnhancedWindow:
# 	def __init__(self, x, y, width, height, title, minimizable = True):
# 		self.__x = x
# 		self.__y = y
# 		self.__width = width
# 		self.__height = height
# 		self.__heightNotMinimized = height
# 		self.__title = title
# 		self.__deltaY = 0
# 		self.__deltaX = 0
# 		self.__isMoving = False
# 		self.__minimized = False;
# 		self.__minimizable = minimizable
# 
# 	def begin(self, frame):
# 		mouseInsideTitleArea = cvui.mouse().inside(cvui.Rect(self.__x, self.__y, self.__width, 20))
# 		self.__height = 20 if self.__minimized else self.__heightNotMinimized
# 
# 		if self.__isMoving == False and cvui.mouse(cvui.DOWN) and mouseInsideTitleArea:
# 			self.__deltaX = cvui.mouse().x - self.__x
# 			self.__deltaY = cvui.mouse().y - self.__y
# 			self.__isMoving = True
# 
# 		elif self.__isMoving and cvui.mouse(cvui.IS_DOWN):
# 			self.__x = cvui.mouse().x - self.__deltaX
# 			self.__y = cvui.mouse().y - self.__deltaY
# 
# 		else:
# 			frameRows,frameCols,frameChannels = frame.shape
# 			self.__isMoving = False
# 			self.__x = max(0, self.__x)
# 			self.__y = max(0, self.__y)
# 			self.__x = min(frameCols - self.__width, self.__x)
# 			self.__y = min(frameRows - 20, self.__y)
# 
# 		cvui.window(frame, self.__x, self.__y, self.__width, self.__height, self.__title)
# 
# 		if self.__minimizable and cvui.button(frame, self.__x + self.__width - 20, self.__y + 1, 18, 18, '+' if self.__minimized else '-'):
# 			self.__minimized = not self.__minimized
# 
# 		cvui.beginRow(frame, self.__x + 10, self.__y + 30, self.__width - 20, self.__height - 20)
# 		cvui.beginColumn(self.__width - 10, self.__height - 20)
# 
# 
# 	def end(self):
# 		cvui.endColumn()
# 		cvui.endRow()
# 
# 	def width(self):
# 		return self.__width
# 
# 	def height(self):
# 		return self.__height
# 
# 	def setWidth(self, w):
# 		self.__width = w
# 
# 	def setHeight(self, h):
# 		self.__height = h
# 		self.__heightNotMinimized = h
# 
# 	def isMinimized(self):
# 		return self.__minimized


# Path: example/src/ui-enhanced-canny/ui-enhanced-canny.py
import numpy as np
import cv2
import cvui
from EnhancedWindow import EnhancedWindow


	while (True):
		# Should we apply Canny edge?
		if use_canny[0]:
			# Yes, we should apply it.
			frame = cv2.cvtColor(fruits, cv2.COLOR_BGR2GRAY)
			frame = cv2.Canny(frame, low_threshold[0], high_threshold[0], 3)
			frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
		else: 
			# No, so just copy the original image to the displaying frame.
			frame[:] = fruits[:]

		# Render the settings window and its content, if it is not minimized.
		settings.begin(frame)
		if settings.isMinimized() == False:
			cvui.checkbox('Use Canny Edge', use_canny)
			cvui.trackbar(settings.width() - 20, low_threshold, 5, 150)
			cvui.trackbar(settings.width() - 20, high_threshold, 80, 300)
			cvui.space(20); # add 20px of empty space
			cvui.text('Drag and minimize this settings window', 0.4, 0xff0000)
		settings.end()

		# This function must be called *AFTER* all UI components. It does
		# all the behind the scenes magic to handle mouse clicks, etc.
		cvui.update()

		# Show everything on the screen
		cv2.imshow(WINDOW_NAME, frame)

		# Check if ESC key was pressed

		if cv2.waitKey(20) == 27: