Skip to content

Commit

Permalink
hyperfocal: Updated ui.CanvasObject and ui.CanvasAlphaObject
Browse files Browse the repository at this point in the history
New methods:
    * `change_icon(img: image)` and `ui.change_icon(img: image, alpha_mask: gray_image)` - used to update the button image, taking into account the new image size
    * `offset_by_img_size()` - offsets the button's location to the top left by half of it's image size, intended usage is to call it once right after loading an image to center the button

ui.py: Updated ui.CanvasObject and ui.CanvasAlphaObject
__main__.py: New functionality allowed by the changes in canvas objects
  • Loading branch information
okawo80085 committed Oct 29, 2021
1 parent bcec46b commit 4e19107
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 40 deletions.
123 changes: 86 additions & 37 deletions hyperfocal/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,22 @@ def prev_img(image_paths: List[str], idx: ref[int], img: ref[np.ndarray]):

canvas_shape = (*app_settings['preview_resolution'][::-1], 3)

bck = ui.CanvasAlphaObject(
np.array(
(0.1, 0.1)
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/back_button.png'
),
lambda: killer_ref.set(True),
layer=3
)

bck.offset_by_img_size()

buttons_transparent = [
# layer 3 - gallery
ui.CanvasAlphaObject(
np.array(
(0.1, 0.1)
) * app_settings['preview_resolution'] - (25, 25),
*img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/back_button.png'
),
lambda: killer_ref.set(True),
layer=3
),
bck,
ui.CanvasAlphaObject(
np.array((0, 0)),
np.zeros((canvas_shape[0], canvas_shape[1] // 2, 3)),
Expand Down Expand Up @@ -302,7 +306,7 @@ def main():
gallery_button = ui.CanvasAlphaObject(
np.array(
(0.25, 0.85)
) * app_settings['preview_resolution'] - (35, 35),
) * app_settings['preview_resolution'],
np.ones((70, 70, 3), dtype=np.uint8) * 150,
cv2.circle(
np.zeros((70, 70), dtype=np.uint8),
Expand All @@ -313,6 +317,7 @@ def main():
) / 255,
lambda: ui.set_layer(3)
)
gallery_button.offset_by_img_size()

# try to make a last photo preview if possible
images_paths = _get_image_paths(app_settings['gallery_dir'])
Expand All @@ -326,25 +331,28 @@ def main():
take_photo_button = ui.CanvasAlphaObject(
np.array(
(0.5, 0.85)
) * app_settings['preview_resolution'] - (50, 50),
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(f'{DATA_DIR}/icons/photo_button.png'),
lambda: take_photo(
app_settings['gallery_dir'],
curr_vod_ref,
curr_camcfg_ref,
vod,
gallery_button
)
),
icon_name="photo_button.png"
)
take_photo_button.offset_by_img_size()

settings_button = ui.CanvasAlphaObject(
np.array(
(0.9, 0.07)
) * app_settings['preview_resolution'] - (25, 25),
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/settings_button.png'
),
None
None,
icon_name="settings_button.png"
)
settings_button.offset_by_img_size()

settings_button.cb = lambda: ui.set_layer(
1 if ui.CV_VISIBLE_LAYER != 1 else 0,
Expand Down Expand Up @@ -399,16 +407,37 @@ def gallery_change_dir_setting_cb(

return True

def theme_change_dir_setting_cb(app: Dict[str, setting]) -> bool:
def theme_change_dir_setting_cb(
app: Dict[str, setting],
buttons: List[Union[ui.CanvasObject, ui.CanvasAlphaObject]],
ROOT_DIR: str
) -> bool:
global args

res = easygui.diropenbox(default=True, msg="choose theme dir")
if res is None:
return False

app['resources_dir'] = res
print(f'changed theme dir to "{res}"')
easygui.msgbox(
'You need to restart to apply the changes.', 'Restart required'
)
# easygui.msgbox(
# 'You need to restart to apply the changes.', 'Restart required'
# )

DATA_DIR = combine_paths(ROOT_DIR, app['resources_dir'])

for i in buttons:
if i.icon_name:
i.change_icon(
*img_proc.open_image_with_alpha(
f"{DATA_DIR}/icons/{i.icon_name}"
)
)

if i.icon_name == "grid_button.png" and not app['use_grid']:
i.mask *= 0.5
elif i.icon_name == "gallery_button.png" and not app['use_system_gallery']: # noqa line too long
i.mask *= 0.5

return True

Expand Down Expand Up @@ -457,11 +486,13 @@ def toggle_grid(
gallery_toggle_setting = ui.CanvasAlphaObject(
np.array(
(0.2, 0.15)
) * app_settings['preview_resolution'] - (25, 25),
) * app_settings['preview_resolution'],
*button_icon,
None, # assigned below
layer=1
layer=1,
icon_name="gallery_button.png"
)
gallery_toggle_setting.offset_by_img_size()

gallery_toggle_setting.cb = lambda: gallery_toggle_setting_cb(
app_settings,
Expand All @@ -471,22 +502,26 @@ def toggle_grid(
gallery_change_dir_setting = ui.CanvasAlphaObject(
np.array(
(0.3, 0.15)
) * app_settings['preview_resolution'] - (25, 25),
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/gallerydir_button.png'
),
lambda: gallery_change_dir_setting_cb(app_settings, gallery_button),
layer=1
layer=1,
icon_name="gallerydir_button.png"
)
gallery_change_dir_setting.offset_by_img_size()

theme_change_dir_setting = ui.CanvasAlphaObject(
np.array(
(0.4, 0.15)
) * app_settings['preview_resolution'] - (25, 25),
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(f'{DATA_DIR}/icons/theme_button.png'),
lambda: theme_change_dir_setting_cb(app_settings),
layer=1
None,
layer=1,
icon_name="theme_button.png"
)
theme_change_dir_setting.offset_by_img_size()

grid_button_icon = img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/grid_button.png'
Expand Down Expand Up @@ -535,7 +570,7 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
clear_filter = ui.CanvasAlphaObject(
np.array(
(0.02, 0.75)
) * app_settings['preview_resolution'] - (0, 15),
) * app_settings['preview_resolution'],
cv2.putText(
np.zeros((30, 80, 3), dtype=np.uint8),
"clear",
Expand All @@ -547,11 +582,12 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
np.ones((30, 80)) * 0.4,
lambda: set_filter(None)
)
# clear_filter.offset_by_img_size()

t1000_filter = ui.CanvasAlphaObject(
np.array(
(0.14, 0.75)
) * app_settings['preview_resolution'] - (0, 15),
) * app_settings['preview_resolution'],
cv2.putText(
np.zeros((30, 80, 3), dtype=np.uint8),
"times1000",
Expand All @@ -563,11 +599,12 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
np.ones((30, 80)) * 0.4,
lambda: set_filter(filters.times1000)
)
# t1000_filter.offset_by_img_size()

t2000_filter = ui.CanvasAlphaObject(
np.array(
(0.26, 0.75)
) * app_settings['preview_resolution'] - (0, 15),
) * app_settings['preview_resolution'],
cv2.putText(
np.zeros((30, 80, 3), dtype=np.uint8),
"times2000",
Expand All @@ -579,11 +616,12 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
np.ones((30, 80)) * 0.4,
lambda: set_filter(filters.times2000)
)
# t2000_filter.offset_by_img_size()

power2_filter = ui.CanvasAlphaObject(
np.array(
(0.38, 0.75)
) * app_settings['preview_resolution'] - (0, 15),
) * app_settings['preview_resolution'],
cv2.putText(
np.zeros((30, 80, 3), dtype=np.uint8),
"power2",
Expand All @@ -595,11 +633,12 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
np.ones((30, 80)) * 0.4,
lambda: set_filter(filters.power2)
)
# power2_filter.offset_by_img_size()

gray_filter = ui.CanvasAlphaObject(
np.array(
(0.50, 0.75)
) * app_settings['preview_resolution'] - (0, 15),
) * app_settings['preview_resolution'],
cv2.putText(
np.zeros((30, 80, 3), dtype=np.uint8),
"gray",
Expand All @@ -611,6 +650,7 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
np.ones((30, 80)) * 0.4,
lambda: set_filter(filters.grayscale)
)
# gray_filter.offset_by_img_size()

################################################
# button lists for rendering
Expand All @@ -631,19 +671,19 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
power2_filter,
gray_filter,
# layer 1 - settings
settings_anywhere_button,
# settings_anywhere_button, i dont need to render this...
gallery_toggle_setting,
gallery_change_dir_setting,
theme_change_dir_setting,
toggle_grid_setting,
]

# don't add this function if only 1 camera is available
if len(cameras) > 1:
if len(camera_ids) > 1:
cycle_cameras_button = ui.CanvasAlphaObject(
np.array(
(0.75, 0.85)
) * app_settings['preview_resolution'] - (35, 35),
) * app_settings['preview_resolution'],
*img_proc.open_image_with_alpha(
f'{DATA_DIR}/icons/change_camera_button.png'
),
Expand All @@ -652,11 +692,20 @@ def set_filter(filt: Union[Callable[[np.ndarray], np.ndarray], None]):
vod,
hyperfocal_backend,
camera_lock_ref
)
),
icon_name="change_camera_button.png"
)
cycle_cameras_button.offset_by_img_size()

buttons_transparent.append(cycle_cameras_button)

# needs to hook back to the list of all displayed buttons
theme_change_dir_setting.cb = lambda: theme_change_dir_setting_cb(
app_settings,
buttons_transparent,
args['<cfg_path>']
)

#############################################
# main runtime loop
#############################################
Expand Down
28 changes: 25 additions & 3 deletions hyperfocal/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,34 @@ def __init__(
pos: Tuple[float, float],
img: np.ndarray,
callback: Callable,
layer: int = 0
layer: int = 0,
icon_name: str = None
):
global CV_VISIBLE_OBJECTS
self.pos = np.array(pos).astype(int)
self.img = img
self.size = self.img.shape[:2][::-1]
self.cb = callback
self.layer = layer
self.icon_name = icon_name

self._mouse_pos = (0, 0)
self._mouse_hold = False
CV_VISIBLE_OBJECTS.append(self)

def offset_by_img_size(self):
self.pos -= (np.array(self.size) / 2).astype(int)

def change_icon(self, img: np.ndarray):
self.img = img
temp_size = self.img.shape[:2][::-1]

self.pos -= (
np.array(temp_size) / 2 - np.array(self.size) / 2
).astype(int)

self.size = temp_size

def _mouse_cb(self, event: int, x: int, y: int, *rest) -> bool:
# print(self.__class__.__name__, self.pos, self._mouse_pos)
self._mouse_pos = (x, y)
Expand Down Expand Up @@ -103,12 +118,19 @@ def __init__(
# expecting a grayscale image
alpha_mask: np.ndarray,
callback: Callable,
layer: int = 0
layer: int = 0,
icon_name: str = None
):
super().__init__(pos, img, callback, layer)
super().__init__(pos, img, callback, layer, icon_name)
self.mask = np.stack((alpha_mask, ) * 3, axis=-1)
self.mask_inv = np.stack((1 - alpha_mask, ) * 3, axis=-1)

def change_icon(self, img: np.ndarray, alpha_mask: np.ndarray):
super().change_icon(img)
self.mask = np.stack((alpha_mask, ) * 3, axis=-1)
self.mask_inv = np.stack((1 - alpha_mask, ) * 3, axis=-1)



def draw_objects(
canvas: np.ndarray,
Expand Down

0 comments on commit 4e19107

Please sign in to comment.