pick up and fix G0gg's C++ pixel2d changes

This commit is contained in:
rdb 2012-01-22 13:32:17 +00:00
parent 9dcc7a5e35
commit fa50a04775
3 changed files with 59 additions and 13 deletions

View File

@ -1527,7 +1527,7 @@ event_window_event(const Event *event, void *data) {
// Adjust aspect ratio.
for (int n = 0; n < (int)self->_windows.size(); n++) {
if (self->_windows[n]->get_graphics_output() == win) {
return self->_windows[n]->adjust_aspect_ratio();
self->_windows[n]->adjust_dimensions();
}
}
}

View File

@ -348,6 +348,37 @@ get_aspect_2d() {
return _aspect_2d;
}
////////////////////////////////////////////////////////////////////
// Function: WindowFramework::get_pixel_2d
// Access: Public
// Description: Returns a special root that uses units in pixels that
// are relative to the window. The upperleft corner of
// the window is (0, 0), the lowerleft corner is
// (xsize, -ysize), in this coordinate system.
////////////////////////////////////////////////////////////////////
NodePath WindowFramework::
get_pixel_2d() {
if (_pixel_2d.is_empty()) {
PGTop *top = new PGTop("pixel_2d");
_pixel_2d = get_render_2d().attach_new_node(top);
_pixel_2d.set_pos(-1, 0, 1);
if (_window->has_size()) {
int x_size = _window->get_sbs_left_x_size();
int y_size = _window->get_sbs_left_y_size();
if (x_size > 0){
_pixel_2d.set_sx(2.0f / (float)x_size);
}
_pixel_2d.set_sy(1.0f);
if (y_size > 0){
_pixel_2d.set_sz(2.0f / (float)y_size);
}
}
}
return _pixel_2d;
}
////////////////////////////////////////////////////////////////////
// Function: WindowFramework::get_mouse
// Access: Public
@ -796,28 +827,30 @@ set_anim_controls(bool enable) {
}
////////////////////////////////////////////////////////////////////
// Function: WindowFramework::adjust_aspect_ratio
// Function: WindowFramework::adjust_dimensions
// Access: Public
// Description: Reevaluates the aspect ratio of the window,
// Description: Reevaluates the dimensions of the window,
// presumably after the window has been resized by the
// user or some other force. Adjusts the render film
// size and aspect2d scale as necessary according to the
// size, aspect2d scale (aspect ratio) and the
// dimensionsas of pixel_2d according to the
// new window shape, or new config setting.
////////////////////////////////////////////////////////////////////
void WindowFramework::
adjust_aspect_ratio() {
adjust_dimensions() {
PN_stdfloat this_aspect_ratio = aspect_ratio;
int x_size = 0, y_size = 0;
if (this_aspect_ratio == 0.0f) {
// An aspect ratio of 0.0 means to try to infer it.
this_aspect_ratio = 1.0f;
int x_size = 0, y_size = 0;
if (_window->has_size()) {
x_size = _window->get_sbs_left_x_size();
y_size = _window->get_sbs_left_y_size();
if (y_size != 0) {
this_aspect_ratio = (PN_stdfloat)x_size / (PN_stdfloat)y_size;
}
if (this_aspect_ratio == 0.0f) {
// An aspect ratio of 0.0 means to try to infer it.
this_aspect_ratio = 1.0f;
if (y_size != 0) {
this_aspect_ratio = (float)x_size / (float)y_size;
}
}
@ -825,6 +858,17 @@ adjust_aspect_ratio() {
_aspect_2d.set_scale(1.0f / this_aspect_ratio, 1.0f, 1.0f);
}
if (!_pixel_2d.is_empty()) {
// Adjust the pixel 2d scale
if (x_size > 0){
_pixel_2d.set_sx(2.0f / (float)x_size);
}
_pixel_2d.set_sy(1.0f);
if (y_size > 0){
_pixel_2d.set_sz(2.0f / (float)y_size);
}
}
Cameras::iterator ci;
for (ci = _cameras.begin(); ci != _cameras.end(); ++ci) {
Lens *lens = (*ci)->get_lens();

View File

@ -81,6 +81,7 @@ public:
NodePath get_render();
NodePath get_render_2d();
NodePath get_aspect_2d();
NodePath get_pixel_2d();
NodePath get_mouse();
NodePath get_button_thrower();
@ -101,7 +102,7 @@ public:
void next_anim_control();
void set_anim_controls(bool enable);
INLINE bool get_anim_controls() const;
void adjust_aspect_ratio();
void adjust_dimensions();
enum BackgroundType {
BT_other = 0,
@ -174,6 +175,7 @@ private:
NodePath _render;
NodePath _render_2d;
NodePath _aspect_2d;
NodePath _pixel_2d;
AnimControlCollection _anim_controls;
bool _anim_controls_enabled;