2021-01-15 19:26:31 +01:00
|
|
|
"""
|
|
|
|
Blender user interface icon handling.
|
|
|
|
"""
|
|
|
|
import os.path
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import bpy.utils.previews
|
|
|
|
|
2021-08-11 14:32:21 +02:00
|
|
|
import arm
|
|
|
|
|
|
|
|
if arm.is_reload(__name__):
|
2021-08-04 22:49:38 +02:00
|
|
|
# _unload_icons is not available in the module scope yet
|
|
|
|
def __unload():
|
|
|
|
_unload_icons()
|
|
|
|
|
|
|
|
# Refresh icons after reload
|
|
|
|
__unload()
|
|
|
|
else:
|
2021-08-11 14:32:21 +02:00
|
|
|
arm.enable_reload(__name__)
|
2021-08-04 22:49:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["get_id"]
|
|
|
|
|
2021-01-15 19:26:31 +01:00
|
|
|
_icons_dict: Optional[bpy.utils.previews.ImagePreviewCollection] = None
|
|
|
|
"""Dictionary of all loaded icons, or `None` if not loaded"""
|
|
|
|
|
|
|
|
_icons_dir = os.path.join(os.path.dirname(__file__), "custom_icons")
|
|
|
|
"""Directory of the icon files"""
|
|
|
|
|
|
|
|
|
2021-08-04 22:49:38 +02:00
|
|
|
def _load_icons():
|
|
|
|
"""(Re)loads all icons."""
|
2021-01-15 19:26:31 +01:00
|
|
|
global _icons_dict
|
|
|
|
|
2021-08-04 22:49:38 +02:00
|
|
|
_unload_icons()
|
2021-01-15 19:26:31 +01:00
|
|
|
|
|
|
|
_icons_dict = bpy.utils.previews.new()
|
2021-08-04 22:49:38 +02:00
|
|
|
_icons_dict.load("bundle", os.path.join(_icons_dir, "bundle.png"), 'IMAGE', force_reload=True)
|
|
|
|
_icons_dict.load("haxe", os.path.join(_icons_dir, "haxe.png"), 'IMAGE', force_reload=True)
|
|
|
|
_icons_dict.load("wasm", os.path.join(_icons_dir, "wasm.png"), 'IMAGE', force_reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
def _unload_icons():
|
|
|
|
"""Unloads all icons."""
|
|
|
|
global _icons_dict
|
|
|
|
if _icons_dict is not None:
|
|
|
|
bpy.utils.previews.remove(_icons_dict)
|
|
|
|
_icons_dict = None
|
2021-01-15 19:26:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_id(identifier: str) -> int:
|
2021-08-04 22:49:38 +02:00
|
|
|
"""Returns the icon ID from the given identifier."""
|
2021-01-15 19:26:31 +01:00
|
|
|
if _icons_dict is None:
|
|
|
|
_load_icons()
|
|
|
|
return _icons_dict[identifier].icon_id
|