xref: /haiku/src/apps/glteapot/TeapotWindow.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2 	Copyright 1999, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 */
5 
6 #include <stdio.h>
7 #include <new>
8 
9 #include <Catalog.h>
10 #include <InterfaceKit.h>
11 #include <Point.h>
12 #include <Rect.h>
13 
14 #include "TeapotWindow.h"
15 
16 #undef B_TRANSLATION_CONTEXT
17 #define B_TRANSLATION_CONTEXT "TeapotWindow"
18 
19 TeapotWindow::TeapotWindow(BRect rect, const char* name, window_type wt,
20 	ulong something)
21 	:
22 	BDirectWindow(rect, name, wt, something)
23 {
24 	GLenum type = BGL_RGB | BGL_DEPTH | BGL_DOUBLE;
25 
26 	Lock();
27 	BRect bounds = Bounds();
28 	bounds.bottom = bounds.top + 14;
29 	BMenuBar* menuBar = new BMenuBar(bounds, "main menu");
30 
31 	BMenu* menu;
32 	BMessage msg(kMsgAddModel);
33 
34 	menuBar->AddItem(menu = new BMenu(B_TRANSLATE("File")));
35 	AddChild(menuBar);
36 
37 	menuBar->ResizeToPreferred();
38 
39 	bounds = Bounds();
40 	bounds.top = menuBar->Bounds().bottom + 1;
41 	BView *subView = new BView(bounds, "subview", B_FOLLOW_ALL, 0);
42 	AddChild(subView);
43 
44 	bounds = subView->Bounds();
45 	fObjectView = new(std::nothrow) ObjectView(bounds, "objectView",
46 		B_FOLLOW_ALL_SIDES, type);
47 	subView->AddChild(fObjectView);
48 
49 	BMenuItem*	item;
50 	msg.AddInt32("num", 256);
51 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Add a teapot"),
52 		new BMessage(msg), 'N'));
53 	item->SetTarget(fObjectView);
54 	menu->AddSeparatorItem();
55 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Quit"),
56 		new BMessage(B_QUIT_REQUESTED), 'Q'));
57 	item->SetTarget(be_app);
58 	msg.RemoveName("num");
59 	menuBar->AddItem(menu = new BMenu(B_TRANSLATE("Settings")));
60 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Perspective"),
61 		new BMessage(kMsgPerspective)));
62 	item->SetTarget(fObjectView);
63 	item->SetMarked(false);
64 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("FPS display"),
65 		new BMessage(kMsgFPS)));
66 	item->SetTarget(fObjectView);
67 	item->SetMarked(true);
68 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Filled polygons"),
69 		new BMessage(kMsgFilled)));
70 	item->SetTarget(fObjectView);
71 	item->SetMarked(true);
72 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Lighting"),
73 		new BMessage(kMsgLighting)));
74 	item->SetTarget(fObjectView);
75 	item->SetMarked(true);
76 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Backface culling"),
77 		new BMessage(kMsgCulling)));
78 	item->SetTarget(fObjectView);
79 	item->SetMarked(true);
80 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Z-buffered"),
81 		new BMessage(kMsgZBuffer)));
82 	item->SetTarget(fObjectView);
83 	item->SetMarked(true);
84 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Gouraud shading"),
85 		new BMessage(kMsgGouraud)));
86 	item->SetTarget(fObjectView);
87 	item->SetMarked(true);
88 //	menu->AddItem(item = new BMenuItem("Texture mapped", new BMessage(kMsgTextured)));
89 //	item->SetTarget(fObjectView);
90 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Fog"),
91 		new BMessage(kMsgFog)));
92 	item->SetTarget(fObjectView);
93 
94 	BMenu *subMenu;
95 	menuBar->AddItem(menu = new BMenu(B_TRANSLATE("Lights")));
96 	msg.what = kMsgLights;
97 
98 	msg.AddInt32("num", 1);
99 	menu->AddItem(item = new BMenuItem(subMenu =
100 		new BMenu(B_TRANSLATE("Upper center")), NULL));
101 	item->SetTarget(fObjectView);
102 	msg.AddInt32("color", lightNone);
103 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
104 		new BMessage(msg)));
105 	item->SetTarget(fObjectView);
106 	subMenu->AddSeparatorItem();
107 	msg.ReplaceInt32("color", lightWhite);
108 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
109 		new BMessage(msg)));
110 	item->SetTarget(fObjectView);
111 	item->SetMarked(true);
112 	msg.ReplaceInt32("color", lightYellow);
113 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
114 		new BMessage(msg)));
115 	item->SetTarget(fObjectView);
116 	msg.ReplaceInt32("color", lightBlue);
117 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
118 		new BMessage(msg)));
119 	item->SetTarget(fObjectView);
120 	msg.ReplaceInt32("color", lightRed);
121 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
122 		new BMessage(msg)));
123 	item->SetTarget(fObjectView);
124 	msg.ReplaceInt32("color", lightGreen);
125 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
126 		new BMessage(msg)));
127 	item->SetTarget(fObjectView);
128 
129 	msg.RemoveName("color");
130 
131 	msg.ReplaceInt32("num", 2);
132 	menu->AddItem(item = new BMenuItem(subMenu =
133 		new BMenu(B_TRANSLATE("Lower left")), NULL));
134 	item->SetTarget(fObjectView);
135 	msg.AddInt32("color", lightNone);
136 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
137 		new BMessage(msg)));
138 	item->SetTarget(fObjectView);
139 	subMenu->AddSeparatorItem();
140 	msg.ReplaceInt32("color", lightWhite);
141 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
142 		new BMessage(msg)));
143 	item->SetTarget(fObjectView);
144 	msg.ReplaceInt32("color", lightYellow);
145 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
146 		new BMessage(msg)));
147 	item->SetTarget(fObjectView);
148 	msg.ReplaceInt32("color", lightBlue);
149 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
150 		new BMessage(msg)));
151 	item->SetTarget(fObjectView);
152 	item->SetMarked(true);
153 	msg.ReplaceInt32("color", lightRed);
154 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
155 		new BMessage(msg)));
156 	item->SetTarget(fObjectView);
157 	msg.ReplaceInt32("color", lightGreen);
158 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
159 		new BMessage(msg)));
160 	item->SetTarget(fObjectView);
161 
162 	msg.RemoveName("color");
163 
164 	msg.ReplaceInt32("num", 3);
165 	menu->AddItem(item = new BMenuItem(subMenu =
166 		new BMenu(B_TRANSLATE("Right")), NULL));
167 	item->SetTarget(fObjectView);
168 	msg.AddInt32("color", lightNone);
169 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
170 		new BMessage(msg)));
171 	item->SetTarget(fObjectView);
172 	item->SetMarked(true);
173 	subMenu->AddSeparatorItem();
174 	msg.ReplaceInt32("color", lightWhite);
175 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
176 		new BMessage(msg)));
177 	item->SetTarget(fObjectView);
178 	msg.ReplaceInt32("color", lightYellow);
179 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
180 		new BMessage(msg)));
181 	item->SetTarget(fObjectView);
182 	msg.ReplaceInt32("color", lightBlue);
183 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
184 		new BMessage(msg)));
185 	item->SetTarget(fObjectView);
186 	msg.ReplaceInt32("color", lightRed);
187 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
188 		new BMessage(msg)));
189 	item->SetTarget(fObjectView);
190 	msg.ReplaceInt32("color", lightGreen);
191 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
192 		new BMessage(msg)));
193 	item->SetTarget(fObjectView);
194 
195 	float f = menuBar->Bounds().IntegerHeight() + 1;
196 	SetSizeLimits(32, 1024, 32 + f, 1024 + f);
197 			//TODO: verify, adding an height to x seems strange
198 	Unlock();
199 }
200 
201 
202 bool
203 TeapotWindow::QuitRequested()
204 {
205 	if (fObjectView != NULL)
206 		fObjectView->EnableDirectMode(false);
207 
208 	be_app->PostMessage(B_QUIT_REQUESTED);
209 	return true;
210 }
211 
212 
213 void
214 TeapotWindow::DirectConnected(direct_buffer_info* info)
215 {
216 	// TODO: Direct rendering causes the mouse to flicker due
217 	// to the lack of a Hardware Cursor, however without
218 	// it the teapot freezes on mouse move. (bug?)
219 	#if 1
220 	if (fObjectView != NULL) {
221 		fObjectView->DirectConnected(info);
222 		fObjectView->EnableDirectMode(true);
223 	}
224 	#endif
225 }
226 
227 
228 void
229 TeapotWindow::MessageReceived(BMessage* msg)
230 {
231 //	msg->PrintToStream();
232 	switch (msg->what) {
233 		default:
234 			BDirectWindow::MessageReceived(msg);
235 	}
236 }
237