Rev 2097 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
964 | jag | 1 | /* |
2 | * ui_sin_cos_radius.cc |
||
2302 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath |
1713 | jag | 4 | * DIN Is Noise is released under GNU Public License 2.0 |
1479 | jag | 5 | * For more information, please visit https://dinisnoise.org/ |
964 | jag | 6 | */ |
7 | |||
8 | |||
9 | #include "ui_sin_cos_radius.h" |
||
10 | #include "plugin.h" |
||
11 | #include "console.h" |
||
12 | #include "ui_list.h" |
||
13 | #include "curve_library.h" |
||
14 | #include <fstream> |
||
15 | using namespace std; |
||
16 | extern string user_data_dir; |
||
17 | extern ofstream dlog; |
||
18 | extern curve_library sin_lib, cos_lib; |
||
19 | extern int line_height; |
||
20 | |||
21 | ui_sin_cos_radius::ui_sin_cos_radius ( |
||
22 | ui_sin_cos_radius_listener* _lis, |
||
23 | const std::string& _scr_fname, |
||
24 | const std::string& _cp_sin_fname, const std::string& _cp_cos_fname, const std::string& _cp_rad_fname, |
||
25 | const std::string& _sin_ed_fname, const std::string& _cos_ed_fname, const std::string& _rad_ed_fname, |
||
26 | int inc |
||
27 | ) : |
||
28 | cp_sin (_cp_sin_fname), cp_cos (_cp_cos_fname), cp_rad (_cp_rad_fname), |
||
29 | sin_ed (_sin_ed_fname), cos_ed (_cos_ed_fname), rad_ed (_rad_ed_fname) { |
||
30 | |||
31 | lis = _lis; |
||
32 | fname = _scr_fname; |
||
33 | sin = cos = radius = 0; |
||
34 | inc_radius = inc; |
||
35 | sin_ed.add (&cp_sin.crv, this); |
||
36 | cos_ed.add (&cp_cos.crv, this); |
||
37 | sin_ed.attach_library (&sin_lib); |
||
38 | cos_ed.attach_library (&cos_lib); |
||
39 | if (inc_radius) { |
||
40 | rad_ed.add (&cp_rad.crv, this); |
||
41 | } |
||
42 | load (); |
||
43 | } |
||
44 | |||
45 | ui_sin_cos_radius::~ui_sin_cos_radius () { |
||
46 | save (); |
||
47 | } |
||
48 | |||
49 | void ui_sin_cos_radius::load () { |
||
50 | ifstream fin ((user_data_dir + fname).c_str()); |
||
51 | if (fin) { |
||
52 | fin >> sin >> cos >> radius; |
||
53 | } else { |
||
54 | sin = cos = radius = 0; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | void ui_sin_cos_radius::save () { |
||
59 | ofstream fout ((user_data_dir + fname).c_str()); |
||
60 | fout << sin << ' ' << cos << ' ' << radius; |
||
61 | } |
||
62 | |||
63 | void ui_sin_cos_radius::setup () { |
||
64 | options_list* ol [] = {&ol_sin, &ol_cos, &ol_radius}; |
||
65 | int n = 2; if (inc_radius) n = 3; |
||
66 | for (int i = 0; i < n; ++i) ol[i]->set_listener (this); |
||
67 | button* b [] = {&b_sin, &b_cos, &b_radius}; |
||
68 | for (int i = 0; i < 3; ++i) { |
||
69 | button* bi = b[i]; |
||
1120 | jag | 70 | bi->set_text ("Edit"); |
964 | jag | 71 | bi->set_listener (this); |
72 | } |
||
73 | if (sin) set_option (ol_sin.option, " Custom sin", &pf_sin, &cp_sin, b_sin, 1); else set_option (ol_sin.option, " Standard sin", &pf_sin, &st_sin, b_sin, 0); |
||
74 | if (cos) set_option (ol_cos.option, " Custom cos", &pf_cos, &cp_cos, b_cos, 1); else set_option (ol_cos.option, " Standard cos", &pf_cos, &st_cos, b_cos, 0); |
||
75 | if (inc_radius) { |
||
76 | if (radius) set_option (ol_radius.option, " Custom radius", &pf_radius, &cp_rad, b_radius, 1); else set_option (ol_radius.option, " Standard radius", &pf_radius, &st_radius, b_radius, 0); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | void ui_sin_cos_radius::set_pos (int x, int y) { |
||
81 | int lh = line_height; |
||
82 | posx = x; |
||
83 | posy = y; |
||
84 | int yw = posy - lh; |
||
85 | widget* wo [] = {&ol_sin, &ol_cos, &ol_radius}; |
||
86 | int n; if (inc_radius) n = 3; else n = 2; |
||
87 | for (int i = 0; i < n; ++i) { |
||
88 | widget* woi = wo[i]; |
||
89 | woi->set_pos (x, yw); |
||
90 | yw -= lh; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | int ui_sin_cos_radius::handle_input () { |
||
95 | widget* wo [] = {&ol_sin, &ol_cos, &ol_radius}; int n; if (inc_radius) n = 3; else n = 2; |
||
96 | button* wb [] = {&b_sin, &b_cos, &b_radius}; |
||
97 | int ret = 0; |
||
98 | for (int i = 0; i < n; ++i) ret |= wo[i]->handle_input (); |
||
99 | for (int i = 0; i < n; ++i) { |
||
100 | button* bi = wb[i]; |
||
101 | if (bi->visible) ret |= bi->handle_input (); |
||
102 | } |
||
103 | return ret; |
||
104 | } |
||
105 | |||
106 | void ui_sin_cos_radius::draw () { |
||
107 | widget* wo [] = {&ol_sin, &ol_cos, &ol_radius}; int n; if (inc_radius) n = 3; else n = 2; |
||
108 | button* wb [] = {&b_sin, &b_cos, &b_radius}; |
||
109 | for (int i = 0; i < n; ++i) wo[i]->draw (); |
||
110 | for (int i = 0; i < n; ++i) { |
||
111 | button* bi = wb[i]; |
||
112 | if (bi->visible) bi->draw (); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | void ui_sin_cos_radius::set_option (label& lbl, const string& text, funktion** pf, funktion* pfv, button& bt, int vis) { |
||
117 | lbl.set_text (text); |
||
118 | *pf = pfv; |
||
119 | if (vis) { |
||
120 | bt.show (); |
||
121 | int spacer = 10; bt.set_pos (lbl.extents.right + spacer, lbl.extents.bottom); |
||
122 | } else bt.hide (); |
||
123 | } |
||
124 | |||
125 | void ui_sin_cos_radius::picked (label& l, int dir) { |
||
126 | if (&l == &ol_sin.option) { |
||
127 | sin = !sin; |
||
128 | if (sin) set_option (l, " Custom sin", &pf_sin, &cp_sin, b_sin, 1); else set_option (l, " Standard sin", &pf_sin, &st_sin, b_sin, 0); |
||
129 | } else if (&l == &ol_cos.option) { |
||
130 | cos = !cos; |
||
131 | if (cos) set_option (l, " Custom cos", &pf_cos, &cp_cos, b_cos, 1); else set_option (l, " Standard cos", &pf_cos, &st_cos, b_cos, 0); |
||
132 | } else { |
||
133 | radius = !radius; |
||
134 | if (radius) set_option (l, " Custom radius", &pf_radius, &cp_rad, b_radius, 1); else set_option (l, " Standard radius", &pf_radius, &st_radius, b_radius, 0); |
||
135 | } |
||
136 | lis->sin_cos_radius_optioned (); |
||
137 | } |
||
138 | |||
139 | void ui_sin_cos_radius::clicked (button& b) { |
||
140 | if (&b == &b_sin) edit_sin (); |
||
141 | else if (&b == &b_cos) edit_cos (); |
||
142 | else edit_radius (); |
||
143 | } |
||
144 | |||
145 | void ui_sin_cos_radius::edit_sin () { |
||
146 | uis.set_current (&sin_ed); |
||
147 | } |
||
148 | |||
149 | void ui_sin_cos_radius::edit_cos () { |
||
150 | uis.set_current (&cos_ed); |
||
151 | } |
||
152 | |||
153 | void ui_sin_cos_radius::edit_radius () { |
||
154 | uis.set_current (&rad_ed); |
||
155 | } |
||
156 | |||
157 | void ui_sin_cos_radius::edited (curve_editor* e, int i) { |
||
158 | multi_curve* crv = e->curveinfo[i].curve; |
||
159 | if (crv == &cp_sin.crv) cp_sin.sol.update (); |
||
160 | else if (crv == &cp_cos.crv) cp_cos.sol.update (); |
||
161 | else cp_rad.sol.update (); |
||
162 | lis->sin_cos_radius_edited (); |
||
163 | } |