Skip to content

Commit

Permalink
Update to ExtensionsAPI 3 (#4)
Browse files Browse the repository at this point in the history
* add a supported version number

* basic setup for new api

* updated to api 3

* add depth distinction by color
  • Loading branch information
Variable-ind committed May 22, 2023
1 parent 0604f37 commit c2fd5ef
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 70 deletions.
2 changes: 1 addition & 1 deletion export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ runnable=true
custom_features=""
export_filter="all_resources"
include_filter="*json"
exclude_filter=""
exclude_filter="res://src/Extensions/KeyDisplay/ExtensionsApi.gd"
export_path=""
script_export_mode=1
script_encryption_key=""
Expand Down
4 changes: 4 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ run/main_scene="res://src/Extensions/Voxelorama/Voxelorama.tscn"

driver="Dummy"

[autoload]

ExtensionsApi="*res://src/Extensions/Voxelorama/ExtensionsApi.gd"

[input]

left_mouse={
Expand Down
181 changes: 181 additions & 0 deletions src/Extensions/Voxelorama/ExtensionsApi.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# gdlint: ignore=max-public-methods
extends Node

# use these variables in your extension to access the api
var general := GeneralAPI.new()
var menu := MenuAPI.new()
var dialog := DialogAPI.new()
var panel := PanelAPI.new()
var theme := ThemeAPI.new()
var tools := ToolAPI.new()
var project := ProjectAPI.new()
var signals := SignalsAPI.new()


# The Api Methods Start Here
func get_api_version() -> int:
# Returns the api version of pixelorama
return 2


class GeneralAPI:
# Version And Config
func get_pixelorama_version() -> String:
# Returns the version of pixelorama
return "0.11.0"

func get_config_file() -> ConfigFile:
# config_file contains all the settings (Brushes, sizes, preferences, etc...)
return ConfigFile.new()

# Nodes
func get_global():
# Returns the Global autoload used by pixelorama
pass

func get_extensions_node() -> Node:
# Returns the Extensions Node (the parent of the extension's Main.tscn)
return Node.new()

func get_canvas():
# Returns the canvas
pass


class MenuAPI:
enum { FILE, EDIT, SELECT, IMAGE, VIEW, WINDOW, HELP }

func add_menu_item(menu_type: int, item_name: String, item_metadata, item_id := -1) -> int:
# item_metadata is usually a popup node you want to appear when you click the item_name
# that popup should also have an (menu_item_clicked) function inside it's script
# "item_idx" of the added entry is returned
return 0

func remove_menu_item(menu_type: int, item_idx: int) -> void:
# removes an entry at "item_idx" from the menu_type (FILE, EDIT, SELECT, IMAGE, VIEW, WINDOW, HELP)
pass


class DialogAPI:
func show_error(text: String) -> void:
# shows an alert dialog with the given "text"
# useful for displaying messages like "Incompatible API" etc...
pass

func get_dialogs_parent_node() -> Node:
# returns the node that is the parent of the dialog used in pixelorama
return Node.new()

func dialog_open(open: bool) -> void:
# Tell pixelorama that a dialog is being opened
pass


class PanelAPI:
func set_tabs_visible(visible: bool) -> void:
# sets the visibility of tabs
pass

func get_tabs_visible() -> bool:
# get the visibility of tabs
return false

func add_node_as_tab(node: Node) -> void:
# Adds a "node" as a tab
pass

func remove_node_from_tab(node: Node) -> void:
# Removes the "node" from the DockableContainer
pass


class ThemeAPI:
func add_theme(theme: Theme) -> void:
# Adds a theme
pass

func find_theme_index(theme: Theme) -> int:
# Returns index of a theme in preferences
return 0

func get_theme() -> Theme:
# Returns the current theme
return Theme.new()

func set_theme(idx: int) -> bool:
# Sets a theme located at a given "idx" in preferences
# If theme set successfully then return true, else false
return false

func remove_theme(theme: Theme) -> void:
# Remove a theme from preferences
pass


class ToolAPI:
# Tool methods
func add_tool(
tool_name: String,
display_name: String,
shortcut: String,
scene: PackedScene,
extra_hint := "",
extra_shortucts := [],
layer_types: PoolIntArray = []
) -> void:
# Adds a tool with the above detail
pass

func remove_tool(tool_name: String) -> void:
# Removes a tool with name "tool_name"
# and assign Pencil as left tool, Eraser as right tool
pass


class ProjectAPI:
func get_current_project():
# Returns the current project (type: Project)
pass

func get_current_cel_info() -> Dictionary:
# As there are more than one types of cel in Pixelorama,
# An extension may try to use a GroupCel as a PixelCel (if it doesn't know the difference)
# So it's encouraged to use this function to access cels

# type can be "GroupCel", "PixelCel", "Cel3D", and "BaseCel"
return {"cel": null, "type": ""}

func get_cel_info_at(project, frame: int, layer: int) -> Dictionary:
# frames from left to right, layers from bottom to top
# frames/layers start at "0"
# and end at (project.frames.size() - 1) and (project.layers.size() - 1) respectively
return {"cel": null, "type": ""}


class SignalsAPI:
# Global signals
func connect_project_changed(target: Object, method: String):
return

func disconnect_project_changed(target: Object, method: String):
return

func connect_cel_changed(target: Object, method: String):
return

func disconnect_cel_changed(target: Object, method: String):
return

# Tool Signal
func connect_tool_color_changed(target: Object, method: String):
return

func disconnect_tool_color_changed(target: Object, method: String):
return

# updater signals
func connect_current_cel_texture_changed(target: Object, method: String):
return

func disconnect_current_cel_texture_changed(target: Object, method: String):
return
50 changes: 40 additions & 10 deletions src/Extensions/Voxelorama/Tools/CanvasDepth.gd
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
extends Node2D

var _voxelorama_root_node: Node
onready var extensions_api = get_node("/root/ExtensionsApi")

var users :int = 1


func _ready() -> void:
if extensions_api:
_voxelorama_root_node = extensions_api.get_extensions_node().get_node("Voxelorama")
_voxelorama_root_node = ExtensionsApi.general.get_extensions_node().get_node("Voxelorama")
# immediately update if another tool is drawing


func _draw() -> void:
if !extensions_api:
return

var project = extensions_api.get_current_project()
var project = ExtensionsApi.project.get_current_project()
var size: Vector2 = project.size
var cel: Reference = project.frames[project.current_frame].cels[project.current_layer]
var image: Image = cel.image
if !cel.has_meta("VoxelDepth"):
return
var depth_array: Array = cel.get_meta("VoxelDepth")

var font: Font = extensions_api.get_theme().default_font
var font: Font = ExtensionsApi.theme.get_theme().default_font
draw_set_transform(position, rotation, Vector2(0.05, 0.05))
image.lock()
for x in range(size.x):
for y in range(size.y):
if image.get_pixel(x, y).a == 0:
continue
var depth_str := str(depth_array[x][y])
draw_string(font, Vector2(x, y) * 20 + Vector2.DOWN * 16, depth_str)
draw_string(
font, Vector2(x, y) * 20 + Vector2.DOWN * 16, depth_str, get_color(depth_array[x][y])
)
image.unlock()
draw_set_transform(position, rotation, scale)


func get_color(depth: float):
var weight = 0
var color_a := Color.red
var color_b := Color.white
if depth > 0 and depth <= 5:
weight = (depth - 1) /5.0
color_a = Color.white
color_b = Color.blue

if depth > 5 and depth <= 10:
weight = (depth - 6)/25.0
color_a = Color.blue
color_b = Color.green

if depth > 10 and depth <= 15:
weight = (depth - 11)/25.0
color_a = Color.green
color_b = Color.yellow

if depth > 15 and depth <= 20:
weight = (depth - 16)/25.0
color_a = Color.yellow
color_b = Color.orange

if depth > 20 and depth <= 25:
weight = (depth - 26)/25.0
color_a = Color.orange
color_b = Color.red

# values larger than 25 onward are in red
return color_a.linear_interpolate(color_b, weight)


func request_deletion():
users -= 1
print(users)
if users == 0: # no one is using this node
queue_free()
# Else there are still active tool using this node so DENIED
49 changes: 19 additions & 30 deletions src/Extensions/Voxelorama/Tools/Depth.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,30 @@ var _canvas_depth_node: Node2D

var _canvas: Node2D

onready var extensions_api = get_node("/root/ExtensionsApi")


func _ready() -> void:
kname = name.replace(" ", "_").to_lower()
load_config()
if extensions_api:
_canvas = extensions_api.get_canvas()
for child in _canvas.get_children():
if child.is_in_group("CanvasDepth"):
_canvas_depth_node = child
_canvas_depth_node.users += 1
# We will share single _canvas_depth_node
return
_canvas_depth_node = _canvas_depth.instance()
_canvas.add_child(_canvas_depth_node)

_canvas = ExtensionsApi.general.get_canvas()
for child in _canvas.get_children():
if child.is_in_group("CanvasDepth"):
_canvas_depth_node = child
_canvas_depth_node.users += 1
# We will share single _canvas_depth_node
return
_canvas_depth_node = _canvas_depth.instance()
_canvas.add_child(_canvas_depth_node)


func save_config() -> void:
if extensions_api:
var config := get_config()
extensions_api.get_config_file().set_value(tool_slot.kname, kname, config)
var config := get_config()
ExtensionsApi.general.get_config_file().set_value(tool_slot.kname, kname, config)


func load_config() -> void:
if extensions_api:
var value = extensions_api.get_config_file().get_value(tool_slot.kname, kname, {})
set_config(value)
update_config()
var value = ExtensionsApi.general.get_config_file().get_value(tool_slot.kname, kname, {})
set_config(value)
update_config()


func get_config() -> Dictionary:
Expand All @@ -58,11 +53,9 @@ func update_config() -> void:


func draw_start(position: Vector2) -> void:
if !extensions_api:
return
is_moving = true
_depth_array = []
var project = extensions_api.get_current_project()
var project = ExtensionsApi.project.get_current_project()
var cel: Reference = project.frames[project.current_frame].cels[project.current_layer]
var image: Image = cel.image
if cel.has_meta("VoxelDepth"):
Expand All @@ -80,22 +73,18 @@ func draw_start(position: Vector2) -> void:


func draw_move(position: Vector2) -> void:
if !extensions_api:
return
# This can happen if the user switches between tools with a shortcut
# while using another tool
if !is_moving:
draw_start(position)
var project = extensions_api.get_current_project()
var project = ExtensionsApi.project.get_current_project()
var cel = project.frames[project.current_frame].cels[project.current_layer]
_update_array(cel, position)


func draw_end(position: Vector2) -> void:
if !extensions_api:
return
is_moving = false
var project = extensions_api.get_current_project()
var project = ExtensionsApi.project.get_current_project()
var cel = project.frames[project.current_frame].cels[project.current_layer]
_update_array(cel, position)

Expand All @@ -107,7 +96,7 @@ func cursor_move(position: Vector2) -> void:
func draw_indicator(left: bool) -> void:
var rect := Rect2(_cursor, Vector2.ONE)
if _canvas:
var global: Node = extensions_api.get_global()
var global: Node = ExtensionsApi.general.get_global()
var color: Color = global.left_tool_color if left else global.right_tool_color
_canvas.indicators.draw_rect(rect, color, false)

Expand Down
Loading

0 comments on commit c2fd5ef

Please sign in to comment.