Changeset 45
- Timestamp:
- 07/12/07 19:58:02 (1 year ago)
- Files:
-
- trunk/src/demo2/main.d (modified) (3 diffs)
- trunk/src/yage/gui/surface.d (modified) (12 diffs)
- trunk/src/yage/resource/texture.d (modified) (1 diff)
- trunk/src/yage/system/device.d (modified) (2 diffs)
- trunk/src/yage/system/input.d (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/demo2/main.d
r43 r45 57 57 CameraNode camera = new CameraNode(ship.getCameraSpot()); 58 58 camera.setView(2, 20000, 60, 0, 1); // wide angle view 59 60 Surface disp = new Surface(null); 61 disp.topLeft = Vec2f(0,0); 62 disp.bottomRight = Vec2f(1, 1); 59 60 Surface bg = new Surface(null); 61 bg.topLeft = Vec2f(.05,.05); 62 bg.bottomRight = Vec2f(.95, .95); 63 bg.setTexture(new GPUTexture("test2.png")); 64 bg.fill = stretched; 65 bg.setVisibility(true); 66 67 Surface disp = new Surface(bg); 68 disp.topLeft = Vec2f(.1,.1); 69 disp.bottomRight = Vec2f(.9, .9); 70 disp.setTexture(new GPUTexture("test/bc-dark.png")); 71 //disp.fill = stretched; 63 72 disp.setVisibility(true); 64 73 65 74 Surface first = new Surface(disp); 66 first.texture = camera.getTexture(); 67 first.topLeft = Vec2f(0,0); 68 first.bottomRight = Vec2f(.75, .75); 75 first.setTexture(new GPUTexture("test.png")); 76 first.fill = stretched; 77 first.topLeft = Vec2f(.8, .8); 78 first.bottomRight = Vec2f(.95, .95); 69 79 first.setVisibility(true); 70 80 81 Surface third = new Surface(disp); 82 third.setTexture(new GPUTexture("box.png")); 83 third.fill = stretched; 84 third.topLeft = Vec2f(.8, .05); 85 third.bottomRight = Vec2f(.95, .2); 86 third.setVisibility(true); 87 88 Surface fourth = new Surface(disp); 89 fourth.setTexture(new GPUTexture("test/button2.png")); 90 fourth.fill = stretched; 91 fourth.topLeft = Vec2f(.05, .8); 92 fourth.bottomRight = Vec2f(.2, .95); 93 fourth.setVisibility(true); 94 95 Surface fifth = new Surface(disp); 96 fifth.setTexture(new GPUTexture("test/radio1.png")); 97 fifth.fill = stretched; 98 fifth.topLeft = Vec2f(.05, .05); 99 fifth.bottomRight = Vec2f(.2, .2); 100 fifth.setVisibility(true); 101 71 102 Surface second = new Surface(disp); 72 second. texture = camera.getTexture();73 second.topLeft = Vec2f(.2 5,.25);74 second.bottomRight = Vec2f( 1,1);103 second.setTexture(camera.getTexture()); 104 second.topLeft = Vec2f(.2,.2); 105 second.bottomRight = Vec2f(.8, .8); 75 106 second.setVisibility(true); 107 108 Surface clear = new Surface(second); 109 clear.setTexture(new GPUTexture("test/clear.png")); 110 clear.topLeft = Vec2f(.1,.1); 111 clear.bottomRight = Vec2f(.9, .9); 112 clear.fill = stretched; 113 clear.setVisibility(true); 76 114 77 115 void onMousedown(Surface self, byte buttons, Vec2i coordinates){ … … 81 119 } 82 120 83 void onResize(Surface self, Vec2i difference){ 84 camera.setResolution(self.size.x, self.size.y); 85 writefln("Resolution changed to ", self.size.x, " x ", self.size.y); 86 } 87 88 first.onMousedown = &onMousedown; 121 void onResize(Surface self){ 122 int xres = self.position2.x - self.position1.x; 123 int yres = self.position2.y - self.position1.y; 124 camera.setResolution(xres, yres); 125 writefln("Camera resolution changed to ", xres, " x ", yres); 126 } 127 128 void onMousedown2(Surface self, byte buttons, Vec2i coordinates){ 129 self.raise(); 130 Input.setSurfaceLock(self); 131 } 132 void onMouseup2(Surface self, byte buttons, Vec2i coordinates){ 133 Input.unlockSurface(); 134 } 135 void onMousemove(Surface self, byte buttons, Vec2i coordinates){ 136 if(buttons == 1) self.moveAdd(coordinates); 137 } 138 89 139 second.onMousedown = &onMousedown; 90 first.onResize = &onResize; 140 second.onResize = &onResize; 141 clear.onMousedown = &onMousedown2; 142 clear.onMousemove = &onMousemove; 143 clear.onMouseup = &onMouseup2; 144 first.onMousedown = &onMousedown2; 145 first.onMousemove = &onMousemove; 146 first.onMouseup = &onMouseup2; 147 148 bg.onMousedown = &onMousedown2; 149 bg.onMousemove = &onMousemove; 150 bg.onMouseup = &onMouseup2; 91 151 92 152 // Music … … 151 211 Input.processInput(); 152 212 camera.toTexture(); 153 disp.render();213 bg.render(); 154 214 155 215 trunk/src/yage/gui/surface.d
r43 r45 13 13 import yage.system.device; 14 14 import yage.system.constant; 15 import yage.system.input; 15 16 import yage.resource.texture; 16 17 import yage.gui.style; 17 18 19 20 //move to constants 21 enum{ 22 traditional, //default 23 stretched, 24 tiled 25 } 26 27 float third = 1.0/3.0; 18 28 19 29 /** … … 23 33 * The children will be positioned relative to the borders of their parent. */ 24 34 class Surface{ 25 static final Style defaultStyle;26 Style style;27 28 //Style style; //move style into a higher level clas, perhaps make a geometry struct isntead29 GPUTexture texture;//Change from GPUTexture to Texture or Material30 31 //Linked list would be faster for raising a window.32 Surface[] subs; //Perhaps this should be changed into a linked list...35 //Not sure that a Surface should have a style... I think surface should be a lower abstraction, for only geometry, rendering, and input. 36 //static final Style defaultStyle; //Default style should be more fitting as something that is global. 37 //Style style; 38 39 //Change from GPUTexture to Texture or Material 40 private GPUTexture texture; 41 42 Surface[] subs; 33 43 //Not sure if I should have a reference to Parent or not, but for now, I will. 34 44 Surface parent; 35 45 36 Vec2f portion; //used for the texture only 37 38 Vec2f topLeft; 46 Vec2f topLeft;//rid self of these 39 47 Vec2f bottomRight; 40 48 41 //these are used for rendering, not calculation49 //these are used for rendering, not for user 42 50 Vec2i position1; 43 51 Vec2i position2; 44 52 45 46 Vec2i size; 53 Vec2i location; 54 55 //used for the texture 56 Vec2f portion; 47 57 48 58 bool visible; 49 50 51 //add root position and stuff 59 60 byte fill = traditional; 52 61 53 62 //Not sure how to impelement gluUnProject 54 63 64 //Perhaps add some of these for global events. 55 65 void delegate(typeof(this) self) onBlur; 56 void blur(){ if(onBlur)onBlur(this);}57 66 67 //dunno how 58 68 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onClick; 59 69 70 //dunno how 60 71 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onDblclick; 61 72 62 void delegate(typeof(this) self) onFocus; 73 void delegate(typeof(this) self) onFocus; //Done -- See Raise, no fall through 63 74 64 75 void delegate(typeof(this) self, byte key, byte modifiers) onKeydown; … … 68 79 void delegate(typeof(this) self, byte key, byte modifiers) onKeyup; 69 80 70 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousedown; 71 void mousedown(byte buttons, Vec2i coordinates){ if(onMousedown)onMousedown(this, buttons, coordinates);} 72 73 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousemove; 74 void mousemove(byte buttons, Vec2i coordinates){ if(onMousemove)onMousemove(this, buttons, coordinates);} 81 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousedown; //Done 82 void mousedown(byte buttons, Vec2i coordinates){ 83 if(onMousedown)onMousedown(this, buttons, coordinates); 84 else if(parent !is null) parent.mousedown(buttons, coordinates); 85 } 86 87 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousemove; //Done 88 void mousemove(byte buttons, Vec2i coordinates){ 89 if(onMousemove)onMousemove(this, buttons, coordinates); 90 else if(parent !is null) parent.mousemove(buttons, coordinates); 91 } 75 92 76 93 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseout; … … 78 95 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseover; 79 96 80 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseup; 81 void mouseup(byte buttons, Vec2i coordinates){ if(onMouseup)onMouseup(this, buttons, coordinates);} 82 83 void delegate(typeof(this) self, Vec2i difference) onResize; 97 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseup; //Done 98 void mouseup(byte buttons, Vec2i coordinates){ 99 if(onMouseup)onMouseup(this, buttons, coordinates); 100 else if(parent !is null) parent.mouseup(buttons, coordinates); 101 } 102 103 void delegate(typeof(this) self) onResize; //Done -- See recalculate, no fall through 84 104 85 105 … … 88 108 if(parent is null){ 89 109 Device.subs ~= this; 90 recalculate(Device.getHeight(), Device.getWidth());110 this.recalculate(); 91 111 } 92 112 else{ 93 113 parent.subs ~= this; 94 recalculate(parent.size.x, parent.size.y);114 this.recalculate(); 95 115 } 96 116 } … … 101 121 } 102 122 103 void recalculate(int width, int height){ //not done 104 position1.x = cast(int)(topLeft.x * cast(float)width); 105 position1.y = cast(int)(topLeft.y * cast(float)height); 106 107 position2.x = cast(int)(bottomRight.x * cast(float)width); 108 position2.y = cast(int)(bottomRight.y * cast(float)height); 109 110 Vec2i temp = size; 111 112 size.x = position2.x - position1.x; 113 size.y = position2.y - position1.y; 114 115 if(onResize)onResize(this, Vec2i(temp.x - size.x, temp.y - size.y)); 123 //MAYBE I SHOULD PUT RECALCUALTE INTO A DELEGATE SO THAT THE USER COULD PICK ANYTHING! 124 void recalculate(Vec2i parent1, Vec2i parent2){ //not done 125 Vec2i parentSize = Vec2i(parent2.x - parent1.x, parent2.y - parent1.y); 126 127 Vec2i tempPosition1; 128 Vec2i tempPosition2; 129 130 tempPosition1.x = cast(int)(topLeft.x * cast(float)parentSize.x) + parent1.x + location.x; 131 tempPosition1.y = cast(int)(topLeft.y * cast(float)parentSize.y) + parent1.y + location.y; 132 tempPosition2.x = parent2.x - cast(int)((1.0 - bottomRight.x) * cast(float)parentSize.x) + location.x; 133 tempPosition2.y = parent2.y - cast(int)((1.0 - bottomRight.y) * cast(float)parentSize.y) + location.y; 134 135 //All of the below is for not going out of boundry 136 if(parent is null) goto setx; 137 bool xfailed; 138 if(tempPosition1.x < parent.position1.x){ 139 xfailed = true; 140 goto afterx; 141 } 142 if(tempPosition2.x > parent.position2.x){ 143 xfailed = true; 144 goto afterx; 145 } 146 147 setx: 148 position1.x = tempPosition1.x; 149 position2.x = tempPosition2.x; 150 if(parent is null) goto sety; 151 152 afterx: 153 if(tempPosition1.y < parent.position1.y){ 154 if(xfailed) return; //subs are not calculated and the window has not been resized 155 else goto aftery; 156 } 157 if(tempPosition2.y > parent.position2.y){ 158 if(xfailed) return; //subs are not calculated and the window has not ben resized 159 else goto aftery; 160 } 161 162 sety: 163 position1.y = tempPosition1.y; 164 position2.y = tempPosition2.y; 165 166 aftery: 167 if(onResize)onResize(this); 116 168 117 169 foreach(sub ;this.subs) 118 sub.recalculate( size.x, size.y);170 sub.recalculate(position1, position2); 119 171 } 120 172 121 173 void recalculate(){ 122 if(parent ==null)123 recalculate( Device.width, Device.height);174 if(parent is null) 175 recalculate(Vec2i(0,0), Vec2i(Device.width, Device.height)); 124 176 else 125 recalculate(parent.size.x, parent.size.y); 126 } 127 128 void recalculateTexture(){ 177 recalculate(parent.position1, parent.position2); 178 } 179 180 void moveAdd(Vec2i add){ 181 location.x -= add.x; 182 location.y -= add.y; 183 recalculate(); 184 } 185 186 //I would like textures to automatically do this so that it doesn't need to happen for every single surface on every single frame 187 void recalculateTexture(){ //Dunno if this will be needed when we change to materials 129 188 portion.x = texture.requested_width/cast(float)texture.getWidth(); 130 189 portion.y = texture.requested_height/cast(float)texture.getHeight(); … … 147 206 glDisable(GL_LIGHTING); 148 207 208 glEnable(GL_BLEND); 209 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 210 //glColor4f(1, 1, 1, 1); 211 212 149 213 glEnable(GL_TEXTURE_2D); 150 214 … … 167 231 168 232 glBegin(GL_QUADS); 169 170 glTexCoord2f(0, 0);171 glVertex2i(position1.x, position2.y);172 233 173 glTexCoord2f(portion.x, 0); 174 glVertex2i(position2.x, position2.y); 175 176 glTexCoord2f(portion.x, portion.y); 177 glVertex2i(position2.x, position1.y); 178 179 glTexCoord2f(0, portion.y); 180 glVertex2i(position1.x, position1.y); 181 234 switch(fill){ 235 case traditional: 236 glTexCoord2f(0, 0); 237 glVertex2i(position1.x, position2.y); 238 239 glTexCoord2f(portion.x, 0); 240 glVertex2i(position2.x, position2.y); 241 242 glTexCoord2f(portion.x, portion.y); 243 glVertex2i(position2.x, position1.y); 244 245 glTexCoord2f(0, portion.y); 246 glVertex2i(position1.x, position1.y); 247 break; 248 case stretched: //Move calculations somwhere else! 249 //Not sure about the difference between /3 and *third, but who cares, I used *third 250 float partx = portion.x * third; 251 float party = portion.y * third; 252 253 float partytimes2 = party * 2; 254 float partxtimes2 = partx * 2; 255 256 int w = cast(int)(texture.requested_width * third); 257 int h = cast(int)(texture.requested_height * third); 258 259 int partwidth = position1.x + w; 260 int partheight = position1.y + h; 261 262 int partwidthminus = position2.x - w; 263 int partheightminus = position2.y - h; 264 265 /* 266 * topleft 267 */ 268 glTexCoord2f(0, party); 269 glVertex2i(position1.x, partheight); 270 271 glTexCoord2f(partx, party); 272 glVertex2i(partwidth, partheight); 273 274 glTexCoord2f(partx, 0); 275 glVertex2i(partwidth, position1.y); 276 277 glTexCoord2f(0, 0); 278 glVertex2i(position1.x, position1.y); 279 280 281 /* 282 * left 283 */ 284 glTexCoord2f(0, partytimes2); 285 glVertex2i(position1.x, partheightminus); 286 287 glTexCoord2f(partx, partytimes2); 288 glVertex2i(partwidth, partheightminus); 289 290 glTexCoord2f(partx, party); 291 glVertex2i(partwidth, partheight); 292 293 glTexCoord2f(0, party); 294 glVertex2i(position1.x, partheight); 295 296 297 /* 298 * bottomleft 299 */ 300 glTexCoord2f(0, portion.y); 301 glVertex2i(position1.x, position2.y); 302 303 glTexCoord2f(partx, portion.y); 304 glVertex2i(partwidth, position2.y); 305 306 glTexCoord2f(partx, partytimes2); 307 glVertex2i(partwidth, partheightminus); 308 309 glTexCoord2f(0, partytimes2); 310 glVertex2i(position1.x, partheightminus); 311 312 313 /* 314 * top 315 */ 316 glTexCoord2f(partx, party); 317 glVertex2i(partwidth, partheight); 318 319 glTexCoord2f(partxtimes2, party); 320 glVertex2i(partwidthminus, partheight); 321 322 glTexCoord2f(partxtimes2, 0); 323 glVertex2i(partwidthminus, position1.y); 324 325 glTexCoord2f(partx, 0); 326 glVertex2i(partwidth, position1.y); 327 328 329 /* 330 * middle 331 */ 332 glTexCoord2f(partx, partytimes2); 333 glVertex2i(partwidth, partheightminus); 334 335 glTexCoord2f(partxtimes2, partytimes2); 336 glVertex2i(partwidthminus, partheightminus); 337 338 glTexCoord2f(partxtimes2, party); 339 glVertex2i(partwidthminus, partheight); 340 341 glTexCoord2f(partx, party); 342 glVertex2i(partwidth, partheight); 343 344 345 /* 346 * bottom 347 */ 348 glTexCoord2f(partx, portion.y); 349 glVertex2i(partwidth, position2.y); 350 351 glTexCoord2f(partxtimes2, portion.y); 352 glVertex2i(partwidthminus, position2.y); 353 354 glTexCoord2f(partxtimes2, partytimes2); 355 glVertex2i(partwidthminus, partheightminus); 356 357 glTexCoord2f(partx, partytimes2); 358 glVertex2i(partwidth, partheightminus); 359 360 361 /* 362 * topright 363 */ 364 glTexCoord2f(partxtimes2, party); 365 glVertex2i(partwidthminus, partheight); 366 367 glTexCoord2f(portion.x, party); 368 glVertex2i(position2.x, partheight); 369 370 glTexCoord2f(portion.x, 0); 371 glVertex2i(position2.x, position1.y); 372 373 glTexCoord2f(partxtimes2, 0); 374 glVertex2i(partwidthminus, position1.y); 375 376 377 /* 378 * right 379 */ 380 glTexCoord2f(partxtimes2, partytimes2); 381 glVertex2i(partwidthminus, partheightminus); 382 383 glTexCoord2f(portion.x, partytimes2); 384 glVertex2i(position2.x, partheightminus); 385 386 glTexCoord2f(portion.x, party); 387 glVertex2i(position2.x, partheight); 388 389 glTexCoord2f(partxtimes2, party); 390 glVertex2i(partwidthminus, partheight); 391 392 /* 393 * bottomright 394 */ 395 glTexCoord2f(partxtimes2, portion.y); 396 glVertex2i(partwidthminus, position2.y); 397 398 glTexCoord2f(portion.x, portion.y); 399 glVertex2i(position2.x, position2.y); 400 401 glTexCoord2f(portion.x, partytimes2); 402 glVertex2i(position2.x, partheightminus); 403 404 glTexCoord2f(partxtimes2, partytimes2); 405 glVertex2i(partwidthminus, partheightminus); 406 break; 407 case tiled: 408 409 break; 410 default: 411 writefln("Not a valid fill type"); 412 //assert(0) 413 break; 414 } 182 415 glEnd(); 183 416 } 184 417 418 //I am clueless about this, so it's commented 185 419 // Sort subs 186 if (!subs.ordered(true, (Surface s){return s.style.zIndex;} ))187 subs.radixSort((Surface s){return s.style.zIndex;} );420 //if (!subs.ordered(true, (Surface s){return s.style.zIndex;} )) 421 // subs.radixSort((Surface s){return s.style.zIndex;} ); 188 422 189 423 foreach(sub; subs) … … 201 435 for(; index < Device.subs.length - 1; index++) 202 436 Device.subs[index] = Device.subs[index+1]; 203 Device.subs[$ ] = this;437 Device.subs[$-1] = this; 204 438 } 205 439 else{ … … 209 443 parent.subs[$-1] = this; 210 444 } 211 } 445 if(onFocus) onFocus(this); 446 } 212 447 } 213 448 449 //Perhaps put into yage.system.input 450 //Could be better, a method perhaps... 214 451 Surface findSurface(int x, int y){ 452 if(Input.surfaceLock) return Input.surfaceLock; 215 453 foreach_reverse(sub; Device.subs){ 216 454 if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ … … 220 458 return null; 221 459 } 222 460 //Could be better, a method perhaps... 223 461 Surface findSurface(Surface surface,int x, int y){ 224 462 foreach_reverse(sub; surface.subs){ … … 234 472 if(current == surface) return index; 235 473 } 236 return 1 << 8; 474 return 1 << 8; //Implement this for not in subs 237 475 } trunk/src/yage/resource/texture.d
r39 r45 318 318 319 319 } 320 if(this.requested_width == 0) this.requested_width = this.getWidth(); 321 if(this.requested_height == 0) this.requested_height = this.getHeight(); 320 322 } 321 323 trunk/src/yage/system/device.d
r43 r45 18 18 import derelict.ogg.vorbis; 19 19 import derelict.ogg.vorbisfile; 20 import yage.resource.texture;20 //import yage.resource.texture; 21 21 import yage.gui.surface; 22 22 import yage.system.log; 23 23 import yage.system.constant; 24 24 import yage.system.input; 25 import yage.core.vector; 25 26 26 27 // Enable specular highlights with textures. … … 321 322 height = _height; 322 323 323 foreach(sub ;this.subs) sub.recalculate( width, height);324 foreach(sub ;this.subs) sub.recalculate(Vec2i(0, 0), Vec2i(width, height)); 324 325 325 326 // For some reason, SDL Linux requires a call to SDL_SetVideoMode for a screen resize that's trunk/src/yage/system/input.d
r40 r45 26 26 static int mousex, mousey; /// The current pixel location of the mouse cursor; (0, 0) is top left. 27 27 static int mousedx, mousedy; /// The number of pixels the mouse has moved since the last time input was queried. 28 28 29 //I do not know if this does the same as moused, I was too lazy to mod it for my needs. 30 static int xdiff, ydiff; 31 29 32 /// A structure to track various state variables associated with each mouse button. 30 33 struct Buttons … … 41 44 static bool exit = false; /// A termination request has been received. 42 45 43 46 static Surface surfaceLock; 44 47 45 48 /** This function fills the above fields with the current intput data. … … 79 82 80 83 auto surface = findSurface(mousex, mousey); 81 if( !(surface is null)) surface.mousedown(event.button.button, Vec2i(mousex,mousey));84 if(surface !is null) surface.mousedown(event.button.button, Vec2i(mousex,mousey)); 82 85 83 86 break; … … 89 92 90 93 auto surface = findSurface(mousex, mousey); 91 if( !(surface is null)) surface.mouseup(event.button.button, Vec2i(mousex,mousey));94 if(surface !is null) surface.mouseup(event.button.button, Vec2i(mousex,mousey)); 92 95 93 96 break; … … 99 102 mousedx += event.motion.xrel; // these seem to behave differently on linux 100 103 mousedy += event.motion.yrel; // than on win32. Testing should be done. 104 105 xdiff = mousex - event.motion.x; 106 ydiff = mousey - event.motion.y; 107 101 108 mousex = event.motion.x; 102 109 mousey = event.motion.y; 103 110 104 //auto surface = findSurface(mousex, mousey);105 //if(!(surface is null)) surface.mousemove(event.button.button, Vec2i(mousex,mousey));111 auto surface = findSurface(mousex, mousey); 112 if(surface !is null) surface.mousemove(event.button.button, Vec2i(xdiff,ydiff)); 106 113 break; 107 114 … … 147 154 { return grabbed; 148 155 } 156 157 static void setSurfaceLock(Surface lock){ 158 surfaceLock = lock; 159 } 160 static void unlockSurface(){ 161 surfaceLock = null; 162 } 149 163 }
