ezgl  1.0.1
An Easy Graphics & GUI Library
camera.hpp
1 /*
2  * Copyright 2019-2022 University of Toronto
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Authors: Mario Badr, Sameh Attia, Tanner Young-Schultz and Vaughn Betz
17  */
18 
19 #ifndef EZGL_CAMERA_HPP
20 #define EZGL_CAMERA_HPP
21 
22 #include "ezgl/point.hpp"
23 #include "ezgl/rectangle.hpp"
24 
25 namespace ezgl {
26 
40 class camera {
41 public:
45  point2d world_to_screen(point2d world_coordinates) const;
46 
50  point2d widget_to_screen(point2d widget_coordinates) const;
51 
55  point2d widget_to_world(point2d widget_coordinates) const;
56 
61  {
62  return m_world;
63  }
64 
69  {
70  return m_screen;
71  }
72 
77  {
78  return m_widget;
79  }
80 
85  {
86  return m_initial_world;
87  }
88 
94  void set_world(rectangle new_world);
95 
101  void reset_world(rectangle new_world);
102 
107  {
108  return m_screen_to_world;
109  }
110 
111 protected:
112  // Only an ezgl::canvas can create a camera.
113  friend class canvas;
114 
120  explicit camera(rectangle bounds);
121 
130  void update_widget(int width, int height);
131 
136 
137 private:
138  // The dimensions of the parent widget.
139  rectangle m_widget = {{0, 0}, 1.0, 1.0};
140 
141  // The dimensions of the world (user-defined bounding box).
142  rectangle m_world;
143 
144  // The dimensions of the screen, which may not match the widget.
145  rectangle m_screen;
146 
147  // The dimensions of the initial world (user-defined bounding box). Needed for zoom_fit
148  rectangle m_initial_world;
149 
150  // The x and y scaling factors.
151  point2d m_world_to_widget = {1.0, 1.0};
152  point2d m_widget_to_screen = {1.0, 1.0};
153  point2d m_screen_to_world = {1.0, 1.0};
154 };
155 }
156 
157 #endif //EZGL_CAMERA_HPP
Manages the transformations between coordinate systems.
Definition: camera.hpp:40
void update_widget(int width, int height)
Update the dimensions of the widget.
void set_world(rectangle new_world)
Update the visible bounds of the world.
point2d widget_to_world(point2d widget_coordinates) const
Convert a point in widget coordinates to world coordinates.
void reset_world(rectangle new_world)
Reset the world coordinates.
camera(rectangle bounds)
Create a camera.
rectangle get_screen() const
Get the dimensions of the screen.
Definition: camera.hpp:68
rectangle get_widget() const
Get the dimensions of the widget.
Definition: camera.hpp:76
rectangle get_initial_world() const
Get the initial bounds of the world.
Definition: camera.hpp:84
point2d widget_to_screen(point2d widget_coordinates) const
Convert a point in widget coordinates to screen coordinates.
point2d world_to_screen(point2d world_coordinates) const
Convert a point in world coordinates to screen coordinates.
void update_scale_factors()
Update the scaling factors.
rectangle get_world() const
Get the currently visible bounds of the world.
Definition: camera.hpp:60
point2d get_world_scale_factor() const
Get the screen to world scaling factor.
Definition: camera.hpp:106
Responsible for creating, destroying, and maintaining the rendering context of a GtkWidget.
Definition: canvas.hpp:54
Represents a two-dimensional point.
Definition: point.hpp:27
Represents a rectangle as two diagonally opposite points.
Definition: rectangle.hpp:31
A library for creating a graphical user interface.
Definition: application.hpp:40