summaryrefslogtreecommitdiff
path: root/MainWindow.h
blob: 11108066d4f457d401eb98cabcf1463b0d7b99ba (plain)
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
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <fx.h>
#include <FXScrollArea.h>
#include <FXMessageBox.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <memory>
#include <string>
#include <fxkeys.h>
#include <cstdlib>
#include "Gate.h"
#include "icons.h"
#include "pugixml.hpp" // saving/loading

class MainWindow : public FXMainWindow
{
	FXDECLARE(MainWindow)
	struct selected_input
	{
		Gate *gate;
		int input;
	};

	public:
		explicit MainWindow(FXApp* a);
		~MainWindow();
		virtual void create();

		/* Event system */
		// Messages for our class
		enum {
			ID_CANVAS       = FXMainWindow::ID_LAST,
			ID_BUTTON_INPUT,
			ID_BUTTON_OUTPUT,
			ID_BUTTON_AND,
			ID_BUTTON_NAND,
			ID_BUTTON_OR,
			ID_BUTTON_NOR,
			ID_BUTTON_XOR,
			ID_BUTTON_XNOR,
			ID_BUTTON_NOT,

			ID_BUTTON_SAVE,
			ID_BUTTON_LOAD,
		};

		/* Event handlers */
		long on_paint(FXObject*,FXSelector,void* ptr);
		long on_left_mouse_down(FXObject*,FXSelector,void* ptr);
		long on_left_mouse_up(FXObject*,FXSelector,void* ptr);
		long on_right_mouse_down(FXObject*,FXSelector,void* ptr);
		long on_key_press(FXObject*,FXSelector,void* ptr);
		long on_key_release(FXObject*,FXSelector,void* ptr);
		long on_mouse_move(FXObject*,FXSelector,void* ptr);
		/* toolbox */
		long input_button_press(FXObject*,FXSelector,void* ptr);
		long output_button_press(FXObject*,FXSelector,void* ptr);
		long and_button_press(FXObject*,FXSelector,void* ptr);
		long nand_button_press(FXObject*,FXSelector,void* ptr);
		long or_button_press(FXObject*,FXSelector,void* ptr);
		long nor_button_press(FXObject*,FXSelector,void* ptr);
		long xor_button_press(FXObject*,FXSelector,void* ptr);
		long xnor_button_press(FXObject*,FXSelector,void* ptr);
		long not_button_press(FXObject*,FXSelector,void* ptr);

		/* options */
		long save_button_press(FXObject*,FXSelector,void* ptr);
		long load_button_press(FXObject*,FXSelector,void* ptr);

		/* selected gate */
		Gate *selected_gate = nullptr;

		FXApp *get_app(){ return app; };


	protected:
		MainWindow(){}

	private:
		void create_ui();
		void draw();
		void update_gate_state(Gate *gate);
		void find_selected_input(int x, int y);
		Gate *find_gate_at(int x, int y);
		Gate *find_gate_by_id(int id);
		void remove_all_gates();
		void find_gates_in_area(int x, int y, int width, int height);

		bool save_file();
		bool load_file();

		FXHorizontalFrame *contents;
		FXVerticalFrame   *canvasFrame;
		FXVerticalFrame   *toolsFrame;
		FXVerticalFrame   *optionsFrame;
		FXScrollWindow 	*scroll_area;
		FXScrollWindow 	*toolbox_scroll_area;
		FXScrollWindow 	*options_scroll_area;
		FXCanvas *canvas;
		FXBMPImage *canvas_image;
		FXApp *app;

		FXHorizontalFrame *input1_frame;
		FXHorizontalFrame *input2_frame;
		FXHorizontalFrame *output_state_frame;

		FXLabel *input_1_details;
		FXLabel *input_2_details;
		FXLabel *output_details;

		/* icons */
		FXGIFIcon *INPUT_icon;
		FXGIFIcon *OUTPUT_icon;
		FXGIFIcon *AND_icon;
		FXGIFIcon *NAND_icon;
		FXGIFIcon *OR_icon;
		FXGIFIcon *NOR_icon;
		FXGIFIcon *XOR_icon;
		FXGIFIcon *XNOR_icon;
		FXGIFIcon *NOT_icon;

		/* buttons */
		FXButton *INPUT_button;
		FXButton *OUTPUT_button;
		FXButton *AND_button;
		FXButton *NAND_button;
		FXButton *OR_button;
		FXButton *NOR_button;
		FXButton *XOR_button;
		FXButton *XNOR_button;
		FXButton *NOT_button;

		Gate::GATE_TYPE selected_gate_type = Gate::NONE; // the type of gate we will place
		struct selected_input selected_input;
		std::vector<Gate *> selected_gates;

		/* mouse */
		bool lmouse_down = false;
		bool rmouse_down = false;
		bool dragging_link = false;
		bool rubberbanding = false;
		int rubberband_startx;
		int rubberband_starty;
		int multiple_move_startx;
		int multiple_move_starty;

		/* keyboard */
		bool lshift_down = false;

		/* general */
		std::vector<std::unique_ptr<Gate>> gates;

		/* saving/loading */
		std::string file_name = "";

};

#endif // MAINWINDOW_H