-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.lua
More file actions
247 lines (197 loc) · 5.49 KB
/
Editor.lua
File metadata and controls
247 lines (197 loc) · 5.49 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
local Function = require("Function")
local Colors = require("Colors")
local Editor = {}
FunctionColors =
{
Colors.Red,
Colors.Green,
Colors.Yellow,
Colors.Blue,
Colors.Purple,
Colors.Cyan,
Colors.Grey,
Colors.BrightRed,
Colors.BrightGreen,
Colors.BrightBlue,
Colors.BrightPurple,
}
local ColorIndex = 0
local function GetNextColor()
ColorIndex = ColorIndex + 1
if ColorIndex > #FunctionColors then
ColorIndex = 1
end
return FunctionColors[ColorIndex]
end
local Functions = {}
local Width, Height
-----------------------------------------------
--[[ Auxiliary Functions ]]--
-----------------------------------------------
--- Returns a table of points `(x, y)` as a sequence `{ x1, y1, x2, y2, ... }`
---
---@param graph table # Table of ordered pairs
---
local function unpack_graph(graph)
local coordinates_sequence = {}
for i = 1, #graph do
local ordered_pair = graph[i]
table.insert(coordinates_sequence, ordered_pair.x)
table.insert(coordinates_sequence, ordered_pair.y)
end
return coordinates_sequence
end
-----------------------------------------------
--[[ Class Methods ]]--
-----------------------------------------------
Editor.Initialize = function (defaults)
love.graphics.setColor(defaults.Color)
love.graphics.setBackgroundColor(defaults.Background)
Width, Height = love.graphics.getDimensions()
Editor.Color = defaults.Color
Editor.Background = defaults.Background
Editor.Mode = defaults.Mode
Editor.Scale = defaults.Scale
Editor.Origin = { x = Width/2, y = Height/2 }
Editor.Domain = Editor.NewDomain(-50, 50, 2000)
end
Editor.NewDomain = function (a, b, subdivisions)
local domain = {}
local dx = (b - a) / subdivisions
for x = a, b, dx do
table.insert(domain, x)
end
return domain
end
--- Creates a new instance of `Function`
---
---@param pretty_exp string # Function expression without `"math."`
---@param mode? string # `"cartesian"` | `"polar"`
---
Editor.NewFunction = function (name, pretty_exp, mode)
local o = Function.New(pretty_exp, mode)
Functions[name] = o
o.name = name
o.isVisible = true
o.domain = Editor.Domain
o.color = GetNextColor()
return o
end
Editor.RemoveFunction = function (name)
Functions[name] = nil
end
Editor.ComputeAllGraphs = function ()
for _, f in pairs(Functions) do
if f.mode == Editor.Mode then
f:computeGraph()
end
end
end
Editor.ComputeAllCOMs = function ()
for _, f in pairs(Functions) do
if f.mode == Editor.Mode then
f:computeCOM()
end
end
end
local Font = love.graphics.getFont()
local Margin = 5
local function WriteNameInList(f, i)
local text = string.format("%s(x) = %s", f.name, f.prettyExp)
local name_y_pos = (Font:getHeight() + Margin) * i
love.graphics.print(text, Margin, Margin + name_y_pos)
end
local function ListFunctionNames()
local index = 0
for _, f in pairs(Functions) do
if f.isVisible then
love.graphics.setColor(f.color)
else
love.graphics.setColor(Colors.BrightGrey)
end
if f.mode == Editor.Mode then
index = index + 1
WriteNameInList(f, index)
end
end
love.graphics.setColor(Editor.Color)
end
Editor.DrawHud = function ()
love.graphics.setColor(Editor.Color)
love.graphics.print("-- Functions --", Margin, Margin)
ListFunctionNames()
local bottom = Height - Margin - Font:getHeight()
local right = Width - Margin - 100
love.graphics.print("Scale: " .. Editor.Scale, Margin, bottom)
love.graphics.print("Mode: " .. Editor.Mode, right, bottom)
end
Editor.DrawAxes = function ()
local O = Editor.Origin
love.graphics.line(0, O.y, Width, O.y) -- Ox
love.graphics.line(O.x, 0, O.x, Height) -- Oy
end
local function Plot(f)
love.graphics.setColor(f.color)
local transformed_graph = (f.graph * Editor.Scale) + Editor.Origin
love.graphics.line(unpack_graph(transformed_graph))
end
Editor.PlotAllGraphs = function ()
for _, f in pairs(Functions) do
if f.mode == Editor.Mode and f.isVisible then
Plot(f)
end
end
end
local point_radius = 4
local function PlotCOM(f)
love.graphics.setColor(f.color)
local com_x_pos = (f.com.x * Editor.Scale) + Editor.Origin.x
local com_y_pos = (f.com.y * -Editor.Scale) + Editor.Origin.y
love.graphics.circle("fill", com_x_pos, com_y_pos, point_radius)
local text_x_pos = com_x_pos + Margin
local text_y_pos = com_y_pos - Margin - Font:getHeight()
local text = string.format("CoM (%.2f, %.2f)", f.com.x, f.com.y)
love.graphics.print(text, text_x_pos, text_y_pos)
end
Editor.PlotAllCOMs = function ()
for _, f in pairs(Functions) do
if f.mode == Editor.Mode and f.isVisible then
PlotCOM(f)
end
end
end
local key = love.keyboard
Editor.ManageOriginPanning = function ()
local delta = 4
if key.isDown("up") then
Editor.Origin.y = Editor.Origin.y + delta
elseif key.isDown("down") then
Editor.Origin.y = Editor.Origin.y - delta
end
if key.isDown("left") then
Editor.Origin.x = Editor.Origin.x + delta
elseif key.isDown("right") then
Editor.Origin.x = Editor.Origin.x - delta
end
end
Editor.ManageZoom = function ()
local delta = 2
local zoom_in_pressed = key.isDown("=", "kp+")
local zoom_out_pressed = key.isDown("-", "kp-")
if zoom_in_pressed then
Editor.Scale = Editor.Scale + delta
elseif zoom_out_pressed then
Editor.Scale = Editor.Scale - delta
end
if Editor.Scale < 1 then
Editor.Scale = 1
end
end
Editor.ChangeMode = function ()
if Editor.Mode == "polar" then
Editor.Mode = "cartesian"
elseif Editor.Mode == "cartesian" then
Editor.Mode = "polar"
end
end
return Editor