Skip to content

Commit

Permalink
Updated the View menu in the 2D preview panel
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Dec 20, 2024
1 parent b2adac4 commit f552785
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 183 deletions.
118 changes: 36 additions & 82 deletions material_maker/panels/preview_2d/lines.gd
Original file line number Diff line number Diff line change
@@ -1,120 +1,74 @@
extends Control

@export var config_var_suffix : String = ""
var _real_style : int = 1
@export var style : int :
get:
return _real_style
@export var style : int = 1:
set(s):
set_style(s)
s = clamp(s, 0, 3)
if s != style:
style = s
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_style", style)

var _real_grid_size : float = 0.0
@export var grid_size : float :
get:
return _real_grid_size
@export var grid_size : float = 0.0:
set(s):
set_grid_size(s)
if s != grid_size:
grid_size = s
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_grid_size", grid_size)
mm_renderer.set_global_parameter("mm_grid_size"+config_var_suffix, grid_size)

var _real_color : Color = Color(0.5, 0.5, 0.5)
@export var color : Color :
get:
return _real_color
@export var color : Color = Color(0.5, 0.5, 0.5):
set(c):
set_color(c)
if c != color:
color = c
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_color", color)


const STYLES : Array = [ "None", "Corners", "Lines", "Grid4x4", "Grid8x8", "Grid10x10", "Grid16x16" ]
const STYLES : Array = [ "None", "Corners", "Lines", "Grid" ]


func _ready():
config_var_suffix = get_parent().config_var_suffix
if mm_globals.has_config("preview"+config_var_suffix+"_guides_style"):
_real_style = mm_globals.get_config("preview"+config_var_suffix+"_guides_style")
style = mm_globals.get_config("preview"+config_var_suffix+"_guides_style")
if mm_globals.has_config("preview"+config_var_suffix+"_guides_grid_size"):
_real_grid_size = mm_globals.get_config("preview"+config_var_suffix+"_guides_grid_size")
grid_size = mm_globals.get_config("preview"+config_var_suffix+"_guides_grid_size")
if mm_globals.has_config("preview"+config_var_suffix+"_guides_color"):
_real_color = mm_globals.get_config("preview"+config_var_suffix+"_guides_color")
set_grid_size(_real_grid_size, false)
set_color(_real_color, false)
set_style(_real_style)
color = mm_globals.get_config("preview"+config_var_suffix+"_guides_color")

func draw_grid(s : int) -> void:
_real_grid_size = s
var parent = get_parent()
for i in range(s+1):
var x = float(i) / float(s) - 0.5
var p : Vector2 = parent.value_to_pos(Vector2(x, x))
draw_line(Vector2(p.x, 0), Vector2(p.x, size.y), _real_color)
draw_line(Vector2(0, p.y), Vector2(size.x, p.y), _real_color)
draw_line(Vector2(p.x, 0), Vector2(p.x, size.y), color)
draw_line(Vector2(0, p.y), Vector2(size.x, p.y), color)

func _draw() -> void:
var parent = get_parent()
var p
match _real_style:
match style:
0:
pass
1:
p = parent.value_to_pos(Vector2(-0.5, -0.52))
draw_line(p, Vector2(p.x, 0), _real_color)
draw_line(p, Vector2(p.x, 0), color)
p = parent.value_to_pos(Vector2(0.5, -0.52))
draw_line(p, Vector2(p.x, 0), _real_color)
draw_line(p, Vector2(p.x, 0), color)
p = parent.value_to_pos(Vector2(-0.52, -0.5))
draw_line(p, Vector2(0, p.y), _real_color)
draw_line(p, Vector2(0, p.y), color)
p = parent.value_to_pos(Vector2(-0.52, 0.5))
draw_line(p, Vector2(0, p.y), _real_color)
draw_line(p, Vector2(0, p.y), color)
p = parent.value_to_pos(Vector2(-0.5, 0.52))
draw_line(p, Vector2(p.x, size.y), _real_color)
draw_line(p, Vector2(p.x, size.y), color)
p = parent.value_to_pos(Vector2(0.5, 0.52))
draw_line(p, Vector2(p.x, size.y), _real_color)
draw_line(p, Vector2(p.x, size.y), color)
p = parent.value_to_pos(Vector2(0.52, -0.5))
draw_line(p, Vector2(size.x, p.y), _real_color)
draw_line(p, Vector2(size.x, p.y), color)
p = parent.value_to_pos(Vector2(0.52, 0.5))
draw_line(p, Vector2(size.x, p.y), _real_color)
draw_line(p, Vector2(size.x, p.y), color)
2:
draw_grid(1)
3:
draw_grid(4)
4:
draw_grid(8)
5:
draw_grid(10)
6:
draw_grid(16)
1000:
draw_grid(int(_real_grid_size))

func set_style(s : int) -> void:
_real_style = s
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_style", s)
var mm_grid_size : int
match _real_style:
2:
mm_grid_size = 1
3:
mm_grid_size = 4
4:
mm_grid_size = 8
5:
mm_grid_size = 10
6:
mm_grid_size = 16
1000:
# warning-ignore:narrowing_conversion
mm_grid_size = _real_grid_size
_:
mm_grid_size = 128
mm_renderer.set_global_parameter("mm_grid_size"+config_var_suffix, mm_grid_size)

func show_grid(value) -> void:
set_grid_size(value, false)
set_style(1000)

func set_color(c : Color, up : bool = true) -> void:
_real_color = c
if up:
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_color", c)

func set_grid_size(s : float, up : bool = true) -> void:
_real_grid_size = s
if up:
queue_redraw()
mm_globals.set_config("preview"+config_var_suffix+"_guides_grid_size", s)
draw_grid(int(grid_size))
45 changes: 20 additions & 25 deletions material_maker/panels/preview_2d/preview_2d_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ var preview_mode := Modes.CUSTOM_PREVIEW:
var center : Vector2 = Vector2(0.5, 0.5)
var view_scale : float = 1.2

var view_mode : int = 2
var view_mode : int = 2:
set(v):
if v == view_mode:
return
view_mode = v
material.set_shader_parameter("mode", view_mode)
mm_globals.set_config("preview"+config_var_suffix+"_view_mode", view_mode)

var view_filter : int = 0:
set(v):
if v == view_filter:
return
view_filter = v
set_generator(generator, output, true)
mm_globals.set_config("preview"+config_var_suffix+"_view_postprocess", view_filter)

var current_postprocess_option := 0
const POSTPROCESS_OPTIONS : Array = [
{ name="None", function="preview_2d(uv)" },
{ name="Lowres 32x32", function="preview_2d((floor(uv*32.0)+vec2(0.5))/32.0)" },
Expand All @@ -37,6 +50,10 @@ const POSTPROCESS_OPTIONS : Array = [
func _ready():
clear()
reset_view()
if mm_globals.has_config("preview"+config_var_suffix+"_view_mode"):
view_mode = mm_globals.get_config("preview"+config_var_suffix+"_view_mode")
if mm_globals.has_config("preview"+config_var_suffix+"_view_postprocess"):
view_filter = mm_globals.get_config("preview"+config_var_suffix+"_view_postprocess")


func _notification(what: int) -> void:
Expand All @@ -53,7 +70,7 @@ func clear() -> void:


func get_shader_custom_functions():
return "vec4 preview_2d_postprocessed(vec2 uv) { return %s; }\n" % POSTPROCESS_OPTIONS[current_postprocess_option].function
return "vec4 preview_2d_postprocessed(vec2 uv) { return %s; }\n" % POSTPROCESS_OPTIONS[view_filter].function


func set_generator(g : MMGenBase, o : int = 0, force : bool = false) -> void:
Expand Down Expand Up @@ -261,28 +278,6 @@ func reset_view() -> void:
view_scale = 1.2
update_shader_options()


func set_view_mode(id:int) -> void:
if id == view_mode:
return
view_mode = id
material.set_shader_parameter("mode", view_mode)
mm_globals.set_config("preview"+config_var_suffix+"_view_mode", view_mode)


func get_view_mode() -> int:
return view_mode


func set_post_processing(id:int) -> void:
current_postprocess_option = id
set_generator(generator, output, true)
mm_globals.set_config("preview"+config_var_suffix+"_view_postprocess", current_postprocess_option)


func get_post_processing() -> int:
return current_postprocess_option

#endregion


Expand Down
Loading

0 comments on commit f552785

Please sign in to comment.