Changeset 46
- Timestamp:
- 07/13/07 00:26:29 (1 year ago)
- Files:
-
- trunk/src/demo1/main.d (modified) (1 diff)
- trunk/src/demo2/main.d (modified) (2 diffs)
- trunk/src/yage/gui/surface.d (modified) (4 diffs)
- trunk/src/yage/system/device.d (modified) (6 diffs)
- trunk/src/yage/system/input.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/demo1/main.d
r43 r46 62 62 disp.bottomRight = Vec2f(1, 1); 63 63 disp.setVisibility(true); 64 disp. texture = camera.getTexture();64 disp.setTexture(camera.getTexture()); 65 65 66 66 trunk/src/demo2/main.d
r45 r46 128 128 void onMousedown2(Surface self, byte buttons, Vec2i coordinates){ 129 129 self.raise(); 130 Input.setSurfaceLock(self);130 self.startDrag(); 131 131 } 132 132 void onMouseup2(Surface self, byte buttons, Vec2i coordinates){ 133 Input.unlockSurface();133 self.endDrag(); 134 134 } 135 135 void onMousemove(Surface self, byte buttons, Vec2i coordinates){ 136 if(buttons == 1) self. moveAdd(coordinates);136 if(buttons == 1) self.drag(coordinates); 137 137 } 138 138 … … 227 227 // Cap framerate 228 228 //if (dtime < 1/60.0) 229 std.c.time.usleep(cast(uint)(1000));229 // std.c.time.usleep(cast(uint)(1000)); 230 230 scene.swapTransformRead(); 231 231 } trunk/src/yage/gui/surface.d
r45 r46 47 47 Vec2f bottomRight; 48 48 49 //these are used for rendering, not for user49 //these are calculated, not for calculating 50 50 Vec2i position1; 51 51 Vec2i position2; 52 53 Vec2i location; 52 Vec2i size; 53 54 //these are for calculating 55 private Vec2i locationAdd; 54 56 55 57 //used for the texture … … 121 123 } 122 124 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; 125 void recalculate(Vec2i parent1, Vec2i parent2, Vec2i parentSize, bool doSubs = true){ //not done 126 127 position1.x = cast(int)(topLeft.x * cast(float)parentSize.x) + parent1.x + locationAdd.x; 128 position1.y = cast(int)(topLeft.y * cast(float)parentSize.y) + parent1.y + locationAdd.y; 129 130 position2.x = parent2.x - cast(int)((1.0 - bottomRight.x) * cast(float)parentSize.x) + locationAdd.x; 131 position2.y = parent2.y - cast(int)((1.0 - bottomRight.y) * cast(float)parentSize.y) + locationAdd.y; 132 133 size.x = position2.x - position1.x; 134 size.y = position2.y - position1.y; 135 136 if(onResize)onResize(this); 137 138 if(doSubs) 139 recalculateSubs(); 140 } 141 142 private void recalculateSubs(){ 143 foreach(sub ;this.subs) 144 sub.recalculate(position1, position2, size); 145 } 146 147 void recalculate(bool doSubs = true){ 148 if(parent is null){ 149 recalculate(Vec2i(0,0), Device.size, Device.size, doSubs); 150 } 151 else 152 recalculate(parent.position1, parent.position2, parent.size, doSubs); 153 } 154 155 void startDrag(){ 156 Input.setSurfaceLock(this); 157 } 158 159 void drag(Vec2i add){ 160 locationAdd.x -= add.x; 161 locationAdd.y -= add.y; 162 163 164 recalculate(false); 165 134 166 135 167 //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); 168 169 foreach(sub ;this.subs) 170 sub.recalculate(position1, position2); 171 } 172 173 void recalculate(){ 174 if(parent is null) 175 recalculate(Vec2i(0,0), Vec2i(Device.width, Device.height)); 176 else 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(); 168 if(parent is null){ 169 recalculateSubs(); 170 return; 171 } 172 173 if(position1.x < parent.position1.x){ 174 position1.x = parent.position1.x; 175 position2.x = position1.x + size.x; 176 } 177 else if(position2.x > parent.position2.x){ 178 position2.x = parent.position2.x; 179 position1.x = position2.x - size.x; 180 } 181 182 if(position1.y < parent.position1.y){ 183 position1.y = parent.position1.y; 184 position2.y = position1.y + size.y; 185 } 186 else if(position2.y > parent.position2.y){ 187 position2.y = parent.position2.y; 188 position1.y = position2.y - size.y; 189 } 190 191 recalculateSubs(); 192 } 193 194 void endDrag(){ 195 recalculate(false); 196 if(parent is null) goto after; 197 198 199 if(position1.x < parent.position1.x) 200 locationAdd.x += parent.position1.x - position1.x; 201 else if(position2.x > parent.position2.x) 202 locationAdd.x -= position2.x - parent.position2.x; 203 204 205 if(position1.y < parent.position1.y) 206 locationAdd.y += parent.position1.y - position1.y; 207 else if(position2.y > parent.position2.y) 208 locationAdd.y -= position2.y - parent.position2.y; 209 210 recalculate(false); 211 after: 212 Input.unlockSurface(); 184 213 } 185 214 … … 196 225 // with dimensions 0..width, 0..height 197 226 // with 0,0 being at the top left. 198 glViewport(0, 0, Device. width, Device.height);227 glViewport(0, 0, Device.size.x, Device.size.y); 199 228 glMatrixMode(GL_PROJECTION); 200 229 glLoadIdentity(); 201 glOrtho(0, Device. width, Device.height, 0, -1, 1);230 glOrtho(0, Device.size.x, Device.size.y, 0, -1, 1); 202 231 glMatrixMode(GL_MODELVIEW); 203 232 glLoadIdentity(); … … 450 479 //Could be better, a method perhaps... 451 480 Surface findSurface(int x, int y){ 452 if(Input.surfaceLock) return Input.surfaceLock;453 481 foreach_reverse(sub; Device.subs){ 454 482 if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ trunk/src/yage/system/device.d
r45 r46 38 38 // Video 39 39 static SDL_Surface* sdl_surface; // Holds a reference to the main (and only) SDL surface 40 static uint width; // The width of the window. 41 static uint height; // The heght of the window.40 41 static Vec2i size; // The width/height of the window. 42 42 static uint viewport_width; // The width of the current viewport 43 43 static uint viewport_height;// The height of the current viewport … … 87 87 body 88 88 { 89 this. width= width;90 this. height= height;89 this.size.x = width; 90 this.size.y= height; 91 91 this.depth = depth; 92 92 this.fullscreen = fullscreen; … … 115 115 uint flags = SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE | SDL_HWPALETTE | SDL_HWACCEL; 116 116 if (fullscreen) flags |= SDL_FULLSCREEN; 117 sdl_surface = SDL_SetVideoMode( width, height, depth, flags);117 sdl_surface = SDL_SetVideoMode(size.x, size.y, depth, flags); 118 118 if(sdl_surface is null) 119 throw new Exception ("Unable to set " ~ .toString( width) ~ "x" ~ .toString(height) ~119 throw new Exception ("Unable to set " ~ .toString(size.x) ~ "x" ~ .toString(size.y) ~ 120 120 " video mode: : " ~ .toString(SDL_GetError())); 121 121 SDL_LockSurface(sdl_surface); … … 277 277 /// Return the aspect ratio (width/height) of the rendering window. 278 278 static float getAspectRatio() 279 { if ( height==0) height=1;280 return width/cast(float)height;279 { if (size.y==0) size.y=1; 280 return size.x/cast(float)size.x; 281 281 } 282 282 283 283 /// Return the current width of the window in pixels. 284 284 static uint getWidth() 285 { return width;285 { return size.x; 286 286 } 287 287 /// return the current height of the window in pixels. 288 288 static uint getHeight() 289 { return height;289 { return size.y; 290 290 } 291 291 … … 297 297 298 298 // special values of 0 means stretch to window size 299 if (viewport_width ==0) viewport_width = width;300 if (viewport_height==0) viewport_height = height;299 if (viewport_width ==0) viewport_width = size.x; 300 if (viewport_height==0) viewport_height = size.y; 301 301 302 302 // Ensure our new resolution is less than the window size 303 303 // This might no longer be an issue once framebufferobjects are used. 304 if (viewport_width > width) viewport_width = width;305 if (viewport_height > height) viewport_height = height;304 if (viewport_width > size.x) viewport_width = size.x; 305 if (viewport_height > size.y) viewport_height = size.y; 306 306 307 307 glViewport(0, 0, viewport_width, viewport_height); … … 318 318 /** Stores the dimensions of the current window size. 319 319 * This is called by a resize event in Input.checkInput(). */ 320 static void resizeWindow(int _width, int _height)321 { width = _width;322 height = _height;320 static void resizeWindow(int width, int height) 321 { size.x = width; 322 size.y = height; 323 323 324 foreach(sub ;this.subs) sub.recalculate(Vec2i(0, 0), Vec2i(width, height));324 foreach(sub ;this.subs) sub.recalculate(Vec2i(0, 0), size, size); 325 325 326 326 // For some reason, SDL Linux requires a call to SDL_SetVideoMode for a screen resize that's trunk/src/yage/system/input.d
r45 r46 81 81 button[event.button.button].ydown = mousey; 82 82 83 auto surface = findSurface(mousex, mousey); 83 auto surface = currentSurface(); 84 84 85 if(surface !is null) surface.mousedown(event.button.button, Vec2i(mousex,mousey)); 85 86 … … 91 92 button[event.button.button].yup = mousey; 92 93 93 auto surface = findSurface(mousex, mousey); 94 auto surface = currentSurface(); 95 94 96 if(surface !is null) surface.mouseup(event.button.button, Vec2i(mousex,mousey)); 95 97 … … 109 111 mousey = event.motion.y; 110 112 111 auto surface = findSurface(mousex, mousey); 113 auto surface = currentSurface(); 114 112 115 if(surface !is null) surface.mousemove(event.button.button, Vec2i(xdiff,ydiff)); 113 116 break; … … 161 164 surfaceLock = null; 162 165 } 166 167 static Surface currentSurface(){ 168 if(surfaceLock) return surfaceLock; 169 return findSurface(mousex, mousey); 170 } 163 171 }
