My Project
listener.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "listener.h"
4 #include "interface\interface.h"
5 #include "interface\text.h"
6 #include "application.h"
7 
8 #define ROTATTION_ANGLE 5 * M_PI / 180
9 
10 extern HINSTANCE hInst;
11 PAINTSTRUCT ps;
14 
15 COLORREF brickColor = RGB(140, 140, 255);
16 
18 
19 //
20 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
21 //
22 // PURPOSE: process messages in main window
23 //
24 // WM_COMMAND — process menu
25 // WM_PAINT — draw main window
26 // WM_DESTROY — send exit message
27 //
28 //
29 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
30 {
31 
32  try
33  {
34 
35  static ActionDraw* actionDraw = new ActionDraw();
36  if (!actionDraw)
37  {
38  throw AllocationMemoryError();
39  }
40 
41  static Application* application = new Application(hWnd, 245, 0, 875, 750);
42  if (!application)
43  {
44  delete actionDraw;
45  throw AllocationMemoryError();
46  }
47 
48  switch (message)
49  {
50  case WM_CREATE:
51  {
52  drawUI->text(TXT_LEGO)
53  ->SetColor(RGB(255, 255, 255))
54  ->SetWeight(800)
55  ->SetHeight(36)
56  ->SetSymbolWidth(13)
57  ->display(20, 30, 0, 0, TEXT("LEGO"));
58  drawUI->text(TXT_DESIGNER)
59  ->SetColor(RGB(37, 45, 74))
60  ->SetWeight(400)
61  ->SetHeight(36)
62  ->SetSymbolWidth(11)
63  ->display(95, 30, 0, 0, TEXT("DESIGNER"));
64 
65  INITCOMMONCONTROLSEX init = { sizeof(INITCOMMONCONTROLSEX), ICC_WIN95_CLASSES };
66  InitCommonControlsEx(&init);
67 
68  InterfaceCtrlInit CtrlInit(hWnd, hInst);
69  UI = new BaseInterface;
70 
71  UI->combobox(CB_CHOOSEBRICK)->create(10, 250, 22, 223, TEXT("Choose brick type..."));
72 
73  UI->editfield(EDIT_X)->create(10, 290, 28, 70, TEXT("0"));
74  UI->editfield(EDIT_Y)->create(87, 290, 28, 70, TEXT("0"));
75  UI->editfield(EDIT_Z)->create(164, 290, 28, 70, TEXT("0"));
76 
77  drawUI->text(TXT_COLOR)
78  ->SetColor(RGB(211, 220, 236))
79  ->SetWeight(200)
80  ->SetHeight(22)
81  ->SetSymbolWidth(8)
82  ->display(20, 335, 0, 0, TEXT("Color"));
83 
84  drawUI->rectangle(RCT_COLOR)
85  ->SetBorderColor(RGB(255, 255, 255))
86  ->SetBorderWidth(1)
88  ->display(88, 330, 156, 363, NULL);
89 
90  UI->button(BTN_CHCOLOR)->create(164, 330, 33, 70, TEXT("Change!"));
92 
93  UI->button(BTN_OK)->create(10, 500, 33, 223, TEXT("Add Brick!"));
95 
96 
97  ActionLoadbrick* LoadFirst = new ActionLoadbrick("objs/untitled.obj");
98  if (!LoadFirst)
99  {
100  throw AllocationMemoryError();
101  }
102 
103  application->call(*LoadFirst, 0);
104  UI->combobox(CB_CHOOSEBRICK)->addItem(TEXT("2x1"));
105 
106  }
107  break;
108 
109  case WM_SETCURSOR:
110  if ((HWND)wParam == UI->button(BTN_OK)->getHWND())
111  {
112  SetCursor(LoadCursor(nullptr, IDC_HAND));
114  return true;
115  }
116  else
117  {
119  if ((HWND)wParam == UI->button(BTN_CHCOLOR)->getHWND())
120  {
121  SetCursor(LoadCursor(nullptr, IDC_HAND));
123  return true;
124  }
125  else
126  {
127  SetCursor(LoadCursor(nullptr, IDC_ARROW));
129  return true;
130  }
131  }
132  break;
133  case WM_COMMAND:
134  case WM_KEYDOWN:
135  {
136  int wmId = LOWORD(wParam);
137  // Ðàçîáðàòü âûáîð â ìåíþ:
138  switch (wmId)
139  {
140  case CB_CHOOSEBRICK:
141  {
142 
143  }
144  break;
145  case BTN_CHCOLOR:
146  {
149  RedrawWindow(hWnd, NULL, 0, RDW_INVALIDATE);
150  }
151  break;
152  case BTN_OK:
153  {
154  int brickID = UI->combobox(CB_CHOOSEBRICK)->getCurrentItem();
155  if (brickID == 0)
156  {
157  throw ModelChoosingError();
158  }
159 
160  int X = UI->editfield(EDIT_X)->getInt();
161  int Y = UI->editfield(EDIT_Y)->getInt();
162  int Z = UI->editfield(EDIT_Z)->getInt();
163 
164  ActionAddbrick* addBrick = new ActionAddbrick(X,Y,Z, brickColor);
165  if (!addBrick)
166  {
167  throw AllocationMemoryError();
168  }
169 
170  application->call(*addBrick, brickID-1);
171  application->call(*actionDraw, 0);
172  SetFocus(hWnd);
173  }
174  break;
175  case VK_RIGHT:
176  {
178  if (!rotatex)
179  {
180  throw AllocationMemoryError();
181  }
182 
183  application->call(*rotatex, 0);
184  application->call(*actionDraw, 0);
185  }
186  break;
187  case VK_LEFT:
188  {
190  if (!rotatex)
191  {
192  throw AllocationMemoryError();
193  }
194 
195  application->call(*rotatex, 0);
196  application->call(*actionDraw, 0);
197  }
198  break;
199  case VK_UP:
200  {
202  if (!rotatez)
203  {
204  throw AllocationMemoryError();
205  }
206 
207  application->call(*rotatez, 0);
208  application->call(*actionDraw, 0);
209  }
210  break;
211  case VK_DOWN:
212  {
214  if (!rotatez)
215  {
216  throw AllocationMemoryError();
217  }
218 
219  application->call(*rotatez, 0);
220  application->call(*actionDraw, 0);
221  }
222  break;
223  case 104: // 8 NUMPAD
224  {
226  if (!rotateh)
227  {
228  throw AllocationMemoryError();
229  }
230 
231  application->call(*rotateh, 0);
232  application->call(*actionDraw, 0);
233  }
234  break;
235  case 98: // 2 NUMPAD
236  {
238  if (!rotateh)
239  {
240  throw AllocationMemoryError();
241  }
242 
243  application->call(*rotateh, 0);
244  application->call(*actionDraw, 0);
245  }
246  break;
247  case 100: // 4 NUMPAD
248  {
250  if (!rotatev)
251  {
252  throw AllocationMemoryError();
253  }
254 
255  application->call(*rotatev, 0);
256  application->call(*actionDraw, 0);
257  }
258  break;
259  case 102: // 6 NUMPAD
260  {
262  if (!rotatev)
263  {
264  throw AllocationMemoryError();
265  }
266 
267  application->call(*rotatev, 0);
268  application->call(*actionDraw, 0);
269  }
270  break;
271  case IDM_EXIT:
272  DestroyWindow(hWnd);
273  break;
274  default:
275  return DefWindowProc(hWnd, message, wParam, lParam);
276  }
277  }
278  break;
279  case WM_PAINT:
280  {
281  double width = 1;
282  double height = 1;
283  HDC hdc = BeginPaint(hWnd, &ps);
284  InterfaceDrawInit DrawInit(hdc);
285  drawUI->redraw();
286  application->call(*actionDraw, 0);
287  EndPaint(hWnd, &ps);
288  }
289  break;
290  case WM_DESTROY:
291  PostQuitMessage(0);
292  break;
293  default:
294  return DefWindowProc(hWnd, message, wParam, lParam);
295  }
296  }
297  catch (BaseException& err)
298  {
299  WCHAR msg[256];
300  MultiByteToWideChar(0, 0, err.what(), 255, msg, 256);
301  MessageBox(NULL, msg, TEXT("ERROR"), MB_OK);
302  }
303 
304  return 0;
305 }
int getCurrentItem()
Definition: combobox.cpp:53
#define EDIT_X
Definition: stdafx.h:43
HINSTANCE hInst
Definition: lego.cpp:12
#define BTN_COLOR_ACTIVE
Definition: Resource.h:23
Text * SetWeight(int w)
Definition: text.cpp:57
bool redraw(int ID=-1)
Text interface class.
#define EDIT_Z
Definition: stdafx.h:45
double angle
Definition: listener.cpp:17
URectangle * SetColor(COLORREF color)
Definition: rectangle.cpp:36
void call(Action &act, int id)
Definition: application.cpp:35
int getInt()
Definition: editfield.cpp:49
URectangle * rectangle(int ID)
Editfield * editfield(int ID)
Definition: interface.cpp:73
Application * application
Definition: lego.cpp:15
Combobox * combobox(int ID)
Definition: interface.cpp:56
#define BTN_CHCOLOR
Definition: stdafx.h:48
BaseInterface * UI
Definition: listener.cpp:12
PAINTSTRUCT ps
Definition: listener.cpp:11
Common draw interface class.
Main interface class. Provides access to create/remove/update elements.
Definition: interface.h:138
BaseDrawInterface * drawUI
Definition: listener.cpp:13
Text * text(int ID)
COLORREF colorDialog()
Definition: interface.cpp:103
#define BTN_ADDBRICK_ACTIVE
Definition: Resource.h:21
#define TXT_DESIGNER
Definition: stdafx.h:42
#define TXT_LEGO
Definition: stdafx.h:41
HWND create(int X, int Y, int HEIGHT, int WIDTH, WCHAR *TEXT) override
Definition: combobox.cpp:25
#define CB_CHOOSEBRICK
Definition: Resource.h:19
URectangle * SetBorderWidth(int w)
Definition: rectangle.cpp:48
#define TXT_COLOR
Definition: stdafx.h:46
#define RCT_COLOR
Definition: stdafx.h:47
Main drwa interface class. Provides access to create/remove/update elements.
LRESULT setImage(int rID)
Definition: button.cpp:38
COLORREF brickColor
Definition: listener.cpp:15
HWND create(int X, int Y, int HEIGHT, int WIDTH, WCHAR *TEXT) override
Definition: button.cpp:25
URectangle * SetBorderColor(COLORREF color)
Definition: rectangle.cpp:42
#define EDIT_Y
Definition: stdafx.h:44
void addItem(WCHAR *item)
Definition: combobox.cpp:44
virtual const char * what() const =0
#define BTN_ADDBRICK
Definition: Resource.h:20
Text * SetColor(COLORREF color)
Definition: text.cpp:50
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: listener.cpp:29
Text * SetSymbolWidth(int w)
Definition: text.cpp:69
#define ROTATTION_ANGLE
Definition: listener.cpp:8
bool display(int X1, int Y1, int X2, int Y2, WCHAR *TEXT) override
Definition: text.cpp:32
#define BTN_COLOR
Definition: Resource.h:22
Text * SetHeight(int h)
Definition: text.cpp:63
#define BTN_OK
Definition: stdafx.h:40
bool display(int X1, int Y1, int X2, int Y2, WCHAR *TEXT) override
Definition: rectangle.cpp:18
HWND create(int X, int Y, int HEIGHT, int WIDTH, WCHAR *TEXT) override
Definition: editfield.cpp:25
HWND getHWND()
Definition: button.cpp:44
#define IDM_EXIT
Definition: Resource.h:12
Button * button(int ID)
Definition: interface.cpp:39