xref: /haiku/src/apps/glteapot/TeapotWindow.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
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_SYSTEM_NAME("GLTeapot")));
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 	menu->AddSeparatorItem();
94 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Limit FPS to refresh rate"),
95 		new BMessage(kMsgLimitFps)));
96 	item->SetTarget(fObjectView);
97 	item->SetMarked(true);
98 
99 	BMenu *subMenu;
100 	menuBar->AddItem(menu = new BMenu(B_TRANSLATE("Lights")));
101 	msg.what = kMsgLights;
102 
103 	msg.AddInt32("num", 1);
104 	menu->AddItem(item = new BMenuItem(subMenu =
105 		new BMenu(B_TRANSLATE("Upper center")), NULL));
106 	item->SetTarget(fObjectView);
107 	msg.AddInt32("color", lightNone);
108 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
109 		new BMessage(msg)));
110 	item->SetTarget(fObjectView);
111 	subMenu->AddSeparatorItem();
112 	msg.ReplaceInt32("color", lightWhite);
113 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
114 		new BMessage(msg)));
115 	item->SetTarget(fObjectView);
116 	item->SetMarked(true);
117 	msg.ReplaceInt32("color", lightYellow);
118 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
119 		new BMessage(msg)));
120 	item->SetTarget(fObjectView);
121 	msg.ReplaceInt32("color", lightBlue);
122 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
123 		new BMessage(msg)));
124 	item->SetTarget(fObjectView);
125 	msg.ReplaceInt32("color", lightRed);
126 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
127 		new BMessage(msg)));
128 	item->SetTarget(fObjectView);
129 	msg.ReplaceInt32("color", lightGreen);
130 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
131 		new BMessage(msg)));
132 	item->SetTarget(fObjectView);
133 
134 	msg.RemoveName("color");
135 
136 	msg.ReplaceInt32("num", 2);
137 	menu->AddItem(item = new BMenuItem(subMenu =
138 		new BMenu(B_TRANSLATE("Lower left")), NULL));
139 	item->SetTarget(fObjectView);
140 	msg.AddInt32("color", lightNone);
141 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
142 		new BMessage(msg)));
143 	item->SetTarget(fObjectView);
144 	subMenu->AddSeparatorItem();
145 	msg.ReplaceInt32("color", lightWhite);
146 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
147 		new BMessage(msg)));
148 	item->SetTarget(fObjectView);
149 	msg.ReplaceInt32("color", lightYellow);
150 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
151 		new BMessage(msg)));
152 	item->SetTarget(fObjectView);
153 	msg.ReplaceInt32("color", lightBlue);
154 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
155 		new BMessage(msg)));
156 	item->SetTarget(fObjectView);
157 	item->SetMarked(true);
158 	msg.ReplaceInt32("color", lightRed);
159 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
160 		new BMessage(msg)));
161 	item->SetTarget(fObjectView);
162 	msg.ReplaceInt32("color", lightGreen);
163 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
164 		new BMessage(msg)));
165 	item->SetTarget(fObjectView);
166 
167 	msg.RemoveName("color");
168 
169 	msg.ReplaceInt32("num", 3);
170 	menu->AddItem(item = new BMenuItem(subMenu =
171 		new BMenu(B_TRANSLATE("Right")), NULL));
172 	item->SetTarget(fObjectView);
173 	msg.AddInt32("color", lightNone);
174 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Off"),
175 		new BMessage(msg)));
176 	item->SetTarget(fObjectView);
177 	item->SetMarked(true);
178 	subMenu->AddSeparatorItem();
179 	msg.ReplaceInt32("color", lightWhite);
180 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("White"),
181 		new BMessage(msg)));
182 	item->SetTarget(fObjectView);
183 	msg.ReplaceInt32("color", lightYellow);
184 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Yellow"),
185 		new BMessage(msg)));
186 	item->SetTarget(fObjectView);
187 	msg.ReplaceInt32("color", lightBlue);
188 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Blue"),
189 		new BMessage(msg)));
190 	item->SetTarget(fObjectView);
191 	msg.ReplaceInt32("color", lightRed);
192 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Red"),
193 		new BMessage(msg)));
194 	item->SetTarget(fObjectView);
195 	msg.ReplaceInt32("color", lightGreen);
196 	subMenu->AddItem(item = new BMenuItem(B_TRANSLATE("Green"),
197 		new BMessage(msg)));
198 	item->SetTarget(fObjectView);
199 
200 	float f = menuBar->Bounds().IntegerHeight() + 1;
201 	SetSizeLimits(32, 1024, 32 + f, 1024 + f);
202 			//TODO: verify, adding an height to x seems strange
203 	Unlock();
204 }
205 
206 
207 bool
208 TeapotWindow::QuitRequested()
209 {
210 	if (fObjectView != NULL)
211 		fObjectView->EnableDirectMode(false);
212 
213 	be_app->PostMessage(B_QUIT_REQUESTED);
214 	return true;
215 }
216 
217 
218 void
219 TeapotWindow::DirectConnected(direct_buffer_info* info)
220 {
221 	// TODO: Direct rendering causes the mouse to flicker due
222 	// to the lack of a Hardware Cursor, however without
223 	// it the teapot freezes on mouse move. (bug?)
224 	#if 1
225 	if (fObjectView != NULL) {
226 		fObjectView->DirectConnected(info);
227 		fObjectView->EnableDirectMode(true);
228 	}
229 	#endif
230 }
231 
232 
233 void
234 TeapotWindow::MessageReceived(BMessage* msg)
235 {
236 //	msg->PrintToStream();
237 	switch (msg->what) {
238 		default:
239 			BDirectWindow::MessageReceived(msg);
240 	}
241 }
242