refactor: update spacing for operators and method params

This commit is contained in:
eddex 2020-10-04 15:21:02 +02:00
parent 265e28d1f1
commit a743850f13

View File

@ -28,8 +28,8 @@ var point_node: PackedScene = preload("../Utilities/Point/Point.tscn")
var FunctionLegend: PackedScene = preload("../Utilities/Legend/FunctionLegend.tscn")
var font_size: float = 16
var const_height: float = font_size/2*font_size/20
var const_width: float = font_size/2
var const_height: float = font_size / 2 * font_size / 20
var const_width: float = font_size / 2
var OFFSET: Vector2 = Vector2(0,0)
@ -88,14 +88,14 @@ export (String) var delimiter: String = ";"
export (bool) var origin_at_zero: bool = true
export (bool) var are_values_columns: bool = false
export (int,0,100) var x_values_index: int = 0
export (int, 0, 100) var x_values_index: int = 0
export(bool) var show_x_values_as_labels: bool = true
#export (float,1,20,0.5) var column_width: float = 10
#export (float,0,10,0.5) var column_gap: float = 2
export (float,0.1,10.0) var x_decim: float = 5.0
export (float,0.1,10.0) var y_decim: float = 5.0
export (float, 0.1, 10.0) var x_decim: float = 5.0
export (float, 0.1, 10.0) var y_decim: float = 5.0
export (PointShapes) var point_shape: int = 0
export (PoolColorArray) var function_colors = [Color("#1e1e1e")]
export (Color) var v_lines_color: Color = Color("#cacaca")
@ -107,7 +107,7 @@ export (Font) var font: Font
export (Font) var bold_font: Font
export (Color) var font_color: Color = Color("#1e1e1e")
export (TemplatesNames) var template: int = Chart.TemplatesNames.Default setget apply_template
export (float,0.1,1) var drawing_duration: float = 0.5
export (float, 0.1, 1) var drawing_duration: float = 0.5
export (bool) var invert_chart: bool = false
var templates: Dictionary = {}
@ -135,16 +135,16 @@ func _set_size(size: Vector2):
build_chart()
if Engine.editor_hint:
_get_children()
Outlines.set_point_position(0,Vector2(origin.x,0))
Outlines.set_point_position(1,Vector2(SIZE.x,0))
Outlines.set_point_position(2,Vector2(SIZE.x,origin.y))
Outlines.set_point_position(3,origin)
Outlines.set_point_position(4,Vector2(origin.x,0))
Outlines.set_point_position(0, Vector2(origin.x, 0))
Outlines.set_point_position(1, Vector2(SIZE.x, 0))
Outlines.set_point_position(2, Vector2(SIZE.x, origin.y))
Outlines.set_point_position(3, origin)
Outlines.set_point_position(4, Vector2(origin.x, 0))
Grid.get_node("VLine").set_point_position(0,Vector2((OFFSET.x+SIZE.x)/2,0))
Grid.get_node("VLine").set_point_position(1,Vector2((OFFSET.x+SIZE.x)/2,origin.y))
Grid.get_node("HLine").set_point_position(0,Vector2(origin.x,origin.y/2))
Grid.get_node("HLine").set_point_position(1,Vector2(SIZE.x,origin.y/2))
Grid.get_node("VLine").set_point_position(0, Vector2((OFFSET.x + SIZE.x) / 2,0))
Grid.get_node("VLine").set_point_position(1, Vector2((OFFSET.x + SIZE.x) / 2, origin.y))
Grid.get_node("HLine").set_point_position(0, Vector2(origin.x, origin.y / 2))
Grid.get_node("HLine").set_point_position(1, Vector2(SIZE.x, origin.y / 2))
func clear():
Outlines.points = []
@ -174,7 +174,7 @@ func _plot(source_: String, delimiter_: String, are_values_columns_: bool, x_val
datas = read_datas(source_,delimiter_)
count_functions()
structure_datas(datas,are_values_columns_,x_values_index_)
structure_datas(datas, are_values_columns_, x_values_index_)
build_chart()
calculate_pass()
calculate_coordinates()
@ -193,11 +193,11 @@ func plot():
PointData.hide()
if source == "" or source == null:
Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().",1)
Utilities._print_message("Can't plot a chart without a Source file. Please, choose it in editor, or use the custom function _plot().", 1)
return
datas = read_datas(source,delimiter)
count_functions()
structure_datas(datas,are_values_columns,x_values_index)
structure_datas(datas, are_values_columns, x_values_index)
build_chart()
calculate_pass()
calculate_coordinates()
@ -221,44 +221,83 @@ func draw_chart():
func draw_outlines():
if boxed:
Outlines.set_default_color(box_color)
OutlinesTween.interpolate_method(Outlines,"add_point",
Vector2(origin.x,0),Vector2(SIZE.x,0),drawing_duration*0.5,Tween.TRANS_QUINT,Tween.EASE_OUT)
OutlinesTween.interpolate_method(
Outlines,
"add_point",
Vector2(origin.x, 0),
Vector2(SIZE.x, 0),
drawing_duration * 0.5,
Tween.TRANS_QUINT,
Tween.EASE_OUT)
OutlinesTween.start()
yield(OutlinesTween, "tween_all_completed")
OutlinesTween.interpolate_method(
Outlines,
"add_point",
Vector2(SIZE.x,0),
Vector2(SIZE.x, origin.y),
drawing_duration * 0.5,
Tween.TRANS_QUINT,
Tween.EASE_OUT)
OutlinesTween.start()
yield(OutlinesTween,"tween_all_completed")
OutlinesTween.interpolate_method(Outlines,"add_point",
Vector2(SIZE.x,0),Vector2(SIZE.x,origin.y),drawing_duration*0.5,Tween.TRANS_QUINT,Tween.EASE_OUT)
OutlinesTween.start()
yield(OutlinesTween,"tween_all_completed")
OutlinesTween.interpolate_method(Outlines,"add_point",
Vector2(SIZE.x,origin.y),origin,drawing_duration*0.5,Tween.TRANS_QUINT,Tween.EASE_OUT)
OutlinesTween.interpolate_method(
Outlines,
"add_point",
Vector2(SIZE.x,origin.y),
origin,
drawing_duration * 0.5,
Tween.TRANS_QUINT,
Tween.EASE_OUT)
OutlinesTween.start()
yield(OutlinesTween,"tween_all_completed")
OutlinesTween.interpolate_method(Outlines,"add_point",
origin,Vector2(origin.x,0),drawing_duration*0.5,Tween.TRANS_QUINT,Tween.EASE_OUT)
yield(OutlinesTween, "tween_all_completed")
OutlinesTween.interpolate_method(
Outlines,
"add_point",
origin,
Vector2(origin.x, 0),
drawing_duration * 0.5,
Tween.TRANS_QUINT,
Tween.EASE_OUT)
OutlinesTween.start()
yield(OutlinesTween,"tween_all_completed")
yield(OutlinesTween, "tween_all_completed")
func draw_v_grid():
for p in x_chors.size():
var point: Vector2 = origin+Vector2((p)*x_pass,0)
var point: Vector2 = origin + Vector2((p) * x_pass, 0)
var v_grid: Line2D = Line2D.new()
Grid.add_child(v_grid)
v_grid.set_width(1)
v_grid.set_default_color(v_lines_color)
add_label(point+Vector2(-const_width/2*x_chors[p].length(),font_size/2), x_chors[p])
GridTween.interpolate_method(v_grid,"add_point",point,point-Vector2(0,SIZE.y-OFFSET.y),drawing_duration/(x_chors.size()),Tween.TRANS_EXPO,Tween.EASE_OUT)
add_label(point + Vector2(-const_width / 2 * x_chors[p].length(), font_size / 2), x_chors[p])
GridTween.interpolate_method(
v_grid,
"add_point",
point,
point - Vector2(0, SIZE.y - OFFSET.y),
drawing_duration / (x_chors.size()),
Tween.TRANS_EXPO,
Tween.EASE_OUT)
GridTween.start()
yield(GridTween,"tween_all_completed")
func draw_h_grid():
for p in y_chors.size():
var point: Vector2 = origin-Vector2(0,(p)*y_pass)
var point: Vector2 = origin - Vector2(0, p * y_pass)
var h_grid: Line2D = Line2D.new()
Grid.add_child(h_grid)
h_grid.set_width(1)
h_grid.set_default_color(h_lines_color)
add_label(point-Vector2(y_chors[p].length()*const_width+font_size,font_size/2), y_chors[p])
GridTween.interpolate_method(h_grid,"add_point",point,Vector2(SIZE.x,point.y),drawing_duration/(y_chors.size()),Tween.TRANS_EXPO,Tween.EASE_OUT)
add_label(point-Vector2(y_chors[p].length() * const_width + font_size, font_size / 2), y_chors[p])
GridTween.interpolate_method(
h_grid,
"add_point",
point,
Vector2(SIZE.x,point.y),
drawing_duration / (y_chors.size()),
Tween.TRANS_EXPO,
Tween.EASE_OUT)
GridTween.start()
yield(GridTween,"tween_all_completed")
@ -266,38 +305,52 @@ func draw_h_grid():
func add_label(point: Vector2, text: String):
var lbl: Label = Label.new()
if font != null:
lbl.set("custom_fonts/font",font)
lbl.set("custom_colors/font_color",font_color)
lbl.set("custom_fonts/font", font)
lbl.set("custom_colors/font_color", font_color)
Grid.add_child(lbl)
lbl.rect_position = point
lbl.set_text(text)
func draw_functions():
for function in point_positions.size():
draw_function(function,point_positions[function])
draw_function(function, point_positions[function])
func draw_function(f_index: int, function: Array):
var line: Line2D = Line2D.new()
var backline: Line2D = Line2D.new()
construct_line(line,backline,f_index,function)
construct_line(line, backline, f_index, function)
var pointv: Point
for point in function.size():
pointv = point_node.instance()
Functions.add_child(pointv)
pointv.connect("_mouse_entered",self,"show_data")
pointv.connect("_mouse_exited",self,"hide_data")
pointv.connect("_point_pressed",self,"point_pressed")
pointv.create_point(point_shape, function_colors[f_index], Color.white, function[point],
pointv.format_value(point_values[f_index][point],false,false),
y_labels[point if invert_chart else f_index] as String)
if point < function.size()-1:
FunctionsTween.interpolate_method(line,"add_point",function[point],function[point+1],drawing_duration/function.size(),Tween.TRANS_QUINT,Tween.EASE_OUT)
pointv.connect("_mouse_entered", self, "show_data")
pointv.connect("_mouse_exited", self, "hide_data")
pointv.connect("_point_pressed", self, "point_pressed")
pointv.create_point(
point_shape,
function_colors[f_index],
Color.white, function[point],
pointv.format_value(point_values[f_index][point], false, false),
y_labels[point if invert_chart else f_index] as String)
if point < function.size() - 1:
FunctionsTween.interpolate_method(
line,
"add_point",
function[point],
function[point + 1],
drawing_duration / function.size(),
Tween.TRANS_QUINT,
Tween.EASE_OUT)
FunctionsTween.start()
yield(FunctionsTween,"tween_all_completed")
yield(FunctionsTween, "tween_all_completed")
func construct_line(line: Line2D, backline: Line2D, f_index: int, function: Array):
var midtone = Color(Color(function_colors[f_index]).r,Color(function_colors[f_index]).g,Color(function_colors[f_index]).b,Color(function_colors[f_index]).a/2)
var midtone = Color(
Color(function_colors[f_index]).r,
Color(function_colors[f_index]).g,
Color(function_colors[f_index]).b,
Color(function_colors[f_index]).a / 2)
backline.set_width(3)
backline.set_default_color(midtone)
backline.antialiased = true
@ -309,7 +362,7 @@ func construct_line(line: Line2D, backline: Line2D, f_index: int, function: Arra
func read_datas(source: String, delimiter: String):
var file: File = File.new()
file.open(source,File.READ)
file.open(source, File.READ)
var content: Array
while not file.eof_reached():
var line: PoolStringArray = file.get_csv_line(delimiter)
@ -334,14 +387,14 @@ func structure_datas(database: Array, are_values_columns: bool, x_values_index:
if x_data.is_valid_float() or x_data.is_valid_integer():
x_datas.append(x_data as float)
else:
x_datas.append(x_data.replace(",",".") as float)
x_datas.append(x_data.replace(",", ".") as float)
else:
if row != 0:
var y_data = database[row][column]
if y_data.is_valid_float() or y_data.is_valid_integer():
t_vals.append(y_data as float)
else:
t_vals.append(y_data.replace(",",".") as float)
t_vals.append(y_data.replace(",", ".") as float)
else:
y_labels.append(str(database[row][column]))
if not t_vals.empty():
@ -368,7 +421,7 @@ func structure_datas(database: Array, are_values_columns: bool, x_values_index:
var ordered_cluster = y_datas[cluster] as Array
ordered_cluster.sort()
ordered_cluster = PoolIntArray(ordered_cluster)
var margin_max = ordered_cluster[ordered_cluster.size()-1]
var margin_max = ordered_cluster[ordered_cluster.size() - 1]
var margin_min = ordered_cluster[0]
to_order.append(margin_max)
to_order_min.append(margin_min)
@ -378,13 +431,13 @@ func structure_datas(database: Array, are_values_columns: bool, x_values_index:
var margin = to_order.pop_back()
if not origin_at_zero:
y_margin_min = to_order_min.pop_front()
v_dist = y_decim * pow(10.0,str(margin).length()-2)
v_dist = y_decim * pow(10.0,str(margin).length() - 2)
var multi = 0
var p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0)
var p = (v_dist * multi) + (y_margin_min if not origin_at_zero else 0)
y_chors.append(p as String)
while p < margin:
multi+=1
p = (v_dist*multi) + ((y_margin_min) if not origin_at_zero else 0)
p = (v_dist * multi) + (y_margin_min if not origin_at_zero else 0)
y_chors.append(p as String)
# draw x_labels
@ -396,17 +449,17 @@ func structure_datas(database: Array, are_values_columns: bool, x_values_index:
margin = to_order.pop_back()
if not origin_at_zero:
x_margin_min = to_order.pop_front()
h_dist = x_decim * pow(10.0,str(margin).length()-2)
h_dist = x_decim * pow(10.0, str(margin).length() - 2)
multi = 0
p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0)
p = (h_dist * multi) + (x_margin_min if not origin_at_zero else 0)
x_labels.append(p as String)
while p < margin:
multi+=1
p = (h_dist*multi) + ((x_margin_min) if not origin_at_zero else 0)
multi += 1
p = (h_dist * multi) + (x_margin_min if not origin_at_zero else 0)
x_labels.append(p as String)
func build_chart():
origin = Vector2(OFFSET.x,SIZE.y-OFFSET.y)
origin = Vector2(OFFSET.x, SIZE.y - OFFSET.y)
func calculate_pass():
if invert_chart:
@ -418,8 +471,8 @@ func calculate_pass():
x_chors = x_labels
# calculate distance in pixel between 2 consecutive values/datas
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size()-1)
y_pass = origin.y / (y_chors.size()-1)
x_pass = (SIZE.x - OFFSET.x) / (x_chors.size() - 1)
y_pass = origin.y / (y_chors.size() - 1)
func calculate_coordinates():
x_coordinates.clear()
@ -432,32 +485,32 @@ func calculate_coordinates():
var single_coordinates: Array
for row in y_datas:
if origin_at_zero:
single_coordinates.append((row[column]*y_pass)/v_dist)
single_coordinates.append((row[column] * y_pass) / v_dist)
else:
single_coordinates.append((row[column] - y_margin_min)*y_pass/v_dist)
single_coordinates.append((row[column] - y_margin_min) * y_pass / v_dist)
y_coordinates.append(single_coordinates)
else:
for cluster in y_datas:
var single_coordinates: Array
for value in cluster.size():
if origin_at_zero:
single_coordinates.append((cluster[value]*y_pass)/v_dist)
single_coordinates.append((cluster[value] * y_pass) / v_dist)
else:
single_coordinates.append((cluster[value] - y_margin_min)*y_pass/v_dist)
single_coordinates.append((cluster[value] - y_margin_min) * y_pass / v_dist)
y_coordinates.append(single_coordinates)
if show_x_values_as_labels:
for x in x_datas.size():
x_coordinates.append(x_pass*x)
x_coordinates.append(x_pass * x)
else:
for x in x_datas.size():
if origin_at_zero:
if invert_chart:
x_coordinates.append(x_pass*x)
x_coordinates.append(x_pass * x)
else:
x_coordinates.append(x_datas[x]*x_pass/h_dist)
x_coordinates.append(x_datas[x] * x_pass / h_dist)
else:
x_coordinates.append((x_datas[x] - x_margin_min)*x_pass/h_dist)
x_coordinates.append((x_datas[x] - x_margin_min) * x_pass / h_dist)
for f in functions:
point_values.append([])
@ -467,20 +520,30 @@ func calculate_coordinates():
for function in y_coordinates.size():
for function_value in y_coordinates[function].size():
if are_values_columns:
point_positions[function_value].append(Vector2(x_coordinates[function]+origin.x, origin.y-y_coordinates[function][function_value]))
point_values[function_value].append([x_datas[function_value],y_datas[function_value][function]])
point_positions[function_value].append(Vector2(
x_coordinates[function] + origin.x,
origin.y - y_coordinates[function][function_value]))
point_values[function_value].append(
[x_datas[function_value], y_datas[function_value][function]])
else:
point_positions[function].append(Vector2(x_coordinates[function_value]+origin.x,origin.y-y_coordinates[function][function_value]))
point_values[function].append([x_datas[function_value],y_datas[function_value][function]])
point_positions[function].append(Vector2(
x_coordinates[function_value] + origin.x,
origin.y - y_coordinates[function][function_value]))
point_values[function].append(
[x_datas[function_value], y_datas[function_value][function]])
else:
for cluster in y_coordinates.size():
for y in y_coordinates[cluster].size():
if are_values_columns:
point_values[y].append([x_datas[cluster],y_datas[cluster][y]])
point_positions[y].append(Vector2(x_coordinates[cluster]+origin.x,origin.y-y_coordinates[cluster][y]))
point_positions[y].append(Vector2(
x_coordinates[cluster] + origin.x,
origin.y - y_coordinates[cluster][y]))
else:
point_values[cluster].append([x_datas[y],y_datas[cluster][y]])
point_positions[cluster].append(Vector2(x_coordinates[y]+origin.x,origin.y-y_coordinates[cluster][y]))
point_positions[cluster].append(Vector2(
x_coordinates[y] + origin.x,
origin.y - y_coordinates[cluster][y]))
func redraw():
build_chart()
@ -517,14 +580,14 @@ func invert_chart():
func count_functions():
if are_values_columns:
if not invert_chart:
functions = datas[0].size()-1
functions = datas[0].size() - 1
else:
functions = datas.size()-1
functions = datas.size() - 1
else:
if invert_chart:
functions = datas[0].size()-1
functions = datas[0].size() - 1
else:
functions = datas.size()-1
functions = datas.size() - 1
func create_legend():
legend.clear()
@ -540,13 +603,13 @@ func create_legend():
legend_font = font
if bold_font != null:
legend_font = bold_font
function_legend.create_legend(f_name,function_colors[function],bold_font,font_color)
function_legend.create_legend(f_name, function_colors[function], bold_font, font_color)
legend.append(function_legend)
func apply_template(template_name: int):
template = template_name
templates = Utilities._load_templates()
if template_name!=null:
if template_name != null:
var custom_template = templates.get(templates.keys()[template_name])
function_colors = custom_template.function_colors as PoolColorArray
v_lines_color = Color(custom_template.v_lines_color)