-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScene.cs
116 lines (109 loc) · 2.96 KB
/
MainScene.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System.Collections.Specialized;
using System.ComponentModel;
using Godot;
using System;
[Tool]
public class MainScene : Control
{
Control _background;
const string SHADER_COLOR_MAP = "color_map";
const string SHADER_MAX_ITERATIONS = "max_iterations";
const string SHADER_OFFSET = "offset";
const string SHADER_ZOOM = "zoom";
const string SHADER_DIVERGENCE_THRESHOLD = "divergence_threshold";
const float _zoomPerTick = 1.2f;
const float _movementRate = 0.001f;
float _zoom = 1.0f;
[Export]
public float Zoom
{
get => _zoom;
set {_zoom = value; UpdateShader();}
}
Vector2 _offset = new Vector2(0,0);
[Export]
public Vector2 Offset
{
get => _offset;
set {_offset = value; UpdateShader();}
}
float _realZoom = 1.0f;
bool _idleAnimation = true;
[Export]
public bool IdleAnimation
{
get => _idleAnimation;
set
{
_idleAnimation = value;
SetProcess(_idleAnimation || !Engine.EditorHint);
if(!_idleAnimation)
Zoom = _realZoom;
_realZoom = Zoom;
UpdateShader();
}
}
bool _updateQueued = false;
void UpdateShader()
{
if(_updateQueued)
return;
_updateQueued = true;
CallDeferred("RealUpdateShader");
}
void RealUpdateShader()
{
_updateQueued = false;
ShaderMaterial mat = (ShaderMaterial)_background.Material;
mat.SetShaderParam(SHADER_OFFSET, Offset);
mat.SetShaderParam(SHADER_ZOOM, Zoom);
}
public override void _Ready()
{
_realZoom = _zoom;
_updateQueued = false;
_background = GetNode<Control>("ViewportContainer/Viewport/Background");
RealUpdateShader();
}
int dir = 1;
public override void _Process(float delta)
{
if(Engine.EditorHint)
{
if(dir == 1)
{
_zoom = Mathf.Lerp(_zoom, _zoom * _zoomPerTick, delta*10.0f);
if(Zoom > 300.0f)
dir = -1;
}
else
{
_zoom = Mathf.Lerp(_zoom, _zoom * (1.0f/_zoomPerTick), delta*10.0f);
if(Zoom < 0.3)
dir = 1;
}
UpdateShader();
}
}
public override void _Input(InputEvent @event)
{
if(@event is InputEventMouseButton iemb)
{
if(iemb.ButtonIndex == (int)ButtonList.WheelUp)
{
Zoom *= _zoomPerTick;
}
else if(iemb.ButtonIndex == (int)ButtonList.WheelDown)
{
Zoom *= 1.0f/(_zoomPerTick);
}
}
else if(@event is InputEventMouseMotion iemm)
{
if(Input.IsMouseButtonPressed((int)ButtonList.Left))
{
Offset += _movementRate*iemm.Relative/Zoom;
}
}
}
}