Pie Noon
An open source project by FPL.
 All Classes Pages
touchscreen_button.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TOUCHSCREEN_BUTTON_H
16 #define TOUCHSCREEN_BUTTON_H
17 
18 #include "common.h"
19 #include "config_generated.h"
20 #include "pie_noon_common_generated.h"
21 #include "precompiled.h"
22 
23 namespace fpl {
24 namespace pie_noon {
25 
27  public:
29 
30  void AdvanceFrame(WorldTime delta_time, fplbase::InputSystem* input,
31  vec2 window_size);
32 
33  // bool HandlePointer(Pointer pointer, vec2 window_size);
34  void Render(fplbase::Renderer& renderer);
35  void AdvanceFrame(WorldTime delta_time);
36  ButtonId GetId() const;
37  bool WillCapturePointer(const fplbase::InputPointer& pointer,
38  vec2 window_size);
39  bool IsTriggered();
40 
41  fplbase::Button& button() { return button_; }
42 
43  const std::vector<fplbase::Material*>& up_materials() const {
44  return up_materials_;
45  }
46  void set_up_material(size_t i, fplbase::Material* up_material) {
47  assert(up_material);
48  if (i >= up_materials_.size()) up_materials_.resize(i + 1);
49  up_materials_[i] = up_material;
50  }
51  void set_current_up_material(size_t which) {
52  assert(which < up_materials_.size());
53  up_current_ = which;
54  }
55 
56  fplbase::Material* down_material() const { return down_material_; }
57  void set_down_material(fplbase::Material* down_material) {
58  down_material_ = down_material;
59  }
60 
61  mathfu::vec2 up_offset() const { return mathfu::vec2(up_offset_); }
62  void set_up_offset(mathfu::vec2 up_offset) { up_offset_ = up_offset; }
63 
64  mathfu::vec2 down_offset() const { return mathfu::vec2(down_offset_); }
65  void set_down_offset(mathfu::vec2 down_offset) { down_offset_ = down_offset; }
66 
67  const ButtonDef* button_def() const { return button_def_; }
68  void set_button_def(const ButtonDef* button_def) { button_def_ = button_def; }
69 
70  fplbase::Shader* inactive_shader() const { return inactive_shader_; }
71  void set_inactive_shader(fplbase::Shader* inactive_shader) {
72  inactive_shader_ = inactive_shader;
73  }
74 
75  fplbase::Shader* shader() const { return shader_; }
76  void set_shader(fplbase::Shader* shader) { shader_ = shader; }
77  fplbase::Shader* debug_shader() const { return debug_shader_; }
78  void set_debug_shader(fplbase::Shader* shader) { debug_shader_ = shader; }
79  void DebugRender(const vec3& position, const vec3& texture_size,
80  fplbase::Renderer& renderer) const;
81  void set_draw_bounds(bool enable) { draw_bounds_ = enable; };
82 
83  bool is_active() const { return is_active_; }
84  void set_is_active(bool is_active) { is_active_ = is_active; }
85 
86  bool is_visible() const { return is_visible_; }
87  void set_is_visible(bool is_visible) { is_visible_ = is_visible; }
88 
89  bool is_highlighted() const { return is_highlighted_; }
90  void set_is_highlighted(bool is_highlighted) {
91  is_highlighted_ = is_highlighted;
92  }
93 
94  void set_color(const mathfu::vec4& color) { color_ = color; }
95  mathfu::vec4 color() { return mathfu::vec4(color_); }
96 
97  void SetCannonicalWindowHeight(int height) {
98  one_over_cannonical_window_height_ = 1.0f / static_cast<float>(height);
99  }
100 
101  private:
102  fplbase::Button button_;
103  WorldTime elapsed_time_;
104 
105  const ButtonDef* button_def_;
106  fplbase::Shader* shader_;
107  fplbase::Shader* inactive_shader_;
108  fplbase::Shader* debug_shader_;
109 
110  // Textures to draw for the up/down states:
111  std::vector<fplbase::Material*> up_materials_;
112  size_t up_current_;
113 
114  fplbase::Material* down_material_;
115 
116  // Allow overriding the default color in code.
117  mathfu::vec4_packed color_;
118 
119  // Offsets to draw the textures at,
120  mathfu::vec2_packed up_offset_;
121  mathfu::vec2_packed down_offset_;
122 
123  bool is_active_;
124  bool is_visible_;
125  bool is_highlighted_;
126  bool draw_bounds_;
127 
128  // Scale the textures by the y-axis so that they are (proportionally)
129  // the same height on every platform.
130  float one_over_cannonical_window_height_;
131 };
132 
133 class StaticImage {
134  public:
135  StaticImage();
136  void Initialize(const StaticImageDef& image_def,
137  std::vector<fplbase::Material*> materials,
138  fplbase::Shader* shader,
139  int cannonical_window_height);
140  void Render(fplbase::Renderer& renderer);
141  bool Valid() const;
142  ButtonId GetId() const {
143  return image_def_ == nullptr ? ButtonId_Undefined : image_def_->ID();
144  }
145  const StaticImageDef* image_def() const { return image_def_; }
146  mathfu::vec2 scale() const { return mathfu::vec2(scale_); }
147  void set_scale(const mathfu::vec2& scale) { scale_ = scale; }
148  void set_current_material_index(int i) { current_material_index_ = i; }
149 
150  void set_is_visible(bool b) { is_visible_ = b; }
151  bool is_visible() { return is_visible_; }
152 
153  void set_color(const mathfu::vec4& color) { color_ = color; }
154  mathfu::vec4 color() { return mathfu::vec4(color_); }
155 
156  // Set the image position on screen, expressed as a fraction of the screen
157  // dimensions to place the center point.
158  void set_texture_position(const mathfu::vec2& position) {
159  texture_position_ = position;
160  }
161  mathfu::vec2 texture_position() { return mathfu::vec2(texture_position_); }
162 
163  private:
164  // Flatbuffer's definition of this image.
165  const StaticImageDef* image_def_;
166 
167  // A list of materials that can be drawn. Choose current material with
168  // set_current_material_index.
169  std::vector<fplbase::Material*> materials_;
170 
171  // The material that is currently being displayed.
172  int current_material_index_;
173 
174  // The shader used to render the material.
175  fplbase::Shader* shader_;
176 
177  // Draw image bigger or smaller. (1.0f, 1.0f) means no scaling.
178  mathfu::vec2_packed scale_;
179 
180  // Where to display the texture on screen.
181  mathfu::vec2_packed texture_position_;
182 
183  // Allow overriding the default color in code.
184  mathfu::vec4_packed color_;
185 
186  // Scale the textures by the y-axis so that they are (proportionally)
187  // the same height on every platform.
188  float one_over_cannonical_window_height_;
189 
190  bool is_visible_;
191 };
192 
193 } // pie_noon
194 } // fpl
195 
196 #endif // TOUCHSCREEN_BUTTON_H
Definition: touchscreen_button.h:26
Definition: touchscreen_button.h:133